blob: cb7a373ec15ae4b89bc044a4a092e33d5f2b23b0 (
plain)
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
|
/**
* @file
* @author 2017 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
* @copyright Simplified BSD
*
* @cond
* This program is free software: you can redistribute it and/or modify
* it under the terms of the FreeBSD license as published by the FreeBSD
* project.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the FreeBSD license along with this
* program. If not, see <http://www.opensource.org/licenses/bsd-license>.
* @endcond
*/
#include "PausableDelayedEventQueue.h"
#include <assert.h>
namespace uscxml {
PausableDelayedEventQueue::PausableDelayedEventQueue(DelayedEventQueueCallbacks* callbacks) : BasicDelayedEventQueue(callbacks) {
_pausedAt.tv_sec = 0;
_pausedAt.tv_usec = 0;
}
std::shared_ptr<DelayedEventQueueImpl> PausableDelayedEventQueue::create(DelayedEventQueueCallbacks* callbacks) {
return std::shared_ptr<PausableDelayedEventQueue>(new PausableDelayedEventQueue(callbacks));
}
void PausableDelayedEventQueue::pause() {
if(_pausedAt.tv_sec != 0 || _pausedAt.tv_usec != 0) {
return; // we are already paused!
}
evutil_gettimeofday(&_pausedAt, NULL); // remember when we paused
{
// Verbatim copy of stop() without cancelAllDelayed()
if (_isStarted) {
_isStarted = false;
event_base_loopbreak(_eventLoop);
}
if (_thread) {
_thread->join();
delete _thread;
_thread = NULL;
}
}
std::lock_guard<std::recursive_mutex> lock(_mutex);
// remove all events from libevent without deleting them
for(auto callbackData : _callbackData) {
Event data = callbackData.second.userData;
event_del(callbackData.second.event);
}
}
void PausableDelayedEventQueue::resume() {
if (_pausedAt.tv_sec != 0 || _pausedAt.tv_usec != 0) {
struct timeval now;
struct timeval pausedFor;
evutil_gettimeofday(&now, NULL);
evutil_timersub(&now, &_pausedAt, &pausedFor);
_pausedAt.tv_sec = 0;
_pausedAt.tv_usec = 0;
for(auto& callbackData : _callbackData) {
// add the time we were paused to all due times
evutil_timeradd(&callbackData.second.due, &pausedFor, &callbackData.second.due);
struct timeval remain;
evutil_timersub(&callbackData.second.due, &now, &remain);
#if 0
std::cout << "Now : " << now.tv_sec << "." << now.tv_usec << std::endl;
std::cout << "Paused : " << pausedFor.tv_sec << "." << pausedFor.tv_usec << std::endl;
std::cout << "Remaining: " << remain.tv_sec << "." << remain.tv_usec << std::endl;
#endif
assert(remain.tv_usec >= 0 && remain.tv_sec >= 0);
// reenqueue with libevent
event_add(callbackData.second.event, &remain);
}
}
start();
}
}
|