Repository navigation
Expand file tree
/
Copy pathtimer_run_loop.cpp
More file actions
221 lines (184 loc) · 4.7 KB
/
Copy pathtimer_run_loop.cpp
File metadata and controls
221 lines (184 loc) · 4.7 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Licensed to Bradley J. Snyder under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the
* Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef _TIMER_RUN_LOOP_SOURCE_
#define _TIMER_RUN_LOOP_SOURCE_
#include <thread>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unistd.h>
#include "timer_run_loop.hpp"
namespace ThreadPoolTimer
{
TimerRunLoop::TimerRunLoop() :
_thread(NULL)
{
_start();
}
TimerRunLoop::~TimerRunLoop()
{
_stop();
delete _thread;
}
TimerRunLoop& TimerRunLoop::getSingleton()
{
static TimerRunLoop *singleton = new TimerRunLoop();
return *singleton;
}
bool TimerRunLoop::getRunning()
{
std::lock_guard<std::mutex> lock( _timersMutex );
return _running;
}
void TimerRunLoop::start()
{
_start();
}
void TimerRunLoop::stop()
{
_stop();
}
void TimerRunLoop::registerTimer( TimerImpl &timer )
{
// Lock the mutex
std::lock_guard<std::mutex> lock( _timersMutex );
// Add to timers list
_timers.push_back(&timer);
// Lock-in the current time and set next trigger time
_updateNowTime();
_resetTimer(timer);
// Re-sort all the timers
_sortTimers();
// Notify the run loop thread that something's changed
_timersCondition.notify_one();
}
void TimerRunLoop::unRegisterTimer( TimerImpl &timer )
{
// Lock the mutex
std::lock_guard<std::mutex> lock( _timersMutex );
// Find and delete the timer from the list
int i = 0;
for( auto &tp : _timers )
{
if( tp == &timer ) {
_timers.erase(_timers.begin() + i);
break;
}
i++;
}
// Notify the run loop thread that something's changed
_timersCondition.notify_one();
}
bool timerCompare( TimerImpl *timerA, TimerImpl *timerB )
{
return (*timerA).getNextTrigger() < (*timerB).getNextTrigger();
}
void TimerRunLoop::_sortTimers()
{
std::sort( _timers.begin(), _timers.end(), timerCompare );
}
void TimerRunLoop::_updateNowTime()
{
_nowTime = std::chrono::steady_clock::now();
}
void TimerRunLoop::_resetTimer( TimerImpl &timer )
{
// Update the timer's next trigger time using its interval
timer.setNextTrigger( _nowTime + timer.getInterval() );
}
void TimerRunLoop::_triggerTimers()
{
_updateNowTime();
int triggered = 0;
for( auto &tp : _timers )
{
TimerImpl &timer = *tp;
if( timer.getNextTrigger() <= _nowTime )
{
_resetTimer(timer);
triggered++;
// TODO: create execution threads with a queue rather than giving each
// event its own thread
std::thread thread([&](){
timer.trigger();
});
thread.detach();
}
else
{
break;
}
}
if( triggered )
{
_sortTimers();
}
}
void TimerRunLoop::_start()
{
std::lock_guard<std::mutex> lock( _timersMutex );
if( !_running )
{
_running = true;
_thread = new std::thread([&](){
_loopTask();
});
}
}
void TimerRunLoop::_stop()
{
bool stopping = false;
std::unique_lock<std::mutex> lock( _timersMutex );
std::unique_lock<std::mutex> slock( _stopMutex );
if( _running )
{
stopping = true;
_running = false;
_timersCondition.notify_one();
lock.unlock();
_stopCondition.wait( slock );
slock.unlock();
}
_thread->join();
}
void TimerRunLoop::_loopTask()
{
std::unique_lock<std::mutex> lock(_timersMutex);
while(_running)
{
_triggerTimers();
std::chrono::steady_clock::time_point tillTime;
if( _timers.size() > 0 )
{
TimerImpl &t = *(_timers[0]);
tillTime = t.getNextTrigger();
}
else
{
tillTime = _nowTime + std::chrono::seconds(1);
}
std::cv_status result = _timersCondition.wait_until( lock, tillTime );
}
lock.unlock();
std::unique_lock<std::mutex> slock( _stopMutex );
_stopCondition.notify_one();
slock.unlock();
}
}
#endif