1
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2
// vim: ts=8 sw=2 smarttab
4
* Ceph - scalable distributed file system
6
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8
* This is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License version 2.1, as published by the Free Software
11
* Foundation. See file COPYING.
19
#include "include/types.h"
20
#include "include/Context.h"
32
#include <ext/hash_map>
33
using namespace __gnu_cxx;
44
template<> struct hash<Context*> {
45
size_t operator()(const Context *p) const {
46
static hash<unsigned long> H;
47
return H((unsigned long)p);
55
map< utime_t, set<Context*> > scheduled; // time -> (context ...)
56
hash_map< Context*, utime_t > event_times; // event -> time
58
bool get_next_due(utime_t &when);
60
void register_timer(); // make sure i get a callback
61
void cancel_timer(); // make sure i get a callback
71
void timer_entry(); // waiter thread (that wakes us up)
73
class TimerThread : public Thread {
80
TimerThread(Timer *_t) : t(_t) {}
102
for (map< utime_t, set<Context*> >::iterator it = scheduled.begin();
103
it != scheduled.end();
105
for (set<Context*>::iterator sit = it->second.begin();
106
sit != it->second.end();
122
virtual void add_event_after(double seconds,
124
virtual void add_event_at(utime_t when,
126
virtual bool cancel_event(Context *callback);
127
virtual void cancel_all_events();
129
// execute pending events
130
void execute_pending();
136
* SafeTimer is a wrapper around the a Timer that protects event
137
* execution with an existing mutex. It provides for, among other
138
* things, reliable event cancellation on class destruction. The
139
* caller just needs to cancel each event (or cancel_all()), and then
140
* call join() to ensure any concurrently exectuting events (in other
141
* threads) get flushed.
143
class SafeTimer : public Timer {
146
map<Context*,Context*> scheduled; // actual -> wrapper
147
map<Context*,Context*> canceled;
149
class EventWrapper : public Context {
153
EventWrapper(SafeTimer *st, Context *c) : timer(st),
159
SafeTimer(Mutex& l) : lock(l) { }
162
void add_event_after(double seconds, Context *c);
163
void add_event_at(utime_t when, Context *c);
164
bool cancel_event(Context *c);
168
int get_num_scheduled() { return scheduled.size(); }
169
int get_num_canceled() { return canceled.size(); }