| 1 | /* |
| 2 | * Copyright (c) 2009, Cybozu Labs, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * * Neither the name of the <ORGANIZATION> nor the names of its contributors |
| 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | * POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/event.h> |
| 33 | #include <sys/time.h> |
| 34 | #include <unistd.h> |
| 35 | #include "picoev.h" |
| 36 | |
| 37 | #define EV_QUEUE_SZ 128 |
| 38 | |
| 39 | #define BACKEND_BUILD(next_fd, events) \ |
| 40 | ((unsigned)((next_fd << 8) | (events & 0xff))) |
| 41 | #define BACKEND_GET_NEXT_FD(backend) ((int)(backend) >> 8) |
| 42 | #define BACKEND_GET_OLD_EVENTS(backend) ((int)(backend) & 0xff) |
| 43 | |
| 44 | typedef struct picoev_loop_kqueue_st { |
| 45 | picoev_loop loop; |
| 46 | int kq; |
| 47 | int changed_fds; /* link list using picoev_fd::_backend, -1 if not changed */ |
| 48 | struct kevent events[1024]; |
| 49 | struct kevent changelist[256]; |
| 50 | } picoev_loop_kqueue; |
| 51 | |
| 52 | picoev_globals picoev; |
| 53 | |
| 54 | static int apply_pending_changes(picoev_loop_kqueue* loop, int apply_all) |
| 55 | { |
| 56 | #define SET(op, events) \ |
| 57 | EV_SET(loop->changelist + cl_off++, loop->changed_fds, \ |
| 58 | (((events) & PICOEV_READ) != 0 ? EVFILT_READ : 0) \ |
| 59 | | (((events) & PICOEV_WRITE) != 0 ? EVFILT_WRITE : 0), \ |
| 60 | (op), 0, 0, NULL) |
| 61 | |
| 62 | int cl_off = 0, nevents; |
| 63 | |
| 64 | while (loop->changed_fds != -1) { |
| 65 | picoev_fd* changed = picoev.fds + loop->changed_fds; |
| 66 | int old_events = BACKEND_GET_OLD_EVENTS(changed->_backend); |
| 67 | if (changed->events != old_events) { |
| 68 | if (old_events != 0) { |
| 69 | SET(EV_DISABLE, old_events); |
| 70 | } |
| 71 | if (changed->events != 0) { |
| 72 | SET(EV_ADD | EV_ENABLE, changed->events); |
| 73 | } |
| 74 | if ((size_t)cl_off + 1 |
| 75 | >= sizeof(loop->changelist) / sizeof(loop->changelist[0])) { |
| 76 | nevents = kevent(loop->kq, loop->changelist, cl_off, NULL, 0, NULL); |
| 77 | assert(nevents == 0); |
| 78 | cl_off = 0; |
| 79 | } |
| 80 | } |
| 81 | loop->changed_fds = BACKEND_GET_NEXT_FD(changed->_backend); |
| 82 | changed->_backend = -1; |
| 83 | } |
| 84 | |
| 85 | if (apply_all && cl_off != 0) { |
| 86 | nevents = kevent(loop->kq, loop->changelist, cl_off, NULL, 0, NULL); |
| 87 | assert(nevents == 0); |
| 88 | cl_off = 0; |
| 89 | } |
| 90 | |
| 91 | return cl_off; |
| 92 | |
| 93 | #undef SET |
| 94 | } |
| 95 | |
| 96 | picoev_loop* picoev_create_loop(int max_timeout) |
| 97 | { |
| 98 | picoev_loop_kqueue* loop; |
| 99 | |
| 100 | /* init parent */ |
| 101 | assert(PICOEV_IS_INITED); |
| 102 | if ((loop = (picoev_loop_kqueue*)malloc(sizeof(picoev_loop_kqueue))) |
| 103 | == NULL) { |
| 104 | return NULL; |
| 105 | } |
| 106 | if (picoev_init_loop_internal(&loop->loop, max_timeout) != 0) { |
| 107 | free(loop); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | /* init kqueue */ |
| 112 | if ((loop->kq = kqueue()) == -1) { |
| 113 | picoev_deinit_loop_internal(&loop->loop); |
| 114 | free(loop); |
| 115 | return NULL; |
| 116 | } |
| 117 | loop->changed_fds = -1; |
| 118 | |
| 119 | loop->loop.now = time(NULL); |
| 120 | return &loop->loop; |
| 121 | } |
| 122 | |
| 123 | int picoev_destroy_loop(picoev_loop* _loop) |
| 124 | { |
| 125 | picoev_loop_kqueue* loop = (picoev_loop_kqueue*)_loop; |
| 126 | |
| 127 | if (close(loop->kq) != 0) { |
| 128 | return -1; |
| 129 | } |
| 130 | picoev_deinit_loop_internal(&loop->loop); |
| 131 | free(loop); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | int picoev_update_events_internal(picoev_loop* _loop, int fd, int events) |
| 136 | { |
| 137 | picoev_loop_kqueue* loop = (picoev_loop_kqueue*)_loop; |
| 138 | picoev_fd* target = picoev.fds + fd; |
| 139 | |
| 140 | assert(PICOEV_FD_BELONGS_TO_LOOP(&loop->loop, fd)); |
| 141 | |
| 142 | /* initialize if adding the fd */ |
| 143 | if ((events & PICOEV_ADD) != 0) { |
| 144 | target->_backend = -1; |
| 145 | } |
| 146 | /* return if nothing to do */ |
| 147 | if (events == PICOEV_DEL |
| 148 | ? target->_backend == -1 |
| 149 | : (events & PICOEV_READWRITE) == target->events) { |
| 150 | return 0; |
| 151 | } |
| 152 | /* add to changed list if not yet being done */ |
| 153 | if (target->_backend == -1) { |
| 154 | target->_backend = BACKEND_BUILD(loop->changed_fds, target->events); |
| 155 | loop->changed_fds = fd; |
| 156 | } |
| 157 | /* update events */ |
| 158 | target->events = events & PICOEV_READWRITE; |
| 159 | /* apply immediately if is a DELETE */ |
| 160 | if ((events & PICOEV_DEL) != 0) { |
| 161 | apply_pending_changes(loop, 1); |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | int picoev_poll_once_internal(picoev_loop* _loop, int max_wait) |
| 168 | { |
| 169 | picoev_loop_kqueue* loop = (picoev_loop_kqueue*)_loop; |
| 170 | struct timespec ts; |
| 171 | int cl_off = 0, nevents, i; |
| 172 | |
| 173 | /* apply pending changes, with last changes stored to loop->changelist */ |
| 174 | cl_off = apply_pending_changes(loop, 0); |
| 175 | |
| 176 | ts.tv_sec = max_wait; |
| 177 | ts.tv_nsec = 0; |
| 178 | nevents = kevent(loop->kq, loop->changelist, cl_off, loop->events, |
| 179 | sizeof(loop->events) / sizeof(loop->events[0]), &ts); |
| 180 | if (nevents == -1) { |
| 181 | /* the errors we can only rescue */ |
| 182 | assert(errno == EACCES || errno == EFAULT || errno == EINTR); |
| 183 | return -1; |
| 184 | } |
| 185 | for (i = 0; i < nevents; ++i) { |
| 186 | struct kevent* event = loop->events + i; |
| 187 | picoev_fd* target = picoev.fds + event->ident; |
| 188 | assert((event->flags & EV_ERROR) == 0); /* changelist errors are fatal */ |
| 189 | if (loop->loop.loop_id == target->loop_id |
| 190 | && (event->filter & (EVFILT_READ | EVFILT_WRITE)) != 0) { |
| 191 | int revents; |
| 192 | switch (event->filter) { |
| 193 | case EVFILT_READ: |
| 194 | revents = PICOEV_READ; |
| 195 | break; |
| 196 | case EVFILT_WRITE: |
| 197 | revents = PICOEV_WRITE; |
| 198 | break; |
| 199 | default: |
| 200 | assert(0); |
| 201 | revents = 0; // suppress compiler warning |
| 202 | break; |
| 203 | } |
| 204 | (*target->callback)(&loop->loop, event->ident, revents, target->cb_arg); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |