Ensure that Time::now never returns negative time.

This commit is contained in:
levlam 2020-11-21 20:33:15 +03:00
parent 375c638936
commit 77de5110f5

View File

@ -18,7 +18,13 @@ bool operator==(Timestamp a, Timestamp b) {
static std::atomic<double> time_diff;
double Time::now() {
return now_unadjusted() + time_diff.load(std::memory_order_relaxed);
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;
}
double Time::now_unadjusted() {