Seastar
High performance C++ framework for concurrent servers
timer.hh
Go to the documentation of this file.
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 <seastar/core/future.hh>
26#include <seastar/core/timer-set.hh>
27#include <seastar/util/std-compat.hh>
28#include <seastar/util/modules.hh>
29#ifndef SEASTAR_MODULE
30#include <boost/intrusive/list.hpp>
31#include <chrono>
32#include <optional>
33#endif
34
36
51
52namespace seastar {
53
54SEASTAR_MODULE_EXPORT_BEGIN
55
56using steady_clock_type = std::chrono::steady_clock;
57
58
61
82template <typename Clock = steady_clock_type>
83class timer {
84public:
85 typedef typename Clock::time_point time_point;
86 typedef typename Clock::duration duration;
87 typedef Clock clock;
88private:
89 using callback_t = noncopyable_function<void()>;
90 boost::intrusive::list_member_hook<> _link;
92 callback_t _callback;
93 time_point _expiry;
94 std::optional<duration> _period;
95 bool _armed = false;
96 bool _queued = false;
97 bool _expired = false;
98 void readd_periodic() noexcept;
99 void arm_state(time_point until, std::optional<duration> period) noexcept {
100 assert(!_armed);
101 _period = period;
102 _armed = true;
103 _expired = false;
104 _expiry = until;
105 _queued = true;
106 }
107public:
109 timer() noexcept {}; // implementation is required (instead of = default) for noexcept due to a bug in gcc 9.3.1,
110 // since boost::intrusive::list_member_hook default constructor is not specified as noexcept.
115 timer(timer&& t) noexcept : _sg(t._sg), _callback(std::move(t._callback)), _expiry(std::move(t._expiry)), _period(std::move(t._period)),
116 _armed(t._armed), _queued(t._queued), _expired(t._expired) {
117 _link.swap_nodes(t._link);
118 t._queued = false;
119 t._armed = false;
120 }
125 timer(scheduling_group sg, noncopyable_function<void ()>&& callback) noexcept : _sg(sg), _callback{std::move(callback)} {
126 }
130 explicit timer(noncopyable_function<void ()>&& callback) noexcept : timer(current_scheduling_group(), std::move(callback)) {
131 }
138 void set_callback(scheduling_group sg, noncopyable_function<void ()>&& callback) noexcept {
139 _sg = sg;
140 _callback = std::move(callback);
141 }
145 void set_callback(noncopyable_function<void ()>&& callback) noexcept {
146 set_callback(current_scheduling_group(), std::move(callback));
147 }
159 void arm(time_point until, std::optional<duration> period = {}) noexcept;
167 void rearm(time_point until, std::optional<duration> period = {}) noexcept {
168 if (_armed) {
169 cancel();
170 }
171 arm(until, period);
172 }
181 void arm(duration delta) noexcept {
182 return arm(Clock::now() + delta);
183 }
188 void arm_periodic(duration delta) noexcept {
189 arm(Clock::now() + delta, {delta});
190 }
196 void rearm_periodic(duration delta) noexcept {
197 if (_armed) {
198 cancel();
199 }
200 arm_periodic(delta);
201 }
205 bool armed() const noexcept { return _armed; }
212 bool cancel() noexcept;
217 time_point get_timeout() const noexcept {
218 return _expiry;
219 }
220
221 friend class timer_set<timer, &timer::_link>;
223};
224
225extern template class timer<steady_clock_type>;
226
227
229SEASTAR_MODULE_EXPORT_END
230}
231
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:285
Definition: timer-set.hh:44
Definition: timer.hh:83
time_point get_timeout() const noexcept
Definition: timer.hh:217
void arm(time_point until, std::optional< duration > period={}) noexcept
bool armed() const noexcept
Definition: timer.hh:205
~timer()
Destroys the timer. The timer is cancelled if armed.
void arm(duration delta) noexcept
Definition: timer.hh:181
bool cancel() noexcept
timer(scheduling_group sg, noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:125
void rearm(time_point until, std::optional< duration > period={}) noexcept
Definition: timer.hh:167
timer(noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:130
void rearm_periodic(duration delta) noexcept
Definition: timer.hh:196
void arm_periodic(duration delta) noexcept
Definition: timer.hh:188
timer() noexcept
Constructs a timer with no callback set and no expiration time.
Definition: timer.hh:109
void set_callback(noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:145
timer(timer &&t) noexcept
Definition: timer.hh:115
void set_callback(scheduling_group sg, noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:138
future now()
Returns a ready future.
Definition: later.hh:35
Seastar API namespace.
Definition: abort_on_ebadf.hh:26
scheduling_group current_scheduling_group() noexcept
Returns the current scheduling group.
Definition: scheduling.hh:397
Definition: noncopyable_function.hh:37