From 50ec7c3a53b3cfe83b5ac8fc42cc28127e113dda Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Thu, 4 Feb 2021 15:05:05 +0800 Subject: [PATCH] common/Timer: add SafeTimer::add_event_at(real_clock::time_point) so we can schedule an event with real_clock::time_point. timer should wait using a mono_time, but user might want to pass a real_clock::time_point to it when, for instance, scheduling a task which should be performed in future at a specified time. in this case, we need to convert the real_clock::time_point to a "known" time_point. Signed-off-by: Kefu Chai --- src/common/Timer.cc | 12 ++++++++++++ src/common/Timer.h | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/common/Timer.cc b/src/common/Timer.cc index eab46661c99e1..17547962086e6 100644 --- a/src/common/Timer.cc +++ b/src/common/Timer.cc @@ -153,6 +153,18 @@ Context* SafeTimer::add_event_at(SafeTimer::clock_t::time_point when, Context *c return callback; } +Context* SafeTimer::add_event_at(ceph::real_clock::time_point when, Context *callback) +{ + ceph_assert(ceph_mutex_is_locked(lock)); + // convert from real_clock to mono_clock + auto mono_now = ceph::mono_clock::now(); + auto real_now = ceph::real_clock::now(); + const auto delta = when - real_now; + const auto mono_atime = (mono_now + + std::chrono::ceil(delta)); + return add_event_at(mono_atime, callback); +} + bool SafeTimer::cancel_event(Context *callback) { ceph_assert(ceph_mutex_is_locked(lock)); diff --git a/src/common/Timer.h b/src/common/Timer.h index f543be68a7664..32ca733b361e4 100644 --- a/src/common/Timer.h +++ b/src/common/Timer.h @@ -77,7 +77,7 @@ public: Context* add_event_after(ceph::timespan duration, Context *callback); Context* add_event_after(double seconds, Context *callback); Context* add_event_at(clock_t::time_point when, Context *callback); - + Context* add_event_at(ceph::real_clock::time_point when, Context *callback); /* Cancel an event. * Call with the event_lock LOCKED * -- 2.39.5