From 740da72fbdecefebd858d5b29616789f778af635 Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Fri, 13 Feb 2009 13:28:24 -0800 Subject: [PATCH] SafeTimer: getting rid of global timer --- src/common/Timer.cc | 11 ++++++----- src/common/Timer.h | 12 ++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/common/Timer.cc b/src/common/Timer.cc index 9f927c82313..75b0de42101 100644 --- a/src/common/Timer.cc +++ b/src/common/Timer.cc @@ -218,7 +218,7 @@ void Timer::add_event_after(double seconds, { utime_t when = g_clock.now(); when += seconds; - add_event_at(when, callback); + Timer::add_event_at(when, callback); } void Timer::add_event_at(utime_t when, @@ -279,7 +279,7 @@ void SafeTimer::add_event_after(double seconds, Context *c) Context *w = new EventWrapper(this, c); dout(DBL) << "SafeTimer.add_event_after wrapping " << c << " with " << w << dendl; scheduled[c] = w; - g_timer.add_event_after(seconds, w); + Timer::add_event_after(seconds, w); } void SafeTimer::add_event_at(utime_t when, Context *c) @@ -288,7 +288,7 @@ void SafeTimer::add_event_at(utime_t when, Context *c) Context *w = new EventWrapper(this, c); dout(DBL) << "SafeTimer.add_event_at wrapping " << c << " with " << w << dendl; scheduled[c] = w; - g_timer.add_event_at(when, w); + Timer::add_event_at(when, w); } void SafeTimer::EventWrapper::finish(int r) @@ -316,12 +316,12 @@ void SafeTimer::EventWrapper::finish(int r) timer->lock.Unlock(); } -void SafeTimer::cancel_event(Context *c) +bool SafeTimer::cancel_event(Context *c) { assert(lock.is_locked()); assert(scheduled.count(c)); - if (g_timer.cancel_event(scheduled[c])) { + if (Timer::cancel_event(scheduled[c])) { // hosed wrapper. hose original event too. delete c; } else { @@ -360,5 +360,6 @@ SafeTimer::~SafeTimer() derr(0) << "SafeTimer.~SafeTimer " << scheduled.size() << " events scheduled, " << canceled.size() << " canceled but unflushed" << dendl; + assert(0); } } diff --git a/src/common/Timer.h b/src/common/Timer.h index 19ded170201..515371c4d84 100644 --- a/src/common/Timer.h +++ b/src/common/Timer.h @@ -119,12 +119,12 @@ class Timer { } // schedule events - void add_event_after(double seconds, + virtual void add_event_after(double seconds, Context *callback); - void add_event_at(utime_t when, + virtual void add_event_at(utime_t when, Context *callback); - bool cancel_event(Context *callback); - void cancel_all_events(); + virtual bool cancel_event(Context *callback); + virtual void cancel_all_events(); // execute pending events void execute_pending(); @@ -140,7 +140,7 @@ class Timer { * and then call join() to ensure any concurrently exectuting events (in other * threads) get flushed. */ -class SafeTimer { +class SafeTimer : public Timer { Mutex& lock; Cond cond; map scheduled; // actual -> wrapper @@ -161,7 +161,7 @@ public: void add_event_after(double seconds, Context *c); void add_event_at(utime_t when, Context *c); - void cancel_event(Context *c); + bool cancel_event(Context *c); void cancel_all(); void join();