2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
class Time {
|
|
|
|
public:
|
2019-07-06 13:29:15 +02:00
|
|
|
static double now();
|
2018-12-31 20:04:05 +01:00
|
|
|
static double now_cached() {
|
2019-07-06 13:29:15 +02:00
|
|
|
// Temporary(?) use now in now_cached
|
|
|
|
// Problem:
|
|
|
|
// thread A: check that now() > timestamp and notifies thread B
|
|
|
|
// thread B: must see that now() > timestamp()
|
|
|
|
//
|
|
|
|
// now() and now_cached() must be monotonic
|
|
|
|
//
|
|
|
|
// if a=now[_cached]() happens before b=now[_cached] than
|
|
|
|
// a <= b
|
|
|
|
//
|
|
|
|
// As an alternative we may say that now_cached is a thread local copy of now
|
|
|
|
return now();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-06-24 13:47:36 +02:00
|
|
|
static double now_unadjusted();
|
|
|
|
|
|
|
|
// Used for testing. After jump_in_future(at) is called, now() >= at.
|
|
|
|
static void jump_in_future(double at);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline void relax_timeout_at(double *timeout, double new_timeout) {
|
|
|
|
if (new_timeout == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (*timeout == 0 || new_timeout < *timeout) {
|
|
|
|
*timeout = new_timeout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Timestamp {
|
|
|
|
public:
|
|
|
|
Timestamp() = default;
|
|
|
|
static Timestamp never() {
|
|
|
|
return Timestamp{};
|
|
|
|
}
|
|
|
|
static Timestamp now() {
|
|
|
|
return Timestamp{Time::now()};
|
|
|
|
}
|
|
|
|
static Timestamp now_cached() {
|
|
|
|
return Timestamp{Time::now_cached()};
|
|
|
|
}
|
|
|
|
static Timestamp at(double timeout) {
|
|
|
|
return Timestamp{timeout};
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
static Timestamp in(double timeout, Timestamp now = now_cached()) {
|
2020-06-24 13:47:36 +02:00
|
|
|
return Timestamp{now.at() + timeout};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-06-26 01:24:13 +02:00
|
|
|
bool is_in_past(Timestamp now) const {
|
2020-06-24 13:47:36 +02:00
|
|
|
return at_ <= now.at();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
bool is_in_past() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
return is_in_past(now_cached());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2022-10-09 18:30:18 +02:00
|
|
|
explicit operator bool() const noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
return at_ > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double at() const {
|
|
|
|
return at_;
|
|
|
|
}
|
|
|
|
|
|
|
|
double in() const {
|
|
|
|
return at_ - Time::now_cached();
|
|
|
|
}
|
|
|
|
|
|
|
|
void relax(const Timestamp &timeout) {
|
|
|
|
if (!timeout) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*this || at_ > timeout.at_) {
|
|
|
|
at_ = timeout.at_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 09:40:52 +01:00
|
|
|
friend bool operator==(Timestamp a, Timestamp b);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
double at_{0};
|
|
|
|
|
|
|
|
explicit Timestamp(double timeout) : at_(timeout) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-24 13:47:36 +02:00
|
|
|
inline bool operator<(const Timestamp &a, const Timestamp &b) {
|
|
|
|
return a.at() < b.at();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|