blob: 23f70e847059c584a7b66298f05bc84540d01d4d [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
T Jake Lucianib5e62212009-01-31 22:36:20 +000018namespace apache { 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 * @version $Id:$
26 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000027class TimerManager {
28
29 public:
30
31 TimerManager();
32
Marc Slemko8a40a762006-07-19 17:46:50 +000033 virtual ~TimerManager();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000034
Mark Slee5ea15f92007-03-05 22:55:59 +000035 virtual boost::shared_ptr<const ThreadFactory> threadFactory() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000036
Mark Slee5ea15f92007-03-05 22:55:59 +000037 virtual void threadFactory(boost::shared_ptr<const ThreadFactory> value);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000038
Mark Sleef5f2be42006-09-05 21:05:31 +000039 /**
David Reiss0c90f6f2008-02-06 22:18:40 +000040 * Starts the timer manager service
Mark Sleef5f2be42006-09-05 21:05:31 +000041 *
42 * @throws IllegalArgumentException Missing thread factory attribute
43 */
Marc Slemko8a40a762006-07-19 17:46:50 +000044 virtual void start();
45
Mark Sleef5f2be42006-09-05 21:05:31 +000046 /**
47 * Stops the timer manager service
48 */
Marc Slemko8a40a762006-07-19 17:46:50 +000049 virtual void stop();
50
51 virtual size_t taskCount() const ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000052
Mark Sleef5f2be42006-09-05 21:05:31 +000053 /**
54 * Adds a task to be executed at some time in the future by a worker thread.
David Reiss0c90f6f2008-02-06 22:18:40 +000055 *
Mark Sleef5f2be42006-09-05 21:05:31 +000056 * @param task The task to execute
57 * @param timeout Time in milliseconds to delay before executing task
58 */
Mark Slee9b82d272007-05-23 05:16:07 +000059 virtual void add(boost::shared_ptr<Runnable> task, int64_t timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000060
Mark Sleef5f2be42006-09-05 21:05:31 +000061 /**
62 * Adds a task to be executed at some time in the future by a worker thread.
David Reiss0c90f6f2008-02-06 22:18:40 +000063 *
Mark Sleef5f2be42006-09-05 21:05:31 +000064 * @param task The task to execute
65 * @param timeout Absolute time in the future to execute task.
David Reiss0c90f6f2008-02-06 22:18:40 +000066 */
Mark Slee5ea15f92007-03-05 22:55:59 +000067 virtual void add(boost::shared_ptr<Runnable> task, const struct timespec& timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000068
Mark Sleef5f2be42006-09-05 21:05:31 +000069 /**
David Reiss0c90f6f2008-02-06 22:18:40 +000070 * Removes a pending task
Mark Sleef5f2be42006-09-05 21:05:31 +000071 *
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 Slee5ea15f92007-03-05 22:55:59 +000079 virtual void remove(boost::shared_ptr<Runnable> task);
Marc Slemko8a40a762006-07-19 17:46:50 +000080
81 enum STATE {
Marc Slemkod466b212006-07-20 00:04:18 +000082 UNINITIALIZED,
83 STARTING,
84 STARTED,
85 STOPPING,
86 STOPPED
Marc Slemko8a40a762006-07-19 17:46:50 +000087 };
David Reiss0c90f6f2008-02-06 22:18:40 +000088
Marc Slemko8a40a762006-07-19 17:46:50 +000089 virtual const STATE state() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000090
91 private:
Mark Slee5ea15f92007-03-05 22:55:59 +000092 boost::shared_ptr<const ThreadFactory> threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000093 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000094 friend class Task;
Mark Slee9b82d272007-05-23 05:16:07 +000095 std::multimap<int64_t, boost::shared_ptr<Task> > taskMap_;
Mark Slee2f6404d2006-10-10 01:37:40 +000096 size_t taskCount_;
97 Monitor monitor_;
98 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000099 class Dispatcher;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000100 friend class Dispatcher;
Mark Slee5ea15f92007-03-05 22:55:59 +0000101 boost::shared_ptr<Dispatcher> dispatcher_;
102 boost::shared_ptr<Thread> dispatcherThread_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000103};
104
T Jake Lucianib5e62212009-01-31 22:36:20 +0000105}}} // apache::thrift::concurrency
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000106
Mark Sleef5f2be42006-09-05 21:05:31 +0000107#endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_