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