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 <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>
31 
33 
48 
49 namespace seastar {
50 
51 using steady_clock_type = std::chrono::steady_clock;
52 
53 
56 
77 template <typename Clock = steady_clock_type>
78 class timer {
79 public:
80  typedef typename Clock::time_point time_point;
81  typedef typename Clock::duration duration;
82  typedef Clock clock;
83 private:
84  using callback_t = noncopyable_function<void()>;
85  boost::intrusive::list_member_hook<> _link;
86  scheduling_group _sg;
87  callback_t _callback;
88  time_point _expiry;
89  std::optional<duration> _period;
90  bool _armed = false;
91  bool _queued = false;
92  bool _expired = false;
93  void readd_periodic() noexcept;
94  void arm_state(time_point until, std::optional<duration> period) noexcept {
95  assert(!_armed);
96  _period = period;
97  _armed = true;
98  _expired = false;
99  _expiry = until;
100  _queued = true;
101  }
102 public:
104  timer() noexcept {}; // implementation is required (instead of = default) for noexcept due to a bug in gcc 9.3.1,
105  // since boost::intrusive::list_member_hook default constructor is not specified as noexcept.
110  timer(timer&& t) noexcept : _sg(t._sg), _callback(std::move(t._callback)), _expiry(std::move(t._expiry)), _period(std::move(t._period)),
111  _armed(t._armed), _queued(t._queued), _expired(t._expired) {
112  _link.swap_nodes(t._link);
113  t._queued = false;
114  t._armed = false;
115  }
120  timer(scheduling_group sg, noncopyable_function<void ()>&& callback) noexcept : _sg(sg), _callback{std::move(callback)} {
121  }
125  explicit timer(noncopyable_function<void ()>&& callback) noexcept : timer(current_scheduling_group(), std::move(callback)) {
126  }
133  void set_callback(scheduling_group sg, noncopyable_function<void ()>&& callback) noexcept {
134  _sg = sg;
135  _callback = std::move(callback);
136  }
140  void set_callback(noncopyable_function<void ()>&& callback) noexcept {
141  set_callback(current_scheduling_group(), std::move(callback));
142  }
154  void arm(time_point until, std::optional<duration> period = {}) noexcept;
162  void rearm(time_point until, std::optional<duration> period = {}) noexcept {
163  if (_armed) {
164  cancel();
165  }
166  arm(until, period);
167  }
176  void arm(duration delta) noexcept {
177  return arm(Clock::now() + delta);
178  }
183  void arm_periodic(duration delta) noexcept {
184  arm(Clock::now() + delta, {delta});
185  }
191  void rearm_periodic(duration delta) noexcept {
192  if (_armed) {
193  cancel();
194  }
195  arm_periodic(delta);
196  }
200  bool armed() const noexcept { return _armed; }
207  bool cancel() noexcept;
212  time_point get_timeout() const noexcept {
213  return _expiry;
214  }
215  friend class reactor;
216  friend class timer_set<timer, &timer::_link>;
217 };
218 
219 extern template class timer<steady_clock_type>;
220 
221 
223 
224 }
225 
seastar::noncopyable_function< void()>
seastar::timer::arm_periodic
void arm_periodic(duration delta) noexcept
Definition: timer.hh:183
seastar::now
future now()
Returns a ready future.
Definition: later.hh:35
seastar
Seastar API namespace.
Definition: abort_on_ebadf.hh:24
seastar::timer::set_callback
void set_callback(noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:140
seastar::reactor
Definition: reactor.hh:187
seastar::timer::~timer
~timer()
Destroys the timer. The timer is cancelled if armed.
seastar::timer::get_timeout
time_point get_timeout() const noexcept
Definition: timer.hh:212
seastar::timer::rearm_periodic
void rearm_periodic(duration delta) noexcept
Definition: timer.hh:191
seastar::timer::timer
timer(timer &&t) noexcept
Definition: timer.hh:110
seastar::timer::cancel
bool cancel() noexcept
seastar::timer::set_callback
void set_callback(scheduling_group sg, noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:133
seastar::current_scheduling_group
scheduling_group current_scheduling_group() noexcept
Returns the current scheduling group.
Definition: scheduling.hh:339
seastar::timer::timer
timer(noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:125
scheduling.hh
seastar::timer_set
Definition: timer-set.hh:37
seastar::timer
Definition: timer.hh:78
seastar::timer::armed
bool armed() const noexcept
Definition: timer.hh:200
seastar::timer::rearm
void rearm(time_point until, std::optional< duration > period={}) noexcept
Definition: timer.hh:162
seastar::timer::arm
void arm(duration delta) noexcept
Definition: timer.hh:176
seastar::timer::arm
void arm(time_point until, std::optional< duration > period={}) noexcept
seastar::timer::timer
timer(scheduling_group sg, noncopyable_function< void()> &&callback) noexcept
Definition: timer.hh:120
seastar::timer::timer
timer() noexcept
Constructs a timer with no callback set and no expiration time.
Definition: timer.hh:104
seastar::noncopyable_function
Definition: noncopyable_function.hh:33
seastar::scheduling_group
Identifies function calls that are accounted as a group.
Definition: scheduling.hh:251