blob: c0e63408cdb17b5fa293f2891ac9eb3034ba1241 [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
Marc Slemko6f038a72006-08-03 18:58:09 +00008#include <boost/shared_ptr.hpp>
9
Marc Slemko0e53ccd2006-07-17 23:51:05 +000010#include <map>
11
12#include <time.h>
13
14namespace facebook { namespace thrift { namespace concurrency {
Marc Slemko8a40a762006-07-19 17:46:50 +000015
Marc Slemko6f038a72006-08-03 18:58:09 +000016using namespace boost;
17
Marc Slemko0e53ccd2006-07-17 23:51:05 +000018/** Timer Manager
19
20 This class dispatches timer tasks when they fall due.
21
22 @author marc
23 @version $Id:$ */
24
25class TimerManager {
26
27 public:
28
29 TimerManager();
30
Marc Slemko8a40a762006-07-19 17:46:50 +000031 virtual ~TimerManager();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000032
Marc Slemko6f038a72006-08-03 18:58:09 +000033 virtual shared_ptr<const ThreadFactory> threadFactory() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000034
Marc Slemko6f038a72006-08-03 18:58:09 +000035 virtual void threadFactory(shared_ptr<const ThreadFactory> value);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000036
Marc Slemko8a40a762006-07-19 17:46:50 +000037 /** Starts the timer manager service
38
39 @throws IllegalArgumentException Missing thread factory attribute */
40
41 virtual void start();
42
43 /** Stops the timer manager service */
44
45 virtual void stop();
46
47 virtual size_t taskCount() const ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000048
49 /** Adds a task to be executed at some time in the future by a worker thread.
50
51 @param task The task to execute
52 @param timeout Time in milliseconds to delay before executing task */
53
Marc Slemko6f038a72006-08-03 18:58:09 +000054 virtual void add(shared_ptr<Runnable> task, long long timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000055
56 /** Adds a task to be executed at some time in the future by a worker thread.
57
58 @param task The task to execute
59 @param timeout Absolute time in the future to execute task. */
60
Marc Slemko6f038a72006-08-03 18:58:09 +000061 virtual void add(shared_ptr<Runnable> task, const struct timespec& timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000062
Marc Slemko8a40a762006-07-19 17:46:50 +000063 /** Removes a pending task
Marc Slemko0e53ccd2006-07-17 23:51:05 +000064
Marc Slemko8a40a762006-07-19 17:46:50 +000065 @throws NoSuchTaskException Specified task doesn't exist. It was either processed already or this call was made for a task that
66 was never added to this timer
67
68 @throws UncancellableTaskException Specified task is already being executed or has completed execution. */
69
Marc Slemko6f038a72006-08-03 18:58:09 +000070 virtual void remove(shared_ptr<Runnable> task);
Marc Slemko8a40a762006-07-19 17:46:50 +000071
72 enum STATE {
Marc Slemkod466b212006-07-20 00:04:18 +000073 UNINITIALIZED,
74 STARTING,
75 STARTED,
76 STOPPING,
77 STOPPED
Marc Slemko8a40a762006-07-19 17:46:50 +000078 };
79
80 virtual const STATE state() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000081
82 private:
83
Marc Slemko6f038a72006-08-03 18:58:09 +000084 shared_ptr<const ThreadFactory> _threadFactory;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000085
86 class Task;
87
88 friend class Task;
89
Marc Slemko6f038a72006-08-03 18:58:09 +000090 std::multimap<long long, shared_ptr<Task> > _taskMap;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000091
92 size_t _taskCount;
93
Marc Slemko0e53ccd2006-07-17 23:51:05 +000094 Monitor _monitor;
95
Marc Slemko8a40a762006-07-19 17:46:50 +000096 STATE _state;
97
Marc Slemko0e53ccd2006-07-17 23:51:05 +000098 class Dispatcher;
99
100 friend class Dispatcher;
101
Marc Slemko6f038a72006-08-03 18:58:09 +0000102 shared_ptr<Dispatcher> _dispatcher;
Marc Slemko8a40a762006-07-19 17:46:50 +0000103
Marc Slemko6f038a72006-08-03 18:58:09 +0000104 shared_ptr<Thread> _dispatcherThread;
Marc Slemko8a40a762006-07-19 17:46:50 +0000105
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000106};
107
108}}} // facebook::thrift::concurrency
109
110#endif // !defined(_concurrency_TimerManager_h_)