blob: 50a0c13fe498a240e69127853a48ad76ec1eeaae [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_
2#define _THRIFT_CONCURRENCY_TIMERMANAGER_H_ 1
Marc Slemko0e53ccd2006-07-17 23:51:05 +00003
Marc Slemko8a40a762006-07-19 17:46:50 +00004#include "Exception.h"
Marc Slemko0e53ccd2006-07-17 23:51:05 +00005#include "Monitor.h"
6#include "Thread.h"
7
Marc Slemko6f038a72006-08-03 18:58:09 +00008#include <boost/shared_ptr.hpp>
Marc Slemko0e53ccd2006-07-17 23:51:05 +00009#include <map>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000010#include <time.h>
11
12namespace facebook { namespace thrift { namespace concurrency {
Marc Slemko8a40a762006-07-19 17:46:50 +000013
Marc Slemko6f038a72006-08-03 18:58:09 +000014using namespace boost;
15
Mark Sleef5f2be42006-09-05 21:05:31 +000016/**
17 * Timer Manager
18 *
19 * This class dispatches timer tasks when they fall due.
20 *
21 * @author marc
22 * @version $Id:$
23 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000024class TimerManager {
25
26 public:
27
28 TimerManager();
29
Marc Slemko8a40a762006-07-19 17:46:50 +000030 virtual ~TimerManager();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000031
Marc Slemko6f038a72006-08-03 18:58:09 +000032 virtual shared_ptr<const ThreadFactory> threadFactory() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000033
Marc Slemko6f038a72006-08-03 18:58:09 +000034 virtual void threadFactory(shared_ptr<const ThreadFactory> value);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000035
Mark Sleef5f2be42006-09-05 21:05:31 +000036 /**
37 * Starts the timer manager service
38 *
39 * @throws IllegalArgumentException Missing thread factory attribute
40 */
Marc Slemko8a40a762006-07-19 17:46:50 +000041 virtual void start();
42
Mark Sleef5f2be42006-09-05 21:05:31 +000043 /**
44 * Stops the timer manager service
45 */
Marc Slemko8a40a762006-07-19 17:46:50 +000046 virtual void stop();
47
48 virtual size_t taskCount() const ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000049
Mark Sleef5f2be42006-09-05 21:05:31 +000050 /**
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 Slemko6f038a72006-08-03 18:58:09 +000056 virtual void add(shared_ptr<Runnable> task, long long timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000057
Mark Sleef5f2be42006-09-05 21:05:31 +000058 /**
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 Slemko6f038a72006-08-03 18:58:09 +000064 virtual void add(shared_ptr<Runnable> task, const struct timespec& timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000065
Mark Sleef5f2be42006-09-05 21:05:31 +000066 /**
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 Slemko6f038a72006-08-03 18:58:09 +000076 virtual void remove(shared_ptr<Runnable> task);
Marc Slemko8a40a762006-07-19 17:46:50 +000077
78 enum STATE {
Marc Slemkod466b212006-07-20 00:04:18 +000079 UNINITIALIZED,
80 STARTING,
81 STARTED,
82 STOPPING,
83 STOPPED
Marc Slemko8a40a762006-07-19 17:46:50 +000084 };
85
86 virtual const STATE state() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000087
88 private:
Marc Slemko6f038a72006-08-03 18:58:09 +000089 shared_ptr<const ThreadFactory> _threadFactory;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000090 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000091 friend class Task;
Marc Slemko6f038a72006-08-03 18:58:09 +000092 std::multimap<long long, shared_ptr<Task> > _taskMap;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000093 size_t _taskCount;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000094 Monitor _monitor;
Marc Slemko8a40a762006-07-19 17:46:50 +000095 STATE _state;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000096 class Dispatcher;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000097 friend class Dispatcher;
Marc Slemko6f038a72006-08-03 18:58:09 +000098 shared_ptr<Dispatcher> _dispatcher;
Marc Slemko6f038a72006-08-03 18:58:09 +000099 shared_ptr<Thread> _dispatcherThread;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000100};
101
102}}} // facebook::thrift::concurrency
103
Mark Sleef5f2be42006-09-05 21:05:31 +0000104#endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_