michael@0: /* michael@0: * Copyright (c) 2000-2007 Niels Provos michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #include "event2/event-config.h" michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #define WIN32_LEAN_AND_MEAN michael@0: #include michael@0: #undef WIN32_LEAN_AND_MEAN michael@0: #endif michael@0: #include michael@0: #if !defined(WIN32) && defined(_EVENT_HAVE_SYS_TIME_H) michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifdef _EVENT_HAVE_SYS_SOCKET_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_UNISTD_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_SYS_EVENTFD_H michael@0: #include michael@0: #endif michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "event2/event.h" michael@0: #include "event2/event_struct.h" michael@0: #include "event2/event_compat.h" michael@0: #include "event-internal.h" michael@0: #include "defer-internal.h" michael@0: #include "evthread-internal.h" michael@0: #include "event2/thread.h" michael@0: #include "event2/util.h" michael@0: #include "log-internal.h" michael@0: #include "evmap-internal.h" michael@0: #include "iocp-internal.h" michael@0: #include "changelist-internal.h" michael@0: #include "ht-internal.h" michael@0: #include "util-internal.h" michael@0: michael@0: #ifdef _EVENT_HAVE_EVENT_PORTS michael@0: extern const struct eventop evportops; michael@0: #endif michael@0: #ifdef _EVENT_HAVE_SELECT michael@0: extern const struct eventop selectops; michael@0: #endif michael@0: #ifdef _EVENT_HAVE_POLL michael@0: extern const struct eventop pollops; michael@0: #endif michael@0: #ifdef _EVENT_HAVE_EPOLL michael@0: extern const struct eventop epollops; michael@0: #endif michael@0: #ifdef _EVENT_HAVE_WORKING_KQUEUE michael@0: extern const struct eventop kqops; michael@0: #endif michael@0: #ifdef _EVENT_HAVE_DEVPOLL michael@0: extern const struct eventop devpollops; michael@0: #endif michael@0: #ifdef WIN32 michael@0: extern const struct eventop win32ops; michael@0: #endif michael@0: michael@0: /* Array of backends in order of preference. */ michael@0: static const struct eventop *eventops[] = { michael@0: #ifdef _EVENT_HAVE_EVENT_PORTS michael@0: &evportops, michael@0: #endif michael@0: #ifdef _EVENT_HAVE_WORKING_KQUEUE michael@0: &kqops, michael@0: #endif michael@0: #ifdef _EVENT_HAVE_EPOLL michael@0: &epollops, michael@0: #endif michael@0: #ifdef _EVENT_HAVE_DEVPOLL michael@0: &devpollops, michael@0: #endif michael@0: #ifdef _EVENT_HAVE_POLL michael@0: &pollops, michael@0: #endif michael@0: #ifdef _EVENT_HAVE_SELECT michael@0: &selectops, michael@0: #endif michael@0: #ifdef WIN32 michael@0: &win32ops, michael@0: #endif michael@0: NULL michael@0: }; michael@0: michael@0: /* Global state; deprecated */ michael@0: struct event_base *event_global_current_base_ = NULL; michael@0: #define current_base event_global_current_base_ michael@0: michael@0: /* Global state */ michael@0: michael@0: static int use_monotonic; michael@0: michael@0: /* Prototypes */ michael@0: static inline int event_add_internal(struct event *ev, michael@0: const struct timeval *tv, int tv_is_absolute); michael@0: static inline int event_del_internal(struct event *ev); michael@0: michael@0: static void event_queue_insert(struct event_base *, struct event *, int); michael@0: static void event_queue_remove(struct event_base *, struct event *, int); michael@0: static int event_haveevents(struct event_base *); michael@0: michael@0: static int event_process_active(struct event_base *); michael@0: michael@0: static int timeout_next(struct event_base *, struct timeval **); michael@0: static void timeout_process(struct event_base *); michael@0: static void timeout_correct(struct event_base *, struct timeval *); michael@0: michael@0: static inline void event_signal_closure(struct event_base *, struct event *ev); michael@0: static inline void event_persist_closure(struct event_base *, struct event *ev); michael@0: michael@0: static int evthread_notify_base(struct event_base *base); michael@0: michael@0: #ifndef _EVENT_DISABLE_DEBUG_MODE michael@0: /* These functions implement a hashtable of which 'struct event *' structures michael@0: * have been setup or added. We don't want to trust the content of the struct michael@0: * event itself, since we're trying to work through cases where an event gets michael@0: * clobbered or freed. Instead, we keep a hashtable indexed by the pointer. michael@0: */ michael@0: michael@0: struct event_debug_entry { michael@0: HT_ENTRY(event_debug_entry) node; michael@0: const struct event *ptr; michael@0: unsigned added : 1; michael@0: }; michael@0: michael@0: static inline unsigned michael@0: hash_debug_entry(const struct event_debug_entry *e) michael@0: { michael@0: /* We need to do this silliness to convince compilers that we michael@0: * honestly mean to cast e->ptr to an integer, and discard any michael@0: * part of it that doesn't fit in an unsigned. michael@0: */ michael@0: unsigned u = (unsigned) ((ev_uintptr_t) e->ptr); michael@0: /* Our hashtable implementation is pretty sensitive to low bits, michael@0: * and every struct event is over 64 bytes in size, so we can michael@0: * just say >>6. */ michael@0: return (u >> 6); michael@0: } michael@0: michael@0: static inline int michael@0: eq_debug_entry(const struct event_debug_entry *a, michael@0: const struct event_debug_entry *b) michael@0: { michael@0: return a->ptr == b->ptr; michael@0: } michael@0: michael@0: int _event_debug_mode_on = 0; michael@0: /* Set if it's too late to enable event_debug_mode. */ michael@0: static int event_debug_mode_too_late = 0; michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: static void *_event_debug_map_lock = NULL; michael@0: #endif michael@0: static HT_HEAD(event_debug_map, event_debug_entry) global_debug_map = michael@0: HT_INITIALIZER(); michael@0: michael@0: HT_PROTOTYPE(event_debug_map, event_debug_entry, node, hash_debug_entry, michael@0: eq_debug_entry) michael@0: HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry, michael@0: eq_debug_entry, 0.5, mm_malloc, mm_realloc, mm_free) michael@0: michael@0: /* Macro: record that ev is now setup (that is, ready for an add) */ michael@0: #define _event_debug_note_setup(ev) do { \ michael@0: if (_event_debug_mode_on) { \ michael@0: struct event_debug_entry *dent,find; \ michael@0: find.ptr = (ev); \ michael@0: EVLOCK_LOCK(_event_debug_map_lock, 0); \ michael@0: dent = HT_FIND(event_debug_map, &global_debug_map, &find); \ michael@0: if (dent) { \ michael@0: dent->added = 0; \ michael@0: } else { \ michael@0: dent = mm_malloc(sizeof(*dent)); \ michael@0: if (!dent) \ michael@0: event_err(1, \ michael@0: "Out of memory in debugging code"); \ michael@0: dent->ptr = (ev); \ michael@0: dent->added = 0; \ michael@0: HT_INSERT(event_debug_map, &global_debug_map, dent); \ michael@0: } \ michael@0: EVLOCK_UNLOCK(_event_debug_map_lock, 0); \ michael@0: } \ michael@0: event_debug_mode_too_late = 1; \ michael@0: } while (0) michael@0: /* Macro: record that ev is no longer setup */ michael@0: #define _event_debug_note_teardown(ev) do { \ michael@0: if (_event_debug_mode_on) { \ michael@0: struct event_debug_entry *dent,find; \ michael@0: find.ptr = (ev); \ michael@0: EVLOCK_LOCK(_event_debug_map_lock, 0); \ michael@0: dent = HT_REMOVE(event_debug_map, &global_debug_map, &find); \ michael@0: if (dent) \ michael@0: mm_free(dent); \ michael@0: EVLOCK_UNLOCK(_event_debug_map_lock, 0); \ michael@0: } \ michael@0: event_debug_mode_too_late = 1; \ michael@0: } while (0) michael@0: /* Macro: record that ev is now added */ michael@0: #define _event_debug_note_add(ev) do { \ michael@0: if (_event_debug_mode_on) { \ michael@0: struct event_debug_entry *dent,find; \ michael@0: find.ptr = (ev); \ michael@0: EVLOCK_LOCK(_event_debug_map_lock, 0); \ michael@0: dent = HT_FIND(event_debug_map, &global_debug_map, &find); \ michael@0: if (dent) { \ michael@0: dent->added = 1; \ michael@0: } else { \ michael@0: event_errx(_EVENT_ERR_ABORT, \ michael@0: "%s: noting an add on a non-setup event %p" \ michael@0: " (events: 0x%x, fd: "EV_SOCK_FMT \ michael@0: ", flags: 0x%x)", \ michael@0: __func__, (ev), (ev)->ev_events, \ michael@0: EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \ michael@0: } \ michael@0: EVLOCK_UNLOCK(_event_debug_map_lock, 0); \ michael@0: } \ michael@0: event_debug_mode_too_late = 1; \ michael@0: } while (0) michael@0: /* Macro: record that ev is no longer added */ michael@0: #define _event_debug_note_del(ev) do { \ michael@0: if (_event_debug_mode_on) { \ michael@0: struct event_debug_entry *dent,find; \ michael@0: find.ptr = (ev); \ michael@0: EVLOCK_LOCK(_event_debug_map_lock, 0); \ michael@0: dent = HT_FIND(event_debug_map, &global_debug_map, &find); \ michael@0: if (dent) { \ michael@0: dent->added = 0; \ michael@0: } else { \ michael@0: event_errx(_EVENT_ERR_ABORT, \ michael@0: "%s: noting a del on a non-setup event %p" \ michael@0: " (events: 0x%x, fd: "EV_SOCK_FMT \ michael@0: ", flags: 0x%x)", \ michael@0: __func__, (ev), (ev)->ev_events, \ michael@0: EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \ michael@0: } \ michael@0: EVLOCK_UNLOCK(_event_debug_map_lock, 0); \ michael@0: } \ michael@0: event_debug_mode_too_late = 1; \ michael@0: } while (0) michael@0: /* Macro: assert that ev is setup (i.e., okay to add or inspect) */ michael@0: #define _event_debug_assert_is_setup(ev) do { \ michael@0: if (_event_debug_mode_on) { \ michael@0: struct event_debug_entry *dent,find; \ michael@0: find.ptr = (ev); \ michael@0: EVLOCK_LOCK(_event_debug_map_lock, 0); \ michael@0: dent = HT_FIND(event_debug_map, &global_debug_map, &find); \ michael@0: if (!dent) { \ michael@0: event_errx(_EVENT_ERR_ABORT, \ michael@0: "%s called on a non-initialized event %p" \ michael@0: " (events: 0x%x, fd: "EV_SOCK_FMT\ michael@0: ", flags: 0x%x)", \ michael@0: __func__, (ev), (ev)->ev_events, \ michael@0: EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \ michael@0: } \ michael@0: EVLOCK_UNLOCK(_event_debug_map_lock, 0); \ michael@0: } \ michael@0: } while (0) michael@0: /* Macro: assert that ev is not added (i.e., okay to tear down or set michael@0: * up again) */ michael@0: #define _event_debug_assert_not_added(ev) do { \ michael@0: if (_event_debug_mode_on) { \ michael@0: struct event_debug_entry *dent,find; \ michael@0: find.ptr = (ev); \ michael@0: EVLOCK_LOCK(_event_debug_map_lock, 0); \ michael@0: dent = HT_FIND(event_debug_map, &global_debug_map, &find); \ michael@0: if (dent && dent->added) { \ michael@0: event_errx(_EVENT_ERR_ABORT, \ michael@0: "%s called on an already added event %p" \ michael@0: " (events: 0x%x, fd: "EV_SOCK_FMT", " \ michael@0: "flags: 0x%x)", \ michael@0: __func__, (ev), (ev)->ev_events, \ michael@0: EV_SOCK_ARG((ev)->ev_fd), (ev)->ev_flags); \ michael@0: } \ michael@0: EVLOCK_UNLOCK(_event_debug_map_lock, 0); \ michael@0: } \ michael@0: } while (0) michael@0: #else michael@0: #define _event_debug_note_setup(ev) \ michael@0: ((void)0) michael@0: #define _event_debug_note_teardown(ev) \ michael@0: ((void)0) michael@0: #define _event_debug_note_add(ev) \ michael@0: ((void)0) michael@0: #define _event_debug_note_del(ev) \ michael@0: ((void)0) michael@0: #define _event_debug_assert_is_setup(ev) \ michael@0: ((void)0) michael@0: #define _event_debug_assert_not_added(ev) \ michael@0: ((void)0) michael@0: #endif michael@0: michael@0: #define EVENT_BASE_ASSERT_LOCKED(base) \ michael@0: EVLOCK_ASSERT_LOCKED((base)->th_base_lock) michael@0: michael@0: /* The first time this function is called, it sets use_monotonic to 1 michael@0: * if we have a clock function that supports monotonic time */ michael@0: static void michael@0: detect_monotonic(void) michael@0: { michael@0: #if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) michael@0: struct timespec ts; michael@0: static int use_monotonic_initialized = 0; michael@0: michael@0: if (use_monotonic_initialized) michael@0: return; michael@0: michael@0: if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) michael@0: use_monotonic = 1; michael@0: michael@0: use_monotonic_initialized = 1; michael@0: #endif michael@0: } michael@0: michael@0: /* How often (in seconds) do we check for changes in wall clock time relative michael@0: * to monotonic time? Set this to -1 for 'never.' */ michael@0: #define CLOCK_SYNC_INTERVAL -1 michael@0: michael@0: /** Set 'tp' to the current time according to 'base'. We must hold the lock michael@0: * on 'base'. If there is a cached time, return it. Otherwise, use michael@0: * clock_gettime or gettimeofday as appropriate to find out the right time. michael@0: * Return 0 on success, -1 on failure. michael@0: */ michael@0: static int michael@0: gettime(struct event_base *base, struct timeval *tp) michael@0: { michael@0: EVENT_BASE_ASSERT_LOCKED(base); michael@0: michael@0: if (base->tv_cache.tv_sec) { michael@0: *tp = base->tv_cache; michael@0: return (0); michael@0: } michael@0: michael@0: #if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) michael@0: if (use_monotonic) { michael@0: struct timespec ts; michael@0: michael@0: if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) michael@0: return (-1); michael@0: michael@0: tp->tv_sec = ts.tv_sec; michael@0: tp->tv_usec = ts.tv_nsec / 1000; michael@0: if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL michael@0: < ts.tv_sec) { michael@0: struct timeval tv; michael@0: evutil_gettimeofday(&tv,NULL); michael@0: evutil_timersub(&tv, tp, &base->tv_clock_diff); michael@0: base->last_updated_clock_diff = ts.tv_sec; michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: #endif michael@0: michael@0: return (evutil_gettimeofday(tp, NULL)); michael@0: } michael@0: michael@0: int michael@0: event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv) michael@0: { michael@0: int r; michael@0: if (!base) { michael@0: base = current_base; michael@0: if (!current_base) michael@0: return evutil_gettimeofday(tv, NULL); michael@0: } michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: if (base->tv_cache.tv_sec == 0) { michael@0: r = evutil_gettimeofday(tv, NULL); michael@0: } else { michael@0: #if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) michael@0: evutil_timeradd(&base->tv_cache, &base->tv_clock_diff, tv); michael@0: #else michael@0: *tv = base->tv_cache; michael@0: #endif michael@0: r = 0; michael@0: } michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: return r; michael@0: } michael@0: michael@0: /** Make 'base' have no current cached time. */ michael@0: static inline void michael@0: clear_time_cache(struct event_base *base) michael@0: { michael@0: base->tv_cache.tv_sec = 0; michael@0: } michael@0: michael@0: /** Replace the cached time in 'base' with the current time. */ michael@0: static inline void michael@0: update_time_cache(struct event_base *base) michael@0: { michael@0: base->tv_cache.tv_sec = 0; michael@0: if (!(base->flags & EVENT_BASE_FLAG_NO_CACHE_TIME)) michael@0: gettime(base, &base->tv_cache); michael@0: } michael@0: michael@0: struct event_base * michael@0: event_init(void) michael@0: { michael@0: struct event_base *base = event_base_new_with_config(NULL); michael@0: michael@0: if (base == NULL) { michael@0: event_errx(1, "%s: Unable to construct event_base", __func__); michael@0: return NULL; michael@0: } michael@0: michael@0: current_base = base; michael@0: michael@0: return (base); michael@0: } michael@0: michael@0: struct event_base * michael@0: event_base_new(void) michael@0: { michael@0: struct event_base *base = NULL; michael@0: struct event_config *cfg = event_config_new(); michael@0: if (cfg) { michael@0: base = event_base_new_with_config(cfg); michael@0: event_config_free(cfg); michael@0: } michael@0: return base; michael@0: } michael@0: michael@0: /** Return true iff 'method' is the name of a method that 'cfg' tells us to michael@0: * avoid. */ michael@0: static int michael@0: event_config_is_avoided_method(const struct event_config *cfg, michael@0: const char *method) michael@0: { michael@0: struct event_config_entry *entry; michael@0: michael@0: TAILQ_FOREACH(entry, &cfg->entries, next) { michael@0: if (entry->avoid_method != NULL && michael@0: strcmp(entry->avoid_method, method) == 0) michael@0: return (1); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: /** Return true iff 'method' is disabled according to the environment. */ michael@0: static int michael@0: event_is_method_disabled(const char *name) michael@0: { michael@0: char environment[64]; michael@0: int i; michael@0: michael@0: evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name); michael@0: for (i = 8; environment[i] != '\0'; ++i) michael@0: environment[i] = EVUTIL_TOUPPER(environment[i]); michael@0: /* Note that evutil_getenv() ignores the environment entirely if michael@0: * we're setuid */ michael@0: return (evutil_getenv(environment) != NULL); michael@0: } michael@0: michael@0: int michael@0: event_base_get_features(const struct event_base *base) michael@0: { michael@0: return base->evsel->features; michael@0: } michael@0: michael@0: void michael@0: event_deferred_cb_queue_init(struct deferred_cb_queue *cb) michael@0: { michael@0: memset(cb, 0, sizeof(struct deferred_cb_queue)); michael@0: TAILQ_INIT(&cb->deferred_cb_list); michael@0: } michael@0: michael@0: /** Helper for the deferred_cb queue: wake up the event base. */ michael@0: static void michael@0: notify_base_cbq_callback(struct deferred_cb_queue *cb, void *baseptr) michael@0: { michael@0: struct event_base *base = baseptr; michael@0: if (EVBASE_NEED_NOTIFY(base)) michael@0: evthread_notify_base(base); michael@0: } michael@0: michael@0: struct deferred_cb_queue * michael@0: event_base_get_deferred_cb_queue(struct event_base *base) michael@0: { michael@0: return base ? &base->defer_queue : NULL; michael@0: } michael@0: michael@0: void michael@0: event_enable_debug_mode(void) michael@0: { michael@0: #ifndef _EVENT_DISABLE_DEBUG_MODE michael@0: if (_event_debug_mode_on) michael@0: event_errx(1, "%s was called twice!", __func__); michael@0: if (event_debug_mode_too_late) michael@0: event_errx(1, "%s must be called *before* creating any events " michael@0: "or event_bases",__func__); michael@0: michael@0: _event_debug_mode_on = 1; michael@0: michael@0: HT_INIT(event_debug_map, &global_debug_map); michael@0: #endif michael@0: } michael@0: michael@0: #if 0 michael@0: void michael@0: event_disable_debug_mode(void) michael@0: { michael@0: struct event_debug_entry **ent, *victim; michael@0: michael@0: EVLOCK_LOCK(_event_debug_map_lock, 0); michael@0: for (ent = HT_START(event_debug_map, &global_debug_map); ent; ) { michael@0: victim = *ent; michael@0: ent = HT_NEXT_RMV(event_debug_map,&global_debug_map, ent); michael@0: mm_free(victim); michael@0: } michael@0: HT_CLEAR(event_debug_map, &global_debug_map); michael@0: EVLOCK_UNLOCK(_event_debug_map_lock , 0); michael@0: } michael@0: #endif michael@0: michael@0: struct event_base * michael@0: event_base_new_with_config(const struct event_config *cfg) michael@0: { michael@0: int i; michael@0: struct event_base *base; michael@0: int should_check_environment; michael@0: michael@0: #ifndef _EVENT_DISABLE_DEBUG_MODE michael@0: event_debug_mode_too_late = 1; michael@0: #endif michael@0: michael@0: if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) { michael@0: event_warn("%s: calloc", __func__); michael@0: return NULL; michael@0: } michael@0: detect_monotonic(); michael@0: gettime(base, &base->event_tv); michael@0: michael@0: min_heap_ctor(&base->timeheap); michael@0: TAILQ_INIT(&base->eventqueue); michael@0: base->sig.ev_signal_pair[0] = -1; michael@0: base->sig.ev_signal_pair[1] = -1; michael@0: base->th_notify_fd[0] = -1; michael@0: base->th_notify_fd[1] = -1; michael@0: michael@0: event_deferred_cb_queue_init(&base->defer_queue); michael@0: base->defer_queue.notify_fn = notify_base_cbq_callback; michael@0: base->defer_queue.notify_arg = base; michael@0: if (cfg) michael@0: base->flags = cfg->flags; michael@0: michael@0: evmap_io_initmap(&base->io); michael@0: evmap_signal_initmap(&base->sigmap); michael@0: event_changelist_init(&base->changelist); michael@0: michael@0: base->evbase = NULL; michael@0: michael@0: should_check_environment = michael@0: !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV)); michael@0: michael@0: for (i = 0; eventops[i] && !base->evbase; i++) { michael@0: if (cfg != NULL) { michael@0: /* determine if this backend should be avoided */ michael@0: if (event_config_is_avoided_method(cfg, michael@0: eventops[i]->name)) michael@0: continue; michael@0: if ((eventops[i]->features & cfg->require_features) michael@0: != cfg->require_features) michael@0: continue; michael@0: } michael@0: michael@0: /* also obey the environment variables */ michael@0: if (should_check_environment && michael@0: event_is_method_disabled(eventops[i]->name)) michael@0: continue; michael@0: michael@0: base->evsel = eventops[i]; michael@0: michael@0: base->evbase = base->evsel->init(base); michael@0: } michael@0: michael@0: if (base->evbase == NULL) { michael@0: event_warnx("%s: no event mechanism available", michael@0: __func__); michael@0: base->evsel = NULL; michael@0: event_base_free(base); michael@0: return NULL; michael@0: } michael@0: michael@0: if (evutil_getenv("EVENT_SHOW_METHOD")) michael@0: event_msgx("libevent using: %s", base->evsel->name); michael@0: michael@0: /* allocate a single active event queue */ michael@0: if (event_base_priority_init(base, 1) < 0) { michael@0: event_base_free(base); michael@0: return NULL; michael@0: } michael@0: michael@0: /* prepare for threading */ michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: if (EVTHREAD_LOCKING_ENABLED() && michael@0: (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) { michael@0: int r; michael@0: EVTHREAD_ALLOC_LOCK(base->th_base_lock, michael@0: EVTHREAD_LOCKTYPE_RECURSIVE); michael@0: base->defer_queue.lock = base->th_base_lock; michael@0: EVTHREAD_ALLOC_COND(base->current_event_cond); michael@0: r = evthread_make_base_notifiable(base); michael@0: if (r<0) { michael@0: event_warnx("%s: Unable to make base notifiable.", __func__); michael@0: event_base_free(base); michael@0: return NULL; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef WIN32 michael@0: if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP)) michael@0: event_base_start_iocp(base, cfg->n_cpus_hint); michael@0: #endif michael@0: michael@0: return (base); michael@0: } michael@0: michael@0: int michael@0: event_base_start_iocp(struct event_base *base, int n_cpus) michael@0: { michael@0: #ifdef WIN32 michael@0: if (base->iocp) michael@0: return 0; michael@0: base->iocp = event_iocp_port_launch(n_cpus); michael@0: if (!base->iocp) { michael@0: event_warnx("%s: Couldn't launch IOCP", __func__); michael@0: return -1; michael@0: } michael@0: return 0; michael@0: #else michael@0: return -1; michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: event_base_stop_iocp(struct event_base *base) michael@0: { michael@0: #ifdef WIN32 michael@0: int rv; michael@0: michael@0: if (!base->iocp) michael@0: return; michael@0: rv = event_iocp_shutdown(base->iocp, -1); michael@0: EVUTIL_ASSERT(rv >= 0); michael@0: base->iocp = NULL; michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: event_base_free(struct event_base *base) michael@0: { michael@0: int i, n_deleted=0; michael@0: struct event *ev; michael@0: /* XXXX grab the lock? If there is contention when one thread frees michael@0: * the base, then the contending thread will be very sad soon. */ michael@0: michael@0: /* event_base_free(NULL) is how to free the current_base if we michael@0: * made it with event_init and forgot to hold a reference to it. */ michael@0: if (base == NULL && current_base) michael@0: base = current_base; michael@0: /* If we're freeing current_base, there won't be a current_base. */ michael@0: if (base == current_base) michael@0: current_base = NULL; michael@0: /* Don't actually free NULL. */ michael@0: if (base == NULL) { michael@0: event_warnx("%s: no base to free", __func__); michael@0: return; michael@0: } michael@0: /* XXX(niels) - check for internal events first */ michael@0: michael@0: #ifdef WIN32 michael@0: event_base_stop_iocp(base); michael@0: #endif michael@0: michael@0: /* threading fds if we have them */ michael@0: if (base->th_notify_fd[0] != -1) { michael@0: event_del(&base->th_notify); michael@0: EVUTIL_CLOSESOCKET(base->th_notify_fd[0]); michael@0: if (base->th_notify_fd[1] != -1) michael@0: EVUTIL_CLOSESOCKET(base->th_notify_fd[1]); michael@0: base->th_notify_fd[0] = -1; michael@0: base->th_notify_fd[1] = -1; michael@0: event_debug_unassign(&base->th_notify); michael@0: } michael@0: michael@0: /* Delete all non-internal events. */ michael@0: for (ev = TAILQ_FIRST(&base->eventqueue); ev; ) { michael@0: struct event *next = TAILQ_NEXT(ev, ev_next); michael@0: if (!(ev->ev_flags & EVLIST_INTERNAL)) { michael@0: event_del(ev); michael@0: ++n_deleted; michael@0: } michael@0: ev = next; michael@0: } michael@0: while ((ev = min_heap_top(&base->timeheap)) != NULL) { michael@0: event_del(ev); michael@0: ++n_deleted; michael@0: } michael@0: for (i = 0; i < base->n_common_timeouts; ++i) { michael@0: struct common_timeout_list *ctl = michael@0: base->common_timeout_queues[i]; michael@0: event_del(&ctl->timeout_event); /* Internal; doesn't count */ michael@0: event_debug_unassign(&ctl->timeout_event); michael@0: for (ev = TAILQ_FIRST(&ctl->events); ev; ) { michael@0: struct event *next = TAILQ_NEXT(ev, michael@0: ev_timeout_pos.ev_next_with_common_timeout); michael@0: if (!(ev->ev_flags & EVLIST_INTERNAL)) { michael@0: event_del(ev); michael@0: ++n_deleted; michael@0: } michael@0: ev = next; michael@0: } michael@0: mm_free(ctl); michael@0: } michael@0: if (base->common_timeout_queues) michael@0: mm_free(base->common_timeout_queues); michael@0: michael@0: for (i = 0; i < base->nactivequeues; ++i) { michael@0: for (ev = TAILQ_FIRST(&base->activequeues[i]); ev; ) { michael@0: struct event *next = TAILQ_NEXT(ev, ev_active_next); michael@0: if (!(ev->ev_flags & EVLIST_INTERNAL)) { michael@0: event_del(ev); michael@0: ++n_deleted; michael@0: } michael@0: ev = next; michael@0: } michael@0: } michael@0: michael@0: if (n_deleted) michael@0: event_debug(("%s: %d events were still set in base", michael@0: __func__, n_deleted)); michael@0: michael@0: if (base->evsel != NULL && base->evsel->dealloc != NULL) michael@0: base->evsel->dealloc(base); michael@0: michael@0: for (i = 0; i < base->nactivequeues; ++i) michael@0: EVUTIL_ASSERT(TAILQ_EMPTY(&base->activequeues[i])); michael@0: michael@0: EVUTIL_ASSERT(min_heap_empty(&base->timeheap)); michael@0: min_heap_dtor(&base->timeheap); michael@0: michael@0: mm_free(base->activequeues); michael@0: michael@0: EVUTIL_ASSERT(TAILQ_EMPTY(&base->eventqueue)); michael@0: michael@0: evmap_io_clear(&base->io); michael@0: evmap_signal_clear(&base->sigmap); michael@0: event_changelist_freemem(&base->changelist); michael@0: michael@0: EVTHREAD_FREE_LOCK(base->th_base_lock, EVTHREAD_LOCKTYPE_RECURSIVE); michael@0: EVTHREAD_FREE_COND(base->current_event_cond); michael@0: michael@0: mm_free(base); michael@0: } michael@0: michael@0: /* reinitialize the event base after a fork */ michael@0: int michael@0: event_reinit(struct event_base *base) michael@0: { michael@0: const struct eventop *evsel; michael@0: int res = 0; michael@0: struct event *ev; michael@0: int was_notifiable = 0; michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: michael@0: evsel = base->evsel; michael@0: michael@0: #if 0 michael@0: /* Right now, reinit always takes effect, since even if the michael@0: backend doesn't require it, the signal socketpair code does. michael@0: michael@0: XXX michael@0: */ michael@0: /* check if this event mechanism requires reinit */ michael@0: if (!evsel->need_reinit) michael@0: goto done; michael@0: #endif michael@0: michael@0: /* prevent internal delete */ michael@0: if (base->sig.ev_signal_added) { michael@0: /* we cannot call event_del here because the base has michael@0: * not been reinitialized yet. */ michael@0: event_queue_remove(base, &base->sig.ev_signal, michael@0: EVLIST_INSERTED); michael@0: if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE) michael@0: event_queue_remove(base, &base->sig.ev_signal, michael@0: EVLIST_ACTIVE); michael@0: if (base->sig.ev_signal_pair[0] != -1) michael@0: EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]); michael@0: if (base->sig.ev_signal_pair[1] != -1) michael@0: EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]); michael@0: base->sig.ev_signal_added = 0; michael@0: } michael@0: if (base->th_notify_fd[0] != -1) { michael@0: /* we cannot call event_del here because the base has michael@0: * not been reinitialized yet. */ michael@0: was_notifiable = 1; michael@0: event_queue_remove(base, &base->th_notify, michael@0: EVLIST_INSERTED); michael@0: if (base->th_notify.ev_flags & EVLIST_ACTIVE) michael@0: event_queue_remove(base, &base->th_notify, michael@0: EVLIST_ACTIVE); michael@0: base->sig.ev_signal_added = 0; michael@0: EVUTIL_CLOSESOCKET(base->th_notify_fd[0]); michael@0: if (base->th_notify_fd[1] != -1) michael@0: EVUTIL_CLOSESOCKET(base->th_notify_fd[1]); michael@0: base->th_notify_fd[0] = -1; michael@0: base->th_notify_fd[1] = -1; michael@0: event_debug_unassign(&base->th_notify); michael@0: } michael@0: michael@0: if (base->evsel->dealloc != NULL) michael@0: base->evsel->dealloc(base); michael@0: base->evbase = evsel->init(base); michael@0: if (base->evbase == NULL) { michael@0: event_errx(1, "%s: could not reinitialize event mechanism", michael@0: __func__); michael@0: res = -1; michael@0: goto done; michael@0: } michael@0: michael@0: event_changelist_freemem(&base->changelist); /* XXX */ michael@0: evmap_io_clear(&base->io); michael@0: evmap_signal_clear(&base->sigmap); michael@0: michael@0: TAILQ_FOREACH(ev, &base->eventqueue, ev_next) { michael@0: if (ev->ev_events & (EV_READ|EV_WRITE)) { michael@0: if (ev == &base->sig.ev_signal) { michael@0: /* If we run into the ev_signal event, it's only michael@0: * in eventqueue because some signal event was michael@0: * added, which made evsig_add re-add ev_signal. michael@0: * So don't double-add it. */ michael@0: continue; michael@0: } michael@0: if (evmap_io_add(base, ev->ev_fd, ev) == -1) michael@0: res = -1; michael@0: } else if (ev->ev_events & EV_SIGNAL) { michael@0: if (evmap_signal_add(base, (int)ev->ev_fd, ev) == -1) michael@0: res = -1; michael@0: } michael@0: } michael@0: michael@0: if (was_notifiable && res == 0) michael@0: res = evthread_make_base_notifiable(base); michael@0: michael@0: done: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: return (res); michael@0: } michael@0: michael@0: const char ** michael@0: event_get_supported_methods(void) michael@0: { michael@0: static const char **methods = NULL; michael@0: const struct eventop **method; michael@0: const char **tmp; michael@0: int i = 0, k; michael@0: michael@0: /* count all methods */ michael@0: for (method = &eventops[0]; *method != NULL; ++method) { michael@0: ++i; michael@0: } michael@0: michael@0: /* allocate one more than we need for the NULL pointer */ michael@0: tmp = mm_calloc((i + 1), sizeof(char *)); michael@0: if (tmp == NULL) michael@0: return (NULL); michael@0: michael@0: /* populate the array with the supported methods */ michael@0: for (k = 0, i = 0; eventops[k] != NULL; ++k) { michael@0: tmp[i++] = eventops[k]->name; michael@0: } michael@0: tmp[i] = NULL; michael@0: michael@0: if (methods != NULL) michael@0: mm_free((char**)methods); michael@0: michael@0: methods = tmp; michael@0: michael@0: return (methods); michael@0: } michael@0: michael@0: struct event_config * michael@0: event_config_new(void) michael@0: { michael@0: struct event_config *cfg = mm_calloc(1, sizeof(*cfg)); michael@0: michael@0: if (cfg == NULL) michael@0: return (NULL); michael@0: michael@0: TAILQ_INIT(&cfg->entries); michael@0: michael@0: return (cfg); michael@0: } michael@0: michael@0: static void michael@0: event_config_entry_free(struct event_config_entry *entry) michael@0: { michael@0: if (entry->avoid_method != NULL) michael@0: mm_free((char *)entry->avoid_method); michael@0: mm_free(entry); michael@0: } michael@0: michael@0: void michael@0: event_config_free(struct event_config *cfg) michael@0: { michael@0: struct event_config_entry *entry; michael@0: michael@0: while ((entry = TAILQ_FIRST(&cfg->entries)) != NULL) { michael@0: TAILQ_REMOVE(&cfg->entries, entry, next); michael@0: event_config_entry_free(entry); michael@0: } michael@0: mm_free(cfg); michael@0: } michael@0: michael@0: int michael@0: event_config_set_flag(struct event_config *cfg, int flag) michael@0: { michael@0: if (!cfg) michael@0: return -1; michael@0: cfg->flags |= flag; michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: event_config_avoid_method(struct event_config *cfg, const char *method) michael@0: { michael@0: struct event_config_entry *entry = mm_malloc(sizeof(*entry)); michael@0: if (entry == NULL) michael@0: return (-1); michael@0: michael@0: if ((entry->avoid_method = mm_strdup(method)) == NULL) { michael@0: mm_free(entry); michael@0: return (-1); michael@0: } michael@0: michael@0: TAILQ_INSERT_TAIL(&cfg->entries, entry, next); michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: event_config_require_features(struct event_config *cfg, michael@0: int features) michael@0: { michael@0: if (!cfg) michael@0: return (-1); michael@0: cfg->require_features = features; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: event_config_set_num_cpus_hint(struct event_config *cfg, int cpus) michael@0: { michael@0: if (!cfg) michael@0: return (-1); michael@0: cfg->n_cpus_hint = cpus; michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: event_priority_init(int npriorities) michael@0: { michael@0: return event_base_priority_init(current_base, npriorities); michael@0: } michael@0: michael@0: int michael@0: event_base_priority_init(struct event_base *base, int npriorities) michael@0: { michael@0: int i; michael@0: michael@0: if (N_ACTIVE_CALLBACKS(base) || npriorities < 1 michael@0: || npriorities >= EVENT_MAX_PRIORITIES) michael@0: return (-1); michael@0: michael@0: if (npriorities == base->nactivequeues) michael@0: return (0); michael@0: michael@0: if (base->nactivequeues) { michael@0: mm_free(base->activequeues); michael@0: base->nactivequeues = 0; michael@0: } michael@0: michael@0: /* Allocate our priority queues */ michael@0: base->activequeues = (struct event_list *) michael@0: mm_calloc(npriorities, sizeof(struct event_list)); michael@0: if (base->activequeues == NULL) { michael@0: event_warn("%s: calloc", __func__); michael@0: return (-1); michael@0: } michael@0: base->nactivequeues = npriorities; michael@0: michael@0: for (i = 0; i < base->nactivequeues; ++i) { michael@0: TAILQ_INIT(&base->activequeues[i]); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: /* Returns true iff we're currently watching any events. */ michael@0: static int michael@0: event_haveevents(struct event_base *base) michael@0: { michael@0: /* Caller must hold th_base_lock */ michael@0: return (base->virtual_event_count > 0 || base->event_count > 0); michael@0: } michael@0: michael@0: /* "closure" function called when processing active signal events */ michael@0: static inline void michael@0: event_signal_closure(struct event_base *base, struct event *ev) michael@0: { michael@0: short ncalls; michael@0: int should_break; michael@0: michael@0: /* Allows deletes to work */ michael@0: ncalls = ev->ev_ncalls; michael@0: if (ncalls != 0) michael@0: ev->ev_pncalls = &ncalls; michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: while (ncalls) { michael@0: ncalls--; michael@0: ev->ev_ncalls = ncalls; michael@0: if (ncalls == 0) michael@0: ev->ev_pncalls = NULL; michael@0: (*ev->ev_callback)(ev->ev_fd, ev->ev_res, ev->ev_arg); michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: should_break = base->event_break; michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: michael@0: if (should_break) { michael@0: if (ncalls != 0) michael@0: ev->ev_pncalls = NULL; michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Common timeouts are special timeouts that are handled as queues rather than michael@0: * in the minheap. This is more efficient than the minheap if we happen to michael@0: * know that we're going to get several thousands of timeout events all with michael@0: * the same timeout value. michael@0: * michael@0: * Since all our timeout handling code assumes timevals can be copied, michael@0: * assigned, etc, we can't use "magic pointer" to encode these common michael@0: * timeouts. Searching through a list to see if every timeout is common could michael@0: * also get inefficient. Instead, we take advantage of the fact that tv_usec michael@0: * is 32 bits long, but only uses 20 of those bits (since it can never be over michael@0: * 999999.) We use the top bits to encode 4 bites of magic number, and 8 bits michael@0: * of index into the event_base's aray of common timeouts. michael@0: */ michael@0: michael@0: #define MICROSECONDS_MASK COMMON_TIMEOUT_MICROSECONDS_MASK michael@0: #define COMMON_TIMEOUT_IDX_MASK 0x0ff00000 michael@0: #define COMMON_TIMEOUT_IDX_SHIFT 20 michael@0: #define COMMON_TIMEOUT_MASK 0xf0000000 michael@0: #define COMMON_TIMEOUT_MAGIC 0x50000000 michael@0: michael@0: #define COMMON_TIMEOUT_IDX(tv) \ michael@0: (((tv)->tv_usec & COMMON_TIMEOUT_IDX_MASK)>>COMMON_TIMEOUT_IDX_SHIFT) michael@0: michael@0: /** Return true iff if 'tv' is a common timeout in 'base' */ michael@0: static inline int michael@0: is_common_timeout(const struct timeval *tv, michael@0: const struct event_base *base) michael@0: { michael@0: int idx; michael@0: if ((tv->tv_usec & COMMON_TIMEOUT_MASK) != COMMON_TIMEOUT_MAGIC) michael@0: return 0; michael@0: idx = COMMON_TIMEOUT_IDX(tv); michael@0: return idx < base->n_common_timeouts; michael@0: } michael@0: michael@0: /* True iff tv1 and tv2 have the same common-timeout index, or if neither michael@0: * one is a common timeout. */ michael@0: static inline int michael@0: is_same_common_timeout(const struct timeval *tv1, const struct timeval *tv2) michael@0: { michael@0: return (tv1->tv_usec & ~MICROSECONDS_MASK) == michael@0: (tv2->tv_usec & ~MICROSECONDS_MASK); michael@0: } michael@0: michael@0: /** Requires that 'tv' is a common timeout. Return the corresponding michael@0: * common_timeout_list. */ michael@0: static inline struct common_timeout_list * michael@0: get_common_timeout_list(struct event_base *base, const struct timeval *tv) michael@0: { michael@0: return base->common_timeout_queues[COMMON_TIMEOUT_IDX(tv)]; michael@0: } michael@0: michael@0: #if 0 michael@0: static inline int michael@0: common_timeout_ok(const struct timeval *tv, michael@0: struct event_base *base) michael@0: { michael@0: const struct timeval *expect = michael@0: &get_common_timeout_list(base, tv)->duration; michael@0: return tv->tv_sec == expect->tv_sec && michael@0: tv->tv_usec == expect->tv_usec; michael@0: } michael@0: #endif michael@0: michael@0: /* Add the timeout for the first event in given common timeout list to the michael@0: * event_base's minheap. */ michael@0: static void michael@0: common_timeout_schedule(struct common_timeout_list *ctl, michael@0: const struct timeval *now, struct event *head) michael@0: { michael@0: struct timeval timeout = head->ev_timeout; michael@0: timeout.tv_usec &= MICROSECONDS_MASK; michael@0: event_add_internal(&ctl->timeout_event, &timeout, 1); michael@0: } michael@0: michael@0: /* Callback: invoked when the timeout for a common timeout queue triggers. michael@0: * This means that (at least) the first event in that queue should be run, michael@0: * and the timeout should be rescheduled if there are more events. */ michael@0: static void michael@0: common_timeout_callback(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: struct timeval now; michael@0: struct common_timeout_list *ctl = arg; michael@0: struct event_base *base = ctl->base; michael@0: struct event *ev = NULL; michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: gettime(base, &now); michael@0: while (1) { michael@0: ev = TAILQ_FIRST(&ctl->events); michael@0: if (!ev || ev->ev_timeout.tv_sec > now.tv_sec || michael@0: (ev->ev_timeout.tv_sec == now.tv_sec && michael@0: (ev->ev_timeout.tv_usec&MICROSECONDS_MASK) > now.tv_usec)) michael@0: break; michael@0: event_del_internal(ev); michael@0: event_active_nolock(ev, EV_TIMEOUT, 1); michael@0: } michael@0: if (ev) michael@0: common_timeout_schedule(ctl, &now, ev); michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: } michael@0: michael@0: #define MAX_COMMON_TIMEOUTS 256 michael@0: michael@0: const struct timeval * michael@0: event_base_init_common_timeout(struct event_base *base, michael@0: const struct timeval *duration) michael@0: { michael@0: int i; michael@0: struct timeval tv; michael@0: const struct timeval *result=NULL; michael@0: struct common_timeout_list *new_ctl; michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: if (duration->tv_usec > 1000000) { michael@0: memcpy(&tv, duration, sizeof(struct timeval)); michael@0: if (is_common_timeout(duration, base)) michael@0: tv.tv_usec &= MICROSECONDS_MASK; michael@0: tv.tv_sec += tv.tv_usec / 1000000; michael@0: tv.tv_usec %= 1000000; michael@0: duration = &tv; michael@0: } michael@0: for (i = 0; i < base->n_common_timeouts; ++i) { michael@0: const struct common_timeout_list *ctl = michael@0: base->common_timeout_queues[i]; michael@0: if (duration->tv_sec == ctl->duration.tv_sec && michael@0: duration->tv_usec == michael@0: (ctl->duration.tv_usec & MICROSECONDS_MASK)) { michael@0: EVUTIL_ASSERT(is_common_timeout(&ctl->duration, base)); michael@0: result = &ctl->duration; michael@0: goto done; michael@0: } michael@0: } michael@0: if (base->n_common_timeouts == MAX_COMMON_TIMEOUTS) { michael@0: event_warnx("%s: Too many common timeouts already in use; " michael@0: "we only support %d per event_base", __func__, michael@0: MAX_COMMON_TIMEOUTS); michael@0: goto done; michael@0: } michael@0: if (base->n_common_timeouts_allocated == base->n_common_timeouts) { michael@0: int n = base->n_common_timeouts < 16 ? 16 : michael@0: base->n_common_timeouts*2; michael@0: struct common_timeout_list **newqueues = michael@0: mm_realloc(base->common_timeout_queues, michael@0: n*sizeof(struct common_timeout_queue *)); michael@0: if (!newqueues) { michael@0: event_warn("%s: realloc",__func__); michael@0: goto done; michael@0: } michael@0: base->n_common_timeouts_allocated = n; michael@0: base->common_timeout_queues = newqueues; michael@0: } michael@0: new_ctl = mm_calloc(1, sizeof(struct common_timeout_list)); michael@0: if (!new_ctl) { michael@0: event_warn("%s: calloc",__func__); michael@0: goto done; michael@0: } michael@0: TAILQ_INIT(&new_ctl->events); michael@0: new_ctl->duration.tv_sec = duration->tv_sec; michael@0: new_ctl->duration.tv_usec = michael@0: duration->tv_usec | COMMON_TIMEOUT_MAGIC | michael@0: (base->n_common_timeouts << COMMON_TIMEOUT_IDX_SHIFT); michael@0: evtimer_assign(&new_ctl->timeout_event, base, michael@0: common_timeout_callback, new_ctl); michael@0: new_ctl->timeout_event.ev_flags |= EVLIST_INTERNAL; michael@0: event_priority_set(&new_ctl->timeout_event, 0); michael@0: new_ctl->base = base; michael@0: base->common_timeout_queues[base->n_common_timeouts++] = new_ctl; michael@0: result = &new_ctl->duration; michael@0: michael@0: done: michael@0: if (result) michael@0: EVUTIL_ASSERT(is_common_timeout(result, base)); michael@0: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: return result; michael@0: } michael@0: michael@0: /* Closure function invoked when we're activating a persistent event. */ michael@0: static inline void michael@0: event_persist_closure(struct event_base *base, struct event *ev) michael@0: { michael@0: /* reschedule the persistent event if we have a timeout. */ michael@0: if (ev->ev_io_timeout.tv_sec || ev->ev_io_timeout.tv_usec) { michael@0: /* If there was a timeout, we want it to run at an interval of michael@0: * ev_io_timeout after the last time it was _scheduled_ for, michael@0: * not ev_io_timeout after _now_. If it fired for another michael@0: * reason, though, the timeout ought to start ticking _now_. */ michael@0: struct timeval run_at, relative_to, delay, now; michael@0: ev_uint32_t usec_mask = 0; michael@0: EVUTIL_ASSERT(is_same_common_timeout(&ev->ev_timeout, michael@0: &ev->ev_io_timeout)); michael@0: gettime(base, &now); michael@0: if (is_common_timeout(&ev->ev_timeout, base)) { michael@0: delay = ev->ev_io_timeout; michael@0: usec_mask = delay.tv_usec & ~MICROSECONDS_MASK; michael@0: delay.tv_usec &= MICROSECONDS_MASK; michael@0: if (ev->ev_res & EV_TIMEOUT) { michael@0: relative_to = ev->ev_timeout; michael@0: relative_to.tv_usec &= MICROSECONDS_MASK; michael@0: } else { michael@0: relative_to = now; michael@0: } michael@0: } else { michael@0: delay = ev->ev_io_timeout; michael@0: if (ev->ev_res & EV_TIMEOUT) { michael@0: relative_to = ev->ev_timeout; michael@0: } else { michael@0: relative_to = now; michael@0: } michael@0: } michael@0: evutil_timeradd(&relative_to, &delay, &run_at); michael@0: if (evutil_timercmp(&run_at, &now, <)) { michael@0: /* Looks like we missed at least one invocation due to michael@0: * a clock jump, not running the event loop for a michael@0: * while, really slow callbacks, or michael@0: * something. Reschedule relative to now. michael@0: */ michael@0: evutil_timeradd(&now, &delay, &run_at); michael@0: } michael@0: run_at.tv_usec |= usec_mask; michael@0: event_add_internal(ev, &run_at, 1); michael@0: } michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: (*ev->ev_callback)(ev->ev_fd, ev->ev_res, ev->ev_arg); michael@0: } michael@0: michael@0: /* michael@0: Helper for event_process_active to process all the events in a single queue, michael@0: releasing the lock as we go. This function requires that the lock be held michael@0: when it's invoked. Returns -1 if we get a signal or an event_break that michael@0: means we should stop processing any active events now. Otherwise returns michael@0: the number of non-internal events that we processed. michael@0: */ michael@0: static int michael@0: event_process_active_single_queue(struct event_base *base, michael@0: struct event_list *activeq) michael@0: { michael@0: struct event *ev; michael@0: int count = 0; michael@0: michael@0: EVUTIL_ASSERT(activeq != NULL); michael@0: michael@0: for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) { michael@0: if (ev->ev_events & EV_PERSIST) michael@0: event_queue_remove(base, ev, EVLIST_ACTIVE); michael@0: else michael@0: event_del_internal(ev); michael@0: if (!(ev->ev_flags & EVLIST_INTERNAL)) michael@0: ++count; michael@0: michael@0: event_debug(( michael@0: "event_process_active: event: %p, %s%scall %p", michael@0: ev, michael@0: ev->ev_res & EV_READ ? "EV_READ " : " ", michael@0: ev->ev_res & EV_WRITE ? "EV_WRITE " : " ", michael@0: ev->ev_callback)); michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: base->current_event = ev; michael@0: base->current_event_waiters = 0; michael@0: #endif michael@0: michael@0: switch (ev->ev_closure) { michael@0: case EV_CLOSURE_SIGNAL: michael@0: event_signal_closure(base, ev); michael@0: break; michael@0: case EV_CLOSURE_PERSIST: michael@0: event_persist_closure(base, ev); michael@0: break; michael@0: default: michael@0: case EV_CLOSURE_NONE: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: (*ev->ev_callback)( michael@0: ev->ev_fd, ev->ev_res, ev->ev_arg); michael@0: break; michael@0: } michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: base->current_event = NULL; michael@0: if (base->current_event_waiters) { michael@0: base->current_event_waiters = 0; michael@0: EVTHREAD_COND_BROADCAST(base->current_event_cond); michael@0: } michael@0: #endif michael@0: michael@0: if (base->event_break) michael@0: return -1; michael@0: if (base->event_continue) michael@0: break; michael@0: } michael@0: return count; michael@0: } michael@0: michael@0: /* michael@0: Process up to MAX_DEFERRED of the defered_cb entries in 'queue'. If michael@0: *breakptr becomes set to 1, stop. Requires that we start out holding michael@0: the lock on 'queue'; releases the lock around 'queue' for each deferred_cb michael@0: we process. michael@0: */ michael@0: static int michael@0: event_process_deferred_callbacks(struct deferred_cb_queue *queue, int *breakptr) michael@0: { michael@0: int count = 0; michael@0: struct deferred_cb *cb; michael@0: michael@0: #define MAX_DEFERRED 16 michael@0: while ((cb = TAILQ_FIRST(&queue->deferred_cb_list))) { michael@0: cb->queued = 0; michael@0: TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next); michael@0: --queue->active_count; michael@0: UNLOCK_DEFERRED_QUEUE(queue); michael@0: michael@0: cb->cb(cb, cb->arg); michael@0: michael@0: LOCK_DEFERRED_QUEUE(queue); michael@0: if (*breakptr) michael@0: return -1; michael@0: if (++count == MAX_DEFERRED) michael@0: break; michael@0: } michael@0: #undef MAX_DEFERRED michael@0: return count; michael@0: } michael@0: michael@0: /* michael@0: * Active events are stored in priority queues. Lower priorities are always michael@0: * process before higher priorities. Low priority events can starve high michael@0: * priority ones. michael@0: */ michael@0: michael@0: static int michael@0: event_process_active(struct event_base *base) michael@0: { michael@0: /* Caller must hold th_base_lock */ michael@0: struct event_list *activeq = NULL; michael@0: int i, c = 0; michael@0: michael@0: for (i = 0; i < base->nactivequeues; ++i) { michael@0: if (TAILQ_FIRST(&base->activequeues[i]) != NULL) { michael@0: base->event_running_priority = i; michael@0: activeq = &base->activequeues[i]; michael@0: c = event_process_active_single_queue(base, activeq); michael@0: if (c < 0) { michael@0: base->event_running_priority = -1; michael@0: return -1; michael@0: } else if (c > 0) michael@0: break; /* Processed a real event; do not michael@0: * consider lower-priority events */ michael@0: /* If we get here, all of the events we processed michael@0: * were internal. Continue. */ michael@0: } michael@0: } michael@0: michael@0: event_process_deferred_callbacks(&base->defer_queue,&base->event_break); michael@0: base->event_running_priority = -1; michael@0: return c; michael@0: } michael@0: michael@0: /* michael@0: * Wait continuously for events. We exit only if no events are left. michael@0: */ michael@0: michael@0: int michael@0: event_dispatch(void) michael@0: { michael@0: return (event_loop(0)); michael@0: } michael@0: michael@0: int michael@0: event_base_dispatch(struct event_base *event_base) michael@0: { michael@0: return (event_base_loop(event_base, 0)); michael@0: } michael@0: michael@0: const char * michael@0: event_base_get_method(const struct event_base *base) michael@0: { michael@0: EVUTIL_ASSERT(base); michael@0: return (base->evsel->name); michael@0: } michael@0: michael@0: /** Callback: used to implement event_base_loopexit by telling the event_base michael@0: * that it's time to exit its loop. */ michael@0: static void michael@0: event_loopexit_cb(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: struct event_base *base = arg; michael@0: base->event_gotterm = 1; michael@0: } michael@0: michael@0: int michael@0: event_loopexit(const struct timeval *tv) michael@0: { michael@0: return (event_once(-1, EV_TIMEOUT, event_loopexit_cb, michael@0: current_base, tv)); michael@0: } michael@0: michael@0: int michael@0: event_base_loopexit(struct event_base *event_base, const struct timeval *tv) michael@0: { michael@0: return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb, michael@0: event_base, tv)); michael@0: } michael@0: michael@0: int michael@0: event_loopbreak(void) michael@0: { michael@0: return (event_base_loopbreak(current_base)); michael@0: } michael@0: michael@0: int michael@0: event_base_loopbreak(struct event_base *event_base) michael@0: { michael@0: int r = 0; michael@0: if (event_base == NULL) michael@0: return (-1); michael@0: michael@0: EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); michael@0: event_base->event_break = 1; michael@0: michael@0: if (EVBASE_NEED_NOTIFY(event_base)) { michael@0: r = evthread_notify_base(event_base); michael@0: } else { michael@0: r = (0); michael@0: } michael@0: EVBASE_RELEASE_LOCK(event_base, th_base_lock); michael@0: return r; michael@0: } michael@0: michael@0: int michael@0: event_base_got_break(struct event_base *event_base) michael@0: { michael@0: int res; michael@0: EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); michael@0: res = event_base->event_break; michael@0: EVBASE_RELEASE_LOCK(event_base, th_base_lock); michael@0: return res; michael@0: } michael@0: michael@0: int michael@0: event_base_got_exit(struct event_base *event_base) michael@0: { michael@0: int res; michael@0: EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); michael@0: res = event_base->event_gotterm; michael@0: EVBASE_RELEASE_LOCK(event_base, th_base_lock); michael@0: return res; michael@0: } michael@0: michael@0: /* not thread safe */ michael@0: michael@0: int michael@0: event_loop(int flags) michael@0: { michael@0: return event_base_loop(current_base, flags); michael@0: } michael@0: michael@0: int michael@0: event_base_loop(struct event_base *base, int flags) michael@0: { michael@0: const struct eventop *evsel = base->evsel; michael@0: struct timeval tv; michael@0: struct timeval *tv_p; michael@0: int res, done, retval = 0; michael@0: michael@0: /* Grab the lock. We will release it inside evsel.dispatch, and again michael@0: * as we invoke user callbacks. */ michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: michael@0: if (base->running_loop) { michael@0: event_warnx("%s: reentrant invocation. Only one event_base_loop" michael@0: " can run on each event_base at once.", __func__); michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: return -1; michael@0: } michael@0: michael@0: base->running_loop = 1; michael@0: michael@0: clear_time_cache(base); michael@0: michael@0: if (base->sig.ev_signal_added && base->sig.ev_n_signals_added) michael@0: evsig_set_base(base); michael@0: michael@0: done = 0; michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: base->th_owner_id = EVTHREAD_GET_ID(); michael@0: #endif michael@0: michael@0: base->event_gotterm = base->event_break = 0; michael@0: michael@0: while (!done) { michael@0: base->event_continue = 0; michael@0: michael@0: /* Terminate the loop if we have been asked to */ michael@0: if (base->event_gotterm) { michael@0: break; michael@0: } michael@0: michael@0: if (base->event_break) { michael@0: break; michael@0: } michael@0: michael@0: timeout_correct(base, &tv); michael@0: michael@0: tv_p = &tv; michael@0: if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) { michael@0: timeout_next(base, &tv_p); michael@0: } else { michael@0: /* michael@0: * if we have active events, we just poll new events michael@0: * without waiting. michael@0: */ michael@0: evutil_timerclear(&tv); michael@0: } michael@0: michael@0: /* If we have no events, we just exit */ michael@0: if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) { michael@0: event_debug(("%s: no events registered.", __func__)); michael@0: retval = 1; michael@0: goto done; michael@0: } michael@0: michael@0: /* update last old time */ michael@0: gettime(base, &base->event_tv); michael@0: michael@0: clear_time_cache(base); michael@0: michael@0: res = evsel->dispatch(base, tv_p); michael@0: michael@0: if (res == -1) { michael@0: event_debug(("%s: dispatch returned unsuccessfully.", michael@0: __func__)); michael@0: retval = -1; michael@0: goto done; michael@0: } michael@0: michael@0: update_time_cache(base); michael@0: michael@0: timeout_process(base); michael@0: michael@0: if (N_ACTIVE_CALLBACKS(base)) { michael@0: int n = event_process_active(base); michael@0: if ((flags & EVLOOP_ONCE) michael@0: && N_ACTIVE_CALLBACKS(base) == 0 michael@0: && n != 0) michael@0: done = 1; michael@0: } else if (flags & EVLOOP_NONBLOCK) michael@0: done = 1; michael@0: } michael@0: event_debug(("%s: asked to terminate loop.", __func__)); michael@0: michael@0: done: michael@0: clear_time_cache(base); michael@0: base->running_loop = 0; michael@0: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: michael@0: return (retval); michael@0: } michael@0: michael@0: /* Sets up an event for processing once */ michael@0: struct event_once { michael@0: struct event ev; michael@0: michael@0: void (*cb)(evutil_socket_t, short, void *); michael@0: void *arg; michael@0: }; michael@0: michael@0: /* One-time callback to implement event_base_once: invokes the user callback, michael@0: * then deletes the allocated storage */ michael@0: static void michael@0: event_once_cb(evutil_socket_t fd, short events, void *arg) michael@0: { michael@0: struct event_once *eonce = arg; michael@0: michael@0: (*eonce->cb)(fd, events, eonce->arg); michael@0: event_debug_unassign(&eonce->ev); michael@0: mm_free(eonce); michael@0: } michael@0: michael@0: /* not threadsafe, event scheduled once. */ michael@0: int michael@0: event_once(evutil_socket_t fd, short events, michael@0: void (*callback)(evutil_socket_t, short, void *), michael@0: void *arg, const struct timeval *tv) michael@0: { michael@0: return event_base_once(current_base, fd, events, callback, arg, tv); michael@0: } michael@0: michael@0: /* Schedules an event once */ michael@0: int michael@0: event_base_once(struct event_base *base, evutil_socket_t fd, short events, michael@0: void (*callback)(evutil_socket_t, short, void *), michael@0: void *arg, const struct timeval *tv) michael@0: { michael@0: struct event_once *eonce; michael@0: struct timeval etv; michael@0: int res = 0; michael@0: michael@0: /* We cannot support signals that just fire once, or persistent michael@0: * events. */ michael@0: if (events & (EV_SIGNAL|EV_PERSIST)) michael@0: return (-1); michael@0: michael@0: if ((eonce = mm_calloc(1, sizeof(struct event_once))) == NULL) michael@0: return (-1); michael@0: michael@0: eonce->cb = callback; michael@0: eonce->arg = arg; michael@0: michael@0: if (events == EV_TIMEOUT) { michael@0: if (tv == NULL) { michael@0: evutil_timerclear(&etv); michael@0: tv = &etv; michael@0: } michael@0: michael@0: evtimer_assign(&eonce->ev, base, event_once_cb, eonce); michael@0: } else if (events & (EV_READ|EV_WRITE)) { michael@0: events &= EV_READ|EV_WRITE; michael@0: michael@0: event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce); michael@0: } else { michael@0: /* Bad event combination */ michael@0: mm_free(eonce); michael@0: return (-1); michael@0: } michael@0: michael@0: if (res == 0) michael@0: res = event_add(&eonce->ev, tv); michael@0: if (res != 0) { michael@0: mm_free(eonce); michael@0: return (res); michael@0: } michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg) michael@0: { michael@0: if (!base) michael@0: base = current_base; michael@0: michael@0: _event_debug_assert_not_added(ev); michael@0: michael@0: ev->ev_base = base; michael@0: michael@0: ev->ev_callback = callback; michael@0: ev->ev_arg = arg; michael@0: ev->ev_fd = fd; michael@0: ev->ev_events = events; michael@0: ev->ev_res = 0; michael@0: ev->ev_flags = EVLIST_INIT; michael@0: ev->ev_ncalls = 0; michael@0: ev->ev_pncalls = NULL; michael@0: michael@0: if (events & EV_SIGNAL) { michael@0: if ((events & (EV_READ|EV_WRITE)) != 0) { michael@0: event_warnx("%s: EV_SIGNAL is not compatible with " michael@0: "EV_READ or EV_WRITE", __func__); michael@0: return -1; michael@0: } michael@0: ev->ev_closure = EV_CLOSURE_SIGNAL; michael@0: } else { michael@0: if (events & EV_PERSIST) { michael@0: evutil_timerclear(&ev->ev_io_timeout); michael@0: ev->ev_closure = EV_CLOSURE_PERSIST; michael@0: } else { michael@0: ev->ev_closure = EV_CLOSURE_NONE; michael@0: } michael@0: } michael@0: michael@0: min_heap_elem_init(ev); michael@0: michael@0: if (base != NULL) { michael@0: /* by default, we put new events into the middle priority */ michael@0: ev->ev_pri = base->nactivequeues / 2; michael@0: } michael@0: michael@0: _event_debug_note_setup(ev); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: int michael@0: event_base_set(struct event_base *base, struct event *ev) michael@0: { michael@0: /* Only innocent events may be assigned to a different base */ michael@0: if (ev->ev_flags != EVLIST_INIT) michael@0: return (-1); michael@0: michael@0: _event_debug_assert_is_setup(ev); michael@0: michael@0: ev->ev_base = base; michael@0: ev->ev_pri = base->nactivequeues/2; michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: void michael@0: event_set(struct event *ev, evutil_socket_t fd, short events, michael@0: void (*callback)(evutil_socket_t, short, void *), void *arg) michael@0: { michael@0: int r; michael@0: r = event_assign(ev, current_base, fd, events, callback, arg); michael@0: EVUTIL_ASSERT(r == 0); michael@0: } michael@0: michael@0: struct event * michael@0: event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg) michael@0: { michael@0: struct event *ev; michael@0: ev = mm_malloc(sizeof(struct event)); michael@0: if (ev == NULL) michael@0: return (NULL); michael@0: if (event_assign(ev, base, fd, events, cb, arg) < 0) { michael@0: mm_free(ev); michael@0: return (NULL); michael@0: } michael@0: michael@0: return (ev); michael@0: } michael@0: michael@0: void michael@0: event_free(struct event *ev) michael@0: { michael@0: _event_debug_assert_is_setup(ev); michael@0: michael@0: /* make sure that this event won't be coming back to haunt us. */ michael@0: event_del(ev); michael@0: _event_debug_note_teardown(ev); michael@0: mm_free(ev); michael@0: michael@0: } michael@0: michael@0: void michael@0: event_debug_unassign(struct event *ev) michael@0: { michael@0: _event_debug_assert_not_added(ev); michael@0: _event_debug_note_teardown(ev); michael@0: michael@0: ev->ev_flags &= ~EVLIST_INIT; michael@0: } michael@0: michael@0: /* michael@0: * Set's the priority of an event - if an event is already scheduled michael@0: * changing the priority is going to fail. michael@0: */ michael@0: michael@0: int michael@0: event_priority_set(struct event *ev, int pri) michael@0: { michael@0: _event_debug_assert_is_setup(ev); michael@0: michael@0: if (ev->ev_flags & EVLIST_ACTIVE) michael@0: return (-1); michael@0: if (pri < 0 || pri >= ev->ev_base->nactivequeues) michael@0: return (-1); michael@0: michael@0: ev->ev_pri = pri; michael@0: michael@0: return (0); michael@0: } michael@0: michael@0: /* michael@0: * Checks if a specific event is pending or scheduled. michael@0: */ michael@0: michael@0: int michael@0: event_pending(const struct event *ev, short event, struct timeval *tv) michael@0: { michael@0: int flags = 0; michael@0: michael@0: if (EVUTIL_FAILURE_CHECK(ev->ev_base == NULL)) { michael@0: event_warnx("%s: event has no event_base set.", __func__); michael@0: return 0; michael@0: } michael@0: michael@0: EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); michael@0: _event_debug_assert_is_setup(ev); michael@0: michael@0: if (ev->ev_flags & EVLIST_INSERTED) michael@0: flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)); michael@0: if (ev->ev_flags & EVLIST_ACTIVE) michael@0: flags |= ev->ev_res; michael@0: if (ev->ev_flags & EVLIST_TIMEOUT) michael@0: flags |= EV_TIMEOUT; michael@0: michael@0: event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_SIGNAL); michael@0: michael@0: /* See if there is a timeout that we should report */ michael@0: if (tv != NULL && (flags & event & EV_TIMEOUT)) { michael@0: struct timeval tmp = ev->ev_timeout; michael@0: tmp.tv_usec &= MICROSECONDS_MASK; michael@0: #if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) michael@0: /* correctly remamp to real time */ michael@0: evutil_timeradd(&ev->ev_base->tv_clock_diff, &tmp, tv); michael@0: #else michael@0: *tv = tmp; michael@0: #endif michael@0: } michael@0: michael@0: EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); michael@0: michael@0: return (flags & event); michael@0: } michael@0: michael@0: int michael@0: event_initialized(const struct event *ev) michael@0: { michael@0: if (!(ev->ev_flags & EVLIST_INIT)) michael@0: return 0; michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: void michael@0: 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) michael@0: { michael@0: _event_debug_assert_is_setup(event); michael@0: michael@0: if (base_out) michael@0: *base_out = event->ev_base; michael@0: if (fd_out) michael@0: *fd_out = event->ev_fd; michael@0: if (events_out) michael@0: *events_out = event->ev_events; michael@0: if (callback_out) michael@0: *callback_out = event->ev_callback; michael@0: if (arg_out) michael@0: *arg_out = event->ev_arg; michael@0: } michael@0: michael@0: size_t michael@0: event_get_struct_event_size(void) michael@0: { michael@0: return sizeof(struct event); michael@0: } michael@0: michael@0: evutil_socket_t michael@0: event_get_fd(const struct event *ev) michael@0: { michael@0: _event_debug_assert_is_setup(ev); michael@0: return ev->ev_fd; michael@0: } michael@0: michael@0: struct event_base * michael@0: event_get_base(const struct event *ev) michael@0: { michael@0: _event_debug_assert_is_setup(ev); michael@0: return ev->ev_base; michael@0: } michael@0: michael@0: short michael@0: event_get_events(const struct event *ev) michael@0: { michael@0: _event_debug_assert_is_setup(ev); michael@0: return ev->ev_events; michael@0: } michael@0: michael@0: event_callback_fn michael@0: event_get_callback(const struct event *ev) michael@0: { michael@0: _event_debug_assert_is_setup(ev); michael@0: return ev->ev_callback; michael@0: } michael@0: michael@0: void * michael@0: event_get_callback_arg(const struct event *ev) michael@0: { michael@0: _event_debug_assert_is_setup(ev); michael@0: return ev->ev_arg; michael@0: } michael@0: michael@0: int michael@0: event_add(struct event *ev, const struct timeval *tv) michael@0: { michael@0: int res; michael@0: michael@0: if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { michael@0: event_warnx("%s: event has no event_base set.", __func__); michael@0: return -1; michael@0: } michael@0: michael@0: EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); michael@0: michael@0: res = event_add_internal(ev, tv, 0); michael@0: michael@0: EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); michael@0: michael@0: return (res); michael@0: } michael@0: michael@0: /* Helper callback: wake an event_base from another thread. This version michael@0: * works by writing a byte to one end of a socketpair, so that the event_base michael@0: * listening on the other end will wake up as the corresponding event michael@0: * triggers */ michael@0: static int michael@0: evthread_notify_base_default(struct event_base *base) michael@0: { michael@0: char buf[1]; michael@0: int r; michael@0: buf[0] = (char) 0; michael@0: #ifdef WIN32 michael@0: r = send(base->th_notify_fd[1], buf, 1, 0); michael@0: #else michael@0: r = write(base->th_notify_fd[1], buf, 1); michael@0: #endif michael@0: return (r < 0 && errno != EAGAIN) ? -1 : 0; michael@0: } michael@0: michael@0: #if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H) michael@0: /* Helper callback: wake an event_base from another thread. This version michael@0: * assumes that you have a working eventfd() implementation. */ michael@0: static int michael@0: evthread_notify_base_eventfd(struct event_base *base) michael@0: { michael@0: ev_uint64_t msg = 1; michael@0: int r; michael@0: do { michael@0: r = write(base->th_notify_fd[0], (void*) &msg, sizeof(msg)); michael@0: } while (r < 0 && errno == EAGAIN); michael@0: michael@0: return (r < 0) ? -1 : 0; michael@0: } michael@0: #endif michael@0: michael@0: /** Tell the thread currently running the event_loop for base (if any) that it michael@0: * needs to stop waiting in its dispatch function (if it is) and process all michael@0: * active events and deferred callbacks (if there are any). */ michael@0: static int michael@0: evthread_notify_base(struct event_base *base) michael@0: { michael@0: EVENT_BASE_ASSERT_LOCKED(base); michael@0: if (!base->th_notify_fn) michael@0: return -1; michael@0: if (base->is_notify_pending) michael@0: return 0; michael@0: base->is_notify_pending = 1; michael@0: return base->th_notify_fn(base); michael@0: } michael@0: michael@0: /* Implementation function to add an event. Works just like event_add, michael@0: * except: 1) it requires that we have the lock. 2) if tv_is_absolute is set, michael@0: * we treat tv as an absolute time, not as an interval to add to the current michael@0: * time */ michael@0: static inline int michael@0: event_add_internal(struct event *ev, const struct timeval *tv, michael@0: int tv_is_absolute) michael@0: { michael@0: struct event_base *base = ev->ev_base; michael@0: int res = 0; michael@0: int notify = 0; michael@0: michael@0: EVENT_BASE_ASSERT_LOCKED(base); michael@0: _event_debug_assert_is_setup(ev); michael@0: michael@0: event_debug(( michael@0: "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%scall %p", michael@0: ev, michael@0: EV_SOCK_ARG(ev->ev_fd), michael@0: ev->ev_events & EV_READ ? "EV_READ " : " ", michael@0: ev->ev_events & EV_WRITE ? "EV_WRITE " : " ", michael@0: tv ? "EV_TIMEOUT " : " ", michael@0: ev->ev_callback)); michael@0: michael@0: EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL)); michael@0: michael@0: /* michael@0: * prepare for timeout insertion further below, if we get a michael@0: * failure on any step, we should not change any state. michael@0: */ michael@0: if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) { michael@0: if (min_heap_reserve(&base->timeheap, michael@0: 1 + min_heap_size(&base->timeheap)) == -1) michael@0: return (-1); /* ENOMEM == errno */ michael@0: } michael@0: michael@0: /* If the main thread is currently executing a signal event's michael@0: * callback, and we are not the main thread, then we want to wait michael@0: * until the callback is done before we mess with the event, or else michael@0: * we can race on ev_ncalls and ev_pncalls below. */ michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: if (base->current_event == ev && (ev->ev_events & EV_SIGNAL) michael@0: && !EVBASE_IN_THREAD(base)) { michael@0: ++base->current_event_waiters; michael@0: EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); michael@0: } michael@0: #endif michael@0: michael@0: if ((ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)) && michael@0: !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) { michael@0: if (ev->ev_events & (EV_READ|EV_WRITE)) michael@0: res = evmap_io_add(base, ev->ev_fd, ev); michael@0: else if (ev->ev_events & EV_SIGNAL) michael@0: res = evmap_signal_add(base, (int)ev->ev_fd, ev); michael@0: if (res != -1) michael@0: event_queue_insert(base, ev, EVLIST_INSERTED); michael@0: if (res == 1) { michael@0: /* evmap says we need to notify the main thread. */ michael@0: notify = 1; michael@0: res = 0; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * we should change the timeout state only if the previous event michael@0: * addition succeeded. michael@0: */ michael@0: if (res != -1 && tv != NULL) { michael@0: struct timeval now; michael@0: int common_timeout; michael@0: michael@0: /* michael@0: * for persistent timeout events, we remember the michael@0: * timeout value and re-add the event. michael@0: * michael@0: * If tv_is_absolute, this was already set. michael@0: */ michael@0: if (ev->ev_closure == EV_CLOSURE_PERSIST && !tv_is_absolute) michael@0: ev->ev_io_timeout = *tv; michael@0: michael@0: /* michael@0: * we already reserved memory above for the case where we michael@0: * are not replacing an existing timeout. michael@0: */ michael@0: if (ev->ev_flags & EVLIST_TIMEOUT) { michael@0: /* XXX I believe this is needless. */ michael@0: if (min_heap_elt_is_top(ev)) michael@0: notify = 1; michael@0: event_queue_remove(base, ev, EVLIST_TIMEOUT); michael@0: } michael@0: michael@0: /* Check if it is active due to a timeout. Rescheduling michael@0: * this timeout before the callback can be executed michael@0: * removes it from the active list. */ michael@0: if ((ev->ev_flags & EVLIST_ACTIVE) && michael@0: (ev->ev_res & EV_TIMEOUT)) { michael@0: if (ev->ev_events & EV_SIGNAL) { michael@0: /* See if we are just active executing michael@0: * this event in a loop michael@0: */ michael@0: if (ev->ev_ncalls && ev->ev_pncalls) { michael@0: /* Abort loop */ michael@0: *ev->ev_pncalls = 0; michael@0: } michael@0: } michael@0: michael@0: event_queue_remove(base, ev, EVLIST_ACTIVE); michael@0: } michael@0: michael@0: gettime(base, &now); michael@0: michael@0: common_timeout = is_common_timeout(tv, base); michael@0: if (tv_is_absolute) { michael@0: ev->ev_timeout = *tv; michael@0: } else if (common_timeout) { michael@0: struct timeval tmp = *tv; michael@0: tmp.tv_usec &= MICROSECONDS_MASK; michael@0: evutil_timeradd(&now, &tmp, &ev->ev_timeout); michael@0: ev->ev_timeout.tv_usec |= michael@0: (tv->tv_usec & ~MICROSECONDS_MASK); michael@0: } else { michael@0: evutil_timeradd(&now, tv, &ev->ev_timeout); michael@0: } michael@0: michael@0: event_debug(( michael@0: "event_add: timeout in %d seconds, call %p", michael@0: (int)tv->tv_sec, ev->ev_callback)); michael@0: michael@0: event_queue_insert(base, ev, EVLIST_TIMEOUT); michael@0: if (common_timeout) { michael@0: struct common_timeout_list *ctl = michael@0: get_common_timeout_list(base, &ev->ev_timeout); michael@0: if (ev == TAILQ_FIRST(&ctl->events)) { michael@0: common_timeout_schedule(ctl, &now, ev); michael@0: } michael@0: } else { michael@0: /* See if the earliest timeout is now earlier than it michael@0: * was before: if so, we will need to tell the main michael@0: * thread to wake up earlier than it would michael@0: * otherwise. */ michael@0: if (min_heap_elt_is_top(ev)) michael@0: notify = 1; michael@0: } michael@0: } michael@0: michael@0: /* if we are not in the right thread, we need to wake up the loop */ michael@0: if (res != -1 && notify && EVBASE_NEED_NOTIFY(base)) michael@0: evthread_notify_base(base); michael@0: michael@0: _event_debug_note_add(ev); michael@0: michael@0: return (res); michael@0: } michael@0: michael@0: int michael@0: event_del(struct event *ev) michael@0: { michael@0: int res; michael@0: michael@0: if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { michael@0: event_warnx("%s: event has no event_base set.", __func__); michael@0: return -1; michael@0: } michael@0: michael@0: EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); michael@0: michael@0: res = event_del_internal(ev); michael@0: michael@0: EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); michael@0: michael@0: return (res); michael@0: } michael@0: michael@0: /* Helper for event_del: always called with th_base_lock held. */ michael@0: static inline int michael@0: event_del_internal(struct event *ev) michael@0: { michael@0: struct event_base *base; michael@0: int res = 0, notify = 0; michael@0: michael@0: event_debug(("event_del: %p (fd "EV_SOCK_FMT"), callback %p", michael@0: ev, EV_SOCK_ARG(ev->ev_fd), ev->ev_callback)); michael@0: michael@0: /* An event without a base has not been added */ michael@0: if (ev->ev_base == NULL) michael@0: return (-1); michael@0: michael@0: EVENT_BASE_ASSERT_LOCKED(ev->ev_base); michael@0: michael@0: /* If the main thread is currently executing this event's callback, michael@0: * and we are not the main thread, then we want to wait until the michael@0: * callback is done before we start removing the event. That way, michael@0: * when this function returns, it will be safe to free the michael@0: * user-supplied argument. */ michael@0: base = ev->ev_base; michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: if (base->current_event == ev && !EVBASE_IN_THREAD(base)) { michael@0: ++base->current_event_waiters; michael@0: EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); michael@0: } michael@0: #endif michael@0: michael@0: EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL)); michael@0: michael@0: /* See if we are just active executing this event in a loop */ michael@0: if (ev->ev_events & EV_SIGNAL) { michael@0: if (ev->ev_ncalls && ev->ev_pncalls) { michael@0: /* Abort loop */ michael@0: *ev->ev_pncalls = 0; michael@0: } michael@0: } michael@0: michael@0: if (ev->ev_flags & EVLIST_TIMEOUT) { michael@0: /* NOTE: We never need to notify the main thread because of a michael@0: * deleted timeout event: all that could happen if we don't is michael@0: * that the dispatch loop might wake up too early. But the michael@0: * point of notifying the main thread _is_ to wake up the michael@0: * dispatch loop early anyway, so we wouldn't gain anything by michael@0: * doing it. michael@0: */ michael@0: event_queue_remove(base, ev, EVLIST_TIMEOUT); michael@0: } michael@0: michael@0: if (ev->ev_flags & EVLIST_ACTIVE) michael@0: event_queue_remove(base, ev, EVLIST_ACTIVE); michael@0: michael@0: if (ev->ev_flags & EVLIST_INSERTED) { michael@0: event_queue_remove(base, ev, EVLIST_INSERTED); michael@0: if (ev->ev_events & (EV_READ|EV_WRITE)) michael@0: res = evmap_io_del(base, ev->ev_fd, ev); michael@0: else michael@0: res = evmap_signal_del(base, (int)ev->ev_fd, ev); michael@0: if (res == 1) { michael@0: /* evmap says we need to notify the main thread. */ michael@0: notify = 1; michael@0: res = 0; michael@0: } michael@0: } michael@0: michael@0: /* if we are not in the right thread, we need to wake up the loop */ michael@0: if (res != -1 && notify && EVBASE_NEED_NOTIFY(base)) michael@0: evthread_notify_base(base); michael@0: michael@0: _event_debug_note_del(ev); michael@0: michael@0: return (res); michael@0: } michael@0: michael@0: void michael@0: event_active(struct event *ev, int res, short ncalls) michael@0: { michael@0: if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { michael@0: event_warnx("%s: event has no event_base set.", __func__); michael@0: return; michael@0: } michael@0: michael@0: EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); michael@0: michael@0: _event_debug_assert_is_setup(ev); michael@0: michael@0: event_active_nolock(ev, res, ncalls); michael@0: michael@0: EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); michael@0: } michael@0: michael@0: michael@0: void michael@0: event_active_nolock(struct event *ev, int res, short ncalls) michael@0: { michael@0: struct event_base *base; michael@0: michael@0: event_debug(("event_active: %p (fd "EV_SOCK_FMT"), res %d, callback %p", michael@0: ev, EV_SOCK_ARG(ev->ev_fd), (int)res, ev->ev_callback)); michael@0: michael@0: michael@0: /* We get different kinds of events, add them together */ michael@0: if (ev->ev_flags & EVLIST_ACTIVE) { michael@0: ev->ev_res |= res; michael@0: return; michael@0: } michael@0: michael@0: base = ev->ev_base; michael@0: michael@0: EVENT_BASE_ASSERT_LOCKED(base); michael@0: michael@0: ev->ev_res = res; michael@0: michael@0: if (ev->ev_pri < base->event_running_priority) michael@0: base->event_continue = 1; michael@0: michael@0: if (ev->ev_events & EV_SIGNAL) { michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: if (base->current_event == ev && !EVBASE_IN_THREAD(base)) { michael@0: ++base->current_event_waiters; michael@0: EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); michael@0: } michael@0: #endif michael@0: ev->ev_ncalls = ncalls; michael@0: ev->ev_pncalls = NULL; michael@0: } michael@0: michael@0: event_queue_insert(base, ev, EVLIST_ACTIVE); michael@0: michael@0: if (EVBASE_NEED_NOTIFY(base)) michael@0: evthread_notify_base(base); michael@0: } michael@0: michael@0: void michael@0: event_deferred_cb_init(struct deferred_cb *cb, deferred_cb_fn fn, void *arg) michael@0: { michael@0: memset(cb, 0, sizeof(struct deferred_cb)); michael@0: cb->cb = fn; michael@0: cb->arg = arg; michael@0: } michael@0: michael@0: void michael@0: event_deferred_cb_cancel(struct deferred_cb_queue *queue, michael@0: struct deferred_cb *cb) michael@0: { michael@0: if (!queue) { michael@0: if (current_base) michael@0: queue = ¤t_base->defer_queue; michael@0: else michael@0: return; michael@0: } michael@0: michael@0: LOCK_DEFERRED_QUEUE(queue); michael@0: if (cb->queued) { michael@0: TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next); michael@0: --queue->active_count; michael@0: cb->queued = 0; michael@0: } michael@0: UNLOCK_DEFERRED_QUEUE(queue); michael@0: } michael@0: michael@0: void michael@0: event_deferred_cb_schedule(struct deferred_cb_queue *queue, michael@0: struct deferred_cb *cb) michael@0: { michael@0: if (!queue) { michael@0: if (current_base) michael@0: queue = ¤t_base->defer_queue; michael@0: else michael@0: return; michael@0: } michael@0: michael@0: LOCK_DEFERRED_QUEUE(queue); michael@0: if (!cb->queued) { michael@0: cb->queued = 1; michael@0: TAILQ_INSERT_TAIL(&queue->deferred_cb_list, cb, cb_next); michael@0: ++queue->active_count; michael@0: if (queue->notify_fn) michael@0: queue->notify_fn(queue, queue->notify_arg); michael@0: } michael@0: UNLOCK_DEFERRED_QUEUE(queue); michael@0: } michael@0: michael@0: static int michael@0: timeout_next(struct event_base *base, struct timeval **tv_p) michael@0: { michael@0: /* Caller must hold th_base_lock */ michael@0: struct timeval now; michael@0: struct event *ev; michael@0: struct timeval *tv = *tv_p; michael@0: int res = 0; michael@0: michael@0: ev = min_heap_top(&base->timeheap); michael@0: michael@0: if (ev == NULL) { michael@0: /* if no time-based events are active wait for I/O */ michael@0: *tv_p = NULL; michael@0: goto out; michael@0: } michael@0: michael@0: if (gettime(base, &now) == -1) { michael@0: res = -1; michael@0: goto out; michael@0: } michael@0: michael@0: if (evutil_timercmp(&ev->ev_timeout, &now, <=)) { michael@0: evutil_timerclear(tv); michael@0: goto out; michael@0: } michael@0: michael@0: evutil_timersub(&ev->ev_timeout, &now, tv); michael@0: michael@0: EVUTIL_ASSERT(tv->tv_sec >= 0); michael@0: EVUTIL_ASSERT(tv->tv_usec >= 0); michael@0: event_debug(("timeout_next: in %d seconds", (int)tv->tv_sec)); michael@0: michael@0: out: michael@0: return (res); michael@0: } michael@0: michael@0: /* michael@0: * Determines if the time is running backwards by comparing the current time michael@0: * against the last time we checked. Not needed when using clock monotonic. michael@0: * If time is running backwards, we adjust the firing time of every event by michael@0: * the amount that time seems to have jumped. michael@0: */ michael@0: static void michael@0: timeout_correct(struct event_base *base, struct timeval *tv) michael@0: { michael@0: /* Caller must hold th_base_lock. */ michael@0: struct event **pev; michael@0: unsigned int size; michael@0: struct timeval off; michael@0: int i; michael@0: michael@0: if (use_monotonic) michael@0: return; michael@0: michael@0: /* Check if time is running backwards */ michael@0: gettime(base, tv); michael@0: michael@0: if (evutil_timercmp(tv, &base->event_tv, >=)) { michael@0: base->event_tv = *tv; michael@0: return; michael@0: } michael@0: michael@0: event_debug(("%s: time is running backwards, corrected", michael@0: __func__)); michael@0: evutil_timersub(&base->event_tv, tv, &off); michael@0: michael@0: /* michael@0: * We can modify the key element of the node without destroying michael@0: * the minheap property, because we change every element. michael@0: */ michael@0: pev = base->timeheap.p; michael@0: size = base->timeheap.n; michael@0: for (; size-- > 0; ++pev) { michael@0: struct timeval *ev_tv = &(**pev).ev_timeout; michael@0: evutil_timersub(ev_tv, &off, ev_tv); michael@0: } michael@0: for (i=0; in_common_timeouts; ++i) { michael@0: struct event *ev; michael@0: struct common_timeout_list *ctl = michael@0: base->common_timeout_queues[i]; michael@0: TAILQ_FOREACH(ev, &ctl->events, michael@0: ev_timeout_pos.ev_next_with_common_timeout) { michael@0: struct timeval *ev_tv = &ev->ev_timeout; michael@0: ev_tv->tv_usec &= MICROSECONDS_MASK; michael@0: evutil_timersub(ev_tv, &off, ev_tv); michael@0: ev_tv->tv_usec |= COMMON_TIMEOUT_MAGIC | michael@0: (i<event_tv = *tv; michael@0: } michael@0: michael@0: /* Activate every event whose timeout has elapsed. */ michael@0: static void michael@0: timeout_process(struct event_base *base) michael@0: { michael@0: /* Caller must hold lock. */ michael@0: struct timeval now; michael@0: struct event *ev; michael@0: michael@0: if (min_heap_empty(&base->timeheap)) { michael@0: return; michael@0: } michael@0: michael@0: gettime(base, &now); michael@0: michael@0: while ((ev = min_heap_top(&base->timeheap))) { michael@0: if (evutil_timercmp(&ev->ev_timeout, &now, >)) michael@0: break; michael@0: michael@0: /* delete this event from the I/O queues */ michael@0: event_del_internal(ev); michael@0: michael@0: event_debug(("timeout_process: call %p", michael@0: ev->ev_callback)); michael@0: event_active_nolock(ev, EV_TIMEOUT, 1); michael@0: } michael@0: } michael@0: michael@0: /* Remove 'ev' from 'queue' (EVLIST_...) in base. */ michael@0: static void michael@0: event_queue_remove(struct event_base *base, struct event *ev, int queue) michael@0: { michael@0: EVENT_BASE_ASSERT_LOCKED(base); michael@0: michael@0: if (!(ev->ev_flags & queue)) { michael@0: event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__, michael@0: ev, EV_SOCK_ARG(ev->ev_fd), queue); michael@0: return; michael@0: } michael@0: michael@0: if (~ev->ev_flags & EVLIST_INTERNAL) michael@0: base->event_count--; michael@0: michael@0: ev->ev_flags &= ~queue; michael@0: switch (queue) { michael@0: case EVLIST_INSERTED: michael@0: TAILQ_REMOVE(&base->eventqueue, ev, ev_next); michael@0: break; michael@0: case EVLIST_ACTIVE: michael@0: base->event_count_active--; michael@0: TAILQ_REMOVE(&base->activequeues[ev->ev_pri], michael@0: ev, ev_active_next); michael@0: break; michael@0: case EVLIST_TIMEOUT: michael@0: if (is_common_timeout(&ev->ev_timeout, base)) { michael@0: struct common_timeout_list *ctl = michael@0: get_common_timeout_list(base, &ev->ev_timeout); michael@0: TAILQ_REMOVE(&ctl->events, ev, michael@0: ev_timeout_pos.ev_next_with_common_timeout); michael@0: } else { michael@0: min_heap_erase(&base->timeheap, ev); michael@0: } michael@0: break; michael@0: default: michael@0: event_errx(1, "%s: unknown queue %x", __func__, queue); michael@0: } michael@0: } michael@0: michael@0: /* Add 'ev' to the common timeout list in 'ev'. */ michael@0: static void michael@0: insert_common_timeout_inorder(struct common_timeout_list *ctl, michael@0: struct event *ev) michael@0: { michael@0: struct event *e; michael@0: /* By all logic, we should just be able to append 'ev' to the end of michael@0: * ctl->events, since the timeout on each 'ev' is set to {the common michael@0: * timeout} + {the time when we add the event}, and so the events michael@0: * should arrive in order of their timeeouts. But just in case michael@0: * there's some wacky threading issue going on, we do a search from michael@0: * the end of 'ev' to find the right insertion point. michael@0: */ michael@0: TAILQ_FOREACH_REVERSE(e, &ctl->events, michael@0: event_list, ev_timeout_pos.ev_next_with_common_timeout) { michael@0: /* This timercmp is a little sneaky, since both ev and e have michael@0: * magic values in tv_usec. Fortunately, they ought to have michael@0: * the _same_ magic values in tv_usec. Let's assert for that. michael@0: */ michael@0: EVUTIL_ASSERT( michael@0: is_same_common_timeout(&e->ev_timeout, &ev->ev_timeout)); michael@0: if (evutil_timercmp(&ev->ev_timeout, &e->ev_timeout, >=)) { michael@0: TAILQ_INSERT_AFTER(&ctl->events, e, ev, michael@0: ev_timeout_pos.ev_next_with_common_timeout); michael@0: return; michael@0: } michael@0: } michael@0: TAILQ_INSERT_HEAD(&ctl->events, ev, michael@0: ev_timeout_pos.ev_next_with_common_timeout); michael@0: } michael@0: michael@0: static void michael@0: event_queue_insert(struct event_base *base, struct event *ev, int queue) michael@0: { michael@0: EVENT_BASE_ASSERT_LOCKED(base); michael@0: michael@0: if (ev->ev_flags & queue) { michael@0: /* Double insertion is possible for active events */ michael@0: if (queue & EVLIST_ACTIVE) michael@0: return; michael@0: michael@0: event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on queue %x", __func__, michael@0: ev, EV_SOCK_ARG(ev->ev_fd), queue); michael@0: return; michael@0: } michael@0: michael@0: if (~ev->ev_flags & EVLIST_INTERNAL) michael@0: base->event_count++; michael@0: michael@0: ev->ev_flags |= queue; michael@0: switch (queue) { michael@0: case EVLIST_INSERTED: michael@0: TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next); michael@0: break; michael@0: case EVLIST_ACTIVE: michael@0: base->event_count_active++; michael@0: TAILQ_INSERT_TAIL(&base->activequeues[ev->ev_pri], michael@0: ev,ev_active_next); michael@0: break; michael@0: case EVLIST_TIMEOUT: { michael@0: if (is_common_timeout(&ev->ev_timeout, base)) { michael@0: struct common_timeout_list *ctl = michael@0: get_common_timeout_list(base, &ev->ev_timeout); michael@0: insert_common_timeout_inorder(ctl, ev); michael@0: } else michael@0: min_heap_push(&base->timeheap, ev); michael@0: break; michael@0: } michael@0: default: michael@0: event_errx(1, "%s: unknown queue %x", __func__, queue); michael@0: } michael@0: } michael@0: michael@0: /* Functions for debugging */ michael@0: michael@0: const char * michael@0: event_get_version(void) michael@0: { michael@0: return (_EVENT_VERSION); michael@0: } michael@0: michael@0: ev_uint32_t michael@0: event_get_version_number(void) michael@0: { michael@0: return (_EVENT_NUMERIC_VERSION); michael@0: } michael@0: michael@0: /* michael@0: * No thread-safe interface needed - the information should be the same michael@0: * for all threads. michael@0: */ michael@0: michael@0: const char * michael@0: event_get_method(void) michael@0: { michael@0: return (current_base->evsel->name); michael@0: } michael@0: michael@0: #ifndef _EVENT_DISABLE_MM_REPLACEMENT michael@0: static void *(*_mm_malloc_fn)(size_t sz) = NULL; michael@0: static void *(*_mm_realloc_fn)(void *p, size_t sz) = NULL; michael@0: static void (*_mm_free_fn)(void *p) = NULL; michael@0: michael@0: void * michael@0: event_mm_malloc_(size_t sz) michael@0: { michael@0: if (_mm_malloc_fn) michael@0: return _mm_malloc_fn(sz); michael@0: else michael@0: return malloc(sz); michael@0: } michael@0: michael@0: void * michael@0: event_mm_calloc_(size_t count, size_t size) michael@0: { michael@0: if (_mm_malloc_fn) { michael@0: size_t sz = count * size; michael@0: void *p = _mm_malloc_fn(sz); michael@0: if (p) michael@0: memset(p, 0, sz); michael@0: return p; michael@0: } else michael@0: return calloc(count, size); michael@0: } michael@0: michael@0: char * michael@0: event_mm_strdup_(const char *str) michael@0: { michael@0: if (_mm_malloc_fn) { michael@0: size_t ln = strlen(str); michael@0: void *p = _mm_malloc_fn(ln+1); michael@0: if (p) michael@0: memcpy(p, str, ln+1); michael@0: return p; michael@0: } else michael@0: #ifdef WIN32 michael@0: return _strdup(str); michael@0: #else michael@0: return strdup(str); michael@0: #endif michael@0: } michael@0: michael@0: void * michael@0: event_mm_realloc_(void *ptr, size_t sz) michael@0: { michael@0: if (_mm_realloc_fn) michael@0: return _mm_realloc_fn(ptr, sz); michael@0: else michael@0: return realloc(ptr, sz); michael@0: } michael@0: michael@0: void michael@0: event_mm_free_(void *ptr) michael@0: { michael@0: if (_mm_free_fn) michael@0: _mm_free_fn(ptr); michael@0: else michael@0: free(ptr); michael@0: } michael@0: michael@0: void michael@0: event_set_mem_functions(void *(*malloc_fn)(size_t sz), michael@0: void *(*realloc_fn)(void *ptr, size_t sz), michael@0: void (*free_fn)(void *ptr)) michael@0: { michael@0: _mm_malloc_fn = malloc_fn; michael@0: _mm_realloc_fn = realloc_fn; michael@0: _mm_free_fn = free_fn; michael@0: } michael@0: #endif michael@0: michael@0: #if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H) michael@0: static void michael@0: evthread_notify_drain_eventfd(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: ev_uint64_t msg; michael@0: ev_ssize_t r; michael@0: struct event_base *base = arg; michael@0: michael@0: r = read(fd, (void*) &msg, sizeof(msg)); michael@0: if (r<0 && errno != EAGAIN) { michael@0: event_sock_warn(fd, "Error reading from eventfd"); michael@0: } michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: base->is_notify_pending = 0; michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: } michael@0: #endif michael@0: michael@0: static void michael@0: evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: unsigned char buf[1024]; michael@0: struct event_base *base = arg; michael@0: #ifdef WIN32 michael@0: while (recv(fd, (char*)buf, sizeof(buf), 0) > 0) michael@0: ; michael@0: #else michael@0: while (read(fd, (char*)buf, sizeof(buf)) > 0) michael@0: ; michael@0: #endif michael@0: michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: base->is_notify_pending = 0; michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: } michael@0: michael@0: int michael@0: evthread_make_base_notifiable(struct event_base *base) michael@0: { michael@0: void (*cb)(evutil_socket_t, short, void *) = evthread_notify_drain_default; michael@0: int (*notify)(struct event_base *) = evthread_notify_base_default; michael@0: michael@0: /* XXXX grab the lock here? */ michael@0: if (!base) michael@0: return -1; michael@0: michael@0: if (base->th_notify_fd[0] >= 0) michael@0: return 0; michael@0: michael@0: #if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H) michael@0: #ifndef EFD_CLOEXEC michael@0: #define EFD_CLOEXEC 0 michael@0: #endif michael@0: base->th_notify_fd[0] = eventfd(0, EFD_CLOEXEC); michael@0: if (base->th_notify_fd[0] >= 0) { michael@0: evutil_make_socket_closeonexec(base->th_notify_fd[0]); michael@0: notify = evthread_notify_base_eventfd; michael@0: cb = evthread_notify_drain_eventfd; michael@0: } michael@0: #endif michael@0: #if defined(_EVENT_HAVE_PIPE) michael@0: if (base->th_notify_fd[0] < 0) { michael@0: if ((base->evsel->features & EV_FEATURE_FDS)) { michael@0: if (pipe(base->th_notify_fd) < 0) { michael@0: event_warn("%s: pipe", __func__); michael@0: } else { michael@0: evutil_make_socket_closeonexec(base->th_notify_fd[0]); michael@0: evutil_make_socket_closeonexec(base->th_notify_fd[1]); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef WIN32 michael@0: #define LOCAL_SOCKETPAIR_AF AF_INET michael@0: #else michael@0: #define LOCAL_SOCKETPAIR_AF AF_UNIX michael@0: #endif michael@0: if (base->th_notify_fd[0] < 0) { michael@0: if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0, michael@0: base->th_notify_fd) == -1) { michael@0: event_sock_warn(-1, "%s: socketpair", __func__); michael@0: return (-1); michael@0: } else { michael@0: evutil_make_socket_closeonexec(base->th_notify_fd[0]); michael@0: evutil_make_socket_closeonexec(base->th_notify_fd[1]); michael@0: } michael@0: } michael@0: michael@0: evutil_make_socket_nonblocking(base->th_notify_fd[0]); michael@0: michael@0: base->th_notify_fn = notify; michael@0: michael@0: /* michael@0: Making the second socket nonblocking is a bit subtle, given that we michael@0: ignore any EAGAIN returns when writing to it, and you don't usally michael@0: do that for a nonblocking socket. But if the kernel gives us EAGAIN, michael@0: then there's no need to add any more data to the buffer, since michael@0: the main thread is already either about to wake up and drain it, michael@0: or woken up and in the process of draining it. michael@0: */ michael@0: if (base->th_notify_fd[1] > 0) michael@0: evutil_make_socket_nonblocking(base->th_notify_fd[1]); michael@0: michael@0: /* prepare an event that we can use for wakeup */ michael@0: event_assign(&base->th_notify, base, base->th_notify_fd[0], michael@0: EV_READ|EV_PERSIST, cb, base); michael@0: michael@0: /* we need to mark this as internal event */ michael@0: base->th_notify.ev_flags |= EVLIST_INTERNAL; michael@0: event_priority_set(&base->th_notify, 0); michael@0: michael@0: return event_add(&base->th_notify, NULL); michael@0: } michael@0: michael@0: void michael@0: event_base_dump_events(struct event_base *base, FILE *output) michael@0: { michael@0: struct event *e; michael@0: int i; michael@0: fprintf(output, "Inserted events:\n"); michael@0: TAILQ_FOREACH(e, &base->eventqueue, ev_next) { michael@0: fprintf(output, " %p [fd "EV_SOCK_FMT"]%s%s%s%s%s\n", michael@0: (void*)e, EV_SOCK_ARG(e->ev_fd), michael@0: (e->ev_events&EV_READ)?" Read":"", michael@0: (e->ev_events&EV_WRITE)?" Write":"", michael@0: (e->ev_events&EV_SIGNAL)?" Signal":"", michael@0: (e->ev_events&EV_TIMEOUT)?" Timeout":"", michael@0: (e->ev_events&EV_PERSIST)?" Persist":""); michael@0: michael@0: } michael@0: for (i = 0; i < base->nactivequeues; ++i) { michael@0: if (TAILQ_EMPTY(&base->activequeues[i])) michael@0: continue; michael@0: fprintf(output, "Active events [priority %d]:\n", i); michael@0: TAILQ_FOREACH(e, &base->eventqueue, ev_next) { michael@0: fprintf(output, " %p [fd "EV_SOCK_FMT"]%s%s%s%s\n", michael@0: (void*)e, EV_SOCK_ARG(e->ev_fd), michael@0: (e->ev_res&EV_READ)?" Read active":"", michael@0: (e->ev_res&EV_WRITE)?" Write active":"", michael@0: (e->ev_res&EV_SIGNAL)?" Signal active":"", michael@0: (e->ev_res&EV_TIMEOUT)?" Timeout active":""); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: event_base_add_virtual(struct event_base *base) michael@0: { michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: base->virtual_event_count++; michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: } michael@0: michael@0: void michael@0: event_base_del_virtual(struct event_base *base) michael@0: { michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: EVUTIL_ASSERT(base->virtual_event_count > 0); michael@0: base->virtual_event_count--; michael@0: if (base->virtual_event_count == 0 && EVBASE_NEED_NOTIFY(base)) michael@0: evthread_notify_base(base); michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: } michael@0: michael@0: #ifndef _EVENT_DISABLE_THREAD_SUPPORT michael@0: int michael@0: event_global_setup_locks_(const int enable_locks) michael@0: { michael@0: #ifndef _EVENT_DISABLE_DEBUG_MODE michael@0: EVTHREAD_SETUP_GLOBAL_LOCK(_event_debug_map_lock, 0); michael@0: #endif michael@0: if (evsig_global_setup_locks_(enable_locks) < 0) michael@0: return -1; michael@0: if (evutil_secure_rng_global_setup_locks_(enable_locks) < 0) michael@0: return -1; michael@0: return 0; michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: event_base_assert_ok(struct event_base *base) michael@0: { michael@0: int i; michael@0: EVBASE_ACQUIRE_LOCK(base, th_base_lock); michael@0: evmap_check_integrity(base); michael@0: michael@0: /* Check the heap property */ michael@0: for (i = 1; i < (int)base->timeheap.n; ++i) { michael@0: int parent = (i - 1) / 2; michael@0: struct event *ev, *p_ev; michael@0: ev = base->timeheap.p[i]; michael@0: p_ev = base->timeheap.p[parent]; michael@0: EVUTIL_ASSERT(ev->ev_flags & EV_TIMEOUT); michael@0: EVUTIL_ASSERT(evutil_timercmp(&p_ev->ev_timeout, &ev->ev_timeout, <=)); michael@0: EVUTIL_ASSERT(ev->ev_timeout_pos.min_heap_idx == i); michael@0: } michael@0: michael@0: /* Check that the common timeouts are fine */ michael@0: for (i = 0; i < base->n_common_timeouts; ++i) { michael@0: struct common_timeout_list *ctl = base->common_timeout_queues[i]; michael@0: struct event *last=NULL, *ev; michael@0: TAILQ_FOREACH(ev, &ctl->events, ev_timeout_pos.ev_next_with_common_timeout) { michael@0: if (last) michael@0: EVUTIL_ASSERT(evutil_timercmp(&last->ev_timeout, &ev->ev_timeout, <=)); michael@0: EVUTIL_ASSERT(ev->ev_flags & EV_TIMEOUT); michael@0: EVUTIL_ASSERT(is_common_timeout(&ev->ev_timeout,base)); michael@0: EVUTIL_ASSERT(COMMON_TIMEOUT_IDX(&ev->ev_timeout) == i); michael@0: last = ev; michael@0: } michael@0: } michael@0: michael@0: EVBASE_RELEASE_LOCK(base, th_base_lock); michael@0: }