blob: 002460aded123d9c15fedba958cd585e9521a6f3 [file] [log] [blame]
Marc Slemko0e53ccd2006-07-17 23:51:05 +00001#if !defined(_concurrency_TimerManager_h_)
2#define _concurrency_TimerManager_h_ 1
3
4#include "Monitor.h"
5#include "Thread.h"
6
7#include <map>
8
9#include <time.h>
10
11namespace facebook { namespace thrift { namespace concurrency {
12
13/** Timer Manager
14
15 This class dispatches timer tasks when they fall due.
16
17 @author marc
18 @version $Id:$ */
19
20class TimerManager {
21
22 public:
23
24 TimerManager();
25
26 virtual ~TimerManager() = 0;
27
28 virtual const ThreadFactory* threadFactory() const = 0;
29
30 virtual void threadFactory(const ThreadFactory* value) = 0;
31
32 virtual size_t taskCount() const = 0;
33
34 /** Adds a task to be executed at some time in the future by a worker thread.
35
36 @param task The task to execute
37 @param timeout Time in milliseconds to delay before executing task */
38
39 virtual void add(Runnable* task, long long timeout) = 0;
40
41 /** Adds a task to be executed at some time in the future by a worker thread.
42
43 @param task The task to execute
44 @param timeout Absolute time in the future to execute task. */
45
46 virtual void add(Runnable* task, const struct timespec& timeout) = 0;
47
48 /** Removes a pending task */
49
50 virtual void remove(Runnable* task) = 0;
51
52 private:
53
54 const ThreadFactory* _threadFactory;
55
56 class Task;
57
58 friend class Task;
59
60 std::multimap<long long, Task*> _taskMap;
61
62 size_t _taskCount;
63
64 long long _nextTimeout;
65
66 Monitor _monitor;
67
68 class Dispatcher;
69
70 friend class Dispatcher;
71
72 Dispatcher* _dispatcher;
73};
74
75}}} // facebook::thrift::concurrency
76
77#endif // !defined(_concurrency_TimerManager_h_)