blob: 6d2eae95cb1fb7f0b30dea6213949520f9d799ca [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
David Reiss0c90f6f2008-02-06 22:18:40 +000018namespace facebook { namespace thrift { namespace concurrency {
Marc Slemko8a40a762006-07-19 17:46:50 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020/**
David Reiss0c90f6f2008-02-06 22:18:40 +000021 * Timer Manager
22 *
Mark Sleef5f2be42006-09-05 21:05:31 +000023 * This class dispatches timer tasks when they fall due.
David Reiss0c90f6f2008-02-06 22:18:40 +000024 *
Mark Sleef5f2be42006-09-05 21:05:31 +000025 * @author marc
26 * @version $Id:$
27 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000028class TimerManager {
29
30 public:
31
32 TimerManager();
33
Marc Slemko8a40a762006-07-19 17:46:50 +000034 virtual ~TimerManager();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000035
Mark Slee5ea15f92007-03-05 22:55:59 +000036 virtual boost::shared_ptr<const ThreadFactory> threadFactory() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000037
Mark Slee5ea15f92007-03-05 22:55:59 +000038 virtual void threadFactory(boost::shared_ptr<const ThreadFactory> value);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000039
Mark Sleef5f2be42006-09-05 21:05:31 +000040 /**
David Reiss0c90f6f2008-02-06 22:18:40 +000041 * Starts the timer manager service
Mark Sleef5f2be42006-09-05 21:05:31 +000042 *
43 * @throws IllegalArgumentException Missing thread factory attribute
44 */
Marc Slemko8a40a762006-07-19 17:46:50 +000045 virtual void start();
46
Mark Sleef5f2be42006-09-05 21:05:31 +000047 /**
48 * Stops the timer manager service
49 */
Marc Slemko8a40a762006-07-19 17:46:50 +000050 virtual void stop();
51
52 virtual size_t taskCount() const ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000053
Mark Sleef5f2be42006-09-05 21:05:31 +000054 /**
55 * Adds a task to be executed at some time in the future by a worker thread.
David Reiss0c90f6f2008-02-06 22:18:40 +000056 *
Mark Sleef5f2be42006-09-05 21:05:31 +000057 * @param task The task to execute
58 * @param timeout Time in milliseconds to delay before executing task
59 */
Mark Slee9b82d272007-05-23 05:16:07 +000060 virtual void add(boost::shared_ptr<Runnable> task, int64_t timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000061
Mark Sleef5f2be42006-09-05 21:05:31 +000062 /**
63 * Adds a task to be executed at some time in the future by a worker thread.
David Reiss0c90f6f2008-02-06 22:18:40 +000064 *
Mark Sleef5f2be42006-09-05 21:05:31 +000065 * @param task The task to execute
66 * @param timeout Absolute time in the future to execute task.
David Reiss0c90f6f2008-02-06 22:18:40 +000067 */
Mark Slee5ea15f92007-03-05 22:55:59 +000068 virtual void add(boost::shared_ptr<Runnable> task, const struct timespec& timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000069
Mark Sleef5f2be42006-09-05 21:05:31 +000070 /**
David Reiss0c90f6f2008-02-06 22:18:40 +000071 * Removes a pending task
Mark Sleef5f2be42006-09-05 21:05:31 +000072 *
73 * @throws NoSuchTaskException Specified task doesn't exist. It was either
74 * processed already or this call was made for a
75 * task that was never added to this timer
76 *
77 * @throws UncancellableTaskException Specified task is already being
78 * executed or has completed execution.
79 */
Mark Slee5ea15f92007-03-05 22:55:59 +000080 virtual void remove(boost::shared_ptr<Runnable> task);
Marc Slemko8a40a762006-07-19 17:46:50 +000081
82 enum STATE {
Marc Slemkod466b212006-07-20 00:04:18 +000083 UNINITIALIZED,
84 STARTING,
85 STARTED,
86 STOPPING,
87 STOPPED
Marc Slemko8a40a762006-07-19 17:46:50 +000088 };
David Reiss0c90f6f2008-02-06 22:18:40 +000089
Marc Slemko8a40a762006-07-19 17:46:50 +000090 virtual const STATE state() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000091
92 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000093 boost::shared_ptr<const ThreadFactory> threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000094 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000095 friend class Task;
Mark Slee9b82d272007-05-23 05:16:07 +000096 std::multimap<int64_t, boost::shared_ptr<Task> > taskMap_;
Mark Slee2f6404d2006-10-10 01:37:40 +000097 size_t taskCount_;
98 Monitor monitor_;
99 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000100 class Dispatcher;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000101 friend class Dispatcher;
Mark Slee5ea15f92007-03-05 22:55:59 +0000102 boost::shared_ptr<Dispatcher> dispatcher_;
103 boost::shared_ptr<Thread> dispatcherThread_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000104};
105
106}}} // facebook::thrift::concurrency
107
Mark Sleef5f2be42006-09-05 21:05:31 +0000108#endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_