-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlooping_thread.hpp
More file actions
181 lines (165 loc) · 4.57 KB
/
looping_thread.hpp
File metadata and controls
181 lines (165 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* \brief Class for wrapping a thread that calls a routine periodically until the instance of this object is destroyed
*
* The period is the period of calling the routine, not the wait between the calls. It doesn't wait for this period when it's being destroyed, but it waits
* while the routine is running.
*
* \note The waiting is implemented using std::timed_mutex
*/
#ifndef LOOPING_THREAD_H
#define LOOPING_THREAD_H
#include <thread>
#include <functional>
#include <mutex>
#include <chrono>
#include <iostream>
class LoopingThread {
std::chrono::steady_clock::duration period_;
std::function<void()> routine_;
std::function<void(const std::exception&)> errorCallback_;
std::timed_mutex waitMutex_;
std::unique_lock<std::timed_mutex> waitLock_;
std::mutex pauseMutex_;
std::unique_lock<std::mutex> pauseLock_;
std::mutex resumeMutex_;
std::unique_lock<std::mutex> resumeLock_;
bool exiting_ = false;
bool paused_;
bool resetTimeOnPause_ = true;
bool catchUp_ = true;
std::thread worker_;
inline void work()
{
std::chrono::steady_clock::time_point awakenAt = std::chrono::steady_clock::now();
while (true) {
bool interrupted = false;
{
std::unique_lock<std::timed_mutex> lock(waitMutex_, std::try_to_lock);
if (lock.owns_lock())
interrupted = true;
else {
if (awakenAt >= std::chrono::steady_clock::now() && lock.try_lock_until(awakenAt))
interrupted = true;
}
if (resetTimeOnPause_) {
awakenAt = std::chrono::steady_clock::now() + period_;
resetTimeOnPause_ = false;
}
}
if (interrupted) {
if (exiting_) break;
pauseLock_.unlock();
std::unique_lock<std::mutex> lock(resumeMutex_);
pauseLock_.lock();
} else {
try {
routine_();
} catch(std::exception& e) {
errorCallback_(e);
} catch(...) {
errorCallback_(std::runtime_error("An unknown error has been thrown in a looping thread"));
}
if (catchUp_)
awakenAt += period_;
else
awakenAt = std::chrono::steady_clock::now() + period_;
}
}
}
public:
/*!
* \brief Default contructor, nothing is done if created this way
*/
inline LoopingThread() : routine_(nullptr)
{
}
/*!
* \brief Constructs the thread and starts running the routine periodically
* \param The calling period
* \param The function that is called periodically
* \param If the thread starts running or is paused until resume() is called
*/
inline LoopingThread(std::chrono::steady_clock::duration period, std::function<void()> routine, bool run = true) :
period_(period),
routine_(routine),
waitLock_(waitMutex_, std::defer_lock),
pauseLock_(pauseMutex_),
resumeLock_(resumeMutex_),
paused_(true),
resetTimeOnPause_(false),
worker_(&LoopingThread::work, this),
errorCallback_([](const std::exception& e) { std::cout << e.what() << std::endl; })
{
if (run)
resume();
}
/*!
* \brief The destructor, interrupts the wait for another routine call, but waits for the routine to end if it's running
*/
inline ~LoopingThread()
{
if (routine_) {
exiting_ = true;
if (paused_)
resumeLock_.unlock();
else
waitLock_.unlock();
worker_.join();
pauseLock_.unlock();
}
}
/*!
* \brief Pause the execution, will wait until the end of routine
*/
inline void pause(bool resetTime = true)
{
if (routine_) {
if (paused_) throw std::logic_error("Pausing a looping thread that is already paused");
paused_ = true;
waitLock_.unlock();
resumeLock_.lock();
std::unique_lock<std::mutex> pauseLock(pauseMutex_);
resetTimeOnPause_ = resetTime;
}
}
/*!
* \brief Resume paused execution
*/
inline void resume()
{
if (routine_) {
if (!paused_) throw std::logic_error("Resuming a looping thread that is not paused");
paused_ = false;
waitLock_.lock();
resumeLock_.unlock();
}
}
/*!
* \brief Changes the period
* \param The new calling period
*/
inline void setPeriod(std::chrono::steady_clock::duration newPeriod)
{
period_ = newPeriod;
}
/*!
* \brief Enables or disables the catch up feature
* \param If it should be enabled
*
* \note Catch up enabled causes the program call the routine only the proper number of times, so if the thread is delayed, the routine
* will still be called as many times as expected over a long time
*/
inline void setCatchUp(bool catchUp)
{
catchUp_ = catchUp;
}
/*!
* \brief Changes error callback
* \param errorCallback function which is called when exception is thrown in routine
*/
inline void setErrorCallback(std::function<void(const std::exception&)> errorCallback)
{
errorCallback_ = errorCallback;
}
};
#endif // LOOPING_THREAD_H