-
Notifications
You must be signed in to change notification settings - Fork 2
/
OSTimer.c
340 lines (317 loc) · 10.1 KB
/
OSTimer.c
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/***************************************************************************************************************:')
OSTimer.c
Timers.
Fabrice Le Bars
Created : 2011-07-30
***************************************************************************************************************:)*/
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#include "OSTimer.h"
#ifdef _WIN32
#ifdef WINCE
void CALLBACK _TimerCallbackThreadProc(UINT uTimerID, UINT uMsg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
{
TIMERTHREADPARAM* pTimerThreadParam = (TIMERTHREADPARAM*)dwUser;
UNREFERENCED_PARAMETER(uTimerID);
UNREFERENCED_PARAMETER(uMsg);
UNREFERENCED_PARAMETER(dw1);
UNREFERENCED_PARAMETER(dw2);
pTimerThreadParam->CallbackFunction(pTimerThreadParam->pCallbackParam, TRUE);
}
#endif // WINCE
#else
void* _TimerCallbackThreadProc(void* pParam)
{
TIMERTHREADPARAM* pTimerThreadParam = (TIMERTHREADPARAM*)pParam;
if (pthread_detach(pthread_self()) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerCallbackThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_detach failed. ",
pParam));
return 0;
}
pTimerThreadParam->CallbackFunction(pTimerThreadParam->pCallbackParam, TRUE);
if (pthread_mutex_lock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerCallbackThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_lock failed. ",
pParam));
return 0;
}
//PRINT_DEBUG_MESSAGE_OSTIMER(("nbTimerCallbackThreads=%d\n", pTimerThreadParam->nbTimerCallbackThreads));
pTimerThreadParam->nbTimerCallbackThreads--;
if (pTimerThreadParam->nbTimerCallbackThreads <= 0)
{
if (pthread_cond_broadcast(&pTimerThreadParam->cv) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerCallbackThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_cond_broadcast failed. ",
pParam));
pthread_mutex_unlock(&pTimerThreadParam->mutex);
return 0;
}
}
if (pthread_mutex_unlock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerCallbackThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_unlock failed. ",
pParam));
return 0;
}
return 0;
}
#ifndef USE_OLD_TIMER
void* _TimerThreadProc(void* pParam)
{
TIMERTHREADPARAM* pTimerThreadParam = (TIMERTHREADPARAM*)pParam;
pthread_t TimerCallbackThreadId;
uint64_t exp = 0;
ssize_t s = 0;
for (;;)
{
exp = 0;
// Wait for timer expirations.
s = read(pTimerThreadParam->timerfd, &exp, sizeof(uint64_t));
if (s != sizeof(uint64_t))
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
pParam));
}
else
{
// exp should contain the number of expirations that have occurred.
while (exp--)
{
//pTimerThreadParam->CallbackFunction(pTimerThreadParam->pCallbackParam, TRUE);
if (pTimerThreadParam->nbTimerCallbackThreads < MAX_NB_TIMER_CALLBACK_THREADS-1)
{
if (pthread_mutex_lock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_lock failed. ",
pParam));
}
pTimerThreadParam->nbTimerCallbackThreads++;
if (pthread_mutex_unlock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_unlock failed. ",
pParam));
}
if (pthread_create(&TimerCallbackThreadId, NULL, _TimerCallbackThreadProc, pTimerThreadParam) != EXIT_SUCCESS)
{
#ifdef TIMER_THREAD_PROC_DEBUG
// Because printf() may be a cancellation point, there might be a deadlock here
// if DeleteTimer() is called in the same time (nbTimerCallbackThreads will never be 0).
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_create failed. ",
pParam));
#endif // TIMER_THREAD_PROC_DEBUG
if (pthread_mutex_lock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_lock failed. ",
pParam));
}
pTimerThreadParam->nbTimerCallbackThreads--;
if (pTimerThreadParam->nbTimerCallbackThreads <= 0)
{
if (pthread_cond_broadcast(&pTimerThreadParam->cv) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_cond_broadcast failed. ",
pParam));
pthread_mutex_unlock(&pTimerThreadParam->mutex);
}
}
if (pthread_mutex_unlock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_unlock failed. ",
pParam));
}
}
}
}
}
} // for (;;)
return 0;
}
#else
void* _TimerThreadProc(void* pParam)
{
TIMERTHREADPARAM* pTimerThreadParam = (TIMERTHREADPARAM*)pParam;
pthread_t TimerCallbackThreadId;
struct timespec req;
// usleep() is considered as obsolete.
//usleep(pTimerThreadParam->DueTime*1000);
req.tv_sec = pTimerThreadParam->DueTime/1000; // Seconds.
req.tv_nsec = (pTimerThreadParam->DueTime%1000)*1000000; // Additional nanoseconds.
nanosleep(&req, NULL);
//pTimerThreadParam->CallbackFunction(pTimerThreadParam->pCallbackParam, TRUE);
if (pTimerThreadParam->nbTimerCallbackThreads < MAX_NB_TIMER_CALLBACK_THREADS-1)
{
if (pthread_mutex_lock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_lock failed. ",
pParam));
}
pTimerThreadParam->nbTimerCallbackThreads++;
if (pthread_mutex_unlock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_unlock failed. ",
pParam));
}
if (pthread_create(&TimerCallbackThreadId, NULL, _TimerCallbackThreadProc, pTimerThreadParam) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_create failed. ",
pParam));
if (pthread_mutex_lock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_lock failed. ",
pParam));
}
pTimerThreadParam->nbTimerCallbackThreads--;
if (pTimerThreadParam->nbTimerCallbackThreads <= 0)
{
if (pthread_cond_broadcast(&pTimerThreadParam->cv) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_cond_broadcast failed. ",
pParam));
pthread_mutex_unlock(&pTimerThreadParam->mutex);
}
}
if (pthread_mutex_unlock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_unlock failed. ",
pParam));
}
}
}
// If the period of the timer is 0, the timer is signaled once.
// If >0, the timer is periodic.
if (pTimerThreadParam->Period == 0)
{
return 0;
}
for (;;)
{
// usleep() is considered as obsolete.
//usleep(pTimerThreadParam->Period*1000);
req.tv_sec = pTimerThreadParam->Period/1000; // Seconds.
req.tv_nsec = (pTimerThreadParam->Period%1000)*1000000; // Additional nanoseconds.
nanosleep(&req, NULL);
//pTimerThreadParam->CallbackFunction(pTimerThreadParam->pCallbackParam, TRUE);
if (pTimerThreadParam->nbTimerCallbackThreads < MAX_NB_TIMER_CALLBACK_THREADS-1)
{
if (pthread_mutex_lock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_lock failed. ",
pParam));
}
pTimerThreadParam->nbTimerCallbackThreads++;
if (pthread_mutex_unlock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_unlock failed. ",
pParam));
}
if (pthread_create(&TimerCallbackThreadId, NULL, _TimerCallbackThreadProc, pTimerThreadParam) != EXIT_SUCCESS)
{
#ifdef TIMER_THREAD_PROC_DEBUG
// Because printf() may be a cancellation point, there might be a deadlock here
// if DeleteTimer() is called in the same time (nbTimerCallbackThreads will never be 0).
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_create failed. ",
pParam));
#endif // TIMER_THREAD_PROC_DEBUG
if (pthread_mutex_lock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_lock failed. ",
pParam));
}
pTimerThreadParam->nbTimerCallbackThreads--;
if (pTimerThreadParam->nbTimerCallbackThreads <= 0)
{
if (pthread_cond_broadcast(&pTimerThreadParam->cv) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_cond_broadcast failed. ",
pParam));
pthread_mutex_unlock(&pTimerThreadParam->mutex);
}
}
if (pthread_mutex_unlock(&pTimerThreadParam->mutex) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIMER(("_TimerThreadProc error (%s) : %s"
"(pParam=%#x)\n",
strtime_m(),
"pthread_mutex_unlock failed. ",
pParam));
}
}
}
}
return 0;
}
#endif // !USE_OLD_TIMER
#endif // _WIN32