2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 #include "event2/event-config.h"
28 #include "evconfig-private.h"
32 #define WIN32_LEAN_AND_MEAN
34 #undef WIN32_LEAN_AND_MEAN
36 #include <sys/types.h>
37 #if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H)
40 #include <sys/queue.h>
41 #ifdef EVENT__HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
46 #ifdef EVENT__HAVE_UNISTD_H
56 #include "event2/event.h"
57 #include "event2/event_struct.h"
58 #include "event2/event_compat.h"
59 #include "event-internal.h"
60 #include "defer-internal.h"
61 #include "evthread-internal.h"
62 #include "event2/thread.h"
63 #include "event2/util.h"
64 #include "log-internal.h"
65 #include "evmap-internal.h"
66 #include "iocp-internal.h"
67 #include "changelist-internal.h"
68 #define HT_NO_CACHE_HASH_VALUES
69 #include "ht-internal.h"
70 #include "util-internal.h"
73 #ifdef EVENT__HAVE_WORKING_KQUEUE
74 #include "kqueue-internal.h"
77 #ifdef EVENT__HAVE_EVENT_PORTS
78 extern const struct eventop evportops;
80 #ifdef EVENT__HAVE_SELECT
81 extern const struct eventop selectops;
83 #ifdef EVENT__HAVE_POLL
84 extern const struct eventop pollops;
86 #ifdef EVENT__HAVE_EPOLL
87 extern const struct eventop epollops;
89 #ifdef EVENT__HAVE_WORKING_KQUEUE
90 extern const struct eventop kqops;
92 #ifdef EVENT__HAVE_DEVPOLL
93 extern const struct eventop devpollops;
96 extern const struct eventop win32ops;
99 /* Array of backends in order of preference. */
100 static const struct eventop *eventops[] = {
101 #ifdef EVENT__HAVE_EVENT_PORTS
104 #ifdef EVENT__HAVE_WORKING_KQUEUE
107 #ifdef EVENT__HAVE_EPOLL
110 #ifdef EVENT__HAVE_DEVPOLL
113 #ifdef EVENT__HAVE_POLL
116 #ifdef EVENT__HAVE_SELECT
125 /* Global state; deprecated */
126 struct event_base *event_global_current_base_ = NULL;
127 #define current_base event_global_current_base_
131 static void *event_self_cbarg_ptr_ = NULL;
134 static void event_queue_insert_active(struct event_base *, struct event_callback *);
135 static void event_queue_insert_active_later(struct event_base *, struct event_callback *);
136 static void event_queue_insert_timeout(struct event_base *, struct event *);
137 static void event_queue_insert_inserted(struct event_base *, struct event *);
138 static void event_queue_remove_active(struct event_base *, struct event_callback *);
139 static void event_queue_remove_active_later(struct event_base *, struct event_callback *);
140 static void event_queue_remove_timeout(struct event_base *, struct event *);
141 static void event_queue_remove_inserted(struct event_base *, struct event *);
142 static void event_queue_make_later_events_active(struct event_base *base);
144 static int evthread_make_base_notifiable_nolock_(struct event_base *base);
145 static int event_del_(struct event *ev, int blocking);
147 #ifdef USE_REINSERT_TIMEOUT
148 /* This code seems buggy; only turn it on if we find out what the trouble is. */
149 static void event_queue_reinsert_timeout(struct event_base *,struct event *, int was_common, int is_common, int old_timeout_idx);
152 static int event_haveevents(struct event_base *);
154 static int event_process_active(struct event_base *);
156 static int timeout_next(struct event_base *, struct timeval **);
157 static void timeout_process(struct event_base *);
159 static inline void event_signal_closure(struct event_base *, struct event *ev);
160 static inline void event_persist_closure(struct event_base *, struct event *ev);
162 static int evthread_notify_base(struct event_base *base);
164 static void insert_common_timeout_inorder(struct common_timeout_list *ctl,
167 #ifndef EVENT__DISABLE_DEBUG_MODE
168 /* These functions implement a hashtable of which 'struct event *' structures
169 * have been setup or added. We don't want to trust the content of the struct
170 * event itself, since we're trying to work through cases where an event gets
171 * clobbered or freed. Instead, we keep a hashtable indexed by the pointer.
174 struct event_debug_entry {
175 HT_ENTRY(event_debug_entry) node;
176 const struct event *ptr;
180 static inline unsigned
181 hash_debug_entry(const struct event_debug_entry *e)
183 /* We need to do this silliness to convince compilers that we
184 * honestly mean to cast e->ptr to an integer, and discard any
185 * part of it that doesn't fit in an unsigned.
187 unsigned u = (unsigned) ((ev_uintptr_t) e->ptr);
188 /* Our hashtable implementation is pretty sensitive to low bits,
189 * and every struct event is over 64 bytes in size, so we can
195 eq_debug_entry(const struct event_debug_entry *a,
196 const struct event_debug_entry *b)
198 return a->ptr == b->ptr;
201 int event_debug_mode_on_ = 0;
202 /* Set if it's too late to enable event_debug_mode. */
203 static int event_debug_mode_too_late = 0;
204 #ifndef EVENT__DISABLE_THREAD_SUPPORT
205 static void *event_debug_map_lock_ = NULL;
207 static HT_HEAD(event_debug_map, event_debug_entry) global_debug_map =
210 HT_PROTOTYPE(event_debug_map, event_debug_entry, node, hash_debug_entry,
212 HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry,
213 eq_debug_entry, 0.5, mm_malloc, mm_realloc, mm_free)
215 /* Macro: record that ev is now setup (that is, ready for an add) */
216 #define event_debug_note_setup_(ev) do { \
217 if (event_debug_mode_on_) { \
218 struct event_debug_entry *dent,find; \
220 EVLOCK_LOCK(event_debug_map_lock_, 0); \
221 dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
225 dent = mm_malloc(sizeof(*dent)); \
228 "Out of memory in debugging code"); \
231 HT_INSERT(event_debug_map, &global_debug_map, dent); \
233 EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
235 event_debug_mode_too_late = 1; \
237 /* Macro: record that ev is no longer setup */
238 #define event_debug_note_teardown_(ev) do { \
239 if (event_debug_mode_on_) { \
240 struct event_debug_entry *dent,find; \
242 EVLOCK_LOCK(event_debug_map_lock_, 0); \
243 dent = HT_REMOVE(event_debug_map, &global_debug_map, &find); \
246 EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
248 event_debug_mode_too_late = 1; \
250 /* Macro: record that ev is now added */
251 #define event_debug_note_add_(ev) do { \
252 if (event_debug_mode_on_) { \
253 struct event_debug_entry *dent,find; \
255 EVLOCK_LOCK(event_debug_map_lock_, 0); \
256 dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
260 event_errx(EVENT_ERR_ABORT_, \
261 "%s: noting an add on a non-setup event %p" \
262 " (events: 0x%x, fd: "EV_SOCK_FMT \
264 __func__, (ev), (ev)->ev_events, \
265 EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
267 EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
269 event_debug_mode_too_late = 1; \
271 /* Macro: record that ev is no longer added */
272 #define event_debug_note_del_(ev) do { \
273 if (event_debug_mode_on_) { \
274 struct event_debug_entry *dent,find; \
276 EVLOCK_LOCK(event_debug_map_lock_, 0); \
277 dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
281 event_errx(EVENT_ERR_ABORT_, \
282 "%s: noting a del on a non-setup event %p" \
283 " (events: 0x%x, fd: "EV_SOCK_FMT \
285 __func__, (ev), (ev)->ev_events, \
286 EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
288 EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
290 event_debug_mode_too_late = 1; \
292 /* Macro: assert that ev is setup (i.e., okay to add or inspect) */
293 #define event_debug_assert_is_setup_(ev) do { \
294 if (event_debug_mode_on_) { \
295 struct event_debug_entry *dent,find; \
297 EVLOCK_LOCK(event_debug_map_lock_, 0); \
298 dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
300 event_errx(EVENT_ERR_ABORT_, \
301 "%s called on a non-initialized event %p" \
302 " (events: 0x%x, fd: "EV_SOCK_FMT\
304 __func__, (ev), (ev)->ev_events, \
305 EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
307 EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
310 /* Macro: assert that ev is not added (i.e., okay to tear down or set
312 #define event_debug_assert_not_added_(ev) do { \
313 if (event_debug_mode_on_) { \
314 struct event_debug_entry *dent,find; \
316 EVLOCK_LOCK(event_debug_map_lock_, 0); \
317 dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
318 if (dent && dent->added) { \
319 event_errx(EVENT_ERR_ABORT_, \
320 "%s called on an already added event %p" \
321 " (events: 0x%x, fd: "EV_SOCK_FMT", " \
323 __func__, (ev), (ev)->ev_events, \
324 EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \
326 EVLOCK_UNLOCK(event_debug_map_lock_, 0); \
330 #define event_debug_note_setup_(ev) \
332 #define event_debug_note_teardown_(ev) \
334 #define event_debug_note_add_(ev) \
336 #define event_debug_note_del_(ev) \
338 #define event_debug_assert_is_setup_(ev) \
340 #define event_debug_assert_not_added_(ev) \
344 #define EVENT_BASE_ASSERT_LOCKED(base) \
345 EVLOCK_ASSERT_LOCKED((base)->th_base_lock)
347 /* How often (in seconds) do we check for changes in wall clock time relative
348 * to monotonic time? Set this to -1 for 'never.' */
349 #define CLOCK_SYNC_INTERVAL 5
351 /** Set 'tp' to the current time according to 'base'. We must hold the lock
352 * on 'base'. If there is a cached time, return it. Otherwise, use
353 * clock_gettime or gettimeofday as appropriate to find out the right time.
354 * Return 0 on success, -1 on failure.
357 gettime(struct event_base *base, struct timeval *tp)
359 EVENT_BASE_ASSERT_LOCKED(base);
361 if (base->tv_cache.tv_sec) {
362 *tp = base->tv_cache;
366 if (evutil_gettime_monotonic_(&base->monotonic_timer, tp) == -1) {
370 if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL
373 evutil_gettimeofday(&tv,NULL);
374 evutil_timersub(&tv, tp, &base->tv_clock_diff);
375 base->last_updated_clock_diff = tp->tv_sec;
382 event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv)
388 return evutil_gettimeofday(tv, NULL);
391 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
392 if (base->tv_cache.tv_sec == 0) {
393 r = evutil_gettimeofday(tv, NULL);
395 evutil_timeradd(&base->tv_cache, &base->tv_clock_diff, tv);
398 EVBASE_RELEASE_LOCK(base, th_base_lock);
402 /** Make 'base' have no current cached time. */
404 clear_time_cache(struct event_base *base)
406 base->tv_cache.tv_sec = 0;
409 /** Replace the cached time in 'base' with the current time. */
411 update_time_cache(struct event_base *base)
413 base->tv_cache.tv_sec = 0;
414 if (!(base->flags & EVENT_BASE_FLAG_NO_CACHE_TIME))
415 gettime(base, &base->tv_cache);
419 event_base_update_cache_time(struct event_base *base)
428 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
429 if (base->running_loop)
430 update_time_cache(base);
431 EVBASE_RELEASE_LOCK(base, th_base_lock);
435 static inline struct event *
436 event_callback_to_event(struct event_callback *evcb)
438 EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_INIT));
439 return EVUTIL_UPCAST(evcb, struct event, ev_evcallback);
442 static inline struct event_callback *
443 event_to_event_callback(struct event *ev)
445 return &ev->ev_evcallback;
451 struct event_base *base = event_base_new_with_config(NULL);
454 event_errx(1, "%s: Unable to construct event_base", __func__);
466 struct event_base *base = NULL;
467 struct event_config *cfg = event_config_new();
469 base = event_base_new_with_config(cfg);
470 event_config_free(cfg);
475 /** Return true iff 'method' is the name of a method that 'cfg' tells us to
478 event_config_is_avoided_method(const struct event_config *cfg,
481 struct event_config_entry *entry;
483 TAILQ_FOREACH(entry, &cfg->entries, next) {
484 if (entry->avoid_method != NULL &&
485 strcmp(entry->avoid_method, method) == 0)
492 /** Return true iff 'method' is disabled according to the environment. */
494 event_is_method_disabled(const char *name)
496 char environment[64];
499 evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name);
500 for (i = 8; environment[i] != '\0'; ++i)
501 environment[i] = EVUTIL_TOUPPER_(environment[i]);
502 /* Note that evutil_getenv_() ignores the environment entirely if
504 return (evutil_getenv_(environment) != NULL);
508 event_base_get_features(const struct event_base *base)
510 return base->evsel->features;
514 event_enable_debug_mode(void)
516 #ifndef EVENT__DISABLE_DEBUG_MODE
517 if (event_debug_mode_on_)
518 event_errx(1, "%s was called twice!", __func__);
519 if (event_debug_mode_too_late)
520 event_errx(1, "%s must be called *before* creating any events "
521 "or event_bases",__func__);
523 event_debug_mode_on_ = 1;
525 HT_INIT(event_debug_map, &global_debug_map);
530 event_disable_debug_mode(void)
532 #ifndef EVENT__DISABLE_DEBUG_MODE
533 struct event_debug_entry **ent, *victim;
535 EVLOCK_LOCK(event_debug_map_lock_, 0);
536 for (ent = HT_START(event_debug_map, &global_debug_map); ent; ) {
538 ent = HT_NEXT_RMV(event_debug_map, &global_debug_map, ent);
541 HT_CLEAR(event_debug_map, &global_debug_map);
542 EVLOCK_UNLOCK(event_debug_map_lock_ , 0);
544 event_debug_mode_on_ = 0;
549 event_base_new_with_config(const struct event_config *cfg)
552 struct event_base *base;
553 int should_check_environment;
555 #ifndef EVENT__DISABLE_DEBUG_MODE
556 event_debug_mode_too_late = 1;
559 if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) {
560 event_warn("%s: calloc", __func__);
565 base->flags = cfg->flags;
567 should_check_environment =
568 !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV));
573 cfg && (cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER);
575 if (should_check_environment && !precise_time) {
576 precise_time = evutil_getenv_("EVENT_PRECISE_TIMER") != NULL;
577 base->flags |= EVENT_BASE_FLAG_PRECISE_TIMER;
579 flags = precise_time ? EV_MONOT_PRECISE : 0;
580 evutil_configure_monotonic_time_(&base->monotonic_timer, flags);
585 min_heap_ctor_(&base->timeheap);
587 base->sig.ev_signal_pair[0] = -1;
588 base->sig.ev_signal_pair[1] = -1;
589 base->th_notify_fd[0] = -1;
590 base->th_notify_fd[1] = -1;
592 TAILQ_INIT(&base->active_later_queue);
594 evmap_io_initmap_(&base->io);
595 evmap_signal_initmap_(&base->sigmap);
596 event_changelist_init_(&base->changelist);
601 memcpy(&base->max_dispatch_time,
602 &cfg->max_dispatch_interval, sizeof(struct timeval));
603 base->limit_callbacks_after_prio =
604 cfg->limit_callbacks_after_prio;
606 base->max_dispatch_time.tv_sec = -1;
607 base->limit_callbacks_after_prio = 1;
609 if (cfg && cfg->max_dispatch_callbacks >= 0) {
610 base->max_dispatch_callbacks = cfg->max_dispatch_callbacks;
612 base->max_dispatch_callbacks = INT_MAX;
614 if (base->max_dispatch_callbacks == INT_MAX &&
615 base->max_dispatch_time.tv_sec == -1)
616 base->limit_callbacks_after_prio = INT_MAX;
618 for (i = 0; eventops[i] && !base->evbase; i++) {
620 /* determine if this backend should be avoided */
621 if (event_config_is_avoided_method(cfg,
624 if ((eventops[i]->features & cfg->require_features)
625 != cfg->require_features)
629 /* also obey the environment variables */
630 if (should_check_environment &&
631 event_is_method_disabled(eventops[i]->name))
634 base->evsel = eventops[i];
636 base->evbase = base->evsel->init(base);
639 if (base->evbase == NULL) {
640 event_warnx("%s: no event mechanism available",
643 event_base_free(base);
647 if (evutil_getenv_("EVENT_SHOW_METHOD"))
648 event_msgx("libevent using: %s", base->evsel->name);
650 /* allocate a single active event queue */
651 if (event_base_priority_init(base, 1) < 0) {
652 event_base_free(base);
656 /* prepare for threading */
658 #ifndef EVENT__DISABLE_THREAD_SUPPORT
659 if (EVTHREAD_LOCKING_ENABLED() &&
660 (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) {
662 EVTHREAD_ALLOC_LOCK(base->th_base_lock, 0);
663 EVTHREAD_ALLOC_COND(base->current_event_cond);
664 r = evthread_make_base_notifiable(base);
666 event_warnx("%s: Unable to make base notifiable.", __func__);
667 event_base_free(base);
674 if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP))
675 event_base_start_iocp_(base, cfg->n_cpus_hint);
682 event_base_start_iocp_(struct event_base *base, int n_cpus)
687 base->iocp = event_iocp_port_launch_(n_cpus);
689 event_warnx("%s: Couldn't launch IOCP", __func__);
699 event_base_stop_iocp_(struct event_base *base)
706 rv = event_iocp_shutdown_(base->iocp, -1);
707 EVUTIL_ASSERT(rv >= 0);
713 event_base_cancel_single_callback_(struct event_base *base,
714 struct event_callback *evcb,
719 if (evcb->evcb_flags & EVLIST_INIT) {
720 struct event *ev = event_callback_to_event(evcb);
721 if (!(ev->ev_flags & EVLIST_INTERNAL)) {
722 event_del_(ev, EVENT_DEL_EVEN_IF_FINALIZING);
726 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
727 event_callback_cancel_nolock_(base, evcb, 1);
728 EVBASE_RELEASE_LOCK(base, th_base_lock);
732 if (run_finalizers && (evcb->evcb_flags & EVLIST_FINALIZING)) {
733 switch (evcb->evcb_closure) {
734 case EV_CLOSURE_EVENT_FINALIZE:
735 case EV_CLOSURE_EVENT_FINALIZE_FREE: {
736 struct event *ev = event_callback_to_event(evcb);
737 ev->ev_evcallback.evcb_cb_union.evcb_evfinalize(ev, ev->ev_arg);
738 if (evcb->evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE)
742 case EV_CLOSURE_CB_FINALIZE:
743 evcb->evcb_cb_union.evcb_cbfinalize(evcb, evcb->evcb_arg);
753 event_base_free_(struct event_base *base, int run_finalizers)
757 /* XXXX grab the lock? If there is contention when one thread frees
758 * the base, then the contending thread will be very sad soon. */
760 /* event_base_free(NULL) is how to free the current_base if we
761 * made it with event_init and forgot to hold a reference to it. */
762 if (base == NULL && current_base)
764 /* Don't actually free NULL. */
766 event_warnx("%s: no base to free", __func__);
769 /* XXX(niels) - check for internal events first */
772 event_base_stop_iocp_(base);
775 /* threading fds if we have them */
776 if (base->th_notify_fd[0] != -1) {
777 event_del(&base->th_notify);
778 EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
779 if (base->th_notify_fd[1] != -1)
780 EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
781 base->th_notify_fd[0] = -1;
782 base->th_notify_fd[1] = -1;
783 event_debug_unassign(&base->th_notify);
786 /* Delete all non-internal events. */
787 evmap_delete_all_(base);
789 while ((ev = min_heap_top_(&base->timeheap)) != NULL) {
793 for (i = 0; i < base->n_common_timeouts; ++i) {
794 struct common_timeout_list *ctl =
795 base->common_timeout_queues[i];
796 event_del(&ctl->timeout_event); /* Internal; doesn't count */
797 event_debug_unassign(&ctl->timeout_event);
798 for (ev = TAILQ_FIRST(&ctl->events); ev; ) {
799 struct event *next = TAILQ_NEXT(ev,
800 ev_timeout_pos.ev_next_with_common_timeout);
801 if (!(ev->ev_flags & EVLIST_INTERNAL)) {
809 if (base->common_timeout_queues)
810 mm_free(base->common_timeout_queues);
812 for (i = 0; i < base->nactivequeues; ++i) {
813 struct event_callback *evcb, *next;
814 for (evcb = TAILQ_FIRST(&base->activequeues[i]); evcb; ) {
815 next = TAILQ_NEXT(evcb, evcb_active_next);
816 n_deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers);
821 struct event_callback *evcb;
822 while ((evcb = TAILQ_FIRST(&base->active_later_queue))) {
823 n_deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers);
829 event_debug(("%s: %d events were still set in base",
830 __func__, n_deleted));
832 while (LIST_FIRST(&base->once_events)) {
833 struct event_once *eonce = LIST_FIRST(&base->once_events);
834 LIST_REMOVE(eonce, next_once);
838 if (base->evsel != NULL && base->evsel->dealloc != NULL)
839 base->evsel->dealloc(base);
841 for (i = 0; i < base->nactivequeues; ++i)
842 EVUTIL_ASSERT(TAILQ_EMPTY(&base->activequeues[i]));
844 EVUTIL_ASSERT(min_heap_empty_(&base->timeheap));
845 min_heap_dtor_(&base->timeheap);
847 mm_free(base->activequeues);
849 evmap_io_clear_(&base->io);
850 evmap_signal_clear_(&base->sigmap);
851 event_changelist_freemem_(&base->changelist);
853 EVTHREAD_FREE_LOCK(base->th_base_lock, 0);
854 EVTHREAD_FREE_COND(base->current_event_cond);
856 /* If we're freeing current_base, there won't be a current_base. */
857 if (base == current_base)
863 event_base_free_nofinalize(struct event_base *base)
865 event_base_free_(base, 0);
869 event_base_free(struct event_base *base)
871 event_base_free_(base, 1);
874 /* Fake eventop; used to disable the backend temporarily inside event_reinit
875 * so that we can call event_del() on an event without telling the backend.
878 nil_backend_del(struct event_base *b, evutil_socket_t fd, short old,
879 short events, void *fdinfo)
883 const struct eventop nil_eventop = {
885 NULL, /* init: unused. */
886 NULL, /* add: unused. */
887 nil_backend_del, /* del: used, so needs to be killed. */
888 NULL, /* dispatch: unused. */
889 NULL, /* dealloc: unused. */
893 /* reinitialize the event base after a fork */
895 event_reinit(struct event_base *base)
897 const struct eventop *evsel;
899 int was_notifiable = 0;
900 int had_signal_added = 0;
902 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
906 /* check if this event mechanism requires reinit on the backend */
907 if (evsel->need_reinit) {
908 /* We're going to call event_del() on our notify events (the
909 * ones that tell about signals and wakeup events). But we
910 * don't actually want to tell the backend to change its
911 * state, since it might still share some resource (a kqueue,
912 * an epoll fd) with the parent process, and we don't want to
913 * delete the fds from _that_ backend, we temporarily stub out
914 * the evsel with a replacement.
916 base->evsel = &nil_eventop;
919 /* We need to re-create a new signal-notification fd and a new
920 * thread-notification fd. Otherwise, we'll still share those with
921 * the parent process, which would make any notification sent to them
922 * get received by one or both of the event loops, more or less at
925 if (base->sig.ev_signal_added) {
926 event_del_nolock_(&base->sig.ev_signal, EVENT_DEL_AUTOBLOCK);
927 event_debug_unassign(&base->sig.ev_signal);
928 memset(&base->sig.ev_signal, 0, sizeof(base->sig.ev_signal));
929 if (base->sig.ev_signal_pair[0] != -1)
930 EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]);
931 if (base->sig.ev_signal_pair[1] != -1)
932 EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]);
933 had_signal_added = 1;
934 base->sig.ev_signal_added = 0;
936 if (base->th_notify_fn != NULL) {
938 base->th_notify_fn = NULL;
940 if (base->th_notify_fd[0] != -1) {
941 event_del_nolock_(&base->th_notify, EVENT_DEL_AUTOBLOCK);
942 EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
943 if (base->th_notify_fd[1] != -1)
944 EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
945 base->th_notify_fd[0] = -1;
946 base->th_notify_fd[1] = -1;
947 event_debug_unassign(&base->th_notify);
950 /* Replace the original evsel. */
953 if (evsel->need_reinit) {
954 /* Reconstruct the backend through brute-force, so that we do
955 * not share any structures with the parent process. For some
956 * backends, this is necessary: epoll and kqueue, for
957 * instance, have events associated with a kernel
958 * structure. If didn't reinitialize, we'd share that
959 * structure with the parent process, and any changes made by
960 * the parent would affect our backend's behavior (and vice
963 if (base->evsel->dealloc != NULL)
964 base->evsel->dealloc(base);
965 base->evbase = evsel->init(base);
966 if (base->evbase == NULL) {
968 "%s: could not reinitialize event mechanism",
974 /* Empty out the changelist (if any): we are starting from a
976 event_changelist_freemem_(&base->changelist);
978 /* Tell the event maps to re-inform the backend about all
979 * pending events. This will make the signal notification
980 * event get re-created if necessary. */
981 if (evmap_reinit_(base) < 0)
984 if (had_signal_added)
985 res = evsig_init_(base);
988 /* If we were notifiable before, and nothing just exploded, become
989 * notifiable again. */
990 if (was_notifiable && res == 0)
991 res = evthread_make_base_notifiable_nolock_(base);
994 EVBASE_RELEASE_LOCK(base, th_base_lock);
998 /* Get the monotonic time for this event_base' timer */
1000 event_gettime_monotonic(struct event_base *base, struct timeval *tv)
1005 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1006 rv = evutil_gettime_monotonic_(&(base->monotonic_timer), tv);
1007 EVBASE_RELEASE_LOCK(base, th_base_lock);
1014 event_get_supported_methods(void)
1016 static const char **methods = NULL;
1017 const struct eventop **method;
1021 /* count all methods */
1022 for (method = &eventops[0]; *method != NULL; ++method) {
1026 /* allocate one more than we need for the NULL pointer */
1027 tmp = mm_calloc((i + 1), sizeof(char *));
1031 /* populate the array with the supported methods */
1032 for (k = 0, i = 0; eventops[k] != NULL; ++k) {
1033 tmp[i++] = eventops[k]->name;
1037 if (methods != NULL)
1038 mm_free((char**)methods);
1045 struct event_config *
1046 event_config_new(void)
1048 struct event_config *cfg = mm_calloc(1, sizeof(*cfg));
1053 TAILQ_INIT(&cfg->entries);
1054 cfg->max_dispatch_interval.tv_sec = -1;
1055 cfg->max_dispatch_callbacks = INT_MAX;
1056 cfg->limit_callbacks_after_prio = 1;
1062 event_config_entry_free(struct event_config_entry *entry)
1064 if (entry->avoid_method != NULL)
1065 mm_free((char *)entry->avoid_method);
1070 event_config_free(struct event_config *cfg)
1072 struct event_config_entry *entry;
1074 while ((entry = TAILQ_FIRST(&cfg->entries)) != NULL) {
1075 TAILQ_REMOVE(&cfg->entries, entry, next);
1076 event_config_entry_free(entry);
1082 event_config_set_flag(struct event_config *cfg, int flag)
1091 event_config_avoid_method(struct event_config *cfg, const char *method)
1093 struct event_config_entry *entry = mm_malloc(sizeof(*entry));
1097 if ((entry->avoid_method = mm_strdup(method)) == NULL) {
1102 TAILQ_INSERT_TAIL(&cfg->entries, entry, next);
1108 event_config_require_features(struct event_config *cfg,
1113 cfg->require_features = features;
1118 event_config_set_num_cpus_hint(struct event_config *cfg, int cpus)
1122 cfg->n_cpus_hint = cpus;
1127 event_config_set_max_dispatch_interval(struct event_config *cfg,
1128 const struct timeval *max_interval, int max_callbacks, int min_priority)
1131 memcpy(&cfg->max_dispatch_interval, max_interval,
1132 sizeof(struct timeval));
1134 cfg->max_dispatch_interval.tv_sec = -1;
1135 cfg->max_dispatch_callbacks =
1136 max_callbacks >= 0 ? max_callbacks : INT_MAX;
1137 if (min_priority < 0)
1139 cfg->limit_callbacks_after_prio = min_priority;
1144 event_priority_init(int npriorities)
1146 return event_base_priority_init(current_base, npriorities);
1150 event_base_priority_init(struct event_base *base, int npriorities)
1155 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1157 if (N_ACTIVE_CALLBACKS(base) || npriorities < 1
1158 || npriorities >= EVENT_MAX_PRIORITIES)
1161 if (npriorities == base->nactivequeues)
1164 if (base->nactivequeues) {
1165 mm_free(base->activequeues);
1166 base->nactivequeues = 0;
1169 /* Allocate our priority queues */
1170 base->activequeues = (struct evcallback_list *)
1171 mm_calloc(npriorities, sizeof(struct evcallback_list));
1172 if (base->activequeues == NULL) {
1173 event_warn("%s: calloc", __func__);
1176 base->nactivequeues = npriorities;
1178 for (i = 0; i < base->nactivequeues; ++i) {
1179 TAILQ_INIT(&base->activequeues[i]);
1185 EVBASE_RELEASE_LOCK(base, th_base_lock);
1190 event_base_get_npriorities(struct event_base *base)
1195 base = current_base;
1197 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1198 n = base->nactivequeues;
1199 EVBASE_RELEASE_LOCK(base, th_base_lock);
1204 event_base_get_num_events(struct event_base *base, unsigned int type)
1208 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1210 if (type & EVENT_BASE_COUNT_ACTIVE)
1211 r += base->event_count_active;
1213 if (type & EVENT_BASE_COUNT_VIRTUAL)
1214 r += base->virtual_event_count;
1216 if (type & EVENT_BASE_COUNT_ADDED)
1217 r += base->event_count;
1219 EVBASE_RELEASE_LOCK(base, th_base_lock);
1225 event_base_get_max_events(struct event_base *base, unsigned int type, int clear)
1229 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1231 if (type & EVENT_BASE_COUNT_ACTIVE) {
1232 r += base->event_count_active_max;
1234 base->event_count_active_max = 0;
1237 if (type & EVENT_BASE_COUNT_VIRTUAL) {
1238 r += base->virtual_event_count_max;
1240 base->virtual_event_count_max = 0;
1243 if (type & EVENT_BASE_COUNT_ADDED) {
1244 r += base->event_count_max;
1246 base->event_count_max = 0;
1249 EVBASE_RELEASE_LOCK(base, th_base_lock);
1254 /* Returns true iff we're currently watching any events. */
1256 event_haveevents(struct event_base *base)
1258 /* Caller must hold th_base_lock */
1259 return (base->virtual_event_count > 0 || base->event_count > 0);
1262 /* "closure" function called when processing active signal events */
1264 event_signal_closure(struct event_base *base, struct event *ev)
1269 /* Allows deletes to work */
1270 ncalls = ev->ev_ncalls;
1272 ev->ev_pncalls = &ncalls;
1273 EVBASE_RELEASE_LOCK(base, th_base_lock);
1276 ev->ev_ncalls = ncalls;
1278 ev->ev_pncalls = NULL;
1279 (*ev->ev_callback)(ev->ev_fd, ev->ev_res, ev->ev_arg);
1281 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1282 should_break = base->event_break;
1283 EVBASE_RELEASE_LOCK(base, th_base_lock);
1287 ev->ev_pncalls = NULL;
1293 /* Common timeouts are special timeouts that are handled as queues rather than
1294 * in the minheap. This is more efficient than the minheap if we happen to
1295 * know that we're going to get several thousands of timeout events all with
1296 * the same timeout value.
1298 * Since all our timeout handling code assumes timevals can be copied,
1299 * assigned, etc, we can't use "magic pointer" to encode these common
1300 * timeouts. Searching through a list to see if every timeout is common could
1301 * also get inefficient. Instead, we take advantage of the fact that tv_usec
1302 * is 32 bits long, but only uses 20 of those bits (since it can never be over
1303 * 999999.) We use the top bits to encode 4 bites of magic number, and 8 bits
1304 * of index into the event_base's aray of common timeouts.
1307 #define MICROSECONDS_MASK COMMON_TIMEOUT_MICROSECONDS_MASK
1308 #define COMMON_TIMEOUT_IDX_MASK 0x0ff00000
1309 #define COMMON_TIMEOUT_IDX_SHIFT 20
1310 #define COMMON_TIMEOUT_MASK 0xf0000000
1311 #define COMMON_TIMEOUT_MAGIC 0x50000000
1313 #define COMMON_TIMEOUT_IDX(tv) \
1314 (((tv)->tv_usec & COMMON_TIMEOUT_IDX_MASK)>>COMMON_TIMEOUT_IDX_SHIFT)
1316 /** Return true iff if 'tv' is a common timeout in 'base' */
1318 is_common_timeout(const struct timeval *tv,
1319 const struct event_base *base)
1322 if ((tv->tv_usec & COMMON_TIMEOUT_MASK) != COMMON_TIMEOUT_MAGIC)
1324 idx = COMMON_TIMEOUT_IDX(tv);
1325 return idx < base->n_common_timeouts;
1328 /* True iff tv1 and tv2 have the same common-timeout index, or if neither
1329 * one is a common timeout. */
1331 is_same_common_timeout(const struct timeval *tv1, const struct timeval *tv2)
1333 return (tv1->tv_usec & ~MICROSECONDS_MASK) ==
1334 (tv2->tv_usec & ~MICROSECONDS_MASK);
1337 /** Requires that 'tv' is a common timeout. Return the corresponding
1338 * common_timeout_list. */
1339 static inline struct common_timeout_list *
1340 get_common_timeout_list(struct event_base *base, const struct timeval *tv)
1342 return base->common_timeout_queues[COMMON_TIMEOUT_IDX(tv)];
1347 common_timeout_ok(const struct timeval *tv,
1348 struct event_base *base)
1350 const struct timeval *expect =
1351 &get_common_timeout_list(base, tv)->duration;
1352 return tv->tv_sec == expect->tv_sec &&
1353 tv->tv_usec == expect->tv_usec;
1357 /* Add the timeout for the first event in given common timeout list to the
1358 * event_base's minheap. */
1360 common_timeout_schedule(struct common_timeout_list *ctl,
1361 const struct timeval *now, struct event *head)
1363 struct timeval timeout = head->ev_timeout;
1364 timeout.tv_usec &= MICROSECONDS_MASK;
1365 event_add_nolock_(&ctl->timeout_event, &timeout, 1);
1368 /* Callback: invoked when the timeout for a common timeout queue triggers.
1369 * This means that (at least) the first event in that queue should be run,
1370 * and the timeout should be rescheduled if there are more events. */
1372 common_timeout_callback(evutil_socket_t fd, short what, void *arg)
1375 struct common_timeout_list *ctl = arg;
1376 struct event_base *base = ctl->base;
1377 struct event *ev = NULL;
1378 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1379 gettime(base, &now);
1381 ev = TAILQ_FIRST(&ctl->events);
1382 if (!ev || ev->ev_timeout.tv_sec > now.tv_sec ||
1383 (ev->ev_timeout.tv_sec == now.tv_sec &&
1384 (ev->ev_timeout.tv_usec&MICROSECONDS_MASK) > now.tv_usec))
1386 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
1387 event_active_nolock_(ev, EV_TIMEOUT, 1);
1390 common_timeout_schedule(ctl, &now, ev);
1391 EVBASE_RELEASE_LOCK(base, th_base_lock);
1394 #define MAX_COMMON_TIMEOUTS 256
1396 const struct timeval *
1397 event_base_init_common_timeout(struct event_base *base,
1398 const struct timeval *duration)
1402 const struct timeval *result=NULL;
1403 struct common_timeout_list *new_ctl;
1405 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1406 if (duration->tv_usec > 1000000) {
1407 memcpy(&tv, duration, sizeof(struct timeval));
1408 if (is_common_timeout(duration, base))
1409 tv.tv_usec &= MICROSECONDS_MASK;
1410 tv.tv_sec += tv.tv_usec / 1000000;
1411 tv.tv_usec %= 1000000;
1414 for (i = 0; i < base->n_common_timeouts; ++i) {
1415 const struct common_timeout_list *ctl =
1416 base->common_timeout_queues[i];
1417 if (duration->tv_sec == ctl->duration.tv_sec &&
1418 duration->tv_usec ==
1419 (ctl->duration.tv_usec & MICROSECONDS_MASK)) {
1420 EVUTIL_ASSERT(is_common_timeout(&ctl->duration, base));
1421 result = &ctl->duration;
1425 if (base->n_common_timeouts == MAX_COMMON_TIMEOUTS) {
1426 event_warnx("%s: Too many common timeouts already in use; "
1427 "we only support %d per event_base", __func__,
1428 MAX_COMMON_TIMEOUTS);
1431 if (base->n_common_timeouts_allocated == base->n_common_timeouts) {
1432 int n = base->n_common_timeouts < 16 ? 16 :
1433 base->n_common_timeouts*2;
1434 struct common_timeout_list **newqueues =
1435 mm_realloc(base->common_timeout_queues,
1436 n*sizeof(struct common_timeout_queue *));
1438 event_warn("%s: realloc",__func__);
1441 base->n_common_timeouts_allocated = n;
1442 base->common_timeout_queues = newqueues;
1444 new_ctl = mm_calloc(1, sizeof(struct common_timeout_list));
1446 event_warn("%s: calloc",__func__);
1449 TAILQ_INIT(&new_ctl->events);
1450 new_ctl->duration.tv_sec = duration->tv_sec;
1451 new_ctl->duration.tv_usec =
1452 duration->tv_usec | COMMON_TIMEOUT_MAGIC |
1453 (base->n_common_timeouts << COMMON_TIMEOUT_IDX_SHIFT);
1454 evtimer_assign(&new_ctl->timeout_event, base,
1455 common_timeout_callback, new_ctl);
1456 new_ctl->timeout_event.ev_flags |= EVLIST_INTERNAL;
1457 event_priority_set(&new_ctl->timeout_event, 0);
1458 new_ctl->base = base;
1459 base->common_timeout_queues[base->n_common_timeouts++] = new_ctl;
1460 result = &new_ctl->duration;
1464 EVUTIL_ASSERT(is_common_timeout(result, base));
1466 EVBASE_RELEASE_LOCK(base, th_base_lock);
1470 /* Closure function invoked when we're activating a persistent event. */
1472 event_persist_closure(struct event_base *base, struct event *ev)
1474 void (*evcb_callback)(evutil_socket_t, short, void *);
1476 // Other fields of *ev that must be stored before executing
1477 evutil_socket_t evcb_fd;
1481 /* reschedule the persistent event if we have a timeout. */
1482 if (ev->ev_io_timeout.tv_sec || ev->ev_io_timeout.tv_usec) {
1483 /* If there was a timeout, we want it to run at an interval of
1484 * ev_io_timeout after the last time it was _scheduled_ for,
1485 * not ev_io_timeout after _now_. If it fired for another
1486 * reason, though, the timeout ought to start ticking _now_. */
1487 struct timeval run_at, relative_to, delay, now;
1488 ev_uint32_t usec_mask = 0;
1489 EVUTIL_ASSERT(is_same_common_timeout(&ev->ev_timeout,
1490 &ev->ev_io_timeout));
1491 gettime(base, &now);
1492 if (is_common_timeout(&ev->ev_timeout, base)) {
1493 delay = ev->ev_io_timeout;
1494 usec_mask = delay.tv_usec & ~MICROSECONDS_MASK;
1495 delay.tv_usec &= MICROSECONDS_MASK;
1496 if (ev->ev_res & EV_TIMEOUT) {
1497 relative_to = ev->ev_timeout;
1498 relative_to.tv_usec &= MICROSECONDS_MASK;
1503 delay = ev->ev_io_timeout;
1504 if (ev->ev_res & EV_TIMEOUT) {
1505 relative_to = ev->ev_timeout;
1510 evutil_timeradd(&relative_to, &delay, &run_at);
1511 if (evutil_timercmp(&run_at, &now, <)) {
1512 /* Looks like we missed at least one invocation due to
1513 * a clock jump, not running the event loop for a
1514 * while, really slow callbacks, or
1515 * something. Reschedule relative to now.
1517 evutil_timeradd(&now, &delay, &run_at);
1519 run_at.tv_usec |= usec_mask;
1520 event_add_nolock_(ev, &run_at, 1);
1523 // Save our callback before we release the lock
1524 evcb_callback = ev->ev_callback;
1525 evcb_fd = ev->ev_fd;
1526 evcb_res = ev->ev_res;
1527 evcb_arg = ev->ev_arg;
1530 EVBASE_RELEASE_LOCK(base, th_base_lock);
1532 // Execute the callback
1533 (evcb_callback)(evcb_fd, evcb_res, evcb_arg);
1537 Helper for event_process_active to process all the events in a single queue,
1538 releasing the lock as we go. This function requires that the lock be held
1539 when it's invoked. Returns -1 if we get a signal or an event_break that
1540 means we should stop processing any active events now. Otherwise returns
1541 the number of non-internal event_callbacks that we processed.
1544 event_process_active_single_queue(struct event_base *base,
1545 struct evcallback_list *activeq,
1546 int max_to_process, const struct timeval *endtime)
1548 struct event_callback *evcb;
1551 EVUTIL_ASSERT(activeq != NULL);
1553 for (evcb = TAILQ_FIRST(activeq); evcb; evcb = TAILQ_FIRST(activeq)) {
1554 struct event *ev=NULL;
1555 if (evcb->evcb_flags & EVLIST_INIT) {
1556 ev = event_callback_to_event(evcb);
1558 if (ev->ev_events & EV_PERSIST || ev->ev_flags & EVLIST_FINALIZING)
1559 event_queue_remove_active(base, evcb);
1561 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
1563 "event_process_active: event: %p, %s%s%scall %p",
1565 ev->ev_res & EV_READ ? "EV_READ " : " ",
1566 ev->ev_res & EV_WRITE ? "EV_WRITE " : " ",
1567 ev->ev_res & EV_CLOSED ? "EV_CLOSED " : " ",
1570 event_queue_remove_active(base, evcb);
1571 event_debug(("event_process_active: event_callback %p, "
1572 "closure %d, call %p",
1573 evcb, evcb->evcb_closure, evcb->evcb_cb_union.evcb_callback));
1576 if (!(evcb->evcb_flags & EVLIST_INTERNAL))
1580 base->current_event = evcb;
1581 #ifndef EVENT__DISABLE_THREAD_SUPPORT
1582 base->current_event_waiters = 0;
1585 switch (evcb->evcb_closure) {
1586 case EV_CLOSURE_EVENT_SIGNAL:
1587 EVUTIL_ASSERT(ev != NULL);
1588 event_signal_closure(base, ev);
1590 case EV_CLOSURE_EVENT_PERSIST:
1591 EVUTIL_ASSERT(ev != NULL);
1592 event_persist_closure(base, ev);
1594 case EV_CLOSURE_EVENT: {
1595 void (*evcb_callback)(evutil_socket_t, short, void *);
1596 EVUTIL_ASSERT(ev != NULL);
1597 evcb_callback = *ev->ev_callback;
1598 EVBASE_RELEASE_LOCK(base, th_base_lock);
1599 evcb_callback(ev->ev_fd, ev->ev_res, ev->ev_arg);
1602 case EV_CLOSURE_CB_SELF: {
1603 void (*evcb_selfcb)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_selfcb;
1604 EVBASE_RELEASE_LOCK(base, th_base_lock);
1605 evcb_selfcb(evcb, evcb->evcb_arg);
1608 case EV_CLOSURE_EVENT_FINALIZE:
1609 case EV_CLOSURE_EVENT_FINALIZE_FREE: {
1610 void (*evcb_evfinalize)(struct event *, void *);
1611 int evcb_closure = evcb->evcb_closure;
1612 EVUTIL_ASSERT(ev != NULL);
1613 base->current_event = NULL;
1614 evcb_evfinalize = ev->ev_evcallback.evcb_cb_union.evcb_evfinalize;
1615 EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
1616 EVBASE_RELEASE_LOCK(base, th_base_lock);
1617 evcb_evfinalize(ev, ev->ev_arg);
1618 event_debug_note_teardown_(ev);
1619 if (evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE)
1623 case EV_CLOSURE_CB_FINALIZE: {
1624 void (*evcb_cbfinalize)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_cbfinalize;
1625 base->current_event = NULL;
1626 EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
1627 EVBASE_RELEASE_LOCK(base, th_base_lock);
1628 evcb_cbfinalize(evcb, evcb->evcb_arg);
1635 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1636 base->current_event = NULL;
1637 #ifndef EVENT__DISABLE_THREAD_SUPPORT
1638 if (base->current_event_waiters) {
1639 base->current_event_waiters = 0;
1640 EVTHREAD_COND_BROADCAST(base->current_event_cond);
1644 if (base->event_break)
1646 if (count >= max_to_process)
1648 if (count && endtime) {
1650 update_time_cache(base);
1651 gettime(base, &now);
1652 if (evutil_timercmp(&now, endtime, >=))
1655 if (base->event_continue)
1662 * Active events are stored in priority queues. Lower priorities are always
1663 * process before higher priorities. Low priority events can starve high
1668 event_process_active(struct event_base *base)
1670 /* Caller must hold th_base_lock */
1671 struct evcallback_list *activeq = NULL;
1673 const struct timeval *endtime;
1675 const int maxcb = base->max_dispatch_callbacks;
1676 const int limit_after_prio = base->limit_callbacks_after_prio;
1677 if (base->max_dispatch_time.tv_sec >= 0) {
1678 update_time_cache(base);
1680 evutil_timeradd(&base->max_dispatch_time, &tv, &tv);
1686 for (i = 0; i < base->nactivequeues; ++i) {
1687 if (TAILQ_FIRST(&base->activequeues[i]) != NULL) {
1688 base->event_running_priority = i;
1689 activeq = &base->activequeues[i];
1690 if (i < limit_after_prio)
1691 c = event_process_active_single_queue(base, activeq,
1694 c = event_process_active_single_queue(base, activeq,
1699 break; /* Processed a real event; do not
1700 * consider lower-priority events */
1701 /* If we get here, all of the events we processed
1702 * were internal. Continue. */
1707 base->event_running_priority = -1;
1713 * Wait continuously for events. We exit only if no events are left.
1717 event_dispatch(void)
1719 return (event_loop(0));
1723 event_base_dispatch(struct event_base *event_base)
1725 return (event_base_loop(event_base, 0));
1729 event_base_get_method(const struct event_base *base)
1731 EVUTIL_ASSERT(base);
1732 return (base->evsel->name);
1735 /** Callback: used to implement event_base_loopexit by telling the event_base
1736 * that it's time to exit its loop. */
1738 event_loopexit_cb(evutil_socket_t fd, short what, void *arg)
1740 struct event_base *base = arg;
1741 base->event_gotterm = 1;
1745 event_loopexit(const struct timeval *tv)
1747 return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
1752 event_base_loopexit(struct event_base *event_base, const struct timeval *tv)
1754 return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb,
1759 event_loopbreak(void)
1761 return (event_base_loopbreak(current_base));
1765 event_base_loopbreak(struct event_base *event_base)
1768 if (event_base == NULL)
1771 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1772 event_base->event_break = 1;
1774 if (EVBASE_NEED_NOTIFY(event_base)) {
1775 r = evthread_notify_base(event_base);
1779 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1784 event_base_loopcontinue(struct event_base *event_base)
1787 if (event_base == NULL)
1790 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1791 event_base->event_continue = 1;
1793 if (EVBASE_NEED_NOTIFY(event_base)) {
1794 r = evthread_notify_base(event_base);
1798 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1803 event_base_got_break(struct event_base *event_base)
1806 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1807 res = event_base->event_break;
1808 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1813 event_base_got_exit(struct event_base *event_base)
1816 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1817 res = event_base->event_gotterm;
1818 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1822 /* not thread safe */
1825 event_loop(int flags)
1827 return event_base_loop(current_base, flags);
1831 event_base_loop(struct event_base *base, int flags)
1833 const struct eventop *evsel = base->evsel;
1835 struct timeval *tv_p;
1836 int res, done, retval = 0;
1838 /* Grab the lock. We will release it inside evsel.dispatch, and again
1839 * as we invoke user callbacks. */
1840 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1842 if (base->running_loop) {
1843 event_warnx("%s: reentrant invocation. Only one event_base_loop"
1844 " can run on each event_base at once.", __func__);
1845 EVBASE_RELEASE_LOCK(base, th_base_lock);
1849 base->running_loop = 1;
1851 clear_time_cache(base);
1853 if (base->sig.ev_signal_added && base->sig.ev_n_signals_added)
1854 evsig_set_base_(base);
1858 #ifndef EVENT__DISABLE_THREAD_SUPPORT
1859 base->th_owner_id = EVTHREAD_GET_ID();
1862 base->event_gotterm = base->event_break = 0;
1865 base->event_continue = 0;
1866 base->n_deferreds_queued = 0;
1868 /* Terminate the loop if we have been asked to */
1869 if (base->event_gotterm) {
1873 if (base->event_break) {
1878 if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) {
1879 timeout_next(base, &tv_p);
1882 * if we have active events, we just poll new events
1885 evutil_timerclear(&tv);
1888 /* If we have no events, we just exit */
1889 if (0==(flags&EVLOOP_NO_EXIT_ON_EMPTY) &&
1890 !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
1891 event_debug(("%s: no events registered.", __func__));
1896 event_queue_make_later_events_active(base);
1898 clear_time_cache(base);
1900 res = evsel->dispatch(base, tv_p);
1903 event_debug(("%s: dispatch returned unsuccessfully.",
1909 update_time_cache(base);
1911 timeout_process(base);
1913 if (N_ACTIVE_CALLBACKS(base)) {
1914 int n = event_process_active(base);
1915 if ((flags & EVLOOP_ONCE)
1916 && N_ACTIVE_CALLBACKS(base) == 0
1919 } else if (flags & EVLOOP_NONBLOCK)
1922 event_debug(("%s: asked to terminate loop.", __func__));
1925 clear_time_cache(base);
1926 base->running_loop = 0;
1928 EVBASE_RELEASE_LOCK(base, th_base_lock);
1933 /* One-time callback to implement event_base_once: invokes the user callback,
1934 * then deletes the allocated storage */
1936 event_once_cb(evutil_socket_t fd, short events, void *arg)
1938 struct event_once *eonce = arg;
1940 (*eonce->cb)(fd, events, eonce->arg);
1941 EVBASE_ACQUIRE_LOCK(eonce->ev.ev_base, th_base_lock);
1942 LIST_REMOVE(eonce, next_once);
1943 EVBASE_RELEASE_LOCK(eonce->ev.ev_base, th_base_lock);
1944 event_debug_unassign(&eonce->ev);
1948 /* not threadsafe, event scheduled once. */
1950 event_once(evutil_socket_t fd, short events,
1951 void (*callback)(evutil_socket_t, short, void *),
1952 void *arg, const struct timeval *tv)
1954 return event_base_once(current_base, fd, events, callback, arg, tv);
1957 /* Schedules an event once */
1959 event_base_once(struct event_base *base, evutil_socket_t fd, short events,
1960 void (*callback)(evutil_socket_t, short, void *),
1961 void *arg, const struct timeval *tv)
1963 struct event_once *eonce;
1967 /* We cannot support signals that just fire once, or persistent
1969 if (events & (EV_SIGNAL|EV_PERSIST))
1972 if ((eonce = mm_calloc(1, sizeof(struct event_once))) == NULL)
1975 eonce->cb = callback;
1978 if ((events & (EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE|EV_CLOSED)) == EV_TIMEOUT) {
1979 evtimer_assign(&eonce->ev, base, event_once_cb, eonce);
1981 if (tv == NULL || ! evutil_timerisset(tv)) {
1982 /* If the event is going to become active immediately,
1983 * don't put it on the timeout queue. This is one
1984 * idiom for scheduling a callback, so let's make
1985 * it fast (and order-preserving). */
1988 } else if (events & (EV_READ|EV_WRITE|EV_CLOSED)) {
1989 events &= EV_READ|EV_WRITE|EV_CLOSED;
1991 event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce);
1993 /* Bad event combination */
1999 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2001 event_active_nolock_(&eonce->ev, EV_TIMEOUT, 1);
2003 res = event_add_nolock_(&eonce->ev, tv, 0);
2009 LIST_INSERT_HEAD(&base->once_events, eonce, next_once);
2011 EVBASE_RELEASE_LOCK(base, th_base_lock);
2018 event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg)
2021 base = current_base;
2022 if (arg == &event_self_cbarg_ptr_)
2025 event_debug_assert_not_added_(ev);
2029 ev->ev_callback = callback;
2032 ev->ev_events = events;
2034 ev->ev_flags = EVLIST_INIT;
2036 ev->ev_pncalls = NULL;
2038 if (events & EV_SIGNAL) {
2039 if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != 0) {
2040 event_warnx("%s: EV_SIGNAL is not compatible with "
2041 "EV_READ, EV_WRITE or EV_CLOSED", __func__);
2044 ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL;
2046 if (events & EV_PERSIST) {
2047 evutil_timerclear(&ev->ev_io_timeout);
2048 ev->ev_closure = EV_CLOSURE_EVENT_PERSIST;
2050 ev->ev_closure = EV_CLOSURE_EVENT;
2054 min_heap_elem_init_(ev);
2057 /* by default, we put new events into the middle priority */
2058 ev->ev_pri = base->nactivequeues / 2;
2061 event_debug_note_setup_(ev);
2067 event_base_set(struct event_base *base, struct event *ev)
2069 /* Only innocent events may be assigned to a different base */
2070 if (ev->ev_flags != EVLIST_INIT)
2073 event_debug_assert_is_setup_(ev);
2076 ev->ev_pri = base->nactivequeues/2;
2082 event_set(struct event *ev, evutil_socket_t fd, short events,
2083 void (*callback)(evutil_socket_t, short, void *), void *arg)
2086 r = event_assign(ev, current_base, fd, events, callback, arg);
2087 EVUTIL_ASSERT(r == 0);
2091 event_self_cbarg(void)
2093 return &event_self_cbarg_ptr_;
2097 event_base_get_running_event(struct event_base *base)
2099 struct event *ev = NULL;
2100 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2101 if (EVBASE_IN_THREAD(base)) {
2102 struct event_callback *evcb = base->current_event;
2103 if (evcb->evcb_flags & EVLIST_INIT)
2104 ev = event_callback_to_event(evcb);
2106 EVBASE_RELEASE_LOCK(base, th_base_lock);
2111 event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
2114 ev = mm_malloc(sizeof(struct event));
2117 if (event_assign(ev, base, fd, events, cb, arg) < 0) {
2126 event_free(struct event *ev)
2128 /* This is disabled, so that events which have been finalized be a
2129 * valid target for event_free(). That's */
2130 // event_debug_assert_is_setup_(ev);
2132 /* make sure that this event won't be coming back to haunt us. */
2134 event_debug_note_teardown_(ev);
2140 event_debug_unassign(struct event *ev)
2142 event_debug_assert_not_added_(ev);
2143 event_debug_note_teardown_(ev);
2145 ev->ev_flags &= ~EVLIST_INIT;
2148 #define EVENT_FINALIZE_FREE_ 0x10000
2150 event_finalize_nolock_(struct event_base *base, unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2152 ev_uint8_t closure = (flags & EVENT_FINALIZE_FREE_) ?
2153 EV_CLOSURE_EVENT_FINALIZE_FREE : EV_CLOSURE_EVENT_FINALIZE;
2155 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
2156 ev->ev_closure = closure;
2157 ev->ev_evcallback.evcb_cb_union.evcb_evfinalize = cb;
2158 event_active_nolock_(ev, EV_FINALIZE, 1);
2159 ev->ev_flags |= EVLIST_FINALIZING;
2164 event_finalize_impl_(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2167 struct event_base *base = ev->ev_base;
2168 if (EVUTIL_FAILURE_CHECK(!base)) {
2169 event_warnx("%s: event has no event_base set.", __func__);
2173 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2174 r = event_finalize_nolock_(base, flags, ev, cb);
2175 EVBASE_RELEASE_LOCK(base, th_base_lock);
2180 event_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2182 return event_finalize_impl_(flags, ev, cb);
2186 event_free_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2188 return event_finalize_impl_(flags|EVENT_FINALIZE_FREE_, ev, cb);
2192 event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *))
2194 struct event *ev = NULL;
2195 if (evcb->evcb_flags & EVLIST_INIT) {
2196 ev = event_callback_to_event(evcb);
2197 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
2199 event_callback_cancel_nolock_(base, evcb, 0); /*XXX can this fail?*/
2202 evcb->evcb_closure = EV_CLOSURE_CB_FINALIZE;
2203 evcb->evcb_cb_union.evcb_cbfinalize = cb;
2204 event_callback_activate_nolock_(base, evcb); /* XXX can this really fail?*/
2205 evcb->evcb_flags |= EVLIST_FINALIZING;
2209 event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *))
2211 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2212 event_callback_finalize_nolock_(base, flags, evcb, cb);
2213 EVBASE_RELEASE_LOCK(base, th_base_lock);
2216 /** Internal: Finalize all of the n_cbs callbacks in evcbs. The provided
2217 * callback will be invoked on *one of them*, after they have *all* been
2220 event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcbs, void (*cb)(struct event_callback *, void *))
2222 int n_pending = 0, i;
2225 base = current_base;
2227 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2229 event_debug(("%s: %d events finalizing", __func__, n_cbs));
2231 /* At most one can be currently executing; the rest we just
2232 * cancel... But we always make sure that the finalize callback
2234 for (i = 0; i < n_cbs; ++i) {
2235 struct event_callback *evcb = evcbs[i];
2236 if (evcb == base->current_event) {
2237 event_callback_finalize_nolock_(base, 0, evcb, cb);
2240 event_callback_cancel_nolock_(base, evcb, 0);
2244 if (n_pending == 0) {
2245 /* Just do the first one. */
2246 event_callback_finalize_nolock_(base, 0, evcbs[0], cb);
2249 EVBASE_RELEASE_LOCK(base, th_base_lock);
2254 * Set's the priority of an event - if an event is already scheduled
2255 * changing the priority is going to fail.
2259 event_priority_set(struct event *ev, int pri)
2261 event_debug_assert_is_setup_(ev);
2263 if (ev->ev_flags & EVLIST_ACTIVE)
2265 if (pri < 0 || pri >= ev->ev_base->nactivequeues)
2274 * Checks if a specific event is pending or scheduled.
2278 event_pending(const struct event *ev, short event, struct timeval *tv)
2282 if (EVUTIL_FAILURE_CHECK(ev->ev_base == NULL)) {
2283 event_warnx("%s: event has no event_base set.", __func__);
2287 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2288 event_debug_assert_is_setup_(ev);
2290 if (ev->ev_flags & EVLIST_INSERTED)
2291 flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL));
2292 if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))
2293 flags |= ev->ev_res;
2294 if (ev->ev_flags & EVLIST_TIMEOUT)
2295 flags |= EV_TIMEOUT;
2297 event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL);
2299 /* See if there is a timeout that we should report */
2300 if (tv != NULL && (flags & event & EV_TIMEOUT)) {
2301 struct timeval tmp = ev->ev_timeout;
2302 tmp.tv_usec &= MICROSECONDS_MASK;
2303 /* correctly remamp to real time */
2304 evutil_timeradd(&ev->ev_base->tv_clock_diff, &tmp, tv);
2307 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2309 return (flags & event);
2313 event_initialized(const struct event *ev)
2315 if (!(ev->ev_flags & EVLIST_INIT))
2322 event_get_assignment(const struct event *event, struct event_base **base_out, evutil_socket_t *fd_out, short *events_out, event_callback_fn *callback_out, void **arg_out)
2324 event_debug_assert_is_setup_(event);
2327 *base_out = event->ev_base;
2329 *fd_out = event->ev_fd;
2331 *events_out = event->ev_events;
2333 *callback_out = event->ev_callback;
2335 *arg_out = event->ev_arg;
2339 event_get_struct_event_size(void)
2341 return sizeof(struct event);
2345 event_get_fd(const struct event *ev)
2347 event_debug_assert_is_setup_(ev);
2352 event_get_base(const struct event *ev)
2354 event_debug_assert_is_setup_(ev);
2359 event_get_events(const struct event *ev)
2361 event_debug_assert_is_setup_(ev);
2362 return ev->ev_events;
2366 event_get_callback(const struct event *ev)
2368 event_debug_assert_is_setup_(ev);
2369 return ev->ev_callback;
2373 event_get_callback_arg(const struct event *ev)
2375 event_debug_assert_is_setup_(ev);
2380 event_get_priority(const struct event *ev)
2382 event_debug_assert_is_setup_(ev);
2387 event_add(struct event *ev, const struct timeval *tv)
2391 if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2392 event_warnx("%s: event has no event_base set.", __func__);
2396 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2398 res = event_add_nolock_(ev, tv, 0);
2400 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2405 /* Helper callback: wake an event_base from another thread. This version
2406 * works by writing a byte to one end of a socketpair, so that the event_base
2407 * listening on the other end will wake up as the corresponding event
2410 evthread_notify_base_default(struct event_base *base)
2416 r = send(base->th_notify_fd[1], buf, 1, 0);
2418 r = write(base->th_notify_fd[1], buf, 1);
2420 return (r < 0 && ! EVUTIL_ERR_IS_EAGAIN(errno)) ? -1 : 0;
2423 #ifdef EVENT__HAVE_EVENTFD
2424 /* Helper callback: wake an event_base from another thread. This version
2425 * assumes that you have a working eventfd() implementation. */
2427 evthread_notify_base_eventfd(struct event_base *base)
2429 ev_uint64_t msg = 1;
2432 r = write(base->th_notify_fd[0], (void*) &msg, sizeof(msg));
2433 } while (r < 0 && errno == EAGAIN);
2435 return (r < 0) ? -1 : 0;
2440 /** Tell the thread currently running the event_loop for base (if any) that it
2441 * needs to stop waiting in its dispatch function (if it is) and process all
2442 * active callbacks. */
2444 evthread_notify_base(struct event_base *base)
2446 EVENT_BASE_ASSERT_LOCKED(base);
2447 if (!base->th_notify_fn)
2449 if (base->is_notify_pending)
2451 base->is_notify_pending = 1;
2452 return base->th_notify_fn(base);
2455 /* Implementation function to remove a timeout on a currently pending event.
2458 event_remove_timer_nolock_(struct event *ev)
2460 struct event_base *base = ev->ev_base;
2462 EVENT_BASE_ASSERT_LOCKED(base);
2463 event_debug_assert_is_setup_(ev);
2465 event_debug(("event_remove_timer_nolock: event: %p", ev));
2467 /* If it's not pending on a timeout, we don't need to do anything. */
2468 if (ev->ev_flags & EVLIST_TIMEOUT) {
2469 event_queue_remove_timeout(base, ev);
2470 evutil_timerclear(&ev->ev_.ev_io.ev_timeout);
2477 event_remove_timer(struct event *ev)
2481 if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2482 event_warnx("%s: event has no event_base set.", __func__);
2486 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2488 res = event_remove_timer_nolock_(ev);
2490 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2495 /* Implementation function to add an event. Works just like event_add,
2496 * except: 1) it requires that we have the lock. 2) if tv_is_absolute is set,
2497 * we treat tv as an absolute time, not as an interval to add to the current
2500 event_add_nolock_(struct event *ev, const struct timeval *tv,
2503 struct event_base *base = ev->ev_base;
2507 EVENT_BASE_ASSERT_LOCKED(base);
2508 event_debug_assert_is_setup_(ev);
2511 "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%s%scall %p",
2513 EV_SOCK_ARG(ev->ev_fd),
2514 ev->ev_events & EV_READ ? "EV_READ " : " ",
2515 ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
2516 ev->ev_events & EV_CLOSED ? "EV_CLOSED " : " ",
2517 tv ? "EV_TIMEOUT " : " ",
2520 EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
2522 if (ev->ev_flags & EVLIST_FINALIZING) {
2528 * prepare for timeout insertion further below, if we get a
2529 * failure on any step, we should not change any state.
2531 if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
2532 if (min_heap_reserve_(&base->timeheap,
2533 1 + min_heap_size_(&base->timeheap)) == -1)
2534 return (-1); /* ENOMEM == errno */
2537 /* If the main thread is currently executing a signal event's
2538 * callback, and we are not the main thread, then we want to wait
2539 * until the callback is done before we mess with the event, or else
2540 * we can race on ev_ncalls and ev_pncalls below. */
2541 #ifndef EVENT__DISABLE_THREAD_SUPPORT
2542 if (base->current_event == event_to_event_callback(ev) &&
2543 (ev->ev_events & EV_SIGNAL)
2544 && !EVBASE_IN_THREAD(base)) {
2545 ++base->current_event_waiters;
2546 EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2550 if ((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)) &&
2551 !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
2552 if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED))
2553 res = evmap_io_add_(base, ev->ev_fd, ev);
2554 else if (ev->ev_events & EV_SIGNAL)
2555 res = evmap_signal_add_(base, (int)ev->ev_fd, ev);
2557 event_queue_insert_inserted(base, ev);
2559 /* evmap says we need to notify the main thread. */
2566 * we should change the timeout state only if the previous event
2567 * addition succeeded.
2569 if (res != -1 && tv != NULL) {
2572 #ifdef USE_REINSERT_TIMEOUT
2574 int old_timeout_idx;
2578 * for persistent timeout events, we remember the
2579 * timeout value and re-add the event.
2581 * If tv_is_absolute, this was already set.
2583 if (ev->ev_closure == EV_CLOSURE_EVENT_PERSIST && !tv_is_absolute)
2584 ev->ev_io_timeout = *tv;
2586 #ifndef USE_REINSERT_TIMEOUT
2587 if (ev->ev_flags & EVLIST_TIMEOUT) {
2588 event_queue_remove_timeout(base, ev);
2592 /* Check if it is active due to a timeout. Rescheduling
2593 * this timeout before the callback can be executed
2594 * removes it from the active list. */
2595 if ((ev->ev_flags & EVLIST_ACTIVE) &&
2596 (ev->ev_res & EV_TIMEOUT)) {
2597 if (ev->ev_events & EV_SIGNAL) {
2598 /* See if we are just active executing
2599 * this event in a loop
2601 if (ev->ev_ncalls && ev->ev_pncalls) {
2603 *ev->ev_pncalls = 0;
2607 event_queue_remove_active(base, event_to_event_callback(ev));
2610 gettime(base, &now);
2612 common_timeout = is_common_timeout(tv, base);
2613 #ifdef USE_REINSERT_TIMEOUT
2614 was_common = is_common_timeout(&ev->ev_timeout, base);
2615 old_timeout_idx = COMMON_TIMEOUT_IDX(&ev->ev_timeout);
2618 if (tv_is_absolute) {
2619 ev->ev_timeout = *tv;
2620 } else if (common_timeout) {
2621 struct timeval tmp = *tv;
2622 tmp.tv_usec &= MICROSECONDS_MASK;
2623 evutil_timeradd(&now, &tmp, &ev->ev_timeout);
2624 ev->ev_timeout.tv_usec |=
2625 (tv->tv_usec & ~MICROSECONDS_MASK);
2627 evutil_timeradd(&now, tv, &ev->ev_timeout);
2631 "event_add: event %p, timeout in %d seconds %d useconds, call %p",
2632 ev, (int)tv->tv_sec, (int)tv->tv_usec, ev->ev_callback));
2634 #ifdef USE_REINSERT_TIMEOUT
2635 event_queue_reinsert_timeout(base, ev, was_common, common_timeout, old_timeout_idx);
2637 event_queue_insert_timeout(base, ev);
2640 if (common_timeout) {
2641 struct common_timeout_list *ctl =
2642 get_common_timeout_list(base, &ev->ev_timeout);
2643 if (ev == TAILQ_FIRST(&ctl->events)) {
2644 common_timeout_schedule(ctl, &now, ev);
2647 struct event* top = NULL;
2648 /* See if the earliest timeout is now earlier than it
2649 * was before: if so, we will need to tell the main
2650 * thread to wake up earlier than it would otherwise.
2651 * We double check the timeout of the top element to
2652 * handle time distortions due to system suspension.
2654 if (min_heap_elt_is_top_(ev))
2656 else if ((top = min_heap_top_(&base->timeheap)) != NULL &&
2657 evutil_timercmp(&top->ev_timeout, &now, <))
2662 /* if we are not in the right thread, we need to wake up the loop */
2663 if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
2664 evthread_notify_base(base);
2666 event_debug_note_add_(ev);
2672 event_del_(struct event *ev, int blocking)
2676 if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2677 event_warnx("%s: event has no event_base set.", __func__);
2681 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2683 res = event_del_nolock_(ev, blocking);
2685 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2691 event_del(struct event *ev)
2693 return event_del_(ev, EVENT_DEL_AUTOBLOCK);
2697 event_del_block(struct event *ev)
2699 return event_del_(ev, EVENT_DEL_BLOCK);
2703 event_del_noblock(struct event *ev)
2705 return event_del_(ev, EVENT_DEL_NOBLOCK);
2708 /** Helper for event_del: always called with th_base_lock held.
2710 * "blocking" must be one of the EVENT_DEL_{BLOCK, NOBLOCK, AUTOBLOCK,
2711 * EVEN_IF_FINALIZING} values. See those for more information.
2714 event_del_nolock_(struct event *ev, int blocking)
2716 struct event_base *base;
2717 int res = 0, notify = 0;
2719 event_debug(("event_del: %p (fd "EV_SOCK_FMT"), callback %p",
2720 ev, EV_SOCK_ARG(ev->ev_fd), ev->ev_callback));
2722 /* An event without a base has not been added */
2723 if (ev->ev_base == NULL)
2726 EVENT_BASE_ASSERT_LOCKED(ev->ev_base);
2728 if (blocking != EVENT_DEL_EVEN_IF_FINALIZING) {
2729 if (ev->ev_flags & EVLIST_FINALIZING) {
2735 /* If the main thread is currently executing this event's callback,
2736 * and we are not the main thread, then we want to wait until the
2737 * callback is done before we start removing the event. That way,
2738 * when this function returns, it will be safe to free the
2739 * user-supplied argument. */
2741 #ifndef EVENT__DISABLE_THREAD_SUPPORT
2742 if (blocking != EVENT_DEL_NOBLOCK &&
2743 base->current_event == event_to_event_callback(ev) &&
2744 !EVBASE_IN_THREAD(base) &&
2745 (blocking == EVENT_DEL_BLOCK || !(ev->ev_events & EV_FINALIZE))) {
2746 ++base->current_event_waiters;
2747 EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2751 EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
2753 /* See if we are just active executing this event in a loop */
2754 if (ev->ev_events & EV_SIGNAL) {
2755 if (ev->ev_ncalls && ev->ev_pncalls) {
2757 *ev->ev_pncalls = 0;
2761 if (ev->ev_flags & EVLIST_TIMEOUT) {
2762 /* NOTE: We never need to notify the main thread because of a
2763 * deleted timeout event: all that could happen if we don't is
2764 * that the dispatch loop might wake up too early. But the
2765 * point of notifying the main thread _is_ to wake up the
2766 * dispatch loop early anyway, so we wouldn't gain anything by
2769 event_queue_remove_timeout(base, ev);
2772 if (ev->ev_flags & EVLIST_ACTIVE)
2773 event_queue_remove_active(base, event_to_event_callback(ev));
2774 else if (ev->ev_flags & EVLIST_ACTIVE_LATER)
2775 event_queue_remove_active_later(base, event_to_event_callback(ev));
2777 if (ev->ev_flags & EVLIST_INSERTED) {
2778 event_queue_remove_inserted(base, ev);
2779 if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED))
2780 res = evmap_io_del_(base, ev->ev_fd, ev);
2782 res = evmap_signal_del_(base, (int)ev->ev_fd, ev);
2784 /* evmap says we need to notify the main thread. */
2790 /* if we are not in the right thread, we need to wake up the loop */
2791 if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
2792 evthread_notify_base(base);
2794 event_debug_note_del_(ev);
2800 event_active(struct event *ev, int res, short ncalls)
2802 if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2803 event_warnx("%s: event has no event_base set.", __func__);
2807 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2809 event_debug_assert_is_setup_(ev);
2811 event_active_nolock_(ev, res, ncalls);
2813 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2818 event_active_nolock_(struct event *ev, int res, short ncalls)
2820 struct event_base *base;
2822 event_debug(("event_active: %p (fd "EV_SOCK_FMT"), res %d, callback %p",
2823 ev, EV_SOCK_ARG(ev->ev_fd), (int)res, ev->ev_callback));
2826 EVENT_BASE_ASSERT_LOCKED(base);
2828 if (ev->ev_flags & EVLIST_FINALIZING) {
2833 switch ((ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
2835 case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER:
2839 /* We get different kinds of events, add them together */
2842 case EVLIST_ACTIVE_LATER:
2850 if (ev->ev_pri < base->event_running_priority)
2851 base->event_continue = 1;
2853 if (ev->ev_events & EV_SIGNAL) {
2854 #ifndef EVENT__DISABLE_THREAD_SUPPORT
2855 if (base->current_event == event_to_event_callback(ev) &&
2856 !EVBASE_IN_THREAD(base)) {
2857 ++base->current_event_waiters;
2858 EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2861 ev->ev_ncalls = ncalls;
2862 ev->ev_pncalls = NULL;
2865 event_callback_activate_nolock_(base, event_to_event_callback(ev));
2869 event_active_later_(struct event *ev, int res)
2871 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2872 event_active_later_nolock_(ev, res);
2873 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2877 event_active_later_nolock_(struct event *ev, int res)
2879 struct event_base *base = ev->ev_base;
2880 EVENT_BASE_ASSERT_LOCKED(base);
2882 if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) {
2883 /* We get different kinds of events, add them together */
2890 event_callback_activate_later_nolock_(base, event_to_event_callback(ev));
2894 event_callback_activate_(struct event_base *base,
2895 struct event_callback *evcb)
2898 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2899 r = event_callback_activate_nolock_(base, evcb);
2900 EVBASE_RELEASE_LOCK(base, th_base_lock);
2905 event_callback_activate_nolock_(struct event_base *base,
2906 struct event_callback *evcb)
2910 if (evcb->evcb_flags & EVLIST_FINALIZING)
2913 switch (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) {
2916 case EVLIST_ACTIVE_LATER:
2917 event_queue_remove_active_later(base, evcb);
2926 event_queue_insert_active(base, evcb);
2928 if (EVBASE_NEED_NOTIFY(base))
2929 evthread_notify_base(base);
2935 event_callback_activate_later_nolock_(struct event_base *base,
2936 struct event_callback *evcb)
2938 if (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))
2941 event_queue_insert_active_later(base, evcb);
2942 if (EVBASE_NEED_NOTIFY(base))
2943 evthread_notify_base(base);
2947 event_callback_init_(struct event_base *base,
2948 struct event_callback *cb)
2950 memset(cb, 0, sizeof(*cb));
2951 cb->evcb_pri = base->nactivequeues - 1;
2955 event_callback_cancel_(struct event_base *base,
2956 struct event_callback *evcb)
2959 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2960 r = event_callback_cancel_nolock_(base, evcb, 0);
2961 EVBASE_RELEASE_LOCK(base, th_base_lock);
2966 event_callback_cancel_nolock_(struct event_base *base,
2967 struct event_callback *evcb, int even_if_finalizing)
2969 if ((evcb->evcb_flags & EVLIST_FINALIZING) && !even_if_finalizing)
2972 if (evcb->evcb_flags & EVLIST_INIT)
2973 return event_del_nolock_(event_callback_to_event(evcb),
2974 even_if_finalizing ? EVENT_DEL_EVEN_IF_FINALIZING : EVENT_DEL_AUTOBLOCK);
2976 switch ((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
2978 case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER:
2982 /* We get different kinds of events, add them together */
2983 event_queue_remove_active(base, evcb);
2985 case EVLIST_ACTIVE_LATER:
2986 event_queue_remove_active_later(base, evcb);
2996 event_deferred_cb_init_(struct event_callback *cb, ev_uint8_t priority, deferred_cb_fn fn, void *arg)
2998 memset(cb, 0, sizeof(*cb));
2999 cb->evcb_cb_union.evcb_selfcb = fn;
3001 cb->evcb_pri = priority;
3002 cb->evcb_closure = EV_CLOSURE_CB_SELF;
3006 event_deferred_cb_set_priority_(struct event_callback *cb, ev_uint8_t priority)
3008 cb->evcb_pri = priority;
3012 event_deferred_cb_cancel_(struct event_base *base, struct event_callback *cb)
3015 base = current_base;
3016 event_callback_cancel_(base, cb);
3019 #define MAX_DEFERREDS_QUEUED 32
3021 event_deferred_cb_schedule_(struct event_base *base, struct event_callback *cb)
3025 base = current_base;
3026 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3027 if (base->n_deferreds_queued > MAX_DEFERREDS_QUEUED) {
3028 event_callback_activate_later_nolock_(base, cb);
3030 ++base->n_deferreds_queued;
3031 r = event_callback_activate_nolock_(base, cb);
3033 EVBASE_RELEASE_LOCK(base, th_base_lock);
3038 timeout_next(struct event_base *base, struct timeval **tv_p)
3040 /* Caller must hold th_base_lock */
3043 struct timeval *tv = *tv_p;
3046 ev = min_heap_top_(&base->timeheap);
3049 /* if no time-based events are active wait for I/O */
3054 if (gettime(base, &now) == -1) {
3059 if (evutil_timercmp(&ev->ev_timeout, &now, <=)) {
3060 evutil_timerclear(tv);
3064 evutil_timersub(&ev->ev_timeout, &now, tv);
3066 EVUTIL_ASSERT(tv->tv_sec >= 0);
3067 EVUTIL_ASSERT(tv->tv_usec >= 0);
3068 event_debug(("timeout_next: event: %p, in %d seconds, %d useconds", ev, (int)tv->tv_sec, (int)tv->tv_usec));
3074 /* Activate every event whose timeout has elapsed. */
3076 timeout_process(struct event_base *base)
3078 /* Caller must hold lock. */
3082 if (min_heap_empty_(&base->timeheap)) {
3086 gettime(base, &now);
3088 while ((ev = min_heap_top_(&base->timeheap))) {
3089 if (evutil_timercmp(&ev->ev_timeout, &now, >))
3092 /* delete this event from the I/O queues */
3093 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
3095 event_debug(("timeout_process: event: %p, call %p",
3096 ev, ev->ev_callback));
3097 event_active_nolock_(ev, EV_TIMEOUT, 1);
3101 #if (EVLIST_INTERNAL >> 4) != 1
3102 #error "Mismatch for value of EVLIST_INTERNAL"
3106 #define MAX(a,b) (((a)>(b))?(a):(b))
3109 #define MAX_EVENT_COUNT(var, v) var = MAX(var, v)
3111 /* These are a fancy way to spell
3112 if (flags & EVLIST_INTERNAL)
3113 base->event_count--/++;
3115 #define DECR_EVENT_COUNT(base,flags) \
3116 ((base)->event_count -= (~((flags) >> 4) & 1))
3117 #define INCR_EVENT_COUNT(base,flags) do { \
3118 ((base)->event_count += (~((flags) >> 4) & 1)); \
3119 MAX_EVENT_COUNT((base)->event_count_max, (base)->event_count); \
3123 event_queue_remove_inserted(struct event_base *base, struct event *ev)
3125 EVENT_BASE_ASSERT_LOCKED(base);
3126 if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_INSERTED))) {
3127 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__,
3128 ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_INSERTED);
3131 DECR_EVENT_COUNT(base, ev->ev_flags);
3132 ev->ev_flags &= ~EVLIST_INSERTED;
3135 event_queue_remove_active(struct event_base *base, struct event_callback *evcb)
3137 EVENT_BASE_ASSERT_LOCKED(base);
3138 if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE))) {
3139 event_errx(1, "%s: %p not on queue %x", __func__,
3140 evcb, EVLIST_ACTIVE);
3143 DECR_EVENT_COUNT(base, evcb->evcb_flags);
3144 evcb->evcb_flags &= ~EVLIST_ACTIVE;
3145 base->event_count_active--;
3147 TAILQ_REMOVE(&base->activequeues[evcb->evcb_pri],
3148 evcb, evcb_active_next);
3151 event_queue_remove_active_later(struct event_base *base, struct event_callback *evcb)
3153 EVENT_BASE_ASSERT_LOCKED(base);
3154 if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE_LATER))) {
3155 event_errx(1, "%s: %p not on queue %x", __func__,
3156 evcb, EVLIST_ACTIVE_LATER);
3159 DECR_EVENT_COUNT(base, evcb->evcb_flags);
3160 evcb->evcb_flags &= ~EVLIST_ACTIVE_LATER;
3161 base->event_count_active--;
3163 TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next);
3166 event_queue_remove_timeout(struct event_base *base, struct event *ev)
3168 EVENT_BASE_ASSERT_LOCKED(base);
3169 if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_TIMEOUT))) {
3170 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__,
3171 ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_TIMEOUT);
3174 DECR_EVENT_COUNT(base, ev->ev_flags);
3175 ev->ev_flags &= ~EVLIST_TIMEOUT;
3177 if (is_common_timeout(&ev->ev_timeout, base)) {
3178 struct common_timeout_list *ctl =
3179 get_common_timeout_list(base, &ev->ev_timeout);
3180 TAILQ_REMOVE(&ctl->events, ev,
3181 ev_timeout_pos.ev_next_with_common_timeout);
3183 min_heap_erase_(&base->timeheap, ev);
3187 #ifdef USE_REINSERT_TIMEOUT
3188 /* Remove and reinsert 'ev' into the timeout queue. */
3190 event_queue_reinsert_timeout(struct event_base *base, struct event *ev,
3191 int was_common, int is_common, int old_timeout_idx)
3193 struct common_timeout_list *ctl;
3194 if (!(ev->ev_flags & EVLIST_TIMEOUT)) {
3195 event_queue_insert_timeout(base, ev);
3199 switch ((was_common<<1) | is_common) {
3200 case 3: /* Changing from one common timeout to another */
3201 ctl = base->common_timeout_queues[old_timeout_idx];
3202 TAILQ_REMOVE(&ctl->events, ev,
3203 ev_timeout_pos.ev_next_with_common_timeout);
3204 ctl = get_common_timeout_list(base, &ev->ev_timeout);
3205 insert_common_timeout_inorder(ctl, ev);
3207 case 2: /* Was common; is no longer common */
3208 ctl = base->common_timeout_queues[old_timeout_idx];
3209 TAILQ_REMOVE(&ctl->events, ev,
3210 ev_timeout_pos.ev_next_with_common_timeout);
3211 min_heap_push_(&base->timeheap, ev);
3213 case 1: /* Wasn't common; has become common. */
3214 min_heap_erase_(&base->timeheap, ev);
3215 ctl = get_common_timeout_list(base, &ev->ev_timeout);
3216 insert_common_timeout_inorder(ctl, ev);
3218 case 0: /* was in heap; is still on heap. */
3219 min_heap_adjust_(&base->timeheap, ev);
3222 EVUTIL_ASSERT(0); /* unreachable */
3228 /* Add 'ev' to the common timeout list in 'ev'. */
3230 insert_common_timeout_inorder(struct common_timeout_list *ctl,
3234 /* By all logic, we should just be able to append 'ev' to the end of
3235 * ctl->events, since the timeout on each 'ev' is set to {the common
3236 * timeout} + {the time when we add the event}, and so the events
3237 * should arrive in order of their timeeouts. But just in case
3238 * there's some wacky threading issue going on, we do a search from
3239 * the end of 'ev' to find the right insertion point.
3241 TAILQ_FOREACH_REVERSE(e, &ctl->events,
3242 event_list, ev_timeout_pos.ev_next_with_common_timeout) {
3243 /* This timercmp is a little sneaky, since both ev and e have
3244 * magic values in tv_usec. Fortunately, they ought to have
3245 * the _same_ magic values in tv_usec. Let's assert for that.
3248 is_same_common_timeout(&e->ev_timeout, &ev->ev_timeout));
3249 if (evutil_timercmp(&ev->ev_timeout, &e->ev_timeout, >=)) {
3250 TAILQ_INSERT_AFTER(&ctl->events, e, ev,
3251 ev_timeout_pos.ev_next_with_common_timeout);
3255 TAILQ_INSERT_HEAD(&ctl->events, ev,
3256 ev_timeout_pos.ev_next_with_common_timeout);
3260 event_queue_insert_inserted(struct event_base *base, struct event *ev)
3262 EVENT_BASE_ASSERT_LOCKED(base);
3264 if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_INSERTED)) {
3265 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already inserted", __func__,
3266 ev, EV_SOCK_ARG(ev->ev_fd));
3270 INCR_EVENT_COUNT(base, ev->ev_flags);
3272 ev->ev_flags |= EVLIST_INSERTED;
3276 event_queue_insert_active(struct event_base *base, struct event_callback *evcb)
3278 EVENT_BASE_ASSERT_LOCKED(base);
3280 if (evcb->evcb_flags & EVLIST_ACTIVE) {
3281 /* Double insertion is possible for active events */
3285 INCR_EVENT_COUNT(base, evcb->evcb_flags);
3287 evcb->evcb_flags |= EVLIST_ACTIVE;
3289 base->event_count_active++;
3290 MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
3291 EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
3292 TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri],
3293 evcb, evcb_active_next);
3297 event_queue_insert_active_later(struct event_base *base, struct event_callback *evcb)
3299 EVENT_BASE_ASSERT_LOCKED(base);
3300 if (evcb->evcb_flags & (EVLIST_ACTIVE_LATER|EVLIST_ACTIVE)) {
3301 /* Double insertion is possible */
3305 INCR_EVENT_COUNT(base, evcb->evcb_flags);
3306 evcb->evcb_flags |= EVLIST_ACTIVE_LATER;
3307 base->event_count_active++;
3308 MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
3309 EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
3310 TAILQ_INSERT_TAIL(&base->active_later_queue, evcb, evcb_active_next);
3314 event_queue_insert_timeout(struct event_base *base, struct event *ev)
3316 EVENT_BASE_ASSERT_LOCKED(base);
3318 if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_TIMEOUT)) {
3319 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on timeout", __func__,
3320 ev, EV_SOCK_ARG(ev->ev_fd));
3324 INCR_EVENT_COUNT(base, ev->ev_flags);
3326 ev->ev_flags |= EVLIST_TIMEOUT;
3328 if (is_common_timeout(&ev->ev_timeout, base)) {
3329 struct common_timeout_list *ctl =
3330 get_common_timeout_list(base, &ev->ev_timeout);
3331 insert_common_timeout_inorder(ctl, ev);
3333 min_heap_push_(&base->timeheap, ev);
3338 event_queue_make_later_events_active(struct event_base *base)
3340 struct event_callback *evcb;
3341 EVENT_BASE_ASSERT_LOCKED(base);
3343 while ((evcb = TAILQ_FIRST(&base->active_later_queue))) {
3344 TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next);
3345 evcb->evcb_flags = (evcb->evcb_flags & ~EVLIST_ACTIVE_LATER) | EVLIST_ACTIVE;
3346 EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
3347 TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], evcb, evcb_active_next);
3348 base->n_deferreds_queued += (evcb->evcb_closure == EV_CLOSURE_CB_SELF);
3352 /* Functions for debugging */
3355 event_get_version(void)
3357 return (EVENT__VERSION);
3361 event_get_version_number(void)
3363 return (EVENT__NUMERIC_VERSION);
3367 * No thread-safe interface needed - the information should be the same
3372 event_get_method(void)
3374 return (current_base->evsel->name);
3377 #ifndef EVENT__DISABLE_MM_REPLACEMENT
3378 static void *(*mm_malloc_fn_)(size_t sz) = NULL;
3379 static void *(*mm_realloc_fn_)(void *p, size_t sz) = NULL;
3380 static void (*mm_free_fn_)(void *p) = NULL;
3383 event_mm_malloc_(size_t sz)
3389 return mm_malloc_fn_(sz);
3395 event_mm_calloc_(size_t count, size_t size)
3397 if (count == 0 || size == 0)
3400 if (mm_malloc_fn_) {
3401 size_t sz = count * size;
3403 if (count > EV_SIZE_MAX / size)
3405 p = mm_malloc_fn_(sz);
3407 return memset(p, 0, sz);
3409 void *p = calloc(count, size);
3411 /* Windows calloc doesn't reliably set ENOMEM */
3424 event_mm_strdup_(const char *str)
3431 if (mm_malloc_fn_) {
3432 size_t ln = strlen(str);
3434 if (ln == EV_SIZE_MAX)
3436 p = mm_malloc_fn_(ln+1);
3438 return memcpy(p, str, ln+1);
3441 return _strdup(str);
3452 event_mm_realloc_(void *ptr, size_t sz)
3455 return mm_realloc_fn_(ptr, sz);
3457 return realloc(ptr, sz);
3461 event_mm_free_(void *ptr)
3470 event_set_mem_functions(void *(*malloc_fn)(size_t sz),
3471 void *(*realloc_fn)(void *ptr, size_t sz),
3472 void (*free_fn)(void *ptr))
3474 mm_malloc_fn_ = malloc_fn;
3475 mm_realloc_fn_ = realloc_fn;
3476 mm_free_fn_ = free_fn;
3480 #ifdef EVENT__HAVE_EVENTFD
3482 evthread_notify_drain_eventfd(evutil_socket_t fd, short what, void *arg)
3486 struct event_base *base = arg;
3488 r = read(fd, (void*) &msg, sizeof(msg));
3489 if (r<0 && errno != EAGAIN) {
3490 event_sock_warn(fd, "Error reading from eventfd");
3492 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3493 base->is_notify_pending = 0;
3494 EVBASE_RELEASE_LOCK(base, th_base_lock);
3499 evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg)
3501 unsigned char buf[1024];
3502 struct event_base *base = arg;
3504 while (recv(fd, (char*)buf, sizeof(buf), 0) > 0)
3507 while (read(fd, (char*)buf, sizeof(buf)) > 0)
3511 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3512 base->is_notify_pending = 0;
3513 EVBASE_RELEASE_LOCK(base, th_base_lock);
3517 evthread_make_base_notifiable(struct event_base *base)
3523 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3524 r = evthread_make_base_notifiable_nolock_(base);
3525 EVBASE_RELEASE_LOCK(base, th_base_lock);
3530 evthread_make_base_notifiable_nolock_(struct event_base *base)
3532 void (*cb)(evutil_socket_t, short, void *);
3533 int (*notify)(struct event_base *);
3535 if (base->th_notify_fn != NULL) {
3536 /* The base is already notifiable: we're doing fine. */
3540 #if defined(EVENT__HAVE_WORKING_KQUEUE)
3541 if (base->evsel == &kqops && event_kq_add_notify_event_(base) == 0) {
3542 base->th_notify_fn = event_kq_notify_base_;
3543 /* No need to add an event here; the backend can wake
3544 * itself up just fine. */
3549 #ifdef EVENT__HAVE_EVENTFD
3550 base->th_notify_fd[0] = evutil_eventfd_(0,
3551 EVUTIL_EFD_CLOEXEC|EVUTIL_EFD_NONBLOCK);
3552 if (base->th_notify_fd[0] >= 0) {
3553 base->th_notify_fd[1] = -1;
3554 notify = evthread_notify_base_eventfd;
3555 cb = evthread_notify_drain_eventfd;
3558 if (evutil_make_internal_pipe_(base->th_notify_fd) == 0) {
3559 notify = evthread_notify_base_default;
3560 cb = evthread_notify_drain_default;
3565 base->th_notify_fn = notify;
3567 /* prepare an event that we can use for wakeup */
3568 event_assign(&base->th_notify, base, base->th_notify_fd[0],
3569 EV_READ|EV_PERSIST, cb, base);
3571 /* we need to mark this as internal event */
3572 base->th_notify.ev_flags |= EVLIST_INTERNAL;
3573 event_priority_set(&base->th_notify, 0);
3575 return event_add_nolock_(&base->th_notify, NULL, 0);
3579 event_base_foreach_event_nolock_(struct event_base *base,
3580 event_base_foreach_event_cb fn, void *arg)
3586 /* Start out with all the EVLIST_INSERTED events. */
3587 if ((r = evmap_foreach_event_(base, fn, arg)))
3590 /* Okay, now we deal with those events that have timeouts and are in
3592 for (u = 0; u < base->timeheap.n; ++u) {
3593 ev = base->timeheap.p[u];
3594 if (ev->ev_flags & EVLIST_INSERTED) {
3595 /* we already processed this one */
3598 if ((r = fn(base, ev, arg)))
3602 /* Now for the events in one of the timeout queues.
3604 for (i = 0; i < base->n_common_timeouts; ++i) {
3605 struct common_timeout_list *ctl =
3606 base->common_timeout_queues[i];
3607 TAILQ_FOREACH(ev, &ctl->events,
3608 ev_timeout_pos.ev_next_with_common_timeout) {
3609 if (ev->ev_flags & EVLIST_INSERTED) {
3610 /* we already processed this one */
3613 if ((r = fn(base, ev, arg)))
3618 /* Finally, we deal wit all the active events that we haven't touched
3620 for (i = 0; i < base->nactivequeues; ++i) {
3621 struct event_callback *evcb;
3622 TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) {
3623 if ((evcb->evcb_flags & (EVLIST_INIT|EVLIST_INSERTED|EVLIST_TIMEOUT)) != EVLIST_INIT) {
3624 /* This isn't an event (evlist_init clear), or
3625 * we already processed it. (inserted or
3629 ev = event_callback_to_event(evcb);
3630 if ((r = fn(base, ev, arg)))
3638 /* Helper for event_base_dump_events: called on each event in the event base;
3639 * dumps only the inserted events. */
3641 dump_inserted_event_fn(const struct event_base *base, const struct event *e, void *arg)
3644 const char *gloss = (e->ev_events & EV_SIGNAL) ?
3647 if (! (e->ev_flags & (EVLIST_INSERTED|EVLIST_TIMEOUT)))
3650 fprintf(output, " %p [%s "EV_SOCK_FMT"]%s%s%s%s%s%s",
3651 (void*)e, gloss, EV_SOCK_ARG(e->ev_fd),
3652 (e->ev_events&EV_READ)?" Read":"",
3653 (e->ev_events&EV_WRITE)?" Write":"",
3654 (e->ev_events&EV_CLOSED)?" EOF":"",
3655 (e->ev_events&EV_SIGNAL)?" Signal":"",
3656 (e->ev_events&EV_PERSIST)?" Persist":"",
3657 (e->ev_flags&EVLIST_INTERNAL)?" Internal":"");
3658 if (e->ev_flags & EVLIST_TIMEOUT) {
3660 tv.tv_sec = e->ev_timeout.tv_sec;
3661 tv.tv_usec = e->ev_timeout.tv_usec & MICROSECONDS_MASK;
3662 evutil_timeradd(&tv, &base->tv_clock_diff, &tv);
3663 fprintf(output, " Timeout=%ld.%06d",
3664 (long)tv.tv_sec, (int)(tv.tv_usec & MICROSECONDS_MASK));
3666 fputc('\n', output);
3671 /* Helper for event_base_dump_events: called on each event in the event base;
3672 * dumps only the active events. */
3674 dump_active_event_fn(const struct event_base *base, const struct event *e, void *arg)
3677 const char *gloss = (e->ev_events & EV_SIGNAL) ?
3680 if (! (e->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)))
3683 fprintf(output, " %p [%s "EV_SOCK_FMT", priority=%d]%s%s%s%s%s active%s%s\n",
3684 (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), e->ev_pri,
3685 (e->ev_res&EV_READ)?" Read":"",
3686 (e->ev_res&EV_WRITE)?" Write":"",
3687 (e->ev_res&EV_CLOSED)?" EOF":"",
3688 (e->ev_res&EV_SIGNAL)?" Signal":"",
3689 (e->ev_res&EV_TIMEOUT)?" Timeout":"",
3690 (e->ev_flags&EVLIST_INTERNAL)?" [Internal]":"",
3691 (e->ev_flags&EVLIST_ACTIVE_LATER)?" [NextTime]":"");
3697 event_base_foreach_event(struct event_base *base,
3698 event_base_foreach_event_cb fn, void *arg)
3701 if ((!fn) || (!base)) {
3704 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3705 r = event_base_foreach_event_nolock_(base, fn, arg);
3706 EVBASE_RELEASE_LOCK(base, th_base_lock);
3712 event_base_dump_events(struct event_base *base, FILE *output)
3714 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3715 fprintf(output, "Inserted events:\n");
3716 event_base_foreach_event_nolock_(base, dump_inserted_event_fn, output);
3718 fprintf(output, "Active events:\n");
3719 event_base_foreach_event_nolock_(base, dump_active_event_fn, output);
3720 EVBASE_RELEASE_LOCK(base, th_base_lock);
3724 event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events)
3726 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3727 evmap_io_active_(base, fd, events & (EV_READ|EV_WRITE|EV_CLOSED));
3728 EVBASE_RELEASE_LOCK(base, th_base_lock);
3732 event_base_active_by_signal(struct event_base *base, int sig)
3734 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3735 evmap_signal_active_(base, sig, 1);
3736 EVBASE_RELEASE_LOCK(base, th_base_lock);
3741 event_base_add_virtual_(struct event_base *base)
3743 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3744 base->virtual_event_count++;
3745 MAX_EVENT_COUNT(base->virtual_event_count_max, base->virtual_event_count);
3746 EVBASE_RELEASE_LOCK(base, th_base_lock);
3750 event_base_del_virtual_(struct event_base *base)
3752 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3753 EVUTIL_ASSERT(base->virtual_event_count > 0);
3754 base->virtual_event_count--;
3755 if (base->virtual_event_count == 0 && EVBASE_NEED_NOTIFY(base))
3756 evthread_notify_base(base);
3757 EVBASE_RELEASE_LOCK(base, th_base_lock);
3761 event_free_debug_globals_locks(void)
3763 #ifndef EVENT__DISABLE_THREAD_SUPPORT
3764 #ifndef EVENT__DISABLE_DEBUG_MODE
3765 if (event_debug_map_lock_ != NULL) {
3766 EVTHREAD_FREE_LOCK(event_debug_map_lock_, 0);
3767 event_debug_map_lock_ = NULL;
3768 evthreadimpl_disable_lock_debugging_();
3770 #endif /* EVENT__DISABLE_DEBUG_MODE */
3771 #endif /* EVENT__DISABLE_THREAD_SUPPORT */
3776 event_free_debug_globals(void)
3778 event_free_debug_globals_locks();
3782 event_free_evsig_globals(void)
3784 evsig_free_globals_();
3788 event_free_evutil_globals(void)
3790 evutil_free_globals_();
3794 event_free_globals(void)
3796 event_free_debug_globals();
3797 event_free_evsig_globals();
3798 event_free_evutil_globals();
3802 libevent_global_shutdown(void)
3804 event_disable_debug_mode();
3805 event_free_globals();
3808 #ifndef EVENT__DISABLE_THREAD_SUPPORT
3810 event_global_setup_locks_(const int enable_locks)
3812 #ifndef EVENT__DISABLE_DEBUG_MODE
3813 EVTHREAD_SETUP_GLOBAL_LOCK(event_debug_map_lock_, 0);
3815 if (evsig_global_setup_locks_(enable_locks) < 0)
3817 if (evutil_global_setup_locks_(enable_locks) < 0)
3819 if (evutil_secure_rng_global_setup_locks_(enable_locks) < 0)
3826 event_base_assert_ok_(struct event_base *base)
3828 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3829 event_base_assert_ok_nolock_(base);
3830 EVBASE_RELEASE_LOCK(base, th_base_lock);
3834 event_base_assert_ok_nolock_(struct event_base *base)
3839 /* First do checks on the per-fd and per-signal lists */
3840 evmap_check_integrity_(base);
3842 /* Check the heap property */
3843 for (i = 1; i < (int)base->timeheap.n; ++i) {
3844 int parent = (i - 1) / 2;
3845 struct event *ev, *p_ev;
3846 ev = base->timeheap.p[i];
3847 p_ev = base->timeheap.p[parent];
3848 EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT);
3849 EVUTIL_ASSERT(evutil_timercmp(&p_ev->ev_timeout, &ev->ev_timeout, <=));
3850 EVUTIL_ASSERT(ev->ev_timeout_pos.min_heap_idx == i);
3853 /* Check that the common timeouts are fine */
3854 for (i = 0; i < base->n_common_timeouts; ++i) {
3855 struct common_timeout_list *ctl = base->common_timeout_queues[i];
3856 struct event *last=NULL, *ev;
3858 EVUTIL_ASSERT_TAILQ_OK(&ctl->events, event, ev_timeout_pos.ev_next_with_common_timeout);
3860 TAILQ_FOREACH(ev, &ctl->events, ev_timeout_pos.ev_next_with_common_timeout) {
3862 EVUTIL_ASSERT(evutil_timercmp(&last->ev_timeout, &ev->ev_timeout, <=));
3863 EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT);
3864 EVUTIL_ASSERT(is_common_timeout(&ev->ev_timeout,base));
3865 EVUTIL_ASSERT(COMMON_TIMEOUT_IDX(&ev->ev_timeout) == i);
3870 /* Check the active queues. */
3872 for (i = 0; i < base->nactivequeues; ++i) {
3873 struct event_callback *evcb;
3874 EVUTIL_ASSERT_TAILQ_OK(&base->activequeues[i], event_callback, evcb_active_next);
3875 TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) {
3876 EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE);
3877 EVUTIL_ASSERT(evcb->evcb_pri == i);
3883 struct event_callback *evcb;
3884 TAILQ_FOREACH(evcb, &base->active_later_queue, evcb_active_next) {
3885 EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE_LATER);
3889 EVUTIL_ASSERT(count == base->event_count_active);