blob: d905ddb7685d98e19dcdd5da739e276661a671f0 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_
21#define _THRIFT_CONCURRENCY_TIMERMANAGER_H_ 1
Marc Slemko0e53ccd2006-07-17 23:51:05 +000022
Marc Slemko8a40a762006-07-19 17:46:50 +000023#include "Exception.h"
Marc Slemko0e53ccd2006-07-17 23:51:05 +000024#include "Monitor.h"
25#include "Thread.h"
26
Marc Slemko6f038a72006-08-03 18:58:09 +000027#include <boost/shared_ptr.hpp>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000028#include <map>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000029#include <time.h>
30
T Jake Lucianib5e62212009-01-31 22:36:20 +000031namespace apache { namespace thrift { namespace concurrency {
Marc Slemko8a40a762006-07-19 17:46:50 +000032
Mark Sleef5f2be42006-09-05 21:05:31 +000033/**
David Reiss0c90f6f2008-02-06 22:18:40 +000034 * Timer Manager
35 *
Mark Sleef5f2be42006-09-05 21:05:31 +000036 * This class dispatches timer tasks when they fall due.
David Reiss0c90f6f2008-02-06 22:18:40 +000037 *
Mark Sleef5f2be42006-09-05 21:05:31 +000038 * @version $Id:$
39 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000040class TimerManager {
41
42 public:
43
44 TimerManager();
45
Marc Slemko8a40a762006-07-19 17:46:50 +000046 virtual ~TimerManager();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000047
Mark Slee5ea15f92007-03-05 22:55:59 +000048 virtual boost::shared_ptr<const ThreadFactory> threadFactory() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000049
Mark Slee5ea15f92007-03-05 22:55:59 +000050 virtual void threadFactory(boost::shared_ptr<const ThreadFactory> value);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000051
Mark Sleef5f2be42006-09-05 21:05:31 +000052 /**
David Reiss0c90f6f2008-02-06 22:18:40 +000053 * Starts the timer manager service
Mark Sleef5f2be42006-09-05 21:05:31 +000054 *
55 * @throws IllegalArgumentException Missing thread factory attribute
56 */
Marc Slemko8a40a762006-07-19 17:46:50 +000057 virtual void start();
58
Mark Sleef5f2be42006-09-05 21:05:31 +000059 /**
60 * Stops the timer manager service
61 */
Marc Slemko8a40a762006-07-19 17:46:50 +000062 virtual void stop();
63
64 virtual size_t taskCount() const ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000065
Mark Sleef5f2be42006-09-05 21:05:31 +000066 /**
67 * Adds a task to be executed at some time in the future by a worker thread.
David Reiss0c90f6f2008-02-06 22:18:40 +000068 *
Mark Sleef5f2be42006-09-05 21:05:31 +000069 * @param task The task to execute
70 * @param timeout Time in milliseconds to delay before executing task
71 */
Mark Slee9b82d272007-05-23 05:16:07 +000072 virtual void add(boost::shared_ptr<Runnable> task, int64_t timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000073
Mark Sleef5f2be42006-09-05 21:05:31 +000074 /**
75 * Adds a task to be executed at some time in the future by a worker thread.
David Reiss0c90f6f2008-02-06 22:18:40 +000076 *
Mark Sleef5f2be42006-09-05 21:05:31 +000077 * @param task The task to execute
78 * @param timeout Absolute time in the future to execute task.
David Reiss0c90f6f2008-02-06 22:18:40 +000079 */
Mark Slee5ea15f92007-03-05 22:55:59 +000080 virtual void add(boost::shared_ptr<Runnable> task, const struct timespec& timeout);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000081
Mark Sleef5f2be42006-09-05 21:05:31 +000082 /**
David Reiss0c90f6f2008-02-06 22:18:40 +000083 * Removes a pending task
Mark Sleef5f2be42006-09-05 21:05:31 +000084 *
85 * @throws NoSuchTaskException Specified task doesn't exist. It was either
86 * processed already or this call was made for a
87 * task that was never added to this timer
88 *
89 * @throws UncancellableTaskException Specified task is already being
90 * executed or has completed execution.
91 */
Mark Slee5ea15f92007-03-05 22:55:59 +000092 virtual void remove(boost::shared_ptr<Runnable> task);
Marc Slemko8a40a762006-07-19 17:46:50 +000093
94 enum STATE {
Marc Slemkod466b212006-07-20 00:04:18 +000095 UNINITIALIZED,
96 STARTING,
97 STARTED,
98 STOPPING,
99 STOPPED
Marc Slemko8a40a762006-07-19 17:46:50 +0000100 };
David Reiss0c90f6f2008-02-06 22:18:40 +0000101
Roger Meier3b771a12010-11-17 22:11:26 +0000102 virtual STATE state() const;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000103
104 private:
Mark Slee5ea15f92007-03-05 22:55:59 +0000105 boost::shared_ptr<const ThreadFactory> threadFactory_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000106 class Task;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000107 friend class Task;
Mark Slee9b82d272007-05-23 05:16:07 +0000108 std::multimap<int64_t, boost::shared_ptr<Task> > taskMap_;
Mark Slee2f6404d2006-10-10 01:37:40 +0000109 size_t taskCount_;
110 Monitor monitor_;
111 STATE state_;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000112 class Dispatcher;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000113 friend class Dispatcher;
Mark Slee5ea15f92007-03-05 22:55:59 +0000114 boost::shared_ptr<Dispatcher> dispatcher_;
115 boost::shared_ptr<Thread> dispatcherThread_;
David Reiss2ff956c2009-05-21 02:28:23 +0000116 typedef std::multimap<int64_t, boost::shared_ptr<TimerManager::Task> >::iterator task_iterator;
117 typedef std::pair<task_iterator, task_iterator> task_range;
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000118};
119
T Jake Lucianib5e62212009-01-31 22:36:20 +0000120}}} // apache::thrift::concurrency
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000121
Mark Sleef5f2be42006-09-05 21:05:31 +0000122#endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_