SafeTimer: getting rid of global timer

This commit is contained in:
Yehuda Sadeh 2009-02-13 13:28:24 -08:00 committed by Sage Weil
parent 06e396139b
commit 740da72fbd
2 changed files with 12 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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<Context*,Context*> 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();