diff options
author | kikht <kikht@yandex-team.ru> | 2022-02-10 16:45:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:14 +0300 |
commit | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (patch) | |
tree | be835aa92c6248212e705f25388ebafcf84bc7a1 /contrib/libs/libevent/event.c | |
parent | 194cae0e8855b11be2005e1eff12c660c3ee9774 (diff) | |
download | ydb-778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5.tar.gz |
Restoring authorship annotation for <kikht@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/libs/libevent/event.c')
-rw-r--r-- | contrib/libs/libevent/event.c | 3876 |
1 files changed, 1938 insertions, 1938 deletions
diff --git a/contrib/libs/libevent/event.c b/contrib/libs/libevent/event.c index fe147e54a2..7a42b73191 100644 --- a/contrib/libs/libevent/event.c +++ b/contrib/libs/libevent/event.c @@ -25,25 +25,25 @@ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "event2/event-config.h" -#include "evconfig-private.h" +#include "evconfig-private.h" -#ifdef _WIN32 +#ifdef _WIN32 #include <winsock2.h> #define WIN32_LEAN_AND_MEAN #include <windows.h> #undef WIN32_LEAN_AND_MEAN #endif #include <sys/types.h> -#if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H) +#if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H) #include <sys/time.h> #endif #include <sys/queue.h> -#ifdef EVENT__HAVE_SYS_SOCKET_H +#ifdef EVENT__HAVE_SYS_SOCKET_H #include <sys/socket.h> #endif #include <stdio.h> #include <stdlib.h> -#ifdef EVENT__HAVE_UNISTD_H +#ifdef EVENT__HAVE_UNISTD_H #include <unistd.h> #endif #include <ctype.h> @@ -51,10 +51,10 @@ #include <signal.h> #include <string.h> #include <time.h> -#include <limits.h> -#ifdef EVENT__HAVE_FCNTL_H -#include <fcntl.h> -#endif +#include <limits.h> +#ifdef EVENT__HAVE_FCNTL_H +#include <fcntl.h> +#endif #include "event2/event.h" #include "event2/event_struct.h" @@ -68,91 +68,91 @@ #include "evmap-internal.h" #include "iocp-internal.h" #include "changelist-internal.h" -#define HT_NO_CACHE_HASH_VALUES +#define HT_NO_CACHE_HASH_VALUES #include "ht-internal.h" #include "util-internal.h" - -#ifdef EVENT__HAVE_WORKING_KQUEUE -#include "kqueue-internal.h" -#endif - -#ifdef EVENT__HAVE_EVENT_PORTS + +#ifdef EVENT__HAVE_WORKING_KQUEUE +#include "kqueue-internal.h" +#endif + +#ifdef EVENT__HAVE_EVENT_PORTS extern const struct eventop evportops; #endif -#ifdef EVENT__HAVE_SELECT +#ifdef EVENT__HAVE_SELECT extern const struct eventop selectops; #endif -#ifdef EVENT__HAVE_POLL +#ifdef EVENT__HAVE_POLL extern const struct eventop pollops; #endif -#ifdef EVENT__HAVE_EPOLL +#ifdef EVENT__HAVE_EPOLL extern const struct eventop epollops; #endif -#ifdef EVENT__HAVE_WORKING_KQUEUE +#ifdef EVENT__HAVE_WORKING_KQUEUE extern const struct eventop kqops; #endif -#ifdef EVENT__HAVE_DEVPOLL +#ifdef EVENT__HAVE_DEVPOLL extern const struct eventop devpollops; #endif -#ifdef _WIN32 +#ifdef _WIN32 extern const struct eventop win32ops; #endif /* Array of backends in order of preference. */ static const struct eventop *eventops[] = { -#ifdef EVENT__HAVE_EVENT_PORTS +#ifdef EVENT__HAVE_EVENT_PORTS &evportops, #endif -#ifdef EVENT__HAVE_WORKING_KQUEUE +#ifdef EVENT__HAVE_WORKING_KQUEUE &kqops, #endif -#ifdef EVENT__HAVE_EPOLL +#ifdef EVENT__HAVE_EPOLL &epollops, #endif -#ifdef EVENT__HAVE_DEVPOLL +#ifdef EVENT__HAVE_DEVPOLL &devpollops, #endif -#ifdef EVENT__HAVE_POLL +#ifdef EVENT__HAVE_POLL &pollops, #endif -#ifdef EVENT__HAVE_SELECT +#ifdef EVENT__HAVE_SELECT &selectops, #endif -#ifdef _WIN32 +#ifdef _WIN32 &win32ops, #endif NULL }; /* Global state; deprecated */ -EVENT2_EXPORT_SYMBOL +EVENT2_EXPORT_SYMBOL struct event_base *event_global_current_base_ = NULL; #define current_base event_global_current_base_ /* Global state */ -static void *event_self_cbarg_ptr_ = NULL; +static void *event_self_cbarg_ptr_ = NULL; /* Prototypes */ -static void event_queue_insert_active(struct event_base *, struct event_callback *); -static void event_queue_insert_active_later(struct event_base *, struct event_callback *); -static void event_queue_insert_timeout(struct event_base *, struct event *); -static void event_queue_insert_inserted(struct event_base *, struct event *); -static void event_queue_remove_active(struct event_base *, struct event_callback *); -static void event_queue_remove_active_later(struct event_base *, struct event_callback *); -static void event_queue_remove_timeout(struct event_base *, struct event *); -static void event_queue_remove_inserted(struct event_base *, struct event *); -static void event_queue_make_later_events_active(struct event_base *base); - -static int evthread_make_base_notifiable_nolock_(struct event_base *base); -static int event_del_(struct event *ev, int blocking); - -#ifdef USE_REINSERT_TIMEOUT -/* This code seems buggy; only turn it on if we find out what the trouble is. */ -static void event_queue_reinsert_timeout(struct event_base *,struct event *, int was_common, int is_common, int old_timeout_idx); -#endif - +static void event_queue_insert_active(struct event_base *, struct event_callback *); +static void event_queue_insert_active_later(struct event_base *, struct event_callback *); +static void event_queue_insert_timeout(struct event_base *, struct event *); +static void event_queue_insert_inserted(struct event_base *, struct event *); +static void event_queue_remove_active(struct event_base *, struct event_callback *); +static void event_queue_remove_active_later(struct event_base *, struct event_callback *); +static void event_queue_remove_timeout(struct event_base *, struct event *); +static void event_queue_remove_inserted(struct event_base *, struct event *); +static void event_queue_make_later_events_active(struct event_base *base); + +static int evthread_make_base_notifiable_nolock_(struct event_base *base); +static int event_del_(struct event *ev, int blocking); + +#ifdef USE_REINSERT_TIMEOUT +/* This code seems buggy; only turn it on if we find out what the trouble is. */ +static void event_queue_reinsert_timeout(struct event_base *,struct event *, int was_common, int is_common, int old_timeout_idx); +#endif + static int event_haveevents(struct event_base *); static int event_process_active(struct event_base *); @@ -165,10 +165,10 @@ static inline void event_persist_closure(struct event_base *, struct event *ev); static int evthread_notify_base(struct event_base *base); -static void insert_common_timeout_inorder(struct common_timeout_list *ctl, - struct event *ev); - -#ifndef EVENT__DISABLE_DEBUG_MODE +static void insert_common_timeout_inorder(struct common_timeout_list *ctl, + struct event *ev); + +#ifndef EVENT__DISABLE_DEBUG_MODE /* These functions implement a hashtable of which 'struct event *' structures * have been setup or added. We don't want to trust the content of the struct * event itself, since we're trying to work through cases where an event gets @@ -202,27 +202,27 @@ eq_debug_entry(const struct event_debug_entry *a, return a->ptr == b->ptr; } -int event_debug_mode_on_ = 0; - - -#if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE) -/** - * @brief debug mode variable which is set for any function/structure that needs - * to be shared across threads (if thread support is enabled). - * - * When and if evthreads are initialized, this variable will be evaluated, - * and if set to something other than zero, this means the evthread setup - * functions were called out of order. - * - * See: "Locks and threading" in the documentation. - */ -int event_debug_created_threadable_ctx_ = 0; -#endif - +int event_debug_mode_on_ = 0; + + +#if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE) +/** + * @brief debug mode variable which is set for any function/structure that needs + * to be shared across threads (if thread support is enabled). + * + * When and if evthreads are initialized, this variable will be evaluated, + * and if set to something other than zero, this means the evthread setup + * functions were called out of order. + * + * See: "Locks and threading" in the documentation. + */ +int event_debug_created_threadable_ctx_ = 0; +#endif + /* Set if it's too late to enable event_debug_mode. */ static int event_debug_mode_too_late = 0; -#ifndef EVENT__DISABLE_THREAD_SUPPORT -static void *event_debug_map_lock_ = NULL; +#ifndef EVENT__DISABLE_THREAD_SUPPORT +static void *event_debug_map_lock_ = NULL; #endif static HT_HEAD(event_debug_map, event_debug_entry) global_debug_map = HT_INITIALIZER(); @@ -232,177 +232,177 @@ HT_PROTOTYPE(event_debug_map, event_debug_entry, node, hash_debug_entry, HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry, eq_debug_entry, 0.5, mm_malloc, mm_realloc, mm_free) -/* record that ev is now setup (that is, ready for an add) */ -static void event_debug_note_setup_(const struct event *ev) -{ - struct event_debug_entry *dent, find; - - if (!event_debug_mode_on_) - goto out; - - find.ptr = ev; - EVLOCK_LOCK(event_debug_map_lock_, 0); - dent = HT_FIND(event_debug_map, &global_debug_map, &find); - if (dent) { - dent->added = 0; - } else { - dent = mm_malloc(sizeof(*dent)); - if (!dent) - event_err(1, - "Out of memory in debugging code"); - dent->ptr = ev; - dent->added = 0; - HT_INSERT(event_debug_map, &global_debug_map, dent); - } - EVLOCK_UNLOCK(event_debug_map_lock_, 0); - -out: - event_debug_mode_too_late = 1; -} -/* record that ev is no longer setup */ -static void event_debug_note_teardown_(const struct event *ev) -{ - struct event_debug_entry *dent, find; - - if (!event_debug_mode_on_) - goto out; - - find.ptr = ev; - EVLOCK_LOCK(event_debug_map_lock_, 0); - dent = HT_REMOVE(event_debug_map, &global_debug_map, &find); - if (dent) - mm_free(dent); - EVLOCK_UNLOCK(event_debug_map_lock_, 0); - -out: - event_debug_mode_too_late = 1; -} +/* record that ev is now setup (that is, ready for an add) */ +static void event_debug_note_setup_(const struct event *ev) +{ + struct event_debug_entry *dent, find; + + if (!event_debug_mode_on_) + goto out; + + find.ptr = ev; + EVLOCK_LOCK(event_debug_map_lock_, 0); + dent = HT_FIND(event_debug_map, &global_debug_map, &find); + if (dent) { + dent->added = 0; + } else { + dent = mm_malloc(sizeof(*dent)); + if (!dent) + event_err(1, + "Out of memory in debugging code"); + dent->ptr = ev; + dent->added = 0; + HT_INSERT(event_debug_map, &global_debug_map, dent); + } + EVLOCK_UNLOCK(event_debug_map_lock_, 0); + +out: + event_debug_mode_too_late = 1; +} +/* record that ev is no longer setup */ +static void event_debug_note_teardown_(const struct event *ev) +{ + struct event_debug_entry *dent, find; + + if (!event_debug_mode_on_) + goto out; + + find.ptr = ev; + EVLOCK_LOCK(event_debug_map_lock_, 0); + dent = HT_REMOVE(event_debug_map, &global_debug_map, &find); + if (dent) + mm_free(dent); + EVLOCK_UNLOCK(event_debug_map_lock_, 0); + +out: + event_debug_mode_too_late = 1; +} /* Macro: record that ev is now added */ -static void event_debug_note_add_(const struct event *ev) -{ - struct event_debug_entry *dent,find; - - if (!event_debug_mode_on_) - goto out; - - find.ptr = ev; - EVLOCK_LOCK(event_debug_map_lock_, 0); - dent = HT_FIND(event_debug_map, &global_debug_map, &find); - if (dent) { - dent->added = 1; - } else { - event_errx(EVENT_ERR_ABORT_, - "%s: noting an add on a non-setup event %p" - " (events: 0x%x, fd: "EV_SOCK_FMT - ", flags: 0x%x)", - __func__, ev, ev->ev_events, - EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); - } - EVLOCK_UNLOCK(event_debug_map_lock_, 0); - -out: - event_debug_mode_too_late = 1; -} -/* record that ev is no longer added */ -static void event_debug_note_del_(const struct event *ev) -{ - struct event_debug_entry *dent, find; - - if (!event_debug_mode_on_) - goto out; - - find.ptr = ev; - EVLOCK_LOCK(event_debug_map_lock_, 0); - dent = HT_FIND(event_debug_map, &global_debug_map, &find); - if (dent) { - dent->added = 0; - } else { - event_errx(EVENT_ERR_ABORT_, - "%s: noting a del on a non-setup event %p" - " (events: 0x%x, fd: "EV_SOCK_FMT - ", flags: 0x%x)", - __func__, ev, ev->ev_events, - EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); - } - EVLOCK_UNLOCK(event_debug_map_lock_, 0); - -out: - event_debug_mode_too_late = 1; -} -/* assert that ev is setup (i.e., okay to add or inspect) */ -static void event_debug_assert_is_setup_(const struct event *ev) -{ - struct event_debug_entry *dent, find; - - if (!event_debug_mode_on_) +static void event_debug_note_add_(const struct event *ev) +{ + struct event_debug_entry *dent,find; + + if (!event_debug_mode_on_) + goto out; + + find.ptr = ev; + EVLOCK_LOCK(event_debug_map_lock_, 0); + dent = HT_FIND(event_debug_map, &global_debug_map, &find); + if (dent) { + dent->added = 1; + } else { + event_errx(EVENT_ERR_ABORT_, + "%s: noting an add on a non-setup event %p" + " (events: 0x%x, fd: "EV_SOCK_FMT + ", flags: 0x%x)", + __func__, ev, ev->ev_events, + EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); + } + EVLOCK_UNLOCK(event_debug_map_lock_, 0); + +out: + event_debug_mode_too_late = 1; +} +/* record that ev is no longer added */ +static void event_debug_note_del_(const struct event *ev) +{ + struct event_debug_entry *dent, find; + + if (!event_debug_mode_on_) + goto out; + + find.ptr = ev; + EVLOCK_LOCK(event_debug_map_lock_, 0); + dent = HT_FIND(event_debug_map, &global_debug_map, &find); + if (dent) { + dent->added = 0; + } else { + event_errx(EVENT_ERR_ABORT_, + "%s: noting a del on a non-setup event %p" + " (events: 0x%x, fd: "EV_SOCK_FMT + ", flags: 0x%x)", + __func__, ev, ev->ev_events, + EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); + } + EVLOCK_UNLOCK(event_debug_map_lock_, 0); + +out: + event_debug_mode_too_late = 1; +} +/* assert that ev is setup (i.e., okay to add or inspect) */ +static void event_debug_assert_is_setup_(const struct event *ev) +{ + struct event_debug_entry *dent, find; + + if (!event_debug_mode_on_) return; - find.ptr = ev; - EVLOCK_LOCK(event_debug_map_lock_, 0); - dent = HT_FIND(event_debug_map, &global_debug_map, &find); - if (!dent) { - event_errx(EVENT_ERR_ABORT_, - "%s called on a non-initialized event %p" - " (events: 0x%x, fd: "EV_SOCK_FMT - ", flags: 0x%x)", - __func__, ev, ev->ev_events, - EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); - } - EVLOCK_UNLOCK(event_debug_map_lock_, 0); -} -/* assert that ev is not added (i.e., okay to tear down or set up again) */ -static void event_debug_assert_not_added_(const struct event *ev) -{ - struct event_debug_entry *dent, find; - - if (!event_debug_mode_on_) - return; - - find.ptr = ev; - EVLOCK_LOCK(event_debug_map_lock_, 0); - dent = HT_FIND(event_debug_map, &global_debug_map, &find); - if (dent && dent->added) { - event_errx(EVENT_ERR_ABORT_, - "%s called on an already added event %p" - " (events: 0x%x, fd: "EV_SOCK_FMT", " - "flags: 0x%x)", - __func__, ev, ev->ev_events, - EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); - } - EVLOCK_UNLOCK(event_debug_map_lock_, 0); -} -static void event_debug_assert_socket_nonblocking_(evutil_socket_t fd) -{ - if (!event_debug_mode_on_) - return; - if (fd < 0) - return; - -#ifndef _WIN32 - { - int flags; - if ((flags = fcntl(fd, F_GETFL, NULL)) >= 0) { - EVUTIL_ASSERT(flags & O_NONBLOCK); - } - } + find.ptr = ev; + EVLOCK_LOCK(event_debug_map_lock_, 0); + dent = HT_FIND(event_debug_map, &global_debug_map, &find); + if (!dent) { + event_errx(EVENT_ERR_ABORT_, + "%s called on a non-initialized event %p" + " (events: 0x%x, fd: "EV_SOCK_FMT + ", flags: 0x%x)", + __func__, ev, ev->ev_events, + EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); + } + EVLOCK_UNLOCK(event_debug_map_lock_, 0); +} +/* assert that ev is not added (i.e., okay to tear down or set up again) */ +static void event_debug_assert_not_added_(const struct event *ev) +{ + struct event_debug_entry *dent, find; + + if (!event_debug_mode_on_) + return; + + find.ptr = ev; + EVLOCK_LOCK(event_debug_map_lock_, 0); + dent = HT_FIND(event_debug_map, &global_debug_map, &find); + if (dent && dent->added) { + event_errx(EVENT_ERR_ABORT_, + "%s called on an already added event %p" + " (events: 0x%x, fd: "EV_SOCK_FMT", " + "flags: 0x%x)", + __func__, ev, ev->ev_events, + EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); + } + EVLOCK_UNLOCK(event_debug_map_lock_, 0); +} +static void event_debug_assert_socket_nonblocking_(evutil_socket_t fd) +{ + if (!event_debug_mode_on_) + return; + if (fd < 0) + return; + +#ifndef _WIN32 + { + int flags; + if ((flags = fcntl(fd, F_GETFL, NULL)) >= 0) { + EVUTIL_ASSERT(flags & O_NONBLOCK); + } + } #endif } -#else -static void event_debug_note_setup_(const struct event *ev) { (void)ev; } -static void event_debug_note_teardown_(const struct event *ev) { (void)ev; } -static void event_debug_note_add_(const struct event *ev) { (void)ev; } -static void event_debug_note_del_(const struct event *ev) { (void)ev; } -static void event_debug_assert_is_setup_(const struct event *ev) { (void)ev; } -static void event_debug_assert_not_added_(const struct event *ev) { (void)ev; } -static void event_debug_assert_socket_nonblocking_(evutil_socket_t fd) { (void)fd; } -#endif - -#define EVENT_BASE_ASSERT_LOCKED(base) \ - EVLOCK_ASSERT_LOCKED((base)->th_base_lock) - +#else +static void event_debug_note_setup_(const struct event *ev) { (void)ev; } +static void event_debug_note_teardown_(const struct event *ev) { (void)ev; } +static void event_debug_note_add_(const struct event *ev) { (void)ev; } +static void event_debug_note_del_(const struct event *ev) { (void)ev; } +static void event_debug_assert_is_setup_(const struct event *ev) { (void)ev; } +static void event_debug_assert_not_added_(const struct event *ev) { (void)ev; } +static void event_debug_assert_socket_nonblocking_(evutil_socket_t fd) { (void)fd; } +#endif + +#define EVENT_BASE_ASSERT_LOCKED(base) \ + EVLOCK_ASSERT_LOCKED((base)->th_base_lock) + /* How often (in seconds) do we check for changes in wall clock time relative * to monotonic time? Set this to -1 for 'never.' */ -#define CLOCK_SYNC_INTERVAL 5 +#define CLOCK_SYNC_INTERVAL 5 /** Set 'tp' to the current time according to 'base'. We must hold the lock * on 'base'. If there is a cached time, return it. Otherwise, use @@ -419,19 +419,19 @@ gettime(struct event_base *base, struct timeval *tp) return (0); } - if (evutil_gettime_monotonic_(&base->monotonic_timer, tp) == -1) { - return -1; - } + if (evutil_gettime_monotonic_(&base->monotonic_timer, tp) == -1) { + return -1; + } - if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL - < tp->tv_sec) { - struct timeval tv; - evutil_gettimeofday(&tv,NULL); - evutil_timersub(&tv, tp, &base->tv_clock_diff); - base->last_updated_clock_diff = tp->tv_sec; + if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL + < tp->tv_sec) { + struct timeval tv; + evutil_gettimeofday(&tv,NULL); + evutil_timersub(&tv, tp, &base->tv_clock_diff); + base->last_updated_clock_diff = tp->tv_sec; } - return 0; + return 0; } int @@ -471,36 +471,36 @@ update_time_cache(struct event_base *base) gettime(base, &base->tv_cache); } -int -event_base_update_cache_time(struct event_base *base) -{ - - if (!base) { - base = current_base; - if (!current_base) - return -1; - } - - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - if (base->running_loop) - update_time_cache(base); - EVBASE_RELEASE_LOCK(base, th_base_lock); - return 0; -} - -static inline struct event * -event_callback_to_event(struct event_callback *evcb) -{ - EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_INIT)); - return EVUTIL_UPCAST(evcb, struct event, ev_evcallback); -} - -static inline struct event_callback * -event_to_event_callback(struct event *ev) -{ - return &ev->ev_evcallback; -} - +int +event_base_update_cache_time(struct event_base *base) +{ + + if (!base) { + base = current_base; + if (!current_base) + return -1; + } + + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + if (base->running_loop) + update_time_cache(base); + EVBASE_RELEASE_LOCK(base, th_base_lock); + return 0; +} + +static inline struct event * +event_callback_to_event(struct event_callback *evcb) +{ + EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_INIT)); + return EVUTIL_UPCAST(evcb, struct event, ev_evcallback); +} + +static inline struct event_callback * +event_to_event_callback(struct event *ev) +{ + return &ev->ev_evcallback; +} + struct event_base * event_init(void) { @@ -554,10 +554,10 @@ event_is_method_disabled(const char *name) evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name); for (i = 8; environment[i] != '\0'; ++i) - environment[i] = EVUTIL_TOUPPER_(environment[i]); - /* Note that evutil_getenv_() ignores the environment entirely if + environment[i] = EVUTIL_TOUPPER_(environment[i]); + /* Note that evutil_getenv_() ignores the environment entirely if * we're setuid */ - return (evutil_getenv_(environment) != NULL); + return (evutil_getenv_(environment) != NULL); } int @@ -569,14 +569,14 @@ event_base_get_features(const struct event_base *base) void event_enable_debug_mode(void) { -#ifndef EVENT__DISABLE_DEBUG_MODE - if (event_debug_mode_on_) +#ifndef EVENT__DISABLE_DEBUG_MODE + if (event_debug_mode_on_) event_errx(1, "%s was called twice!", __func__); if (event_debug_mode_too_late) event_errx(1, "%s must be called *before* creating any events " "or event_bases",__func__); - event_debug_mode_on_ = 1; + event_debug_mode_on_ = 1; HT_INIT(event_debug_map, &global_debug_map); #endif @@ -585,20 +585,20 @@ event_enable_debug_mode(void) void event_disable_debug_mode(void) { -#ifndef EVENT__DISABLE_DEBUG_MODE +#ifndef EVENT__DISABLE_DEBUG_MODE struct event_debug_entry **ent, *victim; - EVLOCK_LOCK(event_debug_map_lock_, 0); + EVLOCK_LOCK(event_debug_map_lock_, 0); for (ent = HT_START(event_debug_map, &global_debug_map); ent; ) { victim = *ent; - ent = HT_NEXT_RMV(event_debug_map, &global_debug_map, ent); + ent = HT_NEXT_RMV(event_debug_map, &global_debug_map, ent); mm_free(victim); } HT_CLEAR(event_debug_map, &global_debug_map); - EVLOCK_UNLOCK(event_debug_map_lock_ , 0); - - event_debug_mode_on_ = 0; -#endif + EVLOCK_UNLOCK(event_debug_map_lock_ , 0); + + event_debug_mode_on_ = 0; +#endif } struct event_base * @@ -608,7 +608,7 @@ event_base_new_with_config(const struct event_config *cfg) struct event_base *base; int should_check_environment; -#ifndef EVENT__DISABLE_DEBUG_MODE +#ifndef EVENT__DISABLE_DEBUG_MODE event_debug_mode_too_late = 1; #endif @@ -617,61 +617,61 @@ event_base_new_with_config(const struct event_config *cfg) return NULL; } - if (cfg) - base->flags = cfg->flags; - - should_check_environment = - !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV)); - - { - struct timeval tmp; - int precise_time = - cfg && (cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER); - int flags; - if (should_check_environment && !precise_time) { - precise_time = evutil_getenv_("EVENT_PRECISE_TIMER") != NULL; - if (precise_time) { - base->flags |= EVENT_BASE_FLAG_PRECISE_TIMER; - } - } - flags = precise_time ? EV_MONOT_PRECISE : 0; - evutil_configure_monotonic_time_(&base->monotonic_timer, flags); - - gettime(base, &tmp); - } - - min_heap_ctor_(&base->timeheap); - + if (cfg) + base->flags = cfg->flags; + + should_check_environment = + !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV)); + + { + struct timeval tmp; + int precise_time = + cfg && (cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER); + int flags; + if (should_check_environment && !precise_time) { + precise_time = evutil_getenv_("EVENT_PRECISE_TIMER") != NULL; + if (precise_time) { + base->flags |= EVENT_BASE_FLAG_PRECISE_TIMER; + } + } + flags = precise_time ? EV_MONOT_PRECISE : 0; + evutil_configure_monotonic_time_(&base->monotonic_timer, flags); + + gettime(base, &tmp); + } + + min_heap_ctor_(&base->timeheap); + base->sig.ev_signal_pair[0] = -1; base->sig.ev_signal_pair[1] = -1; base->th_notify_fd[0] = -1; base->th_notify_fd[1] = -1; - TAILQ_INIT(&base->active_later_queue); + TAILQ_INIT(&base->active_later_queue); - evmap_io_initmap_(&base->io); - evmap_signal_initmap_(&base->sigmap); - event_changelist_init_(&base->changelist); + evmap_io_initmap_(&base->io); + evmap_signal_initmap_(&base->sigmap); + event_changelist_init_(&base->changelist); base->evbase = NULL; - if (cfg) { - memcpy(&base->max_dispatch_time, - &cfg->max_dispatch_interval, sizeof(struct timeval)); - base->limit_callbacks_after_prio = - cfg->limit_callbacks_after_prio; - } else { - base->max_dispatch_time.tv_sec = -1; - base->limit_callbacks_after_prio = 1; - } - if (cfg && cfg->max_dispatch_callbacks >= 0) { - base->max_dispatch_callbacks = cfg->max_dispatch_callbacks; - } else { - base->max_dispatch_callbacks = INT_MAX; - } - if (base->max_dispatch_callbacks == INT_MAX && - base->max_dispatch_time.tv_sec == -1) - base->limit_callbacks_after_prio = INT_MAX; + if (cfg) { + memcpy(&base->max_dispatch_time, + &cfg->max_dispatch_interval, sizeof(struct timeval)); + base->limit_callbacks_after_prio = + cfg->limit_callbacks_after_prio; + } else { + base->max_dispatch_time.tv_sec = -1; + base->limit_callbacks_after_prio = 1; + } + if (cfg && cfg->max_dispatch_callbacks >= 0) { + base->max_dispatch_callbacks = cfg->max_dispatch_callbacks; + } else { + base->max_dispatch_callbacks = INT_MAX; + } + if (base->max_dispatch_callbacks == INT_MAX && + base->max_dispatch_time.tv_sec == -1) + base->limit_callbacks_after_prio = INT_MAX; for (i = 0; eventops[i] && !base->evbase; i++) { if (cfg != NULL) { @@ -702,7 +702,7 @@ event_base_new_with_config(const struct event_config *cfg) return NULL; } - if (evutil_getenv_("EVENT_SHOW_METHOD")) + if (evutil_getenv_("EVENT_SHOW_METHOD")) event_msgx("libevent using: %s", base->evsel->name); /* allocate a single active event queue */ @@ -713,15 +713,15 @@ event_base_new_with_config(const struct event_config *cfg) /* prepare for threading */ -#if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE) - event_debug_created_threadable_ctx_ = 1; -#endif - -#ifndef EVENT__DISABLE_THREAD_SUPPORT +#if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE) + event_debug_created_threadable_ctx_ = 1; +#endif + +#ifndef EVENT__DISABLE_THREAD_SUPPORT if (EVTHREAD_LOCKING_ENABLED() && (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) { int r; - EVTHREAD_ALLOC_LOCK(base->th_base_lock, 0); + EVTHREAD_ALLOC_LOCK(base->th_base_lock, 0); EVTHREAD_ALLOC_COND(base->current_event_cond); r = evthread_make_base_notifiable(base); if (r<0) { @@ -732,21 +732,21 @@ event_base_new_with_config(const struct event_config *cfg) } #endif -#ifdef _WIN32 +#ifdef _WIN32 if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP)) - event_base_start_iocp_(base, cfg->n_cpus_hint); + event_base_start_iocp_(base, cfg->n_cpus_hint); #endif return (base); } int -event_base_start_iocp_(struct event_base *base, int n_cpus) +event_base_start_iocp_(struct event_base *base, int n_cpus) { -#ifdef _WIN32 +#ifdef _WIN32 if (base->iocp) return 0; - base->iocp = event_iocp_port_launch_(n_cpus); + base->iocp = event_iocp_port_launch_(n_cpus); if (!base->iocp) { event_warnx("%s: Couldn't launch IOCP", __func__); return -1; @@ -758,85 +758,85 @@ event_base_start_iocp_(struct event_base *base, int n_cpus) } void -event_base_stop_iocp_(struct event_base *base) +event_base_stop_iocp_(struct event_base *base) { -#ifdef _WIN32 +#ifdef _WIN32 int rv; if (!base->iocp) return; - rv = event_iocp_shutdown_(base->iocp, -1); + rv = event_iocp_shutdown_(base->iocp, -1); EVUTIL_ASSERT(rv >= 0); base->iocp = NULL; #endif } -static int -event_base_cancel_single_callback_(struct event_base *base, - struct event_callback *evcb, - int run_finalizers) -{ - int result = 0; - - if (evcb->evcb_flags & EVLIST_INIT) { - struct event *ev = event_callback_to_event(evcb); - if (!(ev->ev_flags & EVLIST_INTERNAL)) { - event_del_(ev, EVENT_DEL_EVEN_IF_FINALIZING); - result = 1; - } - } else { - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - event_callback_cancel_nolock_(base, evcb, 1); - EVBASE_RELEASE_LOCK(base, th_base_lock); - result = 1; - } - - if (run_finalizers && (evcb->evcb_flags & EVLIST_FINALIZING)) { - switch (evcb->evcb_closure) { - case EV_CLOSURE_EVENT_FINALIZE: - case EV_CLOSURE_EVENT_FINALIZE_FREE: { - struct event *ev = event_callback_to_event(evcb); - ev->ev_evcallback.evcb_cb_union.evcb_evfinalize(ev, ev->ev_arg); - if (evcb->evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE) - mm_free(ev); - break; - } - case EV_CLOSURE_CB_FINALIZE: - evcb->evcb_cb_union.evcb_cbfinalize(evcb, evcb->evcb_arg); - break; - default: - break; - } - } - return result; -} - -static int event_base_free_queues_(struct event_base *base, int run_finalizers) -{ - int deleted = 0, i; - - for (i = 0; i < base->nactivequeues; ++i) { - struct event_callback *evcb, *next; - for (evcb = TAILQ_FIRST(&base->activequeues[i]); evcb; ) { - next = TAILQ_NEXT(evcb, evcb_active_next); - deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers); - evcb = next; - } - } - - { - struct event_callback *evcb; - while ((evcb = TAILQ_FIRST(&base->active_later_queue))) { - deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers); - } - } - - return deleted; -} - -static void -event_base_free_(struct event_base *base, int run_finalizers) -{ +static int +event_base_cancel_single_callback_(struct event_base *base, + struct event_callback *evcb, + int run_finalizers) +{ + int result = 0; + + if (evcb->evcb_flags & EVLIST_INIT) { + struct event *ev = event_callback_to_event(evcb); + if (!(ev->ev_flags & EVLIST_INTERNAL)) { + event_del_(ev, EVENT_DEL_EVEN_IF_FINALIZING); + result = 1; + } + } else { + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + event_callback_cancel_nolock_(base, evcb, 1); + EVBASE_RELEASE_LOCK(base, th_base_lock); + result = 1; + } + + if (run_finalizers && (evcb->evcb_flags & EVLIST_FINALIZING)) { + switch (evcb->evcb_closure) { + case EV_CLOSURE_EVENT_FINALIZE: + case EV_CLOSURE_EVENT_FINALIZE_FREE: { + struct event *ev = event_callback_to_event(evcb); + ev->ev_evcallback.evcb_cb_union.evcb_evfinalize(ev, ev->ev_arg); + if (evcb->evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE) + mm_free(ev); + break; + } + case EV_CLOSURE_CB_FINALIZE: + evcb->evcb_cb_union.evcb_cbfinalize(evcb, evcb->evcb_arg); + break; + default: + break; + } + } + return result; +} + +static int event_base_free_queues_(struct event_base *base, int run_finalizers) +{ + int deleted = 0, i; + + for (i = 0; i < base->nactivequeues; ++i) { + struct event_callback *evcb, *next; + for (evcb = TAILQ_FIRST(&base->activequeues[i]); evcb; ) { + next = TAILQ_NEXT(evcb, evcb_active_next); + deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers); + evcb = next; + } + } + + { + struct event_callback *evcb; + while ((evcb = TAILQ_FIRST(&base->active_later_queue))) { + deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers); + } + } + + return deleted; +} + +static void +event_base_free_(struct event_base *base, int run_finalizers) +{ int i, n_deleted=0; struct event *ev; /* XXXX grab the lock? If there is contention when one thread frees @@ -853,8 +853,8 @@ event_base_free_(struct event_base *base, int run_finalizers) } /* XXX(niels) - check for internal events first */ -#ifdef _WIN32 - event_base_stop_iocp_(base); +#ifdef _WIN32 + event_base_stop_iocp_(base); #endif /* threading fds if we have them */ @@ -869,9 +869,9 @@ event_base_free_(struct event_base *base, int run_finalizers) } /* Delete all non-internal events. */ - evmap_delete_all_(base); - - while ((ev = min_heap_top_(&base->timeheap)) != NULL) { + evmap_delete_all_(base); + + while ((ev = min_heap_top_(&base->timeheap)) != NULL) { event_del(ev); ++n_deleted; } @@ -894,88 +894,88 @@ event_base_free_(struct event_base *base, int run_finalizers) if (base->common_timeout_queues) mm_free(base->common_timeout_queues); - for (;;) { - /* For finalizers we can register yet another finalizer out from - * finalizer, and iff finalizer will be in active_later_queue we can - * add finalizer to activequeues, and we will have events in - * activequeues after this function returns, which is not what we want - * (we even have an assertion for this). - * - * A simple case is bufferevent with underlying (i.e. filters). - */ - int i = event_base_free_queues_(base, run_finalizers); - event_debug(("%s: %d events freed", __func__, i)); - if (!i) { - break; + for (;;) { + /* For finalizers we can register yet another finalizer out from + * finalizer, and iff finalizer will be in active_later_queue we can + * add finalizer to activequeues, and we will have events in + * activequeues after this function returns, which is not what we want + * (we even have an assertion for this). + * + * A simple case is bufferevent with underlying (i.e. filters). + */ + int i = event_base_free_queues_(base, run_finalizers); + event_debug(("%s: %d events freed", __func__, i)); + if (!i) { + break; } - n_deleted += i; + n_deleted += i; } if (n_deleted) event_debug(("%s: %d events were still set in base", __func__, n_deleted)); - while (LIST_FIRST(&base->once_events)) { - struct event_once *eonce = LIST_FIRST(&base->once_events); - LIST_REMOVE(eonce, next_once); - mm_free(eonce); - } - + while (LIST_FIRST(&base->once_events)) { + struct event_once *eonce = LIST_FIRST(&base->once_events); + LIST_REMOVE(eonce, next_once); + mm_free(eonce); + } + if (base->evsel != NULL && base->evsel->dealloc != NULL) base->evsel->dealloc(base); for (i = 0; i < base->nactivequeues; ++i) EVUTIL_ASSERT(TAILQ_EMPTY(&base->activequeues[i])); - EVUTIL_ASSERT(min_heap_empty_(&base->timeheap)); - min_heap_dtor_(&base->timeheap); + EVUTIL_ASSERT(min_heap_empty_(&base->timeheap)); + min_heap_dtor_(&base->timeheap); mm_free(base->activequeues); - evmap_io_clear_(&base->io); - evmap_signal_clear_(&base->sigmap); - event_changelist_freemem_(&base->changelist); + evmap_io_clear_(&base->io); + evmap_signal_clear_(&base->sigmap); + event_changelist_freemem_(&base->changelist); - EVTHREAD_FREE_LOCK(base->th_base_lock, 0); + EVTHREAD_FREE_LOCK(base->th_base_lock, 0); EVTHREAD_FREE_COND(base->current_event_cond); - /* If we're freeing current_base, there won't be a current_base. */ - if (base == current_base) - current_base = NULL; + /* If we're freeing current_base, there won't be a current_base. */ + if (base == current_base) + current_base = NULL; mm_free(base); } -void -event_base_free_nofinalize(struct event_base *base) -{ - event_base_free_(base, 0); -} - -void -event_base_free(struct event_base *base) -{ - event_base_free_(base, 1); -} - -/* Fake eventop; used to disable the backend temporarily inside event_reinit - * so that we can call event_del() on an event without telling the backend. - */ -static int -nil_backend_del(struct event_base *b, evutil_socket_t fd, short old, - short events, void *fdinfo) -{ - return 0; -} -const struct eventop nil_eventop = { - "nil", - NULL, /* init: unused. */ - NULL, /* add: unused. */ - nil_backend_del, /* del: used, so needs to be killed. */ - NULL, /* dispatch: unused. */ - NULL, /* dealloc: unused. */ - 0, 0, 0 -}; - +void +event_base_free_nofinalize(struct event_base *base) +{ + event_base_free_(base, 0); +} + +void +event_base_free(struct event_base *base) +{ + event_base_free_(base, 1); +} + +/* Fake eventop; used to disable the backend temporarily inside event_reinit + * so that we can call event_del() on an event without telling the backend. + */ +static int +nil_backend_del(struct event_base *b, evutil_socket_t fd, short old, + short events, void *fdinfo) +{ + return 0; +} +const struct eventop nil_eventop = { + "nil", + NULL, /* init: unused. */ + NULL, /* add: unused. */ + nil_backend_del, /* del: used, so needs to be killed. */ + NULL, /* dispatch: unused. */ + NULL, /* dealloc: unused. */ + 0, 0, 0 +}; + /* reinitialize the event base after a fork */ int event_reinit(struct event_base *base) @@ -983,48 +983,48 @@ event_reinit(struct event_base *base) const struct eventop *evsel; int res = 0; int was_notifiable = 0; - int had_signal_added = 0; + int had_signal_added = 0; EVBASE_ACQUIRE_LOCK(base, th_base_lock); evsel = base->evsel; - /* check if this event mechanism requires reinit on the backend */ - if (evsel->need_reinit) { - /* We're going to call event_del() on our notify events (the - * ones that tell about signals and wakeup events). But we - * don't actually want to tell the backend to change its - * state, since it might still share some resource (a kqueue, - * an epoll fd) with the parent process, and we don't want to - * delete the fds from _that_ backend, we temporarily stub out - * the evsel with a replacement. - */ - base->evsel = &nil_eventop; - } - - /* We need to re-create a new signal-notification fd and a new - * thread-notification fd. Otherwise, we'll still share those with - * the parent process, which would make any notification sent to them - * get received by one or both of the event loops, more or less at - * random. + /* check if this event mechanism requires reinit on the backend */ + if (evsel->need_reinit) { + /* We're going to call event_del() on our notify events (the + * ones that tell about signals and wakeup events). But we + * don't actually want to tell the backend to change its + * state, since it might still share some resource (a kqueue, + * an epoll fd) with the parent process, and we don't want to + * delete the fds from _that_ backend, we temporarily stub out + * the evsel with a replacement. + */ + base->evsel = &nil_eventop; + } + + /* We need to re-create a new signal-notification fd and a new + * thread-notification fd. Otherwise, we'll still share those with + * the parent process, which would make any notification sent to them + * get received by one or both of the event loops, more or less at + * random. */ if (base->sig.ev_signal_added) { - event_del_nolock_(&base->sig.ev_signal, EVENT_DEL_AUTOBLOCK); - event_debug_unassign(&base->sig.ev_signal); - memset(&base->sig.ev_signal, 0, sizeof(base->sig.ev_signal)); - had_signal_added = 1; + event_del_nolock_(&base->sig.ev_signal, EVENT_DEL_AUTOBLOCK); + event_debug_unassign(&base->sig.ev_signal); + memset(&base->sig.ev_signal, 0, sizeof(base->sig.ev_signal)); + had_signal_added = 1; base->sig.ev_signal_added = 0; } - if (base->sig.ev_signal_pair[0] != -1) - EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]); - if (base->sig.ev_signal_pair[1] != -1) - EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]); - if (base->th_notify_fn != NULL) { - was_notifiable = 1; - base->th_notify_fn = NULL; - } + if (base->sig.ev_signal_pair[0] != -1) + EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]); + if (base->sig.ev_signal_pair[1] != -1) + EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]); + if (base->th_notify_fn != NULL) { + was_notifiable = 1; + base->th_notify_fn = NULL; + } if (base->th_notify_fd[0] != -1) { - event_del_nolock_(&base->th_notify, EVENT_DEL_AUTOBLOCK); + event_del_nolock_(&base->th_notify, EVENT_DEL_AUTOBLOCK); EVUTIL_CLOSESOCKET(base->th_notify_fd[0]); if (base->th_notify_fd[1] != -1) EVUTIL_CLOSESOCKET(base->th_notify_fd[1]); @@ -1033,73 +1033,73 @@ event_reinit(struct event_base *base) event_debug_unassign(&base->th_notify); } - /* Replace the original evsel. */ - base->evsel = evsel; - - if (evsel->need_reinit) { - /* Reconstruct the backend through brute-force, so that we do - * not share any structures with the parent process. For some - * backends, this is necessary: epoll and kqueue, for - * instance, have events associated with a kernel - * structure. If didn't reinitialize, we'd share that - * structure with the parent process, and any changes made by - * the parent would affect our backend's behavior (and vice - * versa). - */ - if (base->evsel->dealloc != NULL) - base->evsel->dealloc(base); - base->evbase = evsel->init(base); - if (base->evbase == NULL) { - event_errx(1, - "%s: could not reinitialize event mechanism", - __func__); - res = -1; - goto done; - } - - /* Empty out the changelist (if any): we are starting from a - * blank slate. */ - event_changelist_freemem_(&base->changelist); - - /* Tell the event maps to re-inform the backend about all - * pending events. This will make the signal notification - * event get re-created if necessary. */ - if (evmap_reinit_(base) < 0) - res = -1; - } else { - res = evsig_init_(base); - if (res == 0 && had_signal_added) { - res = event_add_nolock_(&base->sig.ev_signal, NULL, 0); - if (res == 0) - base->sig.ev_signal_added = 1; + /* Replace the original evsel. */ + base->evsel = evsel; + + if (evsel->need_reinit) { + /* Reconstruct the backend through brute-force, so that we do + * not share any structures with the parent process. For some + * backends, this is necessary: epoll and kqueue, for + * instance, have events associated with a kernel + * structure. If didn't reinitialize, we'd share that + * structure with the parent process, and any changes made by + * the parent would affect our backend's behavior (and vice + * versa). + */ + if (base->evsel->dealloc != NULL) + base->evsel->dealloc(base); + base->evbase = evsel->init(base); + if (base->evbase == NULL) { + event_errx(1, + "%s: could not reinitialize event mechanism", + __func__); + res = -1; + goto done; + } + + /* Empty out the changelist (if any): we are starting from a + * blank slate. */ + event_changelist_freemem_(&base->changelist); + + /* Tell the event maps to re-inform the backend about all + * pending events. This will make the signal notification + * event get re-created if necessary. */ + if (evmap_reinit_(base) < 0) + res = -1; + } else { + res = evsig_init_(base); + if (res == 0 && had_signal_added) { + res = event_add_nolock_(&base->sig.ev_signal, NULL, 0); + if (res == 0) + base->sig.ev_signal_added = 1; } } - /* If we were notifiable before, and nothing just exploded, become - * notifiable again. */ + /* If we were notifiable before, and nothing just exploded, become + * notifiable again. */ if (was_notifiable && res == 0) - res = evthread_make_base_notifiable_nolock_(base); + res = evthread_make_base_notifiable_nolock_(base); done: EVBASE_RELEASE_LOCK(base, th_base_lock); return (res); } -/* Get the monotonic time for this event_base' timer */ -int -event_gettime_monotonic(struct event_base *base, struct timeval *tv) -{ - int rv = -1; - - if (base && tv) { - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - rv = evutil_gettime_monotonic_(&(base->monotonic_timer), tv); - EVBASE_RELEASE_LOCK(base, th_base_lock); - } - - return rv; -} - +/* Get the monotonic time for this event_base' timer */ +int +event_gettime_monotonic(struct event_base *base, struct timeval *tv) +{ + int rv = -1; + + if (base && tv) { + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + rv = evutil_gettime_monotonic_(&(base->monotonic_timer), tv); + EVBASE_RELEASE_LOCK(base, th_base_lock); + } + + return rv; +} + const char ** event_get_supported_methods(void) { @@ -1141,9 +1141,9 @@ event_config_new(void) return (NULL); TAILQ_INIT(&cfg->entries); - cfg->max_dispatch_interval.tv_sec = -1; - cfg->max_dispatch_callbacks = INT_MAX; - cfg->limit_callbacks_after_prio = 1; + cfg->max_dispatch_interval.tv_sec = -1; + cfg->max_dispatch_callbacks = INT_MAX; + cfg->limit_callbacks_after_prio = 1; return (cfg); } @@ -1214,23 +1214,23 @@ event_config_set_num_cpus_hint(struct event_config *cfg, int cpus) } int -event_config_set_max_dispatch_interval(struct event_config *cfg, - const struct timeval *max_interval, int max_callbacks, int min_priority) -{ - if (max_interval) - memcpy(&cfg->max_dispatch_interval, max_interval, - sizeof(struct timeval)); - else - cfg->max_dispatch_interval.tv_sec = -1; - cfg->max_dispatch_callbacks = - max_callbacks >= 0 ? max_callbacks : INT_MAX; - if (min_priority < 0) - min_priority = 0; - cfg->limit_callbacks_after_prio = min_priority; - return (0); -} - -int +event_config_set_max_dispatch_interval(struct event_config *cfg, + const struct timeval *max_interval, int max_callbacks, int min_priority) +{ + if (max_interval) + memcpy(&cfg->max_dispatch_interval, max_interval, + sizeof(struct timeval)); + else + cfg->max_dispatch_interval.tv_sec = -1; + cfg->max_dispatch_callbacks = + max_callbacks >= 0 ? max_callbacks : INT_MAX; + if (min_priority < 0) + min_priority = 0; + cfg->limit_callbacks_after_prio = min_priority; + return (0); +} + +int event_priority_init(int npriorities) { return event_base_priority_init(current_base, npriorities); @@ -1239,17 +1239,17 @@ event_priority_init(int npriorities) int event_base_priority_init(struct event_base *base, int npriorities) { - int i, r; - r = -1; + int i, r; + r = -1; + + EVBASE_ACQUIRE_LOCK(base, th_base_lock); - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - if (N_ACTIVE_CALLBACKS(base) || npriorities < 1 || npriorities >= EVENT_MAX_PRIORITIES) - goto err; + goto err; if (npriorities == base->nactivequeues) - goto ok; + goto ok; if (base->nactivequeues) { mm_free(base->activequeues); @@ -1257,11 +1257,11 @@ event_base_priority_init(struct event_base *base, int npriorities) } /* Allocate our priority queues */ - base->activequeues = (struct evcallback_list *) - mm_calloc(npriorities, sizeof(struct evcallback_list)); + base->activequeues = (struct evcallback_list *) + mm_calloc(npriorities, sizeof(struct evcallback_list)); if (base->activequeues == NULL) { event_warn("%s: calloc", __func__); - goto err; + goto err; } base->nactivequeues = npriorities; @@ -1269,78 +1269,78 @@ event_base_priority_init(struct event_base *base, int npriorities) TAILQ_INIT(&base->activequeues[i]); } -ok: - r = 0; -err: - EVBASE_RELEASE_LOCK(base, th_base_lock); - return (r); -} - -int -event_base_get_npriorities(struct event_base *base) -{ - - int n; - if (base == NULL) - base = current_base; - - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - n = base->nactivequeues; - EVBASE_RELEASE_LOCK(base, th_base_lock); - return (n); -} - -int -event_base_get_num_events(struct event_base *base, unsigned int type) -{ - int r = 0; - - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - - if (type & EVENT_BASE_COUNT_ACTIVE) - r += base->event_count_active; - - if (type & EVENT_BASE_COUNT_VIRTUAL) - r += base->virtual_event_count; - - if (type & EVENT_BASE_COUNT_ADDED) - r += base->event_count; - - EVBASE_RELEASE_LOCK(base, th_base_lock); - - return r; -} - -int -event_base_get_max_events(struct event_base *base, unsigned int type, int clear) -{ - int r = 0; - - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - - if (type & EVENT_BASE_COUNT_ACTIVE) { - r += base->event_count_active_max; - if (clear) - base->event_count_active_max = 0; - } - - if (type & EVENT_BASE_COUNT_VIRTUAL) { - r += base->virtual_event_count_max; - if (clear) - base->virtual_event_count_max = 0; - } - - if (type & EVENT_BASE_COUNT_ADDED) { - r += base->event_count_max; - if (clear) - base->event_count_max = 0; - } - - EVBASE_RELEASE_LOCK(base, th_base_lock); - - return r; -} - +ok: + r = 0; +err: + EVBASE_RELEASE_LOCK(base, th_base_lock); + return (r); +} + +int +event_base_get_npriorities(struct event_base *base) +{ + + int n; + if (base == NULL) + base = current_base; + + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + n = base->nactivequeues; + EVBASE_RELEASE_LOCK(base, th_base_lock); + return (n); +} + +int +event_base_get_num_events(struct event_base *base, unsigned int type) +{ + int r = 0; + + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + + if (type & EVENT_BASE_COUNT_ACTIVE) + r += base->event_count_active; + + if (type & EVENT_BASE_COUNT_VIRTUAL) + r += base->virtual_event_count; + + if (type & EVENT_BASE_COUNT_ADDED) + r += base->event_count; + + EVBASE_RELEASE_LOCK(base, th_base_lock); + + return r; +} + +int +event_base_get_max_events(struct event_base *base, unsigned int type, int clear) +{ + int r = 0; + + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + + if (type & EVENT_BASE_COUNT_ACTIVE) { + r += base->event_count_active_max; + if (clear) + base->event_count_active_max = 0; + } + + if (type & EVENT_BASE_COUNT_VIRTUAL) { + r += base->virtual_event_count_max; + if (clear) + base->virtual_event_count_max = 0; + } + + if (type & EVENT_BASE_COUNT_ADDED) { + r += base->event_count_max; + if (clear) + base->event_count_max = 0; + } + + EVBASE_RELEASE_LOCK(base, th_base_lock); + + return r; +} + /* Returns true iff we're currently watching any events. */ static int event_haveevents(struct event_base *base) @@ -1452,7 +1452,7 @@ common_timeout_schedule(struct common_timeout_list *ctl, { struct timeval timeout = head->ev_timeout; timeout.tv_usec &= MICROSECONDS_MASK; - event_add_nolock_(&ctl->timeout_event, &timeout, 1); + event_add_nolock_(&ctl->timeout_event, &timeout, 1); } /* Callback: invoked when the timeout for a common timeout queue triggers. @@ -1473,8 +1473,8 @@ common_timeout_callback(evutil_socket_t fd, short what, void *arg) (ev->ev_timeout.tv_sec == now.tv_sec && (ev->ev_timeout.tv_usec&MICROSECONDS_MASK) > now.tv_usec)) break; - event_del_nolock_(ev, EVENT_DEL_NOBLOCK); - event_active_nolock_(ev, EV_TIMEOUT, 1); + event_del_nolock_(ev, EVENT_DEL_NOBLOCK); + event_active_nolock_(ev, EV_TIMEOUT, 1); } if (ev) common_timeout_schedule(ctl, &now, ev); @@ -1607,7 +1607,7 @@ event_persist_closure(struct event_base *base, struct event *ev) evutil_timeradd(&now, &delay, &run_at); } run_at.tv_usec |= usec_mask; - event_add_nolock_(ev, &run_at, 1); + event_add_nolock_(ev, &run_at, 1); } // Save our callback before we release the lock @@ -1628,105 +1628,105 @@ event_persist_closure(struct event_base *base, struct event *ev) releasing the lock as we go. This function requires that the lock be held when it's invoked. Returns -1 if we get a signal or an event_break that means we should stop processing any active events now. Otherwise returns - the number of non-internal event_callbacks that we processed. + the number of non-internal event_callbacks that we processed. */ static int event_process_active_single_queue(struct event_base *base, - struct evcallback_list *activeq, - int max_to_process, const struct timeval *endtime) + struct evcallback_list *activeq, + int max_to_process, const struct timeval *endtime) { - struct event_callback *evcb; + struct event_callback *evcb; int count = 0; EVUTIL_ASSERT(activeq != NULL); - for (evcb = TAILQ_FIRST(activeq); evcb; evcb = TAILQ_FIRST(activeq)) { - struct event *ev=NULL; - if (evcb->evcb_flags & EVLIST_INIT) { - ev = event_callback_to_event(evcb); - - if (ev->ev_events & EV_PERSIST || ev->ev_flags & EVLIST_FINALIZING) - event_queue_remove_active(base, evcb); - else - event_del_nolock_(ev, EVENT_DEL_NOBLOCK); - event_debug(( - "event_process_active: event: %p, %s%s%scall %p", - ev, - ev->ev_res & EV_READ ? "EV_READ " : " ", - ev->ev_res & EV_WRITE ? "EV_WRITE " : " ", - ev->ev_res & EV_CLOSED ? "EV_CLOSED " : " ", - ev->ev_callback)); - } else { - event_queue_remove_active(base, evcb); - event_debug(("event_process_active: event_callback %p, " - "closure %d, call %p", - evcb, evcb->evcb_closure, evcb->evcb_cb_union.evcb_callback)); - } - - if (!(evcb->evcb_flags & EVLIST_INTERNAL)) + for (evcb = TAILQ_FIRST(activeq); evcb; evcb = TAILQ_FIRST(activeq)) { + struct event *ev=NULL; + if (evcb->evcb_flags & EVLIST_INIT) { + ev = event_callback_to_event(evcb); + + if (ev->ev_events & EV_PERSIST || ev->ev_flags & EVLIST_FINALIZING) + event_queue_remove_active(base, evcb); + else + event_del_nolock_(ev, EVENT_DEL_NOBLOCK); + event_debug(( + "event_process_active: event: %p, %s%s%scall %p", + ev, + ev->ev_res & EV_READ ? "EV_READ " : " ", + ev->ev_res & EV_WRITE ? "EV_WRITE " : " ", + ev->ev_res & EV_CLOSED ? "EV_CLOSED " : " ", + ev->ev_callback)); + } else { + event_queue_remove_active(base, evcb); + event_debug(("event_process_active: event_callback %p, " + "closure %d, call %p", + evcb, evcb->evcb_closure, evcb->evcb_cb_union.evcb_callback)); + } + + if (!(evcb->evcb_flags & EVLIST_INTERNAL)) ++count; - base->current_event = evcb; -#ifndef EVENT__DISABLE_THREAD_SUPPORT + base->current_event = evcb; +#ifndef EVENT__DISABLE_THREAD_SUPPORT base->current_event_waiters = 0; #endif - switch (evcb->evcb_closure) { - case EV_CLOSURE_EVENT_SIGNAL: - EVUTIL_ASSERT(ev != NULL); + switch (evcb->evcb_closure) { + case EV_CLOSURE_EVENT_SIGNAL: + EVUTIL_ASSERT(ev != NULL); event_signal_closure(base, ev); break; - case EV_CLOSURE_EVENT_PERSIST: - EVUTIL_ASSERT(ev != NULL); + case EV_CLOSURE_EVENT_PERSIST: + EVUTIL_ASSERT(ev != NULL); event_persist_closure(base, ev); break; - case EV_CLOSURE_EVENT: { - void (*evcb_callback)(evutil_socket_t, short, void *); - short res; - EVUTIL_ASSERT(ev != NULL); - evcb_callback = *ev->ev_callback; - res = ev->ev_res; + case EV_CLOSURE_EVENT: { + void (*evcb_callback)(evutil_socket_t, short, void *); + short res; + EVUTIL_ASSERT(ev != NULL); + evcb_callback = *ev->ev_callback; + res = ev->ev_res; + EVBASE_RELEASE_LOCK(base, th_base_lock); + evcb_callback(ev->ev_fd, res, ev->ev_arg); + } + break; + case EV_CLOSURE_CB_SELF: { + void (*evcb_selfcb)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_selfcb; EVBASE_RELEASE_LOCK(base, th_base_lock); - evcb_callback(ev->ev_fd, res, ev->ev_arg); + evcb_selfcb(evcb, evcb->evcb_arg); } - break; - case EV_CLOSURE_CB_SELF: { - void (*evcb_selfcb)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_selfcb; - EVBASE_RELEASE_LOCK(base, th_base_lock); - evcb_selfcb(evcb, evcb->evcb_arg); - } - break; - case EV_CLOSURE_EVENT_FINALIZE: - case EV_CLOSURE_EVENT_FINALIZE_FREE: { - void (*evcb_evfinalize)(struct event *, void *); - int evcb_closure = evcb->evcb_closure; - EVUTIL_ASSERT(ev != NULL); - base->current_event = NULL; - evcb_evfinalize = ev->ev_evcallback.evcb_cb_union.evcb_evfinalize; - EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING)); - EVBASE_RELEASE_LOCK(base, th_base_lock); + break; + case EV_CLOSURE_EVENT_FINALIZE: + case EV_CLOSURE_EVENT_FINALIZE_FREE: { + void (*evcb_evfinalize)(struct event *, void *); + int evcb_closure = evcb->evcb_closure; + EVUTIL_ASSERT(ev != NULL); + base->current_event = NULL; + evcb_evfinalize = ev->ev_evcallback.evcb_cb_union.evcb_evfinalize; + EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING)); + EVBASE_RELEASE_LOCK(base, th_base_lock); event_debug_note_teardown_(ev); - evcb_evfinalize(ev, ev->ev_arg); - if (evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE) - mm_free(ev); - } - break; - case EV_CLOSURE_CB_FINALIZE: { - void (*evcb_cbfinalize)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_cbfinalize; - base->current_event = NULL; - EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING)); - EVBASE_RELEASE_LOCK(base, th_base_lock); - evcb_cbfinalize(evcb, evcb->evcb_arg); - } - break; - default: - EVUTIL_ASSERT(0); - } + evcb_evfinalize(ev, ev->ev_arg); + if (evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE) + mm_free(ev); + } + break; + case EV_CLOSURE_CB_FINALIZE: { + void (*evcb_cbfinalize)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_cbfinalize; + base->current_event = NULL; + EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING)); + EVBASE_RELEASE_LOCK(base, th_base_lock); + evcb_cbfinalize(evcb, evcb->evcb_arg); + } + break; + default: + EVUTIL_ASSERT(0); + } EVBASE_ACQUIRE_LOCK(base, th_base_lock); base->current_event = NULL; -#ifndef EVENT__DISABLE_THREAD_SUPPORT +#ifndef EVENT__DISABLE_THREAD_SUPPORT if (base->current_event_waiters) { base->current_event_waiters = 0; EVTHREAD_COND_BROADCAST(base->current_event_cond); @@ -1735,15 +1735,15 @@ event_process_active_single_queue(struct event_base *base, if (base->event_break) return -1; - if (count >= max_to_process) - return count; - if (count && endtime) { - struct timeval now; - update_time_cache(base); - gettime(base, &now); - if (evutil_timercmp(&now, endtime, >=)) - return count; - } + if (count >= max_to_process) + return count; + if (count && endtime) { + struct timeval now; + update_time_cache(base); + gettime(base, &now); + if (evutil_timercmp(&now, endtime, >=)) + return count; + } if (base->event_continue) break; } @@ -1760,33 +1760,33 @@ static int event_process_active(struct event_base *base) { /* Caller must hold th_base_lock */ - struct evcallback_list *activeq = NULL; + struct evcallback_list *activeq = NULL; int i, c = 0; - const struct timeval *endtime; - struct timeval tv; - const int maxcb = base->max_dispatch_callbacks; - const int limit_after_prio = base->limit_callbacks_after_prio; - if (base->max_dispatch_time.tv_sec >= 0) { - update_time_cache(base); - gettime(base, &tv); - evutil_timeradd(&base->max_dispatch_time, &tv, &tv); - endtime = &tv; - } else { - endtime = NULL; - } + const struct timeval *endtime; + struct timeval tv; + const int maxcb = base->max_dispatch_callbacks; + const int limit_after_prio = base->limit_callbacks_after_prio; + if (base->max_dispatch_time.tv_sec >= 0) { + update_time_cache(base); + gettime(base, &tv); + evutil_timeradd(&base->max_dispatch_time, &tv, &tv); + endtime = &tv; + } else { + endtime = NULL; + } for (i = 0; i < base->nactivequeues; ++i) { if (TAILQ_FIRST(&base->activequeues[i]) != NULL) { base->event_running_priority = i; activeq = &base->activequeues[i]; - if (i < limit_after_prio) - c = event_process_active_single_queue(base, activeq, - INT_MAX, NULL); - else - c = event_process_active_single_queue(base, activeq, - maxcb, endtime); + if (i < limit_after_prio) + c = event_process_active_single_queue(base, activeq, + INT_MAX, NULL); + else + c = event_process_active_single_queue(base, activeq, + maxcb, endtime); if (c < 0) { - goto done; + goto done; } else if (c > 0) break; /* Processed a real event; do not * consider lower-priority events */ @@ -1795,9 +1795,9 @@ event_process_active(struct event_base *base) } } -done: +done: base->event_running_priority = -1; - + return c; } @@ -1873,25 +1873,25 @@ event_base_loopbreak(struct event_base *event_base) } int -event_base_loopcontinue(struct event_base *event_base) -{ - int r = 0; - if (event_base == NULL) - return (-1); - - EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); - event_base->event_continue = 1; - - if (EVBASE_NEED_NOTIFY(event_base)) { - r = evthread_notify_base(event_base); - } else { - r = (0); - } - EVBASE_RELEASE_LOCK(event_base, th_base_lock); - return r; -} - -int +event_base_loopcontinue(struct event_base *event_base) +{ + int r = 0; + if (event_base == NULL) + return (-1); + + EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); + event_base->event_continue = 1; + + if (EVBASE_NEED_NOTIFY(event_base)) { + r = evthread_notify_base(event_base); + } else { + r = (0); + } + EVBASE_RELEASE_LOCK(event_base, th_base_lock); + return r; +} + +int event_base_got_break(struct event_base *event_base) { int res; @@ -1943,11 +1943,11 @@ event_base_loop(struct event_base *base, int flags) clear_time_cache(base); if (base->sig.ev_signal_added && base->sig.ev_n_signals_added) - evsig_set_base_(base); + evsig_set_base_(base); done = 0; -#ifndef EVENT__DISABLE_THREAD_SUPPORT +#ifndef EVENT__DISABLE_THREAD_SUPPORT base->th_owner_id = EVTHREAD_GET_ID(); #endif @@ -1955,7 +1955,7 @@ event_base_loop(struct event_base *base, int flags) while (!done) { base->event_continue = 0; - base->n_deferreds_queued = 0; + base->n_deferreds_queued = 0; /* Terminate the loop if we have been asked to */ if (base->event_gotterm) { @@ -1978,14 +1978,14 @@ event_base_loop(struct event_base *base, int flags) } /* If we have no events, we just exit */ - if (0==(flags&EVLOOP_NO_EXIT_ON_EMPTY) && - !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) { + if (0==(flags&EVLOOP_NO_EXIT_ON_EMPTY) && + !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) { event_debug(("%s: no events registered.", __func__)); retval = 1; goto done; } - event_queue_make_later_events_active(base); + event_queue_make_later_events_active(base); clear_time_cache(base); @@ -2030,9 +2030,9 @@ event_once_cb(evutil_socket_t fd, short events, void *arg) struct event_once *eonce = arg; (*eonce->cb)(fd, events, eonce->arg); - EVBASE_ACQUIRE_LOCK(eonce->ev.ev_base, th_base_lock); - LIST_REMOVE(eonce, next_once); - EVBASE_RELEASE_LOCK(eonce->ev.ev_base, th_base_lock); + EVBASE_ACQUIRE_LOCK(eonce->ev.ev_base, th_base_lock); + LIST_REMOVE(eonce, next_once); + EVBASE_RELEASE_LOCK(eonce->ev.ev_base, th_base_lock); event_debug_unassign(&eonce->ev); mm_free(eonce); } @@ -2054,7 +2054,7 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events, { struct event_once *eonce; int res = 0; - int activate = 0; + int activate = 0; if (!base) return (-1); @@ -2070,18 +2070,18 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events, eonce->cb = callback; eonce->arg = arg; - if ((events & (EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE|EV_CLOSED)) == EV_TIMEOUT) { - evtimer_assign(&eonce->ev, base, event_once_cb, eonce); - - if (tv == NULL || ! evutil_timerisset(tv)) { - /* If the event is going to become active immediately, - * don't put it on the timeout queue. This is one - * idiom for scheduling a callback, so let's make - * it fast (and order-preserving). */ - activate = 1; + if ((events & (EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE|EV_CLOSED)) == EV_TIMEOUT) { + evtimer_assign(&eonce->ev, base, event_once_cb, eonce); + + if (tv == NULL || ! evutil_timerisset(tv)) { + /* If the event is going to become active immediately, + * don't put it on the timeout queue. This is one + * idiom for scheduling a callback, so let's make + * it fast (and order-preserving). */ + activate = 1; } - } else if (events & (EV_READ|EV_WRITE|EV_CLOSED)) { - events &= EV_READ|EV_WRITE|EV_CLOSED; + } else if (events & (EV_READ|EV_WRITE|EV_CLOSED)) { + events &= EV_READ|EV_WRITE|EV_CLOSED; event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce); } else { @@ -2090,20 +2090,20 @@ event_base_once(struct event_base *base, evutil_socket_t fd, short events, return (-1); } - if (res == 0) { - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - if (activate) - event_active_nolock_(&eonce->ev, EV_TIMEOUT, 1); - else - res = event_add_nolock_(&eonce->ev, tv, 0); - - if (res != 0) { - mm_free(eonce); - return (res); - } else { - LIST_INSERT_HEAD(&base->once_events, eonce, next_once); - } - EVBASE_RELEASE_LOCK(base, th_base_lock); + if (res == 0) { + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + if (activate) + event_active_nolock_(&eonce->ev, EV_TIMEOUT, 1); + else + res = event_add_nolock_(&eonce->ev, tv, 0); + + if (res != 0) { + mm_free(eonce); + return (res); + } else { + LIST_INSERT_HEAD(&base->once_events, eonce, next_once); + } + EVBASE_RELEASE_LOCK(base, th_base_lock); } return (0); @@ -2114,12 +2114,12 @@ event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, shor { if (!base) base = current_base; - if (arg == &event_self_cbarg_ptr_) - arg = ev; + if (arg == &event_self_cbarg_ptr_) + arg = ev; - if (!(events & EV_SIGNAL)) - event_debug_assert_socket_nonblocking_(fd); - event_debug_assert_not_added_(ev); + if (!(events & EV_SIGNAL)) + event_debug_assert_socket_nonblocking_(fd); + event_debug_assert_not_added_(ev); ev->ev_base = base; @@ -2133,29 +2133,29 @@ event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, shor ev->ev_pncalls = NULL; if (events & EV_SIGNAL) { - if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != 0) { + if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != 0) { event_warnx("%s: EV_SIGNAL is not compatible with " - "EV_READ, EV_WRITE or EV_CLOSED", __func__); + "EV_READ, EV_WRITE or EV_CLOSED", __func__); return -1; } - ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL; + ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL; } else { if (events & EV_PERSIST) { evutil_timerclear(&ev->ev_io_timeout); - ev->ev_closure = EV_CLOSURE_EVENT_PERSIST; + ev->ev_closure = EV_CLOSURE_EVENT_PERSIST; } else { - ev->ev_closure = EV_CLOSURE_EVENT; + ev->ev_closure = EV_CLOSURE_EVENT; } } - min_heap_elem_init_(ev); + min_heap_elem_init_(ev); if (base != NULL) { /* by default, we put new events into the middle priority */ ev->ev_pri = base->nactivequeues / 2; } - event_debug_note_setup_(ev); + event_debug_note_setup_(ev); return 0; } @@ -2167,7 +2167,7 @@ event_base_set(struct event_base *base, struct event *ev) if (ev->ev_flags != EVLIST_INIT) return (-1); - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); ev->ev_base = base; ev->ev_pri = base->nactivequeues/2; @@ -2184,27 +2184,27 @@ event_set(struct event *ev, evutil_socket_t fd, short events, EVUTIL_ASSERT(r == 0); } -void * -event_self_cbarg(void) -{ - return &event_self_cbarg_ptr_; -} - +void * +event_self_cbarg(void) +{ + return &event_self_cbarg_ptr_; +} + +struct event * +event_base_get_running_event(struct event_base *base) +{ + struct event *ev = NULL; + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + if (EVBASE_IN_THREAD(base)) { + struct event_callback *evcb = base->current_event; + if (evcb->evcb_flags & EVLIST_INIT) + ev = event_callback_to_event(evcb); + } + EVBASE_RELEASE_LOCK(base, th_base_lock); + return ev; +} + struct event * -event_base_get_running_event(struct event_base *base) -{ - struct event *ev = NULL; - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - if (EVBASE_IN_THREAD(base)) { - struct event_callback *evcb = base->current_event; - if (evcb->evcb_flags & EVLIST_INIT) - ev = event_callback_to_event(evcb); - } - EVBASE_RELEASE_LOCK(base, th_base_lock); - return ev; -} - -struct event * event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg) { struct event *ev; @@ -2222,13 +2222,13 @@ event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)( void event_free(struct event *ev) { - /* This is disabled, so that events which have been finalized be a - * valid target for event_free(). That's */ - // event_debug_assert_is_setup_(ev); + /* This is disabled, so that events which have been finalized be a + * valid target for event_free(). That's */ + // event_debug_assert_is_setup_(ev); /* make sure that this event won't be coming back to haunt us. */ event_del(ev); - event_debug_note_teardown_(ev); + event_debug_note_teardown_(ev); mm_free(ev); } @@ -2236,117 +2236,117 @@ event_free(struct event *ev) void event_debug_unassign(struct event *ev) { - event_debug_assert_not_added_(ev); - event_debug_note_teardown_(ev); + event_debug_assert_not_added_(ev); + event_debug_note_teardown_(ev); ev->ev_flags &= ~EVLIST_INIT; } -#define EVENT_FINALIZE_FREE_ 0x10000 -static int -event_finalize_nolock_(struct event_base *base, unsigned flags, struct event *ev, event_finalize_callback_fn cb) -{ - ev_uint8_t closure = (flags & EVENT_FINALIZE_FREE_) ? - EV_CLOSURE_EVENT_FINALIZE_FREE : EV_CLOSURE_EVENT_FINALIZE; - - event_del_nolock_(ev, EVENT_DEL_NOBLOCK); - ev->ev_closure = closure; - ev->ev_evcallback.evcb_cb_union.evcb_evfinalize = cb; - event_active_nolock_(ev, EV_FINALIZE, 1); - ev->ev_flags |= EVLIST_FINALIZING; - return 0; -} - -static int -event_finalize_impl_(unsigned flags, struct event *ev, event_finalize_callback_fn cb) -{ - int r; - struct event_base *base = ev->ev_base; - if (EVUTIL_FAILURE_CHECK(!base)) { - event_warnx("%s: event has no event_base set.", __func__); - return -1; - } - - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - r = event_finalize_nolock_(base, flags, ev, cb); - EVBASE_RELEASE_LOCK(base, th_base_lock); - return r; -} - -int -event_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb) -{ - return event_finalize_impl_(flags, ev, cb); -} - -int -event_free_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb) -{ - return event_finalize_impl_(flags|EVENT_FINALIZE_FREE_, ev, cb); -} - -void -event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *)) -{ - struct event *ev = NULL; - if (evcb->evcb_flags & EVLIST_INIT) { - ev = event_callback_to_event(evcb); - event_del_nolock_(ev, EVENT_DEL_NOBLOCK); - } else { - event_callback_cancel_nolock_(base, evcb, 0); /*XXX can this fail?*/ - } - - evcb->evcb_closure = EV_CLOSURE_CB_FINALIZE; - evcb->evcb_cb_union.evcb_cbfinalize = cb; - event_callback_activate_nolock_(base, evcb); /* XXX can this really fail?*/ - evcb->evcb_flags |= EVLIST_FINALIZING; -} - -void -event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *)) -{ - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - event_callback_finalize_nolock_(base, flags, evcb, cb); - EVBASE_RELEASE_LOCK(base, th_base_lock); -} - -/** Internal: Finalize all of the n_cbs callbacks in evcbs. The provided - * callback will be invoked on *one of them*, after they have *all* been - * finalized. */ -int -event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcbs, void (*cb)(struct event_callback *, void *)) -{ - int n_pending = 0, i; - - if (base == NULL) - base = current_base; - - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - - event_debug(("%s: %d events finalizing", __func__, n_cbs)); - - /* At most one can be currently executing; the rest we just - * cancel... But we always make sure that the finalize callback - * runs. */ - for (i = 0; i < n_cbs; ++i) { - struct event_callback *evcb = evcbs[i]; - if (evcb == base->current_event) { - event_callback_finalize_nolock_(base, 0, evcb, cb); - ++n_pending; - } else { - event_callback_cancel_nolock_(base, evcb, 0); - } - } - - if (n_pending == 0) { - /* Just do the first one. */ - event_callback_finalize_nolock_(base, 0, evcbs[0], cb); - } - - EVBASE_RELEASE_LOCK(base, th_base_lock); - return 0; -} - +#define EVENT_FINALIZE_FREE_ 0x10000 +static int +event_finalize_nolock_(struct event_base *base, unsigned flags, struct event *ev, event_finalize_callback_fn cb) +{ + ev_uint8_t closure = (flags & EVENT_FINALIZE_FREE_) ? + EV_CLOSURE_EVENT_FINALIZE_FREE : EV_CLOSURE_EVENT_FINALIZE; + + event_del_nolock_(ev, EVENT_DEL_NOBLOCK); + ev->ev_closure = closure; + ev->ev_evcallback.evcb_cb_union.evcb_evfinalize = cb; + event_active_nolock_(ev, EV_FINALIZE, 1); + ev->ev_flags |= EVLIST_FINALIZING; + return 0; +} + +static int +event_finalize_impl_(unsigned flags, struct event *ev, event_finalize_callback_fn cb) +{ + int r; + struct event_base *base = ev->ev_base; + if (EVUTIL_FAILURE_CHECK(!base)) { + event_warnx("%s: event has no event_base set.", __func__); + return -1; + } + + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + r = event_finalize_nolock_(base, flags, ev, cb); + EVBASE_RELEASE_LOCK(base, th_base_lock); + return r; +} + +int +event_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb) +{ + return event_finalize_impl_(flags, ev, cb); +} + +int +event_free_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb) +{ + return event_finalize_impl_(flags|EVENT_FINALIZE_FREE_, ev, cb); +} + +void +event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *)) +{ + struct event *ev = NULL; + if (evcb->evcb_flags & EVLIST_INIT) { + ev = event_callback_to_event(evcb); + event_del_nolock_(ev, EVENT_DEL_NOBLOCK); + } else { + event_callback_cancel_nolock_(base, evcb, 0); /*XXX can this fail?*/ + } + + evcb->evcb_closure = EV_CLOSURE_CB_FINALIZE; + evcb->evcb_cb_union.evcb_cbfinalize = cb; + event_callback_activate_nolock_(base, evcb); /* XXX can this really fail?*/ + evcb->evcb_flags |= EVLIST_FINALIZING; +} + +void +event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *)) +{ + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + event_callback_finalize_nolock_(base, flags, evcb, cb); + EVBASE_RELEASE_LOCK(base, th_base_lock); +} + +/** Internal: Finalize all of the n_cbs callbacks in evcbs. The provided + * callback will be invoked on *one of them*, after they have *all* been + * finalized. */ +int +event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcbs, void (*cb)(struct event_callback *, void *)) +{ + int n_pending = 0, i; + + if (base == NULL) + base = current_base; + + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + + event_debug(("%s: %d events finalizing", __func__, n_cbs)); + + /* At most one can be currently executing; the rest we just + * cancel... But we always make sure that the finalize callback + * runs. */ + for (i = 0; i < n_cbs; ++i) { + struct event_callback *evcb = evcbs[i]; + if (evcb == base->current_event) { + event_callback_finalize_nolock_(base, 0, evcb, cb); + ++n_pending; + } else { + event_callback_cancel_nolock_(base, evcb, 0); + } + } + + if (n_pending == 0) { + /* Just do the first one. */ + event_callback_finalize_nolock_(base, 0, evcbs[0], cb); + } + + EVBASE_RELEASE_LOCK(base, th_base_lock); + return 0; +} + /* * Set's the priority of an event - if an event is already scheduled * changing the priority is going to fail. @@ -2355,7 +2355,7 @@ event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_c int event_priority_set(struct event *ev, int pri) { - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); if (ev->ev_flags & EVLIST_ACTIVE) return (-1); @@ -2382,16 +2382,16 @@ event_pending(const struct event *ev, short event, struct timeval *tv) } EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); if (ev->ev_flags & EVLIST_INSERTED) - flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)); - if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) + flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)); + if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) flags |= ev->ev_res; if (ev->ev_flags & EVLIST_TIMEOUT) flags |= EV_TIMEOUT; - event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL); + event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL); /* See if there is a timeout that we should report */ if (tv != NULL && (flags & event & EV_TIMEOUT)) { @@ -2418,7 +2418,7 @@ event_initialized(const struct event *ev) void 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) { - event_debug_assert_is_setup_(event); + event_debug_assert_is_setup_(event); if (base_out) *base_out = event->ev_base; @@ -2441,46 +2441,46 @@ event_get_struct_event_size(void) evutil_socket_t event_get_fd(const struct event *ev) { - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); return ev->ev_fd; } struct event_base * event_get_base(const struct event *ev) { - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); return ev->ev_base; } short event_get_events(const struct event *ev) { - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); return ev->ev_events; } event_callback_fn event_get_callback(const struct event *ev) { - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); return ev->ev_callback; } void * event_get_callback_arg(const struct event *ev) { - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); return ev->ev_arg; } int -event_get_priority(const struct event *ev) -{ - event_debug_assert_is_setup_(ev); - return ev->ev_pri; -} - -int +event_get_priority(const struct event *ev) +{ + event_debug_assert_is_setup_(ev); + return ev->ev_pri; +} + +int event_add(struct event *ev, const struct timeval *tv) { int res; @@ -2492,7 +2492,7 @@ event_add(struct event *ev, const struct timeval *tv) EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); - res = event_add_nolock_(ev, tv, 0); + res = event_add_nolock_(ev, tv, 0); EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); @@ -2509,15 +2509,15 @@ evthread_notify_base_default(struct event_base *base) char buf[1]; int r; buf[0] = (char) 0; -#ifdef _WIN32 +#ifdef _WIN32 r = send(base->th_notify_fd[1], buf, 1, 0); #else r = write(base->th_notify_fd[1], buf, 1); #endif - return (r < 0 && ! EVUTIL_ERR_IS_EAGAIN(errno)) ? -1 : 0; + return (r < 0 && ! EVUTIL_ERR_IS_EAGAIN(errno)) ? -1 : 0; } -#ifdef EVENT__HAVE_EVENTFD +#ifdef EVENT__HAVE_EVENTFD /* Helper callback: wake an event_base from another thread. This version * assumes that you have a working eventfd() implementation. */ static int @@ -2533,10 +2533,10 @@ evthread_notify_base_eventfd(struct event_base *base) } #endif - + /** Tell the thread currently running the event_loop for base (if any) that it * needs to stop waiting in its dispatch function (if it is) and process all - * active callbacks. */ + * active callbacks. */ static int evthread_notify_base(struct event_base *base) { @@ -2549,52 +2549,52 @@ evthread_notify_base(struct event_base *base) return base->th_notify_fn(base); } -/* Implementation function to remove a timeout on a currently pending event. - */ -int -event_remove_timer_nolock_(struct event *ev) -{ - struct event_base *base = ev->ev_base; - - EVENT_BASE_ASSERT_LOCKED(base); - event_debug_assert_is_setup_(ev); - - event_debug(("event_remove_timer_nolock: event: %p", ev)); - - /* If it's not pending on a timeout, we don't need to do anything. */ - if (ev->ev_flags & EVLIST_TIMEOUT) { - event_queue_remove_timeout(base, ev); - evutil_timerclear(&ev->ev_.ev_io.ev_timeout); - } - - return (0); -} - -int -event_remove_timer(struct event *ev) -{ - int res; - - if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { - event_warnx("%s: event has no event_base set.", __func__); - return -1; - } - - EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); - - res = event_remove_timer_nolock_(ev); - - EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); - - return (res); -} - +/* Implementation function to remove a timeout on a currently pending event. + */ +int +event_remove_timer_nolock_(struct event *ev) +{ + struct event_base *base = ev->ev_base; + + EVENT_BASE_ASSERT_LOCKED(base); + event_debug_assert_is_setup_(ev); + + event_debug(("event_remove_timer_nolock: event: %p", ev)); + + /* If it's not pending on a timeout, we don't need to do anything. */ + if (ev->ev_flags & EVLIST_TIMEOUT) { + event_queue_remove_timeout(base, ev); + evutil_timerclear(&ev->ev_.ev_io.ev_timeout); + } + + return (0); +} + +int +event_remove_timer(struct event *ev) +{ + int res; + + if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { + event_warnx("%s: event has no event_base set.", __func__); + return -1; + } + + EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); + + res = event_remove_timer_nolock_(ev); + + EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); + + return (res); +} + /* Implementation function to add an event. Works just like event_add, * except: 1) it requires that we have the lock. 2) if tv_is_absolute is set, * we treat tv as an absolute time, not as an interval to add to the current * time */ -int -event_add_nolock_(struct event *ev, const struct timeval *tv, +int +event_add_nolock_(struct event *ev, const struct timeval *tv, int tv_is_absolute) { struct event_base *base = ev->ev_base; @@ -2602,32 +2602,32 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, int notify = 0; EVENT_BASE_ASSERT_LOCKED(base); - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); event_debug(( - "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%s%scall %p", + "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%s%scall %p", ev, EV_SOCK_ARG(ev->ev_fd), ev->ev_events & EV_READ ? "EV_READ " : " ", ev->ev_events & EV_WRITE ? "EV_WRITE " : " ", - ev->ev_events & EV_CLOSED ? "EV_CLOSED " : " ", + ev->ev_events & EV_CLOSED ? "EV_CLOSED " : " ", tv ? "EV_TIMEOUT " : " ", ev->ev_callback)); EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL)); - if (ev->ev_flags & EVLIST_FINALIZING) { - /* XXXX debug */ - return (-1); - } - + if (ev->ev_flags & EVLIST_FINALIZING) { + /* XXXX debug */ + return (-1); + } + /* * prepare for timeout insertion further below, if we get a * failure on any step, we should not change any state. */ if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) { - if (min_heap_reserve_(&base->timeheap, - 1 + min_heap_size_(&base->timeheap)) == -1) + if (min_heap_reserve_(&base->timeheap, + 1 + min_heap_size_(&base->timeheap)) == -1) return (-1); /* ENOMEM == errno */ } @@ -2635,23 +2635,23 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, * callback, and we are not the main thread, then we want to wait * until the callback is done before we mess with the event, or else * we can race on ev_ncalls and ev_pncalls below. */ -#ifndef EVENT__DISABLE_THREAD_SUPPORT - if (base->current_event == event_to_event_callback(ev) && - (ev->ev_events & EV_SIGNAL) +#ifndef EVENT__DISABLE_THREAD_SUPPORT + if (base->current_event == event_to_event_callback(ev) && + (ev->ev_events & EV_SIGNAL) && !EVBASE_IN_THREAD(base)) { ++base->current_event_waiters; EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); } #endif - if ((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)) && - !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { - if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)) - res = evmap_io_add_(base, ev->ev_fd, ev); + if ((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)) && + !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { + if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)) + res = evmap_io_add_(base, ev->ev_fd, ev); else if (ev->ev_events & EV_SIGNAL) - res = evmap_signal_add_(base, (int)ev->ev_fd, ev); + res = evmap_signal_add_(base, (int)ev->ev_fd, ev); if (res != -1) - event_queue_insert_inserted(base, ev); + event_queue_insert_inserted(base, ev); if (res == 1) { /* evmap says we need to notify the main thread. */ notify = 1; @@ -2666,10 +2666,10 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, if (res != -1 && tv != NULL) { struct timeval now; int common_timeout; -#ifdef USE_REINSERT_TIMEOUT - int was_common; - int old_timeout_idx; -#endif +#ifdef USE_REINSERT_TIMEOUT + int was_common; + int old_timeout_idx; +#endif /* * for persistent timeout events, we remember the @@ -2677,14 +2677,14 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, * * If tv_is_absolute, this was already set. */ - if (ev->ev_closure == EV_CLOSURE_EVENT_PERSIST && !tv_is_absolute) + if (ev->ev_closure == EV_CLOSURE_EVENT_PERSIST && !tv_is_absolute) ev->ev_io_timeout = *tv; -#ifndef USE_REINSERT_TIMEOUT +#ifndef USE_REINSERT_TIMEOUT if (ev->ev_flags & EVLIST_TIMEOUT) { - event_queue_remove_timeout(base, ev); + event_queue_remove_timeout(base, ev); } -#endif +#endif /* Check if it is active due to a timeout. Rescheduling * this timeout before the callback can be executed @@ -2701,17 +2701,17 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, } } - event_queue_remove_active(base, event_to_event_callback(ev)); + event_queue_remove_active(base, event_to_event_callback(ev)); } gettime(base, &now); common_timeout = is_common_timeout(tv, base); -#ifdef USE_REINSERT_TIMEOUT - was_common = is_common_timeout(&ev->ev_timeout, base); - old_timeout_idx = COMMON_TIMEOUT_IDX(&ev->ev_timeout); -#endif - +#ifdef USE_REINSERT_TIMEOUT + was_common = is_common_timeout(&ev->ev_timeout, base); + old_timeout_idx = COMMON_TIMEOUT_IDX(&ev->ev_timeout); +#endif + if (tv_is_absolute) { ev->ev_timeout = *tv; } else if (common_timeout) { @@ -2725,15 +2725,15 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, } event_debug(( - "event_add: event %p, timeout in %d seconds %d useconds, call %p", - ev, (int)tv->tv_sec, (int)tv->tv_usec, ev->ev_callback)); - -#ifdef USE_REINSERT_TIMEOUT - event_queue_reinsert_timeout(base, ev, was_common, common_timeout, old_timeout_idx); -#else - event_queue_insert_timeout(base, ev); -#endif - + "event_add: event %p, timeout in %d seconds %d useconds, call %p", + ev, (int)tv->tv_sec, (int)tv->tv_usec, ev->ev_callback)); + +#ifdef USE_REINSERT_TIMEOUT + event_queue_reinsert_timeout(base, ev, was_common, common_timeout, old_timeout_idx); +#else + event_queue_insert_timeout(base, ev); +#endif + if (common_timeout) { struct common_timeout_list *ctl = get_common_timeout_list(base, &ev->ev_timeout); @@ -2741,18 +2741,18 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, common_timeout_schedule(ctl, &now, ev); } } else { - struct event* top = NULL; + struct event* top = NULL; /* See if the earliest timeout is now earlier than it * was before: if so, we will need to tell the main - * thread to wake up earlier than it would otherwise. - * We double check the timeout of the top element to - * handle time distortions due to system suspension. - */ - if (min_heap_elt_is_top_(ev)) + * thread to wake up earlier than it would otherwise. + * We double check the timeout of the top element to + * handle time distortions due to system suspension. + */ + if (min_heap_elt_is_top_(ev)) + notify = 1; + else if ((top = min_heap_top_(&base->timeheap)) != NULL && + evutil_timercmp(&top->ev_timeout, &now, <)) notify = 1; - else if ((top = min_heap_top_(&base->timeheap)) != NULL && - evutil_timercmp(&top->ev_timeout, &now, <)) - notify = 1; } } @@ -2760,55 +2760,55 @@ event_add_nolock_(struct event *ev, const struct timeval *tv, if (res != -1 && notify && EVBASE_NEED_NOTIFY(base)) evthread_notify_base(base); - event_debug_note_add_(ev); + event_debug_note_add_(ev); return (res); } -static int -event_del_(struct event *ev, int blocking) +static int +event_del_(struct event *ev, int blocking) { int res; - struct event_base *base = ev->ev_base; + struct event_base *base = ev->ev_base; - if (EVUTIL_FAILURE_CHECK(!base)) { + if (EVUTIL_FAILURE_CHECK(!base)) { event_warnx("%s: event has no event_base set.", __func__); return -1; } - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - res = event_del_nolock_(ev, blocking); - EVBASE_RELEASE_LOCK(base, th_base_lock); + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + res = event_del_nolock_(ev, blocking); + EVBASE_RELEASE_LOCK(base, th_base_lock); - return (res); -} + return (res); +} -int -event_del(struct event *ev) -{ - return event_del_(ev, EVENT_DEL_AUTOBLOCK); -} +int +event_del(struct event *ev) +{ + return event_del_(ev, EVENT_DEL_AUTOBLOCK); +} -int -event_del_block(struct event *ev) -{ - return event_del_(ev, EVENT_DEL_BLOCK); +int +event_del_block(struct event *ev) +{ + return event_del_(ev, EVENT_DEL_BLOCK); } -int -event_del_noblock(struct event *ev) +int +event_del_noblock(struct event *ev) +{ + return event_del_(ev, EVENT_DEL_NOBLOCK); +} + +/** Helper for event_del: always called with th_base_lock held. + * + * "blocking" must be one of the EVENT_DEL_{BLOCK, NOBLOCK, AUTOBLOCK, + * EVEN_IF_FINALIZING} values. See those for more information. + */ +int +event_del_nolock_(struct event *ev, int blocking) { - return event_del_(ev, EVENT_DEL_NOBLOCK); -} - -/** Helper for event_del: always called with th_base_lock held. - * - * "blocking" must be one of the EVENT_DEL_{BLOCK, NOBLOCK, AUTOBLOCK, - * EVEN_IF_FINALIZING} values. See those for more information. - */ -int -event_del_nolock_(struct event *ev, int blocking) -{ struct event_base *base; int res = 0, notify = 0; @@ -2821,15 +2821,15 @@ event_del_nolock_(struct event *ev, int blocking) EVENT_BASE_ASSERT_LOCKED(ev->ev_base); - if (blocking != EVENT_DEL_EVEN_IF_FINALIZING) { - if (ev->ev_flags & EVLIST_FINALIZING) { - /* XXXX Debug */ - return 0; - } + if (blocking != EVENT_DEL_EVEN_IF_FINALIZING) { + if (ev->ev_flags & EVLIST_FINALIZING) { + /* XXXX Debug */ + return 0; + } } - base = ev->ev_base; - + base = ev->ev_base; + EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL)); /* See if we are just active executing this event in a loop */ @@ -2848,52 +2848,52 @@ event_del_nolock_(struct event *ev, int blocking) * dispatch loop early anyway, so we wouldn't gain anything by * doing it. */ - event_queue_remove_timeout(base, ev); + event_queue_remove_timeout(base, ev); } if (ev->ev_flags & EVLIST_ACTIVE) - event_queue_remove_active(base, event_to_event_callback(ev)); - else if (ev->ev_flags & EVLIST_ACTIVE_LATER) - event_queue_remove_active_later(base, event_to_event_callback(ev)); + event_queue_remove_active(base, event_to_event_callback(ev)); + else if (ev->ev_flags & EVLIST_ACTIVE_LATER) + event_queue_remove_active_later(base, event_to_event_callback(ev)); if (ev->ev_flags & EVLIST_INSERTED) { - event_queue_remove_inserted(base, ev); - if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)) - res = evmap_io_del_(base, ev->ev_fd, ev); + event_queue_remove_inserted(base, ev); + if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)) + res = evmap_io_del_(base, ev->ev_fd, ev); else - res = evmap_signal_del_(base, (int)ev->ev_fd, ev); + res = evmap_signal_del_(base, (int)ev->ev_fd, ev); if (res == 1) { /* evmap says we need to notify the main thread. */ notify = 1; res = 0; } - /* If we do not have events, let's notify event base so it can - * exit without waiting */ - if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) - notify = 1; + /* If we do not have events, let's notify event base so it can + * exit without waiting */ + if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) + notify = 1; } /* if we are not in the right thread, we need to wake up the loop */ if (res != -1 && notify && EVBASE_NEED_NOTIFY(base)) evthread_notify_base(base); - event_debug_note_del_(ev); - - /* If the main thread is currently executing this event's callback, - * and we are not the main thread, then we want to wait until the - * callback is done before returning. That way, when this function - * returns, it will be safe to free the user-supplied argument. - */ -#ifndef EVENT__DISABLE_THREAD_SUPPORT - if (blocking != EVENT_DEL_NOBLOCK && - base->current_event == event_to_event_callback(ev) && - !EVBASE_IN_THREAD(base) && - (blocking == EVENT_DEL_BLOCK || !(ev->ev_events & EV_FINALIZE))) { - ++base->current_event_waiters; - EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); - } -#endif - + event_debug_note_del_(ev); + + /* If the main thread is currently executing this event's callback, + * and we are not the main thread, then we want to wait until the + * callback is done before returning. That way, when this function + * returns, it will be safe to free the user-supplied argument. + */ +#ifndef EVENT__DISABLE_THREAD_SUPPORT + if (blocking != EVENT_DEL_NOBLOCK && + base->current_event == event_to_event_callback(ev) && + !EVBASE_IN_THREAD(base) && + (blocking == EVENT_DEL_BLOCK || !(ev->ev_events & EV_FINALIZE))) { + ++base->current_event_waiters; + EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); + } +#endif + return (res); } @@ -2907,54 +2907,54 @@ event_active(struct event *ev, int res, short ncalls) EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); - event_debug_assert_is_setup_(ev); + event_debug_assert_is_setup_(ev); - event_active_nolock_(ev, res, ncalls); + event_active_nolock_(ev, res, ncalls); EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); } void -event_active_nolock_(struct event *ev, int res, short ncalls) +event_active_nolock_(struct event *ev, int res, short ncalls) { struct event_base *base; event_debug(("event_active: %p (fd "EV_SOCK_FMT"), res %d, callback %p", ev, EV_SOCK_ARG(ev->ev_fd), (int)res, ev->ev_callback)); - base = ev->ev_base; - EVENT_BASE_ASSERT_LOCKED(base); - - if (ev->ev_flags & EVLIST_FINALIZING) { - /* XXXX debug */ - return; - } - - switch ((ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { - default: - case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER: - EVUTIL_ASSERT(0); - break; - case EVLIST_ACTIVE: - /* We get different kinds of events, add them together */ + base = ev->ev_base; + EVENT_BASE_ASSERT_LOCKED(base); + + if (ev->ev_flags & EVLIST_FINALIZING) { + /* XXXX debug */ + return; + } + + switch ((ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { + default: + case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER: + EVUTIL_ASSERT(0); + break; + case EVLIST_ACTIVE: + /* We get different kinds of events, add them together */ ev->ev_res |= res; return; - case EVLIST_ACTIVE_LATER: - ev->ev_res |= res; - break; - case 0: - ev->ev_res = res; - break; + case EVLIST_ACTIVE_LATER: + ev->ev_res |= res; + break; + case 0: + ev->ev_res = res; + break; } if (ev->ev_pri < base->event_running_priority) base->event_continue = 1; if (ev->ev_events & EV_SIGNAL) { -#ifndef EVENT__DISABLE_THREAD_SUPPORT - if (base->current_event == event_to_event_callback(ev) && - !EVBASE_IN_THREAD(base)) { +#ifndef EVENT__DISABLE_THREAD_SUPPORT + if (base->current_event == event_to_event_callback(ev) && + !EVBASE_IN_THREAD(base)) { ++base->current_event_waiters; EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); } @@ -2963,180 +2963,180 @@ event_active_nolock_(struct event *ev, int res, short ncalls) ev->ev_pncalls = NULL; } - event_callback_activate_nolock_(base, event_to_event_callback(ev)); + event_callback_activate_nolock_(base, event_to_event_callback(ev)); +} + +void +event_active_later_(struct event *ev, int res) +{ + EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); + event_active_later_nolock_(ev, res); + EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); +} + +void +event_active_later_nolock_(struct event *ev, int res) +{ + struct event_base *base = ev->ev_base; + EVENT_BASE_ASSERT_LOCKED(base); + + if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) { + /* We get different kinds of events, add them together */ + ev->ev_res |= res; + return; + } + + ev->ev_res = res; + + event_callback_activate_later_nolock_(base, event_to_event_callback(ev)); +} + +int +event_callback_activate_(struct event_base *base, + struct event_callback *evcb) +{ + int r; + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + r = event_callback_activate_nolock_(base, evcb); + EVBASE_RELEASE_LOCK(base, th_base_lock); + return r; +} + +int +event_callback_activate_nolock_(struct event_base *base, + struct event_callback *evcb) +{ + int r = 1; + + if (evcb->evcb_flags & EVLIST_FINALIZING) + return 0; + + switch (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) { + default: + EVUTIL_ASSERT(0); + EVUTIL_FALLTHROUGH; + case EVLIST_ACTIVE_LATER: + event_queue_remove_active_later(base, evcb); + r = 0; + break; + case EVLIST_ACTIVE: + return 0; + case 0: + break; + } + + event_queue_insert_active(base, evcb); + + if (EVBASE_NEED_NOTIFY(base)) + evthread_notify_base(base); + + return r; +} + +int +event_callback_activate_later_nolock_(struct event_base *base, + struct event_callback *evcb) +{ + if (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) + return 0; + + event_queue_insert_active_later(base, evcb); + if (EVBASE_NEED_NOTIFY(base)) + evthread_notify_base(base); + return 1; +} + +void +event_callback_init_(struct event_base *base, + struct event_callback *cb) +{ + memset(cb, 0, sizeof(*cb)); + cb->evcb_pri = base->nactivequeues - 1; +} + +int +event_callback_cancel_(struct event_base *base, + struct event_callback *evcb) +{ + int r; + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + r = event_callback_cancel_nolock_(base, evcb, 0); + EVBASE_RELEASE_LOCK(base, th_base_lock); + return r; +} + +int +event_callback_cancel_nolock_(struct event_base *base, + struct event_callback *evcb, int even_if_finalizing) +{ + if ((evcb->evcb_flags & EVLIST_FINALIZING) && !even_if_finalizing) + return 0; + + if (evcb->evcb_flags & EVLIST_INIT) + return event_del_nolock_(event_callback_to_event(evcb), + even_if_finalizing ? EVENT_DEL_EVEN_IF_FINALIZING : EVENT_DEL_AUTOBLOCK); + + switch ((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { + default: + case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER: + EVUTIL_ASSERT(0); + break; + case EVLIST_ACTIVE: + /* We get different kinds of events, add them together */ + event_queue_remove_active(base, evcb); + return 0; + case EVLIST_ACTIVE_LATER: + event_queue_remove_active_later(base, evcb); + break; + case 0: + break; + } + + return 0; } void -event_active_later_(struct event *ev, int res) +event_deferred_cb_init_(struct event_callback *cb, ev_uint8_t priority, deferred_cb_fn fn, void *arg) { - EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); - event_active_later_nolock_(ev, res); - EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); + memset(cb, 0, sizeof(*cb)); + cb->evcb_cb_union.evcb_selfcb = fn; + cb->evcb_arg = arg; + cb->evcb_pri = priority; + cb->evcb_closure = EV_CLOSURE_CB_SELF; } void -event_active_later_nolock_(struct event *ev, int res) -{ - struct event_base *base = ev->ev_base; - EVENT_BASE_ASSERT_LOCKED(base); - - if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) { - /* We get different kinds of events, add them together */ - ev->ev_res |= res; - return; - } - - ev->ev_res = res; - - event_callback_activate_later_nolock_(base, event_to_event_callback(ev)); -} - -int -event_callback_activate_(struct event_base *base, - struct event_callback *evcb) -{ - int r; - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - r = event_callback_activate_nolock_(base, evcb); - EVBASE_RELEASE_LOCK(base, th_base_lock); - return r; -} - -int -event_callback_activate_nolock_(struct event_base *base, - struct event_callback *evcb) -{ - int r = 1; - - if (evcb->evcb_flags & EVLIST_FINALIZING) - return 0; - - switch (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) { - default: - EVUTIL_ASSERT(0); - EVUTIL_FALLTHROUGH; - case EVLIST_ACTIVE_LATER: - event_queue_remove_active_later(base, evcb); - r = 0; - break; - case EVLIST_ACTIVE: - return 0; - case 0: - break; - } - - event_queue_insert_active(base, evcb); - - if (EVBASE_NEED_NOTIFY(base)) - evthread_notify_base(base); - - return r; -} - -int -event_callback_activate_later_nolock_(struct event_base *base, - struct event_callback *evcb) -{ - if (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) - return 0; - - event_queue_insert_active_later(base, evcb); - if (EVBASE_NEED_NOTIFY(base)) - evthread_notify_base(base); - return 1; -} - +event_deferred_cb_set_priority_(struct event_callback *cb, ev_uint8_t priority) +{ + cb->evcb_pri = priority; +} + void -event_callback_init_(struct event_base *base, - struct event_callback *cb) -{ - memset(cb, 0, sizeof(*cb)); - cb->evcb_pri = base->nactivequeues - 1; -} - -int -event_callback_cancel_(struct event_base *base, - struct event_callback *evcb) -{ - int r; - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - r = event_callback_cancel_nolock_(base, evcb, 0); - EVBASE_RELEASE_LOCK(base, th_base_lock); - return r; -} - -int -event_callback_cancel_nolock_(struct event_base *base, - struct event_callback *evcb, int even_if_finalizing) -{ - if ((evcb->evcb_flags & EVLIST_FINALIZING) && !even_if_finalizing) - return 0; - - if (evcb->evcb_flags & EVLIST_INIT) - return event_del_nolock_(event_callback_to_event(evcb), - even_if_finalizing ? EVENT_DEL_EVEN_IF_FINALIZING : EVENT_DEL_AUTOBLOCK); - - switch ((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { - default: - case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER: - EVUTIL_ASSERT(0); - break; - case EVLIST_ACTIVE: - /* We get different kinds of events, add them together */ - event_queue_remove_active(base, evcb); - return 0; - case EVLIST_ACTIVE_LATER: - event_queue_remove_active_later(base, evcb); - break; - case 0: - break; - } - - return 0; -} - -void -event_deferred_cb_init_(struct event_callback *cb, ev_uint8_t priority, deferred_cb_fn fn, void *arg) -{ - memset(cb, 0, sizeof(*cb)); - cb->evcb_cb_union.evcb_selfcb = fn; - cb->evcb_arg = arg; - cb->evcb_pri = priority; - cb->evcb_closure = EV_CLOSURE_CB_SELF; -} - -void -event_deferred_cb_set_priority_(struct event_callback *cb, ev_uint8_t priority) -{ - cb->evcb_pri = priority; -} - -void -event_deferred_cb_cancel_(struct event_base *base, struct event_callback *cb) -{ - if (!base) - base = current_base; - event_callback_cancel_(base, cb); -} - -#define MAX_DEFERREDS_QUEUED 32 -int -event_deferred_cb_schedule_(struct event_base *base, struct event_callback *cb) -{ - int r = 1; - if (!base) - base = current_base; - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - if (base->n_deferreds_queued > MAX_DEFERREDS_QUEUED) { - r = event_callback_activate_later_nolock_(base, cb); - } else { - r = event_callback_activate_nolock_(base, cb); - if (r) { - ++base->n_deferreds_queued; - } - } - EVBASE_RELEASE_LOCK(base, th_base_lock); - return r; +event_deferred_cb_cancel_(struct event_base *base, struct event_callback *cb) +{ + if (!base) + base = current_base; + event_callback_cancel_(base, cb); +} + +#define MAX_DEFERREDS_QUEUED 32 +int +event_deferred_cb_schedule_(struct event_base *base, struct event_callback *cb) +{ + int r = 1; + if (!base) + base = current_base; + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + if (base->n_deferreds_queued > MAX_DEFERREDS_QUEUED) { + r = event_callback_activate_later_nolock_(base, cb); + } else { + r = event_callback_activate_nolock_(base, cb); + if (r) { + ++base->n_deferreds_queued; + } + } + EVBASE_RELEASE_LOCK(base, th_base_lock); + return r; } static int @@ -3148,7 +3148,7 @@ timeout_next(struct event_base *base, struct timeval **tv_p) struct timeval *tv = *tv_p; int res = 0; - ev = min_heap_top_(&base->timeheap); + ev = min_heap_top_(&base->timeheap); if (ev == NULL) { /* if no time-based events are active wait for I/O */ @@ -3170,7 +3170,7 @@ timeout_next(struct event_base *base, struct timeval **tv_p) EVUTIL_ASSERT(tv->tv_sec >= 0); EVUTIL_ASSERT(tv->tv_usec >= 0); - event_debug(("timeout_next: event: %p, in %d seconds, %d useconds", ev, (int)tv->tv_sec, (int)tv->tv_usec)); + event_debug(("timeout_next: event: %p, in %d seconds, %d useconds", ev, (int)tv->tv_sec, (int)tv->tv_usec)); out: return (res); @@ -3184,147 +3184,147 @@ timeout_process(struct event_base *base) struct timeval now; struct event *ev; - if (min_heap_empty_(&base->timeheap)) { + if (min_heap_empty_(&base->timeheap)) { return; } gettime(base, &now); - while ((ev = min_heap_top_(&base->timeheap))) { + while ((ev = min_heap_top_(&base->timeheap))) { if (evutil_timercmp(&ev->ev_timeout, &now, >)) break; /* delete this event from the I/O queues */ - event_del_nolock_(ev, EVENT_DEL_NOBLOCK); - - event_debug(("timeout_process: event: %p, call %p", - ev, ev->ev_callback)); - event_active_nolock_(ev, EV_TIMEOUT, 1); - } -} - -#ifndef MAX -#define MAX(a,b) (((a)>(b))?(a):(b)) -#endif - -#define MAX_EVENT_COUNT(var, v) var = MAX(var, v) - -/* These are a fancy way to spell - if (~flags & EVLIST_INTERNAL) - base->event_count--/++; -*/ -#define DECR_EVENT_COUNT(base,flags) \ - ((base)->event_count -= !((flags) & EVLIST_INTERNAL)) -#define INCR_EVENT_COUNT(base,flags) do { \ - ((base)->event_count += !((flags) & EVLIST_INTERNAL)); \ - MAX_EVENT_COUNT((base)->event_count_max, (base)->event_count); \ -} while (0) - + event_del_nolock_(ev, EVENT_DEL_NOBLOCK); + + event_debug(("timeout_process: event: %p, call %p", + ev, ev->ev_callback)); + event_active_nolock_(ev, EV_TIMEOUT, 1); + } +} + +#ifndef MAX +#define MAX(a,b) (((a)>(b))?(a):(b)) +#endif + +#define MAX_EVENT_COUNT(var, v) var = MAX(var, v) + +/* These are a fancy way to spell + if (~flags & EVLIST_INTERNAL) + base->event_count--/++; +*/ +#define DECR_EVENT_COUNT(base,flags) \ + ((base)->event_count -= !((flags) & EVLIST_INTERNAL)) +#define INCR_EVENT_COUNT(base,flags) do { \ + ((base)->event_count += !((flags) & EVLIST_INTERNAL)); \ + MAX_EVENT_COUNT((base)->event_count_max, (base)->event_count); \ +} while (0) + +static void +event_queue_remove_inserted(struct event_base *base, struct event *ev) +{ + EVENT_BASE_ASSERT_LOCKED(base); + if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_INSERTED))) { + event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__, + ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_INSERTED); + return; + } + DECR_EVENT_COUNT(base, ev->ev_flags); + ev->ev_flags &= ~EVLIST_INSERTED; +} +static void +event_queue_remove_active(struct event_base *base, struct event_callback *evcb) +{ + EVENT_BASE_ASSERT_LOCKED(base); + if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE))) { + event_errx(1, "%s: %p not on queue %x", __func__, + evcb, EVLIST_ACTIVE); + return; + } + DECR_EVENT_COUNT(base, evcb->evcb_flags); + evcb->evcb_flags &= ~EVLIST_ACTIVE; + base->event_count_active--; + + TAILQ_REMOVE(&base->activequeues[evcb->evcb_pri], + evcb, evcb_active_next); +} +static void +event_queue_remove_active_later(struct event_base *base, struct event_callback *evcb) +{ + EVENT_BASE_ASSERT_LOCKED(base); + if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE_LATER))) { + event_errx(1, "%s: %p not on queue %x", __func__, + evcb, EVLIST_ACTIVE_LATER); + return; + } + DECR_EVENT_COUNT(base, evcb->evcb_flags); + evcb->evcb_flags &= ~EVLIST_ACTIVE_LATER; + base->event_count_active--; + + TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next); +} static void -event_queue_remove_inserted(struct event_base *base, struct event *ev) +event_queue_remove_timeout(struct event_base *base, struct event *ev) { EVENT_BASE_ASSERT_LOCKED(base); - if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_INSERTED))) { - event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__, - ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_INSERTED); - return; - } - DECR_EVENT_COUNT(base, ev->ev_flags); - ev->ev_flags &= ~EVLIST_INSERTED; -} -static void -event_queue_remove_active(struct event_base *base, struct event_callback *evcb) -{ - EVENT_BASE_ASSERT_LOCKED(base); - if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE))) { - event_errx(1, "%s: %p not on queue %x", __func__, - evcb, EVLIST_ACTIVE); - return; - } - DECR_EVENT_COUNT(base, evcb->evcb_flags); - evcb->evcb_flags &= ~EVLIST_ACTIVE; - base->event_count_active--; - - TAILQ_REMOVE(&base->activequeues[evcb->evcb_pri], - evcb, evcb_active_next); -} -static void -event_queue_remove_active_later(struct event_base *base, struct event_callback *evcb) -{ - EVENT_BASE_ASSERT_LOCKED(base); - if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE_LATER))) { - event_errx(1, "%s: %p not on queue %x", __func__, - evcb, EVLIST_ACTIVE_LATER); - return; - } - DECR_EVENT_COUNT(base, evcb->evcb_flags); - evcb->evcb_flags &= ~EVLIST_ACTIVE_LATER; - base->event_count_active--; - - TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next); -} -static void -event_queue_remove_timeout(struct event_base *base, struct event *ev) -{ - EVENT_BASE_ASSERT_LOCKED(base); - if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_TIMEOUT))) { + if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_TIMEOUT))) { event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__, - ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_TIMEOUT); + ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_TIMEOUT); + return; + } + DECR_EVENT_COUNT(base, ev->ev_flags); + ev->ev_flags &= ~EVLIST_TIMEOUT; + + if (is_common_timeout(&ev->ev_timeout, base)) { + struct common_timeout_list *ctl = + get_common_timeout_list(base, &ev->ev_timeout); + TAILQ_REMOVE(&ctl->events, ev, + ev_timeout_pos.ev_next_with_common_timeout); + } else { + min_heap_erase_(&base->timeheap, ev); + } +} + +#ifdef USE_REINSERT_TIMEOUT +/* Remove and reinsert 'ev' into the timeout queue. */ +static void +event_queue_reinsert_timeout(struct event_base *base, struct event *ev, + int was_common, int is_common, int old_timeout_idx) +{ + struct common_timeout_list *ctl; + if (!(ev->ev_flags & EVLIST_TIMEOUT)) { + event_queue_insert_timeout(base, ev); return; } - DECR_EVENT_COUNT(base, ev->ev_flags); - ev->ev_flags &= ~EVLIST_TIMEOUT; - - if (is_common_timeout(&ev->ev_timeout, base)) { - struct common_timeout_list *ctl = - get_common_timeout_list(base, &ev->ev_timeout); - TAILQ_REMOVE(&ctl->events, ev, - ev_timeout_pos.ev_next_with_common_timeout); - } else { - min_heap_erase_(&base->timeheap, ev); - } -} - -#ifdef USE_REINSERT_TIMEOUT -/* Remove and reinsert 'ev' into the timeout queue. */ -static void -event_queue_reinsert_timeout(struct event_base *base, struct event *ev, - int was_common, int is_common, int old_timeout_idx) -{ - struct common_timeout_list *ctl; - if (!(ev->ev_flags & EVLIST_TIMEOUT)) { - event_queue_insert_timeout(base, ev); - return; - } - - switch ((was_common<<1) | is_common) { - case 3: /* Changing from one common timeout to another */ - ctl = base->common_timeout_queues[old_timeout_idx]; - TAILQ_REMOVE(&ctl->events, ev, - ev_timeout_pos.ev_next_with_common_timeout); - ctl = get_common_timeout_list(base, &ev->ev_timeout); - insert_common_timeout_inorder(ctl, ev); + + switch ((was_common<<1) | is_common) { + case 3: /* Changing from one common timeout to another */ + ctl = base->common_timeout_queues[old_timeout_idx]; + TAILQ_REMOVE(&ctl->events, ev, + ev_timeout_pos.ev_next_with_common_timeout); + ctl = get_common_timeout_list(base, &ev->ev_timeout); + insert_common_timeout_inorder(ctl, ev); + break; + case 2: /* Was common; is no longer common */ + ctl = base->common_timeout_queues[old_timeout_idx]; + TAILQ_REMOVE(&ctl->events, ev, + ev_timeout_pos.ev_next_with_common_timeout); + min_heap_push_(&base->timeheap, ev); break; - case 2: /* Was common; is no longer common */ - ctl = base->common_timeout_queues[old_timeout_idx]; - TAILQ_REMOVE(&ctl->events, ev, - ev_timeout_pos.ev_next_with_common_timeout); - min_heap_push_(&base->timeheap, ev); + case 1: /* Wasn't common; has become common. */ + min_heap_erase_(&base->timeheap, ev); + ctl = get_common_timeout_list(base, &ev->ev_timeout); + insert_common_timeout_inorder(ctl, ev); break; - case 1: /* Wasn't common; has become common. */ - min_heap_erase_(&base->timeheap, ev); - ctl = get_common_timeout_list(base, &ev->ev_timeout); - insert_common_timeout_inorder(ctl, ev); + case 0: /* was in heap; is still on heap. */ + min_heap_adjust_(&base->timeheap, ev); break; - case 0: /* was in heap; is still on heap. */ - min_heap_adjust_(&base->timeheap, ev); - break; default: - EVUTIL_ASSERT(0); /* unreachable */ - break; + EVUTIL_ASSERT(0); /* unreachable */ + break; } } -#endif +#endif /* Add 'ev' to the common timeout list in 'ev'. */ static void @@ -3358,110 +3358,110 @@ insert_common_timeout_inorder(struct common_timeout_list *ctl, } static void -event_queue_insert_inserted(struct event_base *base, struct event *ev) +event_queue_insert_inserted(struct event_base *base, struct event *ev) +{ + EVENT_BASE_ASSERT_LOCKED(base); + + if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_INSERTED)) { + event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already inserted", __func__, + ev, EV_SOCK_ARG(ev->ev_fd)); + return; + } + + INCR_EVENT_COUNT(base, ev->ev_flags); + + ev->ev_flags |= EVLIST_INSERTED; +} + +static void +event_queue_insert_active(struct event_base *base, struct event_callback *evcb) { EVENT_BASE_ASSERT_LOCKED(base); - if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_INSERTED)) { - event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already inserted", __func__, - ev, EV_SOCK_ARG(ev->ev_fd)); - return; - } - - INCR_EVENT_COUNT(base, ev->ev_flags); - - ev->ev_flags |= EVLIST_INSERTED; -} - -static void -event_queue_insert_active(struct event_base *base, struct event_callback *evcb) -{ - EVENT_BASE_ASSERT_LOCKED(base); - - if (evcb->evcb_flags & EVLIST_ACTIVE) { + if (evcb->evcb_flags & EVLIST_ACTIVE) { /* Double insertion is possible for active events */ - return; - } - - INCR_EVENT_COUNT(base, evcb->evcb_flags); - - evcb->evcb_flags |= EVLIST_ACTIVE; - - base->event_count_active++; - MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active); - EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); - TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], - evcb, evcb_active_next); -} - -static void -event_queue_insert_active_later(struct event_base *base, struct event_callback *evcb) -{ - EVENT_BASE_ASSERT_LOCKED(base); - if (evcb->evcb_flags & (EVLIST_ACTIVE_LATER|EVLIST_ACTIVE)) { - /* Double insertion is possible */ return; } - INCR_EVENT_COUNT(base, evcb->evcb_flags); - evcb->evcb_flags |= EVLIST_ACTIVE_LATER; - base->event_count_active++; - MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active); - EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); - TAILQ_INSERT_TAIL(&base->active_later_queue, evcb, evcb_active_next); -} - -static void -event_queue_insert_timeout(struct event_base *base, struct event *ev) -{ - EVENT_BASE_ASSERT_LOCKED(base); - - if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_TIMEOUT)) { - event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on timeout", __func__, - ev, EV_SOCK_ARG(ev->ev_fd)); - return; - } - - INCR_EVENT_COUNT(base, ev->ev_flags); - - ev->ev_flags |= EVLIST_TIMEOUT; - - if (is_common_timeout(&ev->ev_timeout, base)) { - struct common_timeout_list *ctl = - get_common_timeout_list(base, &ev->ev_timeout); - insert_common_timeout_inorder(ctl, ev); - } else { - min_heap_push_(&base->timeheap, ev); - } -} - -static void -event_queue_make_later_events_active(struct event_base *base) -{ - struct event_callback *evcb; - EVENT_BASE_ASSERT_LOCKED(base); - - while ((evcb = TAILQ_FIRST(&base->active_later_queue))) { - TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next); - evcb->evcb_flags = (evcb->evcb_flags & ~EVLIST_ACTIVE_LATER) | EVLIST_ACTIVE; - EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); - TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], evcb, evcb_active_next); - base->n_deferreds_queued += (evcb->evcb_closure == EV_CLOSURE_CB_SELF); - } -} - + INCR_EVENT_COUNT(base, evcb->evcb_flags); + + evcb->evcb_flags |= EVLIST_ACTIVE; + + base->event_count_active++; + MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active); + EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); + TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], + evcb, evcb_active_next); +} + +static void +event_queue_insert_active_later(struct event_base *base, struct event_callback *evcb) +{ + EVENT_BASE_ASSERT_LOCKED(base); + if (evcb->evcb_flags & (EVLIST_ACTIVE_LATER|EVLIST_ACTIVE)) { + /* Double insertion is possible */ + return; + } + + INCR_EVENT_COUNT(base, evcb->evcb_flags); + evcb->evcb_flags |= EVLIST_ACTIVE_LATER; + base->event_count_active++; + MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active); + EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); + TAILQ_INSERT_TAIL(&base->active_later_queue, evcb, evcb_active_next); +} + +static void +event_queue_insert_timeout(struct event_base *base, struct event *ev) +{ + EVENT_BASE_ASSERT_LOCKED(base); + + if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_TIMEOUT)) { + event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on timeout", __func__, + ev, EV_SOCK_ARG(ev->ev_fd)); + return; + } + + INCR_EVENT_COUNT(base, ev->ev_flags); + + ev->ev_flags |= EVLIST_TIMEOUT; + + if (is_common_timeout(&ev->ev_timeout, base)) { + struct common_timeout_list *ctl = + get_common_timeout_list(base, &ev->ev_timeout); + insert_common_timeout_inorder(ctl, ev); + } else { + min_heap_push_(&base->timeheap, ev); + } +} + +static void +event_queue_make_later_events_active(struct event_base *base) +{ + struct event_callback *evcb; + EVENT_BASE_ASSERT_LOCKED(base); + + while ((evcb = TAILQ_FIRST(&base->active_later_queue))) { + TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next); + evcb->evcb_flags = (evcb->evcb_flags & ~EVLIST_ACTIVE_LATER) | EVLIST_ACTIVE; + EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); + TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], evcb, evcb_active_next); + base->n_deferreds_queued += (evcb->evcb_closure == EV_CLOSURE_CB_SELF); + } +} + /* Functions for debugging */ const char * event_get_version(void) { - return (EVENT__VERSION); + return (EVENT__VERSION); } ev_uint32_t event_get_version_number(void) { - return (EVENT__NUMERIC_VERSION); + return (EVENT__NUMERIC_VERSION); } /* @@ -3475,19 +3475,19 @@ event_get_method(void) return (current_base->evsel->name); } -#ifndef EVENT__DISABLE_MM_REPLACEMENT -static void *(*mm_malloc_fn_)(size_t sz) = NULL; -static void *(*mm_realloc_fn_)(void *p, size_t sz) = NULL; -static void (*mm_free_fn_)(void *p) = NULL; +#ifndef EVENT__DISABLE_MM_REPLACEMENT +static void *(*mm_malloc_fn_)(size_t sz) = NULL; +static void *(*mm_realloc_fn_)(void *p, size_t sz) = NULL; +static void (*mm_free_fn_)(void *p) = NULL; void * event_mm_malloc_(size_t sz) { - if (sz == 0) - return NULL; - - if (mm_malloc_fn_) - return mm_malloc_fn_(sz); + if (sz == 0) + return NULL; + + if (mm_malloc_fn_) + return mm_malloc_fn_(sz); else return malloc(sz); } @@ -3495,65 +3495,65 @@ event_mm_malloc_(size_t sz) void * event_mm_calloc_(size_t count, size_t size) { - if (count == 0 || size == 0) - return NULL; - - if (mm_malloc_fn_) { + if (count == 0 || size == 0) + return NULL; + + if (mm_malloc_fn_) { size_t sz = count * size; - void *p = NULL; - if (count > EV_SIZE_MAX / size) - goto error; - p = mm_malloc_fn_(sz); + void *p = NULL; + if (count > EV_SIZE_MAX / size) + goto error; + p = mm_malloc_fn_(sz); if (p) - return memset(p, 0, sz); - } else { - void *p = calloc(count, size); -#ifdef _WIN32 - /* Windows calloc doesn't reliably set ENOMEM */ - if (p == NULL) - goto error; -#endif + return memset(p, 0, sz); + } else { + void *p = calloc(count, size); +#ifdef _WIN32 + /* Windows calloc doesn't reliably set ENOMEM */ + if (p == NULL) + goto error; +#endif return p; - } - -error: - errno = ENOMEM; - return NULL; + } + +error: + errno = ENOMEM; + return NULL; } char * event_mm_strdup_(const char *str) { - if (!str) { - errno = EINVAL; - return NULL; - } - - if (mm_malloc_fn_) { + if (!str) { + errno = EINVAL; + return NULL; + } + + if (mm_malloc_fn_) { size_t ln = strlen(str); - void *p = NULL; - if (ln == EV_SIZE_MAX) - goto error; - p = mm_malloc_fn_(ln+1); + void *p = NULL; + if (ln == EV_SIZE_MAX) + goto error; + p = mm_malloc_fn_(ln+1); if (p) - return memcpy(p, str, ln+1); + return memcpy(p, str, ln+1); } else -#ifdef _WIN32 +#ifdef _WIN32 return _strdup(str); #else return strdup(str); #endif - -error: - errno = ENOMEM; - return NULL; + +error: + errno = ENOMEM; + return NULL; } void * event_mm_realloc_(void *ptr, size_t sz) { - if (mm_realloc_fn_) - return mm_realloc_fn_(ptr, sz); + if (mm_realloc_fn_) + return mm_realloc_fn_(ptr, sz); else return realloc(ptr, sz); } @@ -3561,8 +3561,8 @@ event_mm_realloc_(void *ptr, size_t sz) void event_mm_free_(void *ptr) { - if (mm_free_fn_) - mm_free_fn_(ptr); + if (mm_free_fn_) + mm_free_fn_(ptr); else free(ptr); } @@ -3572,13 +3572,13 @@ event_set_mem_functions(void *(*malloc_fn)(size_t sz), void *(*realloc_fn)(void *ptr, size_t sz), void (*free_fn)(void *ptr)) { - mm_malloc_fn_ = malloc_fn; - mm_realloc_fn_ = realloc_fn; - mm_free_fn_ = free_fn; + mm_malloc_fn_ = malloc_fn; + mm_realloc_fn_ = realloc_fn; + mm_free_fn_ = free_fn; } #endif -#ifdef EVENT__HAVE_EVENTFD +#ifdef EVENT__HAVE_EVENTFD static void evthread_notify_drain_eventfd(evutil_socket_t fd, short what, void *arg) { @@ -3601,7 +3601,7 @@ evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg) { unsigned char buf[1024]; struct event_base *base = arg; -#ifdef _WIN32 +#ifdef _WIN32 while (recv(fd, (char*)buf, sizeof(buf), 0) > 0) ; #else @@ -3617,50 +3617,50 @@ evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg) int evthread_make_base_notifiable(struct event_base *base) { - int r; + int r; if (!base) return -1; - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - r = evthread_make_base_notifiable_nolock_(base); - EVBASE_RELEASE_LOCK(base, th_base_lock); - return r; -} - -static int -evthread_make_base_notifiable_nolock_(struct event_base *base) -{ - void (*cb)(evutil_socket_t, short, void *); - int (*notify)(struct event_base *); - - if (base->th_notify_fn != NULL) { - /* The base is already notifiable: we're doing fine. */ + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + r = evthread_make_base_notifiable_nolock_(base); + EVBASE_RELEASE_LOCK(base, th_base_lock); + return r; +} + +static int +evthread_make_base_notifiable_nolock_(struct event_base *base) +{ + void (*cb)(evutil_socket_t, short, void *); + int (*notify)(struct event_base *); + + if (base->th_notify_fn != NULL) { + /* The base is already notifiable: we're doing fine. */ + return 0; + } + +#if defined(EVENT__HAVE_WORKING_KQUEUE) + if (base->evsel == &kqops && event_kq_add_notify_event_(base) == 0) { + base->th_notify_fn = event_kq_notify_base_; + /* No need to add an event here; the backend can wake + * itself up just fine. */ return 0; - } - -#if defined(EVENT__HAVE_WORKING_KQUEUE) - if (base->evsel == &kqops && event_kq_add_notify_event_(base) == 0) { - base->th_notify_fn = event_kq_notify_base_; - /* No need to add an event here; the backend can wake - * itself up just fine. */ - return 0; - } + } #endif - -#ifdef EVENT__HAVE_EVENTFD - base->th_notify_fd[0] = evutil_eventfd_(0, - EVUTIL_EFD_CLOEXEC|EVUTIL_EFD_NONBLOCK); + +#ifdef EVENT__HAVE_EVENTFD + base->th_notify_fd[0] = evutil_eventfd_(0, + EVUTIL_EFD_CLOEXEC|EVUTIL_EFD_NONBLOCK); if (base->th_notify_fd[0] >= 0) { - base->th_notify_fd[1] = -1; + base->th_notify_fd[1] = -1; notify = evthread_notify_base_eventfd; cb = evthread_notify_drain_eventfd; - } else + } else #endif - if (evutil_make_internal_pipe_(base->th_notify_fd) == 0) { - notify = evthread_notify_base_default; - cb = evthread_notify_drain_default; - } else { - return -1; + if (evutil_make_internal_pipe_(base->th_notify_fd) == 0) { + notify = evthread_notify_base_default; + cb = evthread_notify_drain_default; + } else { + return -1; } base->th_notify_fn = notify; @@ -3673,211 +3673,211 @@ evthread_make_base_notifiable_nolock_(struct event_base *base) base->th_notify.ev_flags |= EVLIST_INTERNAL; event_priority_set(&base->th_notify, 0); - return event_add_nolock_(&base->th_notify, NULL, 0); -} - -int -event_base_foreach_event_nolock_(struct event_base *base, - event_base_foreach_event_cb fn, void *arg) -{ - int r, i; - unsigned u; - struct event *ev; - - /* Start out with all the EVLIST_INSERTED events. */ - if ((r = evmap_foreach_event_(base, fn, arg))) - return r; - - /* Okay, now we deal with those events that have timeouts and are in - * the min-heap. */ - for (u = 0; u < base->timeheap.n; ++u) { - ev = base->timeheap.p[u]; - if (ev->ev_flags & EVLIST_INSERTED) { - /* we already processed this one */ - continue; - } - if ((r = fn(base, ev, arg))) - return r; - } - - /* Now for the events in one of the timeout queues. - * the min-heap. */ - for (i = 0; i < base->n_common_timeouts; ++i) { - struct common_timeout_list *ctl = - base->common_timeout_queues[i]; - TAILQ_FOREACH(ev, &ctl->events, - ev_timeout_pos.ev_next_with_common_timeout) { - if (ev->ev_flags & EVLIST_INSERTED) { - /* we already processed this one */ - continue; - } - if ((r = fn(base, ev, arg))) - return r; - } - } - - /* Finally, we deal wit all the active events that we haven't touched - * yet. */ - for (i = 0; i < base->nactivequeues; ++i) { - struct event_callback *evcb; - TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) { - if ((evcb->evcb_flags & (EVLIST_INIT|EVLIST_INSERTED|EVLIST_TIMEOUT)) != EVLIST_INIT) { - /* This isn't an event (evlist_init clear), or - * we already processed it. (inserted or - * timeout set */ - continue; - } - ev = event_callback_to_event(evcb); - if ((r = fn(base, ev, arg))) - return r; - } - } - - return 0; -} - -/* Helper for event_base_dump_events: called on each event in the event base; - * dumps only the inserted events. */ -static int -dump_inserted_event_fn(const struct event_base *base, const struct event *e, void *arg) -{ - FILE *output = arg; - const char *gloss = (e->ev_events & EV_SIGNAL) ? - "sig" : "fd "; - - if (! (e->ev_flags & (EVLIST_INSERTED|EVLIST_TIMEOUT))) - return 0; - - fprintf(output, " %p [%s "EV_SOCK_FMT"]%s%s%s%s%s%s%s", - (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), - (e->ev_events&EV_READ)?" Read":"", - (e->ev_events&EV_WRITE)?" Write":"", - (e->ev_events&EV_CLOSED)?" EOF":"", - (e->ev_events&EV_SIGNAL)?" Signal":"", - (e->ev_events&EV_PERSIST)?" Persist":"", - (e->ev_events&EV_ET)?" ET":"", - (e->ev_flags&EVLIST_INTERNAL)?" Internal":""); - if (e->ev_flags & EVLIST_TIMEOUT) { - struct timeval tv; - tv.tv_sec = e->ev_timeout.tv_sec; - tv.tv_usec = e->ev_timeout.tv_usec & MICROSECONDS_MASK; - evutil_timeradd(&tv, &base->tv_clock_diff, &tv); - fprintf(output, " Timeout=%ld.%06d", - (long)tv.tv_sec, (int)(tv.tv_usec & MICROSECONDS_MASK)); - } - fputc('\n', output); - - return 0; -} - -/* Helper for event_base_dump_events: called on each event in the event base; - * dumps only the active events. */ -static int -dump_active_event_fn(const struct event_base *base, const struct event *e, void *arg) -{ - FILE *output = arg; - const char *gloss = (e->ev_events & EV_SIGNAL) ? - "sig" : "fd "; - - if (! (e->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) - return 0; - - fprintf(output, " %p [%s "EV_SOCK_FMT", priority=%d]%s%s%s%s%s active%s%s\n", - (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), e->ev_pri, - (e->ev_res&EV_READ)?" Read":"", - (e->ev_res&EV_WRITE)?" Write":"", - (e->ev_res&EV_CLOSED)?" EOF":"", - (e->ev_res&EV_SIGNAL)?" Signal":"", - (e->ev_res&EV_TIMEOUT)?" Timeout":"", - (e->ev_flags&EVLIST_INTERNAL)?" [Internal]":"", - (e->ev_flags&EVLIST_ACTIVE_LATER)?" [NextTime]":""); - - return 0; -} - -int -event_base_foreach_event(struct event_base *base, - event_base_foreach_event_cb fn, void *arg) -{ - int r; - if ((!fn) || (!base)) { - return -1; - } - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - r = event_base_foreach_event_nolock_(base, fn, arg); - EVBASE_RELEASE_LOCK(base, th_base_lock); - return r; -} - - + return event_add_nolock_(&base->th_notify, NULL, 0); +} + +int +event_base_foreach_event_nolock_(struct event_base *base, + event_base_foreach_event_cb fn, void *arg) +{ + int r, i; + unsigned u; + struct event *ev; + + /* Start out with all the EVLIST_INSERTED events. */ + if ((r = evmap_foreach_event_(base, fn, arg))) + return r; + + /* Okay, now we deal with those events that have timeouts and are in + * the min-heap. */ + for (u = 0; u < base->timeheap.n; ++u) { + ev = base->timeheap.p[u]; + if (ev->ev_flags & EVLIST_INSERTED) { + /* we already processed this one */ + continue; + } + if ((r = fn(base, ev, arg))) + return r; + } + + /* Now for the events in one of the timeout queues. + * the min-heap. */ + for (i = 0; i < base->n_common_timeouts; ++i) { + struct common_timeout_list *ctl = + base->common_timeout_queues[i]; + TAILQ_FOREACH(ev, &ctl->events, + ev_timeout_pos.ev_next_with_common_timeout) { + if (ev->ev_flags & EVLIST_INSERTED) { + /* we already processed this one */ + continue; + } + if ((r = fn(base, ev, arg))) + return r; + } + } + + /* Finally, we deal wit all the active events that we haven't touched + * yet. */ + for (i = 0; i < base->nactivequeues; ++i) { + struct event_callback *evcb; + TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) { + if ((evcb->evcb_flags & (EVLIST_INIT|EVLIST_INSERTED|EVLIST_TIMEOUT)) != EVLIST_INIT) { + /* This isn't an event (evlist_init clear), or + * we already processed it. (inserted or + * timeout set */ + continue; + } + ev = event_callback_to_event(evcb); + if ((r = fn(base, ev, arg))) + return r; + } + } + + return 0; +} + +/* Helper for event_base_dump_events: called on each event in the event base; + * dumps only the inserted events. */ +static int +dump_inserted_event_fn(const struct event_base *base, const struct event *e, void *arg) +{ + FILE *output = arg; + const char *gloss = (e->ev_events & EV_SIGNAL) ? + "sig" : "fd "; + + if (! (e->ev_flags & (EVLIST_INSERTED|EVLIST_TIMEOUT))) + return 0; + + fprintf(output, " %p [%s "EV_SOCK_FMT"]%s%s%s%s%s%s%s", + (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), + (e->ev_events&EV_READ)?" Read":"", + (e->ev_events&EV_WRITE)?" Write":"", + (e->ev_events&EV_CLOSED)?" EOF":"", + (e->ev_events&EV_SIGNAL)?" Signal":"", + (e->ev_events&EV_PERSIST)?" Persist":"", + (e->ev_events&EV_ET)?" ET":"", + (e->ev_flags&EVLIST_INTERNAL)?" Internal":""); + if (e->ev_flags & EVLIST_TIMEOUT) { + struct timeval tv; + tv.tv_sec = e->ev_timeout.tv_sec; + tv.tv_usec = e->ev_timeout.tv_usec & MICROSECONDS_MASK; + evutil_timeradd(&tv, &base->tv_clock_diff, &tv); + fprintf(output, " Timeout=%ld.%06d", + (long)tv.tv_sec, (int)(tv.tv_usec & MICROSECONDS_MASK)); + } + fputc('\n', output); + + return 0; +} + +/* Helper for event_base_dump_events: called on each event in the event base; + * dumps only the active events. */ +static int +dump_active_event_fn(const struct event_base *base, const struct event *e, void *arg) +{ + FILE *output = arg; + const char *gloss = (e->ev_events & EV_SIGNAL) ? + "sig" : "fd "; + + if (! (e->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) + return 0; + + fprintf(output, " %p [%s "EV_SOCK_FMT", priority=%d]%s%s%s%s%s active%s%s\n", + (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), e->ev_pri, + (e->ev_res&EV_READ)?" Read":"", + (e->ev_res&EV_WRITE)?" Write":"", + (e->ev_res&EV_CLOSED)?" EOF":"", + (e->ev_res&EV_SIGNAL)?" Signal":"", + (e->ev_res&EV_TIMEOUT)?" Timeout":"", + (e->ev_flags&EVLIST_INTERNAL)?" [Internal]":"", + (e->ev_flags&EVLIST_ACTIVE_LATER)?" [NextTime]":""); + + return 0; +} + +int +event_base_foreach_event(struct event_base *base, + event_base_foreach_event_cb fn, void *arg) +{ + int r; + if ((!fn) || (!base)) { + return -1; + } + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + r = event_base_foreach_event_nolock_(base, fn, arg); + EVBASE_RELEASE_LOCK(base, th_base_lock); + return r; +} + + void event_base_dump_events(struct event_base *base, FILE *output) { - EVBASE_ACQUIRE_LOCK(base, th_base_lock); + EVBASE_ACQUIRE_LOCK(base, th_base_lock); fprintf(output, "Inserted events:\n"); - event_base_foreach_event_nolock_(base, dump_inserted_event_fn, output); - - fprintf(output, "Active events:\n"); - event_base_foreach_event_nolock_(base, dump_active_event_fn, output); - EVBASE_RELEASE_LOCK(base, th_base_lock); -} - -void -event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events) -{ - EVBASE_ACQUIRE_LOCK(base, th_base_lock); - - /* Activate any non timer events */ - if (!(events & EV_TIMEOUT)) { - evmap_io_active_(base, fd, events & (EV_READ|EV_WRITE|EV_CLOSED)); - } else { - /* If we want to activate timer events, loop and activate each event with - * the same fd in both the timeheap and common timeouts list */ - int i; - unsigned u; - struct event *ev; - - for (u = 0; u < base->timeheap.n; ++u) { - ev = base->timeheap.p[u]; - if (ev->ev_fd == fd) { - event_active_nolock_(ev, EV_TIMEOUT, 1); - } + event_base_foreach_event_nolock_(base, dump_inserted_event_fn, output); + + fprintf(output, "Active events:\n"); + event_base_foreach_event_nolock_(base, dump_active_event_fn, output); + EVBASE_RELEASE_LOCK(base, th_base_lock); +} + +void +event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events) +{ + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + + /* Activate any non timer events */ + if (!(events & EV_TIMEOUT)) { + evmap_io_active_(base, fd, events & (EV_READ|EV_WRITE|EV_CLOSED)); + } else { + /* If we want to activate timer events, loop and activate each event with + * the same fd in both the timeheap and common timeouts list */ + int i; + unsigned u; + struct event *ev; + + for (u = 0; u < base->timeheap.n; ++u) { + ev = base->timeheap.p[u]; + if (ev->ev_fd == fd) { + event_active_nolock_(ev, EV_TIMEOUT, 1); + } + } + + for (i = 0; i < base->n_common_timeouts; ++i) { + struct common_timeout_list *ctl = base->common_timeout_queues[i]; + TAILQ_FOREACH(ev, &ctl->events, + ev_timeout_pos.ev_next_with_common_timeout) { + if (ev->ev_fd == fd) { + event_active_nolock_(ev, EV_TIMEOUT, 1); + } + } } - - for (i = 0; i < base->n_common_timeouts; ++i) { - struct common_timeout_list *ctl = base->common_timeout_queues[i]; - TAILQ_FOREACH(ev, &ctl->events, - ev_timeout_pos.ev_next_with_common_timeout) { - if (ev->ev_fd == fd) { - event_active_nolock_(ev, EV_TIMEOUT, 1); - } - } - } } - - EVBASE_RELEASE_LOCK(base, th_base_lock); + + EVBASE_RELEASE_LOCK(base, th_base_lock); } void -event_base_active_by_signal(struct event_base *base, int sig) +event_base_active_by_signal(struct event_base *base, int sig) +{ + EVBASE_ACQUIRE_LOCK(base, th_base_lock); + evmap_signal_active_(base, sig, 1); + EVBASE_RELEASE_LOCK(base, th_base_lock); +} + + +void +event_base_add_virtual_(struct event_base *base) { EVBASE_ACQUIRE_LOCK(base, th_base_lock); - evmap_signal_active_(base, sig, 1); - EVBASE_RELEASE_LOCK(base, th_base_lock); -} - - -void -event_base_add_virtual_(struct event_base *base) -{ - EVBASE_ACQUIRE_LOCK(base, th_base_lock); base->virtual_event_count++; - MAX_EVENT_COUNT(base->virtual_event_count_max, base->virtual_event_count); + MAX_EVENT_COUNT(base->virtual_event_count_max, base->virtual_event_count); EVBASE_RELEASE_LOCK(base, th_base_lock); } void -event_base_del_virtual_(struct event_base *base) +event_base_del_virtual_(struct event_base *base) { EVBASE_ACQUIRE_LOCK(base, th_base_lock); EVUTIL_ASSERT(base->virtual_event_count > 0); @@ -3887,65 +3887,65 @@ event_base_del_virtual_(struct event_base *base) EVBASE_RELEASE_LOCK(base, th_base_lock); } -static void -event_free_debug_globals_locks(void) -{ -#ifndef EVENT__DISABLE_THREAD_SUPPORT -#ifndef EVENT__DISABLE_DEBUG_MODE - if (event_debug_map_lock_ != NULL) { - EVTHREAD_FREE_LOCK(event_debug_map_lock_, 0); - event_debug_map_lock_ = NULL; - evthreadimpl_disable_lock_debugging_(); - } -#endif /* EVENT__DISABLE_DEBUG_MODE */ -#endif /* EVENT__DISABLE_THREAD_SUPPORT */ - return; -} - -static void -event_free_debug_globals(void) -{ - event_free_debug_globals_locks(); -} - -static void -event_free_evsig_globals(void) -{ - evsig_free_globals_(); -} - -static void -event_free_evutil_globals(void) -{ - evutil_free_globals_(); -} - -static void -event_free_globals(void) -{ - event_free_debug_globals(); - event_free_evsig_globals(); - event_free_evutil_globals(); -} - -void -libevent_global_shutdown(void) -{ - event_disable_debug_mode(); - event_free_globals(); -} - -#ifndef EVENT__DISABLE_THREAD_SUPPORT +static void +event_free_debug_globals_locks(void) +{ +#ifndef EVENT__DISABLE_THREAD_SUPPORT +#ifndef EVENT__DISABLE_DEBUG_MODE + if (event_debug_map_lock_ != NULL) { + EVTHREAD_FREE_LOCK(event_debug_map_lock_, 0); + event_debug_map_lock_ = NULL; + evthreadimpl_disable_lock_debugging_(); + } +#endif /* EVENT__DISABLE_DEBUG_MODE */ +#endif /* EVENT__DISABLE_THREAD_SUPPORT */ + return; +} + +static void +event_free_debug_globals(void) +{ + event_free_debug_globals_locks(); +} + +static void +event_free_evsig_globals(void) +{ + evsig_free_globals_(); +} + +static void +event_free_evutil_globals(void) +{ + evutil_free_globals_(); +} + +static void +event_free_globals(void) +{ + event_free_debug_globals(); + event_free_evsig_globals(); + event_free_evutil_globals(); +} + +void +libevent_global_shutdown(void) +{ + event_disable_debug_mode(); + event_free_globals(); +} + +#ifndef EVENT__DISABLE_THREAD_SUPPORT int event_global_setup_locks_(const int enable_locks) { -#ifndef EVENT__DISABLE_DEBUG_MODE - EVTHREAD_SETUP_GLOBAL_LOCK(event_debug_map_lock_, 0); +#ifndef EVENT__DISABLE_DEBUG_MODE + EVTHREAD_SETUP_GLOBAL_LOCK(event_debug_map_lock_, 0); #endif if (evsig_global_setup_locks_(enable_locks) < 0) return -1; - if (evutil_global_setup_locks_(enable_locks) < 0) - return -1; + if (evutil_global_setup_locks_(enable_locks) < 0) + return -1; if (evutil_secure_rng_global_setup_locks_(enable_locks) < 0) return -1; return 0; @@ -3953,29 +3953,29 @@ event_global_setup_locks_(const int enable_locks) #endif void -event_base_assert_ok_(struct event_base *base) +event_base_assert_ok_(struct event_base *base) { EVBASE_ACQUIRE_LOCK(base, th_base_lock); - event_base_assert_ok_nolock_(base); - EVBASE_RELEASE_LOCK(base, th_base_lock); -} - -void -event_base_assert_ok_nolock_(struct event_base *base) -{ - int i; - int count; - - /* First do checks on the per-fd and per-signal lists */ - evmap_check_integrity_(base); - + event_base_assert_ok_nolock_(base); + EVBASE_RELEASE_LOCK(base, th_base_lock); +} + +void +event_base_assert_ok_nolock_(struct event_base *base) +{ + int i; + int count; + + /* First do checks on the per-fd and per-signal lists */ + evmap_check_integrity_(base); + /* Check the heap property */ for (i = 1; i < (int)base->timeheap.n; ++i) { int parent = (i - 1) / 2; struct event *ev, *p_ev; ev = base->timeheap.p[i]; p_ev = base->timeheap.p[parent]; - EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT); + EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT); EVUTIL_ASSERT(evutil_timercmp(&p_ev->ev_timeout, &ev->ev_timeout, <=)); EVUTIL_ASSERT(ev->ev_timeout_pos.min_heap_idx == i); } @@ -3984,37 +3984,37 @@ event_base_assert_ok_nolock_(struct event_base *base) for (i = 0; i < base->n_common_timeouts; ++i) { struct common_timeout_list *ctl = base->common_timeout_queues[i]; struct event *last=NULL, *ev; - - EVUTIL_ASSERT_TAILQ_OK(&ctl->events, event, ev_timeout_pos.ev_next_with_common_timeout); - + + EVUTIL_ASSERT_TAILQ_OK(&ctl->events, event, ev_timeout_pos.ev_next_with_common_timeout); + TAILQ_FOREACH(ev, &ctl->events, ev_timeout_pos.ev_next_with_common_timeout) { if (last) EVUTIL_ASSERT(evutil_timercmp(&last->ev_timeout, &ev->ev_timeout, <=)); - EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT); + EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT); EVUTIL_ASSERT(is_common_timeout(&ev->ev_timeout,base)); EVUTIL_ASSERT(COMMON_TIMEOUT_IDX(&ev->ev_timeout) == i); last = ev; } } - /* Check the active queues. */ - count = 0; - for (i = 0; i < base->nactivequeues; ++i) { - struct event_callback *evcb; - EVUTIL_ASSERT_TAILQ_OK(&base->activequeues[i], event_callback, evcb_active_next); - TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) { - EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE); - EVUTIL_ASSERT(evcb->evcb_pri == i); - ++count; - } - } - - { - struct event_callback *evcb; - TAILQ_FOREACH(evcb, &base->active_later_queue, evcb_active_next) { - EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE_LATER); - ++count; - } - } - EVUTIL_ASSERT(count == base->event_count_active); + /* Check the active queues. */ + count = 0; + for (i = 0; i < base->nactivequeues; ++i) { + struct event_callback *evcb; + EVUTIL_ASSERT_TAILQ_OK(&base->activequeues[i], event_callback, evcb_active_next); + TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) { + EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE); + EVUTIL_ASSERT(evcb->evcb_pri == i); + ++count; + } + } + + { + struct event_callback *evcb; + TAILQ_FOREACH(evcb, &base->active_later_queue, evcb_active_next) { + EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE_LATER); + ++count; + } + } + EVUTIL_ASSERT(count == base->event_count_active); } |