]>
git.saurik.com Git - redis.git/blob - src/ae_kqueue.c
1 /* Kqueue(2)-based ae.c module
2 * Copyright (C) 2009 Harish Mallipeddi - harish.mallipeddi@gmail.com
3 * Released under the BSD license. See the COPYING file for more info. */
9 typedef struct aeApiState
{
11 struct kevent
*events
;
14 static int aeApiCreate(aeEventLoop
*eventLoop
) {
15 aeApiState
*state
= zmalloc(sizeof(aeApiState
));
17 if (!state
) return -1;
18 state
->events
= zmalloc(sizeof(struct kevent
)*eventLoop
->setsize
);
23 state
->kqfd
= kqueue();
24 if (state
->kqfd
== -1) {
29 eventLoop
->apidata
= state
;
34 static void aeApiFree(aeEventLoop
*eventLoop
) {
35 aeApiState
*state
= eventLoop
->apidata
;
42 static int aeApiAddEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
43 aeApiState
*state
= eventLoop
->apidata
;
46 if (mask
& AE_READABLE
) {
47 EV_SET(&ke
, fd
, EVFILT_READ
, EV_ADD
, 0, 0, NULL
);
48 if (kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
) == -1) return -1;
50 if (mask
& AE_WRITABLE
) {
51 EV_SET(&ke
, fd
, EVFILT_WRITE
, EV_ADD
, 0, 0, NULL
);
52 if (kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
) == -1) return -1;
57 static void aeApiDelEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
58 aeApiState
*state
= eventLoop
->apidata
;
61 if (mask
& AE_READABLE
) {
62 EV_SET(&ke
, fd
, EVFILT_READ
, EV_DELETE
, 0, 0, NULL
);
63 kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
);
65 if (mask
& AE_WRITABLE
) {
66 EV_SET(&ke
, fd
, EVFILT_WRITE
, EV_DELETE
, 0, 0, NULL
);
67 kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
);
71 static int aeApiPoll(aeEventLoop
*eventLoop
, struct timeval
*tvp
) {
72 aeApiState
*state
= eventLoop
->apidata
;
73 int retval
, numevents
= 0;
76 struct timespec timeout
;
77 timeout
.tv_sec
= tvp
->tv_sec
;
78 timeout
.tv_nsec
= tvp
->tv_usec
* 1000;
79 retval
= kevent(state
->kqfd
, NULL
, 0, state
->events
, eventLoop
->setsize
,
82 retval
= kevent(state
->kqfd
, NULL
, 0, state
->events
, eventLoop
->setsize
,
90 for(j
= 0; j
< numevents
; j
++) {
92 struct kevent
*e
= state
->events
+j
;
94 if (e
->filter
== EVFILT_READ
) mask
|= AE_READABLE
;
95 if (e
->filter
== EVFILT_WRITE
) mask
|= AE_WRITABLE
;
96 eventLoop
->fired
[j
].fd
= e
->ident
;
97 eventLoop
->fired
[j
].mask
= mask
;
103 static char *aeApiName(void) {