Mercurial > code
comparison modules/timer/timer.hpp @ 531:25e6380d0849
Timer: threaded timer import
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 02 Jun 2016 16:56:30 +0200 |
parents | 409cf1aa4af9 |
children | f7af45aeb197 |
comparison
equal
deleted
inserted
replaced
530:3e89f5684cef | 531:25e6380d0849 |
---|---|
1 /* | |
2 * timer.hpp -- threaded timers | |
3 * | |
4 * Copyright (c) 2016 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
1 #ifndef TIMER_HPP | 19 #ifndef TIMER_HPP |
2 #define TIMER_HPP | 20 #define TIMER_HPP |
3 | 21 |
22 /** | |
23 * \file timer.hpp | |
24 * \brief Threaded timers. | |
25 */ | |
26 | |
4 #include <atomic> | 27 #include <atomic> |
5 #include <condition_variable> | 28 #include <condition_variable> |
6 #include <chrono> | |
7 #include <cstdint> | 29 #include <cstdint> |
8 #include <functional> | 30 #include <functional> |
9 #include <mutex> | 31 #include <mutex> |
10 #include <thread> | 32 #include <thread> |
11 | 33 |
34 /** | |
35 * \brief Threaded timer. | |
36 */ | |
12 class Timer { | 37 class Timer { |
13 public: | 38 public: |
39 /** | |
40 * \brief Type of timer. | |
41 */ | |
14 enum Type { | 42 enum Type { |
43 /// Periodic timer. | |
15 Repeat, | 44 Repeat, |
45 /// Oneshot timer. | |
16 Single | 46 Single |
17 }; | 47 }; |
18 | 48 |
19 private: | 49 private: |
20 Type m_type; | 50 Type m_type; |
29 void wait(); | 59 void wait(); |
30 void runOne(); | 60 void runOne(); |
31 void runForever(); | 61 void runForever(); |
32 | 62 |
33 public: | 63 public: |
64 /** | |
65 * Constructor. | |
66 * | |
67 * \param type the type of timer | |
68 * \param delay the delay in milliseconds | |
69 */ | |
34 inline Timer(Type type, std::uint32_t delay) noexcept | 70 inline Timer(Type type, std::uint32_t delay) noexcept |
35 : m_type(type) | 71 : m_type(type) |
36 , m_delay(delay) | 72 , m_delay(delay) |
37 { | 73 { |
38 } | 74 } |
39 | 75 |
76 /** | |
77 * Stop the timer. | |
78 */ | |
40 inline ~Timer() | 79 inline ~Timer() |
41 { | 80 { |
42 stop(); | 81 stop(); |
43 } | 82 } |
44 | 83 |
84 /** | |
85 * Get the current handler. | |
86 * | |
87 * \return the handler | |
88 */ | |
45 inline const std::function<void ()> &handler() const noexcept | 89 inline const std::function<void ()> &handler() const noexcept |
46 { | 90 { |
47 return m_handler; | 91 return m_handler; |
48 } | 92 } |
49 | 93 |
94 /** | |
95 * Overloaded function. | |
96 * | |
97 * \return the handler | |
98 */ | |
50 inline std::function<void ()> &handler() noexcept | 99 inline std::function<void ()> &handler() noexcept |
51 { | 100 { |
52 return m_handler; | 101 return m_handler; |
53 } | 102 } |
54 | 103 |
104 /** | |
105 * Set the handler. | |
106 * | |
107 * \pre !isActive() | |
108 * \param handler the handler | |
109 */ | |
55 inline void setHandler(std::function<void ()> handler) | 110 inline void setHandler(std::function<void ()> handler) |
56 { | 111 { |
112 assert(!m_thread.joinable()); | |
113 | |
57 m_handler = std::move(handler); | 114 m_handler = std::move(handler); |
58 } | 115 } |
59 | 116 |
117 /** | |
118 * Tells if a pending thread is still active even if the execution has finished. | |
119 * | |
120 * \return true if active | |
121 */ | |
122 inline bool isActive() const noexcept | |
123 { | |
124 return m_thread.joinable(); | |
125 } | |
126 | |
127 /** | |
128 * Start the timer. | |
129 * | |
130 * \pre the timer must not be running | |
131 */ | |
60 void start(); | 132 void start(); |
61 | 133 |
134 /** | |
135 * Stop the timer. | |
136 * | |
137 * Can be safely called multiple times. | |
138 */ | |
62 void stop(); | 139 void stop(); |
63 }; | 140 }; |
64 | 141 |
65 #endif // !TIMER_HPP | 142 #endif // !TIMER_HPP |