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 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 8 | #include <boost/shared_ptr.hpp> |
| 9 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 10 | #include <map> |
| 11 | |
| 12 | #include <time.h> |
| 13 | |
| 14 | namespace facebook { namespace thrift { namespace concurrency { |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 15 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 16 | using namespace boost; |
| 17 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 18 | /** Timer Manager |
| 19 | |
| 20 | This class dispatches timer tasks when they fall due. |
| 21 | |
| 22 | @author marc |
| 23 | @version $Id:$ */ |
| 24 | |
| 25 | class TimerManager { |
| 26 | |
| 27 | public: |
| 28 | |
| 29 | TimerManager(); |
| 30 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 31 | virtual ~TimerManager(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 32 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 33 | virtual shared_ptr<const ThreadFactory> threadFactory() const; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 34 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 35 | virtual void threadFactory(shared_ptr<const ThreadFactory> value); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 36 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 37 | /** 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 Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 48 | |
| 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 Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 54 | virtual void add(shared_ptr<Runnable> task, long long timeout); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 55 | |
| 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 Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 61 | virtual void add(shared_ptr<Runnable> task, const struct timespec& timeout); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 62 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 63 | /** Removes a pending task |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 64 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 65 | @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 Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 70 | virtual void remove(shared_ptr<Runnable> task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 71 | |
| 72 | enum STATE { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 73 | UNINITIALIZED, |
| 74 | STARTING, |
| 75 | STARTED, |
| 76 | STOPPING, |
| 77 | STOPPED |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | virtual const STATE state() const; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 84 | shared_ptr<const ThreadFactory> _threadFactory; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 85 | |
| 86 | class Task; |
| 87 | |
| 88 | friend class Task; |
| 89 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 90 | std::multimap<long long, shared_ptr<Task> > _taskMap; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 91 | |
| 92 | size_t _taskCount; |
| 93 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 94 | Monitor _monitor; |
| 95 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 96 | STATE _state; |
| 97 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 98 | class Dispatcher; |
| 99 | |
| 100 | friend class Dispatcher; |
| 101 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 102 | shared_ptr<Dispatcher> _dispatcher; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 103 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame^] | 104 | shared_ptr<Thread> _dispatcherThread; |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 105 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | }}} // facebook::thrift::concurrency |
| 109 | |
| 110 | #endif // !defined(_concurrency_TimerManager_h_) |