]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/cpp/src/thrift/concurrency/TimerManager.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / cpp / src / thrift / concurrency / TimerManager.h
1 /*
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 */
19
20 #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_
21 #define _THRIFT_CONCURRENCY_TIMERMANAGER_H_ 1
22
23 #include <thrift/concurrency/Monitor.h>
24 #include <thrift/concurrency/ThreadFactory.h>
25
26 #include <memory>
27 #include <map>
28
29 namespace apache {
30 namespace thrift {
31 namespace concurrency {
32
33 /**
34 * Timer Manager
35 *
36 * This class dispatches timer tasks when they fall due.
37 *
38 * @version $Id:$
39 */
40 class TimerManager {
41
42 public:
43 class Task;
44 typedef std::weak_ptr<Task> Timer;
45
46 TimerManager();
47
48 virtual ~TimerManager();
49
50 virtual std::shared_ptr<const ThreadFactory> threadFactory() const;
51
52 virtual void threadFactory(std::shared_ptr<const ThreadFactory> value);
53
54 /**
55 * Starts the timer manager service
56 *
57 * @throws IllegalArgumentException Missing thread factory attribute
58 */
59 virtual void start();
60
61 /**
62 * Stops the timer manager service
63 */
64 virtual void stop();
65
66 virtual size_t taskCount() const;
67
68 /**
69 * Adds a task to be executed at some time in the future by a worker thread.
70 *
71 * @param task The task to execute
72 * @param timeout Time in milliseconds to delay before executing task
73 * @return Handle of the timer, which can be used to remove the timer.
74 */
75 virtual Timer add(std::shared_ptr<Runnable> task, const std::chrono::milliseconds &timeout);
76 Timer add(std::shared_ptr<Runnable> task, uint64_t timeout) { return add(task,std::chrono::milliseconds(timeout)); }
77
78 /**
79 * Adds a task to be executed at some time in the future by a worker thread.
80 *
81 * @param task The task to execute
82 * @param abstime Absolute time in the future to execute task.
83 * @return Handle of the timer, which can be used to remove the timer.
84 */
85 virtual Timer add(std::shared_ptr<Runnable> task, const std::chrono::time_point<std::chrono::steady_clock>& abstime);
86
87 /**
88 * Removes a pending task
89 *
90 * @param task The task to remove. All timers which execute this task will
91 * be removed.
92 * @throws NoSuchTaskException Specified task doesn't exist. It was either
93 * processed already or this call was made for a
94 * task that was never added to this timer
95 *
96 * @throws UncancellableTaskException Specified task is already being
97 * executed or has completed execution.
98 */
99 virtual void remove(std::shared_ptr<Runnable> task);
100
101 /**
102 * Removes a single pending task
103 *
104 * @param timer The timer to remove. The timer is returned when calling the
105 * add() method.
106 * @throws NoSuchTaskException Specified task doesn't exist. It was either
107 * processed already or this call was made for a
108 * task that was never added to this timer
109 *
110 * @throws UncancellableTaskException Specified task is already being
111 * executed or has completed execution.
112 */
113 virtual void remove(Timer timer);
114
115 enum STATE { UNINITIALIZED, STARTING, STARTED, STOPPING, STOPPED };
116
117 virtual STATE state() const;
118
119 private:
120 std::shared_ptr<const ThreadFactory> threadFactory_;
121 friend class Task;
122 std::multimap<std::chrono::time_point<std::chrono::steady_clock>, std::shared_ptr<Task> > taskMap_;
123 size_t taskCount_;
124 Monitor monitor_;
125 STATE state_;
126 class Dispatcher;
127 friend class Dispatcher;
128 std::shared_ptr<Dispatcher> dispatcher_;
129 std::shared_ptr<Thread> dispatcherThread_;
130 using task_iterator = decltype(taskMap_)::iterator;
131 typedef std::pair<task_iterator, task_iterator> task_range;
132 };
133 }
134 }
135 } // apache::thrift::concurrency
136
137 #endif // #ifndef _THRIFT_CONCURRENCY_TIMERMANAGER_H_