Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 1 | #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_ |
| 2 | #define _THRIFT_CONCURRENCY_TIMERMANAGER_H_ 1 |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 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> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 9 | #include <map> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 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 | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 14 | using namespace boost; |
| 15 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 16 | /** |
| 17 | * Timer Manager |
| 18 | * |
| 19 | * This class dispatches timer tasks when they fall due. |
| 20 | * |
| 21 | * @author marc |
| 22 | * @version $Id:$ |
| 23 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 24 | class TimerManager { |
| 25 | |
| 26 | public: |
| 27 | |
| 28 | TimerManager(); |
| 29 | |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 30 | virtual ~TimerManager(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 31 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 32 | virtual shared_ptr<const ThreadFactory> threadFactory() const; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 33 | |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 34 | virtual void threadFactory(shared_ptr<const ThreadFactory> value); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 35 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Starts the timer manager service |
| 38 | * |
| 39 | * @throws IllegalArgumentException Missing thread factory attribute |
| 40 | */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 41 | virtual void start(); |
| 42 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Stops the timer manager service |
| 45 | */ |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 46 | virtual void stop(); |
| 47 | |
| 48 | virtual size_t taskCount() const ; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 49 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Adds a task to be executed at some time in the future by a worker thread. |
| 52 | * |
| 53 | * @param task The task to execute |
| 54 | * @param timeout Time in milliseconds to delay before executing task |
| 55 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 56 | virtual void add(shared_ptr<Runnable> task, long long timeout); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 57 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 58 | /** |
| 59 | * Adds a task to be executed at some time in the future by a worker thread. |
| 60 | * |
| 61 | * @param task The task to execute |
| 62 | * @param timeout Absolute time in the future to execute task. |
| 63 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 64 | virtual void add(shared_ptr<Runnable> task, const struct timespec& timeout); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 65 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 66 | /** |
| 67 | * Removes a pending task |
| 68 | * |
| 69 | * @throws NoSuchTaskException Specified task doesn't exist. It was either |
| 70 | * processed already or this call was made for a |
| 71 | * task that was never added to this timer |
| 72 | * |
| 73 | * @throws UncancellableTaskException Specified task is already being |
| 74 | * executed or has completed execution. |
| 75 | */ |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 76 | virtual void remove(shared_ptr<Runnable> task); |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 77 | |
| 78 | enum STATE { |
Marc Slemko | d466b21 | 2006-07-20 00:04:18 +0000 | [diff] [blame] | 79 | UNINITIALIZED, |
| 80 | STARTING, |
| 81 | STARTED, |
| 82 | STOPPING, |
| 83 | STOPPED |
Marc Slemko | 8a40a76 | 2006-07-19 17:46:50 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | virtual const STATE state() const; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 87 | |
| 88 | private: |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 89 | shared_ptr<const ThreadFactory> threadFactory_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 90 | class Task; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 91 | friend class Task; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 92 | std::multimap<long long, shared_ptr<Task> > taskMap_; |
| 93 | size_t taskCount_; |
| 94 | Monitor monitor_; |
| 95 | STATE state_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 96 | class Dispatcher; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 97 | friend class Dispatcher; |
Mark Slee | 2f6404d | 2006-10-10 01:37:40 +0000 | [diff] [blame^] | 98 | shared_ptr<Dispatcher> dispatcher_; |
| 99 | shared_ptr<Thread> dispatcherThread_; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | }}} // facebook::thrift::concurrency |
| 103 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 104 | #endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_ |