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