Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 1 | #if !defined(_concurrency_TimerManager_h_) |
| 2 | #define _concurrency_TimerManager_h_ 1 |
| 3 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 4 | #include "Exception.h" |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 5 | #include "Monitor.h" |
| 6 | #include "Thread.h" |
| 7 | |
| 8 | #include <map> |
| 9 | |
| 10 | #include <time.h> |
| 11 | |
| 12 | namespace facebook { namespace thrift { namespace concurrency { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 13 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 14 | /** Timer Manager |
| 15 | |
| 16 | This class dispatches timer tasks when they fall due. |
| 17 | |
| 18 | @author marc |
| 19 | @version $Id:$ */ |
| 20 | |
| 21 | class TimerManager { |
| 22 | |
| 23 | public: |
| 24 | |
| 25 | TimerManager(); |
| 26 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 27 | virtual ~TimerManager(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 28 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 29 | virtual const ThreadFactory* threadFactory() const; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 30 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 31 | virtual void threadFactory(const ThreadFactory* value); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 32 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 33 | /** 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 Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 44 | |
| 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 Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 50 | virtual void add(Runnable* task, long long timeout); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 51 | |
| 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 Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 57 | virtual void add(Runnable* task, const struct timespec& timeout); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 58 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 59 | /** Removes a pending task |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 60 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 61 | @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 Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame^] | 69 | UNINITIALIZED, |
| 70 | STARTING, |
| 71 | STARTED, |
| 72 | STOPPING, |
| 73 | STOPPED |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | virtual const STATE state() const; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 77 | |
| 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 Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 90 | Monitor _monitor; |
| 91 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 92 | STATE _state; |
| 93 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 94 | class Dispatcher; |
| 95 | |
| 96 | friend class Dispatcher; |
| 97 | |
| 98 | Dispatcher* _dispatcher; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 99 | |
| 100 | Thread* _dispatcherThread; |
| 101 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | }}} // facebook::thrift::concurrency |
| 105 | |
| 106 | #endif // !defined(_concurrency_TimerManager_h_) |