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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright 2015 Cloudius Systems
25 #include <seastar/util/std-compat.hh>
28 #include <seastar/core/future.hh>
29 #include <seastar/core/timer-set.hh>
33 using steady_clock_type = std::chrono::steady_clock;
35 template <typename Clock = steady_clock_type>
38 typedef typename Clock::time_point time_point;
39 typedef typename Clock::duration duration;
42 using callback_t = noncopyable_function<void()>;
43 boost::intrusive::list_member_hook<> _link;
46 compat::optional<duration> _period;
49 bool _expired = false;
50 void readd_periodic();
51 void arm_state(time_point until, compat::optional<duration> period) {
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);
67 explicit timer(callback_t&& callback) : _callback{std::move(callback)} {
70 void set_callback(callback_t&& callback) {
71 _callback = std::move(callback);
73 void arm(time_point until, compat::optional<duration> period = {});
74 void rearm(time_point until, compat::optional<duration> period = {}) {
80 void arm(duration delta) {
81 return arm(Clock::now() + delta);
83 void arm_periodic(duration delta) {
84 arm(Clock::now() + delta, {delta});
86 bool armed() const { return _armed; }
88 time_point get_timeout() {
92 friend class timer_set<timer, &timer::_link>;
95 extern template class timer<steady_clock_type>;