]>
git.saurik.com Git - redis.git/blob - src/ae_kqueue.c
1 /* Kqueue(2)-based ae.c module
3 * Copyright (C) 2009 Harish Mallipeddi - harish.mallipeddi@gmail.com
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of Redis nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/types.h>
33 #include <sys/event.h>
36 typedef struct aeApiState
{
38 struct kevent
*events
;
41 static int aeApiCreate(aeEventLoop
*eventLoop
) {
42 aeApiState
*state
= zmalloc(sizeof(aeApiState
));
44 if (!state
) return -1;
45 state
->events
= zmalloc(sizeof(struct kevent
)*eventLoop
->setsize
);
50 state
->kqfd
= kqueue();
51 if (state
->kqfd
== -1) {
56 eventLoop
->apidata
= state
;
61 static void aeApiFree(aeEventLoop
*eventLoop
) {
62 aeApiState
*state
= eventLoop
->apidata
;
69 static int aeApiAddEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
70 aeApiState
*state
= eventLoop
->apidata
;
73 if (mask
& AE_READABLE
) {
74 EV_SET(&ke
, fd
, EVFILT_READ
, EV_ADD
, 0, 0, NULL
);
75 if (kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
) == -1) return -1;
77 if (mask
& AE_WRITABLE
) {
78 EV_SET(&ke
, fd
, EVFILT_WRITE
, EV_ADD
, 0, 0, NULL
);
79 if (kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
) == -1) return -1;
84 static void aeApiDelEvent(aeEventLoop
*eventLoop
, int fd
, int mask
) {
85 aeApiState
*state
= eventLoop
->apidata
;
88 if (mask
& AE_READABLE
) {
89 EV_SET(&ke
, fd
, EVFILT_READ
, EV_DELETE
, 0, 0, NULL
);
90 kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
);
92 if (mask
& AE_WRITABLE
) {
93 EV_SET(&ke
, fd
, EVFILT_WRITE
, EV_DELETE
, 0, 0, NULL
);
94 kevent(state
->kqfd
, &ke
, 1, NULL
, 0, NULL
);
98 static int aeApiPoll(aeEventLoop
*eventLoop
, struct timeval
*tvp
) {
99 aeApiState
*state
= eventLoop
->apidata
;
100 int retval
, numevents
= 0;
103 struct timespec timeout
;
104 timeout
.tv_sec
= tvp
->tv_sec
;
105 timeout
.tv_nsec
= tvp
->tv_usec
* 1000;
106 retval
= kevent(state
->kqfd
, NULL
, 0, state
->events
, eventLoop
->setsize
,
109 retval
= kevent(state
->kqfd
, NULL
, 0, state
->events
, eventLoop
->setsize
,
117 for(j
= 0; j
< numevents
; j
++) {
119 struct kevent
*e
= state
->events
+j
;
121 if (e
->filter
== EVFILT_READ
) mask
|= AE_READABLE
;
122 if (e
->filter
== EVFILT_WRITE
) mask
|= AE_WRITABLE
;
123 eventLoop
->fired
[j
].fd
= e
->ident
;
124 eventLoop
->fired
[j
].mask
= mask
;
130 static char *aeApiName(void) {