1*572c4311Sfengbojiang /* Kqueue(2)-based ae.c module
2*572c4311Sfengbojiang *
3*572c4311Sfengbojiang * Copyright (C) 2009 Harish Mallipeddi - [email protected]
4*572c4311Sfengbojiang * All rights reserved.
5*572c4311Sfengbojiang *
6*572c4311Sfengbojiang * Redistribution and use in source and binary forms, with or without
7*572c4311Sfengbojiang * modification, are permitted provided that the following conditions are met:
8*572c4311Sfengbojiang *
9*572c4311Sfengbojiang * * Redistributions of source code must retain the above copyright notice,
10*572c4311Sfengbojiang * this list of conditions and the following disclaimer.
11*572c4311Sfengbojiang * * Redistributions in binary form must reproduce the above copyright
12*572c4311Sfengbojiang * notice, this list of conditions and the following disclaimer in the
13*572c4311Sfengbojiang * documentation and/or other materials provided with the distribution.
14*572c4311Sfengbojiang * * Neither the name of Redis nor the names of its contributors may be used
15*572c4311Sfengbojiang * to endorse or promote products derived from this software without
16*572c4311Sfengbojiang * specific prior written permission.
17*572c4311Sfengbojiang *
18*572c4311Sfengbojiang * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*572c4311Sfengbojiang * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*572c4311Sfengbojiang * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*572c4311Sfengbojiang * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22*572c4311Sfengbojiang * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*572c4311Sfengbojiang * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*572c4311Sfengbojiang * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*572c4311Sfengbojiang * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*572c4311Sfengbojiang * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*572c4311Sfengbojiang * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*572c4311Sfengbojiang * POSSIBILITY OF SUCH DAMAGE.
29*572c4311Sfengbojiang */
30*572c4311Sfengbojiang
31*572c4311Sfengbojiang
32*572c4311Sfengbojiang #include <sys/types.h>
33*572c4311Sfengbojiang #include <sys/time.h>
34*572c4311Sfengbojiang typedef unsigned short u_short;
35*572c4311Sfengbojiang typedef unsigned int u_int;
36*572c4311Sfengbojiang #include "ff_api.h"
37*572c4311Sfengbojiang
38*572c4311Sfengbojiang typedef struct aeApiState {
39*572c4311Sfengbojiang int kqfd;
40*572c4311Sfengbojiang struct kevent *events;
41*572c4311Sfengbojiang } aeApiState;
42*572c4311Sfengbojiang
aeApiCreate(aeEventLoop * eventLoop)43*572c4311Sfengbojiang static int aeApiCreate(aeEventLoop *eventLoop) {
44*572c4311Sfengbojiang aeApiState *state = zmalloc(sizeof(aeApiState));
45*572c4311Sfengbojiang
46*572c4311Sfengbojiang if (!state) return -1;
47*572c4311Sfengbojiang state->events = zmalloc(sizeof(struct kevent)*eventLoop->setsize);
48*572c4311Sfengbojiang if (!state->events) {
49*572c4311Sfengbojiang zfree(state);
50*572c4311Sfengbojiang return -1;
51*572c4311Sfengbojiang }
52*572c4311Sfengbojiang state->kqfd = ff_kqueue();
53*572c4311Sfengbojiang if (state->kqfd == -1) {
54*572c4311Sfengbojiang zfree(state->events);
55*572c4311Sfengbojiang zfree(state);
56*572c4311Sfengbojiang return -1;
57*572c4311Sfengbojiang }
58*572c4311Sfengbojiang eventLoop->apidata = state;
59*572c4311Sfengbojiang return 0;
60*572c4311Sfengbojiang }
61*572c4311Sfengbojiang
aeApiResize(aeEventLoop * eventLoop,int setsize)62*572c4311Sfengbojiang static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
63*572c4311Sfengbojiang aeApiState *state = eventLoop->apidata;
64*572c4311Sfengbojiang
65*572c4311Sfengbojiang state->events = zrealloc(state->events, sizeof(struct kevent)*setsize);
66*572c4311Sfengbojiang return 0;
67*572c4311Sfengbojiang }
68*572c4311Sfengbojiang
aeApiFree(aeEventLoop * eventLoop)69*572c4311Sfengbojiang static void aeApiFree(aeEventLoop *eventLoop) {
70*572c4311Sfengbojiang aeApiState *state = eventLoop->apidata;
71*572c4311Sfengbojiang
72*572c4311Sfengbojiang close(state->kqfd);
73*572c4311Sfengbojiang zfree(state->events);
74*572c4311Sfengbojiang zfree(state);
75*572c4311Sfengbojiang }
76*572c4311Sfengbojiang
aeApiAddEvent(aeEventLoop * eventLoop,int fd,int mask)77*572c4311Sfengbojiang static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
78*572c4311Sfengbojiang aeApiState *state = eventLoop->apidata;
79*572c4311Sfengbojiang struct kevent ke;
80*572c4311Sfengbojiang
81*572c4311Sfengbojiang if (mask & AE_READABLE) {
82*572c4311Sfengbojiang EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
83*572c4311Sfengbojiang if (ff_kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;
84*572c4311Sfengbojiang }
85*572c4311Sfengbojiang if (mask & AE_WRITABLE) {
86*572c4311Sfengbojiang EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
87*572c4311Sfengbojiang if (ff_kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;
88*572c4311Sfengbojiang }
89*572c4311Sfengbojiang return 0;
90*572c4311Sfengbojiang }
91*572c4311Sfengbojiang
aeApiDelEvent(aeEventLoop * eventLoop,int fd,int mask)92*572c4311Sfengbojiang static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
93*572c4311Sfengbojiang aeApiState *state = eventLoop->apidata;
94*572c4311Sfengbojiang struct kevent ke;
95*572c4311Sfengbojiang
96*572c4311Sfengbojiang if (mask & AE_READABLE) {
97*572c4311Sfengbojiang EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
98*572c4311Sfengbojiang ff_kevent(state->kqfd, &ke, 1, NULL, 0, NULL);
99*572c4311Sfengbojiang }
100*572c4311Sfengbojiang if (mask & AE_WRITABLE) {
101*572c4311Sfengbojiang EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
102*572c4311Sfengbojiang ff_kevent(state->kqfd, &ke, 1, NULL, 0, NULL);
103*572c4311Sfengbojiang }
104*572c4311Sfengbojiang }
105*572c4311Sfengbojiang
aeApiPoll(aeEventLoop * eventLoop,struct timeval * tvp)106*572c4311Sfengbojiang static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
107*572c4311Sfengbojiang aeApiState *state = eventLoop->apidata;
108*572c4311Sfengbojiang int retval, numevents = 0;
109*572c4311Sfengbojiang
110*572c4311Sfengbojiang if (tvp != NULL) {
111*572c4311Sfengbojiang struct timespec timeout;
112*572c4311Sfengbojiang timeout.tv_sec = tvp->tv_sec;
113*572c4311Sfengbojiang timeout.tv_nsec = tvp->tv_usec * 1000;
114*572c4311Sfengbojiang retval = ff_kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
115*572c4311Sfengbojiang &timeout);
116*572c4311Sfengbojiang } else {
117*572c4311Sfengbojiang retval = ff_kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
118*572c4311Sfengbojiang NULL);
119*572c4311Sfengbojiang }
120*572c4311Sfengbojiang
121*572c4311Sfengbojiang if (retval > 0) {
122*572c4311Sfengbojiang int j;
123*572c4311Sfengbojiang
124*572c4311Sfengbojiang numevents = retval;
125*572c4311Sfengbojiang for(j = 0; j < numevents; j++) {
126*572c4311Sfengbojiang int mask = 0;
127*572c4311Sfengbojiang struct kevent *e = state->events+j;
128*572c4311Sfengbojiang
129*572c4311Sfengbojiang if (e->filter == EVFILT_READ) mask |= AE_READABLE;
130*572c4311Sfengbojiang if (e->filter == EVFILT_WRITE) mask |= AE_WRITABLE;
131*572c4311Sfengbojiang eventLoop->fired[j].fd = e->ident;
132*572c4311Sfengbojiang eventLoop->fired[j].mask = mask;
133*572c4311Sfengbojiang }
134*572c4311Sfengbojiang }
135*572c4311Sfengbojiang return numevents;
136*572c4311Sfengbojiang }
137*572c4311Sfengbojiang
aeApiName(void)138*572c4311Sfengbojiang static char *aeApiName(void) {
139*572c4311Sfengbojiang return "ff_kqueue";
140*572c4311Sfengbojiang }
141