1 /* Kqueue(2)-based ae.c module 2 * 3 * Copyright (C) 2009 Harish Mallipeddi - [email protected] 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 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. 17 * 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. 29 */ 30 31 32 #include <sys/types.h> 33 #include <sys/event.h> 34 #include <sys/time.h> 35 36 typedef struct aeApiState { 37 int kqfd; 38 struct kevent *events; 39 } aeApiState; 40 41 static int aeApiCreate(aeEventLoop *eventLoop) { 42 aeApiState *state = zmalloc(sizeof(aeApiState)); 43 44 if (!state) return -1; 45 state->events = zmalloc(sizeof(struct kevent)*eventLoop->setsize); 46 if (!state->events) { 47 zfree(state); 48 return -1; 49 } 50 state->kqfd = kqueue(); 51 if (state->kqfd == -1) { 52 zfree(state->events); 53 zfree(state); 54 return -1; 55 } 56 eventLoop->apidata = state; 57 return 0; 58 } 59 60 static int aeApiResize(aeEventLoop *eventLoop, int setsize) { 61 aeApiState *state = eventLoop->apidata; 62 63 state->events = zrealloc(state->events, sizeof(struct kevent)*setsize); 64 return 0; 65 } 66 67 static void aeApiFree(aeEventLoop *eventLoop) { 68 aeApiState *state = eventLoop->apidata; 69 70 close(state->kqfd); 71 zfree(state->events); 72 zfree(state); 73 } 74 75 static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) { 76 aeApiState *state = eventLoop->apidata; 77 struct kevent ke; 78 79 if (mask & AE_READABLE) { 80 EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL); 81 if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1; 82 } 83 if (mask & AE_WRITABLE) { 84 EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL); 85 if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1; 86 } 87 return 0; 88 } 89 90 static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) { 91 aeApiState *state = eventLoop->apidata; 92 struct kevent ke; 93 94 if (mask & AE_READABLE) { 95 EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); 96 kevent(state->kqfd, &ke, 1, NULL, 0, NULL); 97 } 98 if (mask & AE_WRITABLE) { 99 EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL); 100 kevent(state->kqfd, &ke, 1, NULL, 0, NULL); 101 } 102 } 103 104 static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) { 105 aeApiState *state = eventLoop->apidata; 106 int retval, numevents = 0; 107 108 if (tvp != NULL) { 109 struct timespec timeout; 110 timeout.tv_sec = tvp->tv_sec; 111 timeout.tv_nsec = tvp->tv_usec * 1000; 112 retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize, 113 &timeout); 114 } else { 115 retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize, 116 NULL); 117 } 118 119 if (retval > 0) { 120 int j; 121 122 numevents = retval; 123 for(j = 0; j < numevents; j++) { 124 int mask = 0; 125 struct kevent *e = state->events+j; 126 127 if (e->filter == EVFILT_READ) mask |= AE_READABLE; 128 if (e->filter == EVFILT_WRITE) mask |= AE_WRITABLE; 129 eventLoop->fired[j].fd = e->ident; 130 eventLoop->fired[j].mask = mask; 131 } 132 } 133 return numevents; 134 } 135 136 static char *aeApiName(void) { 137 return "kqueue"; 138 } 139