]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/core/timer.hh
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / include / seastar / core / timer.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright 2015 Cloudius Systems
20 */
21
22 #pragma once
23
24 #include <chrono>
25 #include <seastar/util/std-compat.hh>
26 #include <atomic>
27 #include <functional>
28 #include <seastar/core/future.hh>
29 #include <seastar/core/timer-set.hh>
30
31 namespace seastar {
32
33 using steady_clock_type = std::chrono::steady_clock;
34
35 template <typename Clock = steady_clock_type>
36 class timer {
37 public:
38 typedef typename Clock::time_point time_point;
39 typedef typename Clock::duration duration;
40 typedef Clock clock;
41 private:
42 using callback_t = noncopyable_function<void()>;
43 boost::intrusive::list_member_hook<> _link;
44 callback_t _callback;
45 time_point _expiry;
46 compat::optional<duration> _period;
47 bool _armed = false;
48 bool _queued = false;
49 bool _expired = false;
50 void readd_periodic();
51 void arm_state(time_point until, compat::optional<duration> period) {
52 assert(!_armed);
53 _period = period;
54 _armed = true;
55 _expired = false;
56 _expiry = until;
57 _queued = true;
58 }
59 public:
60 timer() = default;
61 timer(timer&& t) noexcept : _callback(std::move(t._callback)), _expiry(std::move(t._expiry)), _period(std::move(t._period)),
62 _armed(t._armed), _queued(t._queued), _expired(t._expired) {
63 _link.swap_nodes(t._link);
64 t._queued = false;
65 t._armed = false;
66 }
67 explicit timer(callback_t&& callback) : _callback{std::move(callback)} {
68 }
69 ~timer();
70 void set_callback(callback_t&& callback) {
71 _callback = std::move(callback);
72 }
73 void arm(time_point until, compat::optional<duration> period = {});
74 void rearm(time_point until, compat::optional<duration> period = {}) {
75 if (_armed) {
76 cancel();
77 }
78 arm(until, period);
79 }
80 void arm(duration delta) {
81 return arm(Clock::now() + delta);
82 }
83 void arm_periodic(duration delta) {
84 arm(Clock::now() + delta, {delta});
85 }
86 bool armed() const { return _armed; }
87 bool cancel();
88 time_point get_timeout() {
89 return _expiry;
90 }
91 friend class reactor;
92 friend class timer_set<timer, &timer::_link>;
93 };
94
95 extern template class timer<steady_clock_type>;
96
97 }
98