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