|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#ifndef UTIL_TIMER_HPP |
| 20 | +#define UTIL_TIMER_HPP |
| 21 | + |
| 22 | +#include <timers> |
| 23 | + |
| 24 | +/** |
| 25 | + * @brief A start- and stoppable timer |
| 26 | + * @details A timer which can be started, stopped and restarted. |
| 27 | + * Uses the underlying Timers interface. |
| 28 | + * |
| 29 | + */ |
| 30 | +class Timer { |
| 31 | +public: |
| 32 | + using id_t = Timers::id_t; |
| 33 | + using duration_t = Timers::duration_t; |
| 34 | + using handler_t = Timers::handler_t; |
| 35 | + |
| 36 | + /** |
| 37 | + * @brief Constructs a Timer without a handler |
| 38 | + */ |
| 39 | + Timer() : Timer(nullptr) {} |
| 40 | + |
| 41 | + /** |
| 42 | + * @brief Constructs a Timer with a handler |
| 43 | + * |
| 44 | + * @param on_timeout function to be executed on timeout |
| 45 | + */ |
| 46 | + Timer(handler_t on_timeout) |
| 47 | + : on_timeout_{on_timeout}, running_{false} {} |
| 48 | + |
| 49 | + /** |
| 50 | + * @brief Start the timer with a timeout duration |
| 51 | + * @details Starts the timer (if not already running) |
| 52 | + * and stores the current timer id returned by the underlying Timers. |
| 53 | + * |
| 54 | + * Requires on_timeout to be set. |
| 55 | + * |
| 56 | + * @param duration until timing out |
| 57 | + */ |
| 58 | + inline void start(duration_t); |
| 59 | + |
| 60 | + /** |
| 61 | + * @brief Stops the timer |
| 62 | + * @details Stops the timer (if running) by |
| 63 | + * calling stop with the current id to the underlying Timers. |
| 64 | + */ |
| 65 | + inline void stop(); |
| 66 | + |
| 67 | + /** |
| 68 | + * @brief Restart the timer |
| 69 | + * @details First stops the timer (if running) |
| 70 | + * and then starts the timer with a new refreshed duration. |
| 71 | + * |
| 72 | + * @param duration until timing out |
| 73 | + */ |
| 74 | + inline void restart(duration_t); |
| 75 | + |
| 76 | + /** |
| 77 | + * @brief Sets the on timeout handler |
| 78 | + * @details Sets what to be done when the timer times out. |
| 79 | + * |
| 80 | + * @param on_timeout a timeout handler |
| 81 | + */ |
| 82 | + void set_on_timeout(handler_t on_timeout) |
| 83 | + { on_timeout_ = on_timeout; } |
| 84 | + |
| 85 | + /** |
| 86 | + * @brief If the timer is running (active) |
| 87 | + * |
| 88 | + * @return Wether the timer is running or not |
| 89 | + */ |
| 90 | + bool is_running() const |
| 91 | + { return running_; } |
| 92 | + |
| 93 | + /** |
| 94 | + * @brief Destroys the Timer |
| 95 | + * @details Makes sure to stop any eventual underlying Timer |
| 96 | + * if its running. |
| 97 | + */ |
| 98 | + ~Timer() |
| 99 | + { stop(); } |
| 100 | + |
| 101 | + /** Delete copy and move */ |
| 102 | + Timer(const Timer&) = delete; |
| 103 | + Timer(Timer&&) = delete; |
| 104 | + Timer& operator=(const Timer&) = delete; |
| 105 | + Timer& operator=(Timer&&) = delete; |
| 106 | + |
| 107 | +private: |
| 108 | + /** ID for the running timer. */ |
| 109 | + Timers::id_t id_; |
| 110 | + /** Function to execute on timeout */ |
| 111 | + handler_t on_timeout_; |
| 112 | + /** Wether the timer is running or not */ |
| 113 | + bool running_; |
| 114 | + |
| 115 | + /** |
| 116 | + * @brief Sets the timer to inactive before calling the user callback |
| 117 | + * @details Wraps the on timeout handler, setting the timer to not running |
| 118 | + * before calling the timeout handler. |
| 119 | + * This is the delegate being sent to the underlying Timers interface |
| 120 | + * |
| 121 | + * @param id the timer id returned by the Timers interface |
| 122 | + */ |
| 123 | + inline void _internal_timeout(id_t id); |
| 124 | + |
| 125 | +} __attribute__((packed)); // < class Timer |
| 126 | + |
| 127 | +inline void Timer::start(duration_t when) { |
| 128 | + Ensures(on_timeout_); |
| 129 | + if(!running_) { |
| 130 | + id_ = Timers::oneshot(when, {this, &Timer::_internal_timeout}); |
| 131 | + running_ = true; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +inline void Timer::stop() { |
| 136 | + if(running_) { |
| 137 | + Timers::stop(id_); |
| 138 | + running_ = false; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +inline void Timer::restart(duration_t when) { |
| 143 | + stop(); |
| 144 | + start(when); |
| 145 | +} |
| 146 | + |
| 147 | +inline void Timer::_internal_timeout(id_t id) { |
| 148 | + running_ = false; |
| 149 | + on_timeout_(id); |
| 150 | +} |
| 151 | + |
| 152 | +#endif |
0 commit comments