forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin_timers.c
364 lines (289 loc) · 9.18 KB
/
win_timers.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#if defined (_WIN32)
#include "my_global.h"
#include "my_thread.h" /* my_thread_init, my_thread_end */
#include "my_sys.h" /* my_message_local */
#include "my_timer.h" /* my_timer_t */
#include <windows.h> /* Timer Queue and IO completion port functions */
enum enum_timer_state { TIMER_SET=FALSE, TIMER_EXPIRED=TRUE };
// Timer notifier thread id.
static my_thread_handle timer_notify_thread;
// IO completion port handle
HANDLE io_compl_port= 0;
// Timer queue handle
HANDLE timer_queue= 0;
/**
Callback function registered to execute on timer expiration.
@param timer_data timer data passed to function.
@param timer_or_wait_fired flag to represent timer fired or signalled.
@remark this function is executed in timer owner thread when timer
expires.
*/
static void CALLBACK
timer_callback_function(PVOID timer_data, BOOLEAN timer_or_wait_fired __attribute__((unused)))
{
my_timer_t *timer= (my_timer_t *)timer_data;
DBUG_ASSERT(timer != NULL);
timer->id.timer_state= TIMER_EXPIRED;
PostQueuedCompletionStatus(io_compl_port, 0, (ULONG_PTR)timer, 0);
}
/**
Timer expiration notification thread.
@param arg Unused.
*/
static void*
timer_notify_thread_func(void *arg __attribute__((unused)))
{
DWORD bytes_transferred;
ULONG_PTR compl_key;
LPOVERLAPPED overlapped;
my_timer_t *timer;
my_thread_init();
while(1)
{
// Get IO Completion status.
if (GetQueuedCompletionStatus(io_compl_port, &bytes_transferred, &compl_key,
&overlapped, INFINITE) == 0)
break;
timer= (my_timer_t*)compl_key;
timer->notify_function(timer);
}
my_thread_end();
return NULL;
}
/**
Delete a timer.
@param timer Timer Object.
@param state The state of the timer at the time of deletion, either
signaled (0) or nonsignaled (1).
@return 0 On Success
-1 On error.
*/
static int
delete_timer(my_timer_t *timer, int *state)
{
int ret_val;
int retry_count= 3;
DBUG_ASSERT(timer != 0);
DBUG_ASSERT(timer_queue != 0);
if (state != NULL)
*state= 0;
if (timer->id.timer_handle)
{
do {
ret_val= DeleteTimerQueueTimer(timer_queue, timer->id.timer_handle, NULL);
if (ret_val != 0)
{
/**
From MSDN documentation of DeleteTimerQueueTimer:
------------------------------------------------------------------
BOOL WINAPI DeleteTimerQueueTimer(
_In_opt_ HANDLE TimerQueue,
_In_ HANDLE Timer,
_In_opt_ HANDLE CompletionEvent
);
...
If there are outstanding callback functions and CompletionEvent is
NULL, the function will fail and set the error code to
ERROR_IO_PENDING. This indicates that there are outstanding callback
functions. Those callbacks either will execute or are in the middle
of executing.
...
------------------------------------------------------------------
So we are here only in 2 cases,
1 When timer is *not* expired yet.
2 When timer is expired and callback function execution is
completed.
So here in case 1 timer.id->timer_state is TIMER_SET and
in case 2 timer.id->timer_state is TIMER_EXPIRED
(From MSDN documentation(pasted above), if timer callback function is
not yet executed or it is in the middle of execution then
DeleteTimerQueueTimer() fails. Hence when we are here, we are sure
that state is either TIMER_SET(case 1) or TIMER_EXPIRED(case 2))
Note:
timer.id->timer_state is set to TIMER_EXPIRED in
timer_callback_function(). This function is executed by the OS
thread on timer expiration.
On timer expiration, when callback function is in the middle of
execution or it is yet to be executed by OS thread the call to
DeleteTimerQueueTimer() fails with an error "ERROR_IO_PENDING".
In this case, timer.id->timer_state is not accessed in the current
code (please check else if block below).
Since timer.id->timer_state is not accessed in the current code
while it is getting modified in timer_callback_function,
no synchronization mechanism used.
Setting state to 1(non-signaled) if timer_state is not set to
"TIMER_EXPIRED"
*/
if (timer->id.timer_state != TIMER_EXPIRED && state != NULL)
*state= 1;
timer->id.timer_handle= 0;
}
else if (GetLastError() == ERROR_IO_PENDING)
{
/**
Timer is expired and timer callback function execution is not
yet completed.
Note: timer->id.timer_state is modified in callback function.
Accessing timer->id.timer_state here might result in
race conditions.
Currently we are not accessing timer->id.timer_state
here so not using any synchronization mechanism.
*/
timer->id.timer_handle= 0;
ret_val= 1;
}
else
{
/**
Timer deletion from queue failed and there are no outstanding
callback functions for this timer.
*/
if (--retry_count == 0)
{
my_message_local(ERROR_LEVEL, "Failed to delete timer (errno= %d).",
errno);
return -1;
}
}
} while (ret_val == 0);
}
return 0;
}
/**
Initialize internal components.
@return 0 On success
-1 On error.
*/
int
my_timer_initialize(void)
{
// Create timer queue.
timer_queue= CreateTimerQueue();
if (!timer_queue)
{
my_message_local(ERROR_LEVEL, "Failed to create Timer Queue (errno= %d)",
errno);
goto err;
}
// Create IO completion port.
io_compl_port= CreateIoCompletionPort(INVALID_HANDLE_VALUE, 0, 0, 0);
if (!io_compl_port)
{
my_message_local(ERROR_LEVEL,
"Failed to create IO completion port (errno= %d)",
errno);
goto err;
}
if (mysql_thread_create(key_thread_timer_notifier, &timer_notify_thread, 0,
timer_notify_thread_func, 0))
{
my_message_local(ERROR_LEVEL,
"Failed to create timer notify thread (errno= %d).",
errno);
goto err;
}
return 0;
err:
if (timer_queue)
{
DeleteTimerQueueEx(timer_queue, NULL);
timer_queue= 0;
}
if (io_compl_port)
{
CloseHandle(io_compl_port);
io_compl_port= 0;
}
return -1;
}
/**
Release any resources that were allocated as part of initialization.
*/
void
my_timer_deinitialize()
{
if (timer_queue)
{
DeleteTimerQueueEx(timer_queue, NULL);
timer_queue= 0;
}
if (io_compl_port)
{
CloseHandle(io_compl_port);
io_compl_port= 0;
}
my_thread_join(&timer_notify_thread, NULL);
}
/**
Create a timer object.
@param timer Timer object.
@return On success, 0.
*/
int
my_timer_create(my_timer_t *timer)
{
DBUG_ASSERT(timer_queue != 0);
timer->id.timer_handle= 0;
return 0;
}
/**
Set the time until the next expiration of the timer.
@param timer Timer object.
@param time Amount of time (in milliseconds) before the timer expires.
@return On success, 0.
On error, -1.
*/
int
my_timer_set(my_timer_t *timer, unsigned long time)
{
DBUG_ASSERT(timer != NULL);
DBUG_ASSERT(timer_queue != 0);
/**
If timer set previously is expired then it will not be
removed from the timer queue. Removing it before creating
a new timer queue timer.
*/
if (timer->id.timer_handle != 0)
my_timer_delete(timer);
timer->id.timer_state= TIMER_SET;
if (CreateTimerQueueTimer(&timer->id.timer_handle, timer_queue,
timer_callback_function, timer, time, 0,
WT_EXECUTEONLYONCE) == 0)
return -1;
return 0;
}
/**
Cancel the timer.
@param timer Timer object.
@param state The state of the timer at the time of cancellation, either
signaled (0) or nonsignaled (1).
@return On success, 0.
On error, -1.
*/
int
my_timer_cancel(my_timer_t *timer, int *state)
{
DBUG_ASSERT(state != NULL);
return delete_timer(timer, state);
}
/**
Delete a timer object.
@param timer Timer Object.
*/
void
my_timer_delete(my_timer_t *timer)
{
delete_timer(timer, NULL);
}
#endif