forked from mlkazar/lwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadtimer.h
More file actions
223 lines (186 loc) · 6.09 KB
/
threadtimer.h
File metadata and controls
223 lines (186 loc) · 6.09 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
222
223
/*
Copyright 2016-2020 Cazamar Systems
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __THREADTIMER_H_ENV__
#define __THREADTIMER_H_ENV__ 1
#include <sys/types.h>
#include <pthread.h>
#include "thread.h"
#include "threadmutex.h"
#include "dqueue.h"
class ThreadTimer;
/* helper class for simple sleep operations */
class ThreadTimerSleep {
uint32_t _ms;
ThreadMutex _mutex;
ThreadCond _cv;
static void condWakeup(ThreadTimer *timerp, void *contextp);
public:
ThreadTimerSleep() {
_cv.setMutex(&_mutex);
}
int32_t sleep(uint32_t ms);
};
/* The model for ThreadTimers is a little subtle. Because we're on an MP, there's
* an inherent race between canceling a timer and the callback from a timer. If a timer
* fires, it will be automatically canceled when you return. But also, the owner of a
* timer can cancel it at any time, including a nanosecond before the callback begins
* execution, when it is too late for the timer package to stop the callback.
*
* Thus, any timer callback needs to start with a call to _isDeleted, while holding a
* lock that protects the pointer to the user's ThreadTimer. If isDeleted says
* that the timer has been canceled, then the callback must return immediately,
* without accessing any thing through the context pointer. That's because the
* code that canceled the timer may have also deleted any structures holding the
* timer.
*/
class ThreadTimer {
/* statics used by the threadtimer server */
static pthread_mutex_t _timerMutex;
static pthread_cond_t _timerCond;
static int _noticeFds[2];
static int _threadRunning;
static void *timerManager(void *contextp);
static dqueue<ThreadTimer> _allTimers;
static int _didInit;
public:
typedef void Callback(ThreadTimer *timerp, void *contextp);
protected:
/* stuff present in all of the timer instances */
uint32_t _refCount;
uint8_t _canceled;
uint8_t _inQueue;
uint32_t _msecs;
uint64_t _expiration;
public:
ThreadTimer *_dqNextp;
ThreadTimer *_dqPrevp;
Callback *_callbackp;
void *_contextp;
public:
ThreadTimer(uint32_t ms, Callback *procp, void *contextp) {
_refCount = 1;
_canceled = 0;
_inQueue = 0;
_msecs = ms;
_callbackp = procp;
_contextp = contextp;
}
void start();
void hold() {
_refCount++;
}
/* called with global lock held */
void release() {
assert(_refCount > 0);
if (--_refCount == 0) {
/* make sure it isn't in a queue */
if (_inQueue) {
_allTimers.remove(this);
_inQueue = 0;
}
assert(_canceled);
delete this;
}
}
/* returns true if canceled, false if timer is already going to run; frees the timer
* structure (eventually, if it is already running).
*/
int32_t cancel();
int isCanceled() {
return _canceled;
}
static int32_t sleep(uint32_t ams) {
ThreadTimerSleep sleeper;
int32_t code;
code = sleeper.sleep(ams);
return code;
}
static void init();
};
/* the reason for all this complexity is that we can't recognize a canceled timer
* without using a static lock, since the context may be freed by the time the
* timer fires.
*/
class ThreadCondTimed {
static ThreadMutex _internalLock;
ThreadTimer *_timerp;
ThreadCond _cv;
ThreadMutex *_mutexp;
static void timerFired(ThreadTimer *timerp, void *contextp) {
ThreadCondTimed *p = (ThreadCondTimed *) contextp;
_internalLock.take();
if (timerp->isCanceled()) {
_internalLock.release();
return;
}
p->_cv.broadcast();
p->_timerp = NULL;
_internalLock.release();
}
public:
void setMutex(ThreadMutex *mutexp) {
_mutexp = mutexp;
}
ThreadCondTimed() {
_cv.setMutex(&_internalLock);
_mutexp = NULL;
_timerp = NULL;
}
ThreadCondTimed(ThreadMutex *mutexp) {
_mutexp = mutexp;
_timerp = NULL;
}
void broadcast() {
_internalLock.take();
_cv.broadcast();
_internalLock.release();
}
void wait() {
_internalLock.take();
_mutexp->release();
_cv.wait();
_internalLock.release();
_mutexp->take();
}
/* returns true if timer didn't fire, false if it did. Note that timer firing
* still might mean we were woken up by a broadcast, but if timer didn't fire,
* we were definitely woken by a broadcast. The return value really shouldn't be
* used, except by code coverage tests.
*/
int timedWait(uint32_t ms) {
int rval;
_internalLock.take();
_mutexp->release();
_timerp = new ThreadTimer(ms, &ThreadCondTimed::timerFired, this);
_timerp->start();
_cv.wait();
if (_timerp) {
_timerp->cancel();
_timerp = NULL;
rval = 1;
}
else
rval = 0;
_internalLock.release();
_mutexp->take();
return rval;
}
};
#endif /* __THREADTIMER_H_ENV__ */