blob: 2b92f232b0b2dd31fa0905b668f8f20aba70b30d [file] [log] [blame]
Marc Slemko0e53ccd2006-07-17 23:51:05 +00001#if !defined(_concurrency_TimerManager_h_)
2#define _concurrency_TimerManager_h_ 1
3
Marc Slemko8a40a762006-07-19 17:46:50 +00004#include "Exception.h"
Marc Slemko0e53ccd2006-07-17 23:51:05 +00005#include "Monitor.h"
6#include "Thread.h"
7
8#include <map>
9
10#include <time.h>
11
12namespace facebook { namespace thrift { namespace concurrency {
Marc Slemko8a40a762006-07-19 17:46:50 +000013
Marc Slemko0e53ccd2006-07-17 23:51:05 +000014/** Timer Manager
15
16 This class dispatches timer tasks when they fall due.
17
18 @author marc
19 @version $Id:$ */
20
21class TimerManager {
22
23 public:
24
25 TimerManager();
26
Marc Slemko8a40a762006-07-19 17:46:50 +000027 virtual ~TimerManager();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000028
Marc Slemko8a40a762006-07-19 17:46:50 +000029 virtual const ThreadFactory* threadFactory() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000030
Marc Slemko8a40a762006-07-19 17:46:50 +000031 virtual void threadFactory(const ThreadFactory* value);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000032
Marc Slemko8a40a762006-07-19 17:46:50 +000033 /** Starts the timer manager service
34
35 @throws IllegalArgumentException Missing thread factory attribute */
36
37 virtual void start();
38
39 /** Stops the timer manager service */
40
41 virtual void stop();
42
43 virtual size_t taskCount() const ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000044
45 /** Adds a task to be executed at some time in the future by a worker thread.
46
47 @param task The task to execute
48 @param timeout Time in milliseconds to delay before executing task */
49
Marc Slemko8a40a762006-07-19 17:46:50 +000050 virtual void add(Runnable* task, long long timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000051
52 /** Adds a task to be executed at some time in the future by a worker thread.
53
54 @param task The task to execute
55 @param timeout Absolute time in the future to execute task. */
56
Marc Slemko8a40a762006-07-19 17:46:50 +000057 virtual void add(Runnable* task, const struct timespec& timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000058
Marc Slemko8a40a762006-07-19 17:46:50 +000059 /** Removes a pending task
Marc Slemko0e53ccd2006-07-17 23:51:05 +000060
Marc Slemko8a40a762006-07-19 17:46:50 +000061 @throws NoSuchTaskException Specified task doesn't exist. It was either processed already or this call was made for a task that
62 was never added to this timer
63
64 @throws UncancellableTaskException Specified task is already being executed or has completed execution. */
65
66 virtual void remove(Runnable* task);
67
68 enum STATE {
Marc Slemkod466b212006-07-20 00:04:18 +000069 UNINITIALIZED,
70 STARTING,
71 STARTED,
72 STOPPING,
73 STOPPED
Marc Slemko8a40a762006-07-19 17:46:50 +000074 };
75
76 virtual const STATE state() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000077
78 private:
79
80 const ThreadFactory* _threadFactory;
81
82 class Task;
83
84 friend class Task;
85
86 std::multimap<long long, Task*> _taskMap;
87
88 size_t _taskCount;
89
Marc Slemko0e53ccd2006-07-17 23:51:05 +000090 Monitor _monitor;
91
Marc Slemko8a40a762006-07-19 17:46:50 +000092 STATE _state;
93
Marc Slemko0e53ccd2006-07-17 23:51:05 +000094 class Dispatcher;
95
96 friend class Dispatcher;
97
98 Dispatcher* _dispatcher;
Marc Slemko8a40a762006-07-19 17:46:50 +000099
100 Thread* _dispatcherThread;
101
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000102};
103
104}}} // facebook::thrift::concurrency
105
106#endif // !defined(_concurrency_TimerManager_h_)