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 <atomic>
32 #include <chrono>
33 #include <functional>
34 #endif
35 
37 
52 
53 namespace seastar {
54 
55 SEASTAR_MODULE_EXPORT_BEGIN
56 
57 using steady_clock_type = std::chrono::steady_clock;
58 
59 
62 
83 template <typename Clock = steady_clock_type>
84 class timer {
85 public:
86  typedef typename Clock::time_point time_point;
87  typedef typename Clock::duration duration;
88  typedef Clock clock;
89 private:
90  using callback_t = noncopyable_function<void()>;
91  boost::intrusive::list_member_hook<> _link;
92  scheduling_group _sg;
93  callback_t _callback;
94  time_point _expiry;
95  std::optional<duration> _period;
96  bool _armed = false;
97  bool _queued = false;
98  bool _expired = false;
99  void readd_periodic() noexcept;
100  void arm_state(time_point until, std::optional<duration> period) noexcept {
101  assert(!_armed);
102  _period = period;
103  _armed = true;
104  _expired = false;
105  _expiry = until;
106  _queued = true;
107  }
108 public:
110  timer() noexcept {}; // implementation is required (instead of = default) for noexcept due to a bug in gcc 9.3.1,
111  // since boost::intrusive::list_member_hook default constructor is not specified as noexcept.
116  timer(timer&& t) noexcept : _sg(t._sg), _callback(std::move(t._callback)), _expiry(std::move(t._expiry)), _period(std::move(t._period)),
117  _armed(t._armed), _queued(t._queued), _expired(t._expired) {
118  _link.swap_nodes(t._link);
119  t._queued = false;
120  t._armed = false;
121  }
126  timer(scheduling_group sg, noncopyable_function<void ()>&& callback) noexcept : _sg(sg), _callback{std::move(callback)} {
127  }
131  explicit timer(noncopyable_function<void ()>&& callback) noexcept : timer(current_scheduling_group(), std::move(callback)) {
132  }
139  void set_callback(scheduling_group sg, noncopyable_function<void ()>&& callback) noexcept {
140  _sg = sg;
141  _callback = std::move(callback);
142  }
146  void set_callback(noncopyable_function<void ()>&& callback) noexcept {
147  set_callback(current_scheduling_group(), std::move(callback));
148  }
160  void arm(time_point until, std::optional<duration> period = {}) noexcept;
168  void rearm(time_point until, std::optional<duration> period = {}) noexcept {
169  if (_armed) {
170  cancel();
171  }
172  arm(until, period);
173  }
182  void arm(duration delta) noexcept {
183  return arm(Clock::now() + delta);
184  }
189  void arm_periodic(duration delta) noexcept {
190  arm(Clock::now() + delta, {delta});
191  }
197  void rearm_periodic(duration delta) noexcept {
198  if (_armed) {
199  cancel();
200  }
201  arm_periodic(delta);
202  }
206  bool armed() const noexcept { return _armed; }
213  bool cancel() noexcept;
218  time_point get_timeout() const noexcept {
219  return _expiry;
220  }
221  friend class reactor;
222  friend class timer_set<timer, &timer::_link>;
223 };
224 
225 extern template class timer<steady_clock_type>;
226 
227 
229 SEASTAR_MODULE_EXPORT_END
230 }
231 
Definition: reactor.hh:168
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:286
Definition: timer-set.hh:40
Definition: timer.hh:84
time_point get_timeout() const noexcept
Definition: timer.hh:218
void arm(time_point until, std::optional< duration > period={}) noexcept
bool armed() const noexcept
Definition: timer.hh:206
~timer()
Destroys the timer. The timer is cancelled if armed.
void arm(duration delta) noexcept
Definition: timer.hh:182
bool cancel() noexcept
timer(scheduling_group sg, noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:126
void rearm(time_point until, std::optional< duration > period={}) noexcept
Definition: timer.hh:168
timer(noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:131
void rearm_periodic(duration delta) noexcept
Definition: timer.hh:197
void arm_periodic(duration delta) noexcept
Definition: timer.hh:189
timer() noexcept
Constructs a timer with no callback set and no expiration time.
Definition: timer.hh:110
void set_callback(noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:146
timer(timer &&t) noexcept
Definition: timer.hh:116
void set_callback(scheduling_group sg, noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:139
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:400
Definition: noncopyable_function.hh:37