-
Notifications
You must be signed in to change notification settings - Fork 68
/
aioOSX.c
447 lines (348 loc) · 10.8 KB
/
aioOSX.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
/*
* I am a new implementation of AIO using kqueue for OSX
*
* This version is supposed to work correctly with pipes and also
* it is supposed to be faster and cheaper.
*
* Sometimes... I think it is too good to be true...
*
*/
#include "pharovm/debug.h"
#include "pharovm/semaphores/platformSemaphore.h"
#include "sqaio.h"
#include "sqMemoryFence.h"
#include "sqaio.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/event.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#ifndef NULL
# define NULL 0
#endif
#ifndef true
# define true 1
#endif
#ifndef false
# define false 0
#endif
#define INCOMING_EVENTS_SIZE 50
/*
* This is the struct that I am keeping for the registered FD
*/
typedef struct _AioOSXDescriptor {
int fd;
void* clientData;
aioHandler readHandlerFn;
aioHandler writeHandlerFn;
struct _AioOSXDescriptor* next;
} AioOSXDescriptor;
/*
* I have to keep a list of the registered FDs as the operations are divided in two functions
* I only need to use the aioHandle, but I need information from the aioEnable
*/
AioOSXDescriptor* descriptorList = NULL;
/*
* I can access the elements in the list
*/
AioOSXDescriptor* AioOSXDescriptor_find(int fd);
void AioOSXDescriptor_remove(int fd);
/*
* This is kqueue used in the poll of the events.
*/
int kqueueDescriptor;
/*
* These functions are used to notify the heartbeat if we are entering and leaving a long pause.
* Maybe the heartbeat want to stop if we are in a long pause.
*/
void heartbeat_poll_enter(long microSeconds);
void heartbeat_poll_exit(long microSeconds);
static int aio_handle_events(struct kevent* changes, int numberOfChanges, long microSecondsTimeout);
/*
* This is important, the AIO poll should only do a long pause if there is no pending signals for semaphores.
* Check ExternalSemaphores to understand this function.
*/
int isPendingSemaphores();
/*
* The access to the pendingInterruption variable is done through the use of a mutex
*/
Semaphore * interruptFIFOMutex;
volatile int pendingInterruption = 0;
volatile int isPooling = 0;
#define INTERRUPT_EVENT_ID 0
/*
* I initialize the AIO infrastructure
*/
EXPORT(void)
aioInit(void){
struct kevent userEvent;
if((kqueueDescriptor = kqueue()) < 0) {
logErrorFromErrno("kqueue");
}
interruptFIFOMutex = platform_semaphore_new(1);
EV_SET(&userEvent, INTERRUPT_EVENT_ID, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, NULL);
kevent(kqueueDescriptor, &userEvent, 1, NULL, 0, NULL);
}
/*
* I process the changes and then process the events calling the handlers
* if there is one.
*
* I return if messages has been processed.
* Also I clean up the pipe if there are notifications.
*/
static int
aio_handle_events(struct kevent* changes, int numberOfChanges, long microSecondsTimeout){
struct kevent incomingEvents[INCOMING_EVENTS_SIZE];
int keventReturn;
struct timespec timeout;
//I notify the heartbeat of a pause
heartbeat_poll_enter(microSecondsTimeout);
sqLowLevelMFence();
isPooling = 1;
timeout.tv_nsec = (microSecondsTimeout % 1000000) * 1000;
timeout.tv_sec = microSecondsTimeout / 1000000;
keventReturn = kevent(kqueueDescriptor, changes, numberOfChanges, incomingEvents, INCOMING_EVENTS_SIZE, &timeout);
sqLowLevelMFence();
isPooling = 0;
interruptFIFOMutex->wait(interruptFIFOMutex);
pendingInterruption = false;
interruptFIFOMutex->signal(interruptFIFOMutex);
//I notify the heartbeat of the end of the pause
heartbeat_poll_exit(microSecondsTimeout);
if(keventReturn == -1){
if(errno != EINTR){
logErrorFromErrno("kevent");
}
return 0;
}
if(keventReturn == 0){
return 0;
}
for(int index = 0; index < keventReturn; index++){
//First I check if the event is an error in the registration
if((incomingEvents[index].flags & EV_ERROR) && (incomingEvents[index].flags & EV_ADD)){
int previousErrno = errno;
logError("Error registering FD: %d", (int) incomingEvents[index].ident);
errno = incomingEvents[index].data;
logErrorFromErrno("Registering event");
errno = previousErrno;
}else{
//If the event is not of the signal pipe I process them
if(incomingEvents[index].filter != EVFILT_USER){
//If not is a regular registered FD
AioOSXDescriptor *descriptor = (AioOSXDescriptor*)incomingEvents[index].udata;
logTrace("Event %d %hd %hd %hd %hd", incomingEvents[index].ident, incomingEvents[index].filter, EVFILT_EXCEPT, EVFILT_READ, EVFILT_WRITE );
// Check if we have an exception
/*
OUT OF BAND data comes as an error.
We ignore it
*/
if(incomingEvents[index].filter == EVFILT_EXCEPT
&& ((incomingEvents[index].flags & EV_ERROR) == EV_ERROR)
&& ((incomingEvents[index].flags & NOTE_OOB) != NOTE_OOB)){
logTrace("Event Flags: %x Data: %d %s %x %x %x", incomingEvents[index].flags, incomingEvents[index].data, strerror(incomingEvents[index].flags),EV_EOF, EV_ERROR, NOTE_OOB);
if(descriptor->readHandlerFn){
descriptor->readHandlerFn(incomingEvents[index].ident, descriptor->clientData, AIO_R | AIO_X);
}else{
if(descriptor->writeHandlerFn)
descriptor->writeHandlerFn(incomingEvents[index].ident, descriptor->clientData, AIO_W | AIO_X);
}
}else{
if(incomingEvents[index].filter == EVFILT_READ){
if(descriptor->readHandlerFn)
descriptor->readHandlerFn(incomingEvents[index].ident, descriptor->clientData, AIO_R);
}
if(incomingEvents[index].filter == EVFILT_WRITE){
if(descriptor->writeHandlerFn)
descriptor->writeHandlerFn(incomingEvents[index].ident, descriptor->clientData, AIO_W);
}
}
}
}
}
return 1;
}
/*
* There is no implementation of the shutdown.
*/
EXPORT(void)
aioFini(void){
}
/*
* This is the entry point to the aioPoll
* The parameter is the maximum time the poll will stop.
* The pause can be smaller if there are pending interruptions or pending signals to semaphores
* from external semaphores.
* Also if there is IO operations it will return ASAP.
*/
EXPORT(long)
aioPoll(long microSeconds){
long timeout;
interruptFIFOMutex->wait(interruptFIFOMutex);
if(pendingInterruption || isPendingSemaphores()){
timeout = 0;
}else{
timeout = microSeconds;
}
if(pendingInterruption){
pendingInterruption = false;
}
interruptFIFOMutex->signal(interruptFIFOMutex);
return aio_handle_events(NULL, 0, timeout);
}
/*
* With this is is possible to interrupt a long AIO poll.
* The external semaphores uses this function to interrupt the poll loop.
*/
EXPORT(void)
aioInterruptPoll(){
int n;
struct kevent userEvent;
sqLowLevelMFence();
if(isPooling){
EV_SET(&userEvent, INTERRUPT_EVENT_ID, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
kevent(kqueueDescriptor, &userEvent, 1, NULL, 0, NULL);
}
interruptFIFOMutex->wait(interruptFIFOMutex);
pendingInterruption = true;
interruptFIFOMutex->signal(interruptFIFOMutex);
}
/*
* I am part of the API of AIO
* I enable the FD to use AIO.
* The possible flags are here: AIO_EXT if the FD is external (and we should not change its properties).
* This function should be call to each FD to use.
*/
EXPORT(void)
aioEnable(int fd, void *clientData, int flags){
AioOSXDescriptor * descriptor;
descriptor = AioOSXDescriptor_find(fd);
if(descriptor == NULL){
descriptor = malloc(sizeof(AioOSXDescriptor));
descriptor->readHandlerFn = NULL;
descriptor->writeHandlerFn = NULL;
descriptor->next = descriptorList;
descriptorList = descriptor;
}
descriptor->fd = fd;
descriptor->clientData = clientData;
if ((flags & AIO_EXT) != AIO_EXT) {
/*
* enable non-blocking asynchronous i/o and delivery of SIGIO
* to the active process
*/
int arg;
if (fcntl(fd, F_SETOWN, getpid()) < 0)
logErrorFromErrno("fcntl(F_SETOWN, getpid())");
if ((arg = fcntl(fd, F_GETFL, 0)) < 0)
logErrorFromErrno("fcntl(F_GETFL)");
if (fcntl(fd, F_SETFL, arg | O_NONBLOCK | O_ASYNC) < 0)
logErrorFromErrno("fcntl(F_SETFL, O_ASYNC)");
}
}
/*
* This function is part of the API
* This is used to suspend the receive of events.
* The mask parameter says which handlers to suspend, it can be any combinatio of
*
* - AIO_R
* - AIO_W
*/
EXPORT(void)
aioSuspend(int fd, int mask){
int cant = 0;
int nextIndex = 0;
struct kevent newEvents[3];
AioOSXDescriptor *descriptor = AioOSXDescriptor_find(fd);
if(descriptor == NULL){
logWarn("Suspending a FD that is not present: %d - IGNORING", fd);
return;
}
if((mask & AIO_X) == AIO_X){
EV_SET(&newEvents[nextIndex], fd, EVFILT_EXCEPT, EV_DELETE, 0, 0, descriptor);
nextIndex++;
cant++;
}
if((mask & AIO_R) == AIO_R){
descriptor->readHandlerFn = NULL;
EV_SET(&newEvents[nextIndex], fd, EVFILT_READ, EV_DELETE, 0, 0, descriptor);
nextIndex++;
cant++;
}
if((mask & AIO_W) == AIO_W){
descriptor->writeHandlerFn = NULL;
EV_SET(&newEvents[nextIndex], fd, EVFILT_WRITE, EV_DELETE, 0, 0, descriptor);
nextIndex++;
cant++;
}
aio_handle_events(newEvents, cant, 0);
}
/*
* I disable all the events of a given FD and I forget about it!
*/
EXPORT(void)
aioDisable(int fd){
aioSuspend(fd, AIO_RWX);
AioOSXDescriptor_remove(fd);
}
/*
* This function is part of the API
* This is used to enable the receive of events.
* The mask parameter says which handlers to receive with this handle, it can be any combination of:
*
* - AIO_R
* - AIO_W
*
* Once the event arrives and the handle is notified, it will be disabled.
* If the same event is desired again, it should be re-register.
*/
EXPORT(void)
aioHandle(int fd, aioHandler handlerFn, int mask){
struct kevent newEvents[3];
AioOSXDescriptor *descriptor = AioOSXDescriptor_find(fd);
if(descriptor == NULL){
logWarn("Enabling a FD that is not present: %d - IGNORING", fd);
return;
}
int hasRead = (mask & AIO_R) == AIO_R;
int hasWrite = (mask & AIO_W) == AIO_W;
int hasExceptions = (mask & AIO_X) == AIO_X;
EV_SET(&newEvents[0], fd, EVFILT_EXCEPT, hasExceptions?(EV_ADD | EV_ONESHOT):EV_DELETE, 0, 0, descriptor);
descriptor->readHandlerFn = hasRead ? handlerFn : NULL;
EV_SET(&newEvents[1], fd, EVFILT_READ, hasRead?(EV_ADD | EV_ONESHOT):EV_DELETE, 0, 0, descriptor);
descriptor->writeHandlerFn = hasWrite ? handlerFn : NULL;
EV_SET(&newEvents[2], fd, EVFILT_WRITE, hasWrite?(EV_ADD | EV_ONESHOT):EV_DELETE, 0, 0, descriptor);
aio_handle_events(newEvents, 3, 0);
}
AioOSXDescriptor* AioOSXDescriptor_find(int fd){
AioOSXDescriptor* found;
found = descriptorList;
while(found != NULL){
if(found->fd == fd)
return found;
found = found->next;
}
return NULL;
}
void AioOSXDescriptor_remove(int fd){
AioOSXDescriptor* found;
AioOSXDescriptor* prev = NULL;
found = descriptorList;
while(found != NULL){
if(found->fd == fd){
if(descriptorList == found){
descriptorList = found->next;
}else{
prev->next = found->next;
}
free(found);
return;
}
prev = found;
found = found->next;
}
}