-
Notifications
You must be signed in to change notification settings - Fork 64
/
aioWin.c
534 lines (388 loc) · 11.7 KB
/
aioWin.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#include "winsock2.h"
#include "pharovm/pharo.h"
#include "sqaio.h"
#include "windows.h"
void heartbeat_poll_enter(long microSeconds);
void heartbeat_poll_exit(long microSeconds);
typedef struct _AioFileDescriptor {
int fd;
void* clientData;
int flags;
int mask;
aioHandler handlerFn;
HANDLE readEvent;
HANDLE writeEvent;
struct _AioFileDescriptor* next;
} AioFileDescriptor;
AioFileDescriptor* fileDescriptorList = NULL;
HANDLE interruptEvent;
HANDLE interruptMultiWait;
AioFileDescriptor * aioFileDescriptor_new(){
AioFileDescriptor* last;
AioFileDescriptor* aNewFD = malloc(sizeof(AioFileDescriptor));
if(fileDescriptorList == NULL){
fileDescriptorList = aNewFD;
return aNewFD;
}
last = fileDescriptorList;
while(last->next != NULL){
last = last->next;
}
last->next = aNewFD;
return aNewFD;
}
AioFileDescriptor * aioFileDescriptor_find(int fd){
AioFileDescriptor* found;
if(fileDescriptorList == NULL){
return NULL;
}
found = fileDescriptorList;
while(found != NULL && found->fd != fd){
found = found->next;
}
return found;
}
void aioFileDescriptor_remove(int fd){
AioFileDescriptor* found;
AioFileDescriptor* previous;
if(fileDescriptorList == NULL){
return;
}
found = fileDescriptorList;
previous = NULL;
while(found != NULL && found->fd != fd){
previous = found;
found = found->next;
}
if(!found) {
return;
}
if(previous == NULL){
fileDescriptorList = found->next;
}else{
previous->next = found->next;
}
if((found->flags & AIO_EXT) == 0){
WSACloseEvent(found->readEvent);
WSACloseEvent(found->writeEvent);
}
free(found);
}
long aioFileDescriptor_numberOfHandles(){
AioFileDescriptor* element = fileDescriptorList;
long count = 0;
while(element){
if(element->readEvent != NULL) count++;
if(element->writeEvent != NULL) count++;
element = element->next;
}
return count;
}
EXPORT(void) aioFileDescriptor_printHandlers(){
AioFileDescriptor* element = fileDescriptorList;
long count = 0;
printf("List of registered aioHandlers\n");
printf("==============================\n\n");
while(element){
printf("FD: %d Mask: %d Flags: %d ClientData %p handlerFn %p\n", element->fd, element->mask, element->flags, element->clientData, element->handlerFn);
element = element->next;
}
}
void aioFileDescriptor_fillHandles(HANDLE* handles){
AioFileDescriptor* element = fileDescriptorList;
long index = 0;
while(element){
if(element->readEvent != NULL){
handles[index] = element->readEvent;
index++;
}
if(element->writeEvent != NULL){
handles[index] = element->writeEvent;
index++;
}
element = element->next;
}
}
void aioFileDescriptor_signal_withHandle(HANDLE event){
AioFileDescriptor* element = fileDescriptorList;
while(element){
if(element->readEvent == event){
/**
* The event should be reset once it has been processed.
*/
WSAResetEvent(element->readEvent);
//We set the event to 0 so it is not recalled after
WSAEventSelect(element->fd, element->readEvent, 0);
element->handlerFn(element->fd, element->clientData, AIO_R);
return;
}
if(element->writeEvent == event){
/**
* The event should be reset once it has been processed.
*/
WSAResetEvent(element->writeEvent);
//We set the event to 0 so it is not recalled after
WSAEventSelect(element->fd, element->writeEvent, 0);
element->handlerFn(element->fd, element->clientData, AIO_W);
return;
}
element = element->next;
}
}
EXPORT(void) aioInit(void){
interruptEvent = CreateEventW(NULL, TRUE, FALSE, L"InterruptEvent");
if(!interruptEvent){
char* msg = formatMessageFromErrorCode(GetLastError());
logError("Impossible to createEvent: %s", msg);
free(msg);
exit(1);
}
interruptMultiWait = CreateEventW(NULL, TRUE, FALSE, L"InterruptMultiWaitEvent");
if(!interruptMultiWait){
char* msg = formatMessageFromErrorCode(GetLastError());
logError("Impossible to createEvent: %s", msg);
free(msg);
exit(1);
}
}
EXPORT(void) aioFini(void){
CloseHandle(interruptEvent);
CloseHandle(interruptMultiWait);
}
EXPORT(void) aioEnable(int fd, void *clientData, int flags){
AioFileDescriptor * aioFileDescriptor;
aioFileDescriptor = aioFileDescriptor_find(fd);
if(!aioFileDescriptor){
aioFileDescriptor = aioFileDescriptor_new();
aioFileDescriptor->next = NULL;
}
aioFileDescriptor->fd = fd;
aioFileDescriptor->clientData = clientData;
aioFileDescriptor->flags = flags;
aioFileDescriptor->readEvent = (HANDLE)WSACreateEvent();
if(aioFileDescriptor->readEvent == WSA_INVALID_EVENT){
int lastError = WSAGetLastError();
logError("Error WSACreateEvent READ: %ld", lastError);
}
aioFileDescriptor->writeEvent = (HANDLE)WSACreateEvent();
if(aioFileDescriptor->writeEvent == WSA_INVALID_EVENT){
int lastError = WSAGetLastError();
logError("Error WSACreateEvent WRITE: %ld", lastError);
}
aioFileDescriptor->mask = 0;
WSAEventSelect(aioFileDescriptor->fd, aioFileDescriptor->writeEvent, 0);
WSAEventSelect(aioFileDescriptor->fd, aioFileDescriptor->readEvent, 0);
u_long iMode = 1;
int iResult;
iResult = ioctlsocket(fd, FIONBIO, &iMode);
if (iResult != NO_ERROR){
char* msg = formatMessageFromErrorCode(GetLastError());
logError("ioctlsocket(FIONBIO, 1): %s", msg);
free(msg);
}
}
EXPORT(void) aioHandle(int fd, aioHandler handlerFn, int mask){
AioFileDescriptor * aioFileDescriptor;
char buf[100];
aioFileDescriptor = aioFileDescriptor_find(fd);
if(!aioFileDescriptor){
return;
}
aioFileDescriptor->handlerFn = handlerFn;
aioFileDescriptor->mask = mask;
/**
* Remember to reset the event once is processed
*/
if(mask & AIO_R){
WSAEventSelect(aioFileDescriptor->fd, aioFileDescriptor->readEvent, FD_READ | FD_ACCEPT | FD_OOB | FD_CLOSE);
//This recv will always generates a WOULDBLOCK, but this is needed to generate the correct event in Windows.
recv(aioFileDescriptor->fd, (void*)buf, 100, MSG_PEEK);
return;
}
if(mask & AIO_W){
WSAEventSelect(aioFileDescriptor->fd, aioFileDescriptor->writeEvent, FD_WRITE);
return;
}
}
EXPORT(void) aioSuspend(int fd, int mask){
/**
* TODO: It is not used, so we don't implement it now
*/
printf("No implemented");
}
EXPORT(void) aioDisable(int fd){
aioFileDescriptor_remove(fd);
}
EXPORT(void) aioEnableExternalHandler(int fd, HANDLE handle, void *clientData, aioHandler handlerFn, int mask){
AioFileDescriptor * aioFileDescriptor;
aioFileDescriptor = aioFileDescriptor_find(fd);
if(!aioFileDescriptor){
aioFileDescriptor = aioFileDescriptor_new();
aioFileDescriptor->next = NULL;
}
aioFileDescriptor->fd = fd;
aioFileDescriptor->clientData = clientData;
aioFileDescriptor->flags = AIO_EXT;
if(mask & AIO_R){
aioFileDescriptor->readEvent = handle;
aioFileDescriptor->writeEvent = NULL;
}else{
aioFileDescriptor->readEvent = NULL;
aioFileDescriptor->writeEvent = handle;
}
aioFileDescriptor->handlerFn = handlerFn;
aioFileDescriptor->mask = mask;
}
/*
* As Pipes may not signal an event when there is data we check if any of the handles is a pipe and check if there
* are data to read, if there is, we signal the handler.
*/
static int checkHandlesForPipes(HANDLE* handlesToQuery, long size){
int hasEvent = 0;
for(int i = 0 ; i < size ; i++){
if (GetFileType(handlesToQuery[i]) == FILE_TYPE_PIPE ){
DWORD maxDataAvailable;
DWORD toRead;
PeekNamedPipe(handlesToQuery[i],
NULL,
0,
NULL,
&maxDataAvailable,
NULL);
if(maxDataAvailable > 0){
aioFileDescriptor_signal_withHandle(handlesToQuery[i]);
hasEvent = 1;
}
}
}
return hasEvent;
}
static int checkEventsInHandles(HANDLE* handlesToQuery, int size){
int hasEvent = 0;
for(int i=0; i < size; i++){
if(WaitForSingleObject(handlesToQuery[i], 0) == WAIT_OBJECT_0) {
aioFileDescriptor_signal_withHandle(handlesToQuery[i]);
hasEvent = 1;
}
}
hasEvent |= checkHandlesForPipes(handlesToQuery, size);
return hasEvent;
}
struct sliceData {
HANDLE* handles;
int size;
long microSeconds;
};
DWORD WINAPI waitHandlesThreadFunction(struct sliceData* sliceData ){
DWORD returnValue;
returnValue = WaitForMultipleObjectsEx(sliceData->size, sliceData->handles, FALSE, sliceData->microSeconds / 1000, FALSE);
free(sliceData->handles);
free(sliceData);
return 0;
}
static HANDLE sliceWaitForMultipleObjects(HANDLE* allHandles, int initialIndex, int sizeToProcess, long microSeconds){
HANDLE r;
HANDLE* handles;
struct sliceData* sliceData = malloc(sizeof(struct sliceData));
sliceData->handles = &(allHandles[initialIndex]);
sliceData->size = sizeToProcess;
sliceData->microSeconds = microSeconds;
handles = malloc(sizeof(HANDLE) * sizeToProcess);
for(int i = 0; i < sizeToProcess; i++){
handles[i] = sliceData->handles[i];
}
sliceData->handles = handles;
r = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) waitHandlesThreadFunction, sliceData, 0, NULL);
if(r)
return r;
int lastError = GetLastError();
char* msg = formatMessageFromErrorCode(lastError);
logError("Error CreateThread: %d - %s", lastError,msg);
return NULL;
}
EXPORT(long) aioPoll(long microSeconds){
HANDLE* allHandles;
HANDLE* waitingHandles;
int numberOfThreads;
DWORD returnValue;
AioFileDescriptor* signaled;
int hasEvents = 0;
/*
* As we only can test for MAXIMUM_WAIT_OBJECTS in a single shot,
* we are going to divide them in MAXIMUM_WAIT_OBJECTS - 1 threads.
* The remaining slot is used by the interruptEvent.
*/
//We need an array with all the handles to process
long size = aioFileDescriptor_numberOfHandles();
allHandles = malloc(sizeof(HANDLE) *size);
aioFileDescriptor_fillHandles(allHandles);
//Let's calculate the number of threads needed.
numberOfThreads = size / MAXIMUM_WAIT_OBJECTS;
if(size % MAXIMUM_WAIT_OBJECTS){
numberOfThreads++;
}
/*
* If the number of threads is bigger than the ones I can manage I will handle as the timeout has arrive.
* This is not the best, but it will not break the behaviour.
*/
if((numberOfThreads + 1) > MAXIMUM_WAIT_OBJECTS){
logTrace("More threads than MAXIMUM_WAIT_OBJECTS, just checking one by one");
checkEventsInHandles(allHandles, size);
free(allHandles);
return 1;
}
waitingHandles = malloc(sizeof(HANDLE) * (numberOfThreads + 1));
heartbeat_poll_enter(microSeconds);
/*
* We pass the interrupt event as the first handler
*/
waitingHandles[0] = interruptEvent;
int remainingSize = size;
int initialIndex = 0;
int sizeToProcess = 0;
for(int i=1; i <= numberOfThreads; i++){
sizeToProcess = remainingSize > MAXIMUM_WAIT_OBJECTS ? MAXIMUM_WAIT_OBJECTS : remainingSize;
waitingHandles[i] = sliceWaitForMultipleObjects(allHandles, initialIndex, sizeToProcess, microSeconds);
if(waitingHandles[i] == NULL){
for(int j=1; j < i; j++){
CloseHandle(waitingHandles[j]);
}
free(waitingHandles);
free(allHandles);
return 0;
}
remainingSize = remainingSize - MAXIMUM_WAIT_OBJECTS;
initialIndex = initialIndex + MAXIMUM_WAIT_OBJECTS;
}
returnValue = WaitForMultipleObjectsEx(numberOfThreads + 1, waitingHandles, FALSE, microSeconds / 1000, FALSE);
/*
* Closing handles
*/
for(int i=1; i <= numberOfThreads; i++){
CloseHandle(waitingHandles[i]);
}
if(returnValue == WAIT_FAILED){
int lastError = GetLastError();
char* msg = formatMessageFromErrorCode(lastError);
logError("Error WaitForMultipleObjecsEx: %d - %s", lastError,msg);
logError("%d - %p - %ld", size+1, waitingHandles, microSeconds);
free(msg);
free(waitingHandles);
free(allHandles);
return 0;
}
heartbeat_poll_exit(microSeconds);
/*
* If interruptEvent was signalled (to interrupt the timeout) we need to clear it.
*/
if(returnValue == WAIT_OBJECT_0){
ResetEvent(interruptEvent);
}
hasEvents = checkEventsInHandles(allHandles, size);
free(waitingHandles);
free(allHandles);
return hasEvents;
}
EXPORT(void) aioInterruptPoll(){
SetEvent(interruptEvent);
}