2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
#include <atomic>
|
2020-06-26 01:24:13 +02:00
|
|
|
#include <cmath>
|
2018-02-12 09:40:52 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
2018-02-12 09:40:52 +01:00
|
|
|
bool operator==(Timestamp a, Timestamp b) {
|
|
|
|
return std::abs(a.at() - b.at()) < 1e-6;
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
|
|
|
|
static std::atomic<double> time_diff;
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
double Time::now() {
|
2020-11-21 18:33:15 +01:00
|
|
|
auto result = now_unadjusted() + time_diff.load(std::memory_order_relaxed);
|
|
|
|
while (result < 0) {
|
|
|
|
auto old_time_diff = time_diff.load();
|
|
|
|
time_diff.compare_exchange_strong(old_time_diff, old_time_diff - result);
|
|
|
|
result = now_unadjusted() + time_diff.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
return result;
|
2020-06-24 13:47:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double Time::now_unadjusted() {
|
2019-07-06 13:29:15 +02:00
|
|
|
return Clocks::monotonic();
|
|
|
|
}
|
2018-02-12 09:40:52 +01:00
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
void Time::jump_in_future(double at) {
|
|
|
|
while (true) {
|
2020-06-26 01:24:13 +02:00
|
|
|
auto old_time_diff = time_diff.load();
|
2020-06-24 13:47:36 +02:00
|
|
|
auto diff = at - now();
|
|
|
|
if (diff < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (time_diff.compare_exchange_strong(old_time_diff, old_time_diff + diff)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|