| 6 |
| * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 |
| 28 | #include "config.h" |
| 29 |
| 30 |
| 31 | #ifdef WIN32 |
| 32 | #define WIN32_LEAN_AND_MEAN |
| 33 | #include <windows.h> |
| 34 | #undef WIN32_LEAN_AND_MEAN |
| 35 | #endif |
| 36 | #include <sys/types.h> |
| 37 | #ifdef HAVE_SYS_TIME_H |
| 38 | #include <sys/time.h> |
| 39 | #else |
| 40 | #include <sys/_libevent_time.h> |
| 41 | #endif |
| 42 | #include <sys/queue.h> |
| 43 | #include <stdio.h> |
| 44 | #include <stdlib.h> |
| 45 | #ifndef WIN32 |
| 46 | #include <unistd.h> |
| 47 | #endif |
| 48 | #include <errno.h> |
| 49 | #include <signal.h> |
| 50 | #include <string.h> |
| 51 | #include <assert.h> |
| 52 | #include <time.h> |
| 53 |
| 54 | #include "event.h" |
| 55 | #include "event-internal.h" |
| 56 | #include "evutil.h" |
| 57 | #include "log.h" |
| 58 |
| 59 | #ifdef HAVE_EVENT_PORTS |
| 60 | extern const struct eventop evportops; |
| 61 | #endif |
| 62 | #ifdef HAVE_SELECT |
| 63 | extern const struct eventop selectops; |
| 64 | #endif |
| 65 | #ifdef HAVE_POLL |
| 66 | extern const struct eventop pollops; |
| 67 | #endif |
| 68 | #ifdef HAVE_EPOLL |
| 69 | extern const struct eventop epollops; |
| 70 | #endif |
| 71 | #ifdef HAVE_WORKING_KQUEUE |
| 72 | extern const struct eventop kqops; |
| 73 | #endif |
| 74 | #ifdef HAVE_DEVPOLL |
| 75 | extern const struct eventop devpollops; |
| 76 | #endif |
| 77 | #ifdef WIN32 |
| 78 | extern const struct eventop win32ops; |
| 79 | #endif |
| 80 |
| 81 | /* In order of preference */ |
| 82 | static const struct eventop *eventops[] = { |
| 83 | #ifdef HAVE_EVENT_PORTS |
| 84 | &evportops, |
| 85 | #endif |
| 86 | #ifdef HAVE_WORKING_KQUEUE |
| 87 | &kqops, |
| 88 | #endif |
| 89 | #ifdef HAVE_EPOLL |
| 90 | &epollops, |
| 91 | #endif |
| 92 | #ifdef HAVE_DEVPOLL |
| 93 | &devpollops, |
| 94 | #endif |
| 95 | #ifdef HAVE_POLL |
| 96 | &pollops, |
| 97 | #endif |
| 98 | #ifdef HAVE_SELECT |
| 99 | &selectops, |
| 100 | #endif |
| 101 | #ifdef WIN32 |
| 102 | &win32ops, |
| 103 | #endif |
| 104 | NULL |
| 105 | }; |
| 106 |
| 107 | /* Global state */ |
| 108 | struct event_base *current_base = NULL; |
| 109 | extern struct event_base *evsignal_base; |
| 110 | static int use_monotonic = 1; |
| 111 |
| 112 | /* Prototypes */ |
| 113 | static void event_queue_insert(struct event_base *, struct event *, int); |
| 114 | static void event_queue_remove(struct event_base *, struct event *, int); |
| 115 | static int event_haveevents(struct event_base *); |
| 116 |
| 117 | static void event_process_active(struct event_base *); |
| 118 |
| 119 | static int timeout_next(struct event_base *, struct timeval **); |
| 120 | static void timeout_process(struct event_base *); |
| 121 | static void timeout_correct(struct event_base *, struct timeval *); |
| 122 |
| 123 | static int |
| 124 | gettime(struct event_base *base, struct timeval *tp) |
| 125 | { |
| 126 | if (base->tv_cache.tv_sec) { |
| 127 | *tp = base->tv_cache; |
| 128 | return (0); |
| 129 | } |
| 130 |
| 131 | #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) |
| 132 | struct timespec ts; |
| 133 |
| 134 | if (use_monotonic && |
| 135 | clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
| 136 | tp->tv_sec = ts.tv_sec; |
| 137 | tp->tv_usec = ts.tv_nsec / 1000; |
| 138 | return (0); |
| 139 | } |
| 140 | #endif |
| 141 |
| 142 | use_monotonic = 0; |
| 143 |
| 144 | return (evutil_gettimeofday(tp, NULL)); |
| 145 | } |
| 146 |
| 147 | struct event_base * |
| 148 | event_init(void) |
| 149 | { |
| 150 | struct event_base *base = event_base_new(); |
| 151 |
| 152 | if (base != NULL) |
| 153 | current_base = base; |
| 154 |
| 155 | return (base); |
| 156 | } |
| 157 |
| 158 | struct event_base * |
| 159 | event_base_new(void) |
| 160 | { |
| 161 | int i; |
| 162 | struct event_base *base; |
| 163 |
| 164 | if ((base = calloc(1, sizeof(struct event_base))) == NULL) |
| 165 | event_err(1, "%s: calloc", __func__); |
| 166 |
| 167 | gettime(base, &base->event_tv); |
| 168 |
| 169 | min_heap_ctor(&base->timeheap); |
| 170 | TAILQ_INIT(&base->eventqueue); |
| 171 | base->sig.ev_signal_pair[0] = -1; |
| 172 | base->sig.ev_signal_pair[1] = -1; |
| 173 |
| 174 | base->evbase = NULL; |
| 175 | for (i = 0; eventops[i] && !base->evbase; i++) { |
| 176 | base->evsel = eventops[i]; |
| 177 |
| 178 | base->evbase = base->evsel->init(base); |
| 179 | } |
| 180 |
| 181 | if (base->evbase == NULL) |
| 182 | event_errx(1, "%s: no event mechanism available", __func__); |
| 183 |
| 184 | if (evutil_getenv("EVENT_SHOW_METHOD")) |
| 185 | event_msgx("libevent using: %s\n", |
| 186 | base->evsel->name); |
| 187 |
| 188 | /* allocate a single active event queue */ |
| 189 | event_base_priority_init(base, 1); |
| 190 |
| 191 | return (base); |
| 192 | } |
| 193 |
| 194 | void |
| 195 | event_base_free(struct event_base *base) |
| 196 | { |
| 197 | int i, n_deleted=0; |
| 198 | struct event *ev; |
| 199 |
| 200 | if (base == NULL && current_base) |
| 201 | base = current_base; |
| 202 | if (base == current_base) |
| 203 | current_base = NULL; |
| 204 |
| 205 | /* XXX(niels) - check for internal events first */ |
| 206 | assert(base); |
| 207 | /* Delete all non-internal events. */ |
| 208 | for (ev = TAILQ_FIRST(&base->eventqueue); ev; ) { |
| 209 | struct event *next = TAILQ_NEXT(ev, ev_next); |
| 210 | if (!(ev->ev_flags & EVLIST_INTERNAL)) { |
| 211 | event_del(ev); |
| 212 | ++n_deleted; |
| 213 | } |
| 214 | ev = next; |
| 215 | } |
| 216 | while ((ev = min_heap_top(&base->timeheap)) != NULL) { |
| 217 | event_del(ev); |
| 218 | ++n_deleted; |
| 219 | } |
| 220 |
| 221 | for (i = 0; i < base->nactivequeues; ++i) { |
| 222 | for (ev = TAILQ_FIRST(base->activequeues[i]); ev; ) { |
| 223 | struct event *next = TAILQ_NEXT(ev, ev_active_next); |
| 224 | if (!(ev->ev_flags & EVLIST_INTERNAL)) { |
| 225 | event_del(ev); |
| 226 | ++n_deleted; |
| 227 | } |
| 228 | ev = next; |
| 229 | } |
| 230 | } |
| 231 |
| 232 | if (n_deleted) |
| 233 | event_debug(("%s: %d events were still set in base", |
| 234 | __func__, n_deleted)); |
| 235 |
| 236 | if (base->evsel->dealloc != NULL) |
| 237 | base->evsel->dealloc(base, base->evbase); |
| 238 |
| 239 | for (i = 0; i < base->nactivequeues; ++i) |
| 240 | assert(TAILQ_EMPTY(base->activequeues[i])); |
| 241 |
| 242 | assert(min_heap_empty(&base->timeheap)); |
| 243 | min_heap_dtor(&base->timeheap); |
| 244 |
| 245 | for (i = 0; i < base->nactivequeues; ++i) |
| 246 | free(base->activequeues[i]); |
| 247 | free(base->activequeues); |
| 248 |
| 249 | assert(TAILQ_EMPTY(&base->eventqueue)); |
| 250 |
| 251 | free(base); |
| 252 | } |
| 253 |
| 254 | /* reinitialized the event base after a fork */ |
| 255 | int |
| 256 | event_reinit(struct event_base *base) |
| 257 | { |
| 258 | const struct eventop *evsel = base->evsel; |
| 259 | void *evbase = base->evbase; |
| 260 | int res = 0; |
| 261 | struct event *ev; |
| 262 |
| 263 | #if 0 |
| 264 | /* Right now, reinit always takes effect, since even if the |
| 265 | backend doesn't require it, the signal socketpair code does. |
| 266 | */ |
| 267 | /* check if this event mechanism requires reinit */ |
| 268 | if (!evsel->need_reinit) |
| 269 | return (0); |
| 270 | #endif |
| 271 |
| 272 | /* prevent internal delete */ |
| 273 | if (base->sig.ev_signal_added) { |
| 274 | /* we cannot call event_del here because the base has |
| 275 | * not been reinitialized yet. */ |
| 276 | event_queue_remove(base, &base->sig.ev_signal, |
| 277 | EVLIST_INSERTED); |
| 278 | if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE) |
| 279 | event_queue_remove(base, &base->sig.ev_signal, |
| 280 | EVLIST_ACTIVE); |
| 281 | base->sig.ev_signal_added = 0; |
| 282 | } |
| 283 |
| 284 | if (base->evsel->dealloc != NULL) |
| 285 | base->evsel->dealloc(base, base->evbase); |
| 286 | evbase = base->evbase = evsel->init(base); |
| 287 | if (base->evbase == NULL) |
| 288 | event_errx(1, "%s: could not reinitialize event mechanism", |
| 289 | __func__); |
| 290 |
| 291 | TAILQ_FOREACH(ev, &base->eventqueue, ev_next) { |
| 292 | if (evsel->add(evbase, ev) == -1) |
| 293 | res = -1; |
| 294 | } |
| 295 |
| 296 | return (res); |
| 297 | } |
| 298 |
| 299 | int |
| 300 | event_priority_init(int npriorities) |
| 301 | { |
| 302 | return event_base_priority_init(current_base, npriorities); |
| 303 | } |
| 304 |
| 305 | int |
| 306 | event_base_priority_init(struct event_base *base, int npriorities) |
| 307 | { |
| 308 | int i; |
| 309 |
| 310 | if (base->event_count_active) |
| 311 | return (-1); |
| 312 |
| 313 | if (npriorities == base->nactivequeues) |
| 314 | return (0); |
| 315 |
| 316 | if (base->nactivequeues) { |
| 317 | for (i = 0; i < base->nactivequeues; ++i) { |
| 318 | free(base->activequeues[i]); |
| 319 | } |
| 320 | free(base->activequeues); |
| 321 | } |
| 322 |
| 323 | /* Allocate our priority queues */ |
| 324 | base->nactivequeues = npriorities; |
| 325 | base->activequeues = (struct event_list **) |
| 326 | calloc(base->nactivequeues, sizeof(struct event_list *)); |
| 327 | if (base->activequeues == NULL) |
| 328 | event_err(1, "%s: calloc", __func__); |
| 329 |
| 330 | for (i = 0; i < base->nactivequeues; ++i) { |
| 331 | base->activequeues[i] = malloc(sizeof(struct event_list)); |
| 332 | if (base->activequeues[i] == NULL) |
| 333 | event_err(1, "%s: malloc", __func__); |
| 334 | TAILQ_INIT(base->activequeues[i]); |
| 335 | } |
| 336 |
| 337 | return (0); |
| 338 | } |
| 339 |
| 340 | int |
| 341 | event_haveevents(struct event_base *base) |
| 342 | { |
| 343 | return (base->event_count > 0); |
| 344 | } |
| 345 |
| 346 | /* |
| 347 | * Active events are stored in priority queues. Lower priorities are always |
| 348 | * process before higher priorities. Low priority events can starve high |
| 349 | * priority ones. |
| 350 | */ |
| 351 |
| 352 | static void |
| 353 | event_process_active(struct event_base *base) |
| 354 | { |
| 355 | struct event *ev; |
| 356 | struct event_list *activeq = NULL; |
| 357 | int i; |
| 358 | short ncalls; |
| 359 |
| 360 | for (i = 0; i < base->nactivequeues; ++i) { |
| 361 | if (TAILQ_FIRST(base->activequeues[i]) != NULL) { |
| 362 | activeq = base->activequeues[i]; |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 |
| 367 | assert(activeq != NULL); |
| 368 |
| 369 | for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) { |
| 370 | if (ev->ev_events & EV_PERSIST) |
| 371 | event_queue_remove(base, ev, EVLIST_ACTIVE); |
| 372 | else |
| 373 | event_del(ev); |
| 374 |
| 375 | /* Allows deletes to work */ |
| 376 | ncalls = ev->ev_ncalls; |
| 377 | ev->ev_pncalls = &ncalls; |
| 378 | while (ncalls) { |
| 379 | ncalls--; |
| 380 | ev->ev_ncalls = ncalls; |
| 381 | (*ev->ev_callback)((int)ev->ev_fd, ev->ev_res, ev->ev_arg); |
| 382 | if (base->event_break) |
| 383 | return; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 |
| 388 | /* |
| 389 | * Wait continously for events. We exit only if no events are left. |
| 390 | */ |
| 391 |
| 392 | int |
| 393 | event_dispatch(void) |
| 394 | { |
| 395 | return (event_loop(0)); |
| 396 | } |
| 397 |
| 398 | int |
| 399 | event_base_dispatch(struct event_base *event_base) |
| 400 | { |
| 401 | return (event_base_loop(event_base, 0)); |
| 402 | } |
| 403 |
| 404 | const char * |
| 405 | event_base_get_method(struct event_base *base) |
| 406 | { |
| 407 | assert(base); |
| 408 | return (base->evsel->name); |
| 409 | } |
| 410 |
| 411 | static void |
| 412 | event_loopexit_cb(int fd, short what, void *arg) |
| 413 | { |
| 414 | struct event_base *base = arg; |
| 415 | base->event_gotterm = 1; |
| 416 | } |
| 417 |
| 418 | /* not thread safe */ |
| 419 | int |
| 420 | event_loopexit(const struct timeval *tv) |
| 421 | { |
| 422 | return (event_once(-1, EV_TIMEOUT, event_loopexit_cb, |
| 423 | current_base, tv)); |
| 424 | } |
| 425 |
| 426 | int |
| 427 | event_base_loopexit(struct event_base *event_base, const struct timeval *tv) |
| 428 | { |
| 429 | return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb, |
| 430 | event_base, tv)); |
| 431 | } |
| 432 |
| 433 | /* not thread safe */ |
| 434 | int |
| 435 | event_loopbreak(void) |
| 436 | { |
| 437 | return (event_base_loopbreak(current_base)); |
| 438 | } |
| 439 |
| 440 | int |
| 441 | event_base_loopbreak(struct event_base *event_base) |
| 442 | { |
| 443 | if (event_base == NULL) |
| 444 | return (-1); |
| 445 |
| 446 | event_base->event_break = 1; |
| 447 | return (0); |
| 448 | } |
| 449 |
| 450 |
| 451 |
| 452 | /* not thread safe */ |
| 453 |
| 454 | int |
| 455 | event_loop(int flags) |
| 456 | { |
| 457 | return event_base_loop(current_base, flags); |
| 458 | } |
| 459 |
| 460 | int |
| 461 | event_base_loop(struct event_base *base, int flags) |
| 462 | { |
| 463 | const struct eventop *evsel = base->evsel; |
| 464 | void *evbase = base->evbase; |
| 465 | struct timeval tv; |
| 466 | struct timeval *tv_p; |
| 467 | int res, done; |
| 468 |
| 469 | /* clear time cache */ |
| 470 | base->tv_cache.tv_sec = 0; |
| 471 |
| 472 | if (base->sig.ev_signal_added) |
| 473 | evsignal_base = base; |
| 474 | done = 0; |
| 475 | while (!done) { |
| 476 | /* Terminate the loop if we have been asked to */ |
| 477 | if (base->event_gotterm) { |
| 478 | base->event_gotterm = 0; |
| 479 | break; |
| 480 | } |
| 481 |
| 482 | if (base->event_break) { |
| 483 | base->event_break = 0; |
| 484 | break; |
| 485 | } |
| 486 |
| 487 | timeout_correct(base, &tv); |
| 488 |
| 489 | tv_p = &tv; |
| 490 | if (!base->event_count_active && !(flags & EVLOOP_NONBLOCK)) { |
| 491 | timeout_next(base, &tv_p); |
| 492 | } else { |
| 493 | /* |
| 494 | * if we have active events, we just poll new events |
| 495 | * without waiting. |
| 496 | */ |
| 497 | evutil_timerclear(&tv); |
| 498 | } |
| 499 |
| 500 | /* If we have no events, we just exit */ |
| 501 | if (!event_haveevents(base)) { |
| 502 | event_debug(("%s: no events registered.", __func__)); |
| 503 | return (1); |
| 504 | } |
| 505 |
| 506 | /* update last old time */ |
| 507 | gettime(base, &base->event_tv); |
| 508 |
| 509 | /* clear time cache */ |
| 510 | base->tv_cache.tv_sec = 0; |
| 511 |
| 512 | res = evsel->dispatch(base, evbase, tv_p); |
| 513 |
| 514 | if (res == -1) |
| 515 | return (-1); |
| 516 | gettime(base, &base->tv_cache); |
| 517 |
| 518 | timeout_process(base); |
| 519 |
| 520 | if (base->event_count_active) { |
| 521 | event_process_active(base); |
| 522 | if (!base->event_count_active && (flags & EVLOOP_ONCE)) |
| 523 | done = 1; |
| 524 | } else if (flags & EVLOOP_NONBLOCK) |
| 525 | done = 1; |
| 526 | } |
| 527 |
| 528 | /* clear time cache */ |
| 529 | base->tv_cache.tv_sec = 0; |
| 530 |
| 531 | event_debug(("%s: asked to terminate loop.", __func__)); |
| 532 | return (0); |
| 533 | } |
| 534 |
| 535 | /* Sets up an event for processing once */ |
| 536 |
| 537 | struct event_once { |
| 538 | struct event ev; |
| 539 |
| 540 | void (*cb)(int, short, void *); |
| 541 | void *arg; |
| 542 | }; |
| 543 |
| 544 | /* One-time callback, it deletes itself */ |
| 545 |
| 546 | static void |
| 547 | event_once_cb(int fd, short events, void *arg) |
| 548 | { |
| 549 | struct event_once *eonce = arg; |
| 550 |
| 551 | (*eonce->cb)(fd, events, eonce->arg); |
| 552 | free(eonce); |
| 553 | } |
| 554 |
| 555 | /* not threadsafe, event scheduled once. */ |
| 556 | int |
| 557 | event_once(int fd, short events, |
| 558 | void (*callback)(int, short, void *), void *arg, const struct timeval *tv) |
| 559 | { |
| 560 | return event_base_once(current_base, fd, events, callback, arg, tv); |
| 561 | } |
| 562 |
| 563 | /* Schedules an event once */ |
| 564 | int |
| 565 | event_base_once(struct event_base *base, int fd, short events, |
| 566 | void (*callback)(int, short, void *), void *arg, const struct timeval *tv) |
| 567 | { |
| 568 | struct event_once *eonce; |
| 569 | struct timeval etv; |
| 570 | int res; |
| 571 |
| 572 | /* We cannot support signals that just fire once */ |
| 573 | if (events & EV_SIGNAL) |
| 574 | return (-1); |
| 575 |
| 576 | if ((eonce = calloc(1, sizeof(struct event_once))) == NULL) |
| 577 | return (-1); |
| 578 |
| 579 | eonce->cb = callback; |
| 580 | eonce->arg = arg; |
| 581 |
| 582 | if (events == EV_TIMEOUT) { |
| 583 | if (tv == NULL) { |
| 584 | evutil_timerclear(&etv); |
| 585 | tv = &etv; |
| 586 | } |
| 587 |
| 588 | evtimer_set(&eonce->ev, event_once_cb, eonce); |
| 589 | } else if (events & (EV_READ|EV_WRITE)) { |
| 590 | events &= EV_READ|EV_WRITE; |
| 591 |
| 592 | event_set(&eonce->ev, fd, events, event_once_cb, eonce); |
| 593 | } else { |
| 594 | /* Bad event combination */ |
| 595 | free(eonce); |
| 596 | return (-1); |
| 597 | } |
| 598 |
| 599 | res = event_base_set(base, &eonce->ev); |
| 600 | if (res == 0) |
| 601 | res = event_add(&eonce->ev, tv); |
| 602 | if (res != 0) { |
| 603 | free(eonce); |
| 604 | return (res); |
| 605 | } |
| 606 |
| 607 | return (0); |
| 608 | } |
| 609 |
| 610 | void |
| 611 | event_set(struct event *ev, int fd, short events, |
| 612 | void (*callback)(int, short, void *), void *arg) |
| 613 | { |
| 614 | /* Take the current base - caller needs to set the real base later */ |
| 615 | ev->ev_base = current_base; |
| 616 |
| 617 | ev->ev_callback = callback; |
| 618 | ev->ev_arg = arg; |
| 619 | ev->ev_fd = fd; |
| 620 | ev->ev_events = events; |
| 621 | ev->ev_res = 0; |
| 622 | ev->ev_flags = EVLIST_INIT; |
| 623 | ev->ev_ncalls = 0; |
| 624 | ev->ev_pncalls = NULL; |
| 625 |
| 626 | min_heap_elem_init(ev); |
| 627 |
| 628 | /* by default, we put new events into the middle priority */ |
| 629 | if(current_base) |
| 630 | ev->ev_pri = current_base->nactivequeues/2; |
| 631 | } |
| 632 |
| 633 | int |
| 634 | event_base_set(struct event_base *base, struct event *ev) |
| 635 | { |
| 636 | /* Only innocent events may be assigned to a different base */ |
| 637 | if (ev->ev_flags != EVLIST_INIT) |
| 638 | return (-1); |
| 639 |
| 640 | ev->ev_base = base; |
| 641 | ev->ev_pri = base->nactivequeues/2; |
| 642 |
| 643 | return (0); |
| 644 | } |
| 645 |
| 646 | /* |
| 647 | * Set's the priority of an event - if an event is already scheduled |
| 648 | * changing the priority is going to fail. |
| 649 | */ |
| 650 |
| 651 | int |
| 652 | event_priority_set(struct event *ev, int pri) |
| 653 | { |
| 654 | if (ev->ev_flags & EVLIST_ACTIVE) |
| 655 | return (-1); |
| 656 | if (pri < 0 || pri >= ev->ev_base->nactivequeues) |
| 657 | return (-1); |
| 658 |
| 659 | ev->ev_pri = pri; |
| 660 |
| 661 | return (0); |
| 662 | } |
| 663 |
| 664 | /* |
| 665 | * Checks if a specific event is pending or scheduled. |
| 666 | */ |
| 667 |
| 668 | int |
| 669 | event_pending(struct event *ev, short event, struct timeval *tv) |
| 670 | { |
| 671 | struct timeval now, res; |
| 672 | int flags = 0; |
| 673 |
| 674 | if (ev->ev_flags & EVLIST_INSERTED) |
| 675 | flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)); |
| 676 | if (ev->ev_flags & EVLIST_ACTIVE) |
| 677 | flags |= ev->ev_res; |
| 678 | if (ev->ev_flags & EVLIST_TIMEOUT) |
| 679 | flags |= EV_TIMEOUT; |
| 680 |
| 681 | event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_SIGNAL); |
| 682 |
| 683 | /* See if there is a timeout that we should report */ |
| 684 | if (tv != NULL && (flags & event & EV_TIMEOUT)) { |
| 685 | gettime(ev->ev_base, &now); |
| 686 | evutil_timersub(&ev->ev_timeout, &now, &res); |
| 687 | /* correctly remap to real time */ |
| 688 | evutil_gettimeofday(&now, NULL); |
| 689 | evutil_timeradd(&now, &res, tv); |
| 690 | } |
| 691 |
| 692 | return (flags & event); |
| 693 | } |
| 694 |
| 695 | int |
| 696 | event_add(struct event *ev, const struct timeval *tv) |
| 697 | { |
| 698 | struct event_base *base = ev->ev_base; |
| 699 | const struct eventop *evsel = base->evsel; |
| 700 | void *evbase = base->evbase; |
| 701 | int res = 0; |
| 702 |
| 703 | event_debug(( |
| 704 | "event_add: event: %p, %s%s%scall %p", |
| 705 | ev, |
| 706 | ev->ev_events & EV_READ ? "EV_READ " : " ", |
| 707 | ev->ev_events & EV_WRITE ? "EV_WRITE " : " ", |
| 708 | tv ? "EV_TIMEOUT " : " ", |
| 709 | ev->ev_callback)); |
| 710 |
| 711 | assert(!(ev->ev_flags & ~EVLIST_ALL)); |
| 712 |
| 713 | /* |
| 714 | * prepare for timeout insertion further below, if we get a |
| 715 | * failure on any step, we should not change any state. |
| 716 | */ |
| 717 | if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) { |
| 718 | if (min_heap_reserve(&base->timeheap, |
| 719 | 1 + min_heap_size(&base->timeheap)) == -1) |
| 720 | return (-1); /* ENOMEM == errno */ |
| 721 | } |
| 722 |
| 723 | if ((ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)) && |
| 724 | !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) { |
| 725 | res = evsel->add(evbase, ev); |
| 726 | if (res != -1) |
| 727 | event_queue_insert(base, ev, EVLIST_INSERTED); |
| 728 | } |
| 729 |
| 730 | /* |
| 731 | * we should change the timout state only if the previous event |
| 732 | * addition succeeded. |
| 733 | */ |
| 734 | if (res != -1 && tv != NULL) { |
| 735 | struct timeval now; |
| 736 |
| 737 | /* |
| 738 | * we already reserved memory above for the case where we |
| 739 | * are not replacing an exisiting timeout. |
| 740 | */ |
| 741 | if (ev->ev_flags & EVLIST_TIMEOUT) |
| 742 | event_queue_remove(base, ev, EVLIST_TIMEOUT); |
| 743 |
| 744 | /* Check if it is active due to a timeout. Rescheduling |
| 745 | * this timeout before the callback can be executed |
| 746 | * removes it from the active list. */ |
| 747 | if ((ev->ev_flags & EVLIST_ACTIVE) && |
| 748 | (ev->ev_res & EV_TIMEOUT)) { |
| 749 | /* See if we are just active executing this |
| 750 | * event in a loop |
| 751 | */ |
| 752 | if (ev->ev_ncalls && ev->ev_pncalls) { |
| 753 | /* Abort loop */ |
| 754 | *ev->ev_pncalls = 0; |
| 755 | } |
| 756 |
| 757 | event_queue_remove(base, ev, EVLIST_ACTIVE); |
| 758 | } |
| 759 |
| 760 | gettime(base, &now); |
| 761 | evutil_timeradd(&now, tv, &ev->ev_timeout); |
| 762 |
| 763 | event_debug(( |
| 764 | "event_add: timeout in %ld seconds, call %p", |
| 765 | tv->tv_sec, ev->ev_callback)); |
| 766 |
| 767 | event_queue_insert(base, ev, EVLIST_TIMEOUT); |
| 768 | } |
| 769 |
| 770 | return (res); |
| 771 | } |
| 772 |
| 773 | int |
| 774 | event_del(struct event *ev) |
| 775 | { |
| 776 | struct event_base *base; |
| 777 |
| 778 | event_debug(("event_del: %p, callback %p", |
| 779 | ev, ev->ev_callback)); |
| 780 |
| 781 | /* An event without a base has not been added */ |
| 782 | if (ev->ev_base == NULL) |
| 783 | return (-1); |
| 784 |
| 785 | base = ev->ev_base; |
| 786 |
| 787 | assert(!(ev->ev_flags & ~EVLIST_ALL)); |
| 788 |
| 789 | /* See if we are just active executing this event in a loop */ |
| 790 | if (ev->ev_ncalls && ev->ev_pncalls) { |
| 791 | /* Abort loop */ |
| 792 | *ev->ev_pncalls = 0; |
| 793 | } |
| 794 |
| 795 | if (ev->ev_flags & EVLIST_TIMEOUT) |
| 796 | event_queue_remove(base, ev, EVLIST_TIMEOUT); |
| 797 |
| 798 | if (ev->ev_flags & EVLIST_ACTIVE) |
| 799 | event_queue_remove(base, ev, EVLIST_ACTIVE); |
| 800 |
| 801 | if (ev->ev_flags & EVLIST_INSERTED) { |
| 802 | event_queue_remove(base, ev, EVLIST_INSERTED); |
| 803 | return (base->evsel->del(base->evbase, ev)); |
| 804 | } |
| 805 |
| 806 | return (0); |
| 807 | } |
| 808 |
| 809 | void |
| 810 | event_active(struct event *ev, int res, short ncalls) |
| 811 | { |
| 812 | /* We get different kinds of events, add them together */ |
| 813 | if (ev->ev_flags & EVLIST_ACTIVE) { |
| 814 | ev->ev_res |= res; |
| 815 | return; |
| 816 | } |
| 817 |
| 818 | ev->ev_res = res; |
| 819 | ev->ev_ncalls = ncalls; |
| 820 | ev->ev_pncalls = NULL; |
| 821 | event_queue_insert(ev->ev_base, ev, EVLIST_ACTIVE); |
| 822 | } |
| 823 |
| 824 | static int |
| 825 | timeout_next(struct event_base *base, struct timeval **tv_p) |
| 826 | { |
| 827 | struct timeval now; |
| 828 | struct event *ev; |
| 829 | struct timeval *tv = *tv_p; |
| 830 |
| 831 | if ((ev = min_heap_top(&base->timeheap)) == NULL) { |
| 832 | /* if no time-based events are active wait for I/O */ |
| 833 | *tv_p = NULL; |
| 834 | return (0); |
| 835 | } |
| 836 |
| 837 | if (gettime(base, &now) == -1) |
| 838 | return (-1); |
| 839 |
| 840 | if (evutil_timercmp(&ev->ev_timeout, &now, <=)) { |
| 841 | evutil_timerclear(tv); |
| 842 | return (0); |
| 843 | } |
| 844 |
| 845 | evutil_timersub(&ev->ev_timeout, &now, tv); |
| 846 |
| 847 | assert(tv->tv_sec >= 0); |
| 848 | assert(tv->tv_usec >= 0); |
| 849 |
| 850 | event_debug(("timeout_next: in %ld seconds", tv->tv_sec)); |
| 851 | return (0); |
| 852 | } |
| 853 |
| 854 | /* |
| 855 | * Determines if the time is running backwards by comparing the current |
| 856 | * time against the last time we checked. Not needed when using clock |
| 857 | * monotonic. |
| 858 | */ |
| 859 |
| 860 | static void |
| 861 | timeout_correct(struct event_base *base, struct timeval *tv) |
| 862 | { |
| 863 | struct event **pev; |
| 864 | unsigned int size; |
| 865 | struct timeval off; |
| 866 |
| 867 | if (use_monotonic) |
| 868 | return; |
| 869 |
| 870 | /* Check if time is running backwards */ |
| 871 | gettime(base, tv); |
| 872 | if (evutil_timercmp(tv, &base->event_tv, >=)) { |
| 873 | base->event_tv = *tv; |
| 874 | return; |
| 875 | } |
| 876 |
| 877 | event_debug(("%s: time is running backwards, corrected", |
| 878 | __func__)); |
| 879 | evutil_timersub(&base->event_tv, tv, &off); |
| 880 |
| 881 | /* |
| 882 | * We can modify the key element of the node without destroying |
| 883 | * the key, beause we apply it to all in the right order. |
| 884 | */ |
| 885 | pev = base->timeheap.p; |
| 886 | size = base->timeheap.n; |
| 887 | for (; size-- > 0; ++pev) { |
| 888 | struct timeval *ev_tv = &(**pev).ev_timeout; |
| 889 | evutil_timersub(ev_tv, &off, ev_tv); |
| 890 | } |
| 891 | /* Now remember what the new time turned out to be. */ |
| 892 | base->event_tv = *tv; |
| 893 | } |
| 894 |
| 895 | void |
| 896 | timeout_process(struct event_base *base) |
| 897 | { |
| 898 | struct timeval now; |
| 899 | struct event *ev; |
| 900 |
| 901 | if (min_heap_empty(&base->timeheap)) |
| 902 | return; |
| 903 |
| 904 | gettime(base, &now); |
| 905 |
| 906 | while ((ev = min_heap_top(&base->timeheap))) { |
| 907 | if (evutil_timercmp(&ev->ev_timeout, &now, >)) |
| 908 | break; |
| 909 |
| 910 | /* delete this event from the I/O queues */ |
| 911 | event_del(ev); |
| 912 |
| 913 | event_debug(("timeout_process: call %p", |
| 914 | ev->ev_callback)); |
| 915 | event_active(ev, EV_TIMEOUT, 1); |
| 916 | } |
| 917 | } |
| 918 |
| 919 | void |
| 920 | event_queue_remove(struct event_base *base, struct event *ev, int queue) |
| 921 | { |
| 922 | if (!(ev->ev_flags & queue)) |
| 923 | event_errx(1, "%s: %p(fd %d) not on queue %x", __func__, |
| 924 | ev, ev->ev_fd, queue); |
| 925 |
| 926 | if (~ev->ev_flags & EVLIST_INTERNAL) |
| 927 | base->event_count--; |
| 928 |
| 929 | ev->ev_flags &= ~queue; |
| 930 | switch (queue) { |
| 931 | case EVLIST_INSERTED: |
| 932 | TAILQ_REMOVE(&base->eventqueue, ev, ev_next); |
| 933 | break; |
| 934 | case EVLIST_ACTIVE: |
| 935 | base->event_count_active--; |
| 936 | TAILQ_REMOVE(base->activequeues[ev->ev_pri], |
| 937 | ev, ev_active_next); |
| 938 | break; |
| 939 | case EVLIST_TIMEOUT: |
| 940 | min_heap_erase(&base->timeheap, ev); |
| 941 | break; |
| 942 | default: |
| 943 | event_errx(1, "%s: unknown queue %x", __func__, queue); |
| 944 | } |
| 945 | } |
| 946 |
| 947 | void |
| 948 | event_queue_insert(struct event_base *base, struct event *ev, int queue) |
| 949 | { |
| 950 | if (ev->ev_flags & queue) { |
| 951 | /* Double insertion is possible for active events */ |
| 952 | if (queue & EVLIST_ACTIVE) |
| 953 | return; |
| 954 |
| 955 | event_errx(1, "%s: %p(fd %d) already on queue %x", __func__, |
| 956 | ev, ev->ev_fd, queue); |
| 957 | } |
| 958 |
| 959 | if (~ev->ev_flags & EVLIST_INTERNAL) |
| 960 | base->event_count++; |
| 961 |
| 962 | ev->ev_flags |= queue; |
| 963 | switch (queue) { |
| 964 | case EVLIST_INSERTED: |
| 965 | TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next); |
| 966 | break; |
| 967 | case EVLIST_ACTIVE: |
| 968 | base->event_count_active++; |
| 969 | TAILQ_INSERT_TAIL(base->activequeues[ev->ev_pri], |
| 970 | ev,ev_active_next); |
| 971 | break; |
| 972 | case EVLIST_TIMEOUT: { |
| 973 | min_heap_push(&base->timeheap, ev); |
| 974 | break; |
| 975 | } |
| 976 | default: |
| 977 | event_errx(1, "%s: unknown queue %x", __func__, queue); |
| 978 | } |
| 979 | } |
| 980 |
| 981 | /* Functions for debugging */ |
| 982 |
| 983 | const char * |
| 984 | event_get_version(void) |
| 985 | { |
| 986 | return (VERSION); |
| 987 | } |
| 988 |
| 989 | /* |
| 990 | * No thread-safe interface needed - the information should be the same |
| 991 | * for all threads. |
| 992 | */ |
| 993 |
| 994 | const char * |
| 995 | event_get_method(void) |
| 996 | { |
| 997 | return (current_base->evsel->name); |
| 998 | } |
| 999 |