1/*
2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27#include "event2/event-config.h"
28
29#ifdef WIN32
30#include <winsock2.h>
31#define WIN32_LEAN_AND_MEAN
32#include <windows.h>
33#undef WIN32_LEAN_AND_MEAN
34#endif
35#include <sys/types.h>
36#if !defined(WIN32) && defined(_EVENT_HAVE_SYS_TIME_H)
37#include <sys/time.h>
38#endif
39#include <sys/queue.h>
40#ifdef _EVENT_HAVE_SYS_SOCKET_H
41#include <sys/socket.h>
42#endif
43#include <stdio.h>
44#include <stdlib.h>
45#ifdef _EVENT_HAVE_UNISTD_H
46#include <unistd.h>
47#endif
48#ifdef _EVENT_HAVE_SYS_EVENTFD_H
49#include <sys/eventfd.h>
50#endif
51#include <ctype.h>
52#include <errno.h>
53#include <signal.h>
54#include <string.h>
55#include <time.h>
56
57#include "event2/event.h"
58#include "event2/event_struct.h"
59#include "event2/event_compat.h"
60#include "event-internal.h"
61#include "defer-internal.h"
62#include "evthread-internal.h"
63#include "event2/thread.h"
64#include "event2/util.h"
65#include "log-internal.h"
66#include "evmap-internal.h"
67#include "iocp-internal.h"
68#include "changelist-internal.h"
69#include "ht-internal.h"
70#include "util-internal.h"
71
72#ifdef _EVENT_HAVE_EVENT_PORTS
73extern const struct eventop evportops;
74#endif
75#ifdef _EVENT_HAVE_SELECT
76extern const struct eventop selectops;
77#endif
78#ifdef _EVENT_HAVE_POLL
79extern const struct eventop pollops;
80#endif
81#ifdef _EVENT_HAVE_EPOLL
82extern const struct eventop epollops;
83#endif
84#ifdef _EVENT_HAVE_WORKING_KQUEUE
85extern const struct eventop kqops;
86#endif
87#ifdef _EVENT_HAVE_DEVPOLL
88extern const struct eventop devpollops;
89#endif
90#ifdef WIN32
91extern const struct eventop win32ops;
92#endif
93
94/* Array of backends in order of preference. */
95static const struct eventop *eventops[] = {
96#ifdef _EVENT_HAVE_EVENT_PORTS
97	&evportops,
98#endif
99#ifdef _EVENT_HAVE_WORKING_KQUEUE
100	&kqops,
101#endif
102#ifdef _EVENT_HAVE_EPOLL
103	&epollops,
104#endif
105#ifdef _EVENT_HAVE_DEVPOLL
106	&devpollops,
107#endif
108#ifdef _EVENT_HAVE_POLL
109	&pollops,
110#endif
111#ifdef _EVENT_HAVE_SELECT
112	&selectops,
113#endif
114#ifdef WIN32
115	&win32ops,
116#endif
117	NULL
118};
119
120/* Global state; deprecated */
121struct event_base *event_global_current_base_ = NULL;
122#define current_base event_global_current_base_
123
124/* Global state */
125
126static int use_monotonic;
127
128/* Prototypes */
129static inline int event_add_internal(struct event *ev,
130    const struct timeval *tv, int tv_is_absolute);
131static inline int event_del_internal(struct event *ev);
132
133static void	event_queue_insert(struct event_base *, struct event *, int);
134static void	event_queue_remove(struct event_base *, struct event *, int);
135static int	event_haveevents(struct event_base *);
136
137static int	event_process_active(struct event_base *);
138
139static int	timeout_next(struct event_base *, struct timeval **);
140static void	timeout_process(struct event_base *);
141static void	timeout_correct(struct event_base *, struct timeval *);
142
143static inline void	event_signal_closure(struct event_base *, struct event *ev);
144static inline void	event_persist_closure(struct event_base *, struct event *ev);
145
146static int	evthread_notify_base(struct event_base *base);
147
148#ifndef _EVENT_DISABLE_DEBUG_MODE
149/* These functions implement a hashtable of which 'struct event *' structures
150 * have been setup or added.  We don't want to trust the content of the struct
151 * event itself, since we're trying to work through cases where an event gets
152 * clobbered or freed.  Instead, we keep a hashtable indexed by the pointer.
153 */
154
155struct event_debug_entry {
156	HT_ENTRY(event_debug_entry) node;
157	const struct event *ptr;
158	unsigned added : 1;
159};
160
161static inline unsigned
162hash_debug_entry(const struct event_debug_entry *e)
163{
164	/* We need to do this silliness to convince compilers that we
165	 * honestly mean to cast e->ptr to an integer, and discard any
166	 * part of it that doesn't fit in an unsigned.
167	 */
168	unsigned u = (unsigned) ((ev_uintptr_t) e->ptr);
169	/* Our hashtable implementation is pretty sensitive to low bits,
170	 * and every struct event is over 64 bytes in size, so we can
171	 * just say >>6. */
172	return (u >> 6);
173}
174
175static inline int
176eq_debug_entry(const struct event_debug_entry *a,
177    const struct event_debug_entry *b)
178{
179	return a->ptr == b->ptr;
180}
181
182int _event_debug_mode_on = 0;
183/* Set if it's too late to enable event_debug_mode. */
184static int event_debug_mode_too_late = 0;
185#ifndef _EVENT_DISABLE_THREAD_SUPPORT
186static void *_event_debug_map_lock = NULL;
187#endif
188static HT_HEAD(event_debug_map, event_debug_entry) global_debug_map =
189	HT_INITIALIZER();
190
191HT_PROTOTYPE(event_debug_map, event_debug_entry, node, hash_debug_entry,
192    eq_debug_entry)
193HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry,
194    eq_debug_entry, 0.5, mm_malloc, mm_realloc, mm_free)
195
196/* Macro: record that ev is now setup (that is, ready for an add) */
197#define _event_debug_note_setup(ev) do {				\
198	if (_event_debug_mode_on) {					\
199		struct event_debug_entry *dent,find;			\
200		find.ptr = (ev);					\
201		EVLOCK_LOCK(_event_debug_map_lock, 0);			\
202		dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
203		if (dent) {						\
204			dent->added = 0;				\
205		} else {						\
206			dent = mm_malloc(sizeof(*dent));		\
207			if (!dent)					\
208				event_err(1,				\
209				    "Out of memory in debugging code");	\
210			dent->ptr = (ev);				\
211			dent->added = 0;				\
212			HT_INSERT(event_debug_map, &global_debug_map, dent); \
213		}							\
214		EVLOCK_UNLOCK(_event_debug_map_lock, 0);		\
215	}								\
216	event_debug_mode_too_late = 1;					\
217	} while (0)
218/* Macro: record that ev is no longer setup */
219#define _event_debug_note_teardown(ev) do {				\
220	if (_event_debug_mode_on) {					\
221		struct event_debug_entry *dent,find;			\
222		find.ptr = (ev);					\
223		EVLOCK_LOCK(_event_debug_map_lock, 0);			\
224		dent = HT_REMOVE(event_debug_map, &global_debug_map, &find); \
225		if (dent)						\
226			mm_free(dent);					\
227		EVLOCK_UNLOCK(_event_debug_map_lock, 0);		\
228	}								\
229	event_debug_mode_too_late = 1;					\
230	} while (0)
231/* Macro: record that ev is now added */
232#define _event_debug_note_add(ev)	do {				\
233	if (_event_debug_mode_on) {					\
234		struct event_debug_entry *dent,find;			\
235		find.ptr = (ev);					\
236		EVLOCK_LOCK(_event_debug_map_lock, 0);			\
237		dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
238		if (dent) {						\
239			dent->added = 1;				\
240		} else {						\
241			event_errx(_EVENT_ERR_ABORT,			\
242			    "%s: noting an add on a non-setup event %p" \
243			    " (events: 0x%x, fd: %d, flags: 0x%x)",	\
244			    __func__, (ev), (ev)->ev_events,		\
245			    (ev)->ev_fd, (ev)->ev_flags);		\
246		}							\
247		EVLOCK_UNLOCK(_event_debug_map_lock, 0);		\
248	}								\
249	event_debug_mode_too_late = 1;					\
250	} while (0)
251/* Macro: record that ev is no longer added */
252#define _event_debug_note_del(ev) do {					\
253	if (_event_debug_mode_on) {					\
254		struct event_debug_entry *dent,find;			\
255		find.ptr = (ev);					\
256		EVLOCK_LOCK(_event_debug_map_lock, 0);			\
257		dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
258		if (dent) {						\
259			dent->added = 0;				\
260		} else {						\
261			event_errx(_EVENT_ERR_ABORT,			\
262			    "%s: noting a del on a non-setup event %p"	\
263			    " (events: 0x%x, fd: %d, flags: 0x%x)",	\
264			    __func__, (ev), (ev)->ev_events,		\
265			    (ev)->ev_fd, (ev)->ev_flags);		\
266		}							\
267		EVLOCK_UNLOCK(_event_debug_map_lock, 0);		\
268	}								\
269	event_debug_mode_too_late = 1;					\
270	} while (0)
271/* Macro: assert that ev is setup (i.e., okay to add or inspect) */
272#define _event_debug_assert_is_setup(ev) do {				\
273	if (_event_debug_mode_on) {					\
274		struct event_debug_entry *dent,find;			\
275		find.ptr = (ev);					\
276		EVLOCK_LOCK(_event_debug_map_lock, 0);			\
277		dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
278		if (!dent) {						\
279			event_errx(_EVENT_ERR_ABORT,			\
280			    "%s called on a non-initialized event %p"	\
281			    " (events: 0x%x, fd: %d, flags: 0x%x)",	\
282			    __func__, (ev), (ev)->ev_events,		\
283			    (ev)->ev_fd, (ev)->ev_flags);		\
284		}							\
285		EVLOCK_UNLOCK(_event_debug_map_lock, 0);		\
286	}								\
287	} while (0)
288/* Macro: assert that ev is not added (i.e., okay to tear down or set
289 * up again) */
290#define _event_debug_assert_not_added(ev) do {				\
291	if (_event_debug_mode_on) {					\
292		struct event_debug_entry *dent,find;			\
293		find.ptr = (ev);					\
294		EVLOCK_LOCK(_event_debug_map_lock, 0);			\
295		dent = HT_FIND(event_debug_map, &global_debug_map, &find); \
296		if (dent && dent->added) {				\
297			event_errx(_EVENT_ERR_ABORT,			\
298			    "%s called on an already added event %p"	\
299			    " (events: 0x%x, fd: %d, flags: 0x%x)",	\
300			    __func__, (ev), (ev)->ev_events,		\
301			    (ev)->ev_fd, (ev)->ev_flags);		\
302		}							\
303		EVLOCK_UNLOCK(_event_debug_map_lock, 0);		\
304	}								\
305	} while (0)
306#else
307#define _event_debug_note_setup(ev) \
308	((void)0)
309#define _event_debug_note_teardown(ev) \
310	((void)0)
311#define _event_debug_note_add(ev) \
312	((void)0)
313#define _event_debug_note_del(ev) \
314	((void)0)
315#define _event_debug_assert_is_setup(ev) \
316	((void)0)
317#define _event_debug_assert_not_added(ev) \
318	((void)0)
319#endif
320
321#define EVENT_BASE_ASSERT_LOCKED(base)		\
322	EVLOCK_ASSERT_LOCKED((base)->th_base_lock)
323
324/* The first time this function is called, it sets use_monotonic to 1
325 * if we have a clock function that supports monotonic time */
326static void
327detect_monotonic(void)
328{
329#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
330	struct timespec	ts;
331	static int use_monotonic_initialized = 0;
332
333	if (use_monotonic_initialized)
334		return;
335
336	if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
337		use_monotonic = 1;
338
339	use_monotonic_initialized = 1;
340#endif
341}
342
343/* How often (in seconds) do we check for changes in wall clock time relative
344 * to monotonic time?  Set this to -1 for 'never.' */
345#define CLOCK_SYNC_INTERVAL -1
346
347/** Set 'tp' to the current time according to 'base'.  We must hold the lock
348 * on 'base'.  If there is a cached time, return it.  Otherwise, use
349 * clock_gettime or gettimeofday as appropriate to find out the right time.
350 * Return 0 on success, -1 on failure.
351 */
352static int
353gettime(struct event_base *base, struct timeval *tp)
354{
355	EVENT_BASE_ASSERT_LOCKED(base);
356
357	if (base->tv_cache.tv_sec) {
358		*tp = base->tv_cache;
359		return (0);
360	}
361
362#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
363	if (use_monotonic) {
364		struct timespec	ts;
365
366		if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
367			return (-1);
368
369		tp->tv_sec = ts.tv_sec;
370		tp->tv_usec = ts.tv_nsec / 1000;
371		if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL
372		    < ts.tv_sec) {
373			struct timeval tv;
374			evutil_gettimeofday(&tv,NULL);
375			evutil_timersub(&tv, tp, &base->tv_clock_diff);
376			base->last_updated_clock_diff = ts.tv_sec;
377		}
378
379		return (0);
380	}
381#endif
382
383	return (evutil_gettimeofday(tp, NULL));
384}
385
386int
387event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv)
388{
389	int r;
390	if (!base) {
391		base = current_base;
392		if (!current_base)
393			return evutil_gettimeofday(tv, NULL);
394	}
395
396	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
397	if (base->tv_cache.tv_sec == 0) {
398		r = evutil_gettimeofday(tv, NULL);
399	} else {
400#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
401		evutil_timeradd(&base->tv_cache, &base->tv_clock_diff, tv);
402#else
403		*tv = base->tv_cache;
404#endif
405		r = 0;
406	}
407	EVBASE_RELEASE_LOCK(base, th_base_lock);
408	return r;
409}
410
411/** Make 'base' have no current cached time. */
412static inline void
413clear_time_cache(struct event_base *base)
414{
415	base->tv_cache.tv_sec = 0;
416}
417
418/** Replace the cached time in 'base' with the current time. */
419static inline void
420update_time_cache(struct event_base *base)
421{
422	base->tv_cache.tv_sec = 0;
423	if (!(base->flags & EVENT_BASE_FLAG_NO_CACHE_TIME))
424	    gettime(base, &base->tv_cache);
425}
426
427struct event_base *
428event_init(void)
429{
430	struct event_base *base = event_base_new_with_config(NULL);
431
432	if (base == NULL) {
433		event_errx(1, "%s: Unable to construct event_base", __func__);
434		return NULL;
435	}
436
437	current_base = base;
438
439	return (base);
440}
441
442struct event_base *
443event_base_new(void)
444{
445	struct event_base *base = NULL;
446	struct event_config *cfg = event_config_new();
447	if (cfg) {
448		base = event_base_new_with_config(cfg);
449		event_config_free(cfg);
450	}
451	return base;
452}
453
454/** Return true iff 'method' is the name of a method that 'cfg' tells us to
455 * avoid. */
456static int
457event_config_is_avoided_method(const struct event_config *cfg,
458    const char *method)
459{
460	struct event_config_entry *entry;
461
462	TAILQ_FOREACH(entry, &cfg->entries, next) {
463		if (entry->avoid_method != NULL &&
464		    strcmp(entry->avoid_method, method) == 0)
465			return (1);
466	}
467
468	return (0);
469}
470
471/** Return true iff 'method' is disabled according to the environment. */
472static int
473event_is_method_disabled(const char *name)
474{
475	char environment[64];
476	int i;
477
478	evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name);
479	for (i = 8; environment[i] != '\0'; ++i)
480		environment[i] = EVUTIL_TOUPPER(environment[i]);
481	/* Note that evutil_getenv() ignores the environment entirely if
482	 * we're setuid */
483	return (evutil_getenv(environment) != NULL);
484}
485
486int
487event_base_get_features(const struct event_base *base)
488{
489	return base->evsel->features;
490}
491
492void
493event_deferred_cb_queue_init(struct deferred_cb_queue *cb)
494{
495	memset(cb, 0, sizeof(struct deferred_cb_queue));
496	TAILQ_INIT(&cb->deferred_cb_list);
497}
498
499/** Helper for the deferred_cb queue: wake up the event base. */
500static void
501notify_base_cbq_callback(struct deferred_cb_queue *cb, void *baseptr)
502{
503	struct event_base *base = baseptr;
504	if (EVBASE_NEED_NOTIFY(base))
505		evthread_notify_base(base);
506}
507
508struct deferred_cb_queue *
509event_base_get_deferred_cb_queue(struct event_base *base)
510{
511	return base ? &base->defer_queue : NULL;
512}
513
514void
515event_enable_debug_mode(void)
516{
517#ifndef _EVENT_DISABLE_DEBUG_MODE
518	if (_event_debug_mode_on)
519		event_errx(1, "%s was called twice!", __func__);
520	if (event_debug_mode_too_late)
521		event_errx(1, "%s must be called *before* creating any events "
522		    "or event_bases",__func__);
523
524	_event_debug_mode_on = 1;
525
526	HT_INIT(event_debug_map, &global_debug_map);
527#endif
528}
529
530#if 0
531void
532event_disable_debug_mode(void)
533{
534	struct event_debug_entry **ent, *victim;
535
536	EVLOCK_LOCK(_event_debug_map_lock, 0);
537	for (ent = HT_START(event_debug_map, &global_debug_map); ent; ) {
538		victim = *ent;
539		ent = HT_NEXT_RMV(event_debug_map,&global_debug_map, ent);
540		mm_free(victim);
541	}
542	HT_CLEAR(event_debug_map, &global_debug_map);
543	EVLOCK_UNLOCK(_event_debug_map_lock , 0);
544}
545#endif
546
547struct event_base *
548event_base_new_with_config(const struct event_config *cfg)
549{
550	int i;
551	struct event_base *base;
552	int should_check_environment;
553
554#ifndef _EVENT_DISABLE_DEBUG_MODE
555	event_debug_mode_too_late = 1;
556#endif
557
558	if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) {
559		event_warn("%s: calloc", __func__);
560		return NULL;
561	}
562	detect_monotonic();
563	gettime(base, &base->event_tv);
564
565	min_heap_ctor(&base->timeheap);
566	TAILQ_INIT(&base->eventqueue);
567	base->sig.ev_signal_pair[0] = -1;
568	base->sig.ev_signal_pair[1] = -1;
569	base->th_notify_fd[0] = -1;
570	base->th_notify_fd[1] = -1;
571
572	event_deferred_cb_queue_init(&base->defer_queue);
573	base->defer_queue.notify_fn = notify_base_cbq_callback;
574	base->defer_queue.notify_arg = base;
575	if (cfg)
576		base->flags = cfg->flags;
577
578	evmap_io_initmap(&base->io);
579	evmap_signal_initmap(&base->sigmap);
580	event_changelist_init(&base->changelist);
581
582	base->evbase = NULL;
583
584	should_check_environment =
585	    !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV));
586
587	for (i = 0; eventops[i] && !base->evbase; i++) {
588		if (cfg != NULL) {
589			/* determine if this backend should be avoided */
590			if (event_config_is_avoided_method(cfg,
591				eventops[i]->name))
592				continue;
593			if ((eventops[i]->features & cfg->require_features)
594			    != cfg->require_features)
595				continue;
596		}
597
598		/* also obey the environment variables */
599		if (should_check_environment &&
600		    event_is_method_disabled(eventops[i]->name))
601			continue;
602
603		base->evsel = eventops[i];
604
605		base->evbase = base->evsel->init(base);
606	}
607
608	if (base->evbase == NULL) {
609		event_warnx("%s: no event mechanism available",
610		    __func__);
611		base->evsel = NULL;
612		event_base_free(base);
613		return NULL;
614	}
615
616	if (evutil_getenv("EVENT_SHOW_METHOD"))
617		event_msgx("libevent using: %s", base->evsel->name);
618
619	/* allocate a single active event queue */
620	if (event_base_priority_init(base, 1) < 0) {
621		event_base_free(base);
622		return NULL;
623	}
624
625	/* prepare for threading */
626
627#ifndef _EVENT_DISABLE_THREAD_SUPPORT
628	if (EVTHREAD_LOCKING_ENABLED() &&
629	    (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) {
630		int r;
631		EVTHREAD_ALLOC_LOCK(base->th_base_lock,
632		    EVTHREAD_LOCKTYPE_RECURSIVE);
633		base->defer_queue.lock = base->th_base_lock;
634		EVTHREAD_ALLOC_COND(base->current_event_cond);
635		r = evthread_make_base_notifiable(base);
636		if (r<0) {
637			event_warnx("%s: Unable to make base notifiable.", __func__);
638			event_base_free(base);
639			return NULL;
640		}
641	}
642#endif
643
644#ifdef WIN32
645	if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP))
646		event_base_start_iocp(base, cfg->n_cpus_hint);
647#endif
648
649	return (base);
650}
651
652int
653event_base_start_iocp(struct event_base *base, int n_cpus)
654{
655#ifdef WIN32
656	if (base->iocp)
657		return 0;
658	base->iocp = event_iocp_port_launch(n_cpus);
659	if (!base->iocp) {
660		event_warnx("%s: Couldn't launch IOCP", __func__);
661		return -1;
662	}
663	return 0;
664#else
665	return -1;
666#endif
667}
668
669void
670event_base_stop_iocp(struct event_base *base)
671{
672#ifdef WIN32
673	int rv;
674
675	if (!base->iocp)
676		return;
677	rv = event_iocp_shutdown(base->iocp, -1);
678	EVUTIL_ASSERT(rv >= 0);
679	base->iocp = NULL;
680#endif
681}
682
683void
684event_base_free(struct event_base *base)
685{
686	int i, n_deleted=0;
687	struct event *ev;
688	/* XXXX grab the lock? If there is contention when one thread frees
689	 * the base, then the contending thread will be very sad soon. */
690
691	/* event_base_free(NULL) is how to free the current_base if we
692	 * made it with event_init and forgot to hold a reference to it. */
693	if (base == NULL && current_base)
694		base = current_base;
695	/* If we're freeing current_base, there won't be a current_base. */
696	if (base == current_base)
697		current_base = NULL;
698	/* Don't actually free NULL. */
699	if (base == NULL) {
700		event_warnx("%s: no base to free", __func__);
701		return;
702	}
703	/* XXX(niels) - check for internal events first */
704
705#ifdef WIN32
706	event_base_stop_iocp(base);
707#endif
708
709	/* threading fds if we have them */
710	if (base->th_notify_fd[0] != -1) {
711		event_del(&base->th_notify);
712		EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
713		if (base->th_notify_fd[1] != -1)
714			EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
715		base->th_notify_fd[0] = -1;
716		base->th_notify_fd[1] = -1;
717		event_debug_unassign(&base->th_notify);
718	}
719
720	/* Delete all non-internal events. */
721	for (ev = TAILQ_FIRST(&base->eventqueue); ev; ) {
722		struct event *next = TAILQ_NEXT(ev, ev_next);
723		if (!(ev->ev_flags & EVLIST_INTERNAL)) {
724			event_del(ev);
725			++n_deleted;
726		}
727		ev = next;
728	}
729	while ((ev = min_heap_top(&base->timeheap)) != NULL) {
730		event_del(ev);
731		++n_deleted;
732	}
733	for (i = 0; i < base->n_common_timeouts; ++i) {
734		struct common_timeout_list *ctl =
735		    base->common_timeout_queues[i];
736		event_del(&ctl->timeout_event); /* Internal; doesn't count */
737		event_debug_unassign(&ctl->timeout_event);
738		for (ev = TAILQ_FIRST(&ctl->events); ev; ) {
739			struct event *next = TAILQ_NEXT(ev,
740			    ev_timeout_pos.ev_next_with_common_timeout);
741			if (!(ev->ev_flags & EVLIST_INTERNAL)) {
742				event_del(ev);
743				++n_deleted;
744			}
745			ev = next;
746		}
747		mm_free(ctl);
748	}
749	if (base->common_timeout_queues)
750		mm_free(base->common_timeout_queues);
751
752	for (i = 0; i < base->nactivequeues; ++i) {
753		for (ev = TAILQ_FIRST(&base->activequeues[i]); ev; ) {
754			struct event *next = TAILQ_NEXT(ev, ev_active_next);
755			if (!(ev->ev_flags & EVLIST_INTERNAL)) {
756				event_del(ev);
757				++n_deleted;
758			}
759			ev = next;
760		}
761	}
762
763	if (n_deleted)
764		event_debug(("%s: %d events were still set in base",
765			__func__, n_deleted));
766
767	if (base->evsel != NULL && base->evsel->dealloc != NULL)
768		base->evsel->dealloc(base);
769
770	for (i = 0; i < base->nactivequeues; ++i)
771		EVUTIL_ASSERT(TAILQ_EMPTY(&base->activequeues[i]));
772
773	EVUTIL_ASSERT(min_heap_empty(&base->timeheap));
774	min_heap_dtor(&base->timeheap);
775
776	mm_free(base->activequeues);
777
778	EVUTIL_ASSERT(TAILQ_EMPTY(&base->eventqueue));
779
780	evmap_io_clear(&base->io);
781	evmap_signal_clear(&base->sigmap);
782	event_changelist_freemem(&base->changelist);
783
784	EVTHREAD_FREE_LOCK(base->th_base_lock, EVTHREAD_LOCKTYPE_RECURSIVE);
785	EVTHREAD_FREE_COND(base->current_event_cond);
786
787	mm_free(base);
788}
789
790/* reinitialize the event base after a fork */
791int
792event_reinit(struct event_base *base)
793{
794	const struct eventop *evsel;
795	int res = 0;
796	struct event *ev;
797	int was_notifiable = 0;
798
799	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
800
801	evsel = base->evsel;
802
803#if 0
804	/* Right now, reinit always takes effect, since even if the
805	   backend doesn't require it, the signal socketpair code does.
806
807	   XXX
808	 */
809	/* check if this event mechanism requires reinit */
810	if (!evsel->need_reinit)
811		goto done;
812#endif
813
814	/* prevent internal delete */
815	if (base->sig.ev_signal_added) {
816		/* we cannot call event_del here because the base has
817		 * not been reinitialized yet. */
818		event_queue_remove(base, &base->sig.ev_signal,
819		    EVLIST_INSERTED);
820		if (base->sig.ev_signal.ev_flags & EVLIST_ACTIVE)
821			event_queue_remove(base, &base->sig.ev_signal,
822			    EVLIST_ACTIVE);
823		if (base->sig.ev_signal_pair[0] != -1)
824			EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]);
825		if (base->sig.ev_signal_pair[1] != -1)
826			EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]);
827		base->sig.ev_signal_added = 0;
828	}
829	if (base->th_notify_fd[0] != -1) {
830		/* we cannot call event_del here because the base has
831		 * not been reinitialized yet. */
832		was_notifiable = 1;
833		event_queue_remove(base, &base->th_notify,
834		    EVLIST_INSERTED);
835		if (base->th_notify.ev_flags & EVLIST_ACTIVE)
836			event_queue_remove(base, &base->th_notify,
837			    EVLIST_ACTIVE);
838		base->sig.ev_signal_added = 0;
839		EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
840		if (base->th_notify_fd[1] != -1)
841			EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
842		base->th_notify_fd[0] = -1;
843		base->th_notify_fd[1] = -1;
844		event_debug_unassign(&base->th_notify);
845	}
846
847	if (base->evsel->dealloc != NULL)
848		base->evsel->dealloc(base);
849	base->evbase = evsel->init(base);
850	if (base->evbase == NULL) {
851		event_errx(1, "%s: could not reinitialize event mechanism",
852		    __func__);
853		res = -1;
854		goto done;
855	}
856
857	event_changelist_freemem(&base->changelist); /* XXX */
858	evmap_io_clear(&base->io);
859	evmap_signal_clear(&base->sigmap);
860
861	TAILQ_FOREACH(ev, &base->eventqueue, ev_next) {
862		if (ev->ev_events & (EV_READ|EV_WRITE)) {
863			if (ev == &base->sig.ev_signal) {
864				/* If we run into the ev_signal event, it's only
865				 * in eventqueue because some signal event was
866				 * added, which made evsig_add re-add ev_signal.
867				 * So don't double-add it. */
868				continue;
869			}
870			if (evmap_io_add(base, ev->ev_fd, ev) == -1)
871				res = -1;
872		} else if (ev->ev_events & EV_SIGNAL) {
873			if (evmap_signal_add(base, (int)ev->ev_fd, ev) == -1)
874				res = -1;
875		}
876	}
877
878	if (was_notifiable && res == 0)
879		res = evthread_make_base_notifiable(base);
880
881done:
882	EVBASE_RELEASE_LOCK(base, th_base_lock);
883	return (res);
884}
885
886const char **
887event_get_supported_methods(void)
888{
889	static const char **methods = NULL;
890	const struct eventop **method;
891	const char **tmp;
892	int i = 0, k;
893
894	/* count all methods */
895	for (method = &eventops[0]; *method != NULL; ++method) {
896		++i;
897	}
898
899	/* allocate one more than we need for the NULL pointer */
900	tmp = mm_calloc((i + 1), sizeof(char *));
901	if (tmp == NULL)
902		return (NULL);
903
904	/* populate the array with the supported methods */
905	for (k = 0, i = 0; eventops[k] != NULL; ++k) {
906		tmp[i++] = eventops[k]->name;
907	}
908	tmp[i] = NULL;
909
910	if (methods != NULL)
911		mm_free((char**)methods);
912
913	methods = tmp;
914
915	return (methods);
916}
917
918struct event_config *
919event_config_new(void)
920{
921	struct event_config *cfg = mm_calloc(1, sizeof(*cfg));
922
923	if (cfg == NULL)
924		return (NULL);
925
926	TAILQ_INIT(&cfg->entries);
927
928	return (cfg);
929}
930
931static void
932event_config_entry_free(struct event_config_entry *entry)
933{
934	if (entry->avoid_method != NULL)
935		mm_free((char *)entry->avoid_method);
936	mm_free(entry);
937}
938
939void
940event_config_free(struct event_config *cfg)
941{
942	struct event_config_entry *entry;
943
944	while ((entry = TAILQ_FIRST(&cfg->entries)) != NULL) {
945		TAILQ_REMOVE(&cfg->entries, entry, next);
946		event_config_entry_free(entry);
947	}
948	mm_free(cfg);
949}
950
951int
952event_config_set_flag(struct event_config *cfg, int flag)
953{
954	if (!cfg)
955		return -1;
956	cfg->flags |= flag;
957	return 0;
958}
959
960int
961event_config_avoid_method(struct event_config *cfg, const char *method)
962{
963	struct event_config_entry *entry = mm_malloc(sizeof(*entry));
964	if (entry == NULL)
965		return (-1);
966
967	if ((entry->avoid_method = mm_strdup(method)) == NULL) {
968		mm_free(entry);
969		return (-1);
970	}
971
972	TAILQ_INSERT_TAIL(&cfg->entries, entry, next);
973
974	return (0);
975}
976
977int
978event_config_require_features(struct event_config *cfg,
979    int features)
980{
981	if (!cfg)
982		return (-1);
983	cfg->require_features = features;
984	return (0);
985}
986
987int
988event_config_set_num_cpus_hint(struct event_config *cfg, int cpus)
989{
990	if (!cfg)
991		return (-1);
992	cfg->n_cpus_hint = cpus;
993	return (0);
994}
995
996int
997event_priority_init(int npriorities)
998{
999	return event_base_priority_init(current_base, npriorities);
1000}
1001
1002int
1003event_base_priority_init(struct event_base *base, int npriorities)
1004{
1005	int i;
1006
1007	if (N_ACTIVE_CALLBACKS(base) || npriorities < 1
1008	    || npriorities >= EVENT_MAX_PRIORITIES)
1009		return (-1);
1010
1011	if (npriorities == base->nactivequeues)
1012		return (0);
1013
1014	if (base->nactivequeues) {
1015		mm_free(base->activequeues);
1016		base->nactivequeues = 0;
1017	}
1018
1019	/* Allocate our priority queues */
1020	base->activequeues = (struct event_list *)
1021	  mm_calloc(npriorities, sizeof(struct event_list));
1022	if (base->activequeues == NULL) {
1023		event_warn("%s: calloc", __func__);
1024		return (-1);
1025	}
1026	base->nactivequeues = npriorities;
1027
1028	for (i = 0; i < base->nactivequeues; ++i) {
1029		TAILQ_INIT(&base->activequeues[i]);
1030	}
1031
1032	return (0);
1033}
1034
1035/* Returns true iff we're currently watching any events. */
1036static int
1037event_haveevents(struct event_base *base)
1038{
1039	/* Caller must hold th_base_lock */
1040	return (base->virtual_event_count > 0 || base->event_count > 0);
1041}
1042
1043/* "closure" function called when processing active signal events */
1044static inline void
1045event_signal_closure(struct event_base *base, struct event *ev)
1046{
1047	short ncalls;
1048	int should_break;
1049
1050	/* Allows deletes to work */
1051	ncalls = ev->ev_ncalls;
1052	if (ncalls != 0)
1053		ev->ev_pncalls = &ncalls;
1054	EVBASE_RELEASE_LOCK(base, th_base_lock);
1055	while (ncalls) {
1056		ncalls--;
1057		ev->ev_ncalls = ncalls;
1058		if (ncalls == 0)
1059			ev->ev_pncalls = NULL;
1060		(*ev->ev_callback)(ev->ev_fd, ev->ev_res, ev->ev_arg);
1061
1062		EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1063		should_break = base->event_break;
1064		EVBASE_RELEASE_LOCK(base, th_base_lock);
1065
1066		if (should_break) {
1067			if (ncalls != 0)
1068				ev->ev_pncalls = NULL;
1069			return;
1070		}
1071	}
1072}
1073
1074/* Common timeouts are special timeouts that are handled as queues rather than
1075 * in the minheap.  This is more efficient than the minheap if we happen to
1076 * know that we're going to get several thousands of timeout events all with
1077 * the same timeout value.
1078 *
1079 * Since all our timeout handling code assumes timevals can be copied,
1080 * assigned, etc, we can't use "magic pointer" to encode these common
1081 * timeouts.  Searching through a list to see if every timeout is common could
1082 * also get inefficient.  Instead, we take advantage of the fact that tv_usec
1083 * is 32 bits long, but only uses 20 of those bits (since it can never be over
1084 * 999999.)  We use the top bits to encode 4 bites of magic number, and 8 bits
1085 * of index into the event_base's aray of common timeouts.
1086 */
1087
1088#define MICROSECONDS_MASK       COMMON_TIMEOUT_MICROSECONDS_MASK
1089#define COMMON_TIMEOUT_IDX_MASK 0x0ff00000
1090#define COMMON_TIMEOUT_IDX_SHIFT 20
1091#define COMMON_TIMEOUT_MASK     0xf0000000
1092#define COMMON_TIMEOUT_MAGIC    0x50000000
1093
1094#define COMMON_TIMEOUT_IDX(tv) \
1095	(((tv)->tv_usec & COMMON_TIMEOUT_IDX_MASK)>>COMMON_TIMEOUT_IDX_SHIFT)
1096
1097/** Return true iff if 'tv' is a common timeout in 'base' */
1098static inline int
1099is_common_timeout(const struct timeval *tv,
1100    const struct event_base *base)
1101{
1102	int idx;
1103	if ((tv->tv_usec & COMMON_TIMEOUT_MASK) != COMMON_TIMEOUT_MAGIC)
1104		return 0;
1105	idx = COMMON_TIMEOUT_IDX(tv);
1106	return idx < base->n_common_timeouts;
1107}
1108
1109/* True iff tv1 and tv2 have the same common-timeout index, or if neither
1110 * one is a common timeout. */
1111static inline int
1112is_same_common_timeout(const struct timeval *tv1, const struct timeval *tv2)
1113{
1114	return (tv1->tv_usec & ~MICROSECONDS_MASK) ==
1115	    (tv2->tv_usec & ~MICROSECONDS_MASK);
1116}
1117
1118/** Requires that 'tv' is a common timeout.  Return the corresponding
1119 * common_timeout_list. */
1120static inline struct common_timeout_list *
1121get_common_timeout_list(struct event_base *base, const struct timeval *tv)
1122{
1123	return base->common_timeout_queues[COMMON_TIMEOUT_IDX(tv)];
1124}
1125
1126#if 0
1127static inline int
1128common_timeout_ok(const struct timeval *tv,
1129    struct event_base *base)
1130{
1131	const struct timeval *expect =
1132	    &get_common_timeout_list(base, tv)->duration;
1133	return tv->tv_sec == expect->tv_sec &&
1134	    tv->tv_usec == expect->tv_usec;
1135}
1136#endif
1137
1138/* Add the timeout for the first event in given common timeout list to the
1139 * event_base's minheap. */
1140static void
1141common_timeout_schedule(struct common_timeout_list *ctl,
1142    const struct timeval *now, struct event *head)
1143{
1144	struct timeval timeout = head->ev_timeout;
1145	timeout.tv_usec &= MICROSECONDS_MASK;
1146	event_add_internal(&ctl->timeout_event, &timeout, 1);
1147}
1148
1149/* Callback: invoked when the timeout for a common timeout queue triggers.
1150 * This means that (at least) the first event in that queue should be run,
1151 * and the timeout should be rescheduled if there are more events. */
1152static void
1153common_timeout_callback(evutil_socket_t fd, short what, void *arg)
1154{
1155	struct timeval now;
1156	struct common_timeout_list *ctl = arg;
1157	struct event_base *base = ctl->base;
1158	struct event *ev = NULL;
1159	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1160	gettime(base, &now);
1161	while (1) {
1162		ev = TAILQ_FIRST(&ctl->events);
1163		if (!ev || ev->ev_timeout.tv_sec > now.tv_sec ||
1164		    (ev->ev_timeout.tv_sec == now.tv_sec &&
1165			(ev->ev_timeout.tv_usec&MICROSECONDS_MASK) > now.tv_usec))
1166			break;
1167		event_del_internal(ev);
1168		event_active_nolock(ev, EV_TIMEOUT, 1);
1169	}
1170	if (ev)
1171		common_timeout_schedule(ctl, &now, ev);
1172	EVBASE_RELEASE_LOCK(base, th_base_lock);
1173}
1174
1175#define MAX_COMMON_TIMEOUTS 256
1176
1177const struct timeval *
1178event_base_init_common_timeout(struct event_base *base,
1179    const struct timeval *duration)
1180{
1181	int i;
1182	struct timeval tv;
1183	const struct timeval *result=NULL;
1184	struct common_timeout_list *new_ctl;
1185
1186	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1187	if (duration->tv_usec > 1000000) {
1188		memcpy(&tv, duration, sizeof(struct timeval));
1189		if (is_common_timeout(duration, base))
1190			tv.tv_usec &= MICROSECONDS_MASK;
1191		tv.tv_sec += tv.tv_usec / 1000000;
1192		tv.tv_usec %= 1000000;
1193		duration = &tv;
1194	}
1195	for (i = 0; i < base->n_common_timeouts; ++i) {
1196		const struct common_timeout_list *ctl =
1197		    base->common_timeout_queues[i];
1198		if (duration->tv_sec == ctl->duration.tv_sec &&
1199		    duration->tv_usec ==
1200		    (ctl->duration.tv_usec & MICROSECONDS_MASK)) {
1201			EVUTIL_ASSERT(is_common_timeout(&ctl->duration, base));
1202			result = &ctl->duration;
1203			goto done;
1204		}
1205	}
1206	if (base->n_common_timeouts == MAX_COMMON_TIMEOUTS) {
1207		event_warnx("%s: Too many common timeouts already in use; "
1208		    "we only support %d per event_base", __func__,
1209		    MAX_COMMON_TIMEOUTS);
1210		goto done;
1211	}
1212	if (base->n_common_timeouts_allocated == base->n_common_timeouts) {
1213		int n = base->n_common_timeouts < 16 ? 16 :
1214		    base->n_common_timeouts*2;
1215		struct common_timeout_list **newqueues =
1216		    mm_realloc(base->common_timeout_queues,
1217			n*sizeof(struct common_timeout_queue *));
1218		if (!newqueues) {
1219			event_warn("%s: realloc",__func__);
1220			goto done;
1221		}
1222		base->n_common_timeouts_allocated = n;
1223		base->common_timeout_queues = newqueues;
1224	}
1225	new_ctl = mm_calloc(1, sizeof(struct common_timeout_list));
1226	if (!new_ctl) {
1227		event_warn("%s: calloc",__func__);
1228		goto done;
1229	}
1230	TAILQ_INIT(&new_ctl->events);
1231	new_ctl->duration.tv_sec = duration->tv_sec;
1232	new_ctl->duration.tv_usec =
1233	    duration->tv_usec | COMMON_TIMEOUT_MAGIC |
1234	    (base->n_common_timeouts << COMMON_TIMEOUT_IDX_SHIFT);
1235	evtimer_assign(&new_ctl->timeout_event, base,
1236	    common_timeout_callback, new_ctl);
1237	new_ctl->timeout_event.ev_flags |= EVLIST_INTERNAL;
1238	event_priority_set(&new_ctl->timeout_event, 0);
1239	new_ctl->base = base;
1240	base->common_timeout_queues[base->n_common_timeouts++] = new_ctl;
1241	result = &new_ctl->duration;
1242
1243done:
1244	if (result)
1245		EVUTIL_ASSERT(is_common_timeout(result, base));
1246
1247	EVBASE_RELEASE_LOCK(base, th_base_lock);
1248	return result;
1249}
1250
1251/* Closure function invoked when we're activating a persistent event. */
1252static inline void
1253event_persist_closure(struct event_base *base, struct event *ev)
1254{
1255	/* reschedule the persistent event if we have a timeout. */
1256	if (ev->ev_io_timeout.tv_sec || ev->ev_io_timeout.tv_usec) {
1257		/* If there was a timeout, we want it to run at an interval of
1258		 * ev_io_timeout after the last time it was _scheduled_ for,
1259		 * not ev_io_timeout after _now_.  If it fired for another
1260		 * reason, though, the timeout ought to start ticking _now_. */
1261		struct timeval run_at, relative_to, delay, now;
1262		ev_uint32_t usec_mask = 0;
1263		EVUTIL_ASSERT(is_same_common_timeout(&ev->ev_timeout,
1264			&ev->ev_io_timeout));
1265		gettime(base, &now);
1266		if (is_common_timeout(&ev->ev_timeout, base)) {
1267			delay = ev->ev_io_timeout;
1268			usec_mask = delay.tv_usec & ~MICROSECONDS_MASK;
1269			delay.tv_usec &= MICROSECONDS_MASK;
1270			if (ev->ev_res & EV_TIMEOUT) {
1271				relative_to = ev->ev_timeout;
1272				relative_to.tv_usec &= MICROSECONDS_MASK;
1273			} else {
1274				relative_to = now;
1275			}
1276		} else {
1277			delay = ev->ev_io_timeout;
1278			if (ev->ev_res & EV_TIMEOUT) {
1279				relative_to = ev->ev_timeout;
1280			} else {
1281				relative_to = now;
1282			}
1283		}
1284		evutil_timeradd(&relative_to, &delay, &run_at);
1285		if (evutil_timercmp(&run_at, &now, <)) {
1286			/* Looks like we missed at least one invocation due to
1287			 * a clock jump, not running the event loop for a
1288			 * while, really slow callbacks, or
1289			 * something. Reschedule relative to now.
1290			 */
1291			evutil_timeradd(&now, &delay, &run_at);
1292		}
1293		run_at.tv_usec |= usec_mask;
1294		event_add_internal(ev, &run_at, 1);
1295	}
1296	EVBASE_RELEASE_LOCK(base, th_base_lock);
1297	(*ev->ev_callback)(ev->ev_fd, ev->ev_res, ev->ev_arg);
1298}
1299
1300/*
1301  Helper for event_process_active to process all the events in a single queue,
1302  releasing the lock as we go.  This function requires that the lock be held
1303  when it's invoked.  Returns -1 if we get a signal or an event_break that
1304  means we should stop processing any active events now.  Otherwise returns
1305  the number of non-internal events that we processed.
1306*/
1307static int
1308event_process_active_single_queue(struct event_base *base,
1309    struct event_list *activeq)
1310{
1311	struct event *ev;
1312	int count = 0;
1313
1314	EVUTIL_ASSERT(activeq != NULL);
1315
1316	for (ev = TAILQ_FIRST(activeq); ev; ev = TAILQ_FIRST(activeq)) {
1317		if (ev->ev_events & EV_PERSIST)
1318			event_queue_remove(base, ev, EVLIST_ACTIVE);
1319		else
1320			event_del_internal(ev);
1321		if (!(ev->ev_flags & EVLIST_INTERNAL))
1322			++count;
1323
1324		event_debug((
1325			 "event_process_active: event: %p, %s%scall %p",
1326			ev,
1327			ev->ev_res & EV_READ ? "EV_READ " : " ",
1328			ev->ev_res & EV_WRITE ? "EV_WRITE " : " ",
1329			ev->ev_callback));
1330
1331#ifndef _EVENT_DISABLE_THREAD_SUPPORT
1332		base->current_event = ev;
1333		base->current_event_waiters = 0;
1334#endif
1335
1336		switch (ev->ev_closure) {
1337		case EV_CLOSURE_SIGNAL:
1338			event_signal_closure(base, ev);
1339			break;
1340		case EV_CLOSURE_PERSIST:
1341			event_persist_closure(base, ev);
1342			break;
1343		default:
1344		case EV_CLOSURE_NONE:
1345			EVBASE_RELEASE_LOCK(base, th_base_lock);
1346			(*ev->ev_callback)(
1347				ev->ev_fd, ev->ev_res, ev->ev_arg);
1348			break;
1349		}
1350
1351		EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1352#ifndef _EVENT_DISABLE_THREAD_SUPPORT
1353		base->current_event = NULL;
1354		if (base->current_event_waiters) {
1355			base->current_event_waiters = 0;
1356			EVTHREAD_COND_BROADCAST(base->current_event_cond);
1357		}
1358#endif
1359
1360		if (base->event_break)
1361			return -1;
1362		if (base->event_continue)
1363			break;
1364	}
1365	return count;
1366}
1367
1368/*
1369   Process up to MAX_DEFERRED of the defered_cb entries in 'queue'.  If
1370   *breakptr becomes set to 1, stop.  Requires that we start out holding
1371   the lock on 'queue'; releases the lock around 'queue' for each deferred_cb
1372   we process.
1373 */
1374static int
1375event_process_deferred_callbacks(struct deferred_cb_queue *queue, int *breakptr)
1376{
1377	int count = 0;
1378	struct deferred_cb *cb;
1379
1380#define MAX_DEFERRED 16
1381	while ((cb = TAILQ_FIRST(&queue->deferred_cb_list))) {
1382		cb->queued = 0;
1383		TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next);
1384		--queue->active_count;
1385		UNLOCK_DEFERRED_QUEUE(queue);
1386
1387		cb->cb(cb, cb->arg);
1388
1389		LOCK_DEFERRED_QUEUE(queue);
1390		if (*breakptr)
1391			return -1;
1392		if (++count == MAX_DEFERRED)
1393			break;
1394	}
1395#undef MAX_DEFERRED
1396	return count;
1397}
1398
1399/*
1400 * Active events are stored in priority queues.  Lower priorities are always
1401 * process before higher priorities.  Low priority events can starve high
1402 * priority ones.
1403 */
1404
1405static int
1406event_process_active(struct event_base *base)
1407{
1408	/* Caller must hold th_base_lock */
1409	struct event_list *activeq = NULL;
1410	int i, c = 0;
1411
1412	for (i = 0; i < base->nactivequeues; ++i) {
1413		if (TAILQ_FIRST(&base->activequeues[i]) != NULL) {
1414			base->event_running_priority = i;
1415			activeq = &base->activequeues[i];
1416			c = event_process_active_single_queue(base, activeq);
1417			if (c < 0) {
1418				base->event_running_priority = -1;
1419				return -1;
1420			} else if (c > 0)
1421				break; /* Processed a real event; do not
1422					* consider lower-priority events */
1423			/* If we get here, all of the events we processed
1424			 * were internal.  Continue. */
1425		}
1426	}
1427
1428	event_process_deferred_callbacks(&base->defer_queue,&base->event_break);
1429	base->event_running_priority = -1;
1430	return c;
1431}
1432
1433/*
1434 * Wait continuously for events.  We exit only if no events are left.
1435 */
1436
1437int
1438event_dispatch(void)
1439{
1440	return (event_loop(0));
1441}
1442
1443int
1444event_base_dispatch(struct event_base *event_base)
1445{
1446	return (event_base_loop(event_base, 0));
1447}
1448
1449const char *
1450event_base_get_method(const struct event_base *base)
1451{
1452	EVUTIL_ASSERT(base);
1453	return (base->evsel->name);
1454}
1455
1456/** Callback: used to implement event_base_loopexit by telling the event_base
1457 * that it's time to exit its loop. */
1458static void
1459event_loopexit_cb(evutil_socket_t fd, short what, void *arg)
1460{
1461	struct event_base *base = arg;
1462	base->event_gotterm = 1;
1463}
1464
1465int
1466event_loopexit(const struct timeval *tv)
1467{
1468	return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
1469		    current_base, tv));
1470}
1471
1472int
1473event_base_loopexit(struct event_base *event_base, const struct timeval *tv)
1474{
1475	return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb,
1476		    event_base, tv));
1477}
1478
1479int
1480event_loopbreak(void)
1481{
1482	return (event_base_loopbreak(current_base));
1483}
1484
1485int
1486event_base_loopbreak(struct event_base *event_base)
1487{
1488	int r = 0;
1489	if (event_base == NULL)
1490		return (-1);
1491
1492	EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1493	event_base->event_break = 1;
1494
1495	if (EVBASE_NEED_NOTIFY(event_base)) {
1496		r = evthread_notify_base(event_base);
1497	} else {
1498		r = (0);
1499	}
1500	EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1501	return r;
1502}
1503
1504int
1505event_base_got_break(struct event_base *event_base)
1506{
1507	int res;
1508	EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1509	res = event_base->event_break;
1510	EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1511	return res;
1512}
1513
1514int
1515event_base_got_exit(struct event_base *event_base)
1516{
1517	int res;
1518	EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1519	res = event_base->event_gotterm;
1520	EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1521	return res;
1522}
1523
1524/* not thread safe */
1525
1526int
1527event_loop(int flags)
1528{
1529	return event_base_loop(current_base, flags);
1530}
1531
1532int
1533event_base_loop(struct event_base *base, int flags)
1534{
1535	const struct eventop *evsel = base->evsel;
1536	struct timeval tv;
1537	struct timeval *tv_p;
1538	int res, done, retval = 0;
1539
1540	/* Grab the lock.  We will release it inside evsel.dispatch, and again
1541	 * as we invoke user callbacks. */
1542	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1543
1544	if (base->running_loop) {
1545		event_warnx("%s: reentrant invocation.  Only one event_base_loop"
1546		    " can run on each event_base at once.", __func__);
1547		EVBASE_RELEASE_LOCK(base, th_base_lock);
1548		return -1;
1549	}
1550
1551	base->running_loop = 1;
1552
1553	clear_time_cache(base);
1554
1555	if (base->sig.ev_signal_added && base->sig.ev_n_signals_added)
1556		evsig_set_base(base);
1557
1558	done = 0;
1559
1560#ifndef _EVENT_DISABLE_THREAD_SUPPORT
1561	base->th_owner_id = EVTHREAD_GET_ID();
1562#endif
1563
1564	base->event_gotterm = base->event_break = 0;
1565
1566	while (!done) {
1567		base->event_continue = 0;
1568
1569		/* Terminate the loop if we have been asked to */
1570		if (base->event_gotterm) {
1571			break;
1572		}
1573
1574		if (base->event_break) {
1575			break;
1576		}
1577
1578		timeout_correct(base, &tv);
1579
1580		tv_p = &tv;
1581		if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) {
1582			timeout_next(base, &tv_p);
1583		} else {
1584			/*
1585			 * if we have active events, we just poll new events
1586			 * without waiting.
1587			 */
1588			evutil_timerclear(&tv);
1589		}
1590
1591		/* If we have no events, we just exit */
1592		if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
1593			event_debug(("%s: no events registered.", __func__));
1594			retval = 1;
1595			goto done;
1596		}
1597
1598		/* update last old time */
1599		gettime(base, &base->event_tv);
1600
1601		clear_time_cache(base);
1602
1603		res = evsel->dispatch(base, tv_p);
1604
1605		if (res == -1) {
1606			event_debug(("%s: dispatch returned unsuccessfully.",
1607				__func__));
1608			retval = -1;
1609			goto done;
1610		}
1611
1612		update_time_cache(base);
1613
1614		timeout_process(base);
1615
1616		if (N_ACTIVE_CALLBACKS(base)) {
1617			int n = event_process_active(base);
1618			if ((flags & EVLOOP_ONCE)
1619			    && N_ACTIVE_CALLBACKS(base) == 0
1620			    && n != 0)
1621				done = 1;
1622		} else if (flags & EVLOOP_NONBLOCK)
1623			done = 1;
1624	}
1625	event_debug(("%s: asked to terminate loop.", __func__));
1626
1627done:
1628	clear_time_cache(base);
1629	base->running_loop = 0;
1630
1631	EVBASE_RELEASE_LOCK(base, th_base_lock);
1632
1633	return (retval);
1634}
1635
1636/* Sets up an event for processing once */
1637struct event_once {
1638	struct event ev;
1639
1640	void (*cb)(evutil_socket_t, short, void *);
1641	void *arg;
1642};
1643
1644/* One-time callback to implement event_base_once: invokes the user callback,
1645 * then deletes the allocated storage */
1646static void
1647event_once_cb(evutil_socket_t fd, short events, void *arg)
1648{
1649	struct event_once *eonce = arg;
1650
1651	(*eonce->cb)(fd, events, eonce->arg);
1652	event_debug_unassign(&eonce->ev);
1653	mm_free(eonce);
1654}
1655
1656/* not threadsafe, event scheduled once. */
1657int
1658event_once(evutil_socket_t fd, short events,
1659    void (*callback)(evutil_socket_t, short, void *),
1660    void *arg, const struct timeval *tv)
1661{
1662	return event_base_once(current_base, fd, events, callback, arg, tv);
1663}
1664
1665/* Schedules an event once */
1666int
1667event_base_once(struct event_base *base, evutil_socket_t fd, short events,
1668    void (*callback)(evutil_socket_t, short, void *),
1669    void *arg, const struct timeval *tv)
1670{
1671	struct event_once *eonce;
1672	struct timeval etv;
1673	int res = 0;
1674
1675	/* We cannot support signals that just fire once, or persistent
1676	 * events. */
1677	if (events & (EV_SIGNAL|EV_PERSIST))
1678		return (-1);
1679
1680	if ((eonce = mm_calloc(1, sizeof(struct event_once))) == NULL)
1681		return (-1);
1682
1683	eonce->cb = callback;
1684	eonce->arg = arg;
1685
1686	if (events == EV_TIMEOUT) {
1687		if (tv == NULL) {
1688			evutil_timerclear(&etv);
1689			tv = &etv;
1690		}
1691
1692		evtimer_assign(&eonce->ev, base, event_once_cb, eonce);
1693	} else if (events & (EV_READ|EV_WRITE)) {
1694		events &= EV_READ|EV_WRITE;
1695
1696		event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce);
1697	} else {
1698		/* Bad event combination */
1699		mm_free(eonce);
1700		return (-1);
1701	}
1702
1703	if (res == 0)
1704		res = event_add(&eonce->ev, tv);
1705	if (res != 0) {
1706		mm_free(eonce);
1707		return (res);
1708	}
1709
1710	return (0);
1711}
1712
1713int
1714event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg)
1715{
1716	if (!base)
1717		base = current_base;
1718
1719	_event_debug_assert_not_added(ev);
1720
1721	ev->ev_base = base;
1722
1723	ev->ev_callback = callback;
1724	ev->ev_arg = arg;
1725	ev->ev_fd = fd;
1726	ev->ev_events = events;
1727	ev->ev_res = 0;
1728	ev->ev_flags = EVLIST_INIT;
1729	ev->ev_ncalls = 0;
1730	ev->ev_pncalls = NULL;
1731
1732	if (events & EV_SIGNAL) {
1733		if ((events & (EV_READ|EV_WRITE)) != 0) {
1734			event_warnx("%s: EV_SIGNAL is not compatible with "
1735			    "EV_READ or EV_WRITE", __func__);
1736			return -1;
1737		}
1738		ev->ev_closure = EV_CLOSURE_SIGNAL;
1739	} else {
1740		if (events & EV_PERSIST) {
1741			evutil_timerclear(&ev->ev_io_timeout);
1742			ev->ev_closure = EV_CLOSURE_PERSIST;
1743		} else {
1744			ev->ev_closure = EV_CLOSURE_NONE;
1745		}
1746	}
1747
1748	min_heap_elem_init(ev);
1749
1750	if (base != NULL) {
1751		/* by default, we put new events into the middle priority */
1752		ev->ev_pri = base->nactivequeues / 2;
1753	}
1754
1755	_event_debug_note_setup(ev);
1756
1757	return 0;
1758}
1759
1760int
1761event_base_set(struct event_base *base, struct event *ev)
1762{
1763	/* Only innocent events may be assigned to a different base */
1764	if (ev->ev_flags != EVLIST_INIT)
1765		return (-1);
1766
1767	_event_debug_assert_is_setup(ev);
1768
1769	ev->ev_base = base;
1770	ev->ev_pri = base->nactivequeues/2;
1771
1772	return (0);
1773}
1774
1775void
1776event_set(struct event *ev, evutil_socket_t fd, short events,
1777	  void (*callback)(evutil_socket_t, short, void *), void *arg)
1778{
1779	int r;
1780	r = event_assign(ev, current_base, fd, events, callback, arg);
1781	EVUTIL_ASSERT(r == 0);
1782}
1783
1784struct event *
1785event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
1786{
1787	struct event *ev;
1788	ev = mm_malloc(sizeof(struct event));
1789	if (ev == NULL)
1790		return (NULL);
1791	if (event_assign(ev, base, fd, events, cb, arg) < 0) {
1792		mm_free(ev);
1793		return (NULL);
1794	}
1795
1796	return (ev);
1797}
1798
1799void
1800event_free(struct event *ev)
1801{
1802	_event_debug_assert_is_setup(ev);
1803
1804	/* make sure that this event won't be coming back to haunt us. */
1805	event_del(ev);
1806	_event_debug_note_teardown(ev);
1807	mm_free(ev);
1808
1809}
1810
1811void
1812event_debug_unassign(struct event *ev)
1813{
1814	_event_debug_assert_not_added(ev);
1815	_event_debug_note_teardown(ev);
1816
1817	ev->ev_flags &= ~EVLIST_INIT;
1818}
1819
1820/*
1821 * Set's the priority of an event - if an event is already scheduled
1822 * changing the priority is going to fail.
1823 */
1824
1825int
1826event_priority_set(struct event *ev, int pri)
1827{
1828	_event_debug_assert_is_setup(ev);
1829
1830	if (ev->ev_flags & EVLIST_ACTIVE)
1831		return (-1);
1832	if (pri < 0 || pri >= ev->ev_base->nactivequeues)
1833		return (-1);
1834
1835	ev->ev_pri = pri;
1836
1837	return (0);
1838}
1839
1840/*
1841 * Checks if a specific event is pending or scheduled.
1842 */
1843
1844int
1845event_pending(const struct event *ev, short event, struct timeval *tv)
1846{
1847	int flags = 0;
1848
1849	EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
1850	_event_debug_assert_is_setup(ev);
1851
1852	if (ev->ev_flags & EVLIST_INSERTED)
1853		flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL));
1854	if (ev->ev_flags & EVLIST_ACTIVE)
1855		flags |= ev->ev_res;
1856	if (ev->ev_flags & EVLIST_TIMEOUT)
1857		flags |= EV_TIMEOUT;
1858
1859	event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_SIGNAL);
1860
1861	/* See if there is a timeout that we should report */
1862	if (tv != NULL && (flags & event & EV_TIMEOUT)) {
1863		struct timeval tmp = ev->ev_timeout;
1864		tmp.tv_usec &= MICROSECONDS_MASK;
1865#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
1866		/* correctly remamp to real time */
1867		evutil_timeradd(&ev->ev_base->tv_clock_diff, &tmp, tv);
1868#else
1869		*tv = tmp;
1870#endif
1871	}
1872
1873	EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
1874
1875	return (flags & event);
1876}
1877
1878int
1879event_initialized(const struct event *ev)
1880{
1881	if (!(ev->ev_flags & EVLIST_INIT))
1882		return 0;
1883
1884	return 1;
1885}
1886
1887void
1888event_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)
1889{
1890	_event_debug_assert_is_setup(event);
1891
1892	if (base_out)
1893		*base_out = event->ev_base;
1894	if (fd_out)
1895		*fd_out = event->ev_fd;
1896	if (events_out)
1897		*events_out = event->ev_events;
1898	if (callback_out)
1899		*callback_out = event->ev_callback;
1900	if (arg_out)
1901		*arg_out = event->ev_arg;
1902}
1903
1904size_t
1905event_get_struct_event_size(void)
1906{
1907	return sizeof(struct event);
1908}
1909
1910evutil_socket_t
1911event_get_fd(const struct event *ev)
1912{
1913	_event_debug_assert_is_setup(ev);
1914	return ev->ev_fd;
1915}
1916
1917struct event_base *
1918event_get_base(const struct event *ev)
1919{
1920	_event_debug_assert_is_setup(ev);
1921	return ev->ev_base;
1922}
1923
1924short
1925event_get_events(const struct event *ev)
1926{
1927	_event_debug_assert_is_setup(ev);
1928	return ev->ev_events;
1929}
1930
1931event_callback_fn
1932event_get_callback(const struct event *ev)
1933{
1934	_event_debug_assert_is_setup(ev);
1935	return ev->ev_callback;
1936}
1937
1938void *
1939event_get_callback_arg(const struct event *ev)
1940{
1941	_event_debug_assert_is_setup(ev);
1942	return ev->ev_arg;
1943}
1944
1945int
1946event_add(struct event *ev, const struct timeval *tv)
1947{
1948	int res;
1949
1950	if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
1951		event_warnx("%s: event has no event_base set.", __func__);
1952		return -1;
1953	}
1954
1955	EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
1956
1957	res = event_add_internal(ev, tv, 0);
1958
1959	EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
1960
1961	return (res);
1962}
1963
1964/* Helper callback: wake an event_base from another thread.  This version
1965 * works by writing a byte to one end of a socketpair, so that the event_base
1966 * listening on the other end will wake up as the corresponding event
1967 * triggers */
1968static int
1969evthread_notify_base_default(struct event_base *base)
1970{
1971	char buf[1];
1972	int r;
1973	buf[0] = (char) 0;
1974#ifdef WIN32
1975	r = send(base->th_notify_fd[1], buf, 1, 0);
1976#else
1977	r = write(base->th_notify_fd[1], buf, 1);
1978#endif
1979	return (r < 0 && errno != EAGAIN) ? -1 : 0;
1980}
1981
1982#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
1983/* Helper callback: wake an event_base from another thread.  This version
1984 * assumes that you have a working eventfd() implementation. */
1985static int
1986evthread_notify_base_eventfd(struct event_base *base)
1987{
1988	ev_uint64_t msg = 1;
1989	int r;
1990	do {
1991		r = write(base->th_notify_fd[0], (void*) &msg, sizeof(msg));
1992	} while (r < 0 && errno == EAGAIN);
1993
1994	return (r < 0) ? -1 : 0;
1995}
1996#endif
1997
1998/** Tell the thread currently running the event_loop for base (if any) that it
1999 * needs to stop waiting in its dispatch function (if it is) and process all
2000 * active events and deferred callbacks (if there are any).  */
2001static int
2002evthread_notify_base(struct event_base *base)
2003{
2004	EVENT_BASE_ASSERT_LOCKED(base);
2005	if (!base->th_notify_fn)
2006		return -1;
2007	if (base->is_notify_pending)
2008		return 0;
2009	base->is_notify_pending = 1;
2010	return base->th_notify_fn(base);
2011}
2012
2013/* Implementation function to add an event.  Works just like event_add,
2014 * except: 1) it requires that we have the lock.  2) if tv_is_absolute is set,
2015 * we treat tv as an absolute time, not as an interval to add to the current
2016 * time */
2017static inline int
2018event_add_internal(struct event *ev, const struct timeval *tv,
2019    int tv_is_absolute)
2020{
2021	struct event_base *base = ev->ev_base;
2022	int res = 0;
2023	int notify = 0;
2024
2025	EVENT_BASE_ASSERT_LOCKED(base);
2026	_event_debug_assert_is_setup(ev);
2027
2028	event_debug((
2029		 "event_add: event: %p (fd %d), %s%s%scall %p",
2030		 ev,
2031		 (int)ev->ev_fd,
2032		 ev->ev_events & EV_READ ? "EV_READ " : " ",
2033		 ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
2034		 tv ? "EV_TIMEOUT " : " ",
2035		 ev->ev_callback));
2036
2037	EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
2038
2039	/*
2040	 * prepare for timeout insertion further below, if we get a
2041	 * failure on any step, we should not change any state.
2042	 */
2043	if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
2044		if (min_heap_reserve(&base->timeheap,
2045			1 + min_heap_size(&base->timeheap)) == -1)
2046			return (-1);  /* ENOMEM == errno */
2047	}
2048
2049	/* If the main thread is currently executing a signal event's
2050	 * callback, and we are not the main thread, then we want to wait
2051	 * until the callback is done before we mess with the event, or else
2052	 * we can race on ev_ncalls and ev_pncalls below. */
2053#ifndef _EVENT_DISABLE_THREAD_SUPPORT
2054	if (base->current_event == ev && (ev->ev_events & EV_SIGNAL)
2055	    && !EVBASE_IN_THREAD(base)) {
2056		++base->current_event_waiters;
2057		EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2058	}
2059#endif
2060
2061	if ((ev->ev_events & (EV_READ|EV_WRITE|EV_SIGNAL)) &&
2062	    !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE))) {
2063		if (ev->ev_events & (EV_READ|EV_WRITE))
2064			res = evmap_io_add(base, ev->ev_fd, ev);
2065		else if (ev->ev_events & EV_SIGNAL)
2066			res = evmap_signal_add(base, (int)ev->ev_fd, ev);
2067		if (res != -1)
2068			event_queue_insert(base, ev, EVLIST_INSERTED);
2069		if (res == 1) {
2070			/* evmap says we need to notify the main thread. */
2071			notify = 1;
2072			res = 0;
2073		}
2074	}
2075
2076	/*
2077	 * we should change the timeout state only if the previous event
2078	 * addition succeeded.
2079	 */
2080	if (res != -1 && tv != NULL) {
2081		struct timeval now;
2082		int common_timeout;
2083
2084		/*
2085		 * for persistent timeout events, we remember the
2086		 * timeout value and re-add the event.
2087		 *
2088		 * If tv_is_absolute, this was already set.
2089		 */
2090		if (ev->ev_closure == EV_CLOSURE_PERSIST && !tv_is_absolute)
2091			ev->ev_io_timeout = *tv;
2092
2093		/*
2094		 * we already reserved memory above for the case where we
2095		 * are not replacing an existing timeout.
2096		 */
2097		if (ev->ev_flags & EVLIST_TIMEOUT) {
2098			/* XXX I believe this is needless. */
2099			if (min_heap_elt_is_top(ev))
2100				notify = 1;
2101			event_queue_remove(base, ev, EVLIST_TIMEOUT);
2102		}
2103
2104		/* Check if it is active due to a timeout.  Rescheduling
2105		 * this timeout before the callback can be executed
2106		 * removes it from the active list. */
2107		if ((ev->ev_flags & EVLIST_ACTIVE) &&
2108		    (ev->ev_res & EV_TIMEOUT)) {
2109			if (ev->ev_events & EV_SIGNAL) {
2110				/* See if we are just active executing
2111				 * this event in a loop
2112				 */
2113				if (ev->ev_ncalls && ev->ev_pncalls) {
2114					/* Abort loop */
2115					*ev->ev_pncalls = 0;
2116				}
2117			}
2118
2119			event_queue_remove(base, ev, EVLIST_ACTIVE);
2120		}
2121
2122		gettime(base, &now);
2123
2124		common_timeout = is_common_timeout(tv, base);
2125		if (tv_is_absolute) {
2126			ev->ev_timeout = *tv;
2127		} else if (common_timeout) {
2128			struct timeval tmp = *tv;
2129			tmp.tv_usec &= MICROSECONDS_MASK;
2130			evutil_timeradd(&now, &tmp, &ev->ev_timeout);
2131			ev->ev_timeout.tv_usec |=
2132			    (tv->tv_usec & ~MICROSECONDS_MASK);
2133		} else {
2134			evutil_timeradd(&now, tv, &ev->ev_timeout);
2135		}
2136
2137		event_debug((
2138			 "event_add: timeout in %d seconds, call %p",
2139			 (int)tv->tv_sec, ev->ev_callback));
2140
2141		event_queue_insert(base, ev, EVLIST_TIMEOUT);
2142		if (common_timeout) {
2143			struct common_timeout_list *ctl =
2144			    get_common_timeout_list(base, &ev->ev_timeout);
2145			if (ev == TAILQ_FIRST(&ctl->events)) {
2146				common_timeout_schedule(ctl, &now, ev);
2147			}
2148		} else {
2149			/* See if the earliest timeout is now earlier than it
2150			 * was before: if so, we will need to tell the main
2151			 * thread to wake up earlier than it would
2152			 * otherwise. */
2153			if (min_heap_elt_is_top(ev))
2154				notify = 1;
2155		}
2156	}
2157
2158	/* if we are not in the right thread, we need to wake up the loop */
2159	if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
2160		evthread_notify_base(base);
2161
2162	_event_debug_note_add(ev);
2163
2164	return (res);
2165}
2166
2167int
2168event_del(struct event *ev)
2169{
2170	int res;
2171
2172	if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2173		event_warnx("%s: event has no event_base set.", __func__);
2174		return -1;
2175	}
2176
2177	EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2178
2179	res = event_del_internal(ev);
2180
2181	EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2182
2183	return (res);
2184}
2185
2186/* Helper for event_del: always called with th_base_lock held. */
2187static inline int
2188event_del_internal(struct event *ev)
2189{
2190	struct event_base *base;
2191	int res = 0, notify = 0;
2192
2193	event_debug(("event_del: %p (fd %d), callback %p",
2194		ev, (int)ev->ev_fd, ev->ev_callback));
2195
2196	/* An event without a base has not been added */
2197	if (ev->ev_base == NULL)
2198		return (-1);
2199
2200	EVENT_BASE_ASSERT_LOCKED(ev->ev_base);
2201
2202	/* If the main thread is currently executing this event's callback,
2203	 * and we are not the main thread, then we want to wait until the
2204	 * callback is done before we start removing the event.  That way,
2205	 * when this function returns, it will be safe to free the
2206	 * user-supplied argument. */
2207	base = ev->ev_base;
2208#ifndef _EVENT_DISABLE_THREAD_SUPPORT
2209	if (base->current_event == ev && !EVBASE_IN_THREAD(base)) {
2210		++base->current_event_waiters;
2211		EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2212	}
2213#endif
2214
2215	EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
2216
2217	/* See if we are just active executing this event in a loop */
2218	if (ev->ev_events & EV_SIGNAL) {
2219		if (ev->ev_ncalls && ev->ev_pncalls) {
2220			/* Abort loop */
2221			*ev->ev_pncalls = 0;
2222		}
2223	}
2224
2225	if (ev->ev_flags & EVLIST_TIMEOUT) {
2226		/* NOTE: We never need to notify the main thread because of a
2227		 * deleted timeout event: all that could happen if we don't is
2228		 * that the dispatch loop might wake up too early.  But the
2229		 * point of notifying the main thread _is_ to wake up the
2230		 * dispatch loop early anyway, so we wouldn't gain anything by
2231		 * doing it.
2232		 */
2233		event_queue_remove(base, ev, EVLIST_TIMEOUT);
2234	}
2235
2236	if (ev->ev_flags & EVLIST_ACTIVE)
2237		event_queue_remove(base, ev, EVLIST_ACTIVE);
2238
2239	if (ev->ev_flags & EVLIST_INSERTED) {
2240		event_queue_remove(base, ev, EVLIST_INSERTED);
2241		if (ev->ev_events & (EV_READ|EV_WRITE))
2242			res = evmap_io_del(base, ev->ev_fd, ev);
2243		else
2244			res = evmap_signal_del(base, (int)ev->ev_fd, ev);
2245		if (res == 1) {
2246			/* evmap says we need to notify the main thread. */
2247			notify = 1;
2248			res = 0;
2249		}
2250	}
2251
2252	/* if we are not in the right thread, we need to wake up the loop */
2253	if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
2254		evthread_notify_base(base);
2255
2256	_event_debug_note_del(ev);
2257
2258	return (res);
2259}
2260
2261void
2262event_active(struct event *ev, int res, short ncalls)
2263{
2264	if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2265		event_warnx("%s: event has no event_base set.", __func__);
2266		return;
2267	}
2268
2269	EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2270
2271	_event_debug_assert_is_setup(ev);
2272
2273	event_active_nolock(ev, res, ncalls);
2274
2275	EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2276}
2277
2278
2279void
2280event_active_nolock(struct event *ev, int res, short ncalls)
2281{
2282	struct event_base *base;
2283
2284	event_debug(("event_active: %p (fd %d), res %d, callback %p",
2285		ev, (int)ev->ev_fd, (int)res, ev->ev_callback));
2286
2287
2288	/* We get different kinds of events, add them together */
2289	if (ev->ev_flags & EVLIST_ACTIVE) {
2290		ev->ev_res |= res;
2291		return;
2292	}
2293
2294	base = ev->ev_base;
2295
2296	EVENT_BASE_ASSERT_LOCKED(base);
2297
2298	ev->ev_res = res;
2299
2300	if (ev->ev_pri < base->event_running_priority)
2301		base->event_continue = 1;
2302
2303	if (ev->ev_events & EV_SIGNAL) {
2304#ifndef _EVENT_DISABLE_THREAD_SUPPORT
2305		if (base->current_event == ev && !EVBASE_IN_THREAD(base)) {
2306			++base->current_event_waiters;
2307			EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2308		}
2309#endif
2310		ev->ev_ncalls = ncalls;
2311		ev->ev_pncalls = NULL;
2312	}
2313
2314	event_queue_insert(base, ev, EVLIST_ACTIVE);
2315
2316	if (EVBASE_NEED_NOTIFY(base))
2317		evthread_notify_base(base);
2318}
2319
2320void
2321event_deferred_cb_init(struct deferred_cb *cb, deferred_cb_fn fn, void *arg)
2322{
2323	memset(cb, 0, sizeof(struct deferred_cb));
2324	cb->cb = fn;
2325	cb->arg = arg;
2326}
2327
2328void
2329event_deferred_cb_cancel(struct deferred_cb_queue *queue,
2330    struct deferred_cb *cb)
2331{
2332	if (!queue) {
2333		if (current_base)
2334			queue = &current_base->defer_queue;
2335		else
2336			return;
2337	}
2338
2339	LOCK_DEFERRED_QUEUE(queue);
2340	if (cb->queued) {
2341		TAILQ_REMOVE(&queue->deferred_cb_list, cb, cb_next);
2342		--queue->active_count;
2343		cb->queued = 0;
2344	}
2345	UNLOCK_DEFERRED_QUEUE(queue);
2346}
2347
2348void
2349event_deferred_cb_schedule(struct deferred_cb_queue *queue,
2350    struct deferred_cb *cb)
2351{
2352	if (!queue) {
2353		if (current_base)
2354			queue = &current_base->defer_queue;
2355		else
2356			return;
2357	}
2358
2359	LOCK_DEFERRED_QUEUE(queue);
2360	if (!cb->queued) {
2361		cb->queued = 1;
2362		TAILQ_INSERT_TAIL(&queue->deferred_cb_list, cb, cb_next);
2363		++queue->active_count;
2364		if (queue->notify_fn)
2365			queue->notify_fn(queue, queue->notify_arg);
2366	}
2367	UNLOCK_DEFERRED_QUEUE(queue);
2368}
2369
2370static int
2371timeout_next(struct event_base *base, struct timeval **tv_p)
2372{
2373	/* Caller must hold th_base_lock */
2374	struct timeval now;
2375	struct event *ev;
2376	struct timeval *tv = *tv_p;
2377	int res = 0;
2378
2379	ev = min_heap_top(&base->timeheap);
2380
2381	if (ev == NULL) {
2382		/* if no time-based events are active wait for I/O */
2383		*tv_p = NULL;
2384		goto out;
2385	}
2386
2387	if (gettime(base, &now) == -1) {
2388		res = -1;
2389		goto out;
2390	}
2391
2392	if (evutil_timercmp(&ev->ev_timeout, &now, <=)) {
2393		evutil_timerclear(tv);
2394		goto out;
2395	}
2396
2397	evutil_timersub(&ev->ev_timeout, &now, tv);
2398
2399	EVUTIL_ASSERT(tv->tv_sec >= 0);
2400	EVUTIL_ASSERT(tv->tv_usec >= 0);
2401	event_debug(("timeout_next: in %d seconds", (int)tv->tv_sec));
2402
2403out:
2404	return (res);
2405}
2406
2407/*
2408 * Determines if the time is running backwards by comparing the current time
2409 * against the last time we checked.  Not needed when using clock monotonic.
2410 * If time is running backwards, we adjust the firing time of every event by
2411 * the amount that time seems to have jumped.
2412 */
2413static void
2414timeout_correct(struct event_base *base, struct timeval *tv)
2415{
2416	/* Caller must hold th_base_lock. */
2417	struct event **pev;
2418	unsigned int size;
2419	struct timeval off;
2420	int i;
2421
2422	if (use_monotonic)
2423		return;
2424
2425	/* Check if time is running backwards */
2426	gettime(base, tv);
2427
2428	if (evutil_timercmp(tv, &base->event_tv, >=)) {
2429		base->event_tv = *tv;
2430		return;
2431	}
2432
2433	event_debug(("%s: time is running backwards, corrected",
2434		    __func__));
2435	evutil_timersub(&base->event_tv, tv, &off);
2436
2437	/*
2438	 * We can modify the key element of the node without destroying
2439	 * the minheap property, because we change every element.
2440	 */
2441	pev = base->timeheap.p;
2442	size = base->timeheap.n;
2443	for (; size-- > 0; ++pev) {
2444		struct timeval *ev_tv = &(**pev).ev_timeout;
2445		evutil_timersub(ev_tv, &off, ev_tv);
2446	}
2447	for (i=0; i<base->n_common_timeouts; ++i) {
2448		struct event *ev;
2449		struct common_timeout_list *ctl =
2450		    base->common_timeout_queues[i];
2451		TAILQ_FOREACH(ev, &ctl->events,
2452		    ev_timeout_pos.ev_next_with_common_timeout) {
2453			struct timeval *ev_tv = &ev->ev_timeout;
2454			ev_tv->tv_usec &= MICROSECONDS_MASK;
2455			evutil_timersub(ev_tv, &off, ev_tv);
2456			ev_tv->tv_usec |= COMMON_TIMEOUT_MAGIC |
2457			    (i<<COMMON_TIMEOUT_IDX_SHIFT);
2458		}
2459	}
2460
2461	/* Now remember what the new time turned out to be. */
2462	base->event_tv = *tv;
2463}
2464
2465/* Activate every event whose timeout has elapsed. */
2466static void
2467timeout_process(struct event_base *base)
2468{
2469	/* Caller must hold lock. */
2470	struct timeval now;
2471	struct event *ev;
2472
2473	if (min_heap_empty(&base->timeheap)) {
2474		return;
2475	}
2476
2477	gettime(base, &now);
2478
2479	while ((ev = min_heap_top(&base->timeheap))) {
2480		if (evutil_timercmp(&ev->ev_timeout, &now, >))
2481			break;
2482
2483		/* delete this event from the I/O queues */
2484		event_del_internal(ev);
2485
2486		event_debug(("timeout_process: call %p",
2487			 ev->ev_callback));
2488		event_active_nolock(ev, EV_TIMEOUT, 1);
2489	}
2490}
2491
2492/* Remove 'ev' from 'queue' (EVLIST_...) in base. */
2493static void
2494event_queue_remove(struct event_base *base, struct event *ev, int queue)
2495{
2496	EVENT_BASE_ASSERT_LOCKED(base);
2497
2498	if (!(ev->ev_flags & queue)) {
2499		event_errx(1, "%s: %p(fd %d) not on queue %x", __func__,
2500			   ev, ev->ev_fd, queue);
2501		return;
2502	}
2503
2504	if (~ev->ev_flags & EVLIST_INTERNAL)
2505		base->event_count--;
2506
2507	ev->ev_flags &= ~queue;
2508	switch (queue) {
2509	case EVLIST_INSERTED:
2510		TAILQ_REMOVE(&base->eventqueue, ev, ev_next);
2511		break;
2512	case EVLIST_ACTIVE:
2513		base->event_count_active--;
2514		TAILQ_REMOVE(&base->activequeues[ev->ev_pri],
2515		    ev, ev_active_next);
2516		break;
2517	case EVLIST_TIMEOUT:
2518		if (is_common_timeout(&ev->ev_timeout, base)) {
2519			struct common_timeout_list *ctl =
2520			    get_common_timeout_list(base, &ev->ev_timeout);
2521			TAILQ_REMOVE(&ctl->events, ev,
2522			    ev_timeout_pos.ev_next_with_common_timeout);
2523		} else {
2524			min_heap_erase(&base->timeheap, ev);
2525		}
2526		break;
2527	default:
2528		event_errx(1, "%s: unknown queue %x", __func__, queue);
2529	}
2530}
2531
2532/* Add 'ev' to the common timeout list in 'ev'. */
2533static void
2534insert_common_timeout_inorder(struct common_timeout_list *ctl,
2535    struct event *ev)
2536{
2537	struct event *e;
2538	/* By all logic, we should just be able to append 'ev' to the end of
2539	 * ctl->events, since the timeout on each 'ev' is set to {the common
2540	 * timeout} + {the time when we add the event}, and so the events
2541	 * should arrive in order of their timeeouts.  But just in case
2542	 * there's some wacky threading issue going on, we do a search from
2543	 * the end of 'ev' to find the right insertion point.
2544	 */
2545	TAILQ_FOREACH_REVERSE(e, &ctl->events,
2546	    event_list, ev_timeout_pos.ev_next_with_common_timeout) {
2547		/* This timercmp is a little sneaky, since both ev and e have
2548		 * magic values in tv_usec.  Fortunately, they ought to have
2549		 * the _same_ magic values in tv_usec.  Let's assert for that.
2550		 */
2551		EVUTIL_ASSERT(
2552			is_same_common_timeout(&e->ev_timeout, &ev->ev_timeout));
2553		if (evutil_timercmp(&ev->ev_timeout, &e->ev_timeout, >=)) {
2554			TAILQ_INSERT_AFTER(&ctl->events, e, ev,
2555			    ev_timeout_pos.ev_next_with_common_timeout);
2556			return;
2557		}
2558	}
2559	TAILQ_INSERT_HEAD(&ctl->events, ev,
2560	    ev_timeout_pos.ev_next_with_common_timeout);
2561}
2562
2563static void
2564event_queue_insert(struct event_base *base, struct event *ev, int queue)
2565{
2566	EVENT_BASE_ASSERT_LOCKED(base);
2567
2568	if (ev->ev_flags & queue) {
2569		/* Double insertion is possible for active events */
2570		if (queue & EVLIST_ACTIVE)
2571			return;
2572
2573		event_errx(1, "%s: %p(fd %d) already on queue %x", __func__,
2574			   ev, ev->ev_fd, queue);
2575		return;
2576	}
2577
2578	if (~ev->ev_flags & EVLIST_INTERNAL)
2579		base->event_count++;
2580
2581	ev->ev_flags |= queue;
2582	switch (queue) {
2583	case EVLIST_INSERTED:
2584		TAILQ_INSERT_TAIL(&base->eventqueue, ev, ev_next);
2585		break;
2586	case EVLIST_ACTIVE:
2587		base->event_count_active++;
2588		TAILQ_INSERT_TAIL(&base->activequeues[ev->ev_pri],
2589		    ev,ev_active_next);
2590		break;
2591	case EVLIST_TIMEOUT: {
2592		if (is_common_timeout(&ev->ev_timeout, base)) {
2593			struct common_timeout_list *ctl =
2594			    get_common_timeout_list(base, &ev->ev_timeout);
2595			insert_common_timeout_inorder(ctl, ev);
2596		} else
2597			min_heap_push(&base->timeheap, ev);
2598		break;
2599	}
2600	default:
2601		event_errx(1, "%s: unknown queue %x", __func__, queue);
2602	}
2603}
2604
2605/* Functions for debugging */
2606
2607const char *
2608event_get_version(void)
2609{
2610	return (_EVENT_VERSION);
2611}
2612
2613ev_uint32_t
2614event_get_version_number(void)
2615{
2616	return (_EVENT_NUMERIC_VERSION);
2617}
2618
2619/*
2620 * No thread-safe interface needed - the information should be the same
2621 * for all threads.
2622 */
2623
2624const char *
2625event_get_method(void)
2626{
2627	return (current_base->evsel->name);
2628}
2629
2630#ifndef _EVENT_DISABLE_MM_REPLACEMENT
2631static void *(*_mm_malloc_fn)(size_t sz) = NULL;
2632static void *(*_mm_realloc_fn)(void *p, size_t sz) = NULL;
2633static void (*_mm_free_fn)(void *p) = NULL;
2634
2635void *
2636event_mm_malloc_(size_t sz)
2637{
2638	if (_mm_malloc_fn)
2639		return _mm_malloc_fn(sz);
2640	else
2641		return malloc(sz);
2642}
2643
2644void *
2645event_mm_calloc_(size_t count, size_t size)
2646{
2647	if (_mm_malloc_fn) {
2648		size_t sz = count * size;
2649		void *p = _mm_malloc_fn(sz);
2650		if (p)
2651			memset(p, 0, sz);
2652		return p;
2653	} else
2654		return calloc(count, size);
2655}
2656
2657char *
2658event_mm_strdup_(const char *str)
2659{
2660	if (_mm_malloc_fn) {
2661		size_t ln = strlen(str);
2662		void *p = _mm_malloc_fn(ln+1);
2663		if (p)
2664			memcpy(p, str, ln+1);
2665		return p;
2666	} else
2667#ifdef WIN32
2668		return _strdup(str);
2669#else
2670		return strdup(str);
2671#endif
2672}
2673
2674void *
2675event_mm_realloc_(void *ptr, size_t sz)
2676{
2677	if (_mm_realloc_fn)
2678		return _mm_realloc_fn(ptr, sz);
2679	else
2680		return realloc(ptr, sz);
2681}
2682
2683void
2684event_mm_free_(void *ptr)
2685{
2686	if (_mm_free_fn)
2687		_mm_free_fn(ptr);
2688	else
2689		free(ptr);
2690}
2691
2692void
2693event_set_mem_functions(void *(*malloc_fn)(size_t sz),
2694			void *(*realloc_fn)(void *ptr, size_t sz),
2695			void (*free_fn)(void *ptr))
2696{
2697	_mm_malloc_fn = malloc_fn;
2698	_mm_realloc_fn = realloc_fn;
2699	_mm_free_fn = free_fn;
2700}
2701#endif
2702
2703#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
2704static void
2705evthread_notify_drain_eventfd(evutil_socket_t fd, short what, void *arg)
2706{
2707	ev_uint64_t msg;
2708	ev_ssize_t r;
2709	struct event_base *base = arg;
2710
2711	r = read(fd, (void*) &msg, sizeof(msg));
2712	if (r<0 && errno != EAGAIN) {
2713		event_sock_warn(fd, "Error reading from eventfd");
2714	}
2715	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2716	base->is_notify_pending = 0;
2717	EVBASE_RELEASE_LOCK(base, th_base_lock);
2718}
2719#endif
2720
2721static void
2722evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg)
2723{
2724	unsigned char buf[1024];
2725	struct event_base *base = arg;
2726#ifdef WIN32
2727	while (recv(fd, (char*)buf, sizeof(buf), 0) > 0)
2728		;
2729#else
2730	while (read(fd, (char*)buf, sizeof(buf)) > 0)
2731		;
2732#endif
2733
2734	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2735	base->is_notify_pending = 0;
2736	EVBASE_RELEASE_LOCK(base, th_base_lock);
2737}
2738
2739int
2740evthread_make_base_notifiable(struct event_base *base)
2741{
2742	void (*cb)(evutil_socket_t, short, void *) = evthread_notify_drain_default;
2743	int (*notify)(struct event_base *) = evthread_notify_base_default;
2744
2745	/* XXXX grab the lock here? */
2746	if (!base)
2747		return -1;
2748
2749	if (base->th_notify_fd[0] >= 0)
2750		return 0;
2751
2752#if defined(_EVENT_HAVE_EVENTFD) && defined(_EVENT_HAVE_SYS_EVENTFD_H)
2753#ifndef EFD_CLOEXEC
2754#define EFD_CLOEXEC 0
2755#endif
2756	base->th_notify_fd[0] = eventfd(0, EFD_CLOEXEC);
2757	if (base->th_notify_fd[0] >= 0) {
2758		evutil_make_socket_closeonexec(base->th_notify_fd[0]);
2759		notify = evthread_notify_base_eventfd;
2760		cb = evthread_notify_drain_eventfd;
2761	}
2762#endif
2763#if defined(_EVENT_HAVE_PIPE)
2764	if (base->th_notify_fd[0] < 0) {
2765		if ((base->evsel->features & EV_FEATURE_FDS)) {
2766			if (pipe(base->th_notify_fd) < 0) {
2767				event_warn("%s: pipe", __func__);
2768			} else {
2769				evutil_make_socket_closeonexec(base->th_notify_fd[0]);
2770				evutil_make_socket_closeonexec(base->th_notify_fd[1]);
2771			}
2772		}
2773	}
2774#endif
2775
2776#ifdef WIN32
2777#define LOCAL_SOCKETPAIR_AF AF_INET
2778#else
2779#define LOCAL_SOCKETPAIR_AF AF_UNIX
2780#endif
2781	if (base->th_notify_fd[0] < 0) {
2782		if (evutil_socketpair(LOCAL_SOCKETPAIR_AF, SOCK_STREAM, 0,
2783			base->th_notify_fd) == -1) {
2784			event_sock_warn(-1, "%s: socketpair", __func__);
2785			return (-1);
2786		} else {
2787			evutil_make_socket_closeonexec(base->th_notify_fd[0]);
2788			evutil_make_socket_closeonexec(base->th_notify_fd[1]);
2789		}
2790	}
2791
2792	evutil_make_socket_nonblocking(base->th_notify_fd[0]);
2793
2794	base->th_notify_fn = notify;
2795
2796	/*
2797	  Making the second socket nonblocking is a bit subtle, given that we
2798	  ignore any EAGAIN returns when writing to it, and you don't usally
2799	  do that for a nonblocking socket. But if the kernel gives us EAGAIN,
2800	  then there's no need to add any more data to the buffer, since
2801	  the main thread is already either about to wake up and drain it,
2802	  or woken up and in the process of draining it.
2803	*/
2804	if (base->th_notify_fd[1] > 0)
2805		evutil_make_socket_nonblocking(base->th_notify_fd[1]);
2806
2807	/* prepare an event that we can use for wakeup */
2808	event_assign(&base->th_notify, base, base->th_notify_fd[0],
2809				 EV_READ|EV_PERSIST, cb, base);
2810
2811	/* we need to mark this as internal event */
2812	base->th_notify.ev_flags |= EVLIST_INTERNAL;
2813	event_priority_set(&base->th_notify, 0);
2814
2815	return event_add(&base->th_notify, NULL);
2816}
2817
2818void
2819event_base_dump_events(struct event_base *base, FILE *output)
2820{
2821	struct event *e;
2822	int i;
2823	fprintf(output, "Inserted events:\n");
2824	TAILQ_FOREACH(e, &base->eventqueue, ev_next) {
2825		fprintf(output, "  %p [fd %ld]%s%s%s%s%s\n",
2826				(void*)e, (long)e->ev_fd,
2827				(e->ev_events&EV_READ)?" Read":"",
2828				(e->ev_events&EV_WRITE)?" Write":"",
2829				(e->ev_events&EV_SIGNAL)?" Signal":"",
2830				(e->ev_events&EV_TIMEOUT)?" Timeout":"",
2831				(e->ev_events&EV_PERSIST)?" Persist":"");
2832
2833	}
2834	for (i = 0; i < base->nactivequeues; ++i) {
2835		if (TAILQ_EMPTY(&base->activequeues[i]))
2836			continue;
2837		fprintf(output, "Active events [priority %d]:\n", i);
2838		TAILQ_FOREACH(e, &base->eventqueue, ev_next) {
2839			fprintf(output, "  %p [fd %ld]%s%s%s%s\n",
2840					(void*)e, (long)e->ev_fd,
2841					(e->ev_res&EV_READ)?" Read active":"",
2842					(e->ev_res&EV_WRITE)?" Write active":"",
2843					(e->ev_res&EV_SIGNAL)?" Signal active":"",
2844					(e->ev_res&EV_TIMEOUT)?" Timeout active":"");
2845		}
2846	}
2847}
2848
2849void
2850event_base_add_virtual(struct event_base *base)
2851{
2852	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2853	base->virtual_event_count++;
2854	EVBASE_RELEASE_LOCK(base, th_base_lock);
2855}
2856
2857void
2858event_base_del_virtual(struct event_base *base)
2859{
2860	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2861	EVUTIL_ASSERT(base->virtual_event_count > 0);
2862	base->virtual_event_count--;
2863	if (base->virtual_event_count == 0 && EVBASE_NEED_NOTIFY(base))
2864		evthread_notify_base(base);
2865	EVBASE_RELEASE_LOCK(base, th_base_lock);
2866}
2867
2868#ifndef _EVENT_DISABLE_THREAD_SUPPORT
2869int
2870event_global_setup_locks_(const int enable_locks)
2871{
2872#ifndef _EVENT_DISABLE_DEBUG_MODE
2873	EVTHREAD_SETUP_GLOBAL_LOCK(_event_debug_map_lock, 0);
2874#endif
2875	if (evsig_global_setup_locks_(enable_locks) < 0)
2876		return -1;
2877	if (evutil_secure_rng_global_setup_locks_(enable_locks) < 0)
2878		return -1;
2879	return 0;
2880}
2881#endif
2882
2883void
2884event_base_assert_ok(struct event_base *base)
2885{
2886	int i;
2887	EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2888	evmap_check_integrity(base);
2889
2890	/* Check the heap property */
2891	for (i = 1; i < (int)base->timeheap.n; ++i) {
2892		int parent = (i - 1) / 2;
2893		struct event *ev, *p_ev;
2894		ev = base->timeheap.p[i];
2895		p_ev = base->timeheap.p[parent];
2896		EVUTIL_ASSERT(ev->ev_flags & EV_TIMEOUT);
2897		EVUTIL_ASSERT(evutil_timercmp(&p_ev->ev_timeout, &ev->ev_timeout, <=));
2898		EVUTIL_ASSERT(ev->ev_timeout_pos.min_heap_idx == i);
2899	}
2900
2901	/* Check that the common timeouts are fine */
2902	for (i = 0; i < base->n_common_timeouts; ++i) {
2903		struct common_timeout_list *ctl = base->common_timeout_queues[i];
2904		struct event *last=NULL, *ev;
2905		TAILQ_FOREACH(ev, &ctl->events, ev_timeout_pos.ev_next_with_common_timeout) {
2906			if (last)
2907				EVUTIL_ASSERT(evutil_timercmp(&last->ev_timeout, &ev->ev_timeout, <=));
2908			EVUTIL_ASSERT(ev->ev_flags & EV_TIMEOUT);
2909			EVUTIL_ASSERT(is_common_timeout(&ev->ev_timeout,base));
2910			EVUTIL_ASSERT(COMMON_TIMEOUT_IDX(&ev->ev_timeout) == i);
2911			last = ev;
2912		}
2913	}
2914
2915	EVBASE_RELEASE_LOCK(base, th_base_lock);
2916}
2917