xref: /f-stack/app/redis-5.0.5/src/ae_kqueue.c (revision 572c4311)
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/event.h>
34*572c4311Sfengbojiang #include <sys/time.h>
35*572c4311Sfengbojiang 
36*572c4311Sfengbojiang typedef struct aeApiState {
37*572c4311Sfengbojiang     int kqfd;
38*572c4311Sfengbojiang     struct kevent *events;
39*572c4311Sfengbojiang } aeApiState;
40*572c4311Sfengbojiang 
aeApiCreate(aeEventLoop * eventLoop)41*572c4311Sfengbojiang static int aeApiCreate(aeEventLoop *eventLoop) {
42*572c4311Sfengbojiang     aeApiState *state = zmalloc(sizeof(aeApiState));
43*572c4311Sfengbojiang 
44*572c4311Sfengbojiang     if (!state) return -1;
45*572c4311Sfengbojiang     state->events = zmalloc(sizeof(struct kevent)*eventLoop->setsize);
46*572c4311Sfengbojiang     if (!state->events) {
47*572c4311Sfengbojiang         zfree(state);
48*572c4311Sfengbojiang         return -1;
49*572c4311Sfengbojiang     }
50*572c4311Sfengbojiang     state->kqfd = kqueue();
51*572c4311Sfengbojiang     if (state->kqfd == -1) {
52*572c4311Sfengbojiang         zfree(state->events);
53*572c4311Sfengbojiang         zfree(state);
54*572c4311Sfengbojiang         return -1;
55*572c4311Sfengbojiang     }
56*572c4311Sfengbojiang     eventLoop->apidata = state;
57*572c4311Sfengbojiang     return 0;
58*572c4311Sfengbojiang }
59*572c4311Sfengbojiang 
aeApiResize(aeEventLoop * eventLoop,int setsize)60*572c4311Sfengbojiang static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
61*572c4311Sfengbojiang     aeApiState *state = eventLoop->apidata;
62*572c4311Sfengbojiang 
63*572c4311Sfengbojiang     state->events = zrealloc(state->events, sizeof(struct kevent)*setsize);
64*572c4311Sfengbojiang     return 0;
65*572c4311Sfengbojiang }
66*572c4311Sfengbojiang 
aeApiFree(aeEventLoop * eventLoop)67*572c4311Sfengbojiang static void aeApiFree(aeEventLoop *eventLoop) {
68*572c4311Sfengbojiang     aeApiState *state = eventLoop->apidata;
69*572c4311Sfengbojiang 
70*572c4311Sfengbojiang     close(state->kqfd);
71*572c4311Sfengbojiang     zfree(state->events);
72*572c4311Sfengbojiang     zfree(state);
73*572c4311Sfengbojiang }
74*572c4311Sfengbojiang 
aeApiAddEvent(aeEventLoop * eventLoop,int fd,int mask)75*572c4311Sfengbojiang static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
76*572c4311Sfengbojiang     aeApiState *state = eventLoop->apidata;
77*572c4311Sfengbojiang     struct kevent ke;
78*572c4311Sfengbojiang 
79*572c4311Sfengbojiang     if (mask & AE_READABLE) {
80*572c4311Sfengbojiang         EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
81*572c4311Sfengbojiang         if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;
82*572c4311Sfengbojiang     }
83*572c4311Sfengbojiang     if (mask & AE_WRITABLE) {
84*572c4311Sfengbojiang         EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
85*572c4311Sfengbojiang         if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;
86*572c4311Sfengbojiang     }
87*572c4311Sfengbojiang     return 0;
88*572c4311Sfengbojiang }
89*572c4311Sfengbojiang 
aeApiDelEvent(aeEventLoop * eventLoop,int fd,int mask)90*572c4311Sfengbojiang static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
91*572c4311Sfengbojiang     aeApiState *state = eventLoop->apidata;
92*572c4311Sfengbojiang     struct kevent ke;
93*572c4311Sfengbojiang 
94*572c4311Sfengbojiang     if (mask & AE_READABLE) {
95*572c4311Sfengbojiang         EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
96*572c4311Sfengbojiang         kevent(state->kqfd, &ke, 1, NULL, 0, NULL);
97*572c4311Sfengbojiang     }
98*572c4311Sfengbojiang     if (mask & AE_WRITABLE) {
99*572c4311Sfengbojiang         EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
100*572c4311Sfengbojiang         kevent(state->kqfd, &ke, 1, NULL, 0, NULL);
101*572c4311Sfengbojiang     }
102*572c4311Sfengbojiang }
103*572c4311Sfengbojiang 
aeApiPoll(aeEventLoop * eventLoop,struct timeval * tvp)104*572c4311Sfengbojiang static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
105*572c4311Sfengbojiang     aeApiState *state = eventLoop->apidata;
106*572c4311Sfengbojiang     int retval, numevents = 0;
107*572c4311Sfengbojiang 
108*572c4311Sfengbojiang     if (tvp != NULL) {
109*572c4311Sfengbojiang         struct timespec timeout;
110*572c4311Sfengbojiang         timeout.tv_sec = tvp->tv_sec;
111*572c4311Sfengbojiang         timeout.tv_nsec = tvp->tv_usec * 1000;
112*572c4311Sfengbojiang         retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
113*572c4311Sfengbojiang                         &timeout);
114*572c4311Sfengbojiang     } else {
115*572c4311Sfengbojiang         retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
116*572c4311Sfengbojiang                         NULL);
117*572c4311Sfengbojiang     }
118*572c4311Sfengbojiang 
119*572c4311Sfengbojiang     if (retval > 0) {
120*572c4311Sfengbojiang         int j;
121*572c4311Sfengbojiang 
122*572c4311Sfengbojiang         numevents = retval;
123*572c4311Sfengbojiang         for(j = 0; j < numevents; j++) {
124*572c4311Sfengbojiang             int mask = 0;
125*572c4311Sfengbojiang             struct kevent *e = state->events+j;
126*572c4311Sfengbojiang 
127*572c4311Sfengbojiang             if (e->filter == EVFILT_READ) mask |= AE_READABLE;
128*572c4311Sfengbojiang             if (e->filter == EVFILT_WRITE) mask |= AE_WRITABLE;
129*572c4311Sfengbojiang             eventLoop->fired[j].fd = e->ident;
130*572c4311Sfengbojiang             eventLoop->fired[j].mask = mask;
131*572c4311Sfengbojiang         }
132*572c4311Sfengbojiang     }
133*572c4311Sfengbojiang     return numevents;
134*572c4311Sfengbojiang }
135*572c4311Sfengbojiang 
aeApiName(void)136*572c4311Sfengbojiang static char *aeApiName(void) {
137*572c4311Sfengbojiang     return "kqueue";
138*572c4311Sfengbojiang }
139