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