blob: b2ad341026a835254f85f8fe7c76f28c4591151c [file] [log] [blame]
Christopher Wileye8679812015-07-01 13:36:18 -07001/*
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"
Narayan Kamathfc74cb42017-09-13 12:53:52 +010028#include "evconfig-private.h"
Christopher Wileye8679812015-07-01 13:36:18 -070029
Narayan Kamathfc74cb42017-09-13 12:53:52 +010030#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -070031#include <winsock2.h>
32#define WIN32_LEAN_AND_MEAN
33#include <windows.h>
34#undef WIN32_LEAN_AND_MEAN
35#endif
36#include <sys/types.h>
Narayan Kamathfc74cb42017-09-13 12:53:52 +010037#if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H)
Christopher Wileye8679812015-07-01 13:36:18 -070038#include <sys/time.h>
39#endif
40#include <sys/queue.h>
Narayan Kamathfc74cb42017-09-13 12:53:52 +010041#ifdef EVENT__HAVE_SYS_SOCKET_H
Christopher Wileye8679812015-07-01 13:36:18 -070042#include <sys/socket.h>
43#endif
44#include <stdio.h>
45#include <stdlib.h>
Narayan Kamathfc74cb42017-09-13 12:53:52 +010046#ifdef EVENT__HAVE_UNISTD_H
Christopher Wileye8679812015-07-01 13:36:18 -070047#include <unistd.h>
48#endif
Christopher Wileye8679812015-07-01 13:36:18 -070049#include <ctype.h>
50#include <errno.h>
51#include <signal.h>
52#include <string.h>
53#include <time.h>
Narayan Kamathfc74cb42017-09-13 12:53:52 +010054#include <limits.h>
Haibo Huangb2279672019-05-31 16:12:39 -070055#ifdef EVENT__HAVE_FCNTL_H
56#include <fcntl.h>
57#endif
Christopher Wileye8679812015-07-01 13:36:18 -070058
59#include "event2/event.h"
60#include "event2/event_struct.h"
61#include "event2/event_compat.h"
62#include "event-internal.h"
63#include "defer-internal.h"
64#include "evthread-internal.h"
65#include "event2/thread.h"
66#include "event2/util.h"
67#include "log-internal.h"
68#include "evmap-internal.h"
69#include "iocp-internal.h"
70#include "changelist-internal.h"
Narayan Kamathfc74cb42017-09-13 12:53:52 +010071#define HT_NO_CACHE_HASH_VALUES
Christopher Wileye8679812015-07-01 13:36:18 -070072#include "ht-internal.h"
73#include "util-internal.h"
74
Narayan Kamathfc74cb42017-09-13 12:53:52 +010075
76#ifdef EVENT__HAVE_WORKING_KQUEUE
77#include "kqueue-internal.h"
78#endif
79
80#ifdef EVENT__HAVE_EVENT_PORTS
Christopher Wileye8679812015-07-01 13:36:18 -070081extern const struct eventop evportops;
82#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +010083#ifdef EVENT__HAVE_SELECT
Christopher Wileye8679812015-07-01 13:36:18 -070084extern const struct eventop selectops;
85#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +010086#ifdef EVENT__HAVE_POLL
Christopher Wileye8679812015-07-01 13:36:18 -070087extern const struct eventop pollops;
88#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +010089#ifdef EVENT__HAVE_EPOLL
Christopher Wileye8679812015-07-01 13:36:18 -070090extern const struct eventop epollops;
91#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +010092#ifdef EVENT__HAVE_WORKING_KQUEUE
Christopher Wileye8679812015-07-01 13:36:18 -070093extern const struct eventop kqops;
94#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +010095#ifdef EVENT__HAVE_DEVPOLL
Christopher Wileye8679812015-07-01 13:36:18 -070096extern const struct eventop devpollops;
97#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +010098#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -070099extern const struct eventop win32ops;
100#endif
101
102/* Array of backends in order of preference. */
103static const struct eventop *eventops[] = {
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100104#ifdef EVENT__HAVE_EVENT_PORTS
Christopher Wileye8679812015-07-01 13:36:18 -0700105 &evportops,
106#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100107#ifdef EVENT__HAVE_WORKING_KQUEUE
Christopher Wileye8679812015-07-01 13:36:18 -0700108 &kqops,
109#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100110#ifdef EVENT__HAVE_EPOLL
Christopher Wileye8679812015-07-01 13:36:18 -0700111 &epollops,
112#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100113#ifdef EVENT__HAVE_DEVPOLL
Christopher Wileye8679812015-07-01 13:36:18 -0700114 &devpollops,
115#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100116#ifdef EVENT__HAVE_POLL
Christopher Wileye8679812015-07-01 13:36:18 -0700117 &pollops,
118#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100119#ifdef EVENT__HAVE_SELECT
Christopher Wileye8679812015-07-01 13:36:18 -0700120 &selectops,
121#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100122#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -0700123 &win32ops,
124#endif
125 NULL
126};
127
128/* Global state; deprecated */
Haibo Huangb2279672019-05-31 16:12:39 -0700129EVENT2_EXPORT_SYMBOL
Christopher Wileye8679812015-07-01 13:36:18 -0700130struct event_base *event_global_current_base_ = NULL;
131#define current_base event_global_current_base_
132
133/* Global state */
134
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100135static void *event_self_cbarg_ptr_ = NULL;
Christopher Wileye8679812015-07-01 13:36:18 -0700136
137/* Prototypes */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100138static void event_queue_insert_active(struct event_base *, struct event_callback *);
139static void event_queue_insert_active_later(struct event_base *, struct event_callback *);
140static void event_queue_insert_timeout(struct event_base *, struct event *);
141static void event_queue_insert_inserted(struct event_base *, struct event *);
142static void event_queue_remove_active(struct event_base *, struct event_callback *);
143static void event_queue_remove_active_later(struct event_base *, struct event_callback *);
144static void event_queue_remove_timeout(struct event_base *, struct event *);
145static void event_queue_remove_inserted(struct event_base *, struct event *);
146static void event_queue_make_later_events_active(struct event_base *base);
Christopher Wileye8679812015-07-01 13:36:18 -0700147
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100148static int evthread_make_base_notifiable_nolock_(struct event_base *base);
149static int event_del_(struct event *ev, int blocking);
150
151#ifdef USE_REINSERT_TIMEOUT
152/* This code seems buggy; only turn it on if we find out what the trouble is. */
153static void event_queue_reinsert_timeout(struct event_base *,struct event *, int was_common, int is_common, int old_timeout_idx);
154#endif
155
Christopher Wileye8679812015-07-01 13:36:18 -0700156static int event_haveevents(struct event_base *);
157
158static int event_process_active(struct event_base *);
159
160static int timeout_next(struct event_base *, struct timeval **);
161static void timeout_process(struct event_base *);
Christopher Wileye8679812015-07-01 13:36:18 -0700162
163static inline void event_signal_closure(struct event_base *, struct event *ev);
164static inline void event_persist_closure(struct event_base *, struct event *ev);
165
166static int evthread_notify_base(struct event_base *base);
167
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100168static void insert_common_timeout_inorder(struct common_timeout_list *ctl,
169 struct event *ev);
170
171#ifndef EVENT__DISABLE_DEBUG_MODE
Christopher Wileye8679812015-07-01 13:36:18 -0700172/* These functions implement a hashtable of which 'struct event *' structures
173 * have been setup or added. We don't want to trust the content of the struct
174 * event itself, since we're trying to work through cases where an event gets
175 * clobbered or freed. Instead, we keep a hashtable indexed by the pointer.
176 */
177
178struct event_debug_entry {
179 HT_ENTRY(event_debug_entry) node;
180 const struct event *ptr;
181 unsigned added : 1;
182};
183
184static inline unsigned
185hash_debug_entry(const struct event_debug_entry *e)
186{
187 /* We need to do this silliness to convince compilers that we
188 * honestly mean to cast e->ptr to an integer, and discard any
189 * part of it that doesn't fit in an unsigned.
190 */
191 unsigned u = (unsigned) ((ev_uintptr_t) e->ptr);
192 /* Our hashtable implementation is pretty sensitive to low bits,
193 * and every struct event is over 64 bytes in size, so we can
194 * just say >>6. */
195 return (u >> 6);
196}
197
198static inline int
199eq_debug_entry(const struct event_debug_entry *a,
200 const struct event_debug_entry *b)
201{
202 return a->ptr == b->ptr;
203}
204
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100205int event_debug_mode_on_ = 0;
206
207
208#if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE)
209/**
210 * @brief debug mode variable which is set for any function/structure that needs
211 * to be shared across threads (if thread support is enabled).
212 *
213 * When and if evthreads are initialized, this variable will be evaluated,
214 * and if set to something other than zero, this means the evthread setup
215 * functions were called out of order.
216 *
217 * See: "Locks and threading" in the documentation.
218 */
219int event_debug_created_threadable_ctx_ = 0;
220#endif
221
Christopher Wileye8679812015-07-01 13:36:18 -0700222/* Set if it's too late to enable event_debug_mode. */
223static int event_debug_mode_too_late = 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100224#ifndef EVENT__DISABLE_THREAD_SUPPORT
225static void *event_debug_map_lock_ = NULL;
Christopher Wileye8679812015-07-01 13:36:18 -0700226#endif
227static HT_HEAD(event_debug_map, event_debug_entry) global_debug_map =
228 HT_INITIALIZER();
229
230HT_PROTOTYPE(event_debug_map, event_debug_entry, node, hash_debug_entry,
231 eq_debug_entry)
232HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry,
233 eq_debug_entry, 0.5, mm_malloc, mm_realloc, mm_free)
234
Haibo Huangb2279672019-05-31 16:12:39 -0700235/* record that ev is now setup (that is, ready for an add) */
236static void event_debug_note_setup_(const struct event *ev)
237{
238 struct event_debug_entry *dent, find;
239
240 if (!event_debug_mode_on_)
241 goto out;
242
243 find.ptr = ev;
244 EVLOCK_LOCK(event_debug_map_lock_, 0);
245 dent = HT_FIND(event_debug_map, &global_debug_map, &find);
246 if (dent) {
247 dent->added = 0;
248 } else {
249 dent = mm_malloc(sizeof(*dent));
250 if (!dent)
251 event_err(1,
252 "Out of memory in debugging code");
253 dent->ptr = ev;
254 dent->added = 0;
255 HT_INSERT(event_debug_map, &global_debug_map, dent);
256 }
257 EVLOCK_UNLOCK(event_debug_map_lock_, 0);
258
259out:
260 event_debug_mode_too_late = 1;
261}
262/* record that ev is no longer setup */
263static void event_debug_note_teardown_(const struct event *ev)
264{
265 struct event_debug_entry *dent, find;
266
267 if (!event_debug_mode_on_)
268 goto out;
269
270 find.ptr = ev;
271 EVLOCK_LOCK(event_debug_map_lock_, 0);
272 dent = HT_REMOVE(event_debug_map, &global_debug_map, &find);
273 if (dent)
274 mm_free(dent);
275 EVLOCK_UNLOCK(event_debug_map_lock_, 0);
276
277out:
278 event_debug_mode_too_late = 1;
279}
Christopher Wileye8679812015-07-01 13:36:18 -0700280/* Macro: record that ev is now added */
Haibo Huangb2279672019-05-31 16:12:39 -0700281static void event_debug_note_add_(const struct event *ev)
282{
283 struct event_debug_entry *dent,find;
284
285 if (!event_debug_mode_on_)
286 goto out;
287
288 find.ptr = ev;
289 EVLOCK_LOCK(event_debug_map_lock_, 0);
290 dent = HT_FIND(event_debug_map, &global_debug_map, &find);
291 if (dent) {
292 dent->added = 1;
293 } else {
294 event_errx(EVENT_ERR_ABORT_,
295 "%s: noting an add on a non-setup event %p"
296 " (events: 0x%x, fd: "EV_SOCK_FMT
297 ", flags: 0x%x)",
298 __func__, ev, ev->ev_events,
299 EV_SOCK_ARG(ev->ev_fd), ev->ev_flags);
300 }
301 EVLOCK_UNLOCK(event_debug_map_lock_, 0);
302
303out:
304 event_debug_mode_too_late = 1;
305}
306/* record that ev is no longer added */
307static void event_debug_note_del_(const struct event *ev)
308{
309 struct event_debug_entry *dent, find;
310
311 if (!event_debug_mode_on_)
312 goto out;
313
314 find.ptr = ev;
315 EVLOCK_LOCK(event_debug_map_lock_, 0);
316 dent = HT_FIND(event_debug_map, &global_debug_map, &find);
317 if (dent) {
318 dent->added = 0;
319 } else {
320 event_errx(EVENT_ERR_ABORT_,
321 "%s: noting a del on a non-setup event %p"
322 " (events: 0x%x, fd: "EV_SOCK_FMT
323 ", flags: 0x%x)",
324 __func__, ev, ev->ev_events,
325 EV_SOCK_ARG(ev->ev_fd), ev->ev_flags);
326 }
327 EVLOCK_UNLOCK(event_debug_map_lock_, 0);
328
329out:
330 event_debug_mode_too_late = 1;
331}
332/* assert that ev is setup (i.e., okay to add or inspect) */
333static void event_debug_assert_is_setup_(const struct event *ev)
334{
335 struct event_debug_entry *dent, find;
336
337 if (!event_debug_mode_on_)
338 return;
339
340 find.ptr = ev;
341 EVLOCK_LOCK(event_debug_map_lock_, 0);
342 dent = HT_FIND(event_debug_map, &global_debug_map, &find);
343 if (!dent) {
344 event_errx(EVENT_ERR_ABORT_,
345 "%s called on a non-initialized event %p"
346 " (events: 0x%x, fd: "EV_SOCK_FMT
347 ", flags: 0x%x)",
348 __func__, ev, ev->ev_events,
349 EV_SOCK_ARG(ev->ev_fd), ev->ev_flags);
350 }
351 EVLOCK_UNLOCK(event_debug_map_lock_, 0);
352}
353/* assert that ev is not added (i.e., okay to tear down or set up again) */
354static void event_debug_assert_not_added_(const struct event *ev)
355{
356 struct event_debug_entry *dent, find;
357
358 if (!event_debug_mode_on_)
359 return;
360
361 find.ptr = ev;
362 EVLOCK_LOCK(event_debug_map_lock_, 0);
363 dent = HT_FIND(event_debug_map, &global_debug_map, &find);
364 if (dent && dent->added) {
365 event_errx(EVENT_ERR_ABORT_,
366 "%s called on an already added event %p"
367 " (events: 0x%x, fd: "EV_SOCK_FMT", "
368 "flags: 0x%x)",
369 __func__, ev, ev->ev_events,
370 EV_SOCK_ARG(ev->ev_fd), ev->ev_flags);
371 }
372 EVLOCK_UNLOCK(event_debug_map_lock_, 0);
373}
374static void event_debug_assert_socket_nonblocking_(evutil_socket_t fd)
375{
376 if (!event_debug_mode_on_)
377 return;
378 if (fd < 0)
379 return;
380
381#ifndef _WIN32
382 {
383 int flags;
384 if ((flags = fcntl(fd, F_GETFL, NULL)) >= 0) {
385 EVUTIL_ASSERT(flags & O_NONBLOCK);
386 }
387 }
388#endif
389}
Christopher Wileye8679812015-07-01 13:36:18 -0700390#else
Haibo Huangb2279672019-05-31 16:12:39 -0700391static void event_debug_note_setup_(const struct event *ev) { (void)ev; }
392static void event_debug_note_teardown_(const struct event *ev) { (void)ev; }
393static void event_debug_note_add_(const struct event *ev) { (void)ev; }
394static void event_debug_note_del_(const struct event *ev) { (void)ev; }
395static void event_debug_assert_is_setup_(const struct event *ev) { (void)ev; }
396static void event_debug_assert_not_added_(const struct event *ev) { (void)ev; }
397static void event_debug_assert_socket_nonblocking_(evutil_socket_t fd) { (void)fd; }
Christopher Wileye8679812015-07-01 13:36:18 -0700398#endif
399
400#define EVENT_BASE_ASSERT_LOCKED(base) \
401 EVLOCK_ASSERT_LOCKED((base)->th_base_lock)
402
Christopher Wileye8679812015-07-01 13:36:18 -0700403/* How often (in seconds) do we check for changes in wall clock time relative
404 * to monotonic time? Set this to -1 for 'never.' */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100405#define CLOCK_SYNC_INTERVAL 5
Christopher Wileye8679812015-07-01 13:36:18 -0700406
407/** Set 'tp' to the current time according to 'base'. We must hold the lock
408 * on 'base'. If there is a cached time, return it. Otherwise, use
409 * clock_gettime or gettimeofday as appropriate to find out the right time.
410 * Return 0 on success, -1 on failure.
411 */
412static int
413gettime(struct event_base *base, struct timeval *tp)
414{
415 EVENT_BASE_ASSERT_LOCKED(base);
416
417 if (base->tv_cache.tv_sec) {
418 *tp = base->tv_cache;
419 return (0);
420 }
421
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100422 if (evutil_gettime_monotonic_(&base->monotonic_timer, tp) == -1) {
423 return -1;
Josh Gao83a0c9c2017-08-10 12:30:25 -0700424 }
Josh Gao83a0c9c2017-08-10 12:30:25 -0700425
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100426 if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL
427 < tp->tv_sec) {
428 struct timeval tv;
429 evutil_gettimeofday(&tv,NULL);
430 evutil_timersub(&tv, tp, &base->tv_clock_diff);
431 base->last_updated_clock_diff = tp->tv_sec;
432 }
433
434 return 0;
Christopher Wileye8679812015-07-01 13:36:18 -0700435}
436
437int
438event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv)
439{
440 int r;
441 if (!base) {
442 base = current_base;
443 if (!current_base)
444 return evutil_gettimeofday(tv, NULL);
445 }
446
447 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
448 if (base->tv_cache.tv_sec == 0) {
449 r = evutil_gettimeofday(tv, NULL);
450 } else {
Christopher Wileye8679812015-07-01 13:36:18 -0700451 evutil_timeradd(&base->tv_cache, &base->tv_clock_diff, tv);
Christopher Wileye8679812015-07-01 13:36:18 -0700452 r = 0;
453 }
454 EVBASE_RELEASE_LOCK(base, th_base_lock);
455 return r;
456}
457
458/** Make 'base' have no current cached time. */
459static inline void
460clear_time_cache(struct event_base *base)
461{
462 base->tv_cache.tv_sec = 0;
463}
464
465/** Replace the cached time in 'base' with the current time. */
466static inline void
467update_time_cache(struct event_base *base)
468{
469 base->tv_cache.tv_sec = 0;
470 if (!(base->flags & EVENT_BASE_FLAG_NO_CACHE_TIME))
471 gettime(base, &base->tv_cache);
472}
473
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100474int
475event_base_update_cache_time(struct event_base *base)
476{
477
478 if (!base) {
479 base = current_base;
480 if (!current_base)
481 return -1;
482 }
483
484 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
485 if (base->running_loop)
486 update_time_cache(base);
487 EVBASE_RELEASE_LOCK(base, th_base_lock);
488 return 0;
489}
490
491static inline struct event *
492event_callback_to_event(struct event_callback *evcb)
493{
494 EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_INIT));
495 return EVUTIL_UPCAST(evcb, struct event, ev_evcallback);
496}
497
498static inline struct event_callback *
499event_to_event_callback(struct event *ev)
500{
501 return &ev->ev_evcallback;
502}
503
Christopher Wileye8679812015-07-01 13:36:18 -0700504struct event_base *
505event_init(void)
506{
507 struct event_base *base = event_base_new_with_config(NULL);
508
509 if (base == NULL) {
510 event_errx(1, "%s: Unable to construct event_base", __func__);
511 return NULL;
512 }
513
514 current_base = base;
515
516 return (base);
517}
518
519struct event_base *
520event_base_new(void)
521{
522 struct event_base *base = NULL;
523 struct event_config *cfg = event_config_new();
524 if (cfg) {
525 base = event_base_new_with_config(cfg);
526 event_config_free(cfg);
527 }
528 return base;
529}
530
531/** Return true iff 'method' is the name of a method that 'cfg' tells us to
532 * avoid. */
533static int
534event_config_is_avoided_method(const struct event_config *cfg,
535 const char *method)
536{
537 struct event_config_entry *entry;
538
539 TAILQ_FOREACH(entry, &cfg->entries, next) {
540 if (entry->avoid_method != NULL &&
541 strcmp(entry->avoid_method, method) == 0)
542 return (1);
543 }
544
545 return (0);
546}
547
548/** Return true iff 'method' is disabled according to the environment. */
549static int
550event_is_method_disabled(const char *name)
551{
552 char environment[64];
553 int i;
554
555 evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name);
556 for (i = 8; environment[i] != '\0'; ++i)
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100557 environment[i] = EVUTIL_TOUPPER_(environment[i]);
558 /* Note that evutil_getenv_() ignores the environment entirely if
Christopher Wileye8679812015-07-01 13:36:18 -0700559 * we're setuid */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100560 return (evutil_getenv_(environment) != NULL);
Christopher Wileye8679812015-07-01 13:36:18 -0700561}
562
563int
564event_base_get_features(const struct event_base *base)
565{
566 return base->evsel->features;
567}
568
569void
Christopher Wileye8679812015-07-01 13:36:18 -0700570event_enable_debug_mode(void)
571{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100572#ifndef EVENT__DISABLE_DEBUG_MODE
573 if (event_debug_mode_on_)
Christopher Wileye8679812015-07-01 13:36:18 -0700574 event_errx(1, "%s was called twice!", __func__);
575 if (event_debug_mode_too_late)
576 event_errx(1, "%s must be called *before* creating any events "
577 "or event_bases",__func__);
578
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100579 event_debug_mode_on_ = 1;
Christopher Wileye8679812015-07-01 13:36:18 -0700580
581 HT_INIT(event_debug_map, &global_debug_map);
582#endif
583}
584
Christopher Wileye8679812015-07-01 13:36:18 -0700585void
586event_disable_debug_mode(void)
587{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100588#ifndef EVENT__DISABLE_DEBUG_MODE
Christopher Wileye8679812015-07-01 13:36:18 -0700589 struct event_debug_entry **ent, *victim;
590
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100591 EVLOCK_LOCK(event_debug_map_lock_, 0);
Christopher Wileye8679812015-07-01 13:36:18 -0700592 for (ent = HT_START(event_debug_map, &global_debug_map); ent; ) {
593 victim = *ent;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100594 ent = HT_NEXT_RMV(event_debug_map, &global_debug_map, ent);
Christopher Wileye8679812015-07-01 13:36:18 -0700595 mm_free(victim);
596 }
597 HT_CLEAR(event_debug_map, &global_debug_map);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100598 EVLOCK_UNLOCK(event_debug_map_lock_ , 0);
599
600 event_debug_mode_on_ = 0;
Josh Gao83a0c9c2017-08-10 12:30:25 -0700601#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100602}
Christopher Wileye8679812015-07-01 13:36:18 -0700603
604struct event_base *
605event_base_new_with_config(const struct event_config *cfg)
606{
607 int i;
608 struct event_base *base;
609 int should_check_environment;
610
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100611#ifndef EVENT__DISABLE_DEBUG_MODE
Christopher Wileye8679812015-07-01 13:36:18 -0700612 event_debug_mode_too_late = 1;
613#endif
614
615 if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) {
616 event_warn("%s: calloc", __func__);
617 return NULL;
618 }
Christopher Wileye8679812015-07-01 13:36:18 -0700619
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100620 if (cfg)
621 base->flags = cfg->flags;
622
623 should_check_environment =
624 !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV));
625
626 {
627 struct timeval tmp;
628 int precise_time =
629 cfg && (cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER);
630 int flags;
631 if (should_check_environment && !precise_time) {
632 precise_time = evutil_getenv_("EVENT_PRECISE_TIMER") != NULL;
Haibo Huangb2279672019-05-31 16:12:39 -0700633 if (precise_time) {
634 base->flags |= EVENT_BASE_FLAG_PRECISE_TIMER;
635 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100636 }
637 flags = precise_time ? EV_MONOT_PRECISE : 0;
638 evutil_configure_monotonic_time_(&base->monotonic_timer, flags);
639
640 gettime(base, &tmp);
641 }
642
643 min_heap_ctor_(&base->timeheap);
644
Christopher Wileye8679812015-07-01 13:36:18 -0700645 base->sig.ev_signal_pair[0] = -1;
646 base->sig.ev_signal_pair[1] = -1;
647 base->th_notify_fd[0] = -1;
648 base->th_notify_fd[1] = -1;
649
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100650 TAILQ_INIT(&base->active_later_queue);
Christopher Wileye8679812015-07-01 13:36:18 -0700651
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100652 evmap_io_initmap_(&base->io);
653 evmap_signal_initmap_(&base->sigmap);
654 event_changelist_init_(&base->changelist);
Christopher Wileye8679812015-07-01 13:36:18 -0700655
656 base->evbase = NULL;
657
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100658 if (cfg) {
659 memcpy(&base->max_dispatch_time,
660 &cfg->max_dispatch_interval, sizeof(struct timeval));
661 base->limit_callbacks_after_prio =
662 cfg->limit_callbacks_after_prio;
663 } else {
664 base->max_dispatch_time.tv_sec = -1;
665 base->limit_callbacks_after_prio = 1;
666 }
667 if (cfg && cfg->max_dispatch_callbacks >= 0) {
668 base->max_dispatch_callbacks = cfg->max_dispatch_callbacks;
669 } else {
670 base->max_dispatch_callbacks = INT_MAX;
671 }
672 if (base->max_dispatch_callbacks == INT_MAX &&
673 base->max_dispatch_time.tv_sec == -1)
674 base->limit_callbacks_after_prio = INT_MAX;
Christopher Wileye8679812015-07-01 13:36:18 -0700675
676 for (i = 0; eventops[i] && !base->evbase; i++) {
677 if (cfg != NULL) {
678 /* determine if this backend should be avoided */
679 if (event_config_is_avoided_method(cfg,
680 eventops[i]->name))
681 continue;
682 if ((eventops[i]->features & cfg->require_features)
683 != cfg->require_features)
684 continue;
685 }
686
687 /* also obey the environment variables */
688 if (should_check_environment &&
689 event_is_method_disabled(eventops[i]->name))
690 continue;
691
692 base->evsel = eventops[i];
693
694 base->evbase = base->evsel->init(base);
695 }
696
697 if (base->evbase == NULL) {
698 event_warnx("%s: no event mechanism available",
699 __func__);
700 base->evsel = NULL;
701 event_base_free(base);
702 return NULL;
703 }
704
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100705 if (evutil_getenv_("EVENT_SHOW_METHOD"))
Christopher Wileye8679812015-07-01 13:36:18 -0700706 event_msgx("libevent using: %s", base->evsel->name);
707
708 /* allocate a single active event queue */
709 if (event_base_priority_init(base, 1) < 0) {
710 event_base_free(base);
711 return NULL;
712 }
713
714 /* prepare for threading */
715
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100716#if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE)
717 event_debug_created_threadable_ctx_ = 1;
718#endif
719
720#ifndef EVENT__DISABLE_THREAD_SUPPORT
Christopher Wileye8679812015-07-01 13:36:18 -0700721 if (EVTHREAD_LOCKING_ENABLED() &&
722 (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) {
723 int r;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100724 EVTHREAD_ALLOC_LOCK(base->th_base_lock, 0);
Christopher Wileye8679812015-07-01 13:36:18 -0700725 EVTHREAD_ALLOC_COND(base->current_event_cond);
726 r = evthread_make_base_notifiable(base);
727 if (r<0) {
728 event_warnx("%s: Unable to make base notifiable.", __func__);
729 event_base_free(base);
730 return NULL;
731 }
732 }
733#endif
734
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100735#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -0700736 if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP))
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100737 event_base_start_iocp_(base, cfg->n_cpus_hint);
Christopher Wileye8679812015-07-01 13:36:18 -0700738#endif
739
740 return (base);
741}
742
743int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100744event_base_start_iocp_(struct event_base *base, int n_cpus)
Christopher Wileye8679812015-07-01 13:36:18 -0700745{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100746#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -0700747 if (base->iocp)
748 return 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100749 base->iocp = event_iocp_port_launch_(n_cpus);
Christopher Wileye8679812015-07-01 13:36:18 -0700750 if (!base->iocp) {
751 event_warnx("%s: Couldn't launch IOCP", __func__);
752 return -1;
753 }
754 return 0;
755#else
756 return -1;
757#endif
758}
759
760void
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100761event_base_stop_iocp_(struct event_base *base)
Christopher Wileye8679812015-07-01 13:36:18 -0700762{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100763#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -0700764 int rv;
765
766 if (!base->iocp)
767 return;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100768 rv = event_iocp_shutdown_(base->iocp, -1);
Christopher Wileye8679812015-07-01 13:36:18 -0700769 EVUTIL_ASSERT(rv >= 0);
770 base->iocp = NULL;
771#endif
772}
773
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100774static int
775event_base_cancel_single_callback_(struct event_base *base,
776 struct event_callback *evcb,
777 int run_finalizers)
778{
779 int result = 0;
780
781 if (evcb->evcb_flags & EVLIST_INIT) {
782 struct event *ev = event_callback_to_event(evcb);
783 if (!(ev->ev_flags & EVLIST_INTERNAL)) {
784 event_del_(ev, EVENT_DEL_EVEN_IF_FINALIZING);
785 result = 1;
786 }
787 } else {
788 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
789 event_callback_cancel_nolock_(base, evcb, 1);
790 EVBASE_RELEASE_LOCK(base, th_base_lock);
791 result = 1;
792 }
793
794 if (run_finalizers && (evcb->evcb_flags & EVLIST_FINALIZING)) {
795 switch (evcb->evcb_closure) {
796 case EV_CLOSURE_EVENT_FINALIZE:
797 case EV_CLOSURE_EVENT_FINALIZE_FREE: {
798 struct event *ev = event_callback_to_event(evcb);
799 ev->ev_evcallback.evcb_cb_union.evcb_evfinalize(ev, ev->ev_arg);
800 if (evcb->evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE)
801 mm_free(ev);
802 break;
803 }
804 case EV_CLOSURE_CB_FINALIZE:
805 evcb->evcb_cb_union.evcb_cbfinalize(evcb, evcb->evcb_arg);
806 break;
807 default:
808 break;
809 }
810 }
811 return result;
812}
813
814static int event_base_free_queues_(struct event_base *base, int run_finalizers)
815{
816 int deleted = 0, i;
817
818 for (i = 0; i < base->nactivequeues; ++i) {
819 struct event_callback *evcb, *next;
820 for (evcb = TAILQ_FIRST(&base->activequeues[i]); evcb; ) {
821 next = TAILQ_NEXT(evcb, evcb_active_next);
822 deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers);
823 evcb = next;
824 }
825 }
826
827 {
828 struct event_callback *evcb;
829 while ((evcb = TAILQ_FIRST(&base->active_later_queue))) {
830 deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers);
831 }
832 }
833
834 return deleted;
835}
836
837static void
838event_base_free_(struct event_base *base, int run_finalizers)
Christopher Wileye8679812015-07-01 13:36:18 -0700839{
Haibo Huang45729092019-08-01 16:15:45 -0700840 int i, n_deleted=0;
Christopher Wileye8679812015-07-01 13:36:18 -0700841 struct event *ev;
842 /* XXXX grab the lock? If there is contention when one thread frees
843 * the base, then the contending thread will be very sad soon. */
844
845 /* event_base_free(NULL) is how to free the current_base if we
846 * made it with event_init and forgot to hold a reference to it. */
847 if (base == NULL && current_base)
848 base = current_base;
Christopher Wileye8679812015-07-01 13:36:18 -0700849 /* Don't actually free NULL. */
850 if (base == NULL) {
851 event_warnx("%s: no base to free", __func__);
852 return;
853 }
854 /* XXX(niels) - check for internal events first */
855
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100856#ifdef _WIN32
857 event_base_stop_iocp_(base);
Christopher Wileye8679812015-07-01 13:36:18 -0700858#endif
859
860 /* threading fds if we have them */
861 if (base->th_notify_fd[0] != -1) {
862 event_del(&base->th_notify);
863 EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
864 if (base->th_notify_fd[1] != -1)
865 EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
866 base->th_notify_fd[0] = -1;
867 base->th_notify_fd[1] = -1;
868 event_debug_unassign(&base->th_notify);
869 }
870
871 /* Delete all non-internal events. */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100872 evmap_delete_all_(base);
873
874 while ((ev = min_heap_top_(&base->timeheap)) != NULL) {
Christopher Wileye8679812015-07-01 13:36:18 -0700875 event_del(ev);
876 ++n_deleted;
877 }
878 for (i = 0; i < base->n_common_timeouts; ++i) {
879 struct common_timeout_list *ctl =
880 base->common_timeout_queues[i];
881 event_del(&ctl->timeout_event); /* Internal; doesn't count */
882 event_debug_unassign(&ctl->timeout_event);
883 for (ev = TAILQ_FIRST(&ctl->events); ev; ) {
884 struct event *next = TAILQ_NEXT(ev,
885 ev_timeout_pos.ev_next_with_common_timeout);
886 if (!(ev->ev_flags & EVLIST_INTERNAL)) {
887 event_del(ev);
888 ++n_deleted;
889 }
890 ev = next;
891 }
892 mm_free(ctl);
893 }
894 if (base->common_timeout_queues)
895 mm_free(base->common_timeout_queues);
896
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100897 for (;;) {
898 /* For finalizers we can register yet another finalizer out from
899 * finalizer, and iff finalizer will be in active_later_queue we can
900 * add finalizer to activequeues, and we will have events in
901 * activequeues after this function returns, which is not what we want
902 * (we even have an assertion for this).
903 *
904 * A simple case is bufferevent with underlying (i.e. filters).
905 */
906 int i = event_base_free_queues_(base, run_finalizers);
Haibo Huangb2279672019-05-31 16:12:39 -0700907 event_debug(("%s: %d events freed", __func__, i));
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100908 if (!i) {
909 break;
Christopher Wileye8679812015-07-01 13:36:18 -0700910 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100911 n_deleted += i;
Christopher Wileye8679812015-07-01 13:36:18 -0700912 }
913
914 if (n_deleted)
Haibo Huang45729092019-08-01 16:15:45 -0700915 event_debug(("%s: %d events were still set in base",
Christopher Wileye8679812015-07-01 13:36:18 -0700916 __func__, n_deleted));
917
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100918 while (LIST_FIRST(&base->once_events)) {
919 struct event_once *eonce = LIST_FIRST(&base->once_events);
920 LIST_REMOVE(eonce, next_once);
921 mm_free(eonce);
922 }
923
Christopher Wileye8679812015-07-01 13:36:18 -0700924 if (base->evsel != NULL && base->evsel->dealloc != NULL)
925 base->evsel->dealloc(base);
926
927 for (i = 0; i < base->nactivequeues; ++i)
928 EVUTIL_ASSERT(TAILQ_EMPTY(&base->activequeues[i]));
929
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100930 EVUTIL_ASSERT(min_heap_empty_(&base->timeheap));
931 min_heap_dtor_(&base->timeheap);
Christopher Wileye8679812015-07-01 13:36:18 -0700932
933 mm_free(base->activequeues);
934
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100935 evmap_io_clear_(&base->io);
936 evmap_signal_clear_(&base->sigmap);
937 event_changelist_freemem_(&base->changelist);
Christopher Wileye8679812015-07-01 13:36:18 -0700938
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100939 EVTHREAD_FREE_LOCK(base->th_base_lock, 0);
Christopher Wileye8679812015-07-01 13:36:18 -0700940 EVTHREAD_FREE_COND(base->current_event_cond);
941
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100942 /* If we're freeing current_base, there won't be a current_base. */
943 if (base == current_base)
944 current_base = NULL;
Christopher Wileye8679812015-07-01 13:36:18 -0700945 mm_free(base);
946}
947
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100948void
949event_base_free_nofinalize(struct event_base *base)
950{
951 event_base_free_(base, 0);
952}
953
954void
955event_base_free(struct event_base *base)
956{
957 event_base_free_(base, 1);
958}
959
960/* Fake eventop; used to disable the backend temporarily inside event_reinit
961 * so that we can call event_del() on an event without telling the backend.
962 */
963static int
964nil_backend_del(struct event_base *b, evutil_socket_t fd, short old,
965 short events, void *fdinfo)
966{
967 return 0;
968}
969const struct eventop nil_eventop = {
970 "nil",
971 NULL, /* init: unused. */
972 NULL, /* add: unused. */
973 nil_backend_del, /* del: used, so needs to be killed. */
974 NULL, /* dispatch: unused. */
975 NULL, /* dealloc: unused. */
976 0, 0, 0
977};
978
Christopher Wileye8679812015-07-01 13:36:18 -0700979/* reinitialize the event base after a fork */
980int
981event_reinit(struct event_base *base)
982{
983 const struct eventop *evsel;
984 int res = 0;
Christopher Wileye8679812015-07-01 13:36:18 -0700985 int was_notifiable = 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100986 int had_signal_added = 0;
Christopher Wileye8679812015-07-01 13:36:18 -0700987
988 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
989
Haibo Huang45729092019-08-01 16:15:45 -0700990 if (base->running_loop) {
991 event_warnx("%s: forked from the event_loop.", __func__);
992 res = -1;
993 goto done;
994 }
995
Christopher Wileye8679812015-07-01 13:36:18 -0700996 evsel = base->evsel;
997
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100998 /* check if this event mechanism requires reinit on the backend */
999 if (evsel->need_reinit) {
1000 /* We're going to call event_del() on our notify events (the
1001 * ones that tell about signals and wakeup events). But we
1002 * don't actually want to tell the backend to change its
1003 * state, since it might still share some resource (a kqueue,
1004 * an epoll fd) with the parent process, and we don't want to
1005 * delete the fds from _that_ backend, we temporarily stub out
1006 * the evsel with a replacement.
1007 */
1008 base->evsel = &nil_eventop;
1009 }
Christopher Wileye8679812015-07-01 13:36:18 -07001010
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001011 /* We need to re-create a new signal-notification fd and a new
1012 * thread-notification fd. Otherwise, we'll still share those with
1013 * the parent process, which would make any notification sent to them
1014 * get received by one or both of the event loops, more or less at
1015 * random.
Christopher Wileye8679812015-07-01 13:36:18 -07001016 */
Christopher Wileye8679812015-07-01 13:36:18 -07001017 if (base->sig.ev_signal_added) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001018 event_del_nolock_(&base->sig.ev_signal, EVENT_DEL_AUTOBLOCK);
1019 event_debug_unassign(&base->sig.ev_signal);
1020 memset(&base->sig.ev_signal, 0, sizeof(base->sig.ev_signal));
1021 had_signal_added = 1;
Christopher Wileye8679812015-07-01 13:36:18 -07001022 base->sig.ev_signal_added = 0;
1023 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001024 if (base->sig.ev_signal_pair[0] != -1)
1025 EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]);
1026 if (base->sig.ev_signal_pair[1] != -1)
1027 EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]);
1028 if (base->th_notify_fn != NULL) {
Josh Gao83a0c9c2017-08-10 12:30:25 -07001029 was_notifiable = 1;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001030 base->th_notify_fn = NULL;
1031 }
1032 if (base->th_notify_fd[0] != -1) {
1033 event_del_nolock_(&base->th_notify, EVENT_DEL_AUTOBLOCK);
Christopher Wileye8679812015-07-01 13:36:18 -07001034 EVUTIL_CLOSESOCKET(base->th_notify_fd[0]);
1035 if (base->th_notify_fd[1] != -1)
1036 EVUTIL_CLOSESOCKET(base->th_notify_fd[1]);
1037 base->th_notify_fd[0] = -1;
1038 base->th_notify_fd[1] = -1;
1039 event_debug_unassign(&base->th_notify);
1040 }
1041
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001042 /* Replace the original evsel. */
1043 base->evsel = evsel;
Christopher Wileye8679812015-07-01 13:36:18 -07001044
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001045 if (evsel->need_reinit) {
1046 /* Reconstruct the backend through brute-force, so that we do
1047 * not share any structures with the parent process. For some
1048 * backends, this is necessary: epoll and kqueue, for
1049 * instance, have events associated with a kernel
1050 * structure. If didn't reinitialize, we'd share that
1051 * structure with the parent process, and any changes made by
1052 * the parent would affect our backend's behavior (and vice
1053 * versa).
1054 */
1055 if (base->evsel->dealloc != NULL)
1056 base->evsel->dealloc(base);
1057 base->evbase = evsel->init(base);
1058 if (base->evbase == NULL) {
1059 event_errx(1,
1060 "%s: could not reinitialize event mechanism",
1061 __func__);
1062 res = -1;
1063 goto done;
1064 }
Christopher Wileye8679812015-07-01 13:36:18 -07001065
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001066 /* Empty out the changelist (if any): we are starting from a
1067 * blank slate. */
1068 event_changelist_freemem_(&base->changelist);
1069
1070 /* Tell the event maps to re-inform the backend about all
1071 * pending events. This will make the signal notification
1072 * event get re-created if necessary. */
1073 if (evmap_reinit_(base) < 0)
1074 res = -1;
1075 } else {
1076 res = evsig_init_(base);
1077 if (res == 0 && had_signal_added) {
1078 res = event_add_nolock_(&base->sig.ev_signal, NULL, 0);
1079 if (res == 0)
1080 base->sig.ev_signal_added = 1;
Christopher Wileye8679812015-07-01 13:36:18 -07001081 }
1082 }
1083
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001084 /* If we were notifiable before, and nothing just exploded, become
1085 * notifiable again. */
Christopher Wileye8679812015-07-01 13:36:18 -07001086 if (was_notifiable && res == 0)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001087 res = evthread_make_base_notifiable_nolock_(base);
Christopher Wileye8679812015-07-01 13:36:18 -07001088
1089done:
1090 EVBASE_RELEASE_LOCK(base, th_base_lock);
1091 return (res);
1092}
1093
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001094/* Get the monotonic time for this event_base' timer */
1095int
1096event_gettime_monotonic(struct event_base *base, struct timeval *tv)
1097{
1098 int rv = -1;
1099
1100 if (base && tv) {
1101 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1102 rv = evutil_gettime_monotonic_(&(base->monotonic_timer), tv);
1103 EVBASE_RELEASE_LOCK(base, th_base_lock);
1104 }
1105
1106 return rv;
1107}
1108
Christopher Wileye8679812015-07-01 13:36:18 -07001109const char **
1110event_get_supported_methods(void)
1111{
1112 static const char **methods = NULL;
1113 const struct eventop **method;
1114 const char **tmp;
1115 int i = 0, k;
1116
1117 /* count all methods */
1118 for (method = &eventops[0]; *method != NULL; ++method) {
1119 ++i;
1120 }
1121
1122 /* allocate one more than we need for the NULL pointer */
1123 tmp = mm_calloc((i + 1), sizeof(char *));
1124 if (tmp == NULL)
1125 return (NULL);
1126
1127 /* populate the array with the supported methods */
1128 for (k = 0, i = 0; eventops[k] != NULL; ++k) {
1129 tmp[i++] = eventops[k]->name;
1130 }
1131 tmp[i] = NULL;
1132
1133 if (methods != NULL)
1134 mm_free((char**)methods);
1135
1136 methods = tmp;
1137
1138 return (methods);
1139}
1140
1141struct event_config *
1142event_config_new(void)
1143{
1144 struct event_config *cfg = mm_calloc(1, sizeof(*cfg));
1145
1146 if (cfg == NULL)
1147 return (NULL);
1148
1149 TAILQ_INIT(&cfg->entries);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001150 cfg->max_dispatch_interval.tv_sec = -1;
1151 cfg->max_dispatch_callbacks = INT_MAX;
1152 cfg->limit_callbacks_after_prio = 1;
Christopher Wileye8679812015-07-01 13:36:18 -07001153
1154 return (cfg);
1155}
1156
1157static void
1158event_config_entry_free(struct event_config_entry *entry)
1159{
1160 if (entry->avoid_method != NULL)
1161 mm_free((char *)entry->avoid_method);
1162 mm_free(entry);
1163}
1164
1165void
1166event_config_free(struct event_config *cfg)
1167{
1168 struct event_config_entry *entry;
1169
1170 while ((entry = TAILQ_FIRST(&cfg->entries)) != NULL) {
1171 TAILQ_REMOVE(&cfg->entries, entry, next);
1172 event_config_entry_free(entry);
1173 }
1174 mm_free(cfg);
1175}
1176
1177int
1178event_config_set_flag(struct event_config *cfg, int flag)
1179{
1180 if (!cfg)
1181 return -1;
1182 cfg->flags |= flag;
1183 return 0;
1184}
1185
1186int
1187event_config_avoid_method(struct event_config *cfg, const char *method)
1188{
1189 struct event_config_entry *entry = mm_malloc(sizeof(*entry));
1190 if (entry == NULL)
1191 return (-1);
1192
1193 if ((entry->avoid_method = mm_strdup(method)) == NULL) {
1194 mm_free(entry);
1195 return (-1);
1196 }
1197
1198 TAILQ_INSERT_TAIL(&cfg->entries, entry, next);
1199
1200 return (0);
1201}
1202
1203int
1204event_config_require_features(struct event_config *cfg,
1205 int features)
1206{
1207 if (!cfg)
1208 return (-1);
1209 cfg->require_features = features;
1210 return (0);
1211}
1212
1213int
1214event_config_set_num_cpus_hint(struct event_config *cfg, int cpus)
1215{
1216 if (!cfg)
1217 return (-1);
1218 cfg->n_cpus_hint = cpus;
1219 return (0);
1220}
1221
1222int
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001223event_config_set_max_dispatch_interval(struct event_config *cfg,
1224 const struct timeval *max_interval, int max_callbacks, int min_priority)
1225{
1226 if (max_interval)
1227 memcpy(&cfg->max_dispatch_interval, max_interval,
1228 sizeof(struct timeval));
1229 else
1230 cfg->max_dispatch_interval.tv_sec = -1;
1231 cfg->max_dispatch_callbacks =
1232 max_callbacks >= 0 ? max_callbacks : INT_MAX;
1233 if (min_priority < 0)
1234 min_priority = 0;
1235 cfg->limit_callbacks_after_prio = min_priority;
1236 return (0);
1237}
1238
1239int
Christopher Wileye8679812015-07-01 13:36:18 -07001240event_priority_init(int npriorities)
1241{
1242 return event_base_priority_init(current_base, npriorities);
1243}
1244
1245int
1246event_base_priority_init(struct event_base *base, int npriorities)
1247{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001248 int i, r;
1249 r = -1;
1250
1251 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
Christopher Wileye8679812015-07-01 13:36:18 -07001252
1253 if (N_ACTIVE_CALLBACKS(base) || npriorities < 1
1254 || npriorities >= EVENT_MAX_PRIORITIES)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001255 goto err;
Christopher Wileye8679812015-07-01 13:36:18 -07001256
1257 if (npriorities == base->nactivequeues)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001258 goto ok;
Christopher Wileye8679812015-07-01 13:36:18 -07001259
1260 if (base->nactivequeues) {
1261 mm_free(base->activequeues);
1262 base->nactivequeues = 0;
1263 }
1264
1265 /* Allocate our priority queues */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001266 base->activequeues = (struct evcallback_list *)
1267 mm_calloc(npriorities, sizeof(struct evcallback_list));
Christopher Wileye8679812015-07-01 13:36:18 -07001268 if (base->activequeues == NULL) {
1269 event_warn("%s: calloc", __func__);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001270 goto err;
Christopher Wileye8679812015-07-01 13:36:18 -07001271 }
1272 base->nactivequeues = npriorities;
1273
1274 for (i = 0; i < base->nactivequeues; ++i) {
1275 TAILQ_INIT(&base->activequeues[i]);
1276 }
1277
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001278ok:
1279 r = 0;
1280err:
1281 EVBASE_RELEASE_LOCK(base, th_base_lock);
1282 return (r);
1283}
1284
1285int
1286event_base_get_npriorities(struct event_base *base)
1287{
1288
1289 int n;
1290 if (base == NULL)
1291 base = current_base;
1292
1293 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1294 n = base->nactivequeues;
1295 EVBASE_RELEASE_LOCK(base, th_base_lock);
1296 return (n);
1297}
1298
1299int
1300event_base_get_num_events(struct event_base *base, unsigned int type)
1301{
1302 int r = 0;
1303
1304 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1305
1306 if (type & EVENT_BASE_COUNT_ACTIVE)
1307 r += base->event_count_active;
1308
1309 if (type & EVENT_BASE_COUNT_VIRTUAL)
1310 r += base->virtual_event_count;
1311
1312 if (type & EVENT_BASE_COUNT_ADDED)
1313 r += base->event_count;
1314
1315 EVBASE_RELEASE_LOCK(base, th_base_lock);
1316
1317 return r;
1318}
1319
1320int
1321event_base_get_max_events(struct event_base *base, unsigned int type, int clear)
1322{
1323 int r = 0;
1324
1325 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1326
1327 if (type & EVENT_BASE_COUNT_ACTIVE) {
1328 r += base->event_count_active_max;
1329 if (clear)
1330 base->event_count_active_max = 0;
1331 }
1332
1333 if (type & EVENT_BASE_COUNT_VIRTUAL) {
1334 r += base->virtual_event_count_max;
1335 if (clear)
1336 base->virtual_event_count_max = 0;
1337 }
1338
1339 if (type & EVENT_BASE_COUNT_ADDED) {
1340 r += base->event_count_max;
1341 if (clear)
1342 base->event_count_max = 0;
1343 }
1344
1345 EVBASE_RELEASE_LOCK(base, th_base_lock);
1346
1347 return r;
Christopher Wileye8679812015-07-01 13:36:18 -07001348}
1349
1350/* Returns true iff we're currently watching any events. */
1351static int
1352event_haveevents(struct event_base *base)
1353{
1354 /* Caller must hold th_base_lock */
1355 return (base->virtual_event_count > 0 || base->event_count > 0);
1356}
1357
1358/* "closure" function called when processing active signal events */
1359static inline void
1360event_signal_closure(struct event_base *base, struct event *ev)
1361{
1362 short ncalls;
1363 int should_break;
1364
1365 /* Allows deletes to work */
1366 ncalls = ev->ev_ncalls;
1367 if (ncalls != 0)
1368 ev->ev_pncalls = &ncalls;
1369 EVBASE_RELEASE_LOCK(base, th_base_lock);
1370 while (ncalls) {
1371 ncalls--;
1372 ev->ev_ncalls = ncalls;
1373 if (ncalls == 0)
1374 ev->ev_pncalls = NULL;
1375 (*ev->ev_callback)(ev->ev_fd, ev->ev_res, ev->ev_arg);
1376
1377 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1378 should_break = base->event_break;
1379 EVBASE_RELEASE_LOCK(base, th_base_lock);
1380
1381 if (should_break) {
1382 if (ncalls != 0)
1383 ev->ev_pncalls = NULL;
1384 return;
1385 }
1386 }
1387}
1388
1389/* Common timeouts are special timeouts that are handled as queues rather than
1390 * in the minheap. This is more efficient than the minheap if we happen to
1391 * know that we're going to get several thousands of timeout events all with
1392 * the same timeout value.
1393 *
1394 * Since all our timeout handling code assumes timevals can be copied,
1395 * assigned, etc, we can't use "magic pointer" to encode these common
1396 * timeouts. Searching through a list to see if every timeout is common could
1397 * also get inefficient. Instead, we take advantage of the fact that tv_usec
1398 * is 32 bits long, but only uses 20 of those bits (since it can never be over
1399 * 999999.) We use the top bits to encode 4 bites of magic number, and 8 bits
1400 * of index into the event_base's aray of common timeouts.
1401 */
1402
1403#define MICROSECONDS_MASK COMMON_TIMEOUT_MICROSECONDS_MASK
1404#define COMMON_TIMEOUT_IDX_MASK 0x0ff00000
1405#define COMMON_TIMEOUT_IDX_SHIFT 20
1406#define COMMON_TIMEOUT_MASK 0xf0000000
1407#define COMMON_TIMEOUT_MAGIC 0x50000000
1408
1409#define COMMON_TIMEOUT_IDX(tv) \
1410 (((tv)->tv_usec & COMMON_TIMEOUT_IDX_MASK)>>COMMON_TIMEOUT_IDX_SHIFT)
1411
1412/** Return true iff if 'tv' is a common timeout in 'base' */
1413static inline int
1414is_common_timeout(const struct timeval *tv,
1415 const struct event_base *base)
1416{
1417 int idx;
1418 if ((tv->tv_usec & COMMON_TIMEOUT_MASK) != COMMON_TIMEOUT_MAGIC)
1419 return 0;
1420 idx = COMMON_TIMEOUT_IDX(tv);
1421 return idx < base->n_common_timeouts;
1422}
1423
1424/* True iff tv1 and tv2 have the same common-timeout index, or if neither
1425 * one is a common timeout. */
1426static inline int
1427is_same_common_timeout(const struct timeval *tv1, const struct timeval *tv2)
1428{
1429 return (tv1->tv_usec & ~MICROSECONDS_MASK) ==
1430 (tv2->tv_usec & ~MICROSECONDS_MASK);
1431}
1432
1433/** Requires that 'tv' is a common timeout. Return the corresponding
1434 * common_timeout_list. */
1435static inline struct common_timeout_list *
1436get_common_timeout_list(struct event_base *base, const struct timeval *tv)
1437{
1438 return base->common_timeout_queues[COMMON_TIMEOUT_IDX(tv)];
1439}
1440
1441#if 0
1442static inline int
1443common_timeout_ok(const struct timeval *tv,
1444 struct event_base *base)
1445{
1446 const struct timeval *expect =
1447 &get_common_timeout_list(base, tv)->duration;
1448 return tv->tv_sec == expect->tv_sec &&
1449 tv->tv_usec == expect->tv_usec;
1450}
1451#endif
1452
1453/* Add the timeout for the first event in given common timeout list to the
1454 * event_base's minheap. */
1455static void
1456common_timeout_schedule(struct common_timeout_list *ctl,
1457 const struct timeval *now, struct event *head)
1458{
1459 struct timeval timeout = head->ev_timeout;
1460 timeout.tv_usec &= MICROSECONDS_MASK;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001461 event_add_nolock_(&ctl->timeout_event, &timeout, 1);
Christopher Wileye8679812015-07-01 13:36:18 -07001462}
1463
1464/* Callback: invoked when the timeout for a common timeout queue triggers.
1465 * This means that (at least) the first event in that queue should be run,
1466 * and the timeout should be rescheduled if there are more events. */
1467static void
1468common_timeout_callback(evutil_socket_t fd, short what, void *arg)
1469{
1470 struct timeval now;
1471 struct common_timeout_list *ctl = arg;
1472 struct event_base *base = ctl->base;
1473 struct event *ev = NULL;
1474 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1475 gettime(base, &now);
1476 while (1) {
1477 ev = TAILQ_FIRST(&ctl->events);
1478 if (!ev || ev->ev_timeout.tv_sec > now.tv_sec ||
1479 (ev->ev_timeout.tv_sec == now.tv_sec &&
1480 (ev->ev_timeout.tv_usec&MICROSECONDS_MASK) > now.tv_usec))
1481 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001482 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
1483 event_active_nolock_(ev, EV_TIMEOUT, 1);
Christopher Wileye8679812015-07-01 13:36:18 -07001484 }
1485 if (ev)
1486 common_timeout_schedule(ctl, &now, ev);
1487 EVBASE_RELEASE_LOCK(base, th_base_lock);
1488}
1489
1490#define MAX_COMMON_TIMEOUTS 256
1491
1492const struct timeval *
1493event_base_init_common_timeout(struct event_base *base,
1494 const struct timeval *duration)
1495{
1496 int i;
1497 struct timeval tv;
1498 const struct timeval *result=NULL;
1499 struct common_timeout_list *new_ctl;
1500
1501 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1502 if (duration->tv_usec > 1000000) {
1503 memcpy(&tv, duration, sizeof(struct timeval));
1504 if (is_common_timeout(duration, base))
1505 tv.tv_usec &= MICROSECONDS_MASK;
1506 tv.tv_sec += tv.tv_usec / 1000000;
1507 tv.tv_usec %= 1000000;
1508 duration = &tv;
1509 }
1510 for (i = 0; i < base->n_common_timeouts; ++i) {
1511 const struct common_timeout_list *ctl =
1512 base->common_timeout_queues[i];
1513 if (duration->tv_sec == ctl->duration.tv_sec &&
1514 duration->tv_usec ==
1515 (ctl->duration.tv_usec & MICROSECONDS_MASK)) {
1516 EVUTIL_ASSERT(is_common_timeout(&ctl->duration, base));
1517 result = &ctl->duration;
1518 goto done;
1519 }
1520 }
1521 if (base->n_common_timeouts == MAX_COMMON_TIMEOUTS) {
1522 event_warnx("%s: Too many common timeouts already in use; "
1523 "we only support %d per event_base", __func__,
1524 MAX_COMMON_TIMEOUTS);
1525 goto done;
1526 }
1527 if (base->n_common_timeouts_allocated == base->n_common_timeouts) {
1528 int n = base->n_common_timeouts < 16 ? 16 :
1529 base->n_common_timeouts*2;
1530 struct common_timeout_list **newqueues =
1531 mm_realloc(base->common_timeout_queues,
1532 n*sizeof(struct common_timeout_queue *));
1533 if (!newqueues) {
1534 event_warn("%s: realloc",__func__);
1535 goto done;
1536 }
1537 base->n_common_timeouts_allocated = n;
1538 base->common_timeout_queues = newqueues;
1539 }
1540 new_ctl = mm_calloc(1, sizeof(struct common_timeout_list));
1541 if (!new_ctl) {
1542 event_warn("%s: calloc",__func__);
1543 goto done;
1544 }
1545 TAILQ_INIT(&new_ctl->events);
1546 new_ctl->duration.tv_sec = duration->tv_sec;
1547 new_ctl->duration.tv_usec =
1548 duration->tv_usec | COMMON_TIMEOUT_MAGIC |
1549 (base->n_common_timeouts << COMMON_TIMEOUT_IDX_SHIFT);
1550 evtimer_assign(&new_ctl->timeout_event, base,
1551 common_timeout_callback, new_ctl);
1552 new_ctl->timeout_event.ev_flags |= EVLIST_INTERNAL;
1553 event_priority_set(&new_ctl->timeout_event, 0);
1554 new_ctl->base = base;
1555 base->common_timeout_queues[base->n_common_timeouts++] = new_ctl;
1556 result = &new_ctl->duration;
1557
1558done:
1559 if (result)
1560 EVUTIL_ASSERT(is_common_timeout(result, base));
1561
1562 EVBASE_RELEASE_LOCK(base, th_base_lock);
1563 return result;
1564}
1565
1566/* Closure function invoked when we're activating a persistent event. */
1567static inline void
1568event_persist_closure(struct event_base *base, struct event *ev)
1569{
Christopher Wileye8679812015-07-01 13:36:18 -07001570 void (*evcb_callback)(evutil_socket_t, short, void *);
1571
1572 // Other fields of *ev that must be stored before executing
1573 evutil_socket_t evcb_fd;
1574 short evcb_res;
1575 void *evcb_arg;
1576
1577 /* reschedule the persistent event if we have a timeout. */
1578 if (ev->ev_io_timeout.tv_sec || ev->ev_io_timeout.tv_usec) {
1579 /* If there was a timeout, we want it to run at an interval of
1580 * ev_io_timeout after the last time it was _scheduled_ for,
1581 * not ev_io_timeout after _now_. If it fired for another
1582 * reason, though, the timeout ought to start ticking _now_. */
1583 struct timeval run_at, relative_to, delay, now;
1584 ev_uint32_t usec_mask = 0;
1585 EVUTIL_ASSERT(is_same_common_timeout(&ev->ev_timeout,
1586 &ev->ev_io_timeout));
1587 gettime(base, &now);
1588 if (is_common_timeout(&ev->ev_timeout, base)) {
1589 delay = ev->ev_io_timeout;
1590 usec_mask = delay.tv_usec & ~MICROSECONDS_MASK;
1591 delay.tv_usec &= MICROSECONDS_MASK;
1592 if (ev->ev_res & EV_TIMEOUT) {
1593 relative_to = ev->ev_timeout;
1594 relative_to.tv_usec &= MICROSECONDS_MASK;
1595 } else {
1596 relative_to = now;
1597 }
1598 } else {
1599 delay = ev->ev_io_timeout;
1600 if (ev->ev_res & EV_TIMEOUT) {
1601 relative_to = ev->ev_timeout;
1602 } else {
1603 relative_to = now;
1604 }
1605 }
1606 evutil_timeradd(&relative_to, &delay, &run_at);
1607 if (evutil_timercmp(&run_at, &now, <)) {
1608 /* Looks like we missed at least one invocation due to
1609 * a clock jump, not running the event loop for a
1610 * while, really slow callbacks, or
1611 * something. Reschedule relative to now.
1612 */
1613 evutil_timeradd(&now, &delay, &run_at);
1614 }
1615 run_at.tv_usec |= usec_mask;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001616 event_add_nolock_(ev, &run_at, 1);
Christopher Wileye8679812015-07-01 13:36:18 -07001617 }
1618
1619 // Save our callback before we release the lock
1620 evcb_callback = ev->ev_callback;
1621 evcb_fd = ev->ev_fd;
1622 evcb_res = ev->ev_res;
1623 evcb_arg = ev->ev_arg;
1624
1625 // Release the lock
1626 EVBASE_RELEASE_LOCK(base, th_base_lock);
1627
1628 // Execute the callback
1629 (evcb_callback)(evcb_fd, evcb_res, evcb_arg);
1630}
1631
1632/*
1633 Helper for event_process_active to process all the events in a single queue,
1634 releasing the lock as we go. This function requires that the lock be held
1635 when it's invoked. Returns -1 if we get a signal or an event_break that
1636 means we should stop processing any active events now. Otherwise returns
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001637 the number of non-internal event_callbacks that we processed.
Christopher Wileye8679812015-07-01 13:36:18 -07001638*/
1639static int
1640event_process_active_single_queue(struct event_base *base,
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001641 struct evcallback_list *activeq,
1642 int max_to_process, const struct timeval *endtime)
Christopher Wileye8679812015-07-01 13:36:18 -07001643{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001644 struct event_callback *evcb;
Christopher Wileye8679812015-07-01 13:36:18 -07001645 int count = 0;
1646
1647 EVUTIL_ASSERT(activeq != NULL);
1648
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001649 for (evcb = TAILQ_FIRST(activeq); evcb; evcb = TAILQ_FIRST(activeq)) {
1650 struct event *ev=NULL;
1651 if (evcb->evcb_flags & EVLIST_INIT) {
1652 ev = event_callback_to_event(evcb);
1653
1654 if (ev->ev_events & EV_PERSIST || ev->ev_flags & EVLIST_FINALIZING)
1655 event_queue_remove_active(base, evcb);
1656 else
1657 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
1658 event_debug((
1659 "event_process_active: event: %p, %s%s%scall %p",
1660 ev,
1661 ev->ev_res & EV_READ ? "EV_READ " : " ",
1662 ev->ev_res & EV_WRITE ? "EV_WRITE " : " ",
1663 ev->ev_res & EV_CLOSED ? "EV_CLOSED " : " ",
1664 ev->ev_callback));
1665 } else {
1666 event_queue_remove_active(base, evcb);
1667 event_debug(("event_process_active: event_callback %p, "
1668 "closure %d, call %p",
1669 evcb, evcb->evcb_closure, evcb->evcb_cb_union.evcb_callback));
1670 }
1671
1672 if (!(evcb->evcb_flags & EVLIST_INTERNAL))
Christopher Wileye8679812015-07-01 13:36:18 -07001673 ++count;
1674
Christopher Wileye8679812015-07-01 13:36:18 -07001675
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001676 base->current_event = evcb;
1677#ifndef EVENT__DISABLE_THREAD_SUPPORT
Christopher Wileye8679812015-07-01 13:36:18 -07001678 base->current_event_waiters = 0;
1679#endif
1680
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001681 switch (evcb->evcb_closure) {
1682 case EV_CLOSURE_EVENT_SIGNAL:
1683 EVUTIL_ASSERT(ev != NULL);
Christopher Wileye8679812015-07-01 13:36:18 -07001684 event_signal_closure(base, ev);
1685 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001686 case EV_CLOSURE_EVENT_PERSIST:
1687 EVUTIL_ASSERT(ev != NULL);
Christopher Wileye8679812015-07-01 13:36:18 -07001688 event_persist_closure(base, ev);
1689 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001690 case EV_CLOSURE_EVENT: {
1691 void (*evcb_callback)(evutil_socket_t, short, void *);
Haibo Huangb2279672019-05-31 16:12:39 -07001692 short res;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001693 EVUTIL_ASSERT(ev != NULL);
1694 evcb_callback = *ev->ev_callback;
Haibo Huangb2279672019-05-31 16:12:39 -07001695 res = ev->ev_res;
Josh Gao83a0c9c2017-08-10 12:30:25 -07001696 EVBASE_RELEASE_LOCK(base, th_base_lock);
Haibo Huangb2279672019-05-31 16:12:39 -07001697 evcb_callback(ev->ev_fd, res, ev->ev_arg);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001698 }
1699 break;
1700 case EV_CLOSURE_CB_SELF: {
1701 void (*evcb_selfcb)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_selfcb;
1702 EVBASE_RELEASE_LOCK(base, th_base_lock);
1703 evcb_selfcb(evcb, evcb->evcb_arg);
1704 }
1705 break;
1706 case EV_CLOSURE_EVENT_FINALIZE:
1707 case EV_CLOSURE_EVENT_FINALIZE_FREE: {
1708 void (*evcb_evfinalize)(struct event *, void *);
1709 int evcb_closure = evcb->evcb_closure;
1710 EVUTIL_ASSERT(ev != NULL);
1711 base->current_event = NULL;
1712 evcb_evfinalize = ev->ev_evcallback.evcb_cb_union.evcb_evfinalize;
1713 EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
1714 EVBASE_RELEASE_LOCK(base, th_base_lock);
1715 evcb_evfinalize(ev, ev->ev_arg);
1716 event_debug_note_teardown_(ev);
1717 if (evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE)
1718 mm_free(ev);
1719 }
1720 break;
1721 case EV_CLOSURE_CB_FINALIZE: {
1722 void (*evcb_cbfinalize)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_cbfinalize;
1723 base->current_event = NULL;
1724 EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING));
1725 EVBASE_RELEASE_LOCK(base, th_base_lock);
1726 evcb_cbfinalize(evcb, evcb->evcb_arg);
1727 }
1728 break;
1729 default:
1730 EVUTIL_ASSERT(0);
Christopher Wileye8679812015-07-01 13:36:18 -07001731 }
1732
1733 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
Christopher Wileye8679812015-07-01 13:36:18 -07001734 base->current_event = NULL;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001735#ifndef EVENT__DISABLE_THREAD_SUPPORT
Christopher Wileye8679812015-07-01 13:36:18 -07001736 if (base->current_event_waiters) {
1737 base->current_event_waiters = 0;
1738 EVTHREAD_COND_BROADCAST(base->current_event_cond);
1739 }
1740#endif
1741
1742 if (base->event_break)
1743 return -1;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001744 if (count >= max_to_process)
1745 return count;
1746 if (count && endtime) {
1747 struct timeval now;
1748 update_time_cache(base);
1749 gettime(base, &now);
1750 if (evutil_timercmp(&now, endtime, >=))
1751 return count;
1752 }
Christopher Wileye8679812015-07-01 13:36:18 -07001753 if (base->event_continue)
1754 break;
1755 }
1756 return count;
1757}
1758
1759/*
Christopher Wileye8679812015-07-01 13:36:18 -07001760 * Active events are stored in priority queues. Lower priorities are always
1761 * process before higher priorities. Low priority events can starve high
1762 * priority ones.
1763 */
1764
1765static int
1766event_process_active(struct event_base *base)
1767{
1768 /* Caller must hold th_base_lock */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001769 struct evcallback_list *activeq = NULL;
Christopher Wileye8679812015-07-01 13:36:18 -07001770 int i, c = 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001771 const struct timeval *endtime;
1772 struct timeval tv;
1773 const int maxcb = base->max_dispatch_callbacks;
1774 const int limit_after_prio = base->limit_callbacks_after_prio;
1775 if (base->max_dispatch_time.tv_sec >= 0) {
1776 update_time_cache(base);
1777 gettime(base, &tv);
1778 evutil_timeradd(&base->max_dispatch_time, &tv, &tv);
1779 endtime = &tv;
1780 } else {
1781 endtime = NULL;
1782 }
Christopher Wileye8679812015-07-01 13:36:18 -07001783
1784 for (i = 0; i < base->nactivequeues; ++i) {
1785 if (TAILQ_FIRST(&base->activequeues[i]) != NULL) {
1786 base->event_running_priority = i;
1787 activeq = &base->activequeues[i];
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001788 if (i < limit_after_prio)
1789 c = event_process_active_single_queue(base, activeq,
1790 INT_MAX, NULL);
1791 else
1792 c = event_process_active_single_queue(base, activeq,
1793 maxcb, endtime);
Christopher Wileye8679812015-07-01 13:36:18 -07001794 if (c < 0) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001795 goto done;
Christopher Wileye8679812015-07-01 13:36:18 -07001796 } else if (c > 0)
1797 break; /* Processed a real event; do not
1798 * consider lower-priority events */
1799 /* If we get here, all of the events we processed
1800 * were internal. Continue. */
1801 }
1802 }
1803
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001804done:
Christopher Wileye8679812015-07-01 13:36:18 -07001805 base->event_running_priority = -1;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001806
Christopher Wileye8679812015-07-01 13:36:18 -07001807 return c;
1808}
1809
1810/*
1811 * Wait continuously for events. We exit only if no events are left.
1812 */
1813
1814int
1815event_dispatch(void)
1816{
1817 return (event_loop(0));
1818}
1819
1820int
1821event_base_dispatch(struct event_base *event_base)
1822{
1823 return (event_base_loop(event_base, 0));
1824}
1825
1826const char *
1827event_base_get_method(const struct event_base *base)
1828{
1829 EVUTIL_ASSERT(base);
1830 return (base->evsel->name);
1831}
1832
1833/** Callback: used to implement event_base_loopexit by telling the event_base
1834 * that it's time to exit its loop. */
1835static void
1836event_loopexit_cb(evutil_socket_t fd, short what, void *arg)
1837{
1838 struct event_base *base = arg;
1839 base->event_gotterm = 1;
1840}
1841
1842int
1843event_loopexit(const struct timeval *tv)
1844{
1845 return (event_once(-1, EV_TIMEOUT, event_loopexit_cb,
1846 current_base, tv));
1847}
1848
1849int
1850event_base_loopexit(struct event_base *event_base, const struct timeval *tv)
1851{
1852 return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb,
1853 event_base, tv));
1854}
1855
1856int
1857event_loopbreak(void)
1858{
1859 return (event_base_loopbreak(current_base));
1860}
1861
1862int
1863event_base_loopbreak(struct event_base *event_base)
1864{
1865 int r = 0;
1866 if (event_base == NULL)
1867 return (-1);
1868
1869 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1870 event_base->event_break = 1;
1871
1872 if (EVBASE_NEED_NOTIFY(event_base)) {
1873 r = evthread_notify_base(event_base);
1874 } else {
1875 r = (0);
1876 }
1877 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1878 return r;
1879}
1880
1881int
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001882event_base_loopcontinue(struct event_base *event_base)
1883{
1884 int r = 0;
1885 if (event_base == NULL)
1886 return (-1);
1887
1888 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1889 event_base->event_continue = 1;
1890
1891 if (EVBASE_NEED_NOTIFY(event_base)) {
1892 r = evthread_notify_base(event_base);
1893 } else {
1894 r = (0);
1895 }
1896 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1897 return r;
1898}
1899
1900int
Christopher Wileye8679812015-07-01 13:36:18 -07001901event_base_got_break(struct event_base *event_base)
1902{
1903 int res;
1904 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1905 res = event_base->event_break;
1906 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1907 return res;
1908}
1909
1910int
1911event_base_got_exit(struct event_base *event_base)
1912{
1913 int res;
1914 EVBASE_ACQUIRE_LOCK(event_base, th_base_lock);
1915 res = event_base->event_gotterm;
1916 EVBASE_RELEASE_LOCK(event_base, th_base_lock);
1917 return res;
1918}
1919
1920/* not thread safe */
1921
1922int
1923event_loop(int flags)
1924{
1925 return event_base_loop(current_base, flags);
1926}
1927
1928int
1929event_base_loop(struct event_base *base, int flags)
1930{
1931 const struct eventop *evsel = base->evsel;
1932 struct timeval tv;
1933 struct timeval *tv_p;
1934 int res, done, retval = 0;
1935
1936 /* Grab the lock. We will release it inside evsel.dispatch, and again
1937 * as we invoke user callbacks. */
1938 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
1939
1940 if (base->running_loop) {
1941 event_warnx("%s: reentrant invocation. Only one event_base_loop"
1942 " can run on each event_base at once.", __func__);
1943 EVBASE_RELEASE_LOCK(base, th_base_lock);
1944 return -1;
1945 }
1946
1947 base->running_loop = 1;
1948
1949 clear_time_cache(base);
1950
1951 if (base->sig.ev_signal_added && base->sig.ev_n_signals_added)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001952 evsig_set_base_(base);
Christopher Wileye8679812015-07-01 13:36:18 -07001953
1954 done = 0;
1955
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001956#ifndef EVENT__DISABLE_THREAD_SUPPORT
Christopher Wileye8679812015-07-01 13:36:18 -07001957 base->th_owner_id = EVTHREAD_GET_ID();
1958#endif
1959
1960 base->event_gotterm = base->event_break = 0;
1961
1962 while (!done) {
1963 base->event_continue = 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001964 base->n_deferreds_queued = 0;
Christopher Wileye8679812015-07-01 13:36:18 -07001965
1966 /* Terminate the loop if we have been asked to */
1967 if (base->event_gotterm) {
1968 break;
1969 }
1970
1971 if (base->event_break) {
1972 break;
1973 }
1974
Christopher Wileye8679812015-07-01 13:36:18 -07001975 tv_p = &tv;
1976 if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) {
1977 timeout_next(base, &tv_p);
1978 } else {
1979 /*
1980 * if we have active events, we just poll new events
1981 * without waiting.
1982 */
1983 evutil_timerclear(&tv);
1984 }
1985
1986 /* If we have no events, we just exit */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001987 if (0==(flags&EVLOOP_NO_EXIT_ON_EMPTY) &&
1988 !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) {
Christopher Wileye8679812015-07-01 13:36:18 -07001989 event_debug(("%s: no events registered.", __func__));
1990 retval = 1;
1991 goto done;
1992 }
1993
Narayan Kamathfc74cb42017-09-13 12:53:52 +01001994 event_queue_make_later_events_active(base);
Christopher Wileye8679812015-07-01 13:36:18 -07001995
1996 clear_time_cache(base);
1997
1998 res = evsel->dispatch(base, tv_p);
1999
2000 if (res == -1) {
2001 event_debug(("%s: dispatch returned unsuccessfully.",
2002 __func__));
2003 retval = -1;
2004 goto done;
2005 }
2006
2007 update_time_cache(base);
2008
2009 timeout_process(base);
2010
2011 if (N_ACTIVE_CALLBACKS(base)) {
2012 int n = event_process_active(base);
2013 if ((flags & EVLOOP_ONCE)
2014 && N_ACTIVE_CALLBACKS(base) == 0
2015 && n != 0)
2016 done = 1;
2017 } else if (flags & EVLOOP_NONBLOCK)
2018 done = 1;
2019 }
2020 event_debug(("%s: asked to terminate loop.", __func__));
2021
2022done:
2023 clear_time_cache(base);
2024 base->running_loop = 0;
2025
2026 EVBASE_RELEASE_LOCK(base, th_base_lock);
2027
2028 return (retval);
2029}
2030
Christopher Wileye8679812015-07-01 13:36:18 -07002031/* One-time callback to implement event_base_once: invokes the user callback,
2032 * then deletes the allocated storage */
2033static void
2034event_once_cb(evutil_socket_t fd, short events, void *arg)
2035{
2036 struct event_once *eonce = arg;
2037
2038 (*eonce->cb)(fd, events, eonce->arg);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002039 EVBASE_ACQUIRE_LOCK(eonce->ev.ev_base, th_base_lock);
2040 LIST_REMOVE(eonce, next_once);
2041 EVBASE_RELEASE_LOCK(eonce->ev.ev_base, th_base_lock);
Christopher Wileye8679812015-07-01 13:36:18 -07002042 event_debug_unassign(&eonce->ev);
2043 mm_free(eonce);
2044}
2045
2046/* not threadsafe, event scheduled once. */
2047int
2048event_once(evutil_socket_t fd, short events,
2049 void (*callback)(evutil_socket_t, short, void *),
2050 void *arg, const struct timeval *tv)
2051{
2052 return event_base_once(current_base, fd, events, callback, arg, tv);
2053}
2054
2055/* Schedules an event once */
2056int
2057event_base_once(struct event_base *base, evutil_socket_t fd, short events,
2058 void (*callback)(evutil_socket_t, short, void *),
2059 void *arg, const struct timeval *tv)
2060{
2061 struct event_once *eonce;
Christopher Wileye8679812015-07-01 13:36:18 -07002062 int res = 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002063 int activate = 0;
Christopher Wileye8679812015-07-01 13:36:18 -07002064
2065 /* We cannot support signals that just fire once, or persistent
2066 * events. */
2067 if (events & (EV_SIGNAL|EV_PERSIST))
2068 return (-1);
2069
2070 if ((eonce = mm_calloc(1, sizeof(struct event_once))) == NULL)
2071 return (-1);
2072
2073 eonce->cb = callback;
2074 eonce->arg = arg;
2075
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002076 if ((events & (EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE|EV_CLOSED)) == EV_TIMEOUT) {
Josh Gao83a0c9c2017-08-10 12:30:25 -07002077 evtimer_assign(&eonce->ev, base, event_once_cb, eonce);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002078
2079 if (tv == NULL || ! evutil_timerisset(tv)) {
2080 /* If the event is going to become active immediately,
2081 * don't put it on the timeout queue. This is one
2082 * idiom for scheduling a callback, so let's make
2083 * it fast (and order-preserving). */
2084 activate = 1;
2085 }
2086 } else if (events & (EV_READ|EV_WRITE|EV_CLOSED)) {
2087 events &= EV_READ|EV_WRITE|EV_CLOSED;
Christopher Wileye8679812015-07-01 13:36:18 -07002088
2089 event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce);
2090 } else {
2091 /* Bad event combination */
2092 mm_free(eonce);
2093 return (-1);
2094 }
2095
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002096 if (res == 0) {
2097 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2098 if (activate)
2099 event_active_nolock_(&eonce->ev, EV_TIMEOUT, 1);
2100 else
2101 res = event_add_nolock_(&eonce->ev, tv, 0);
2102
2103 if (res != 0) {
2104 mm_free(eonce);
2105 return (res);
2106 } else {
2107 LIST_INSERT_HEAD(&base->once_events, eonce, next_once);
2108 }
2109 EVBASE_RELEASE_LOCK(base, th_base_lock);
Christopher Wileye8679812015-07-01 13:36:18 -07002110 }
2111
2112 return (0);
2113}
2114
2115int
2116event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg)
2117{
2118 if (!base)
2119 base = current_base;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002120 if (arg == &event_self_cbarg_ptr_)
2121 arg = ev;
Christopher Wileye8679812015-07-01 13:36:18 -07002122
Haibo Huangb2279672019-05-31 16:12:39 -07002123 if (!(events & EV_SIGNAL))
2124 event_debug_assert_socket_nonblocking_(fd);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002125 event_debug_assert_not_added_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002126
2127 ev->ev_base = base;
2128
2129 ev->ev_callback = callback;
2130 ev->ev_arg = arg;
2131 ev->ev_fd = fd;
2132 ev->ev_events = events;
2133 ev->ev_res = 0;
2134 ev->ev_flags = EVLIST_INIT;
2135 ev->ev_ncalls = 0;
2136 ev->ev_pncalls = NULL;
2137
2138 if (events & EV_SIGNAL) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002139 if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != 0) {
Christopher Wileye8679812015-07-01 13:36:18 -07002140 event_warnx("%s: EV_SIGNAL is not compatible with "
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002141 "EV_READ, EV_WRITE or EV_CLOSED", __func__);
Christopher Wileye8679812015-07-01 13:36:18 -07002142 return -1;
2143 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002144 ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL;
Christopher Wileye8679812015-07-01 13:36:18 -07002145 } else {
2146 if (events & EV_PERSIST) {
2147 evutil_timerclear(&ev->ev_io_timeout);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002148 ev->ev_closure = EV_CLOSURE_EVENT_PERSIST;
Christopher Wileye8679812015-07-01 13:36:18 -07002149 } else {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002150 ev->ev_closure = EV_CLOSURE_EVENT;
Christopher Wileye8679812015-07-01 13:36:18 -07002151 }
2152 }
2153
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002154 min_heap_elem_init_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002155
2156 if (base != NULL) {
2157 /* by default, we put new events into the middle priority */
2158 ev->ev_pri = base->nactivequeues / 2;
2159 }
2160
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002161 event_debug_note_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002162
2163 return 0;
2164}
2165
2166int
2167event_base_set(struct event_base *base, struct event *ev)
2168{
2169 /* Only innocent events may be assigned to a different base */
2170 if (ev->ev_flags != EVLIST_INIT)
2171 return (-1);
2172
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002173 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002174
2175 ev->ev_base = base;
2176 ev->ev_pri = base->nactivequeues/2;
2177
2178 return (0);
2179}
2180
2181void
2182event_set(struct event *ev, evutil_socket_t fd, short events,
2183 void (*callback)(evutil_socket_t, short, void *), void *arg)
2184{
2185 int r;
2186 r = event_assign(ev, current_base, fd, events, callback, arg);
2187 EVUTIL_ASSERT(r == 0);
2188}
2189
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002190void *
2191event_self_cbarg(void)
2192{
2193 return &event_self_cbarg_ptr_;
2194}
2195
2196struct event *
2197event_base_get_running_event(struct event_base *base)
2198{
2199 struct event *ev = NULL;
2200 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2201 if (EVBASE_IN_THREAD(base)) {
2202 struct event_callback *evcb = base->current_event;
2203 if (evcb->evcb_flags & EVLIST_INIT)
2204 ev = event_callback_to_event(evcb);
2205 }
2206 EVBASE_RELEASE_LOCK(base, th_base_lock);
2207 return ev;
2208}
2209
Christopher Wileye8679812015-07-01 13:36:18 -07002210struct event *
2211event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg)
2212{
2213 struct event *ev;
2214 ev = mm_malloc(sizeof(struct event));
2215 if (ev == NULL)
2216 return (NULL);
2217 if (event_assign(ev, base, fd, events, cb, arg) < 0) {
2218 mm_free(ev);
2219 return (NULL);
2220 }
2221
2222 return (ev);
2223}
2224
2225void
2226event_free(struct event *ev)
2227{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002228 /* This is disabled, so that events which have been finalized be a
2229 * valid target for event_free(). That's */
2230 // event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002231
2232 /* make sure that this event won't be coming back to haunt us. */
2233 event_del(ev);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002234 event_debug_note_teardown_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002235 mm_free(ev);
2236
2237}
2238
2239void
2240event_debug_unassign(struct event *ev)
2241{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002242 event_debug_assert_not_added_(ev);
2243 event_debug_note_teardown_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002244
2245 ev->ev_flags &= ~EVLIST_INIT;
2246}
2247
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002248#define EVENT_FINALIZE_FREE_ 0x10000
2249static int
2250event_finalize_nolock_(struct event_base *base, unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2251{
2252 ev_uint8_t closure = (flags & EVENT_FINALIZE_FREE_) ?
2253 EV_CLOSURE_EVENT_FINALIZE_FREE : EV_CLOSURE_EVENT_FINALIZE;
2254
2255 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
2256 ev->ev_closure = closure;
2257 ev->ev_evcallback.evcb_cb_union.evcb_evfinalize = cb;
2258 event_active_nolock_(ev, EV_FINALIZE, 1);
2259 ev->ev_flags |= EVLIST_FINALIZING;
2260 return 0;
2261}
2262
2263static int
2264event_finalize_impl_(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2265{
2266 int r;
2267 struct event_base *base = ev->ev_base;
2268 if (EVUTIL_FAILURE_CHECK(!base)) {
2269 event_warnx("%s: event has no event_base set.", __func__);
2270 return -1;
2271 }
2272
2273 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2274 r = event_finalize_nolock_(base, flags, ev, cb);
2275 EVBASE_RELEASE_LOCK(base, th_base_lock);
2276 return r;
2277}
2278
2279int
2280event_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2281{
2282 return event_finalize_impl_(flags, ev, cb);
2283}
2284
2285int
2286event_free_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb)
2287{
2288 return event_finalize_impl_(flags|EVENT_FINALIZE_FREE_, ev, cb);
2289}
2290
2291void
2292event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *))
2293{
2294 struct event *ev = NULL;
2295 if (evcb->evcb_flags & EVLIST_INIT) {
2296 ev = event_callback_to_event(evcb);
2297 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
2298 } else {
2299 event_callback_cancel_nolock_(base, evcb, 0); /*XXX can this fail?*/
2300 }
2301
2302 evcb->evcb_closure = EV_CLOSURE_CB_FINALIZE;
2303 evcb->evcb_cb_union.evcb_cbfinalize = cb;
2304 event_callback_activate_nolock_(base, evcb); /* XXX can this really fail?*/
2305 evcb->evcb_flags |= EVLIST_FINALIZING;
2306}
2307
2308void
2309event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *))
2310{
2311 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2312 event_callback_finalize_nolock_(base, flags, evcb, cb);
2313 EVBASE_RELEASE_LOCK(base, th_base_lock);
2314}
2315
2316/** Internal: Finalize all of the n_cbs callbacks in evcbs. The provided
2317 * callback will be invoked on *one of them*, after they have *all* been
2318 * finalized. */
2319int
2320event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcbs, void (*cb)(struct event_callback *, void *))
2321{
2322 int n_pending = 0, i;
2323
2324 if (base == NULL)
2325 base = current_base;
2326
2327 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
2328
2329 event_debug(("%s: %d events finalizing", __func__, n_cbs));
2330
2331 /* At most one can be currently executing; the rest we just
2332 * cancel... But we always make sure that the finalize callback
2333 * runs. */
2334 for (i = 0; i < n_cbs; ++i) {
2335 struct event_callback *evcb = evcbs[i];
2336 if (evcb == base->current_event) {
2337 event_callback_finalize_nolock_(base, 0, evcb, cb);
2338 ++n_pending;
2339 } else {
2340 event_callback_cancel_nolock_(base, evcb, 0);
2341 }
2342 }
2343
2344 if (n_pending == 0) {
2345 /* Just do the first one. */
2346 event_callback_finalize_nolock_(base, 0, evcbs[0], cb);
2347 }
2348
2349 EVBASE_RELEASE_LOCK(base, th_base_lock);
2350 return 0;
2351}
2352
Christopher Wileye8679812015-07-01 13:36:18 -07002353/*
2354 * Set's the priority of an event - if an event is already scheduled
2355 * changing the priority is going to fail.
2356 */
2357
2358int
2359event_priority_set(struct event *ev, int pri)
2360{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002361 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002362
2363 if (ev->ev_flags & EVLIST_ACTIVE)
2364 return (-1);
2365 if (pri < 0 || pri >= ev->ev_base->nactivequeues)
2366 return (-1);
2367
2368 ev->ev_pri = pri;
2369
2370 return (0);
2371}
2372
2373/*
2374 * Checks if a specific event is pending or scheduled.
2375 */
2376
2377int
2378event_pending(const struct event *ev, short event, struct timeval *tv)
2379{
2380 int flags = 0;
2381
2382 if (EVUTIL_FAILURE_CHECK(ev->ev_base == NULL)) {
2383 event_warnx("%s: event has no event_base set.", __func__);
2384 return 0;
2385 }
2386
2387 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002388 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002389
2390 if (ev->ev_flags & EVLIST_INSERTED)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002391 flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL));
2392 if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))
Christopher Wileye8679812015-07-01 13:36:18 -07002393 flags |= ev->ev_res;
2394 if (ev->ev_flags & EVLIST_TIMEOUT)
2395 flags |= EV_TIMEOUT;
2396
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002397 event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL);
Christopher Wileye8679812015-07-01 13:36:18 -07002398
2399 /* See if there is a timeout that we should report */
2400 if (tv != NULL && (flags & event & EV_TIMEOUT)) {
2401 struct timeval tmp = ev->ev_timeout;
2402 tmp.tv_usec &= MICROSECONDS_MASK;
Christopher Wileye8679812015-07-01 13:36:18 -07002403 /* correctly remamp to real time */
2404 evutil_timeradd(&ev->ev_base->tv_clock_diff, &tmp, tv);
Christopher Wileye8679812015-07-01 13:36:18 -07002405 }
2406
2407 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2408
2409 return (flags & event);
2410}
2411
2412int
2413event_initialized(const struct event *ev)
2414{
2415 if (!(ev->ev_flags & EVLIST_INIT))
2416 return 0;
2417
2418 return 1;
2419}
2420
2421void
2422event_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)
2423{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002424 event_debug_assert_is_setup_(event);
Christopher Wileye8679812015-07-01 13:36:18 -07002425
2426 if (base_out)
2427 *base_out = event->ev_base;
2428 if (fd_out)
2429 *fd_out = event->ev_fd;
2430 if (events_out)
2431 *events_out = event->ev_events;
2432 if (callback_out)
2433 *callback_out = event->ev_callback;
2434 if (arg_out)
2435 *arg_out = event->ev_arg;
2436}
2437
2438size_t
2439event_get_struct_event_size(void)
2440{
2441 return sizeof(struct event);
2442}
2443
2444evutil_socket_t
2445event_get_fd(const struct event *ev)
2446{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002447 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002448 return ev->ev_fd;
2449}
2450
2451struct event_base *
2452event_get_base(const struct event *ev)
2453{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002454 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002455 return ev->ev_base;
2456}
2457
2458short
2459event_get_events(const struct event *ev)
2460{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002461 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002462 return ev->ev_events;
2463}
2464
2465event_callback_fn
2466event_get_callback(const struct event *ev)
2467{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002468 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002469 return ev->ev_callback;
2470}
2471
2472void *
2473event_get_callback_arg(const struct event *ev)
2474{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002475 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002476 return ev->ev_arg;
2477}
2478
2479int
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002480event_get_priority(const struct event *ev)
2481{
2482 event_debug_assert_is_setup_(ev);
2483 return ev->ev_pri;
2484}
2485
2486int
Christopher Wileye8679812015-07-01 13:36:18 -07002487event_add(struct event *ev, const struct timeval *tv)
2488{
2489 int res;
2490
2491 if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2492 event_warnx("%s: event has no event_base set.", __func__);
2493 return -1;
2494 }
2495
2496 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2497
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002498 res = event_add_nolock_(ev, tv, 0);
Christopher Wileye8679812015-07-01 13:36:18 -07002499
2500 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2501
2502 return (res);
2503}
2504
2505/* Helper callback: wake an event_base from another thread. This version
2506 * works by writing a byte to one end of a socketpair, so that the event_base
2507 * listening on the other end will wake up as the corresponding event
2508 * triggers */
2509static int
2510evthread_notify_base_default(struct event_base *base)
2511{
2512 char buf[1];
2513 int r;
2514 buf[0] = (char) 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002515#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -07002516 r = send(base->th_notify_fd[1], buf, 1, 0);
2517#else
2518 r = write(base->th_notify_fd[1], buf, 1);
2519#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002520 return (r < 0 && ! EVUTIL_ERR_IS_EAGAIN(errno)) ? -1 : 0;
Christopher Wileye8679812015-07-01 13:36:18 -07002521}
2522
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002523#ifdef EVENT__HAVE_EVENTFD
Christopher Wileye8679812015-07-01 13:36:18 -07002524/* Helper callback: wake an event_base from another thread. This version
2525 * assumes that you have a working eventfd() implementation. */
2526static int
2527evthread_notify_base_eventfd(struct event_base *base)
2528{
2529 ev_uint64_t msg = 1;
2530 int r;
2531 do {
2532 r = write(base->th_notify_fd[0], (void*) &msg, sizeof(msg));
2533 } while (r < 0 && errno == EAGAIN);
2534
2535 return (r < 0) ? -1 : 0;
2536}
2537#endif
2538
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002539
Christopher Wileye8679812015-07-01 13:36:18 -07002540/** Tell the thread currently running the event_loop for base (if any) that it
2541 * needs to stop waiting in its dispatch function (if it is) and process all
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002542 * active callbacks. */
Christopher Wileye8679812015-07-01 13:36:18 -07002543static int
2544evthread_notify_base(struct event_base *base)
2545{
2546 EVENT_BASE_ASSERT_LOCKED(base);
2547 if (!base->th_notify_fn)
2548 return -1;
2549 if (base->is_notify_pending)
2550 return 0;
2551 base->is_notify_pending = 1;
2552 return base->th_notify_fn(base);
2553}
2554
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002555/* Implementation function to remove a timeout on a currently pending event.
2556 */
2557int
2558event_remove_timer_nolock_(struct event *ev)
2559{
2560 struct event_base *base = ev->ev_base;
2561
2562 EVENT_BASE_ASSERT_LOCKED(base);
2563 event_debug_assert_is_setup_(ev);
2564
2565 event_debug(("event_remove_timer_nolock: event: %p", ev));
2566
2567 /* If it's not pending on a timeout, we don't need to do anything. */
2568 if (ev->ev_flags & EVLIST_TIMEOUT) {
2569 event_queue_remove_timeout(base, ev);
2570 evutil_timerclear(&ev->ev_.ev_io.ev_timeout);
2571 }
2572
2573 return (0);
2574}
2575
2576int
2577event_remove_timer(struct event *ev)
2578{
2579 int res;
2580
2581 if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2582 event_warnx("%s: event has no event_base set.", __func__);
2583 return -1;
2584 }
2585
2586 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2587
2588 res = event_remove_timer_nolock_(ev);
2589
2590 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2591
2592 return (res);
2593}
2594
Christopher Wileye8679812015-07-01 13:36:18 -07002595/* Implementation function to add an event. Works just like event_add,
2596 * except: 1) it requires that we have the lock. 2) if tv_is_absolute is set,
2597 * we treat tv as an absolute time, not as an interval to add to the current
2598 * time */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002599int
2600event_add_nolock_(struct event *ev, const struct timeval *tv,
Christopher Wileye8679812015-07-01 13:36:18 -07002601 int tv_is_absolute)
2602{
2603 struct event_base *base = ev->ev_base;
2604 int res = 0;
2605 int notify = 0;
2606
2607 EVENT_BASE_ASSERT_LOCKED(base);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002608 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002609
2610 event_debug((
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002611 "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%s%scall %p",
Christopher Wileye8679812015-07-01 13:36:18 -07002612 ev,
2613 EV_SOCK_ARG(ev->ev_fd),
2614 ev->ev_events & EV_READ ? "EV_READ " : " ",
2615 ev->ev_events & EV_WRITE ? "EV_WRITE " : " ",
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002616 ev->ev_events & EV_CLOSED ? "EV_CLOSED " : " ",
Christopher Wileye8679812015-07-01 13:36:18 -07002617 tv ? "EV_TIMEOUT " : " ",
2618 ev->ev_callback));
2619
2620 EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
2621
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002622 if (ev->ev_flags & EVLIST_FINALIZING) {
2623 /* XXXX debug */
2624 return (-1);
2625 }
2626
Christopher Wileye8679812015-07-01 13:36:18 -07002627 /*
2628 * prepare for timeout insertion further below, if we get a
2629 * failure on any step, we should not change any state.
2630 */
2631 if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002632 if (min_heap_reserve_(&base->timeheap,
2633 1 + min_heap_size_(&base->timeheap)) == -1)
Christopher Wileye8679812015-07-01 13:36:18 -07002634 return (-1); /* ENOMEM == errno */
2635 }
2636
2637 /* If the main thread is currently executing a signal event's
2638 * callback, and we are not the main thread, then we want to wait
2639 * until the callback is done before we mess with the event, or else
2640 * we can race on ev_ncalls and ev_pncalls below. */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002641#ifndef EVENT__DISABLE_THREAD_SUPPORT
2642 if (base->current_event == event_to_event_callback(ev) &&
2643 (ev->ev_events & EV_SIGNAL)
Christopher Wileye8679812015-07-01 13:36:18 -07002644 && !EVBASE_IN_THREAD(base)) {
2645 ++base->current_event_waiters;
2646 EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2647 }
2648#endif
2649
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002650 if ((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)) &&
2651 !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
2652 if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED))
2653 res = evmap_io_add_(base, ev->ev_fd, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002654 else if (ev->ev_events & EV_SIGNAL)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002655 res = evmap_signal_add_(base, (int)ev->ev_fd, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002656 if (res != -1)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002657 event_queue_insert_inserted(base, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002658 if (res == 1) {
2659 /* evmap says we need to notify the main thread. */
2660 notify = 1;
2661 res = 0;
2662 }
2663 }
2664
2665 /*
2666 * we should change the timeout state only if the previous event
2667 * addition succeeded.
2668 */
2669 if (res != -1 && tv != NULL) {
2670 struct timeval now;
2671 int common_timeout;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002672#ifdef USE_REINSERT_TIMEOUT
2673 int was_common;
2674 int old_timeout_idx;
2675#endif
Christopher Wileye8679812015-07-01 13:36:18 -07002676
2677 /*
2678 * for persistent timeout events, we remember the
2679 * timeout value and re-add the event.
2680 *
2681 * If tv_is_absolute, this was already set.
2682 */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002683 if (ev->ev_closure == EV_CLOSURE_EVENT_PERSIST && !tv_is_absolute)
Christopher Wileye8679812015-07-01 13:36:18 -07002684 ev->ev_io_timeout = *tv;
2685
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002686#ifndef USE_REINSERT_TIMEOUT
Christopher Wileye8679812015-07-01 13:36:18 -07002687 if (ev->ev_flags & EVLIST_TIMEOUT) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002688 event_queue_remove_timeout(base, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002689 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002690#endif
Christopher Wileye8679812015-07-01 13:36:18 -07002691
2692 /* Check if it is active due to a timeout. Rescheduling
2693 * this timeout before the callback can be executed
2694 * removes it from the active list. */
2695 if ((ev->ev_flags & EVLIST_ACTIVE) &&
2696 (ev->ev_res & EV_TIMEOUT)) {
2697 if (ev->ev_events & EV_SIGNAL) {
2698 /* See if we are just active executing
2699 * this event in a loop
2700 */
2701 if (ev->ev_ncalls && ev->ev_pncalls) {
2702 /* Abort loop */
2703 *ev->ev_pncalls = 0;
2704 }
2705 }
2706
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002707 event_queue_remove_active(base, event_to_event_callback(ev));
Christopher Wileye8679812015-07-01 13:36:18 -07002708 }
2709
2710 gettime(base, &now);
2711
2712 common_timeout = is_common_timeout(tv, base);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002713#ifdef USE_REINSERT_TIMEOUT
2714 was_common = is_common_timeout(&ev->ev_timeout, base);
2715 old_timeout_idx = COMMON_TIMEOUT_IDX(&ev->ev_timeout);
2716#endif
2717
Christopher Wileye8679812015-07-01 13:36:18 -07002718 if (tv_is_absolute) {
2719 ev->ev_timeout = *tv;
2720 } else if (common_timeout) {
2721 struct timeval tmp = *tv;
2722 tmp.tv_usec &= MICROSECONDS_MASK;
2723 evutil_timeradd(&now, &tmp, &ev->ev_timeout);
2724 ev->ev_timeout.tv_usec |=
2725 (tv->tv_usec & ~MICROSECONDS_MASK);
2726 } else {
2727 evutil_timeradd(&now, tv, &ev->ev_timeout);
2728 }
2729
2730 event_debug((
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002731 "event_add: event %p, timeout in %d seconds %d useconds, call %p",
2732 ev, (int)tv->tv_sec, (int)tv->tv_usec, ev->ev_callback));
Christopher Wileye8679812015-07-01 13:36:18 -07002733
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002734#ifdef USE_REINSERT_TIMEOUT
2735 event_queue_reinsert_timeout(base, ev, was_common, common_timeout, old_timeout_idx);
2736#else
2737 event_queue_insert_timeout(base, ev);
2738#endif
2739
Christopher Wileye8679812015-07-01 13:36:18 -07002740 if (common_timeout) {
2741 struct common_timeout_list *ctl =
2742 get_common_timeout_list(base, &ev->ev_timeout);
2743 if (ev == TAILQ_FIRST(&ctl->events)) {
2744 common_timeout_schedule(ctl, &now, ev);
2745 }
2746 } else {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002747 struct event* top = NULL;
Christopher Wileye8679812015-07-01 13:36:18 -07002748 /* See if the earliest timeout is now earlier than it
2749 * was before: if so, we will need to tell the main
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002750 * thread to wake up earlier than it would otherwise.
2751 * We double check the timeout of the top element to
2752 * handle time distortions due to system suspension.
2753 */
2754 if (min_heap_elt_is_top_(ev))
2755 notify = 1;
2756 else if ((top = min_heap_top_(&base->timeheap)) != NULL &&
2757 evutil_timercmp(&top->ev_timeout, &now, <))
Christopher Wileye8679812015-07-01 13:36:18 -07002758 notify = 1;
2759 }
2760 }
2761
2762 /* if we are not in the right thread, we need to wake up the loop */
2763 if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
2764 evthread_notify_base(base);
2765
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002766 event_debug_note_add_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002767
2768 return (res);
2769}
2770
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002771static int
2772event_del_(struct event *ev, int blocking)
Christopher Wileye8679812015-07-01 13:36:18 -07002773{
2774 int res;
Haibo Huangb2279672019-05-31 16:12:39 -07002775 struct event_base *base = ev->ev_base;
Christopher Wileye8679812015-07-01 13:36:18 -07002776
Haibo Huangb2279672019-05-31 16:12:39 -07002777 if (EVUTIL_FAILURE_CHECK(!base)) {
Christopher Wileye8679812015-07-01 13:36:18 -07002778 event_warnx("%s: event has no event_base set.", __func__);
2779 return -1;
2780 }
2781
Haibo Huangb2279672019-05-31 16:12:39 -07002782 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002783 res = event_del_nolock_(ev, blocking);
Haibo Huangb2279672019-05-31 16:12:39 -07002784 EVBASE_RELEASE_LOCK(base, th_base_lock);
Christopher Wileye8679812015-07-01 13:36:18 -07002785
2786 return (res);
2787}
2788
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002789int
2790event_del(struct event *ev)
2791{
2792 return event_del_(ev, EVENT_DEL_AUTOBLOCK);
2793}
2794
2795int
2796event_del_block(struct event *ev)
2797{
2798 return event_del_(ev, EVENT_DEL_BLOCK);
2799}
2800
2801int
2802event_del_noblock(struct event *ev)
2803{
2804 return event_del_(ev, EVENT_DEL_NOBLOCK);
2805}
2806
2807/** Helper for event_del: always called with th_base_lock held.
2808 *
2809 * "blocking" must be one of the EVENT_DEL_{BLOCK, NOBLOCK, AUTOBLOCK,
2810 * EVEN_IF_FINALIZING} values. See those for more information.
2811 */
2812int
2813event_del_nolock_(struct event *ev, int blocking)
Christopher Wileye8679812015-07-01 13:36:18 -07002814{
2815 struct event_base *base;
2816 int res = 0, notify = 0;
2817
2818 event_debug(("event_del: %p (fd "EV_SOCK_FMT"), callback %p",
2819 ev, EV_SOCK_ARG(ev->ev_fd), ev->ev_callback));
2820
2821 /* An event without a base has not been added */
2822 if (ev->ev_base == NULL)
2823 return (-1);
2824
2825 EVENT_BASE_ASSERT_LOCKED(ev->ev_base);
2826
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002827 if (blocking != EVENT_DEL_EVEN_IF_FINALIZING) {
2828 if (ev->ev_flags & EVLIST_FINALIZING) {
2829 /* XXXX Debug */
2830 return 0;
2831 }
2832 }
2833
Christopher Wileye8679812015-07-01 13:36:18 -07002834 base = ev->ev_base;
Christopher Wileye8679812015-07-01 13:36:18 -07002835
2836 EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL));
2837
2838 /* See if we are just active executing this event in a loop */
2839 if (ev->ev_events & EV_SIGNAL) {
2840 if (ev->ev_ncalls && ev->ev_pncalls) {
2841 /* Abort loop */
2842 *ev->ev_pncalls = 0;
2843 }
2844 }
2845
2846 if (ev->ev_flags & EVLIST_TIMEOUT) {
2847 /* NOTE: We never need to notify the main thread because of a
2848 * deleted timeout event: all that could happen if we don't is
2849 * that the dispatch loop might wake up too early. But the
2850 * point of notifying the main thread _is_ to wake up the
2851 * dispatch loop early anyway, so we wouldn't gain anything by
2852 * doing it.
2853 */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002854 event_queue_remove_timeout(base, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002855 }
2856
2857 if (ev->ev_flags & EVLIST_ACTIVE)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002858 event_queue_remove_active(base, event_to_event_callback(ev));
2859 else if (ev->ev_flags & EVLIST_ACTIVE_LATER)
2860 event_queue_remove_active_later(base, event_to_event_callback(ev));
Christopher Wileye8679812015-07-01 13:36:18 -07002861
2862 if (ev->ev_flags & EVLIST_INSERTED) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002863 event_queue_remove_inserted(base, ev);
2864 if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED))
2865 res = evmap_io_del_(base, ev->ev_fd, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002866 else
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002867 res = evmap_signal_del_(base, (int)ev->ev_fd, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002868 if (res == 1) {
2869 /* evmap says we need to notify the main thread. */
2870 notify = 1;
2871 res = 0;
2872 }
Haibo Huangb2279672019-05-31 16:12:39 -07002873 /* If we do not have events, let's notify event base so it can
2874 * exit without waiting */
2875 if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base))
2876 notify = 1;
Christopher Wileye8679812015-07-01 13:36:18 -07002877 }
2878
2879 /* if we are not in the right thread, we need to wake up the loop */
2880 if (res != -1 && notify && EVBASE_NEED_NOTIFY(base))
2881 evthread_notify_base(base);
2882
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002883 event_debug_note_del_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002884
Haibo Huangb2279672019-05-31 16:12:39 -07002885 /* If the main thread is currently executing this event's callback,
2886 * and we are not the main thread, then we want to wait until the
2887 * callback is done before returning. That way, when this function
2888 * returns, it will be safe to free the user-supplied argument.
2889 */
2890#ifndef EVENT__DISABLE_THREAD_SUPPORT
2891 if (blocking != EVENT_DEL_NOBLOCK &&
2892 base->current_event == event_to_event_callback(ev) &&
2893 !EVBASE_IN_THREAD(base) &&
2894 (blocking == EVENT_DEL_BLOCK || !(ev->ev_events & EV_FINALIZE))) {
2895 ++base->current_event_waiters;
2896 EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2897 }
2898#endif
2899
Christopher Wileye8679812015-07-01 13:36:18 -07002900 return (res);
2901}
2902
2903void
2904event_active(struct event *ev, int res, short ncalls)
2905{
2906 if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) {
2907 event_warnx("%s: event has no event_base set.", __func__);
2908 return;
2909 }
2910
2911 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2912
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002913 event_debug_assert_is_setup_(ev);
Christopher Wileye8679812015-07-01 13:36:18 -07002914
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002915 event_active_nolock_(ev, res, ncalls);
Christopher Wileye8679812015-07-01 13:36:18 -07002916
2917 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2918}
2919
2920
2921void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002922event_active_nolock_(struct event *ev, int res, short ncalls)
Christopher Wileye8679812015-07-01 13:36:18 -07002923{
2924 struct event_base *base;
2925
2926 event_debug(("event_active: %p (fd "EV_SOCK_FMT"), res %d, callback %p",
2927 ev, EV_SOCK_ARG(ev->ev_fd), (int)res, ev->ev_callback));
2928
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002929 base = ev->ev_base;
2930 EVENT_BASE_ASSERT_LOCKED(base);
Josh Gao83a0c9c2017-08-10 12:30:25 -07002931
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002932 if (ev->ev_flags & EVLIST_FINALIZING) {
2933 /* XXXX debug */
Josh Gao83a0c9c2017-08-10 12:30:25 -07002934 return;
2935 }
2936
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002937 switch ((ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
2938 default:
2939 case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER:
2940 EVUTIL_ASSERT(0);
2941 break;
2942 case EVLIST_ACTIVE:
2943 /* We get different kinds of events, add them together */
2944 ev->ev_res |= res;
2945 return;
2946 case EVLIST_ACTIVE_LATER:
2947 ev->ev_res |= res;
2948 break;
2949 case 0:
2950 ev->ev_res = res;
2951 break;
2952 }
Christopher Wileye8679812015-07-01 13:36:18 -07002953
2954 if (ev->ev_pri < base->event_running_priority)
2955 base->event_continue = 1;
2956
2957 if (ev->ev_events & EV_SIGNAL) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002958#ifndef EVENT__DISABLE_THREAD_SUPPORT
2959 if (base->current_event == event_to_event_callback(ev) &&
2960 !EVBASE_IN_THREAD(base)) {
Christopher Wileye8679812015-07-01 13:36:18 -07002961 ++base->current_event_waiters;
2962 EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock);
2963 }
2964#endif
2965 ev->ev_ncalls = ncalls;
2966 ev->ev_pncalls = NULL;
2967 }
2968
Narayan Kamathfc74cb42017-09-13 12:53:52 +01002969 event_callback_activate_nolock_(base, event_to_event_callback(ev));
2970}
2971
2972void
2973event_active_later_(struct event *ev, int res)
2974{
2975 EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock);
2976 event_active_later_nolock_(ev, res);
2977 EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock);
2978}
2979
2980void
2981event_active_later_nolock_(struct event *ev, int res)
2982{
2983 struct event_base *base = ev->ev_base;
2984 EVENT_BASE_ASSERT_LOCKED(base);
2985
2986 if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) {
2987 /* We get different kinds of events, add them together */
2988 ev->ev_res |= res;
2989 return;
2990 }
2991
2992 ev->ev_res = res;
2993
2994 event_callback_activate_later_nolock_(base, event_to_event_callback(ev));
2995}
2996
2997int
2998event_callback_activate_(struct event_base *base,
2999 struct event_callback *evcb)
3000{
3001 int r;
3002 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3003 r = event_callback_activate_nolock_(base, evcb);
3004 EVBASE_RELEASE_LOCK(base, th_base_lock);
3005 return r;
3006}
3007
3008int
3009event_callback_activate_nolock_(struct event_base *base,
3010 struct event_callback *evcb)
3011{
3012 int r = 1;
3013
3014 if (evcb->evcb_flags & EVLIST_FINALIZING)
3015 return 0;
3016
3017 switch (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) {
3018 default:
3019 EVUTIL_ASSERT(0);
Haibo Huangb2279672019-05-31 16:12:39 -07003020 EVUTIL_FALLTHROUGH;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003021 case EVLIST_ACTIVE_LATER:
3022 event_queue_remove_active_later(base, evcb);
3023 r = 0;
3024 break;
3025 case EVLIST_ACTIVE:
3026 return 0;
3027 case 0:
3028 break;
3029 }
3030
3031 event_queue_insert_active(base, evcb);
Christopher Wileye8679812015-07-01 13:36:18 -07003032
3033 if (EVBASE_NEED_NOTIFY(base))
3034 evthread_notify_base(base);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003035
3036 return r;
3037}
3038
3039int
3040event_callback_activate_later_nolock_(struct event_base *base,
3041 struct event_callback *evcb)
3042{
3043 if (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))
3044 return 0;
3045
3046 event_queue_insert_active_later(base, evcb);
3047 if (EVBASE_NEED_NOTIFY(base))
3048 evthread_notify_base(base);
3049 return 1;
Christopher Wileye8679812015-07-01 13:36:18 -07003050}
3051
3052void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003053event_callback_init_(struct event_base *base,
3054 struct event_callback *cb)
Christopher Wileye8679812015-07-01 13:36:18 -07003055{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003056 memset(cb, 0, sizeof(*cb));
3057 cb->evcb_pri = base->nactivequeues - 1;
3058}
3059
3060int
3061event_callback_cancel_(struct event_base *base,
3062 struct event_callback *evcb)
3063{
3064 int r;
3065 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3066 r = event_callback_cancel_nolock_(base, evcb, 0);
3067 EVBASE_RELEASE_LOCK(base, th_base_lock);
3068 return r;
3069}
3070
3071int
3072event_callback_cancel_nolock_(struct event_base *base,
3073 struct event_callback *evcb, int even_if_finalizing)
3074{
3075 if ((evcb->evcb_flags & EVLIST_FINALIZING) && !even_if_finalizing)
3076 return 0;
3077
3078 if (evcb->evcb_flags & EVLIST_INIT)
3079 return event_del_nolock_(event_callback_to_event(evcb),
3080 even_if_finalizing ? EVENT_DEL_EVEN_IF_FINALIZING : EVENT_DEL_AUTOBLOCK);
3081
3082 switch ((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) {
3083 default:
3084 case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER:
3085 EVUTIL_ASSERT(0);
3086 break;
3087 case EVLIST_ACTIVE:
3088 /* We get different kinds of events, add them together */
3089 event_queue_remove_active(base, evcb);
3090 return 0;
3091 case EVLIST_ACTIVE_LATER:
3092 event_queue_remove_active_later(base, evcb);
3093 break;
3094 case 0:
3095 break;
3096 }
3097
3098 return 0;
Elliott Hughes2a572d12017-08-07 14:18:18 -07003099}
3100
Josh Gao83a0c9c2017-08-10 12:30:25 -07003101void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003102event_deferred_cb_init_(struct event_callback *cb, ev_uint8_t priority, deferred_cb_fn fn, void *arg)
Elliott Hughes2a572d12017-08-07 14:18:18 -07003103{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003104 memset(cb, 0, sizeof(*cb));
3105 cb->evcb_cb_union.evcb_selfcb = fn;
3106 cb->evcb_arg = arg;
3107 cb->evcb_pri = priority;
3108 cb->evcb_closure = EV_CLOSURE_CB_SELF;
Josh Gao83a0c9c2017-08-10 12:30:25 -07003109}
3110
3111void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003112event_deferred_cb_set_priority_(struct event_callback *cb, ev_uint8_t priority)
Josh Gao83a0c9c2017-08-10 12:30:25 -07003113{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003114 cb->evcb_pri = priority;
3115}
Josh Gao83a0c9c2017-08-10 12:30:25 -07003116
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003117void
3118event_deferred_cb_cancel_(struct event_base *base, struct event_callback *cb)
3119{
3120 if (!base)
3121 base = current_base;
3122 event_callback_cancel_(base, cb);
3123}
3124
3125#define MAX_DEFERREDS_QUEUED 32
3126int
3127event_deferred_cb_schedule_(struct event_base *base, struct event_callback *cb)
3128{
3129 int r = 1;
3130 if (!base)
3131 base = current_base;
3132 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3133 if (base->n_deferreds_queued > MAX_DEFERREDS_QUEUED) {
3134 r = event_callback_activate_later_nolock_(base, cb);
3135 } else {
3136 r = event_callback_activate_nolock_(base, cb);
3137 if (r) {
3138 ++base->n_deferreds_queued;
3139 }
Josh Gao83a0c9c2017-08-10 12:30:25 -07003140 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003141 EVBASE_RELEASE_LOCK(base, th_base_lock);
3142 return r;
Christopher Wileye8679812015-07-01 13:36:18 -07003143}
3144
3145static int
3146timeout_next(struct event_base *base, struct timeval **tv_p)
3147{
3148 /* Caller must hold th_base_lock */
3149 struct timeval now;
3150 struct event *ev;
3151 struct timeval *tv = *tv_p;
3152 int res = 0;
3153
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003154 ev = min_heap_top_(&base->timeheap);
Christopher Wileye8679812015-07-01 13:36:18 -07003155
3156 if (ev == NULL) {
3157 /* if no time-based events are active wait for I/O */
3158 *tv_p = NULL;
3159 goto out;
3160 }
3161
3162 if (gettime(base, &now) == -1) {
3163 res = -1;
3164 goto out;
3165 }
3166
3167 if (evutil_timercmp(&ev->ev_timeout, &now, <=)) {
3168 evutil_timerclear(tv);
3169 goto out;
3170 }
3171
3172 evutil_timersub(&ev->ev_timeout, &now, tv);
3173
3174 EVUTIL_ASSERT(tv->tv_sec >= 0);
3175 EVUTIL_ASSERT(tv->tv_usec >= 0);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003176 event_debug(("timeout_next: event: %p, in %d seconds, %d useconds", ev, (int)tv->tv_sec, (int)tv->tv_usec));
Christopher Wileye8679812015-07-01 13:36:18 -07003177
3178out:
3179 return (res);
3180}
3181
Christopher Wileye8679812015-07-01 13:36:18 -07003182/* Activate every event whose timeout has elapsed. */
3183static void
3184timeout_process(struct event_base *base)
3185{
3186 /* Caller must hold lock. */
3187 struct timeval now;
3188 struct event *ev;
3189
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003190 if (min_heap_empty_(&base->timeheap)) {
Christopher Wileye8679812015-07-01 13:36:18 -07003191 return;
3192 }
3193
3194 gettime(base, &now);
3195
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003196 while ((ev = min_heap_top_(&base->timeheap))) {
Christopher Wileye8679812015-07-01 13:36:18 -07003197 if (evutil_timercmp(&ev->ev_timeout, &now, >))
3198 break;
3199
3200 /* delete this event from the I/O queues */
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003201 event_del_nolock_(ev, EVENT_DEL_NOBLOCK);
Christopher Wileye8679812015-07-01 13:36:18 -07003202
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003203 event_debug(("timeout_process: event: %p, call %p",
3204 ev, ev->ev_callback));
3205 event_active_nolock_(ev, EV_TIMEOUT, 1);
Christopher Wileye8679812015-07-01 13:36:18 -07003206 }
3207}
3208
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003209#ifndef MAX
3210#define MAX(a,b) (((a)>(b))?(a):(b))
3211#endif
3212
3213#define MAX_EVENT_COUNT(var, v) var = MAX(var, v)
3214
3215/* These are a fancy way to spell
Haibo Huangb2279672019-05-31 16:12:39 -07003216 if (~flags & EVLIST_INTERNAL)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003217 base->event_count--/++;
3218*/
3219#define DECR_EVENT_COUNT(base,flags) \
Haibo Huangb2279672019-05-31 16:12:39 -07003220 ((base)->event_count -= !((flags) & EVLIST_INTERNAL))
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003221#define INCR_EVENT_COUNT(base,flags) do { \
Haibo Huangb2279672019-05-31 16:12:39 -07003222 ((base)->event_count += !((flags) & EVLIST_INTERNAL)); \
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003223 MAX_EVENT_COUNT((base)->event_count_max, (base)->event_count); \
3224} while (0)
3225
Christopher Wileye8679812015-07-01 13:36:18 -07003226static void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003227event_queue_remove_inserted(struct event_base *base, struct event *ev)
Christopher Wileye8679812015-07-01 13:36:18 -07003228{
3229 EVENT_BASE_ASSERT_LOCKED(base);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003230 if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_INSERTED))) {
Christopher Wileye8679812015-07-01 13:36:18 -07003231 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__,
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003232 ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_INSERTED);
3233 return;
3234 }
3235 DECR_EVENT_COUNT(base, ev->ev_flags);
3236 ev->ev_flags &= ~EVLIST_INSERTED;
3237}
3238static void
3239event_queue_remove_active(struct event_base *base, struct event_callback *evcb)
3240{
3241 EVENT_BASE_ASSERT_LOCKED(base);
3242 if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE))) {
3243 event_errx(1, "%s: %p not on queue %x", __func__,
3244 evcb, EVLIST_ACTIVE);
3245 return;
3246 }
3247 DECR_EVENT_COUNT(base, evcb->evcb_flags);
3248 evcb->evcb_flags &= ~EVLIST_ACTIVE;
3249 base->event_count_active--;
3250
3251 TAILQ_REMOVE(&base->activequeues[evcb->evcb_pri],
3252 evcb, evcb_active_next);
3253}
3254static void
3255event_queue_remove_active_later(struct event_base *base, struct event_callback *evcb)
3256{
3257 EVENT_BASE_ASSERT_LOCKED(base);
3258 if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE_LATER))) {
3259 event_errx(1, "%s: %p not on queue %x", __func__,
3260 evcb, EVLIST_ACTIVE_LATER);
3261 return;
3262 }
3263 DECR_EVENT_COUNT(base, evcb->evcb_flags);
3264 evcb->evcb_flags &= ~EVLIST_ACTIVE_LATER;
3265 base->event_count_active--;
3266
3267 TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next);
3268}
3269static void
3270event_queue_remove_timeout(struct event_base *base, struct event *ev)
3271{
3272 EVENT_BASE_ASSERT_LOCKED(base);
3273 if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_TIMEOUT))) {
3274 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__,
3275 ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_TIMEOUT);
3276 return;
3277 }
3278 DECR_EVENT_COUNT(base, ev->ev_flags);
3279 ev->ev_flags &= ~EVLIST_TIMEOUT;
3280
3281 if (is_common_timeout(&ev->ev_timeout, base)) {
3282 struct common_timeout_list *ctl =
3283 get_common_timeout_list(base, &ev->ev_timeout);
3284 TAILQ_REMOVE(&ctl->events, ev,
3285 ev_timeout_pos.ev_next_with_common_timeout);
3286 } else {
3287 min_heap_erase_(&base->timeheap, ev);
3288 }
3289}
3290
3291#ifdef USE_REINSERT_TIMEOUT
3292/* Remove and reinsert 'ev' into the timeout queue. */
3293static void
3294event_queue_reinsert_timeout(struct event_base *base, struct event *ev,
3295 int was_common, int is_common, int old_timeout_idx)
3296{
3297 struct common_timeout_list *ctl;
3298 if (!(ev->ev_flags & EVLIST_TIMEOUT)) {
3299 event_queue_insert_timeout(base, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07003300 return;
3301 }
3302
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003303 switch ((was_common<<1) | is_common) {
3304 case 3: /* Changing from one common timeout to another */
3305 ctl = base->common_timeout_queues[old_timeout_idx];
3306 TAILQ_REMOVE(&ctl->events, ev,
3307 ev_timeout_pos.ev_next_with_common_timeout);
3308 ctl = get_common_timeout_list(base, &ev->ev_timeout);
3309 insert_common_timeout_inorder(ctl, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07003310 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003311 case 2: /* Was common; is no longer common */
3312 ctl = base->common_timeout_queues[old_timeout_idx];
3313 TAILQ_REMOVE(&ctl->events, ev,
3314 ev_timeout_pos.ev_next_with_common_timeout);
3315 min_heap_push_(&base->timeheap, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07003316 break;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003317 case 1: /* Wasn't common; has become common. */
3318 min_heap_erase_(&base->timeheap, ev);
3319 ctl = get_common_timeout_list(base, &ev->ev_timeout);
3320 insert_common_timeout_inorder(ctl, ev);
3321 break;
3322 case 0: /* was in heap; is still on heap. */
3323 min_heap_adjust_(&base->timeheap, ev);
Christopher Wileye8679812015-07-01 13:36:18 -07003324 break;
3325 default:
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003326 EVUTIL_ASSERT(0); /* unreachable */
3327 break;
Christopher Wileye8679812015-07-01 13:36:18 -07003328 }
3329}
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003330#endif
Christopher Wileye8679812015-07-01 13:36:18 -07003331
3332/* Add 'ev' to the common timeout list in 'ev'. */
3333static void
3334insert_common_timeout_inorder(struct common_timeout_list *ctl,
3335 struct event *ev)
3336{
3337 struct event *e;
3338 /* By all logic, we should just be able to append 'ev' to the end of
3339 * ctl->events, since the timeout on each 'ev' is set to {the common
3340 * timeout} + {the time when we add the event}, and so the events
3341 * should arrive in order of their timeeouts. But just in case
3342 * there's some wacky threading issue going on, we do a search from
3343 * the end of 'ev' to find the right insertion point.
3344 */
3345 TAILQ_FOREACH_REVERSE(e, &ctl->events,
3346 event_list, ev_timeout_pos.ev_next_with_common_timeout) {
3347 /* This timercmp is a little sneaky, since both ev and e have
3348 * magic values in tv_usec. Fortunately, they ought to have
3349 * the _same_ magic values in tv_usec. Let's assert for that.
3350 */
3351 EVUTIL_ASSERT(
3352 is_same_common_timeout(&e->ev_timeout, &ev->ev_timeout));
3353 if (evutil_timercmp(&ev->ev_timeout, &e->ev_timeout, >=)) {
3354 TAILQ_INSERT_AFTER(&ctl->events, e, ev,
3355 ev_timeout_pos.ev_next_with_common_timeout);
3356 return;
3357 }
3358 }
3359 TAILQ_INSERT_HEAD(&ctl->events, ev,
3360 ev_timeout_pos.ev_next_with_common_timeout);
3361}
3362
3363static void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003364event_queue_insert_inserted(struct event_base *base, struct event *ev)
Christopher Wileye8679812015-07-01 13:36:18 -07003365{
3366 EVENT_BASE_ASSERT_LOCKED(base);
3367
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003368 if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_INSERTED)) {
3369 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already inserted", __func__,
3370 ev, EV_SOCK_ARG(ev->ev_fd));
Elliott Hughes2a572d12017-08-07 14:18:18 -07003371 return;
Christopher Wileye8679812015-07-01 13:36:18 -07003372 }
Elliott Hughes2a572d12017-08-07 14:18:18 -07003373
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003374 INCR_EVENT_COUNT(base, ev->ev_flags);
Elliott Hughes2a572d12017-08-07 14:18:18 -07003375
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003376 ev->ev_flags |= EVLIST_INSERTED;
3377}
3378
3379static void
3380event_queue_insert_active(struct event_base *base, struct event_callback *evcb)
3381{
3382 EVENT_BASE_ASSERT_LOCKED(base);
3383
3384 if (evcb->evcb_flags & EVLIST_ACTIVE) {
3385 /* Double insertion is possible for active events */
3386 return;
Elliott Hughes2a572d12017-08-07 14:18:18 -07003387 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003388
3389 INCR_EVENT_COUNT(base, evcb->evcb_flags);
3390
3391 evcb->evcb_flags |= EVLIST_ACTIVE;
3392
3393 base->event_count_active++;
3394 MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
3395 EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
3396 TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri],
3397 evcb, evcb_active_next);
3398}
3399
3400static void
3401event_queue_insert_active_later(struct event_base *base, struct event_callback *evcb)
3402{
3403 EVENT_BASE_ASSERT_LOCKED(base);
3404 if (evcb->evcb_flags & (EVLIST_ACTIVE_LATER|EVLIST_ACTIVE)) {
3405 /* Double insertion is possible */
3406 return;
3407 }
3408
3409 INCR_EVENT_COUNT(base, evcb->evcb_flags);
3410 evcb->evcb_flags |= EVLIST_ACTIVE_LATER;
3411 base->event_count_active++;
3412 MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active);
3413 EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
3414 TAILQ_INSERT_TAIL(&base->active_later_queue, evcb, evcb_active_next);
3415}
3416
3417static void
3418event_queue_insert_timeout(struct event_base *base, struct event *ev)
3419{
3420 EVENT_BASE_ASSERT_LOCKED(base);
3421
3422 if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_TIMEOUT)) {
3423 event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on timeout", __func__,
3424 ev, EV_SOCK_ARG(ev->ev_fd));
3425 return;
3426 }
3427
3428 INCR_EVENT_COUNT(base, ev->ev_flags);
3429
3430 ev->ev_flags |= EVLIST_TIMEOUT;
3431
3432 if (is_common_timeout(&ev->ev_timeout, base)) {
3433 struct common_timeout_list *ctl =
3434 get_common_timeout_list(base, &ev->ev_timeout);
3435 insert_common_timeout_inorder(ctl, ev);
3436 } else {
3437 min_heap_push_(&base->timeheap, ev);
3438 }
3439}
3440
3441static void
3442event_queue_make_later_events_active(struct event_base *base)
3443{
3444 struct event_callback *evcb;
3445 EVENT_BASE_ASSERT_LOCKED(base);
3446
3447 while ((evcb = TAILQ_FIRST(&base->active_later_queue))) {
3448 TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next);
3449 evcb->evcb_flags = (evcb->evcb_flags & ~EVLIST_ACTIVE_LATER) | EVLIST_ACTIVE;
3450 EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues);
3451 TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], evcb, evcb_active_next);
3452 base->n_deferreds_queued += (evcb->evcb_closure == EV_CLOSURE_CB_SELF);
Christopher Wileye8679812015-07-01 13:36:18 -07003453 }
3454}
3455
3456/* Functions for debugging */
3457
3458const char *
3459event_get_version(void)
3460{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003461 return (EVENT__VERSION);
Christopher Wileye8679812015-07-01 13:36:18 -07003462}
3463
3464ev_uint32_t
3465event_get_version_number(void)
3466{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003467 return (EVENT__NUMERIC_VERSION);
Christopher Wileye8679812015-07-01 13:36:18 -07003468}
3469
3470/*
3471 * No thread-safe interface needed - the information should be the same
3472 * for all threads.
3473 */
3474
3475const char *
3476event_get_method(void)
3477{
3478 return (current_base->evsel->name);
3479}
3480
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003481#ifndef EVENT__DISABLE_MM_REPLACEMENT
3482static void *(*mm_malloc_fn_)(size_t sz) = NULL;
3483static void *(*mm_realloc_fn_)(void *p, size_t sz) = NULL;
3484static void (*mm_free_fn_)(void *p) = NULL;
Christopher Wileye8679812015-07-01 13:36:18 -07003485
3486void *
3487event_mm_malloc_(size_t sz)
3488{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003489 if (sz == 0)
3490 return NULL;
3491
3492 if (mm_malloc_fn_)
3493 return mm_malloc_fn_(sz);
Christopher Wileye8679812015-07-01 13:36:18 -07003494 else
3495 return malloc(sz);
3496}
3497
3498void *
3499event_mm_calloc_(size_t count, size_t size)
3500{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003501 if (count == 0 || size == 0)
3502 return NULL;
3503
3504 if (mm_malloc_fn_) {
Christopher Wileye8679812015-07-01 13:36:18 -07003505 size_t sz = count * size;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003506 void *p = NULL;
3507 if (count > EV_SIZE_MAX / size)
3508 goto error;
3509 p = mm_malloc_fn_(sz);
Christopher Wileye8679812015-07-01 13:36:18 -07003510 if (p)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003511 return memset(p, 0, sz);
3512 } else {
3513 void *p = calloc(count, size);
3514#ifdef _WIN32
3515 /* Windows calloc doesn't reliably set ENOMEM */
3516 if (p == NULL)
3517 goto error;
3518#endif
Christopher Wileye8679812015-07-01 13:36:18 -07003519 return p;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003520 }
3521
3522error:
3523 errno = ENOMEM;
3524 return NULL;
Christopher Wileye8679812015-07-01 13:36:18 -07003525}
3526
3527char *
3528event_mm_strdup_(const char *str)
3529{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003530 if (!str) {
3531 errno = EINVAL;
3532 return NULL;
3533 }
3534
3535 if (mm_malloc_fn_) {
Christopher Wileye8679812015-07-01 13:36:18 -07003536 size_t ln = strlen(str);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003537 void *p = NULL;
3538 if (ln == EV_SIZE_MAX)
3539 goto error;
3540 p = mm_malloc_fn_(ln+1);
Christopher Wileye8679812015-07-01 13:36:18 -07003541 if (p)
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003542 return memcpy(p, str, ln+1);
Christopher Wileye8679812015-07-01 13:36:18 -07003543 } else
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003544#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -07003545 return _strdup(str);
3546#else
3547 return strdup(str);
3548#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003549
3550error:
3551 errno = ENOMEM;
3552 return NULL;
Christopher Wileye8679812015-07-01 13:36:18 -07003553}
3554
3555void *
3556event_mm_realloc_(void *ptr, size_t sz)
3557{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003558 if (mm_realloc_fn_)
3559 return mm_realloc_fn_(ptr, sz);
Christopher Wileye8679812015-07-01 13:36:18 -07003560 else
3561 return realloc(ptr, sz);
3562}
3563
3564void
3565event_mm_free_(void *ptr)
3566{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003567 if (mm_free_fn_)
3568 mm_free_fn_(ptr);
Christopher Wileye8679812015-07-01 13:36:18 -07003569 else
3570 free(ptr);
3571}
3572
3573void
3574event_set_mem_functions(void *(*malloc_fn)(size_t sz),
3575 void *(*realloc_fn)(void *ptr, size_t sz),
3576 void (*free_fn)(void *ptr))
3577{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003578 mm_malloc_fn_ = malloc_fn;
3579 mm_realloc_fn_ = realloc_fn;
3580 mm_free_fn_ = free_fn;
Christopher Wileye8679812015-07-01 13:36:18 -07003581}
3582#endif
3583
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003584#ifdef EVENT__HAVE_EVENTFD
Christopher Wileye8679812015-07-01 13:36:18 -07003585static void
3586evthread_notify_drain_eventfd(evutil_socket_t fd, short what, void *arg)
3587{
3588 ev_uint64_t msg;
3589 ev_ssize_t r;
3590 struct event_base *base = arg;
3591
3592 r = read(fd, (void*) &msg, sizeof(msg));
3593 if (r<0 && errno != EAGAIN) {
3594 event_sock_warn(fd, "Error reading from eventfd");
3595 }
3596 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3597 base->is_notify_pending = 0;
3598 EVBASE_RELEASE_LOCK(base, th_base_lock);
3599}
3600#endif
3601
3602static void
3603evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg)
3604{
3605 unsigned char buf[1024];
3606 struct event_base *base = arg;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003607#ifdef _WIN32
Christopher Wileye8679812015-07-01 13:36:18 -07003608 while (recv(fd, (char*)buf, sizeof(buf), 0) > 0)
3609 ;
3610#else
3611 while (read(fd, (char*)buf, sizeof(buf)) > 0)
3612 ;
3613#endif
3614
3615 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3616 base->is_notify_pending = 0;
3617 EVBASE_RELEASE_LOCK(base, th_base_lock);
3618}
3619
3620int
3621evthread_make_base_notifiable(struct event_base *base)
3622{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003623 int r;
Christopher Wileye8679812015-07-01 13:36:18 -07003624 if (!base)
3625 return -1;
3626
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003627 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3628 r = evthread_make_base_notifiable_nolock_(base);
3629 EVBASE_RELEASE_LOCK(base, th_base_lock);
3630 return r;
3631}
Elliott Hughes2a572d12017-08-07 14:18:18 -07003632
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003633static int
3634evthread_make_base_notifiable_nolock_(struct event_base *base)
3635{
3636 void (*cb)(evutil_socket_t, short, void *);
3637 int (*notify)(struct event_base *);
3638
3639 if (base->th_notify_fn != NULL) {
3640 /* The base is already notifiable: we're doing fine. */
3641 return 0;
3642 }
3643
3644#if defined(EVENT__HAVE_WORKING_KQUEUE)
3645 if (base->evsel == &kqops && event_kq_add_notify_event_(base) == 0) {
3646 base->th_notify_fn = event_kq_notify_base_;
3647 /* No need to add an event here; the backend can wake
3648 * itself up just fine. */
3649 return 0;
3650 }
Christopher Wileye8679812015-07-01 13:36:18 -07003651#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003652
3653#ifdef EVENT__HAVE_EVENTFD
3654 base->th_notify_fd[0] = evutil_eventfd_(0,
3655 EVUTIL_EFD_CLOEXEC|EVUTIL_EFD_NONBLOCK);
Christopher Wileye8679812015-07-01 13:36:18 -07003656 if (base->th_notify_fd[0] >= 0) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003657 base->th_notify_fd[1] = -1;
Christopher Wileye8679812015-07-01 13:36:18 -07003658 notify = evthread_notify_base_eventfd;
3659 cb = evthread_notify_drain_eventfd;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003660 } else
Christopher Wileye8679812015-07-01 13:36:18 -07003661#endif
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003662 if (evutil_make_internal_pipe_(base->th_notify_fd) == 0) {
3663 notify = evthread_notify_base_default;
3664 cb = evthread_notify_drain_default;
3665 } else {
3666 return -1;
Josh Gao83a0c9c2017-08-10 12:30:25 -07003667 }
Josh Gao83a0c9c2017-08-10 12:30:25 -07003668
Christopher Wileye8679812015-07-01 13:36:18 -07003669 base->th_notify_fn = notify;
3670
Christopher Wileye8679812015-07-01 13:36:18 -07003671 /* prepare an event that we can use for wakeup */
3672 event_assign(&base->th_notify, base, base->th_notify_fd[0],
3673 EV_READ|EV_PERSIST, cb, base);
3674
3675 /* we need to mark this as internal event */
3676 base->th_notify.ev_flags |= EVLIST_INTERNAL;
3677 event_priority_set(&base->th_notify, 0);
3678
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003679 return event_add_nolock_(&base->th_notify, NULL, 0);
Christopher Wileye8679812015-07-01 13:36:18 -07003680}
3681
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003682int
3683event_base_foreach_event_nolock_(struct event_base *base,
3684 event_base_foreach_event_cb fn, void *arg)
3685{
3686 int r, i;
Haibo Huang45729092019-08-01 16:15:45 -07003687 unsigned u;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003688 struct event *ev;
3689
3690 /* Start out with all the EVLIST_INSERTED events. */
3691 if ((r = evmap_foreach_event_(base, fn, arg)))
3692 return r;
3693
3694 /* Okay, now we deal with those events that have timeouts and are in
3695 * the min-heap. */
3696 for (u = 0; u < base->timeheap.n; ++u) {
3697 ev = base->timeheap.p[u];
3698 if (ev->ev_flags & EVLIST_INSERTED) {
3699 /* we already processed this one */
3700 continue;
3701 }
3702 if ((r = fn(base, ev, arg)))
3703 return r;
3704 }
3705
3706 /* Now for the events in one of the timeout queues.
3707 * the min-heap. */
3708 for (i = 0; i < base->n_common_timeouts; ++i) {
3709 struct common_timeout_list *ctl =
3710 base->common_timeout_queues[i];
3711 TAILQ_FOREACH(ev, &ctl->events,
3712 ev_timeout_pos.ev_next_with_common_timeout) {
3713 if (ev->ev_flags & EVLIST_INSERTED) {
3714 /* we already processed this one */
3715 continue;
3716 }
3717 if ((r = fn(base, ev, arg)))
3718 return r;
3719 }
3720 }
3721
3722 /* Finally, we deal wit all the active events that we haven't touched
3723 * yet. */
3724 for (i = 0; i < base->nactivequeues; ++i) {
3725 struct event_callback *evcb;
3726 TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) {
3727 if ((evcb->evcb_flags & (EVLIST_INIT|EVLIST_INSERTED|EVLIST_TIMEOUT)) != EVLIST_INIT) {
3728 /* This isn't an event (evlist_init clear), or
3729 * we already processed it. (inserted or
3730 * timeout set */
3731 continue;
3732 }
3733 ev = event_callback_to_event(evcb);
3734 if ((r = fn(base, ev, arg)))
3735 return r;
3736 }
3737 }
3738
3739 return 0;
3740}
3741
3742/* Helper for event_base_dump_events: called on each event in the event base;
3743 * dumps only the inserted events. */
3744static int
3745dump_inserted_event_fn(const struct event_base *base, const struct event *e, void *arg)
3746{
3747 FILE *output = arg;
3748 const char *gloss = (e->ev_events & EV_SIGNAL) ?
3749 "sig" : "fd ";
3750
3751 if (! (e->ev_flags & (EVLIST_INSERTED|EVLIST_TIMEOUT)))
3752 return 0;
3753
Haibo Huangb2279672019-05-31 16:12:39 -07003754 fprintf(output, " %p [%s "EV_SOCK_FMT"]%s%s%s%s%s%s%s",
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003755 (void*)e, gloss, EV_SOCK_ARG(e->ev_fd),
3756 (e->ev_events&EV_READ)?" Read":"",
3757 (e->ev_events&EV_WRITE)?" Write":"",
3758 (e->ev_events&EV_CLOSED)?" EOF":"",
3759 (e->ev_events&EV_SIGNAL)?" Signal":"",
3760 (e->ev_events&EV_PERSIST)?" Persist":"",
Haibo Huangb2279672019-05-31 16:12:39 -07003761 (e->ev_events&EV_ET)?" ET":"",
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003762 (e->ev_flags&EVLIST_INTERNAL)?" Internal":"");
3763 if (e->ev_flags & EVLIST_TIMEOUT) {
3764 struct timeval tv;
3765 tv.tv_sec = e->ev_timeout.tv_sec;
3766 tv.tv_usec = e->ev_timeout.tv_usec & MICROSECONDS_MASK;
3767 evutil_timeradd(&tv, &base->tv_clock_diff, &tv);
3768 fprintf(output, " Timeout=%ld.%06d",
3769 (long)tv.tv_sec, (int)(tv.tv_usec & MICROSECONDS_MASK));
3770 }
3771 fputc('\n', output);
3772
3773 return 0;
3774}
3775
3776/* Helper for event_base_dump_events: called on each event in the event base;
3777 * dumps only the active events. */
3778static int
3779dump_active_event_fn(const struct event_base *base, const struct event *e, void *arg)
3780{
3781 FILE *output = arg;
3782 const char *gloss = (e->ev_events & EV_SIGNAL) ?
3783 "sig" : "fd ";
3784
3785 if (! (e->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)))
3786 return 0;
3787
3788 fprintf(output, " %p [%s "EV_SOCK_FMT", priority=%d]%s%s%s%s%s active%s%s\n",
3789 (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), e->ev_pri,
3790 (e->ev_res&EV_READ)?" Read":"",
3791 (e->ev_res&EV_WRITE)?" Write":"",
3792 (e->ev_res&EV_CLOSED)?" EOF":"",
3793 (e->ev_res&EV_SIGNAL)?" Signal":"",
3794 (e->ev_res&EV_TIMEOUT)?" Timeout":"",
3795 (e->ev_flags&EVLIST_INTERNAL)?" [Internal]":"",
3796 (e->ev_flags&EVLIST_ACTIVE_LATER)?" [NextTime]":"");
3797
3798 return 0;
3799}
3800
3801int
3802event_base_foreach_event(struct event_base *base,
3803 event_base_foreach_event_cb fn, void *arg)
3804{
3805 int r;
3806 if ((!fn) || (!base)) {
3807 return -1;
3808 }
3809 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3810 r = event_base_foreach_event_nolock_(base, fn, arg);
3811 EVBASE_RELEASE_LOCK(base, th_base_lock);
3812 return r;
3813}
3814
3815
Christopher Wileye8679812015-07-01 13:36:18 -07003816void
3817event_base_dump_events(struct event_base *base, FILE *output)
3818{
Elliott Hughes2a572d12017-08-07 14:18:18 -07003819 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003820 fprintf(output, "Inserted events:\n");
3821 event_base_foreach_event_nolock_(base, dump_inserted_event_fn, output);
3822
3823 fprintf(output, "Active events:\n");
3824 event_base_foreach_event_nolock_(base, dump_active_event_fn, output);
Elliott Hughes2a572d12017-08-07 14:18:18 -07003825 EVBASE_RELEASE_LOCK(base, th_base_lock);
3826}
3827
3828void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003829event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events)
3830{
3831 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
Haibo Huangb2279672019-05-31 16:12:39 -07003832
3833 /* Activate any non timer events */
3834 if (!(events & EV_TIMEOUT)) {
3835 evmap_io_active_(base, fd, events & (EV_READ|EV_WRITE|EV_CLOSED));
3836 } else {
3837 /* If we want to activate timer events, loop and activate each event with
3838 * the same fd in both the timeheap and common timeouts list */
3839 int i;
Haibo Huang45729092019-08-01 16:15:45 -07003840 unsigned u;
Haibo Huangb2279672019-05-31 16:12:39 -07003841 struct event *ev;
3842
3843 for (u = 0; u < base->timeheap.n; ++u) {
3844 ev = base->timeheap.p[u];
3845 if (ev->ev_fd == fd) {
3846 event_active_nolock_(ev, EV_TIMEOUT, 1);
3847 }
3848 }
3849
3850 for (i = 0; i < base->n_common_timeouts; ++i) {
3851 struct common_timeout_list *ctl = base->common_timeout_queues[i];
3852 TAILQ_FOREACH(ev, &ctl->events,
3853 ev_timeout_pos.ev_next_with_common_timeout) {
3854 if (ev->ev_fd == fd) {
3855 event_active_nolock_(ev, EV_TIMEOUT, 1);
3856 }
3857 }
3858 }
3859 }
3860
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003861 EVBASE_RELEASE_LOCK(base, th_base_lock);
3862}
3863
3864void
3865event_base_active_by_signal(struct event_base *base, int sig)
3866{
3867 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3868 evmap_signal_active_(base, sig, 1);
3869 EVBASE_RELEASE_LOCK(base, th_base_lock);
3870}
3871
3872
3873void
3874event_base_add_virtual_(struct event_base *base)
3875{
3876 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3877 base->virtual_event_count++;
3878 MAX_EVENT_COUNT(base->virtual_event_count_max, base->virtual_event_count);
3879 EVBASE_RELEASE_LOCK(base, th_base_lock);
3880}
3881
3882void
3883event_base_del_virtual_(struct event_base *base)
Christopher Wileye8679812015-07-01 13:36:18 -07003884{
3885 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3886 EVUTIL_ASSERT(base->virtual_event_count > 0);
3887 base->virtual_event_count--;
3888 if (base->virtual_event_count == 0 && EVBASE_NEED_NOTIFY(base))
3889 evthread_notify_base(base);
3890 EVBASE_RELEASE_LOCK(base, th_base_lock);
3891}
3892
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003893static void
3894event_free_debug_globals_locks(void)
3895{
3896#ifndef EVENT__DISABLE_THREAD_SUPPORT
3897#ifndef EVENT__DISABLE_DEBUG_MODE
3898 if (event_debug_map_lock_ != NULL) {
3899 EVTHREAD_FREE_LOCK(event_debug_map_lock_, 0);
3900 event_debug_map_lock_ = NULL;
3901 evthreadimpl_disable_lock_debugging_();
3902 }
3903#endif /* EVENT__DISABLE_DEBUG_MODE */
3904#endif /* EVENT__DISABLE_THREAD_SUPPORT */
3905 return;
3906}
3907
3908static void
3909event_free_debug_globals(void)
3910{
3911 event_free_debug_globals_locks();
3912}
3913
3914static void
3915event_free_evsig_globals(void)
3916{
3917 evsig_free_globals_();
3918}
3919
3920static void
3921event_free_evutil_globals(void)
3922{
3923 evutil_free_globals_();
3924}
3925
3926static void
3927event_free_globals(void)
3928{
3929 event_free_debug_globals();
3930 event_free_evsig_globals();
3931 event_free_evutil_globals();
3932}
3933
3934void
3935libevent_global_shutdown(void)
3936{
3937 event_disable_debug_mode();
3938 event_free_globals();
3939}
3940
3941#ifndef EVENT__DISABLE_THREAD_SUPPORT
Christopher Wileye8679812015-07-01 13:36:18 -07003942int
3943event_global_setup_locks_(const int enable_locks)
3944{
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003945#ifndef EVENT__DISABLE_DEBUG_MODE
3946 EVTHREAD_SETUP_GLOBAL_LOCK(event_debug_map_lock_, 0);
Christopher Wileye8679812015-07-01 13:36:18 -07003947#endif
3948 if (evsig_global_setup_locks_(enable_locks) < 0)
3949 return -1;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003950 if (evutil_global_setup_locks_(enable_locks) < 0)
3951 return -1;
Christopher Wileye8679812015-07-01 13:36:18 -07003952 if (evutil_secure_rng_global_setup_locks_(enable_locks) < 0)
3953 return -1;
3954 return 0;
3955}
3956#endif
3957
3958void
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003959event_base_assert_ok_(struct event_base *base)
3960{
3961 EVBASE_ACQUIRE_LOCK(base, th_base_lock);
3962 event_base_assert_ok_nolock_(base);
3963 EVBASE_RELEASE_LOCK(base, th_base_lock);
3964}
3965
3966void
3967event_base_assert_ok_nolock_(struct event_base *base)
Christopher Wileye8679812015-07-01 13:36:18 -07003968{
3969 int i;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003970 int count;
3971
3972 /* First do checks on the per-fd and per-signal lists */
3973 evmap_check_integrity_(base);
Christopher Wileye8679812015-07-01 13:36:18 -07003974
3975 /* Check the heap property */
Haibo Huang45729092019-08-01 16:15:45 -07003976 for (i = 1; i < (int)base->timeheap.n; ++i) {
3977 int parent = (i - 1) / 2;
Christopher Wileye8679812015-07-01 13:36:18 -07003978 struct event *ev, *p_ev;
Haibo Huang45729092019-08-01 16:15:45 -07003979 ev = base->timeheap.p[i];
Christopher Wileye8679812015-07-01 13:36:18 -07003980 p_ev = base->timeheap.p[parent];
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003981 EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT);
Christopher Wileye8679812015-07-01 13:36:18 -07003982 EVUTIL_ASSERT(evutil_timercmp(&p_ev->ev_timeout, &ev->ev_timeout, <=));
Haibo Huang45729092019-08-01 16:15:45 -07003983 EVUTIL_ASSERT(ev->ev_timeout_pos.min_heap_idx == i);
Christopher Wileye8679812015-07-01 13:36:18 -07003984 }
3985
3986 /* Check that the common timeouts are fine */
3987 for (i = 0; i < base->n_common_timeouts; ++i) {
3988 struct common_timeout_list *ctl = base->common_timeout_queues[i];
3989 struct event *last=NULL, *ev;
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003990
3991 EVUTIL_ASSERT_TAILQ_OK(&ctl->events, event, ev_timeout_pos.ev_next_with_common_timeout);
3992
Christopher Wileye8679812015-07-01 13:36:18 -07003993 TAILQ_FOREACH(ev, &ctl->events, ev_timeout_pos.ev_next_with_common_timeout) {
3994 if (last)
3995 EVUTIL_ASSERT(evutil_timercmp(&last->ev_timeout, &ev->ev_timeout, <=));
Narayan Kamathfc74cb42017-09-13 12:53:52 +01003996 EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT);
Christopher Wileye8679812015-07-01 13:36:18 -07003997 EVUTIL_ASSERT(is_common_timeout(&ev->ev_timeout,base));
3998 EVUTIL_ASSERT(COMMON_TIMEOUT_IDX(&ev->ev_timeout) == i);
3999 last = ev;
4000 }
4001 }
4002
Narayan Kamathfc74cb42017-09-13 12:53:52 +01004003 /* Check the active queues. */
4004 count = 0;
4005 for (i = 0; i < base->nactivequeues; ++i) {
4006 struct event_callback *evcb;
4007 EVUTIL_ASSERT_TAILQ_OK(&base->activequeues[i], event_callback, evcb_active_next);
4008 TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) {
4009 EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE);
4010 EVUTIL_ASSERT(evcb->evcb_pri == i);
4011 ++count;
4012 }
4013 }
4014
4015 {
4016 struct event_callback *evcb;
4017 TAILQ_FOREACH(evcb, &base->active_later_queue, evcb_active_next) {
4018 EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE_LATER);
4019 ++count;
4020 }
4021 }
4022 EVUTIL_ASSERT(count == base->event_count_active);
Christopher Wileye8679812015-07-01 13:36:18 -07004023}