Switch DomAutomationController to be a RenderFrameObserver.
[chromium-blink-merge.git] / third_party / libevent / event.c
blob125335207d518eb63b6ef0e60453221041d90b92
1 /*
2 * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
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.
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.
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
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>
54 #include "event.h"
55 #include "event-internal.h"
56 #include "evutil.h"
57 #include "log.h"
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
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
107 /* Global state */
108 struct event_base *current_base = NULL;
109 extern struct event_base *evsignal_base;
110 static int use_monotonic;
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 *);
117 static void event_process_active(struct event_base *);
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 *);
123 static void
124 detect_monotonic(void)
126 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
127 struct timespec ts;
129 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
130 use_monotonic = 1;
131 #endif
134 static int
135 gettime(struct event_base *base, struct timeval *tp)
137 if (base->tv_cache.tv_sec) {
138 *tp = base->tv_cache;
139 return (0);
142 #if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
143 if (use_monotonic) {
144 struct timespec ts;
146 if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
147 return (-1);
149 tp->tv_sec = ts.tv_sec;
150 tp->tv_usec = ts.tv_nsec / 1000;
151 return (0);
153 #endif
155 return (evutil_gettimeofday(tp, NULL));
158 struct event_base *
159 event_init(void)
161 struct event_base *base = event_base_new();
163 if (base != NULL)
164 current_base = base;
166 return (base);
169 struct event_base *
170 event_base_new(void)
172 int i;
173 struct event_base *base;
175 if ((base = calloc(1, sizeof(struct event_base))) == NULL)
176 event_err(1, "%s: calloc", __func__);
178 detect_monotonic();
179 gettime(base, &base->event_tv);
181 min_heap_ctor(&base->timeheap);
182 TAILQ_INIT(&base->eventqueue);
183 base->sig.ev_signal_pair[0] = -1;
184 base->sig.ev_signal_pair[1] = -1;
186 base->evbase = NULL;
187 for (i = 0; eventops[i] && !base->evbase; i++) {
188 base->evsel = eventops[i];
190 base->evbase = base->evsel->init(base);
193 if (base->evbase == NULL)
194 event_errx(1, "%s: no event mechanism available", __func__);
196 if (evutil_getenv("EVENT_SHOW_METHOD"))
197 event_msgx("libevent using: %s\n",
198 base->evsel->name);
200 /* allocate a single active event queue */
201 event_base_priority_init(base, 1);
203 return (base);
206 void
207 event_base_free(struct event_base *base)
209 int i, n_deleted=0;
210 struct event *ev;
212 if (base == NULL && current_base)
213 base = current_base;
214 if (base == current_base)
215 current_base = NULL;
217 /* XXX(niels) - check for internal events first */
218 assert(base);
219 /* Delete all non-internal events. */
220 for (ev = TAILQ_FIRST(&base->eventqueue); ev; ) {
221 struct event *next = TAILQ_NEXT(ev, ev_next);
222 if (!(ev->ev_flags & EVLIST_INTERNAL)) {
223 event_del(ev);
224 ++n_deleted;
226 ev = next;
228 while ((ev = min_heap_top(&base->timeheap)) != NULL) {
229 event_del(ev);
230 ++n_deleted;
233 for (i = 0; i < base->nactivequeues; ++i) {
234 for (ev = TAILQ_FIRST(base->activequeues[i]); ev; ) {
235 struct event *next = TAILQ_NEXT(ev, ev_active_next);
236 if (!(ev->ev_flags & EVLIST_INTERNAL)) {
237 event_del(ev);
238 ++n_deleted;
240 ev = next;
244 if (n_deleted)
245 event_debug(("%s: %d events were still set in base",
246 __func__, n_deleted));
248 if (base->evsel->dealloc != NULL)
249 base->evsel->dealloc(base, base->evbase);
251 for (i = 0; i < base->nactivequeues; ++i)
252 assert(TAILQ_EMPTY(base->activequeues[i]));
254 assert(min_heap_empty(&base->timeheap));
255 min_heap_dtor(&base->timeheap);
257 for (i = 0; i < base->nactivequeues; ++i)
258 free(base->activequeues[i]);
259 free(base->activequeues);
261 assert(TAILQ_EMPTY(&base->eventqueue));
263 free(base);
266 /* reinitialized the event base after a fork */
268 event_reinit(struct event_base *base)
270 const struct eventop *evsel = base->evsel;
271 void *evbase = base->evbase;
272 int res = 0;
273 struct event *ev;
275 /* check if this event mechanism requires reinit */
276 if (!evsel->need_reinit)
277 return (0);
279 /* prevent internal delete */
280 if (base->sig.ev_signal_added) {
281 /* we cannot call event_del here because the base has
282 * not been reinitialized yet. */
283 event_queue_remove(base, &base->sig.ev_signal,
284 EVLIST_INSERTED);
285 if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE)
286 event_queue_remove(base, &base->sig.ev_signal,
287 EVLIST_ACTIVE);
288 base->sig.ev_signal_added = 0;
291 if (base->evsel->dealloc != NULL)
292 base->evsel->dealloc(base, base->evbase);
293 evbase = base->evbase = evsel->init(base);
294 if (base->evbase == NULL)
295 event_errx(1, "%s: could not reinitialize event mechanism",
296 __func__);
298 TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
299 if (evsel->add(evbase, ev) == -1)
300 res = -1;
303 return (res);
307 event_priority_init(int npriorities)
309 return event_base_priority_init(current_base, npriorities);
313 event_base_priority_init(struct event_base *base, int npriorities)
315 int i;
317 if (base->event_count_active)
318 return (-1);
320 if (base->nactivequeues && npriorities != base->nactivequeues) {
321 for (i = 0; i < base->nactivequeues; ++i) {
322 free(base->activequeues[i]);
324 free(base->activequeues);
327 /* Allocate our priority queues */
328 base->nactivequeues = npriorities;
329 base->activequeues = (struct event_list **)
330 calloc(base->nactivequeues, sizeof(struct event_list *));
331 if (base->activequeues == NULL)
332 event_err(1, "%s: calloc", __func__);
334 for (i = 0; i < base->nactivequeues; ++i) {
335 base->activequeues[i] = malloc(sizeof(struct event_list));
336 if (base->activequeues[i] == NULL)
337 event_err(1, "%s: malloc", __func__);
338 TAILQ_INIT(base->activequeues[i]);
341 return (0);
345 event_haveevents(struct event_base *base)
347 return (base->event_count > 0);
351 * Active events are stored in priority queues. Lower priorities are always
352 * process before higher priorities. Low priority events can starve high
353 * priority ones.
356 static void
357 event_process_active(struct event_base *base)
359 struct event *ev;
360 struct event_list *activeq = NULL;
361 int i;
362 short ncalls;
364 for (i = 0; i < base->nactivequeues; ++i) {
365 if (TAILQ_FIRST(base->activequeues[i]) != NULL) {
366 activeq = base->activequeues[i];
367 break;
371 assert(activeq != NULL);
373 for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) {
374 if (ev->ev_events & EV_PERSIST)
375 event_queue_remove(base, ev, EVLIST_ACTIVE);
376 else
377 event_del(ev);
379 /* Allows deletes to work */
380 ncalls = ev->ev_ncalls;
381 ev->ev_pncalls = &ncalls;
382 while (ncalls) {
383 ncalls--;
384 ev->ev_ncalls = ncalls;
385 (*ev->ev_callback)((int)ev->ev_fd, ev->ev_res, ev->ev_arg);
386 if (base->event_break)
387 return;
393 * Wait continously for events. We exit only if no events are left.
397 event_dispatch(void)
399 return (event_loop(0));
403 event_base_dispatch(struct event_base *event_base)
405 return (event_base_loop(event_base, 0));
408 const char *
409 event_base_get_method(struct event_base *base)
411 assert(base);
412 return (base->evsel->name);
415 static void
416 event_loopexit_cb(int fd, short what, void *arg)
418 struct event_base *base = arg;
419 base->event_gotterm = 1;
422 /* not thread safe */
424 event_loopexit(const struct timeval *tv)
426 return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
427 current_base, tv));
431 event_base_loopexit(struct event_base *event_base, const struct timeval *tv)
433 return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb,
434 event_base, tv));
437 /* not thread safe */
439 event_loopbreak(void)
441 return (event_base_loopbreak(current_base));
445 event_base_loopbreak(struct event_base *event_base)
447 if (event_base == NULL)
448 return (-1);
450 event_base->event_break = 1;
451 return (0);
456 /* not thread safe */
459 event_loop(int flags)
461 return event_base_loop(current_base, flags);
465 event_base_loop(struct event_base *base, int flags)
467 const struct eventop *evsel = base->evsel;
468 void *evbase = base->evbase;
469 struct timeval tv;
470 struct timeval *tv_p;
471 int res, done;
473 /* clear time cache */
474 base->tv_cache.tv_sec = 0;
476 if (base->sig.ev_signal_added)
477 evsignal_base = base;
478 done = 0;
479 while (!done) {
480 /* Terminate the loop if we have been asked to */
481 if (base->event_gotterm) {
482 base->event_gotterm = 0;
483 break;
486 if (base->event_break) {
487 base->event_break = 0;
488 break;
491 timeout_correct(base, &tv);
493 tv_p = &tv;
494 if (!base->event_count_active && !(flags & EVLOOP_NONBLOCK)) {
495 timeout_next(base, &tv_p);
496 } else {
498 * if we have active events, we just poll new events
499 * without waiting.
501 evutil_timerclear(&tv);
504 /* If we have no events, we just exit */
505 if (!event_haveevents(base)) {
506 event_debug(("%s: no events registered.", __func__));
507 return (1);
510 /* update last old time */
511 gettime(base, &base->event_tv);
513 /* clear time cache */
514 base->tv_cache.tv_sec = 0;
516 res = evsel->dispatch(base, evbase, tv_p);
518 if (res == -1)
519 return (-1);
520 gettime(base, &base->tv_cache);
522 timeout_process(base);
524 if (base->event_count_active) {
525 event_process_active(base);
526 if (!base->event_count_active && (flags & EVLOOP_ONCE))
527 done = 1;
528 } else if (flags & EVLOOP_NONBLOCK)
529 done = 1;
532 /* clear time cache */
533 base->tv_cache.tv_sec = 0;
535 event_debug(("%s: asked to terminate loop.", __func__));
536 return (0);
539 /* Sets up an event for processing once */
541 struct event_once {
542 struct event ev;
544 void (*cb)(int, short, void *);
545 void *arg;
548 /* One-time callback, it deletes itself */
550 static void
551 event_once_cb(int fd, short events, void *arg)
553 struct event_once *eonce = arg;
555 (*eonce->cb)(fd, events, eonce->arg);
556 free(eonce);
559 /* not threadsafe, event scheduled once. */
561 event_once(int fd, short events,
562 void (*callback)(int, short, void *), void *arg, const struct timeval *tv)
564 return event_base_once(current_base, fd, events, callback, arg, tv);
567 /* Schedules an event once */
569 event_base_once(struct event_base *base, int fd, short events,
570 void (*callback)(int, short, void *), void *arg, const struct timeval *tv)
572 struct event_once *eonce;
573 struct timeval etv;
574 int res;
576 /* We cannot support signals that just fire once */
577 if (events & EV_SIGNAL)
578 return (-1);
580 if ((eonce = calloc(1, sizeof(struct event_once))) == NULL)
581 return (-1);
583 eonce->cb = callback;
584 eonce->arg = arg;
586 if (events == EV_TIMEOUT) {
587 if (tv == NULL) {
588 evutil_timerclear(&etv);
589 tv = &etv;
592 evtimer_set(&eonce->ev, event_once_cb, eonce);
593 } else if (events & (EV_READ|EV_WRITE)) {
594 events &= EV_READ|EV_WRITE;
596 event_set(&eonce->ev, fd, events, event_once_cb, eonce);
597 } else {
598 /* Bad event combination */
599 free(eonce);
600 return (-1);
603 res = event_base_set(base, &eonce->ev);
604 if (res == 0)
605 res = event_add(&eonce->ev, tv);
606 if (res != 0) {
607 free(eonce);
608 return (res);
611 return (0);
614 void
615 event_set(struct event *ev, int fd, short events,
616 void (*callback)(int, short, void *), void *arg)
618 /* Take the current base - caller needs to set the real base later */
619 ev->ev_base = current_base;
621 ev->ev_callback = callback;
622 ev->ev_arg = arg;
623 ev->ev_fd = fd;
624 ev->ev_events = events;
625 ev->ev_res = 0;
626 ev->ev_flags = EVLIST_INIT;
627 ev->ev_ncalls = 0;
628 ev->ev_pncalls = NULL;
630 min_heap_elem_init(ev);
632 /* by default, we put new events into the middle priority */
633 if(current_base)
634 ev->ev_pri = current_base->nactivequeues/2;
638 event_base_set(struct event_base *base, struct event *ev)
640 /* Only innocent events may be assigned to a different base */
641 if (ev->ev_flags != EVLIST_INIT)
642 return (-1);
644 ev->ev_base = base;
645 ev->ev_pri = base->nactivequeues/2;
647 return (0);
651 * Set's the priority of an event - if an event is already scheduled
652 * changing the priority is going to fail.
656 event_priority_set(struct event *ev, int pri)
658 if (ev->ev_flags & EVLIST_ACTIVE)
659 return (-1);
660 if (pri < 0 || pri >= ev->ev_base->nactivequeues)
661 return (-1);
663 ev->ev_pri = pri;
665 return (0);
669 * Checks if a specific event is pending or scheduled.
673 event_pending(struct event *ev, short event, struct timeval *tv)
675 struct timeval now, res;
676 int flags = 0;
678 if (ev->ev_flags & EVLIST_INSERTED)
679 flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL));
680 if (ev->ev_flags & EVLIST_ACTIVE)
681 flags |= ev->ev_res;
682 if (ev->ev_flags & EVLIST_TIMEOUT)
683 flags |= EV_TIMEOUT;
685 event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_SIGNAL);
687 /* See if there is a timeout that we should report */
688 if (tv != NULL && (flags & event & EV_TIMEOUT)) {
689 gettime(ev->ev_base, &now);
690 evutil_timersub(&ev->ev_timeout, &now, &res);
691 /* correctly remap to real time */
692 evutil_gettimeofday(&now, NULL);
693 evutil_timeradd(&now, &res, tv);
696 return (flags & event);
700 event_add(struct event *ev, const struct timeval *tv)
702 struct event_base *base = ev->ev_base;
703 const struct eventop *evsel = base->evsel;
704 void *evbase = base->evbase;
705 int res = 0;
707 event_debug((
708 "event_add: event: %p, %s%s%scall %p",
710 ev->ev_events & EV_READ ? "EV_READ " : " ",
711 ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
712 tv ? "EV_TIMEOUT " : " ",
713 ev->ev_callback));
715 assert(!(ev->ev_flags & ~EVLIST_ALL));
718 * prepare for timeout insertion further below, if we get a
719 * failure on any step, we should not change any state.
721 if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
722 if (min_heap_reserve(&base->timeheap,
723 1 + min_heap_size(&base->timeheap)) == -1)
724 return (-1); /* ENOMEM == errno */
727 if ((ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)) &&
728 !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) {
729 res = evsel->add(evbase, ev);
730 if (res != -1)
731 event_queue_insert(base, ev, EVLIST_INSERTED);
735 * we should change the timout state only if the previous event
736 * addition succeeded.
738 if (res != -1 && tv != NULL) {
739 struct timeval now;
742 * we already reserved memory above for the case where we
743 * are not replacing an exisiting timeout.
745 if (ev->ev_flags & EVLIST_TIMEOUT)
746 event_queue_remove(base, ev, EVLIST_TIMEOUT);
748 /* Check if it is active due to a timeout. Rescheduling
749 * this timeout before the callback can be executed
750 * removes it from the active list. */
751 if ((ev->ev_flags & EVLIST_ACTIVE) &&
752 (ev->ev_res & EV_TIMEOUT)) {
753 /* See if we are just active executing this
754 * event in a loop
756 if (ev->ev_ncalls && ev->ev_pncalls) {
757 /* Abort loop */
758 *ev->ev_pncalls = 0;
761 event_queue_remove(base, ev, EVLIST_ACTIVE);
764 gettime(base, &now);
765 evutil_timeradd(&now, tv, &ev->ev_timeout);
767 event_debug((
768 "event_add: timeout in %ld seconds, call %p",
769 tv->tv_sec, ev->ev_callback));
771 event_queue_insert(base, ev, EVLIST_TIMEOUT);
774 return (res);
778 event_del(struct event *ev)
780 struct event_base *base;
782 event_debug(("event_del: %p, callback %p",
783 ev, ev->ev_callback));
785 /* An event without a base has not been added */
786 if (ev->ev_base == NULL)
787 return (-1);
789 base = ev->ev_base;
791 assert(!(ev->ev_flags & ~EVLIST_ALL));
793 /* See if we are just active executing this event in a loop */
794 if (ev->ev_ncalls && ev->ev_pncalls) {
795 /* Abort loop */
796 *ev->ev_pncalls = 0;
799 if (ev->ev_flags & EVLIST_TIMEOUT)
800 event_queue_remove(base, ev, EVLIST_TIMEOUT);
802 if (ev->ev_flags & EVLIST_ACTIVE)
803 event_queue_remove(base, ev, EVLIST_ACTIVE);
805 if (ev->ev_flags & EVLIST_INSERTED) {
806 event_queue_remove(base, ev, EVLIST_INSERTED);
807 return (base->evsel->del(base->evbase, ev));
810 return (0);
813 void
814 event_active(struct event *ev, int res, short ncalls)
816 /* We get different kinds of events, add them together */
817 if (ev->ev_flags & EVLIST_ACTIVE) {
818 ev->ev_res |= res;
819 return;
822 ev->ev_res = res;
823 ev->ev_ncalls = ncalls;
824 ev->ev_pncalls = NULL;
825 event_queue_insert(ev->ev_base, ev, EVLIST_ACTIVE);
828 static int
829 timeout_next(struct event_base *base, struct timeval **tv_p)
831 struct timeval now;
832 struct event *ev;
833 struct timeval *tv = *tv_p;
835 if ((ev = min_heap_top(&base->timeheap)) == NULL) {
836 /* if no time-based events are active wait for I/O */
837 *tv_p = NULL;
838 return (0);
841 if (gettime(base, &now) == -1)
842 return (-1);
844 if (evutil_timercmp(&ev->ev_timeout, &now, <=)) {
845 evutil_timerclear(tv);
846 return (0);
849 evutil_timersub(&ev->ev_timeout, &now, tv);
851 assert(tv->tv_sec >= 0);
852 assert(tv->tv_usec >= 0);
854 event_debug(("timeout_next: in %ld seconds", tv->tv_sec));
855 return (0);
859 * Determines if the time is running backwards by comparing the current
860 * time against the last time we checked. Not needed when using clock
861 * monotonic.
864 static void
865 timeout_correct(struct event_base *base, struct timeval *tv)
867 struct event **pev;
868 unsigned int size;
869 struct timeval off;
871 if (use_monotonic)
872 return;
874 /* Check if time is running backwards */
875 gettime(base, tv);
876 if (evutil_timercmp(tv, &base->event_tv, >=)) {
877 base->event_tv = *tv;
878 return;
881 event_debug(("%s: time is running backwards, corrected",
882 __func__));
883 evutil_timersub(&base->event_tv, tv, &off);
886 * We can modify the key element of the node without destroying
887 * the key, beause we apply it to all in the right order.
889 pev = base->timeheap.p;
890 size = base->timeheap.n;
891 for (; size-- > 0; ++pev) {
892 struct timeval *ev_tv = &(**pev).ev_timeout;
893 evutil_timersub(ev_tv, &off, ev_tv);
895 /* Now remember what the new time turned out to be. */
896 base->event_tv = *tv;
899 void
900 timeout_process(struct event_base *base)
902 struct timeval now;
903 struct event *ev;
905 if (min_heap_empty(&base->timeheap))
906 return;
908 gettime(base, &now);
910 while ((ev = min_heap_top(&base->timeheap))) {
911 if (evutil_timercmp(&ev->ev_timeout, &now, >))
912 break;
914 /* delete this event from the I/O queues */
915 event_del(ev);
917 event_debug(("timeout_process: call %p",
918 ev->ev_callback));
919 event_active(ev, EV_TIMEOUT, 1);
923 void
924 event_queue_remove(struct event_base *base, struct event *ev, int queue)
926 if (!(ev->ev_flags & queue))
927 event_errx(1, "%s: %p(fd %d) not on queue %x", __func__,
928 ev, ev->ev_fd, queue);
930 if (~ev->ev_flags & EVLIST_INTERNAL)
931 base->event_count--;
933 ev->ev_flags &= ~queue;
934 switch (queue) {
935 case EVLIST_INSERTED:
936 TAILQ_REMOVE(&base->eventqueue, ev, ev_next);
937 break;
938 case EVLIST_ACTIVE:
939 base->event_count_active--;
940 TAILQ_REMOVE(base->activequeues[ev->ev_pri],
941 ev, ev_active_next);
942 break;
943 case EVLIST_TIMEOUT:
944 min_heap_erase(&base->timeheap, ev);
945 break;
946 default:
947 event_errx(1, "%s: unknown queue %x", __func__, queue);
951 void
952 event_queue_insert(struct event_base *base, struct event *ev, int queue)
954 if (ev->ev_flags & queue) {
955 /* Double insertion is possible for active events */
956 if (queue & EVLIST_ACTIVE)
957 return;
959 event_errx(1, "%s: %p(fd %d) already on queue %x", __func__,
960 ev, ev->ev_fd, queue);
963 if (~ev->ev_flags & EVLIST_INTERNAL)
964 base->event_count++;
966 ev->ev_flags |= queue;
967 switch (queue) {
968 case EVLIST_INSERTED:
969 TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
970 break;
971 case EVLIST_ACTIVE:
972 base->event_count_active++;
973 TAILQ_INSERT_TAIL(base->activequeues[ev->ev_pri],
974 ev,ev_active_next);
975 break;
976 case EVLIST_TIMEOUT: {
977 min_heap_push(&base->timeheap, ev);
978 break;
980 default:
981 event_errx(1, "%s: unknown queue %x", __func__, queue);
985 /* Functions for debugging */
987 const char *
988 event_get_version(void)
990 return (VERSION);
994 * No thread-safe interface needed - the information should be the same
995 * for all threads.
998 const char *
999 event_get_method(void)
1001 return (current_base->evsel->name);