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 58 return 0; 59 } 60 61 static void aeApiFree(aeEventLoop *eventLoop) { 62 aeApiState *state = eventLoop->apidata; 63 64 close(state->kqfd); 65 zfree(state->events); 66 zfree(state); 67 } 68 69 static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) { 70 aeApiState *state = eventLoop->apidata; 71 struct kevent ke; 72 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; 76 } 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; 80 } 81 return 0; 82 } 83 84 static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) { 85 aeApiState *state = eventLoop->apidata; 86 struct kevent ke; 87 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); 91 } 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); 95 } 96 } 97 98 static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) { 99 aeApiState *state = eventLoop->apidata; 100 int retval, numevents = 0; 101 102 if (tvp != NULL) { 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, 107 &timeout); 108 } else { 109 retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize, 110 NULL); 111 } 112 113 if (retval > 0) { 114 int j; 115 116 numevents = retval; 117 for(j = 0; j < numevents; j++) { 118 int mask = 0; 119 struct kevent *e = state->events+j; 120 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; 125 } 126 } 127 return numevents; 128 } 129 130 static char *aeApiName(void) { 131 return "kqueue"; 132 } 133