void HHWheelTimer::Callback::setScheduled(HHWheelTimer* wheel,
std::chrono::milliseconds timeout) {
assert(wheel_ == nullptr);
- assert(expiration_ == milliseconds(0));
+ assert(expiration_ == decltype(expiration_){});
wheel_ = wheel;
}
wheel_ = nullptr;
- expiration_ = milliseconds(0);
+ expiration_ = {};
}
HHWheelTimer::HHWheelTimer(
timeouts.pop_front();
count_--;
cb->wheel_ = nullptr;
- cb->expiration_ = milliseconds(0);
+ cb->expiration_ = {};
RequestContextScopeGuard rctx(cb->context_);
cb->timeoutExpired();
if (isDestroyed) {
}
int64_t HHWheelTimer::calcNextTick() {
- auto intervals =
- (getCurTime().count() - startTime_.count()) / interval_.count();
+ auto intervals = (getCurTime() - startTime_) / interval_;
// Slow eventbases will have skew between the actual time and the
// callback time. To avoid racing the next scheduleNextTimeout()
// call, always schedule new timeouts against the actual
: public boost::intrusive::list_base_hook<
boost::intrusive::link_mode<boost::intrusive::auto_unlink>> {
public:
- Callback()
- : wheel_(nullptr)
- , expiration_(0) {}
-
+ Callback() = default;
virtual ~Callback();
/**
* Don't override this unless you're doing a test. This is mainly here so
* that we can override it to simulate lag in steady_clock.
*/
- virtual std::chrono::milliseconds getCurTime() {
- return std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::steady_clock::now().time_since_epoch());
+ virtual std::chrono::steady_clock::time_point getCurTime() {
+ return std::chrono::steady_clock::now();
}
private:
// Get the time remaining until this timeout expires
std::chrono::milliseconds getTimeRemaining(
- std::chrono::milliseconds now) const {
+ std::chrono::steady_clock::time_point now) const {
if (now >= expiration_) {
return std::chrono::milliseconds(0);
}
- return expiration_ - now;
+ return std::chrono::duration_cast<std::chrono::milliseconds>(
+ expiration_ - now);
}
void setScheduled(HHWheelTimer* wheel,
std::chrono::milliseconds);
void cancelTimeoutImpl();
- HHWheelTimer* wheel_;
- std::chrono::milliseconds expiration_;
+ HHWheelTimer* wheel_{nullptr};
+ std::chrono::steady_clock::time_point expiration_{};
int bucket_{-1};
typedef boost::intrusive::list<
int64_t lastTick_;
int64_t expireTick_;
uint64_t count_;
- std::chrono::milliseconds startTime_;
+ std::chrono::steady_clock::time_point startTime_;
int64_t calcNextTick();
bool* processingCallbacksGuard_;
CallbackList timeouts; // Timeouts queued to run
- std::chrono::milliseconds getCurTime() {
- return std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::steady_clock::now().time_since_epoch());
+ std::chrono::steady_clock::time_point getCurTime() {
+ return std::chrono::steady_clock::now();
}
};
class TestTimeoutDelayed : public TestTimeout {
protected:
- std::chrono::milliseconds getCurTime() override {
- return std::chrono::duration_cast<std::chrono::milliseconds>(
- std::chrono::steady_clock::now().time_since_epoch()) -
- milliseconds(5);
- }
+ std::chrono::steady_clock::time_point getCurTime() override {
+ return std::chrono::steady_clock::now() - milliseconds(5);
+ }
};
struct HHWheelTimerTest : public ::testing::Test {