blob: 4e83c9ec68ef5a94d61c26420c5c64227679c600 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// 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 Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_
8#define _THRIFT_CONCURRENCY_TIMERMANAGER_H_ 1
Marc Slemko0e53ccd2006-07-17 23:51:05 +00009
Marc Slemko8a40a762006-07-19 17:46:50 +000010#include "Exception.h"
Marc Slemko0e53ccd2006-07-17 23:51:05 +000011#include "Monitor.h"
12#include "Thread.h"
13
Marc Slemko6f038a72006-08-03 18:58:09 +000014#include <boost/shared_ptr.hpp>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000015#include <map>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000016#include <time.h>
17
18namespace facebook { namespace thrift { namespace concurrency {
Marc Slemko8a40a762006-07-19 17:46:50 +000019
Marc Slemko6f038a72006-08-03 18:58:09 +000020using namespace boost;
21
Mark Sleef5f2be42006-09-05 21:05:31 +000022/**
23 * Timer Manager
24 *
25 * This class dispatches timer tasks when they fall due.
26 *
27 * @author marc
28 * @version $Id:$
29 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000030class TimerManager {
31
32 public:
33
34 TimerManager();
35
Marc Slemko8a40a762006-07-19 17:46:50 +000036 virtual ~TimerManager();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000037
Marc Slemko6f038a72006-08-03 18:58:09 +000038 virtual shared_ptr<const ThreadFactory> threadFactory() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000039
Marc Slemko6f038a72006-08-03 18:58:09 +000040 virtual void threadFactory(shared_ptr<const ThreadFactory> value);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000041
Mark Sleef5f2be42006-09-05 21:05:31 +000042 /**
43 * Starts the timer manager service
44 *
45 * @throws IllegalArgumentException Missing thread factory attribute
46 */
Marc Slemko8a40a762006-07-19 17:46:50 +000047 virtual void start();
48
Mark Sleef5f2be42006-09-05 21:05:31 +000049 /**
50 * Stops the timer manager service
51 */
Marc Slemko8a40a762006-07-19 17:46:50 +000052 virtual void stop();
53
54 virtual size_t taskCount() const ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000055
Mark Sleef5f2be42006-09-05 21:05:31 +000056 /**
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 Slemko6f038a72006-08-03 18:58:09 +000062 virtual void add(shared_ptr<Runnable> task, long long timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000063
Mark Sleef5f2be42006-09-05 21:05:31 +000064 /**
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 Slemko6f038a72006-08-03 18:58:09 +000070 virtual void add(shared_ptr<Runnable> task, const struct timespec& timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000071
Mark Sleef5f2be42006-09-05 21:05:31 +000072 /**
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 Slemko6f038a72006-08-03 18:58:09 +000082 virtual void remove(shared_ptr<Runnable> task);
Marc Slemko8a40a762006-07-19 17:46:50 +000083
84 enum STATE {
Marc Slemkod466b212006-07-20 00:04:18 +000085 UNINITIALIZED,
86 STARTING,
87 STARTED,
88 STOPPING,
89 STOPPED
Marc Slemko8a40a762006-07-19 17:46:50 +000090 };
91
92 virtual const STATE state() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000093
94 private:
Mark Slee2f6404d2006-10-10 01:37:40 +000095 shared_ptr<const ThreadFactory> threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000096 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000097 friend class Task;
Mark Slee2f6404d2006-10-10 01:37:40 +000098 std::multimap<long long, shared_ptr<Task> > taskMap_;
99 size_t taskCount_;
100 Monitor monitor_;
101 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000102 class Dispatcher;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000103 friend class Dispatcher;
Mark Slee2f6404d2006-10-10 01:37:40 +0000104 shared_ptr<Dispatcher> dispatcher_;
105 shared_ptr<Thread> dispatcherThread_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000106};
107
108}}} // facebook::thrift::concurrency
109
Mark Sleef5f2be42006-09-05 21:05:31 +0000110#endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_