Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> |
3 | | * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * 3. The name of the author may not be used to endorse or promote products |
14 | | * derived from this software without specific prior written permission. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | | */ |
27 | | #include "event2/event-config.h" |
28 | | #include "evconfig-private.h" |
29 | | |
30 | | #ifdef _WIN32 |
31 | | #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> |
37 | | #if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H) |
38 | | #include <sys/time.h> |
39 | | #endif |
40 | | #include <sys/queue.h> |
41 | | #ifdef EVENT__HAVE_SYS_SOCKET_H |
42 | | #include <sys/socket.h> |
43 | | #endif |
44 | | #include <stdio.h> |
45 | | #include <stdlib.h> |
46 | | #ifdef EVENT__HAVE_UNISTD_H |
47 | | #include <unistd.h> |
48 | | #endif |
49 | | #include <ctype.h> |
50 | | #include <errno.h> |
51 | | #include <signal.h> |
52 | | #include <string.h> |
53 | | #include <time.h> |
54 | | #include <limits.h> |
55 | | |
56 | | #include "event2/event.h" |
57 | | #include "event2/event_struct.h" |
58 | | #include "event2/event_compat.h" |
59 | | #include "event-internal.h" |
60 | | #include "defer-internal.h" |
61 | | #include "evthread-internal.h" |
62 | | #include "event2/thread.h" |
63 | | #include "event2/util.h" |
64 | | #include "log-internal.h" |
65 | | #include "evmap-internal.h" |
66 | | #include "iocp-internal.h" |
67 | | #include "changelist-internal.h" |
68 | | #define HT_NO_CACHE_HASH_VALUES |
69 | | #include "ht-internal.h" |
70 | | #include "util-internal.h" |
71 | | |
72 | | |
73 | | #ifdef EVENT__HAVE_WORKING_KQUEUE |
74 | | #include "kqueue-internal.h" |
75 | | #endif |
76 | | |
77 | | #ifdef EVENT__HAVE_EVENT_PORTS |
78 | | extern const struct eventop evportops; |
79 | | #endif |
80 | | #ifdef EVENT__HAVE_SELECT |
81 | | extern const struct eventop selectops; |
82 | | #endif |
83 | | #ifdef EVENT__HAVE_POLL |
84 | | extern const struct eventop pollops; |
85 | | #endif |
86 | | #ifdef EVENT__HAVE_EPOLL |
87 | | extern const struct eventop epollops; |
88 | | #endif |
89 | | #ifdef EVENT__HAVE_WORKING_KQUEUE |
90 | | extern const struct eventop kqops; |
91 | | #endif |
92 | | #ifdef EVENT__HAVE_DEVPOLL |
93 | | extern const struct eventop devpollops; |
94 | | #endif |
95 | | #ifdef _WIN32 |
96 | | extern const struct eventop win32ops; |
97 | | #endif |
98 | | |
99 | | /* Array of backends in order of preference. */ |
100 | | static const struct eventop *eventops[] = { |
101 | | #ifdef EVENT__HAVE_EVENT_PORTS |
102 | | &evportops, |
103 | | #endif |
104 | | #ifdef EVENT__HAVE_WORKING_KQUEUE |
105 | | &kqops, |
106 | | #endif |
107 | | #ifdef EVENT__HAVE_EPOLL |
108 | | &epollops, |
109 | | #endif |
110 | | #ifdef EVENT__HAVE_DEVPOLL |
111 | | &devpollops, |
112 | | #endif |
113 | | #ifdef EVENT__HAVE_POLL |
114 | | &pollops, |
115 | | #endif |
116 | | #ifdef EVENT__HAVE_SELECT |
117 | | &selectops, |
118 | | #endif |
119 | | #ifdef _WIN32 |
120 | | &win32ops, |
121 | | #endif |
122 | | NULL |
123 | | }; |
124 | | |
125 | | /* Global state; deprecated */ |
126 | | EVENT2_EXPORT_SYMBOL |
127 | | struct event_base *event_global_current_base_ = NULL; |
128 | 0 | #define current_base event_global_current_base_ |
129 | | |
130 | | /* Global state */ |
131 | | |
132 | | static void *event_self_cbarg_ptr_ = NULL; |
133 | | |
134 | | /* Prototypes */ |
135 | | static void event_queue_insert_active(struct event_base *, struct event_callback *); |
136 | | static void event_queue_insert_active_later(struct event_base *, struct event_callback *); |
137 | | static void event_queue_insert_timeout(struct event_base *, struct event *); |
138 | | static void event_queue_insert_inserted(struct event_base *, struct event *); |
139 | | static void event_queue_remove_active(struct event_base *, struct event_callback *); |
140 | | static void event_queue_remove_active_later(struct event_base *, struct event_callback *); |
141 | | static void event_queue_remove_timeout(struct event_base *, struct event *); |
142 | | static void event_queue_remove_inserted(struct event_base *, struct event *); |
143 | | static void event_queue_make_later_events_active(struct event_base *base); |
144 | | |
145 | | static int evthread_make_base_notifiable_nolock_(struct event_base *base); |
146 | | static int event_del_(struct event *ev, int blocking); |
147 | | |
148 | | #ifdef USE_REINSERT_TIMEOUT |
149 | | /* This code seems buggy; only turn it on if we find out what the trouble is. */ |
150 | | static void event_queue_reinsert_timeout(struct event_base *,struct event *, int was_common, int is_common, int old_timeout_idx); |
151 | | #endif |
152 | | |
153 | | static int event_haveevents(struct event_base *); |
154 | | |
155 | | static int event_process_active(struct event_base *); |
156 | | |
157 | | static int timeout_next(struct event_base *, struct timeval **); |
158 | | static void timeout_process(struct event_base *); |
159 | | |
160 | | static inline void event_signal_closure(struct event_base *, struct event *ev); |
161 | | static inline void event_persist_closure(struct event_base *, struct event *ev); |
162 | | |
163 | | static int evthread_notify_base(struct event_base *base); |
164 | | |
165 | | static void insert_common_timeout_inorder(struct common_timeout_list *ctl, |
166 | | struct event *ev); |
167 | | |
168 | | #ifndef EVENT__DISABLE_DEBUG_MODE |
169 | | /* These functions implement a hashtable of which 'struct event *' structures |
170 | | * have been setup or added. We don't want to trust the content of the struct |
171 | | * event itself, since we're trying to work through cases where an event gets |
172 | | * clobbered or freed. Instead, we keep a hashtable indexed by the pointer. |
173 | | */ |
174 | | |
175 | | struct event_debug_entry { |
176 | | HT_ENTRY(event_debug_entry) node; |
177 | | const struct event *ptr; |
178 | | unsigned added : 1; |
179 | | }; |
180 | | |
181 | | static inline unsigned |
182 | | hash_debug_entry(const struct event_debug_entry *e) |
183 | 0 | { |
184 | 0 | /* We need to do this silliness to convince compilers that we |
185 | 0 | * honestly mean to cast e->ptr to an integer, and discard any |
186 | 0 | * part of it that doesn't fit in an unsigned. |
187 | 0 | */ |
188 | 0 | unsigned u = (unsigned) ((ev_uintptr_t) e->ptr); |
189 | 0 | /* Our hashtable implementation is pretty sensitive to low bits, |
190 | 0 | * and every struct event is over 64 bytes in size, so we can |
191 | 0 | * just say >>6. */ |
192 | 0 | return (u >> 6); |
193 | 0 | } |
194 | | |
195 | | static inline int |
196 | | eq_debug_entry(const struct event_debug_entry *a, |
197 | | const struct event_debug_entry *b) |
198 | 0 | { |
199 | 0 | return a->ptr == b->ptr; |
200 | 0 | } |
201 | | |
202 | | int event_debug_mode_on_ = 0; |
203 | | |
204 | | |
205 | | #if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE) |
206 | | /** |
207 | | * @brief debug mode variable which is set for any function/structure that needs |
208 | | * to be shared across threads (if thread support is enabled). |
209 | | * |
210 | | * When and if evthreads are initialized, this variable will be evaluated, |
211 | | * and if set to something other than zero, this means the evthread setup |
212 | | * functions were called out of order. |
213 | | * |
214 | | * See: "Locks and threading" in the documentation. |
215 | | */ |
216 | | int event_debug_created_threadable_ctx_ = 0; |
217 | | #endif |
218 | | |
219 | | /* Set if it's too late to enable event_debug_mode. */ |
220 | | static int event_debug_mode_too_late = 0; |
221 | | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
222 | | static void *event_debug_map_lock_ = NULL; |
223 | | #endif |
224 | | static HT_HEAD(event_debug_map, event_debug_entry) global_debug_map = |
225 | | HT_INITIALIZER(); |
226 | | |
227 | | HT_PROTOTYPE(event_debug_map, event_debug_entry, node, hash_debug_entry, |
228 | | eq_debug_entry) |
229 | | HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry, |
230 | 0 | eq_debug_entry, 0.5, mm_malloc, mm_realloc, mm_free) |
231 | | |
232 | | /* record that ev is now setup (that is, ready for an add) */ |
233 | | static void event_debug_note_setup_(const struct event *ev) |
234 | 0 | { |
235 | 0 | struct event_debug_entry *dent, find; |
236 | 0 |
|
237 | 0 | if (!event_debug_mode_on_) { |
238 | 0 | goto out; |
239 | 0 | } |
240 | 0 | |
241 | 0 | find.ptr = ev; |
242 | 0 | EVLOCK_LOCK(event_debug_map_lock_, 0); |
243 | 0 | dent = HT_FIND(event_debug_map, &global_debug_map, &find); |
244 | 0 | if (dent) { |
245 | 0 | dent->added = 0; |
246 | 0 | } else { |
247 | 0 | dent = mm_malloc(sizeof(*dent)); |
248 | 0 | if (!dent) |
249 | 0 | event_err(1, |
250 | 0 | "Out of memory in debugging code"); |
251 | 0 | dent->ptr = ev; |
252 | 0 | dent->added = 0; |
253 | 0 | HT_INSERT(event_debug_map, &global_debug_map, dent); |
254 | 0 | } |
255 | 0 | EVLOCK_UNLOCK(event_debug_map_lock_, 0); |
256 | 0 |
|
257 | 0 | out: |
258 | 0 | event_debug_mode_too_late = 1; |
259 | 0 | } |
260 | | /* record that ev is no longer setup */ |
261 | | static void event_debug_note_teardown_(const struct event *ev) |
262 | 0 | { |
263 | 0 | struct event_debug_entry *dent, find; |
264 | 0 |
|
265 | 0 | if (!event_debug_mode_on_) { |
266 | 0 | goto out; |
267 | 0 | } |
268 | 0 | |
269 | 0 | find.ptr = ev; |
270 | 0 | EVLOCK_LOCK(event_debug_map_lock_, 0); |
271 | 0 | dent = HT_REMOVE(event_debug_map, &global_debug_map, &find); |
272 | 0 | if (dent) |
273 | 0 | mm_free(dent); |
274 | 0 | EVLOCK_UNLOCK(event_debug_map_lock_, 0); |
275 | 0 |
|
276 | 0 | out: |
277 | 0 | event_debug_mode_too_late = 1; |
278 | 0 | } |
279 | | /* Macro: record that ev is now added */ |
280 | | static void event_debug_note_add_(const struct event *ev) |
281 | 0 | { |
282 | 0 | struct event_debug_entry *dent,find; |
283 | 0 |
|
284 | 0 | if (!event_debug_mode_on_) { |
285 | 0 | goto out; |
286 | 0 | } |
287 | 0 | |
288 | 0 | find.ptr = ev; |
289 | 0 | EVLOCK_LOCK(event_debug_map_lock_, 0); |
290 | 0 | dent = HT_FIND(event_debug_map, &global_debug_map, &find); |
291 | 0 | if (dent) { |
292 | 0 | dent->added = 1; |
293 | 0 | } else { |
294 | 0 | event_errx(EVENT_ERR_ABORT_, |
295 | 0 | "%s: noting an add on a non-setup event %p" |
296 | 0 | " (events: 0x%x, fd: "EV_SOCK_FMT |
297 | 0 | ", flags: 0x%x)", |
298 | 0 | __func__, ev, ev->ev_events, |
299 | 0 | EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); |
300 | 0 | } |
301 | 0 | EVLOCK_UNLOCK(event_debug_map_lock_, 0); |
302 | 0 |
|
303 | 0 | out: |
304 | 0 | event_debug_mode_too_late = 1; |
305 | 0 | } |
306 | | /* record that ev is no longer added */ |
307 | | static void event_debug_note_del_(const struct event *ev) |
308 | 0 | { |
309 | 0 | struct event_debug_entry *dent, find; |
310 | 0 |
|
311 | 0 | if (!event_debug_mode_on_) { |
312 | 0 | goto out; |
313 | 0 | } |
314 | 0 | |
315 | 0 | find.ptr = ev; |
316 | 0 | EVLOCK_LOCK(event_debug_map_lock_, 0); |
317 | 0 | dent = HT_FIND(event_debug_map, &global_debug_map, &find); |
318 | 0 | if (dent) { |
319 | 0 | dent->added = 0; |
320 | 0 | } else { |
321 | 0 | event_errx(EVENT_ERR_ABORT_, |
322 | 0 | "%s: noting a del on a non-setup event %p" |
323 | 0 | " (events: 0x%x, fd: "EV_SOCK_FMT |
324 | 0 | ", flags: 0x%x)", |
325 | 0 | __func__, ev, ev->ev_events, |
326 | 0 | EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); |
327 | 0 | } |
328 | 0 | EVLOCK_UNLOCK(event_debug_map_lock_, 0); |
329 | 0 |
|
330 | 0 | out: |
331 | 0 | event_debug_mode_too_late = 1; |
332 | 0 | } |
333 | | /* assert that ev is setup (i.e., okay to add or inspect) */ |
334 | | static void event_debug_assert_is_setup_(const struct event *ev) |
335 | 0 | { |
336 | 0 | struct event_debug_entry *dent, find; |
337 | 0 |
|
338 | 0 | if (!event_debug_mode_on_) { |
339 | 0 | return; |
340 | 0 | } |
341 | 0 | |
342 | 0 | find.ptr = ev; |
343 | 0 | EVLOCK_LOCK(event_debug_map_lock_, 0); |
344 | 0 | dent = HT_FIND(event_debug_map, &global_debug_map, &find); |
345 | 0 | if (!dent) { |
346 | 0 | event_errx(EVENT_ERR_ABORT_, |
347 | 0 | "%s called on a non-initialized event %p" |
348 | 0 | " (events: 0x%x, fd: "EV_SOCK_FMT |
349 | 0 | ", flags: 0x%x)", |
350 | 0 | __func__, ev, ev->ev_events, |
351 | 0 | EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); |
352 | 0 | } |
353 | 0 | EVLOCK_UNLOCK(event_debug_map_lock_, 0); |
354 | 0 | } |
355 | | /* assert that ev is not added (i.e., okay to tear down or set up again) */ |
356 | | static void event_debug_assert_not_added_(const struct event *ev) |
357 | 0 | { |
358 | 0 | struct event_debug_entry *dent, find; |
359 | 0 |
|
360 | 0 | if (!event_debug_mode_on_) { |
361 | 0 | return; |
362 | 0 | } |
363 | 0 | |
364 | 0 | find.ptr = ev; |
365 | 0 | EVLOCK_LOCK(event_debug_map_lock_, 0); |
366 | 0 | dent = HT_FIND(event_debug_map, &global_debug_map, &find); |
367 | 0 | if (dent && dent->added) { |
368 | 0 | event_errx(EVENT_ERR_ABORT_, |
369 | 0 | "%s called on an already added event %p" |
370 | 0 | " (events: 0x%x, fd: "EV_SOCK_FMT", " |
371 | 0 | "flags: 0x%x)", |
372 | 0 | __func__, ev, ev->ev_events, |
373 | 0 | EV_SOCK_ARG(ev->ev_fd), ev->ev_flags); |
374 | 0 | } |
375 | 0 | EVLOCK_UNLOCK(event_debug_map_lock_, 0); |
376 | 0 | } |
377 | | #else |
378 | | static void event_debug_note_setup_(const struct event *ev) { (void)ev; } |
379 | | static void event_debug_note_teardown_(const struct event *ev) { (void)ev; } |
380 | | static void event_debug_note_add_(const struct event *ev) { (void)ev; } |
381 | | static void event_debug_note_del_(const struct event *ev) { (void)ev; } |
382 | | static void event_debug_assert_is_setup_(const struct event *ev) { (void)ev; } |
383 | | static void event_debug_assert_not_added_(const struct event *ev) { (void)ev; } |
384 | | #endif |
385 | | |
386 | | #define EVENT_BASE_ASSERT_LOCKED(base) \ |
387 | 0 | EVLOCK_ASSERT_LOCKED((base)->th_base_lock) |
388 | | |
389 | | /* How often (in seconds) do we check for changes in wall clock time relative |
390 | | * to monotonic time? Set this to -1 for 'never.' */ |
391 | 0 | #define CLOCK_SYNC_INTERVAL 5 |
392 | | |
393 | | /** Set 'tp' to the current time according to 'base'. We must hold the lock |
394 | | * on 'base'. If there is a cached time, return it. Otherwise, use |
395 | | * clock_gettime or gettimeofday as appropriate to find out the right time. |
396 | | * Return 0 on success, -1 on failure. |
397 | | */ |
398 | | static int |
399 | | gettime(struct event_base *base, struct timeval *tp) |
400 | 0 | { |
401 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
402 | 0 |
|
403 | 0 | if (base->tv_cache.tv_sec) { |
404 | 0 | *tp = base->tv_cache; |
405 | 0 | return (0); |
406 | 0 | } |
407 | 0 | |
408 | 0 | if (evutil_gettime_monotonic_(&base->monotonic_timer, tp) == -1) { |
409 | 0 | return -1; |
410 | 0 | } |
411 | 0 | |
412 | 0 | if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL |
413 | 0 | < tp->tv_sec) { |
414 | 0 | struct timeval tv; |
415 | 0 | evutil_gettimeofday(&tv,NULL); |
416 | 0 | evutil_timersub(&tv, tp, &base->tv_clock_diff); |
417 | 0 | base->last_updated_clock_diff = tp->tv_sec; |
418 | 0 | } |
419 | 0 |
|
420 | 0 | return 0; |
421 | 0 | } |
422 | | |
423 | | int |
424 | | event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv) |
425 | 0 | { |
426 | 0 | int r; |
427 | 0 | if (!base) { |
428 | 0 | base = current_base; |
429 | 0 | if (!current_base) |
430 | 0 | return evutil_gettimeofday(tv, NULL); |
431 | 0 | } |
432 | 0 | |
433 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
434 | 0 | if (base->tv_cache.tv_sec == 0) { |
435 | 0 | r = evutil_gettimeofday(tv, NULL); |
436 | 0 | } else { |
437 | 0 | evutil_timeradd(&base->tv_cache, &base->tv_clock_diff, tv); |
438 | 0 | r = 0; |
439 | 0 | } |
440 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
441 | 0 | return r; |
442 | 0 | } |
443 | | |
444 | | /** Make 'base' have no current cached time. */ |
445 | | static inline void |
446 | | clear_time_cache(struct event_base *base) |
447 | 0 | { |
448 | 0 | base->tv_cache.tv_sec = 0; |
449 | 0 | } |
450 | | |
451 | | /** Replace the cached time in 'base' with the current time. */ |
452 | | static inline void |
453 | | update_time_cache(struct event_base *base) |
454 | 0 | { |
455 | 0 | base->tv_cache.tv_sec = 0; |
456 | 0 | if (!(base->flags & EVENT_BASE_FLAG_NO_CACHE_TIME)) |
457 | 0 | gettime(base, &base->tv_cache); |
458 | 0 | } |
459 | | |
460 | | int |
461 | | event_base_update_cache_time(struct event_base *base) |
462 | 0 | { |
463 | 0 |
|
464 | 0 | if (!base) { |
465 | 0 | base = current_base; |
466 | 0 | if (!current_base) |
467 | 0 | return -1; |
468 | 0 | } |
469 | 0 | |
470 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
471 | 0 | if (base->running_loop) |
472 | 0 | update_time_cache(base); |
473 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
474 | 0 | return 0; |
475 | 0 | } |
476 | | |
477 | | static inline struct event * |
478 | | event_callback_to_event(struct event_callback *evcb) |
479 | 0 | { |
480 | 0 | EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_INIT)); |
481 | 0 | return EVUTIL_UPCAST(evcb, struct event, ev_evcallback); |
482 | 0 | } |
483 | | |
484 | | static inline struct event_callback * |
485 | | event_to_event_callback(struct event *ev) |
486 | 0 | { |
487 | 0 | return &ev->ev_evcallback; |
488 | 0 | } |
489 | | |
490 | | struct event_base * |
491 | | event_init(void) |
492 | 0 | { |
493 | 0 | struct event_base *base = event_base_new_with_config(NULL); |
494 | 0 |
|
495 | 0 | if (base == NULL) { |
496 | 0 | event_errx(1, "%s: Unable to construct event_base", __func__); |
497 | 0 | return NULL; |
498 | 0 | } |
499 | 0 | |
500 | 0 | current_base = base; |
501 | 0 |
|
502 | 0 | return (base); |
503 | 0 | } |
504 | | |
505 | | struct event_base * |
506 | | event_base_new(void) |
507 | 0 | { |
508 | 0 | struct event_base *base = NULL; |
509 | 0 | struct event_config *cfg = event_config_new(); |
510 | 0 | if (cfg) { |
511 | 0 | base = event_base_new_with_config(cfg); |
512 | 0 | event_config_free(cfg); |
513 | 0 | } |
514 | 0 | return base; |
515 | 0 | } |
516 | | |
517 | | /** Return true iff 'method' is the name of a method that 'cfg' tells us to |
518 | | * avoid. */ |
519 | | static int |
520 | | event_config_is_avoided_method(const struct event_config *cfg, |
521 | | const char *method) |
522 | 0 | { |
523 | 0 | struct event_config_entry *entry; |
524 | 0 |
|
525 | 0 | TAILQ_FOREACH(entry, &cfg->entries, next) { |
526 | 0 | if (entry->avoid_method != NULL && |
527 | 0 | strcmp(entry->avoid_method, method) == 0) |
528 | 0 | return (1); |
529 | 0 | } |
530 | 0 |
|
531 | 0 | return (0); |
532 | 0 | } |
533 | | |
534 | | /** Return true iff 'method' is disabled according to the environment. */ |
535 | | static int |
536 | | event_is_method_disabled(const char *name) |
537 | 0 | { |
538 | 0 | char environment[64]; |
539 | 0 | int i; |
540 | 0 |
|
541 | 0 | evutil_snprintf(environment, sizeof(environment), "EVENT_NO%s", name); |
542 | 0 | for (i = 8; environment[i] != '\0'; ++i) |
543 | 0 | environment[i] = EVUTIL_TOUPPER_(environment[i]); |
544 | 0 | /* Note that evutil_getenv_() ignores the environment entirely if |
545 | 0 | * we're setuid */ |
546 | 0 | return (evutil_getenv_(environment) != NULL); |
547 | 0 | } |
548 | | |
549 | | int |
550 | | event_base_get_features(const struct event_base *base) |
551 | 0 | { |
552 | 0 | return base->evsel->features; |
553 | 0 | } |
554 | | |
555 | | void |
556 | | event_enable_debug_mode(void) |
557 | 0 | { |
558 | 0 | #ifndef EVENT__DISABLE_DEBUG_MODE |
559 | 0 | if (event_debug_mode_on_) |
560 | 0 | event_errx(1, "%s was called twice!", __func__); |
561 | 0 | if (event_debug_mode_too_late) |
562 | 0 | event_errx(1, "%s must be called *before* creating any events " |
563 | 0 | "or event_bases",__func__); |
564 | 0 |
|
565 | 0 | event_debug_mode_on_ = 1; |
566 | 0 |
|
567 | 0 | HT_INIT(event_debug_map, &global_debug_map); |
568 | 0 | #endif |
569 | 0 | } |
570 | | |
571 | | void |
572 | | event_disable_debug_mode(void) |
573 | 0 | { |
574 | 0 | #ifndef EVENT__DISABLE_DEBUG_MODE |
575 | 0 | struct event_debug_entry **ent, *victim; |
576 | 0 |
|
577 | 0 | EVLOCK_LOCK(event_debug_map_lock_, 0); |
578 | 0 | for (ent = HT_START(event_debug_map, &global_debug_map); ent; ) { |
579 | 0 | victim = *ent; |
580 | 0 | ent = HT_NEXT_RMV(event_debug_map, &global_debug_map, ent); |
581 | 0 | mm_free(victim); |
582 | 0 | } |
583 | 0 | HT_CLEAR(event_debug_map, &global_debug_map); |
584 | 0 | EVLOCK_UNLOCK(event_debug_map_lock_ , 0); |
585 | 0 |
|
586 | 0 | event_debug_mode_on_ = 0; |
587 | 0 | #endif |
588 | 0 | } |
589 | | |
590 | | struct event_base * |
591 | | event_base_new_with_config(const struct event_config *cfg) |
592 | 0 | { |
593 | 0 | int i; |
594 | 0 | struct event_base *base; |
595 | 0 | int should_check_environment; |
596 | 0 |
|
597 | 0 | #ifndef EVENT__DISABLE_DEBUG_MODE |
598 | 0 | event_debug_mode_too_late = 1; |
599 | 0 | #endif |
600 | 0 |
|
601 | 0 | if ((base = mm_calloc(1, sizeof(struct event_base))) == NULL) { |
602 | 0 | event_warn("%s: calloc", __func__); |
603 | 0 | return NULL; |
604 | 0 | } |
605 | 0 |
|
606 | 0 | if (cfg) |
607 | 0 | base->flags = cfg->flags; |
608 | 0 |
|
609 | 0 | should_check_environment = |
610 | 0 | !(cfg && (cfg->flags & EVENT_BASE_FLAG_IGNORE_ENV)); |
611 | 0 |
|
612 | 0 | { |
613 | 0 | struct timeval tmp; |
614 | 0 | int precise_time = |
615 | 0 | cfg && (cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER); |
616 | 0 | int flags; |
617 | 0 | if (should_check_environment && !precise_time) { |
618 | 0 | precise_time = evutil_getenv_("EVENT_PRECISE_TIMER") != NULL; |
619 | 0 | if (precise_time) { |
620 | 0 | base->flags |= EVENT_BASE_FLAG_PRECISE_TIMER; |
621 | 0 | } |
622 | 0 | } |
623 | 0 | flags = precise_time ? EV_MONOT_PRECISE : 0; |
624 | 0 | evutil_configure_monotonic_time_(&base->monotonic_timer, flags); |
625 | 0 |
|
626 | 0 | gettime(base, &tmp); |
627 | 0 | } |
628 | 0 |
|
629 | 0 | min_heap_ctor_(&base->timeheap); |
630 | 0 |
|
631 | 0 | base->sig.ev_signal_pair[0] = -1; |
632 | 0 | base->sig.ev_signal_pair[1] = -1; |
633 | 0 | base->th_notify_fd[0] = -1; |
634 | 0 | base->th_notify_fd[1] = -1; |
635 | 0 |
|
636 | 0 | TAILQ_INIT(&base->active_later_queue); |
637 | 0 |
|
638 | 0 | evmap_io_initmap_(&base->io); |
639 | 0 | evmap_signal_initmap_(&base->sigmap); |
640 | 0 | event_changelist_init_(&base->changelist); |
641 | 0 |
|
642 | 0 | base->evbase = NULL; |
643 | 0 |
|
644 | 0 | if (cfg) { |
645 | 0 | memcpy(&base->max_dispatch_time, |
646 | 0 | &cfg->max_dispatch_interval, sizeof(struct timeval)); |
647 | 0 | base->limit_callbacks_after_prio = |
648 | 0 | cfg->limit_callbacks_after_prio; |
649 | 0 | } else { |
650 | 0 | base->max_dispatch_time.tv_sec = -1; |
651 | 0 | base->limit_callbacks_after_prio = 1; |
652 | 0 | } |
653 | 0 | if (cfg && cfg->max_dispatch_callbacks >= 0) { |
654 | 0 | base->max_dispatch_callbacks = cfg->max_dispatch_callbacks; |
655 | 0 | } else { |
656 | 0 | base->max_dispatch_callbacks = INT_MAX; |
657 | 0 | } |
658 | 0 | if (base->max_dispatch_callbacks == INT_MAX && |
659 | 0 | base->max_dispatch_time.tv_sec == -1) |
660 | 0 | base->limit_callbacks_after_prio = INT_MAX; |
661 | 0 |
|
662 | 0 | for (i = 0; eventops[i] && !base->evbase; i++) { |
663 | 0 | if (cfg != NULL) { |
664 | 0 | /* determine if this backend should be avoided */ |
665 | 0 | if (event_config_is_avoided_method(cfg, |
666 | 0 | eventops[i]->name)) |
667 | 0 | continue; |
668 | 0 | if ((eventops[i]->features & cfg->require_features) |
669 | 0 | != cfg->require_features) |
670 | 0 | continue; |
671 | 0 | } |
672 | 0 | |
673 | 0 | /* also obey the environment variables */ |
674 | 0 | if (should_check_environment && |
675 | 0 | event_is_method_disabled(eventops[i]->name)) |
676 | 0 | continue; |
677 | 0 | |
678 | 0 | base->evsel = eventops[i]; |
679 | 0 |
|
680 | 0 | base->evbase = base->evsel->init(base); |
681 | 0 | } |
682 | 0 |
|
683 | 0 | if (base->evbase == NULL) { |
684 | 0 | event_warnx("%s: no event mechanism available", |
685 | 0 | __func__); |
686 | 0 | base->evsel = NULL; |
687 | 0 | event_base_free(base); |
688 | 0 | return NULL; |
689 | 0 | } |
690 | 0 |
|
691 | 0 | if (evutil_getenv_("EVENT_SHOW_METHOD")) |
692 | 0 | event_msgx("libevent using: %s", base->evsel->name); |
693 | 0 |
|
694 | 0 | /* allocate a single active event queue */ |
695 | 0 | if (event_base_priority_init(base, 1) < 0) { |
696 | 0 | event_base_free(base); |
697 | 0 | return NULL; |
698 | 0 | } |
699 | 0 | |
700 | 0 | /* prepare for threading */ |
701 | 0 | |
702 | 0 | #if !defined(EVENT__DISABLE_THREAD_SUPPORT) && !defined(EVENT__DISABLE_DEBUG_MODE) |
703 | 0 | event_debug_created_threadable_ctx_ = 1; |
704 | 0 | #endif |
705 | 0 |
|
706 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
707 | 0 | if (EVTHREAD_LOCKING_ENABLED() && |
708 | 0 | (!cfg || !(cfg->flags & EVENT_BASE_FLAG_NOLOCK))) { |
709 | 0 | int r; |
710 | 0 | EVTHREAD_ALLOC_LOCK(base->th_base_lock, 0); |
711 | 0 | EVTHREAD_ALLOC_COND(base->current_event_cond); |
712 | 0 | r = evthread_make_base_notifiable(base); |
713 | 0 | if (r<0) { |
714 | 0 | event_warnx("%s: Unable to make base notifiable.", __func__); |
715 | 0 | event_base_free(base); |
716 | 0 | return NULL; |
717 | 0 | } |
718 | 0 | } |
719 | 0 | #endif |
720 | 0 |
|
721 | | #ifdef _WIN32 |
722 | | if (cfg && (cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP)) |
723 | | event_base_start_iocp_(base, cfg->n_cpus_hint); |
724 | | #endif |
725 | | |
726 | 0 | return (base); |
727 | 0 | } |
728 | | |
729 | | int |
730 | | event_base_start_iocp_(struct event_base *base, int n_cpus) |
731 | 0 | { |
732 | | #ifdef _WIN32 |
733 | | if (base->iocp) |
734 | | return 0; |
735 | | base->iocp = event_iocp_port_launch_(n_cpus); |
736 | | if (!base->iocp) { |
737 | | event_warnx("%s: Couldn't launch IOCP", __func__); |
738 | | return -1; |
739 | | } |
740 | | return 0; |
741 | | #else |
742 | | return -1; |
743 | 0 | #endif |
744 | 0 | } |
745 | | |
746 | | void |
747 | | event_base_stop_iocp_(struct event_base *base) |
748 | 0 | { |
749 | | #ifdef _WIN32 |
750 | | int rv; |
751 | | |
752 | | if (!base->iocp) |
753 | | return; |
754 | | rv = event_iocp_shutdown_(base->iocp, -1); |
755 | | EVUTIL_ASSERT(rv >= 0); |
756 | | base->iocp = NULL; |
757 | | #endif |
758 | | } |
759 | | |
760 | | static int |
761 | | event_base_cancel_single_callback_(struct event_base *base, |
762 | | struct event_callback *evcb, |
763 | | int run_finalizers) |
764 | 0 | { |
765 | 0 | int result = 0; |
766 | 0 |
|
767 | 0 | if (evcb->evcb_flags & EVLIST_INIT) { |
768 | 0 | struct event *ev = event_callback_to_event(evcb); |
769 | 0 | if (!(ev->ev_flags & EVLIST_INTERNAL)) { |
770 | 0 | event_del_(ev, EVENT_DEL_EVEN_IF_FINALIZING); |
771 | 0 | result = 1; |
772 | 0 | } |
773 | 0 | } else { |
774 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
775 | 0 | event_callback_cancel_nolock_(base, evcb, 1); |
776 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
777 | 0 | result = 1; |
778 | 0 | } |
779 | 0 |
|
780 | 0 | if (run_finalizers && (evcb->evcb_flags & EVLIST_FINALIZING)) { |
781 | 0 | switch (evcb->evcb_closure) { |
782 | 0 | case EV_CLOSURE_EVENT_FINALIZE: |
783 | 0 | case EV_CLOSURE_EVENT_FINALIZE_FREE: { |
784 | 0 | struct event *ev = event_callback_to_event(evcb); |
785 | 0 | ev->ev_evcallback.evcb_cb_union.evcb_evfinalize(ev, ev->ev_arg); |
786 | 0 | if (evcb->evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE) |
787 | 0 | mm_free(ev); |
788 | 0 | break; |
789 | 0 | } |
790 | 0 | case EV_CLOSURE_CB_FINALIZE: |
791 | 0 | evcb->evcb_cb_union.evcb_cbfinalize(evcb, evcb->evcb_arg); |
792 | 0 | break; |
793 | 0 | default: |
794 | 0 | break; |
795 | 0 | } |
796 | 0 | } |
797 | 0 | return result; |
798 | 0 | } |
799 | | |
800 | | static int event_base_free_queues_(struct event_base *base, int run_finalizers) |
801 | 0 | { |
802 | 0 | int deleted = 0, i; |
803 | 0 |
|
804 | 0 | for (i = 0; i < base->nactivequeues; ++i) { |
805 | 0 | struct event_callback *evcb, *next; |
806 | 0 | for (evcb = TAILQ_FIRST(&base->activequeues[i]); evcb; ) { |
807 | 0 | next = TAILQ_NEXT(evcb, evcb_active_next); |
808 | 0 | deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers); |
809 | 0 | evcb = next; |
810 | 0 | } |
811 | 0 | } |
812 | 0 |
|
813 | 0 | { |
814 | 0 | struct event_callback *evcb; |
815 | 0 | while ((evcb = TAILQ_FIRST(&base->active_later_queue))) { |
816 | 0 | deleted += event_base_cancel_single_callback_(base, evcb, run_finalizers); |
817 | 0 | } |
818 | 0 | } |
819 | 0 |
|
820 | 0 | return deleted; |
821 | 0 | } |
822 | | |
823 | | static void |
824 | | event_base_free_(struct event_base *base, int run_finalizers) |
825 | 0 | { |
826 | 0 | int i, n_deleted=0; |
827 | 0 | struct event *ev; |
828 | 0 | /* XXXX grab the lock? If there is contention when one thread frees |
829 | 0 | * the base, then the contending thread will be very sad soon. */ |
830 | 0 |
|
831 | 0 | /* event_base_free(NULL) is how to free the current_base if we |
832 | 0 | * made it with event_init and forgot to hold a reference to it. */ |
833 | 0 | if (base == NULL && current_base) |
834 | 0 | base = current_base; |
835 | 0 | /* Don't actually free NULL. */ |
836 | 0 | if (base == NULL) { |
837 | 0 | event_warnx("%s: no base to free", __func__); |
838 | 0 | return; |
839 | 0 | } |
840 | 0 | /* XXX(niels) - check for internal events first */ |
841 | 0 |
|
842 | | #ifdef _WIN32 |
843 | | event_base_stop_iocp_(base); |
844 | | #endif |
845 | | |
846 | 0 | /* threading fds if we have them */ |
847 | 0 | if (base->th_notify_fd[0] != -1) { |
848 | 0 | event_del(&base->th_notify); |
849 | 0 | EVUTIL_CLOSESOCKET(base->th_notify_fd[0]); |
850 | 0 | if (base->th_notify_fd[1] != -1) |
851 | 0 | EVUTIL_CLOSESOCKET(base->th_notify_fd[1]); |
852 | 0 | base->th_notify_fd[0] = -1; |
853 | 0 | base->th_notify_fd[1] = -1; |
854 | 0 | event_debug_unassign(&base->th_notify); |
855 | 0 | } |
856 | 0 |
|
857 | 0 | /* Delete all non-internal events. */ |
858 | 0 | evmap_delete_all_(base); |
859 | 0 |
|
860 | 0 | while ((ev = min_heap_top_(&base->timeheap)) != NULL) { |
861 | 0 | event_del(ev); |
862 | 0 | ++n_deleted; |
863 | 0 | } |
864 | 0 | for (i = 0; i < base->n_common_timeouts; ++i) { |
865 | 0 | struct common_timeout_list *ctl = |
866 | 0 | base->common_timeout_queues[i]; |
867 | 0 | event_del(&ctl->timeout_event); /* Internal; doesn't count */ |
868 | 0 | event_debug_unassign(&ctl->timeout_event); |
869 | 0 | for (ev = TAILQ_FIRST(&ctl->events); ev; ) { |
870 | 0 | struct event *next = TAILQ_NEXT(ev, |
871 | 0 | ev_timeout_pos.ev_next_with_common_timeout); |
872 | 0 | if (!(ev->ev_flags & EVLIST_INTERNAL)) { |
873 | 0 | event_del(ev); |
874 | 0 | ++n_deleted; |
875 | 0 | } |
876 | 0 | ev = next; |
877 | 0 | } |
878 | 0 | mm_free(ctl); |
879 | 0 | } |
880 | 0 | if (base->common_timeout_queues) |
881 | 0 | mm_free(base->common_timeout_queues); |
882 | 0 |
|
883 | 0 | for (;;) { |
884 | 0 | /* For finalizers we can register yet another finalizer out from |
885 | 0 | * finalizer, and iff finalizer will be in active_later_queue we can |
886 | 0 | * add finalizer to activequeues, and we will have events in |
887 | 0 | * activequeues after this function returns, which is not what we want |
888 | 0 | * (we even have an assertion for this). |
889 | 0 | * |
890 | 0 | * A simple case is bufferevent with underlying (i.e. filters). |
891 | 0 | */ |
892 | 0 | int i = event_base_free_queues_(base, run_finalizers); |
893 | 0 | if (!i) { |
894 | 0 | break; |
895 | 0 | } |
896 | 0 | n_deleted += i; |
897 | 0 | } |
898 | 0 |
|
899 | 0 | if (n_deleted) |
900 | 0 | event_debug(("%s: %d events were still set in base", |
901 | 0 | __func__, n_deleted)); |
902 | 0 |
|
903 | 0 | while (LIST_FIRST(&base->once_events)) { |
904 | 0 | struct event_once *eonce = LIST_FIRST(&base->once_events); |
905 | 0 | LIST_REMOVE(eonce, next_once); |
906 | 0 | mm_free(eonce); |
907 | 0 | } |
908 | 0 |
|
909 | 0 | if (base->evsel != NULL && base->evsel->dealloc != NULL) |
910 | 0 | base->evsel->dealloc(base); |
911 | 0 |
|
912 | 0 | for (i = 0; i < base->nactivequeues; ++i) |
913 | 0 | EVUTIL_ASSERT(TAILQ_EMPTY(&base->activequeues[i])); |
914 | 0 |
|
915 | 0 | EVUTIL_ASSERT(min_heap_empty_(&base->timeheap)); |
916 | 0 | min_heap_dtor_(&base->timeheap); |
917 | 0 |
|
918 | 0 | mm_free(base->activequeues); |
919 | 0 |
|
920 | 0 | evmap_io_clear_(&base->io); |
921 | 0 | evmap_signal_clear_(&base->sigmap); |
922 | 0 | event_changelist_freemem_(&base->changelist); |
923 | 0 |
|
924 | 0 | EVTHREAD_FREE_LOCK(base->th_base_lock, 0); |
925 | 0 | EVTHREAD_FREE_COND(base->current_event_cond); |
926 | 0 |
|
927 | 0 | /* If we're freeing current_base, there won't be a current_base. */ |
928 | 0 | if (base == current_base) |
929 | 0 | current_base = NULL; |
930 | 0 | mm_free(base); |
931 | 0 | } |
932 | | |
933 | | void |
934 | | event_base_free_nofinalize(struct event_base *base) |
935 | 0 | { |
936 | 0 | event_base_free_(base, 0); |
937 | 0 | } |
938 | | |
939 | | void |
940 | | event_base_free(struct event_base *base) |
941 | 0 | { |
942 | 0 | event_base_free_(base, 1); |
943 | 0 | } |
944 | | |
945 | | /* Fake eventop; used to disable the backend temporarily inside event_reinit |
946 | | * so that we can call event_del() on an event without telling the backend. |
947 | | */ |
948 | | static int |
949 | | nil_backend_del(struct event_base *b, evutil_socket_t fd, short old, |
950 | | short events, void *fdinfo) |
951 | 0 | { |
952 | 0 | return 0; |
953 | 0 | } |
954 | | const struct eventop nil_eventop = { |
955 | | "nil", |
956 | | NULL, /* init: unused. */ |
957 | | NULL, /* add: unused. */ |
958 | | nil_backend_del, /* del: used, so needs to be killed. */ |
959 | | NULL, /* dispatch: unused. */ |
960 | | NULL, /* dealloc: unused. */ |
961 | | 0, 0, 0 |
962 | | }; |
963 | | |
964 | | /* reinitialize the event base after a fork */ |
965 | | int |
966 | | event_reinit(struct event_base *base) |
967 | 0 | { |
968 | 0 | const struct eventop *evsel; |
969 | 0 | int res = 0; |
970 | 0 | int was_notifiable = 0; |
971 | 0 | int had_signal_added = 0; |
972 | 0 |
|
973 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
974 | 0 |
|
975 | 0 | evsel = base->evsel; |
976 | 0 |
|
977 | 0 | /* check if this event mechanism requires reinit on the backend */ |
978 | 0 | if (evsel->need_reinit) { |
979 | 0 | /* We're going to call event_del() on our notify events (the |
980 | 0 | * ones that tell about signals and wakeup events). But we |
981 | 0 | * don't actually want to tell the backend to change its |
982 | 0 | * state, since it might still share some resource (a kqueue, |
983 | 0 | * an epoll fd) with the parent process, and we don't want to |
984 | 0 | * delete the fds from _that_ backend, we temporarily stub out |
985 | 0 | * the evsel with a replacement. |
986 | 0 | */ |
987 | 0 | base->evsel = &nil_eventop; |
988 | 0 | } |
989 | 0 |
|
990 | 0 | /* We need to re-create a new signal-notification fd and a new |
991 | 0 | * thread-notification fd. Otherwise, we'll still share those with |
992 | 0 | * the parent process, which would make any notification sent to them |
993 | 0 | * get received by one or both of the event loops, more or less at |
994 | 0 | * random. |
995 | 0 | */ |
996 | 0 | if (base->sig.ev_signal_added) { |
997 | 0 | event_del_nolock_(&base->sig.ev_signal, EVENT_DEL_AUTOBLOCK); |
998 | 0 | event_debug_unassign(&base->sig.ev_signal); |
999 | 0 | memset(&base->sig.ev_signal, 0, sizeof(base->sig.ev_signal)); |
1000 | 0 | had_signal_added = 1; |
1001 | 0 | base->sig.ev_signal_added = 0; |
1002 | 0 | } |
1003 | 0 | if (base->sig.ev_signal_pair[0] != -1) |
1004 | 0 | EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[0]); |
1005 | 0 | if (base->sig.ev_signal_pair[1] != -1) |
1006 | 0 | EVUTIL_CLOSESOCKET(base->sig.ev_signal_pair[1]); |
1007 | 0 | if (base->th_notify_fn != NULL) { |
1008 | 0 | was_notifiable = 1; |
1009 | 0 | base->th_notify_fn = NULL; |
1010 | 0 | } |
1011 | 0 | if (base->th_notify_fd[0] != -1) { |
1012 | 0 | event_del_nolock_(&base->th_notify, EVENT_DEL_AUTOBLOCK); |
1013 | 0 | EVUTIL_CLOSESOCKET(base->th_notify_fd[0]); |
1014 | 0 | if (base->th_notify_fd[1] != -1) |
1015 | 0 | EVUTIL_CLOSESOCKET(base->th_notify_fd[1]); |
1016 | 0 | base->th_notify_fd[0] = -1; |
1017 | 0 | base->th_notify_fd[1] = -1; |
1018 | 0 | event_debug_unassign(&base->th_notify); |
1019 | 0 | } |
1020 | 0 |
|
1021 | 0 | /* Replace the original evsel. */ |
1022 | 0 | base->evsel = evsel; |
1023 | 0 |
|
1024 | 0 | if (evsel->need_reinit) { |
1025 | 0 | /* Reconstruct the backend through brute-force, so that we do |
1026 | 0 | * not share any structures with the parent process. For some |
1027 | 0 | * backends, this is necessary: epoll and kqueue, for |
1028 | 0 | * instance, have events associated with a kernel |
1029 | 0 | * structure. If didn't reinitialize, we'd share that |
1030 | 0 | * structure with the parent process, and any changes made by |
1031 | 0 | * the parent would affect our backend's behavior (and vice |
1032 | 0 | * versa). |
1033 | 0 | */ |
1034 | 0 | if (base->evsel->dealloc != NULL) |
1035 | 0 | base->evsel->dealloc(base); |
1036 | 0 | base->evbase = evsel->init(base); |
1037 | 0 | if (base->evbase == NULL) { |
1038 | 0 | event_errx(1, |
1039 | 0 | "%s: could not reinitialize event mechanism", |
1040 | 0 | __func__); |
1041 | 0 | res = -1; |
1042 | 0 | goto done; |
1043 | 0 | } |
1044 | 0 | |
1045 | 0 | /* Empty out the changelist (if any): we are starting from a |
1046 | 0 | * blank slate. */ |
1047 | 0 | event_changelist_freemem_(&base->changelist); |
1048 | 0 |
|
1049 | 0 | /* Tell the event maps to re-inform the backend about all |
1050 | 0 | * pending events. This will make the signal notification |
1051 | 0 | * event get re-created if necessary. */ |
1052 | 0 | if (evmap_reinit_(base) < 0) |
1053 | 0 | res = -1; |
1054 | 0 | } else { |
1055 | 0 | res = evsig_init_(base); |
1056 | 0 | if (res == 0 && had_signal_added) { |
1057 | 0 | res = event_add_nolock_(&base->sig.ev_signal, NULL, 0); |
1058 | 0 | if (res == 0) |
1059 | 0 | base->sig.ev_signal_added = 1; |
1060 | 0 | } |
1061 | 0 | } |
1062 | 0 |
|
1063 | 0 | /* If we were notifiable before, and nothing just exploded, become |
1064 | 0 | * notifiable again. */ |
1065 | 0 | if (was_notifiable && res == 0) |
1066 | 0 | res = evthread_make_base_notifiable_nolock_(base); |
1067 | 0 |
|
1068 | 0 | done: |
1069 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1070 | 0 | return (res); |
1071 | 0 | } |
1072 | | |
1073 | | /* Get the monotonic time for this event_base' timer */ |
1074 | | int |
1075 | | event_gettime_monotonic(struct event_base *base, struct timeval *tv) |
1076 | 0 | { |
1077 | 0 | int rv = -1; |
1078 | 0 |
|
1079 | 0 | if (base && tv) { |
1080 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1081 | 0 | rv = evutil_gettime_monotonic_(&(base->monotonic_timer), tv); |
1082 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1083 | 0 | } |
1084 | 0 |
|
1085 | 0 | return rv; |
1086 | 0 | } |
1087 | | |
1088 | | const char ** |
1089 | | event_get_supported_methods(void) |
1090 | 0 | { |
1091 | 0 | static const char **methods = NULL; |
1092 | 0 | const struct eventop **method; |
1093 | 0 | const char **tmp; |
1094 | 0 | int i = 0, k; |
1095 | 0 |
|
1096 | 0 | /* count all methods */ |
1097 | 0 | for (method = &eventops[0]; *method != NULL; ++method) { |
1098 | 0 | ++i; |
1099 | 0 | } |
1100 | 0 |
|
1101 | 0 | /* allocate one more than we need for the NULL pointer */ |
1102 | 0 | tmp = mm_calloc((i + 1), sizeof(char *)); |
1103 | 0 | if (tmp == NULL) |
1104 | 0 | return (NULL); |
1105 | 0 | |
1106 | 0 | /* populate the array with the supported methods */ |
1107 | 0 | for (k = 0, i = 0; eventops[k] != NULL; ++k) { |
1108 | 0 | tmp[i++] = eventops[k]->name; |
1109 | 0 | } |
1110 | 0 | tmp[i] = NULL; |
1111 | 0 |
|
1112 | 0 | if (methods != NULL) |
1113 | 0 | mm_free((char**)methods); |
1114 | 0 |
|
1115 | 0 | methods = tmp; |
1116 | 0 |
|
1117 | 0 | return (methods); |
1118 | 0 | } |
1119 | | |
1120 | | struct event_config * |
1121 | | event_config_new(void) |
1122 | 0 | { |
1123 | 0 | struct event_config *cfg = mm_calloc(1, sizeof(*cfg)); |
1124 | 0 |
|
1125 | 0 | if (cfg == NULL) |
1126 | 0 | return (NULL); |
1127 | 0 | |
1128 | 0 | TAILQ_INIT(&cfg->entries); |
1129 | 0 | cfg->max_dispatch_interval.tv_sec = -1; |
1130 | 0 | cfg->max_dispatch_callbacks = INT_MAX; |
1131 | 0 | cfg->limit_callbacks_after_prio = 1; |
1132 | 0 |
|
1133 | 0 | return (cfg); |
1134 | 0 | } |
1135 | | |
1136 | | static void |
1137 | | event_config_entry_free(struct event_config_entry *entry) |
1138 | 0 | { |
1139 | 0 | if (entry->avoid_method != NULL) |
1140 | 0 | mm_free((char *)entry->avoid_method); |
1141 | 0 | mm_free(entry); |
1142 | 0 | } |
1143 | | |
1144 | | void |
1145 | | event_config_free(struct event_config *cfg) |
1146 | 0 | { |
1147 | 0 | struct event_config_entry *entry; |
1148 | 0 |
|
1149 | 0 | while ((entry = TAILQ_FIRST(&cfg->entries)) != NULL) { |
1150 | 0 | TAILQ_REMOVE(&cfg->entries, entry, next); |
1151 | 0 | event_config_entry_free(entry); |
1152 | 0 | } |
1153 | 0 | mm_free(cfg); |
1154 | 0 | } |
1155 | | |
1156 | | int |
1157 | | event_config_set_flag(struct event_config *cfg, int flag) |
1158 | 0 | { |
1159 | 0 | if (!cfg) |
1160 | 0 | return -1; |
1161 | 0 | cfg->flags |= flag; |
1162 | 0 | return 0; |
1163 | 0 | } |
1164 | | |
1165 | | int |
1166 | | event_config_avoid_method(struct event_config *cfg, const char *method) |
1167 | 0 | { |
1168 | 0 | struct event_config_entry *entry = mm_malloc(sizeof(*entry)); |
1169 | 0 | if (entry == NULL) |
1170 | 0 | return (-1); |
1171 | 0 | |
1172 | 0 | if ((entry->avoid_method = mm_strdup(method)) == NULL) { |
1173 | 0 | mm_free(entry); |
1174 | 0 | return (-1); |
1175 | 0 | } |
1176 | 0 |
|
1177 | 0 | TAILQ_INSERT_TAIL(&cfg->entries, entry, next); |
1178 | 0 |
|
1179 | 0 | return (0); |
1180 | 0 | } |
1181 | | |
1182 | | int |
1183 | | event_config_require_features(struct event_config *cfg, |
1184 | | int features) |
1185 | 0 | { |
1186 | 0 | if (!cfg) |
1187 | 0 | return (-1); |
1188 | 0 | cfg->require_features = features; |
1189 | 0 | return (0); |
1190 | 0 | } |
1191 | | |
1192 | | int |
1193 | | event_config_set_num_cpus_hint(struct event_config *cfg, int cpus) |
1194 | 0 | { |
1195 | 0 | if (!cfg) |
1196 | 0 | return (-1); |
1197 | 0 | cfg->n_cpus_hint = cpus; |
1198 | 0 | return (0); |
1199 | 0 | } |
1200 | | |
1201 | | int |
1202 | | event_config_set_max_dispatch_interval(struct event_config *cfg, |
1203 | | const struct timeval *max_interval, int max_callbacks, int min_priority) |
1204 | 0 | { |
1205 | 0 | if (max_interval) |
1206 | 0 | memcpy(&cfg->max_dispatch_interval, max_interval, |
1207 | 0 | sizeof(struct timeval)); |
1208 | 0 | else |
1209 | 0 | cfg->max_dispatch_interval.tv_sec = -1; |
1210 | 0 | cfg->max_dispatch_callbacks = |
1211 | 0 | max_callbacks >= 0 ? max_callbacks : INT_MAX; |
1212 | 0 | if (min_priority < 0) |
1213 | 0 | min_priority = 0; |
1214 | 0 | cfg->limit_callbacks_after_prio = min_priority; |
1215 | 0 | return (0); |
1216 | 0 | } |
1217 | | |
1218 | | int |
1219 | | event_priority_init(int npriorities) |
1220 | 0 | { |
1221 | 0 | return event_base_priority_init(current_base, npriorities); |
1222 | 0 | } |
1223 | | |
1224 | | int |
1225 | | event_base_priority_init(struct event_base *base, int npriorities) |
1226 | 0 | { |
1227 | 0 | int i, r; |
1228 | 0 | r = -1; |
1229 | 0 |
|
1230 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1231 | 0 |
|
1232 | 0 | if (N_ACTIVE_CALLBACKS(base) || npriorities < 1 |
1233 | 0 | || npriorities >= EVENT_MAX_PRIORITIES) |
1234 | 0 | goto err; |
1235 | 0 | |
1236 | 0 | if (npriorities == base->nactivequeues) |
1237 | 0 | goto ok; |
1238 | 0 | |
1239 | 0 | if (base->nactivequeues) { |
1240 | 0 | mm_free(base->activequeues); |
1241 | 0 | base->nactivequeues = 0; |
1242 | 0 | } |
1243 | 0 |
|
1244 | 0 | /* Allocate our priority queues */ |
1245 | 0 | base->activequeues = (struct evcallback_list *) |
1246 | 0 | mm_calloc(npriorities, sizeof(struct evcallback_list)); |
1247 | 0 | if (base->activequeues == NULL) { |
1248 | 0 | event_warn("%s: calloc", __func__); |
1249 | 0 | goto err; |
1250 | 0 | } |
1251 | 0 | base->nactivequeues = npriorities; |
1252 | 0 |
|
1253 | 0 | for (i = 0; i < base->nactivequeues; ++i) { |
1254 | 0 | TAILQ_INIT(&base->activequeues[i]); |
1255 | 0 | } |
1256 | 0 |
|
1257 | 0 | ok: |
1258 | 0 | r = 0; |
1259 | 0 | err: |
1260 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1261 | 0 | return (r); |
1262 | 0 | } |
1263 | | |
1264 | | int |
1265 | | event_base_get_npriorities(struct event_base *base) |
1266 | 0 | { |
1267 | 0 |
|
1268 | 0 | int n; |
1269 | 0 | if (base == NULL) |
1270 | 0 | base = current_base; |
1271 | 0 |
|
1272 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1273 | 0 | n = base->nactivequeues; |
1274 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1275 | 0 | return (n); |
1276 | 0 | } |
1277 | | |
1278 | | int |
1279 | | event_base_get_num_events(struct event_base *base, unsigned int type) |
1280 | 0 | { |
1281 | 0 | int r = 0; |
1282 | 0 |
|
1283 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1284 | 0 |
|
1285 | 0 | if (type & EVENT_BASE_COUNT_ACTIVE) |
1286 | 0 | r += base->event_count_active; |
1287 | 0 |
|
1288 | 0 | if (type & EVENT_BASE_COUNT_VIRTUAL) |
1289 | 0 | r += base->virtual_event_count; |
1290 | 0 |
|
1291 | 0 | if (type & EVENT_BASE_COUNT_ADDED) |
1292 | 0 | r += base->event_count; |
1293 | 0 |
|
1294 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1295 | 0 |
|
1296 | 0 | return r; |
1297 | 0 | } |
1298 | | |
1299 | | int |
1300 | | event_base_get_max_events(struct event_base *base, unsigned int type, int clear) |
1301 | 0 | { |
1302 | 0 | int r = 0; |
1303 | 0 |
|
1304 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1305 | 0 |
|
1306 | 0 | if (type & EVENT_BASE_COUNT_ACTIVE) { |
1307 | 0 | r += base->event_count_active_max; |
1308 | 0 | if (clear) |
1309 | 0 | base->event_count_active_max = 0; |
1310 | 0 | } |
1311 | 0 |
|
1312 | 0 | if (type & EVENT_BASE_COUNT_VIRTUAL) { |
1313 | 0 | r += base->virtual_event_count_max; |
1314 | 0 | if (clear) |
1315 | 0 | base->virtual_event_count_max = 0; |
1316 | 0 | } |
1317 | 0 |
|
1318 | 0 | if (type & EVENT_BASE_COUNT_ADDED) { |
1319 | 0 | r += base->event_count_max; |
1320 | 0 | if (clear) |
1321 | 0 | base->event_count_max = 0; |
1322 | 0 | } |
1323 | 0 |
|
1324 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1325 | 0 |
|
1326 | 0 | return r; |
1327 | 0 | } |
1328 | | |
1329 | | /* Returns true iff we're currently watching any events. */ |
1330 | | static int |
1331 | | event_haveevents(struct event_base *base) |
1332 | 0 | { |
1333 | 0 | /* Caller must hold th_base_lock */ |
1334 | 0 | return (base->virtual_event_count > 0 || base->event_count > 0); |
1335 | 0 | } |
1336 | | |
1337 | | /* "closure" function called when processing active signal events */ |
1338 | | static inline void |
1339 | | event_signal_closure(struct event_base *base, struct event *ev) |
1340 | 0 | { |
1341 | 0 | short ncalls; |
1342 | 0 | int should_break; |
1343 | 0 |
|
1344 | 0 | /* Allows deletes to work */ |
1345 | 0 | ncalls = ev->ev_ncalls; |
1346 | 0 | if (ncalls != 0) |
1347 | 0 | ev->ev_pncalls = &ncalls; |
1348 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1349 | 0 | while (ncalls) { |
1350 | 0 | ncalls--; |
1351 | 0 | ev->ev_ncalls = ncalls; |
1352 | 0 | if (ncalls == 0) |
1353 | 0 | ev->ev_pncalls = NULL; |
1354 | 0 | (*ev->ev_callback)(ev->ev_fd, ev->ev_res, ev->ev_arg); |
1355 | 0 |
|
1356 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1357 | 0 | should_break = base->event_break; |
1358 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1359 | 0 |
|
1360 | 0 | if (should_break) { |
1361 | 0 | if (ncalls != 0) |
1362 | 0 | ev->ev_pncalls = NULL; |
1363 | 0 | return; |
1364 | 0 | } |
1365 | 0 | } |
1366 | 0 | } |
1367 | | |
1368 | | /* Common timeouts are special timeouts that are handled as queues rather than |
1369 | | * in the minheap. This is more efficient than the minheap if we happen to |
1370 | | * know that we're going to get several thousands of timeout events all with |
1371 | | * the same timeout value. |
1372 | | * |
1373 | | * Since all our timeout handling code assumes timevals can be copied, |
1374 | | * assigned, etc, we can't use "magic pointer" to encode these common |
1375 | | * timeouts. Searching through a list to see if every timeout is common could |
1376 | | * also get inefficient. Instead, we take advantage of the fact that tv_usec |
1377 | | * is 32 bits long, but only uses 20 of those bits (since it can never be over |
1378 | | * 999999.) We use the top bits to encode 4 bites of magic number, and 8 bits |
1379 | | * of index into the event_base's aray of common timeouts. |
1380 | | */ |
1381 | | |
1382 | 0 | #define MICROSECONDS_MASK COMMON_TIMEOUT_MICROSECONDS_MASK |
1383 | 0 | #define COMMON_TIMEOUT_IDX_MASK 0x0ff00000 |
1384 | 0 | #define COMMON_TIMEOUT_IDX_SHIFT 20 |
1385 | 0 | #define COMMON_TIMEOUT_MASK 0xf0000000 |
1386 | 0 | #define COMMON_TIMEOUT_MAGIC 0x50000000 |
1387 | | |
1388 | | #define COMMON_TIMEOUT_IDX(tv) \ |
1389 | 0 | (((tv)->tv_usec & COMMON_TIMEOUT_IDX_MASK)>>COMMON_TIMEOUT_IDX_SHIFT) |
1390 | | |
1391 | | /** Return true iff if 'tv' is a common timeout in 'base' */ |
1392 | | static inline int |
1393 | | is_common_timeout(const struct timeval *tv, |
1394 | | const struct event_base *base) |
1395 | 0 | { |
1396 | 0 | int idx; |
1397 | 0 | if ((tv->tv_usec & COMMON_TIMEOUT_MASK) != COMMON_TIMEOUT_MAGIC) |
1398 | 0 | return 0; |
1399 | 0 | idx = COMMON_TIMEOUT_IDX(tv); |
1400 | 0 | return idx < base->n_common_timeouts; |
1401 | 0 | } |
1402 | | |
1403 | | /* True iff tv1 and tv2 have the same common-timeout index, or if neither |
1404 | | * one is a common timeout. */ |
1405 | | static inline int |
1406 | | is_same_common_timeout(const struct timeval *tv1, const struct timeval *tv2) |
1407 | 0 | { |
1408 | 0 | return (tv1->tv_usec & ~MICROSECONDS_MASK) == |
1409 | 0 | (tv2->tv_usec & ~MICROSECONDS_MASK); |
1410 | 0 | } |
1411 | | |
1412 | | /** Requires that 'tv' is a common timeout. Return the corresponding |
1413 | | * common_timeout_list. */ |
1414 | | static inline struct common_timeout_list * |
1415 | | get_common_timeout_list(struct event_base *base, const struct timeval *tv) |
1416 | 0 | { |
1417 | 0 | return base->common_timeout_queues[COMMON_TIMEOUT_IDX(tv)]; |
1418 | 0 | } |
1419 | | |
1420 | | #if 0 |
1421 | | static inline int |
1422 | | common_timeout_ok(const struct timeval *tv, |
1423 | | struct event_base *base) |
1424 | | { |
1425 | | const struct timeval *expect = |
1426 | | &get_common_timeout_list(base, tv)->duration; |
1427 | | return tv->tv_sec == expect->tv_sec && |
1428 | | tv->tv_usec == expect->tv_usec; |
1429 | | } |
1430 | | #endif |
1431 | | |
1432 | | /* Add the timeout for the first event in given common timeout list to the |
1433 | | * event_base's minheap. */ |
1434 | | static void |
1435 | | common_timeout_schedule(struct common_timeout_list *ctl, |
1436 | | const struct timeval *now, struct event *head) |
1437 | 0 | { |
1438 | 0 | struct timeval timeout = head->ev_timeout; |
1439 | 0 | timeout.tv_usec &= MICROSECONDS_MASK; |
1440 | 0 | event_add_nolock_(&ctl->timeout_event, &timeout, 1); |
1441 | 0 | } |
1442 | | |
1443 | | /* Callback: invoked when the timeout for a common timeout queue triggers. |
1444 | | * This means that (at least) the first event in that queue should be run, |
1445 | | * and the timeout should be rescheduled if there are more events. */ |
1446 | | static void |
1447 | | common_timeout_callback(evutil_socket_t fd, short what, void *arg) |
1448 | 0 | { |
1449 | 0 | struct timeval now; |
1450 | 0 | struct common_timeout_list *ctl = arg; |
1451 | 0 | struct event_base *base = ctl->base; |
1452 | 0 | struct event *ev = NULL; |
1453 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1454 | 0 | gettime(base, &now); |
1455 | 0 | while (1) { |
1456 | 0 | ev = TAILQ_FIRST(&ctl->events); |
1457 | 0 | if (!ev || ev->ev_timeout.tv_sec > now.tv_sec || |
1458 | 0 | (ev->ev_timeout.tv_sec == now.tv_sec && |
1459 | 0 | (ev->ev_timeout.tv_usec&MICROSECONDS_MASK) > now.tv_usec)) |
1460 | 0 | break; |
1461 | 0 | event_del_nolock_(ev, EVENT_DEL_NOBLOCK); |
1462 | 0 | event_active_nolock_(ev, EV_TIMEOUT, 1); |
1463 | 0 | } |
1464 | 0 | if (ev) |
1465 | 0 | common_timeout_schedule(ctl, &now, ev); |
1466 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1467 | 0 | } |
1468 | | |
1469 | 0 | #define MAX_COMMON_TIMEOUTS 256 |
1470 | | |
1471 | | const struct timeval * |
1472 | | event_base_init_common_timeout(struct event_base *base, |
1473 | | const struct timeval *duration) |
1474 | 0 | { |
1475 | 0 | int i; |
1476 | 0 | struct timeval tv; |
1477 | 0 | const struct timeval *result=NULL; |
1478 | 0 | struct common_timeout_list *new_ctl; |
1479 | 0 |
|
1480 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1481 | 0 | if (duration->tv_usec > 1000000) { |
1482 | 0 | memcpy(&tv, duration, sizeof(struct timeval)); |
1483 | 0 | if (is_common_timeout(duration, base)) |
1484 | 0 | tv.tv_usec &= MICROSECONDS_MASK; |
1485 | 0 | tv.tv_sec += tv.tv_usec / 1000000; |
1486 | 0 | tv.tv_usec %= 1000000; |
1487 | 0 | duration = &tv; |
1488 | 0 | } |
1489 | 0 | for (i = 0; i < base->n_common_timeouts; ++i) { |
1490 | 0 | const struct common_timeout_list *ctl = |
1491 | 0 | base->common_timeout_queues[i]; |
1492 | 0 | if (duration->tv_sec == ctl->duration.tv_sec && |
1493 | 0 | duration->tv_usec == |
1494 | 0 | (ctl->duration.tv_usec & MICROSECONDS_MASK)) { |
1495 | 0 | EVUTIL_ASSERT(is_common_timeout(&ctl->duration, base)); |
1496 | 0 | result = &ctl->duration; |
1497 | 0 | goto done; |
1498 | 0 | } |
1499 | 0 | } |
1500 | 0 | if (base->n_common_timeouts == MAX_COMMON_TIMEOUTS) { |
1501 | 0 | event_warnx("%s: Too many common timeouts already in use; " |
1502 | 0 | "we only support %d per event_base", __func__, |
1503 | 0 | MAX_COMMON_TIMEOUTS); |
1504 | 0 | goto done; |
1505 | 0 | } |
1506 | 0 | if (base->n_common_timeouts_allocated == base->n_common_timeouts) { |
1507 | 0 | int n = base->n_common_timeouts < 16 ? 16 : |
1508 | 0 | base->n_common_timeouts*2; |
1509 | 0 | struct common_timeout_list **newqueues = |
1510 | 0 | mm_realloc(base->common_timeout_queues, |
1511 | 0 | n*sizeof(struct common_timeout_queue *)); |
1512 | 0 | if (!newqueues) { |
1513 | 0 | event_warn("%s: realloc",__func__); |
1514 | 0 | goto done; |
1515 | 0 | } |
1516 | 0 | base->n_common_timeouts_allocated = n; |
1517 | 0 | base->common_timeout_queues = newqueues; |
1518 | 0 | } |
1519 | 0 | new_ctl = mm_calloc(1, sizeof(struct common_timeout_list)); |
1520 | 0 | if (!new_ctl) { |
1521 | 0 | event_warn("%s: calloc",__func__); |
1522 | 0 | goto done; |
1523 | 0 | } |
1524 | 0 | TAILQ_INIT(&new_ctl->events); |
1525 | 0 | new_ctl->duration.tv_sec = duration->tv_sec; |
1526 | 0 | new_ctl->duration.tv_usec = |
1527 | 0 | duration->tv_usec | COMMON_TIMEOUT_MAGIC | |
1528 | 0 | (base->n_common_timeouts << COMMON_TIMEOUT_IDX_SHIFT); |
1529 | 0 | evtimer_assign(&new_ctl->timeout_event, base, |
1530 | 0 | common_timeout_callback, new_ctl); |
1531 | 0 | new_ctl->timeout_event.ev_flags |= EVLIST_INTERNAL; |
1532 | 0 | event_priority_set(&new_ctl->timeout_event, 0); |
1533 | 0 | new_ctl->base = base; |
1534 | 0 | base->common_timeout_queues[base->n_common_timeouts++] = new_ctl; |
1535 | 0 | result = &new_ctl->duration; |
1536 | 0 |
|
1537 | 0 | done: |
1538 | 0 | if (result) |
1539 | 0 | EVUTIL_ASSERT(is_common_timeout(result, base)); |
1540 | 0 |
|
1541 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1542 | 0 | return result; |
1543 | 0 | } |
1544 | | |
1545 | | /* Closure function invoked when we're activating a persistent event. */ |
1546 | | static inline void |
1547 | | event_persist_closure(struct event_base *base, struct event *ev) |
1548 | 0 | { |
1549 | 0 | void (*evcb_callback)(evutil_socket_t, short, void *); |
1550 | 0 |
|
1551 | 0 | // Other fields of *ev that must be stored before executing |
1552 | 0 | evutil_socket_t evcb_fd; |
1553 | 0 | short evcb_res; |
1554 | 0 | void *evcb_arg; |
1555 | 0 |
|
1556 | 0 | /* reschedule the persistent event if we have a timeout. */ |
1557 | 0 | if (ev->ev_io_timeout.tv_sec || ev->ev_io_timeout.tv_usec) { |
1558 | 0 | /* If there was a timeout, we want it to run at an interval of |
1559 | 0 | * ev_io_timeout after the last time it was _scheduled_ for, |
1560 | 0 | * not ev_io_timeout after _now_. If it fired for another |
1561 | 0 | * reason, though, the timeout ought to start ticking _now_. */ |
1562 | 0 | struct timeval run_at, relative_to, delay, now; |
1563 | 0 | ev_uint32_t usec_mask = 0; |
1564 | 0 | EVUTIL_ASSERT(is_same_common_timeout(&ev->ev_timeout, |
1565 | 0 | &ev->ev_io_timeout)); |
1566 | 0 | gettime(base, &now); |
1567 | 0 | if (is_common_timeout(&ev->ev_timeout, base)) { |
1568 | 0 | delay = ev->ev_io_timeout; |
1569 | 0 | usec_mask = delay.tv_usec & ~MICROSECONDS_MASK; |
1570 | 0 | delay.tv_usec &= MICROSECONDS_MASK; |
1571 | 0 | if (ev->ev_res & EV_TIMEOUT) { |
1572 | 0 | relative_to = ev->ev_timeout; |
1573 | 0 | relative_to.tv_usec &= MICROSECONDS_MASK; |
1574 | 0 | } else { |
1575 | 0 | relative_to = now; |
1576 | 0 | } |
1577 | 0 | } else { |
1578 | 0 | delay = ev->ev_io_timeout; |
1579 | 0 | if (ev->ev_res & EV_TIMEOUT) { |
1580 | 0 | relative_to = ev->ev_timeout; |
1581 | 0 | } else { |
1582 | 0 | relative_to = now; |
1583 | 0 | } |
1584 | 0 | } |
1585 | 0 | evutil_timeradd(&relative_to, &delay, &run_at); |
1586 | 0 | if (evutil_timercmp(&run_at, &now, <)) { |
1587 | 0 | /* Looks like we missed at least one invocation due to |
1588 | 0 | * a clock jump, not running the event loop for a |
1589 | 0 | * while, really slow callbacks, or |
1590 | 0 | * something. Reschedule relative to now. |
1591 | 0 | */ |
1592 | 0 | evutil_timeradd(&now, &delay, &run_at); |
1593 | 0 | } |
1594 | 0 | run_at.tv_usec |= usec_mask; |
1595 | 0 | event_add_nolock_(ev, &run_at, 1); |
1596 | 0 | } |
1597 | 0 |
|
1598 | 0 | // Save our callback before we release the lock |
1599 | 0 | evcb_callback = ev->ev_callback; |
1600 | 0 | evcb_fd = ev->ev_fd; |
1601 | 0 | evcb_res = ev->ev_res; |
1602 | 0 | evcb_arg = ev->ev_arg; |
1603 | 0 |
|
1604 | 0 | // Release the lock |
1605 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1606 | 0 |
|
1607 | 0 | // Execute the callback |
1608 | 0 | (evcb_callback)(evcb_fd, evcb_res, evcb_arg); |
1609 | 0 | } |
1610 | | |
1611 | | /* |
1612 | | Helper for event_process_active to process all the events in a single queue, |
1613 | | releasing the lock as we go. This function requires that the lock be held |
1614 | | when it's invoked. Returns -1 if we get a signal or an event_break that |
1615 | | means we should stop processing any active events now. Otherwise returns |
1616 | | the number of non-internal event_callbacks that we processed. |
1617 | | */ |
1618 | | static int |
1619 | | event_process_active_single_queue(struct event_base *base, |
1620 | | struct evcallback_list *activeq, |
1621 | | int max_to_process, const struct timeval *endtime) |
1622 | 0 | { |
1623 | 0 | struct event_callback *evcb; |
1624 | 0 | int count = 0; |
1625 | 0 |
|
1626 | 0 | EVUTIL_ASSERT(activeq != NULL); |
1627 | 0 |
|
1628 | 0 | for (evcb = TAILQ_FIRST(activeq); evcb; evcb = TAILQ_FIRST(activeq)) { |
1629 | 0 | struct event *ev=NULL; |
1630 | 0 | if (evcb->evcb_flags & EVLIST_INIT) { |
1631 | 0 | ev = event_callback_to_event(evcb); |
1632 | 0 |
|
1633 | 0 | if (ev->ev_events & EV_PERSIST || ev->ev_flags & EVLIST_FINALIZING) |
1634 | 0 | event_queue_remove_active(base, evcb); |
1635 | 0 | else |
1636 | 0 | event_del_nolock_(ev, EVENT_DEL_NOBLOCK); |
1637 | 0 | event_debug(( |
1638 | 0 | "event_process_active: event: %p, %s%s%scall %p", |
1639 | 0 | ev, |
1640 | 0 | ev->ev_res & EV_READ ? "EV_READ " : " ", |
1641 | 0 | ev->ev_res & EV_WRITE ? "EV_WRITE " : " ", |
1642 | 0 | ev->ev_res & EV_CLOSED ? "EV_CLOSED " : " ", |
1643 | 0 | ev->ev_callback)); |
1644 | 0 | } else { |
1645 | 0 | event_queue_remove_active(base, evcb); |
1646 | 0 | event_debug(("event_process_active: event_callback %p, " |
1647 | 0 | "closure %d, call %p", |
1648 | 0 | evcb, evcb->evcb_closure, evcb->evcb_cb_union.evcb_callback)); |
1649 | 0 | } |
1650 | 0 |
|
1651 | 0 | if (!(evcb->evcb_flags & EVLIST_INTERNAL)) |
1652 | 0 | ++count; |
1653 | 0 |
|
1654 | 0 |
|
1655 | 0 | base->current_event = evcb; |
1656 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
1657 | 0 | base->current_event_waiters = 0; |
1658 | 0 | #endif |
1659 | 0 |
|
1660 | 0 | switch (evcb->evcb_closure) { |
1661 | 0 | case EV_CLOSURE_EVENT_SIGNAL: |
1662 | 0 | EVUTIL_ASSERT(ev != NULL); |
1663 | 0 | event_signal_closure(base, ev); |
1664 | 0 | break; |
1665 | 0 | case EV_CLOSURE_EVENT_PERSIST: |
1666 | 0 | EVUTIL_ASSERT(ev != NULL); |
1667 | 0 | event_persist_closure(base, ev); |
1668 | 0 | break; |
1669 | 0 | case EV_CLOSURE_EVENT: { |
1670 | 0 | void (*evcb_callback)(evutil_socket_t, short, void *); |
1671 | 0 | short res; |
1672 | 0 | EVUTIL_ASSERT(ev != NULL); |
1673 | 0 | evcb_callback = *ev->ev_callback; |
1674 | 0 | res = ev->ev_res; |
1675 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1676 | 0 | evcb_callback(ev->ev_fd, res, ev->ev_arg); |
1677 | 0 | } |
1678 | 0 | break; |
1679 | 0 | case EV_CLOSURE_CB_SELF: { |
1680 | 0 | void (*evcb_selfcb)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_selfcb; |
1681 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1682 | 0 | evcb_selfcb(evcb, evcb->evcb_arg); |
1683 | 0 | } |
1684 | 0 | break; |
1685 | 0 | case EV_CLOSURE_EVENT_FINALIZE: |
1686 | 0 | case EV_CLOSURE_EVENT_FINALIZE_FREE: { |
1687 | 0 | void (*evcb_evfinalize)(struct event *, void *); |
1688 | 0 | int evcb_closure = evcb->evcb_closure; |
1689 | 0 | EVUTIL_ASSERT(ev != NULL); |
1690 | 0 | base->current_event = NULL; |
1691 | 0 | evcb_evfinalize = ev->ev_evcallback.evcb_cb_union.evcb_evfinalize; |
1692 | 0 | EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING)); |
1693 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1694 | 0 | evcb_evfinalize(ev, ev->ev_arg); |
1695 | 0 | event_debug_note_teardown_(ev); |
1696 | 0 | if (evcb_closure == EV_CLOSURE_EVENT_FINALIZE_FREE) |
1697 | 0 | mm_free(ev); |
1698 | 0 | } |
1699 | 0 | break; |
1700 | 0 | case EV_CLOSURE_CB_FINALIZE: { |
1701 | 0 | void (*evcb_cbfinalize)(struct event_callback *, void *) = evcb->evcb_cb_union.evcb_cbfinalize; |
1702 | 0 | base->current_event = NULL; |
1703 | 0 | EVUTIL_ASSERT((evcb->evcb_flags & EVLIST_FINALIZING)); |
1704 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1705 | 0 | evcb_cbfinalize(evcb, evcb->evcb_arg); |
1706 | 0 | } |
1707 | 0 | break; |
1708 | 0 | default: |
1709 | 0 | EVUTIL_ASSERT(0); |
1710 | 0 | } |
1711 | 0 |
|
1712 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1713 | 0 | base->current_event = NULL; |
1714 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
1715 | 0 | if (base->current_event_waiters) { |
1716 | 0 | base->current_event_waiters = 0; |
1717 | 0 | EVTHREAD_COND_BROADCAST(base->current_event_cond); |
1718 | 0 | } |
1719 | 0 | #endif |
1720 | 0 |
|
1721 | 0 | if (base->event_break) |
1722 | 0 | return -1; |
1723 | 0 | if (count >= max_to_process) |
1724 | 0 | return count; |
1725 | 0 | if (count && endtime) { |
1726 | 0 | struct timeval now; |
1727 | 0 | update_time_cache(base); |
1728 | 0 | gettime(base, &now); |
1729 | 0 | if (evutil_timercmp(&now, endtime, >=)) |
1730 | 0 | return count; |
1731 | 0 | } |
1732 | 0 | if (base->event_continue) |
1733 | 0 | break; |
1734 | 0 | } |
1735 | 0 | return count; |
1736 | 0 | } |
1737 | | |
1738 | | /* |
1739 | | * Active events are stored in priority queues. Lower priorities are always |
1740 | | * process before higher priorities. Low priority events can starve high |
1741 | | * priority ones. |
1742 | | */ |
1743 | | |
1744 | | static int |
1745 | | event_process_active(struct event_base *base) |
1746 | 0 | { |
1747 | 0 | /* Caller must hold th_base_lock */ |
1748 | 0 | struct evcallback_list *activeq = NULL; |
1749 | 0 | int i, c = 0; |
1750 | 0 | const struct timeval *endtime; |
1751 | 0 | struct timeval tv; |
1752 | 0 | const int maxcb = base->max_dispatch_callbacks; |
1753 | 0 | const int limit_after_prio = base->limit_callbacks_after_prio; |
1754 | 0 | if (base->max_dispatch_time.tv_sec >= 0) { |
1755 | 0 | update_time_cache(base); |
1756 | 0 | gettime(base, &tv); |
1757 | 0 | evutil_timeradd(&base->max_dispatch_time, &tv, &tv); |
1758 | 0 | endtime = &tv; |
1759 | 0 | } else { |
1760 | 0 | endtime = NULL; |
1761 | 0 | } |
1762 | 0 |
|
1763 | 0 | for (i = 0; i < base->nactivequeues; ++i) { |
1764 | 0 | if (TAILQ_FIRST(&base->activequeues[i]) != NULL) { |
1765 | 0 | base->event_running_priority = i; |
1766 | 0 | activeq = &base->activequeues[i]; |
1767 | 0 | if (i < limit_after_prio) |
1768 | 0 | c = event_process_active_single_queue(base, activeq, |
1769 | 0 | INT_MAX, NULL); |
1770 | 0 | else |
1771 | 0 | c = event_process_active_single_queue(base, activeq, |
1772 | 0 | maxcb, endtime); |
1773 | 0 | if (c < 0) { |
1774 | 0 | goto done; |
1775 | 0 | } else if (c > 0) |
1776 | 0 | break; /* Processed a real event; do not |
1777 | 0 | * consider lower-priority events */ |
1778 | 0 | /* If we get here, all of the events we processed |
1779 | 0 | * were internal. Continue. */ |
1780 | 0 | } |
1781 | 0 | } |
1782 | 0 |
|
1783 | 0 | done: |
1784 | 0 | base->event_running_priority = -1; |
1785 | 0 |
|
1786 | 0 | return c; |
1787 | 0 | } |
1788 | | |
1789 | | /* |
1790 | | * Wait continuously for events. We exit only if no events are left. |
1791 | | */ |
1792 | | |
1793 | | int |
1794 | | event_dispatch(void) |
1795 | 0 | { |
1796 | 0 | return (event_loop(0)); |
1797 | 0 | } |
1798 | | |
1799 | | int |
1800 | | event_base_dispatch(struct event_base *event_base) |
1801 | 0 | { |
1802 | 0 | return (event_base_loop(event_base, 0)); |
1803 | 0 | } |
1804 | | |
1805 | | const char * |
1806 | | event_base_get_method(const struct event_base *base) |
1807 | 0 | { |
1808 | 0 | EVUTIL_ASSERT(base); |
1809 | 0 | return (base->evsel->name); |
1810 | 0 | } |
1811 | | |
1812 | | /** Callback: used to implement event_base_loopexit by telling the event_base |
1813 | | * that it's time to exit its loop. */ |
1814 | | static void |
1815 | | event_loopexit_cb(evutil_socket_t fd, short what, void *arg) |
1816 | 0 | { |
1817 | 0 | struct event_base *base = arg; |
1818 | 0 | base->event_gotterm = 1; |
1819 | 0 | } |
1820 | | |
1821 | | int |
1822 | | event_loopexit(const struct timeval *tv) |
1823 | 0 | { |
1824 | 0 | return (event_once(-1, EV_TIMEOUT, event_loopexit_cb, |
1825 | 0 | current_base, tv)); |
1826 | 0 | } |
1827 | | |
1828 | | int |
1829 | | event_base_loopexit(struct event_base *event_base, const struct timeval *tv) |
1830 | 0 | { |
1831 | 0 | return (event_base_once(event_base, -1, EV_TIMEOUT, event_loopexit_cb, |
1832 | 0 | event_base, tv)); |
1833 | 0 | } |
1834 | | |
1835 | | int |
1836 | | event_loopbreak(void) |
1837 | 0 | { |
1838 | 0 | return (event_base_loopbreak(current_base)); |
1839 | 0 | } |
1840 | | |
1841 | | int |
1842 | | event_base_loopbreak(struct event_base *event_base) |
1843 | 0 | { |
1844 | 0 | int r = 0; |
1845 | 0 | if (event_base == NULL) |
1846 | 0 | return (-1); |
1847 | 0 | |
1848 | 0 | EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); |
1849 | 0 | event_base->event_break = 1; |
1850 | 0 |
|
1851 | 0 | if (EVBASE_NEED_NOTIFY(event_base)) { |
1852 | 0 | r = evthread_notify_base(event_base); |
1853 | 0 | } else { |
1854 | 0 | r = (0); |
1855 | 0 | } |
1856 | 0 | EVBASE_RELEASE_LOCK(event_base, th_base_lock); |
1857 | 0 | return r; |
1858 | 0 | } |
1859 | | |
1860 | | int |
1861 | | event_base_loopcontinue(struct event_base *event_base) |
1862 | 0 | { |
1863 | 0 | int r = 0; |
1864 | 0 | if (event_base == NULL) |
1865 | 0 | return (-1); |
1866 | 0 | |
1867 | 0 | EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); |
1868 | 0 | event_base->event_continue = 1; |
1869 | 0 |
|
1870 | 0 | if (EVBASE_NEED_NOTIFY(event_base)) { |
1871 | 0 | r = evthread_notify_base(event_base); |
1872 | 0 | } else { |
1873 | 0 | r = (0); |
1874 | 0 | } |
1875 | 0 | EVBASE_RELEASE_LOCK(event_base, th_base_lock); |
1876 | 0 | return r; |
1877 | 0 | } |
1878 | | |
1879 | | int |
1880 | | event_base_got_break(struct event_base *event_base) |
1881 | 0 | { |
1882 | 0 | int res; |
1883 | 0 | EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); |
1884 | 0 | res = event_base->event_break; |
1885 | 0 | EVBASE_RELEASE_LOCK(event_base, th_base_lock); |
1886 | 0 | return res; |
1887 | 0 | } |
1888 | | |
1889 | | int |
1890 | | event_base_got_exit(struct event_base *event_base) |
1891 | 0 | { |
1892 | 0 | int res; |
1893 | 0 | EVBASE_ACQUIRE_LOCK(event_base, th_base_lock); |
1894 | 0 | res = event_base->event_gotterm; |
1895 | 0 | EVBASE_RELEASE_LOCK(event_base, th_base_lock); |
1896 | 0 | return res; |
1897 | 0 | } |
1898 | | |
1899 | | /* not thread safe */ |
1900 | | |
1901 | | int |
1902 | | event_loop(int flags) |
1903 | 0 | { |
1904 | 0 | return event_base_loop(current_base, flags); |
1905 | 0 | } |
1906 | | |
1907 | | int |
1908 | | event_base_loop(struct event_base *base, int flags) |
1909 | 0 | { |
1910 | 0 | const struct eventop *evsel = base->evsel; |
1911 | 0 | struct timeval tv; |
1912 | 0 | struct timeval *tv_p; |
1913 | 0 | int res, done, retval = 0; |
1914 | 0 |
|
1915 | 0 | /* Grab the lock. We will release it inside evsel.dispatch, and again |
1916 | 0 | * as we invoke user callbacks. */ |
1917 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
1918 | 0 |
|
1919 | 0 | if (base->running_loop) { |
1920 | 0 | event_warnx("%s: reentrant invocation. Only one event_base_loop" |
1921 | 0 | " can run on each event_base at once.", __func__); |
1922 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
1923 | 0 | return -1; |
1924 | 0 | } |
1925 | 0 |
|
1926 | 0 | base->running_loop = 1; |
1927 | 0 |
|
1928 | 0 | clear_time_cache(base); |
1929 | 0 |
|
1930 | 0 | if (base->sig.ev_signal_added && base->sig.ev_n_signals_added) |
1931 | 0 | evsig_set_base_(base); |
1932 | 0 |
|
1933 | 0 | done = 0; |
1934 | 0 |
|
1935 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
1936 | 0 | base->th_owner_id = EVTHREAD_GET_ID(); |
1937 | 0 | #endif |
1938 | 0 |
|
1939 | 0 | base->event_gotterm = base->event_break = 0; |
1940 | 0 |
|
1941 | 0 | while (!done) { |
1942 | 0 | base->event_continue = 0; |
1943 | 0 | base->n_deferreds_queued = 0; |
1944 | 0 |
|
1945 | 0 | /* Terminate the loop if we have been asked to */ |
1946 | 0 | if (base->event_gotterm) { |
1947 | 0 | break; |
1948 | 0 | } |
1949 | 0 | |
1950 | 0 | if (base->event_break) { |
1951 | 0 | break; |
1952 | 0 | } |
1953 | 0 | |
1954 | 0 | tv_p = &tv; |
1955 | 0 | if (!N_ACTIVE_CALLBACKS(base) && !(flags & EVLOOP_NONBLOCK)) { |
1956 | 0 | timeout_next(base, &tv_p); |
1957 | 0 | } else { |
1958 | 0 | /* |
1959 | 0 | * if we have active events, we just poll new events |
1960 | 0 | * without waiting. |
1961 | 0 | */ |
1962 | 0 | evutil_timerclear(&tv); |
1963 | 0 | } |
1964 | 0 |
|
1965 | 0 | /* If we have no events, we just exit */ |
1966 | 0 | if (0==(flags&EVLOOP_NO_EXIT_ON_EMPTY) && |
1967 | 0 | !event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) { |
1968 | 0 | event_debug(("%s: no events registered.", __func__)); |
1969 | 0 | retval = 1; |
1970 | 0 | goto done; |
1971 | 0 | } |
1972 | 0 |
|
1973 | 0 | event_queue_make_later_events_active(base); |
1974 | 0 |
|
1975 | 0 | clear_time_cache(base); |
1976 | 0 |
|
1977 | 0 | res = evsel->dispatch(base, tv_p); |
1978 | 0 |
|
1979 | 0 | if (res == -1) { |
1980 | 0 | event_debug(("%s: dispatch returned unsuccessfully.", |
1981 | 0 | __func__)); |
1982 | 0 | retval = -1; |
1983 | 0 | goto done; |
1984 | 0 | } |
1985 | 0 |
|
1986 | 0 | update_time_cache(base); |
1987 | 0 |
|
1988 | 0 | timeout_process(base); |
1989 | 0 |
|
1990 | 0 | if (N_ACTIVE_CALLBACKS(base)) { |
1991 | 0 | int n = event_process_active(base); |
1992 | 0 | if ((flags & EVLOOP_ONCE) |
1993 | 0 | && N_ACTIVE_CALLBACKS(base) == 0 |
1994 | 0 | && n != 0) |
1995 | 0 | done = 1; |
1996 | 0 | } else if (flags & EVLOOP_NONBLOCK) |
1997 | 0 | done = 1; |
1998 | 0 | } |
1999 | 0 | event_debug(("%s: asked to terminate loop.", __func__)); |
2000 | 0 |
|
2001 | 0 | done: |
2002 | 0 | clear_time_cache(base); |
2003 | 0 | base->running_loop = 0; |
2004 | 0 |
|
2005 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2006 | 0 |
|
2007 | 0 | return (retval); |
2008 | 0 | } |
2009 | | |
2010 | | /* One-time callback to implement event_base_once: invokes the user callback, |
2011 | | * then deletes the allocated storage */ |
2012 | | static void |
2013 | | event_once_cb(evutil_socket_t fd, short events, void *arg) |
2014 | 0 | { |
2015 | 0 | struct event_once *eonce = arg; |
2016 | 0 |
|
2017 | 0 | (*eonce->cb)(fd, events, eonce->arg); |
2018 | 0 | EVBASE_ACQUIRE_LOCK(eonce->ev.ev_base, th_base_lock); |
2019 | 0 | LIST_REMOVE(eonce, next_once); |
2020 | 0 | EVBASE_RELEASE_LOCK(eonce->ev.ev_base, th_base_lock); |
2021 | 0 | event_debug_unassign(&eonce->ev); |
2022 | 0 | mm_free(eonce); |
2023 | 0 | } |
2024 | | |
2025 | | /* not threadsafe, event scheduled once. */ |
2026 | | int |
2027 | | event_once(evutil_socket_t fd, short events, |
2028 | | void (*callback)(evutil_socket_t, short, void *), |
2029 | | void *arg, const struct timeval *tv) |
2030 | 0 | { |
2031 | 0 | return event_base_once(current_base, fd, events, callback, arg, tv); |
2032 | 0 | } |
2033 | | |
2034 | | /* Schedules an event once */ |
2035 | | int |
2036 | | event_base_once(struct event_base *base, evutil_socket_t fd, short events, |
2037 | | void (*callback)(evutil_socket_t, short, void *), |
2038 | | void *arg, const struct timeval *tv) |
2039 | 0 | { |
2040 | 0 | struct event_once *eonce; |
2041 | 0 | int res = 0; |
2042 | 0 | int activate = 0; |
2043 | 0 |
|
2044 | 0 | /* We cannot support signals that just fire once, or persistent |
2045 | 0 | * events. */ |
2046 | 0 | if (events & (EV_SIGNAL|EV_PERSIST)) |
2047 | 0 | return (-1); |
2048 | 0 | |
2049 | 0 | if ((eonce = mm_calloc(1, sizeof(struct event_once))) == NULL) |
2050 | 0 | return (-1); |
2051 | 0 | |
2052 | 0 | eonce->cb = callback; |
2053 | 0 | eonce->arg = arg; |
2054 | 0 |
|
2055 | 0 | if ((events & (EV_TIMEOUT|EV_SIGNAL|EV_READ|EV_WRITE|EV_CLOSED)) == EV_TIMEOUT) { |
2056 | 0 | evtimer_assign(&eonce->ev, base, event_once_cb, eonce); |
2057 | 0 |
|
2058 | 0 | if (tv == NULL || ! evutil_timerisset(tv)) { |
2059 | 0 | /* If the event is going to become active immediately, |
2060 | 0 | * don't put it on the timeout queue. This is one |
2061 | 0 | * idiom for scheduling a callback, so let's make |
2062 | 0 | * it fast (and order-preserving). */ |
2063 | 0 | activate = 1; |
2064 | 0 | } |
2065 | 0 | } else if (events & (EV_READ|EV_WRITE|EV_CLOSED)) { |
2066 | 0 | events &= EV_READ|EV_WRITE|EV_CLOSED; |
2067 | 0 |
|
2068 | 0 | event_assign(&eonce->ev, base, fd, events, event_once_cb, eonce); |
2069 | 0 | } else { |
2070 | 0 | /* Bad event combination */ |
2071 | 0 | mm_free(eonce); |
2072 | 0 | return (-1); |
2073 | 0 | } |
2074 | 0 |
|
2075 | 0 | if (res == 0) { |
2076 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
2077 | 0 | if (activate) |
2078 | 0 | event_active_nolock_(&eonce->ev, EV_TIMEOUT, 1); |
2079 | 0 | else |
2080 | 0 | res = event_add_nolock_(&eonce->ev, tv, 0); |
2081 | 0 |
|
2082 | 0 | if (res != 0) { |
2083 | 0 | mm_free(eonce); |
2084 | 0 | return (res); |
2085 | 0 | } else { |
2086 | 0 | LIST_INSERT_HEAD(&base->once_events, eonce, next_once); |
2087 | 0 | } |
2088 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2089 | 0 | } |
2090 | 0 |
|
2091 | 0 | return (0); |
2092 | 0 | } |
2093 | | |
2094 | | int |
2095 | | event_assign(struct event *ev, struct event_base *base, evutil_socket_t fd, short events, void (*callback)(evutil_socket_t, short, void *), void *arg) |
2096 | 0 | { |
2097 | 0 | if (!base) |
2098 | 0 | base = current_base; |
2099 | 0 | if (arg == &event_self_cbarg_ptr_) |
2100 | 0 | arg = ev; |
2101 | 0 |
|
2102 | 0 | event_debug_assert_not_added_(ev); |
2103 | 0 |
|
2104 | 0 | ev->ev_base = base; |
2105 | 0 |
|
2106 | 0 | ev->ev_callback = callback; |
2107 | 0 | ev->ev_arg = arg; |
2108 | 0 | ev->ev_fd = fd; |
2109 | 0 | ev->ev_events = events; |
2110 | 0 | ev->ev_res = 0; |
2111 | 0 | ev->ev_flags = EVLIST_INIT; |
2112 | 0 | ev->ev_ncalls = 0; |
2113 | 0 | ev->ev_pncalls = NULL; |
2114 | 0 |
|
2115 | 0 | if (events & EV_SIGNAL) { |
2116 | 0 | if ((events & (EV_READ|EV_WRITE|EV_CLOSED)) != 0) { |
2117 | 0 | event_warnx("%s: EV_SIGNAL is not compatible with " |
2118 | 0 | "EV_READ, EV_WRITE or EV_CLOSED", __func__); |
2119 | 0 | return -1; |
2120 | 0 | } |
2121 | 0 | ev->ev_closure = EV_CLOSURE_EVENT_SIGNAL; |
2122 | 0 | } else { |
2123 | 0 | if (events & EV_PERSIST) { |
2124 | 0 | evutil_timerclear(&ev->ev_io_timeout); |
2125 | 0 | ev->ev_closure = EV_CLOSURE_EVENT_PERSIST; |
2126 | 0 | } else { |
2127 | 0 | ev->ev_closure = EV_CLOSURE_EVENT; |
2128 | 0 | } |
2129 | 0 | } |
2130 | 0 |
|
2131 | 0 | min_heap_elem_init_(ev); |
2132 | 0 |
|
2133 | 0 | if (base != NULL) { |
2134 | 0 | /* by default, we put new events into the middle priority */ |
2135 | 0 | ev->ev_pri = base->nactivequeues / 2; |
2136 | 0 | } |
2137 | 0 |
|
2138 | 0 | event_debug_note_setup_(ev); |
2139 | 0 |
|
2140 | 0 | return 0; |
2141 | 0 | } |
2142 | | |
2143 | | int |
2144 | | event_base_set(struct event_base *base, struct event *ev) |
2145 | 0 | { |
2146 | 0 | /* Only innocent events may be assigned to a different base */ |
2147 | 0 | if (ev->ev_flags != EVLIST_INIT) |
2148 | 0 | return (-1); |
2149 | 0 | |
2150 | 0 | event_debug_assert_is_setup_(ev); |
2151 | 0 |
|
2152 | 0 | ev->ev_base = base; |
2153 | 0 | ev->ev_pri = base->nactivequeues/2; |
2154 | 0 |
|
2155 | 0 | return (0); |
2156 | 0 | } |
2157 | | |
2158 | | void |
2159 | | event_set(struct event *ev, evutil_socket_t fd, short events, |
2160 | | void (*callback)(evutil_socket_t, short, void *), void *arg) |
2161 | 0 | { |
2162 | 0 | int r; |
2163 | 0 | r = event_assign(ev, current_base, fd, events, callback, arg); |
2164 | 0 | EVUTIL_ASSERT(r == 0); |
2165 | 0 | } |
2166 | | |
2167 | | void * |
2168 | | event_self_cbarg(void) |
2169 | 0 | { |
2170 | 0 | return &event_self_cbarg_ptr_; |
2171 | 0 | } |
2172 | | |
2173 | | struct event * |
2174 | | event_base_get_running_event(struct event_base *base) |
2175 | 0 | { |
2176 | 0 | struct event *ev = NULL; |
2177 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
2178 | 0 | if (EVBASE_IN_THREAD(base)) { |
2179 | 0 | struct event_callback *evcb = base->current_event; |
2180 | 0 | if (evcb->evcb_flags & EVLIST_INIT) |
2181 | 0 | ev = event_callback_to_event(evcb); |
2182 | 0 | } |
2183 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2184 | 0 | return ev; |
2185 | 0 | } |
2186 | | |
2187 | | struct event * |
2188 | | event_new(struct event_base *base, evutil_socket_t fd, short events, void (*cb)(evutil_socket_t, short, void *), void *arg) |
2189 | 0 | { |
2190 | 0 | struct event *ev; |
2191 | 0 | ev = mm_malloc(sizeof(struct event)); |
2192 | 0 | if (ev == NULL) |
2193 | 0 | return (NULL); |
2194 | 0 | if (event_assign(ev, base, fd, events, cb, arg) < 0) { |
2195 | 0 | mm_free(ev); |
2196 | 0 | return (NULL); |
2197 | 0 | } |
2198 | 0 |
|
2199 | 0 | return (ev); |
2200 | 0 | } |
2201 | | |
2202 | | void |
2203 | | event_free(struct event *ev) |
2204 | 0 | { |
2205 | 0 | /* This is disabled, so that events which have been finalized be a |
2206 | 0 | * valid target for event_free(). That's */ |
2207 | 0 | // event_debug_assert_is_setup_(ev); |
2208 | 0 |
|
2209 | 0 | /* make sure that this event won't be coming back to haunt us. */ |
2210 | 0 | event_del(ev); |
2211 | 0 | event_debug_note_teardown_(ev); |
2212 | 0 | mm_free(ev); |
2213 | 0 |
|
2214 | 0 | } |
2215 | | |
2216 | | void |
2217 | | event_debug_unassign(struct event *ev) |
2218 | 0 | { |
2219 | 0 | event_debug_assert_not_added_(ev); |
2220 | 0 | event_debug_note_teardown_(ev); |
2221 | 0 |
|
2222 | 0 | ev->ev_flags &= ~EVLIST_INIT; |
2223 | 0 | } |
2224 | | |
2225 | 0 | #define EVENT_FINALIZE_FREE_ 0x10000 |
2226 | | static int |
2227 | | event_finalize_nolock_(struct event_base *base, unsigned flags, struct event *ev, event_finalize_callback_fn cb) |
2228 | 0 | { |
2229 | 0 | ev_uint8_t closure = (flags & EVENT_FINALIZE_FREE_) ? |
2230 | 0 | EV_CLOSURE_EVENT_FINALIZE_FREE : EV_CLOSURE_EVENT_FINALIZE; |
2231 | 0 |
|
2232 | 0 | event_del_nolock_(ev, EVENT_DEL_NOBLOCK); |
2233 | 0 | ev->ev_closure = closure; |
2234 | 0 | ev->ev_evcallback.evcb_cb_union.evcb_evfinalize = cb; |
2235 | 0 | event_active_nolock_(ev, EV_FINALIZE, 1); |
2236 | 0 | ev->ev_flags |= EVLIST_FINALIZING; |
2237 | 0 | return 0; |
2238 | 0 | } |
2239 | | |
2240 | | static int |
2241 | | event_finalize_impl_(unsigned flags, struct event *ev, event_finalize_callback_fn cb) |
2242 | 0 | { |
2243 | 0 | int r; |
2244 | 0 | struct event_base *base = ev->ev_base; |
2245 | 0 | if (EVUTIL_FAILURE_CHECK(!base)) { |
2246 | 0 | event_warnx("%s: event has no event_base set.", __func__); |
2247 | 0 | return -1; |
2248 | 0 | } |
2249 | 0 |
|
2250 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
2251 | 0 | r = event_finalize_nolock_(base, flags, ev, cb); |
2252 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2253 | 0 | return r; |
2254 | 0 | } |
2255 | | |
2256 | | int |
2257 | | event_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb) |
2258 | 0 | { |
2259 | 0 | return event_finalize_impl_(flags, ev, cb); |
2260 | 0 | } |
2261 | | |
2262 | | int |
2263 | | event_free_finalize(unsigned flags, struct event *ev, event_finalize_callback_fn cb) |
2264 | 0 | { |
2265 | 0 | return event_finalize_impl_(flags|EVENT_FINALIZE_FREE_, ev, cb); |
2266 | 0 | } |
2267 | | |
2268 | | void |
2269 | | event_callback_finalize_nolock_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *)) |
2270 | 0 | { |
2271 | 0 | struct event *ev = NULL; |
2272 | 0 | if (evcb->evcb_flags & EVLIST_INIT) { |
2273 | 0 | ev = event_callback_to_event(evcb); |
2274 | 0 | event_del_nolock_(ev, EVENT_DEL_NOBLOCK); |
2275 | 0 | } else { |
2276 | 0 | event_callback_cancel_nolock_(base, evcb, 0); /*XXX can this fail?*/ |
2277 | 0 | } |
2278 | 0 |
|
2279 | 0 | evcb->evcb_closure = EV_CLOSURE_CB_FINALIZE; |
2280 | 0 | evcb->evcb_cb_union.evcb_cbfinalize = cb; |
2281 | 0 | event_callback_activate_nolock_(base, evcb); /* XXX can this really fail?*/ |
2282 | 0 | evcb->evcb_flags |= EVLIST_FINALIZING; |
2283 | 0 | } |
2284 | | |
2285 | | void |
2286 | | event_callback_finalize_(struct event_base *base, unsigned flags, struct event_callback *evcb, void (*cb)(struct event_callback *, void *)) |
2287 | 0 | { |
2288 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
2289 | 0 | event_callback_finalize_nolock_(base, flags, evcb, cb); |
2290 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2291 | 0 | } |
2292 | | |
2293 | | /** Internal: Finalize all of the n_cbs callbacks in evcbs. The provided |
2294 | | * callback will be invoked on *one of them*, after they have *all* been |
2295 | | * finalized. */ |
2296 | | int |
2297 | | event_callback_finalize_many_(struct event_base *base, int n_cbs, struct event_callback **evcbs, void (*cb)(struct event_callback *, void *)) |
2298 | 0 | { |
2299 | 0 | int n_pending = 0, i; |
2300 | 0 |
|
2301 | 0 | if (base == NULL) |
2302 | 0 | base = current_base; |
2303 | 0 |
|
2304 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
2305 | 0 |
|
2306 | 0 | event_debug(("%s: %d events finalizing", __func__, n_cbs)); |
2307 | 0 |
|
2308 | 0 | /* At most one can be currently executing; the rest we just |
2309 | 0 | * cancel... But we always make sure that the finalize callback |
2310 | 0 | * runs. */ |
2311 | 0 | for (i = 0; i < n_cbs; ++i) { |
2312 | 0 | struct event_callback *evcb = evcbs[i]; |
2313 | 0 | if (evcb == base->current_event) { |
2314 | 0 | event_callback_finalize_nolock_(base, 0, evcb, cb); |
2315 | 0 | ++n_pending; |
2316 | 0 | } else { |
2317 | 0 | event_callback_cancel_nolock_(base, evcb, 0); |
2318 | 0 | } |
2319 | 0 | } |
2320 | 0 |
|
2321 | 0 | if (n_pending == 0) { |
2322 | 0 | /* Just do the first one. */ |
2323 | 0 | event_callback_finalize_nolock_(base, 0, evcbs[0], cb); |
2324 | 0 | } |
2325 | 0 |
|
2326 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2327 | 0 | return 0; |
2328 | 0 | } |
2329 | | |
2330 | | /* |
2331 | | * Set's the priority of an event - if an event is already scheduled |
2332 | | * changing the priority is going to fail. |
2333 | | */ |
2334 | | |
2335 | | int |
2336 | | event_priority_set(struct event *ev, int pri) |
2337 | 0 | { |
2338 | 0 | event_debug_assert_is_setup_(ev); |
2339 | 0 |
|
2340 | 0 | if (ev->ev_flags & EVLIST_ACTIVE) |
2341 | 0 | return (-1); |
2342 | 0 | if (pri < 0 || pri >= ev->ev_base->nactivequeues) |
2343 | 0 | return (-1); |
2344 | 0 | |
2345 | 0 | ev->ev_pri = pri; |
2346 | 0 |
|
2347 | 0 | return (0); |
2348 | 0 | } |
2349 | | |
2350 | | /* |
2351 | | * Checks if a specific event is pending or scheduled. |
2352 | | */ |
2353 | | |
2354 | | int |
2355 | | event_pending(const struct event *ev, short event, struct timeval *tv) |
2356 | 0 | { |
2357 | 0 | int flags = 0; |
2358 | 0 |
|
2359 | 0 | if (EVUTIL_FAILURE_CHECK(ev->ev_base == NULL)) { |
2360 | 0 | event_warnx("%s: event has no event_base set.", __func__); |
2361 | 0 | return 0; |
2362 | 0 | } |
2363 | 0 |
|
2364 | 0 | EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); |
2365 | 0 | event_debug_assert_is_setup_(ev); |
2366 | 0 |
|
2367 | 0 | if (ev->ev_flags & EVLIST_INSERTED) |
2368 | 0 | flags |= (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)); |
2369 | 0 | if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) |
2370 | 0 | flags |= ev->ev_res; |
2371 | 0 | if (ev->ev_flags & EVLIST_TIMEOUT) |
2372 | 0 | flags |= EV_TIMEOUT; |
2373 | 0 |
|
2374 | 0 | event &= (EV_TIMEOUT|EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL); |
2375 | 0 |
|
2376 | 0 | /* See if there is a timeout that we should report */ |
2377 | 0 | if (tv != NULL && (flags & event & EV_TIMEOUT)) { |
2378 | 0 | struct timeval tmp = ev->ev_timeout; |
2379 | 0 | tmp.tv_usec &= MICROSECONDS_MASK; |
2380 | 0 | /* correctly remamp to real time */ |
2381 | 0 | evutil_timeradd(&ev->ev_base->tv_clock_diff, &tmp, tv); |
2382 | 0 | } |
2383 | 0 |
|
2384 | 0 | EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); |
2385 | 0 |
|
2386 | 0 | return (flags & event); |
2387 | 0 | } |
2388 | | |
2389 | | int |
2390 | | event_initialized(const struct event *ev) |
2391 | 0 | { |
2392 | 0 | if (!(ev->ev_flags & EVLIST_INIT)) |
2393 | 0 | return 0; |
2394 | 0 | |
2395 | 0 | return 1; |
2396 | 0 | } |
2397 | | |
2398 | | void |
2399 | | event_get_assignment(const struct event *event, struct event_base **base_out, evutil_socket_t *fd_out, short *events_out, event_callback_fn *callback_out, void **arg_out) |
2400 | 0 | { |
2401 | 0 | event_debug_assert_is_setup_(event); |
2402 | 0 |
|
2403 | 0 | if (base_out) |
2404 | 0 | *base_out = event->ev_base; |
2405 | 0 | if (fd_out) |
2406 | 0 | *fd_out = event->ev_fd; |
2407 | 0 | if (events_out) |
2408 | 0 | *events_out = event->ev_events; |
2409 | 0 | if (callback_out) |
2410 | 0 | *callback_out = event->ev_callback; |
2411 | 0 | if (arg_out) |
2412 | 0 | *arg_out = event->ev_arg; |
2413 | 0 | } |
2414 | | |
2415 | | size_t |
2416 | | event_get_struct_event_size(void) |
2417 | 0 | { |
2418 | 0 | return sizeof(struct event); |
2419 | 0 | } |
2420 | | |
2421 | | evutil_socket_t |
2422 | | event_get_fd(const struct event *ev) |
2423 | 0 | { |
2424 | 0 | event_debug_assert_is_setup_(ev); |
2425 | 0 | return ev->ev_fd; |
2426 | 0 | } |
2427 | | |
2428 | | struct event_base * |
2429 | | event_get_base(const struct event *ev) |
2430 | 0 | { |
2431 | 0 | event_debug_assert_is_setup_(ev); |
2432 | 0 | return ev->ev_base; |
2433 | 0 | } |
2434 | | |
2435 | | short |
2436 | | event_get_events(const struct event *ev) |
2437 | 0 | { |
2438 | 0 | event_debug_assert_is_setup_(ev); |
2439 | 0 | return ev->ev_events; |
2440 | 0 | } |
2441 | | |
2442 | | event_callback_fn |
2443 | | event_get_callback(const struct event *ev) |
2444 | 0 | { |
2445 | 0 | event_debug_assert_is_setup_(ev); |
2446 | 0 | return ev->ev_callback; |
2447 | 0 | } |
2448 | | |
2449 | | void * |
2450 | | event_get_callback_arg(const struct event *ev) |
2451 | 0 | { |
2452 | 0 | event_debug_assert_is_setup_(ev); |
2453 | 0 | return ev->ev_arg; |
2454 | 0 | } |
2455 | | |
2456 | | int |
2457 | | event_get_priority(const struct event *ev) |
2458 | 0 | { |
2459 | 0 | event_debug_assert_is_setup_(ev); |
2460 | 0 | return ev->ev_pri; |
2461 | 0 | } |
2462 | | |
2463 | | int |
2464 | | event_add(struct event *ev, const struct timeval *tv) |
2465 | 0 | { |
2466 | 0 | int res; |
2467 | 0 |
|
2468 | 0 | if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { |
2469 | 0 | event_warnx("%s: event has no event_base set.", __func__); |
2470 | 0 | return -1; |
2471 | 0 | } |
2472 | 0 |
|
2473 | 0 | EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); |
2474 | 0 |
|
2475 | 0 | res = event_add_nolock_(ev, tv, 0); |
2476 | 0 |
|
2477 | 0 | EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); |
2478 | 0 |
|
2479 | 0 | return (res); |
2480 | 0 | } |
2481 | | |
2482 | | /* Helper callback: wake an event_base from another thread. This version |
2483 | | * works by writing a byte to one end of a socketpair, so that the event_base |
2484 | | * listening on the other end will wake up as the corresponding event |
2485 | | * triggers */ |
2486 | | static int |
2487 | | evthread_notify_base_default(struct event_base *base) |
2488 | 0 | { |
2489 | 0 | char buf[1]; |
2490 | 0 | int r; |
2491 | 0 | buf[0] = (char) 0; |
2492 | | #ifdef _WIN32 |
2493 | | r = send(base->th_notify_fd[1], buf, 1, 0); |
2494 | | #else |
2495 | | r = write(base->th_notify_fd[1], buf, 1); |
2496 | 0 | #endif |
2497 | 0 | return (r < 0 && ! EVUTIL_ERR_IS_EAGAIN(errno)) ? -1 : 0; |
2498 | 0 | } |
2499 | | |
2500 | | #ifdef EVENT__HAVE_EVENTFD |
2501 | | /* Helper callback: wake an event_base from another thread. This version |
2502 | | * assumes that you have a working eventfd() implementation. */ |
2503 | | static int |
2504 | | evthread_notify_base_eventfd(struct event_base *base) |
2505 | 0 | { |
2506 | 0 | ev_uint64_t msg = 1; |
2507 | 0 | int r; |
2508 | 0 | do { |
2509 | 0 | r = write(base->th_notify_fd[0], (void*) &msg, sizeof(msg)); |
2510 | 0 | } while (r < 0 && errno == EAGAIN); |
2511 | 0 |
|
2512 | 0 | return (r < 0) ? -1 : 0; |
2513 | 0 | } |
2514 | | #endif |
2515 | | |
2516 | | |
2517 | | /** Tell the thread currently running the event_loop for base (if any) that it |
2518 | | * needs to stop waiting in its dispatch function (if it is) and process all |
2519 | | * active callbacks. */ |
2520 | | static int |
2521 | | evthread_notify_base(struct event_base *base) |
2522 | 0 | { |
2523 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
2524 | 0 | if (!base->th_notify_fn) |
2525 | 0 | return -1; |
2526 | 0 | if (base->is_notify_pending) |
2527 | 0 | return 0; |
2528 | 0 | base->is_notify_pending = 1; |
2529 | 0 | return base->th_notify_fn(base); |
2530 | 0 | } |
2531 | | |
2532 | | /* Implementation function to remove a timeout on a currently pending event. |
2533 | | */ |
2534 | | int |
2535 | | event_remove_timer_nolock_(struct event *ev) |
2536 | 0 | { |
2537 | 0 | struct event_base *base = ev->ev_base; |
2538 | 0 |
|
2539 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
2540 | 0 | event_debug_assert_is_setup_(ev); |
2541 | 0 |
|
2542 | 0 | event_debug(("event_remove_timer_nolock: event: %p", ev)); |
2543 | 0 |
|
2544 | 0 | /* If it's not pending on a timeout, we don't need to do anything. */ |
2545 | 0 | if (ev->ev_flags & EVLIST_TIMEOUT) { |
2546 | 0 | event_queue_remove_timeout(base, ev); |
2547 | 0 | evutil_timerclear(&ev->ev_.ev_io.ev_timeout); |
2548 | 0 | } |
2549 | 0 |
|
2550 | 0 | return (0); |
2551 | 0 | } |
2552 | | |
2553 | | int |
2554 | | event_remove_timer(struct event *ev) |
2555 | 0 | { |
2556 | 0 | int res; |
2557 | 0 |
|
2558 | 0 | if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { |
2559 | 0 | event_warnx("%s: event has no event_base set.", __func__); |
2560 | 0 | return -1; |
2561 | 0 | } |
2562 | 0 |
|
2563 | 0 | EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); |
2564 | 0 |
|
2565 | 0 | res = event_remove_timer_nolock_(ev); |
2566 | 0 |
|
2567 | 0 | EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); |
2568 | 0 |
|
2569 | 0 | return (res); |
2570 | 0 | } |
2571 | | |
2572 | | /* Implementation function to add an event. Works just like event_add, |
2573 | | * except: 1) it requires that we have the lock. 2) if tv_is_absolute is set, |
2574 | | * we treat tv as an absolute time, not as an interval to add to the current |
2575 | | * time */ |
2576 | | int |
2577 | | event_add_nolock_(struct event *ev, const struct timeval *tv, |
2578 | | int tv_is_absolute) |
2579 | 0 | { |
2580 | 0 | struct event_base *base = ev->ev_base; |
2581 | 0 | int res = 0; |
2582 | 0 | int notify = 0; |
2583 | 0 |
|
2584 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
2585 | 0 | event_debug_assert_is_setup_(ev); |
2586 | 0 |
|
2587 | 0 | event_debug(( |
2588 | 0 | "event_add: event: %p (fd "EV_SOCK_FMT"), %s%s%s%scall %p", |
2589 | 0 | ev, |
2590 | 0 | EV_SOCK_ARG(ev->ev_fd), |
2591 | 0 | ev->ev_events & EV_READ ? "EV_READ " : " ", |
2592 | 0 | ev->ev_events & EV_WRITE ? "EV_WRITE " : " ", |
2593 | 0 | ev->ev_events & EV_CLOSED ? "EV_CLOSED " : " ", |
2594 | 0 | tv ? "EV_TIMEOUT " : " ", |
2595 | 0 | ev->ev_callback)); |
2596 | 0 |
|
2597 | 0 | EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL)); |
2598 | 0 |
|
2599 | 0 | if (ev->ev_flags & EVLIST_FINALIZING) { |
2600 | 0 | /* XXXX debug */ |
2601 | 0 | return (-1); |
2602 | 0 | } |
2603 | 0 | |
2604 | 0 | /* |
2605 | 0 | * prepare for timeout insertion further below, if we get a |
2606 | 0 | * failure on any step, we should not change any state. |
2607 | 0 | */ |
2608 | 0 | if (tv != NULL && !(ev->ev_flags & EVLIST_TIMEOUT)) { |
2609 | 0 | if (min_heap_reserve_(&base->timeheap, |
2610 | 0 | 1 + min_heap_size_(&base->timeheap)) == -1) |
2611 | 0 | return (-1); /* ENOMEM == errno */ |
2612 | 0 | } |
2613 | 0 | |
2614 | 0 | /* If the main thread is currently executing a signal event's |
2615 | 0 | * callback, and we are not the main thread, then we want to wait |
2616 | 0 | * until the callback is done before we mess with the event, or else |
2617 | 0 | * we can race on ev_ncalls and ev_pncalls below. */ |
2618 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
2619 | 0 | if (base->current_event == event_to_event_callback(ev) && |
2620 | 0 | (ev->ev_events & EV_SIGNAL) |
2621 | 0 | && !EVBASE_IN_THREAD(base)) { |
2622 | 0 | ++base->current_event_waiters; |
2623 | 0 | EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); |
2624 | 0 | } |
2625 | 0 | #endif |
2626 | 0 |
|
2627 | 0 | if ((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED|EV_SIGNAL)) && |
2628 | 0 | !(ev->ev_flags & (EVLIST_INSERTED|EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { |
2629 | 0 | if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)) |
2630 | 0 | res = evmap_io_add_(base, ev->ev_fd, ev); |
2631 | 0 | else if (ev->ev_events & EV_SIGNAL) |
2632 | 0 | res = evmap_signal_add_(base, (int)ev->ev_fd, ev); |
2633 | 0 | if (res != -1) |
2634 | 0 | event_queue_insert_inserted(base, ev); |
2635 | 0 | if (res == 1) { |
2636 | 0 | /* evmap says we need to notify the main thread. */ |
2637 | 0 | notify = 1; |
2638 | 0 | res = 0; |
2639 | 0 | } |
2640 | 0 | } |
2641 | 0 |
|
2642 | 0 | /* |
2643 | 0 | * we should change the timeout state only if the previous event |
2644 | 0 | * addition succeeded. |
2645 | 0 | */ |
2646 | 0 | if (res != -1 && tv != NULL) { |
2647 | 0 | struct timeval now; |
2648 | 0 | int common_timeout; |
2649 | | #ifdef USE_REINSERT_TIMEOUT |
2650 | | int was_common; |
2651 | | int old_timeout_idx; |
2652 | | #endif |
2653 | |
|
2654 | 0 | /* |
2655 | 0 | * for persistent timeout events, we remember the |
2656 | 0 | * timeout value and re-add the event. |
2657 | 0 | * |
2658 | 0 | * If tv_is_absolute, this was already set. |
2659 | 0 | */ |
2660 | 0 | if (ev->ev_closure == EV_CLOSURE_EVENT_PERSIST && !tv_is_absolute) |
2661 | 0 | ev->ev_io_timeout = *tv; |
2662 | 0 |
|
2663 | 0 | #ifndef USE_REINSERT_TIMEOUT |
2664 | 0 | if (ev->ev_flags & EVLIST_TIMEOUT) { |
2665 | 0 | event_queue_remove_timeout(base, ev); |
2666 | 0 | } |
2667 | 0 | #endif |
2668 | 0 |
|
2669 | 0 | /* Check if it is active due to a timeout. Rescheduling |
2670 | 0 | * this timeout before the callback can be executed |
2671 | 0 | * removes it from the active list. */ |
2672 | 0 | if ((ev->ev_flags & EVLIST_ACTIVE) && |
2673 | 0 | (ev->ev_res & EV_TIMEOUT)) { |
2674 | 0 | if (ev->ev_events & EV_SIGNAL) { |
2675 | 0 | /* See if we are just active executing |
2676 | 0 | * this event in a loop |
2677 | 0 | */ |
2678 | 0 | if (ev->ev_ncalls && ev->ev_pncalls) { |
2679 | 0 | /* Abort loop */ |
2680 | 0 | *ev->ev_pncalls = 0; |
2681 | 0 | } |
2682 | 0 | } |
2683 | 0 |
|
2684 | 0 | event_queue_remove_active(base, event_to_event_callback(ev)); |
2685 | 0 | } |
2686 | 0 |
|
2687 | 0 | gettime(base, &now); |
2688 | 0 |
|
2689 | 0 | common_timeout = is_common_timeout(tv, base); |
2690 | | #ifdef USE_REINSERT_TIMEOUT |
2691 | | was_common = is_common_timeout(&ev->ev_timeout, base); |
2692 | | old_timeout_idx = COMMON_TIMEOUT_IDX(&ev->ev_timeout); |
2693 | | #endif |
2694 | |
|
2695 | 0 | if (tv_is_absolute) { |
2696 | 0 | ev->ev_timeout = *tv; |
2697 | 0 | } else if (common_timeout) { |
2698 | 0 | struct timeval tmp = *tv; |
2699 | 0 | tmp.tv_usec &= MICROSECONDS_MASK; |
2700 | 0 | evutil_timeradd(&now, &tmp, &ev->ev_timeout); |
2701 | 0 | ev->ev_timeout.tv_usec |= |
2702 | 0 | (tv->tv_usec & ~MICROSECONDS_MASK); |
2703 | 0 | } else { |
2704 | 0 | evutil_timeradd(&now, tv, &ev->ev_timeout); |
2705 | 0 | } |
2706 | 0 |
|
2707 | 0 | event_debug(( |
2708 | 0 | "event_add: event %p, timeout in %d seconds %d useconds, call %p", |
2709 | 0 | ev, (int)tv->tv_sec, (int)tv->tv_usec, ev->ev_callback)); |
2710 | 0 |
|
2711 | | #ifdef USE_REINSERT_TIMEOUT |
2712 | | event_queue_reinsert_timeout(base, ev, was_common, common_timeout, old_timeout_idx); |
2713 | | #else |
2714 | | event_queue_insert_timeout(base, ev); |
2715 | 0 | #endif |
2716 | 0 |
|
2717 | 0 | if (common_timeout) { |
2718 | 0 | struct common_timeout_list *ctl = |
2719 | 0 | get_common_timeout_list(base, &ev->ev_timeout); |
2720 | 0 | if (ev == TAILQ_FIRST(&ctl->events)) { |
2721 | 0 | common_timeout_schedule(ctl, &now, ev); |
2722 | 0 | } |
2723 | 0 | } else { |
2724 | 0 | struct event* top = NULL; |
2725 | 0 | /* See if the earliest timeout is now earlier than it |
2726 | 0 | * was before: if so, we will need to tell the main |
2727 | 0 | * thread to wake up earlier than it would otherwise. |
2728 | 0 | * We double check the timeout of the top element to |
2729 | 0 | * handle time distortions due to system suspension. |
2730 | 0 | */ |
2731 | 0 | if (min_heap_elt_is_top_(ev)) |
2732 | 0 | notify = 1; |
2733 | 0 | else if ((top = min_heap_top_(&base->timeheap)) != NULL && |
2734 | 0 | evutil_timercmp(&top->ev_timeout, &now, <)) |
2735 | 0 | notify = 1; |
2736 | 0 | } |
2737 | 0 | } |
2738 | 0 |
|
2739 | 0 | /* if we are not in the right thread, we need to wake up the loop */ |
2740 | 0 | if (res != -1 && notify && EVBASE_NEED_NOTIFY(base)) |
2741 | 0 | evthread_notify_base(base); |
2742 | 0 |
|
2743 | 0 | event_debug_note_add_(ev); |
2744 | 0 |
|
2745 | 0 | return (res); |
2746 | 0 | } |
2747 | | |
2748 | | static int |
2749 | | event_del_(struct event *ev, int blocking) |
2750 | 0 | { |
2751 | 0 | int res; |
2752 | 0 | struct event_base *base = ev->ev_base; |
2753 | 0 |
|
2754 | 0 | if (EVUTIL_FAILURE_CHECK(!base)) { |
2755 | 0 | event_warnx("%s: event has no event_base set.", __func__); |
2756 | 0 | return -1; |
2757 | 0 | } |
2758 | 0 |
|
2759 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
2760 | 0 | res = event_del_nolock_(ev, blocking); |
2761 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2762 | 0 |
|
2763 | 0 | return (res); |
2764 | 0 | } |
2765 | | |
2766 | | int |
2767 | | event_del(struct event *ev) |
2768 | 0 | { |
2769 | 0 | return event_del_(ev, EVENT_DEL_AUTOBLOCK); |
2770 | 0 | } |
2771 | | |
2772 | | int |
2773 | | event_del_block(struct event *ev) |
2774 | 0 | { |
2775 | 0 | return event_del_(ev, EVENT_DEL_BLOCK); |
2776 | 0 | } |
2777 | | |
2778 | | int |
2779 | | event_del_noblock(struct event *ev) |
2780 | 0 | { |
2781 | 0 | return event_del_(ev, EVENT_DEL_NOBLOCK); |
2782 | 0 | } |
2783 | | |
2784 | | /** Helper for event_del: always called with th_base_lock held. |
2785 | | * |
2786 | | * "blocking" must be one of the EVENT_DEL_{BLOCK, NOBLOCK, AUTOBLOCK, |
2787 | | * EVEN_IF_FINALIZING} values. See those for more information. |
2788 | | */ |
2789 | | int |
2790 | | event_del_nolock_(struct event *ev, int blocking) |
2791 | 0 | { |
2792 | 0 | struct event_base *base; |
2793 | 0 | int res = 0, notify = 0; |
2794 | 0 |
|
2795 | 0 | event_debug(("event_del: %p (fd "EV_SOCK_FMT"), callback %p", |
2796 | 0 | ev, EV_SOCK_ARG(ev->ev_fd), ev->ev_callback)); |
2797 | 0 |
|
2798 | 0 | /* An event without a base has not been added */ |
2799 | 0 | if (ev->ev_base == NULL) |
2800 | 0 | return (-1); |
2801 | 0 | |
2802 | 0 | EVENT_BASE_ASSERT_LOCKED(ev->ev_base); |
2803 | 0 |
|
2804 | 0 | if (blocking != EVENT_DEL_EVEN_IF_FINALIZING) { |
2805 | 0 | if (ev->ev_flags & EVLIST_FINALIZING) { |
2806 | 0 | /* XXXX Debug */ |
2807 | 0 | return 0; |
2808 | 0 | } |
2809 | 0 | } |
2810 | 0 | |
2811 | 0 | base = ev->ev_base; |
2812 | 0 |
|
2813 | 0 | EVUTIL_ASSERT(!(ev->ev_flags & ~EVLIST_ALL)); |
2814 | 0 |
|
2815 | 0 | /* See if we are just active executing this event in a loop */ |
2816 | 0 | if (ev->ev_events & EV_SIGNAL) { |
2817 | 0 | if (ev->ev_ncalls && ev->ev_pncalls) { |
2818 | 0 | /* Abort loop */ |
2819 | 0 | *ev->ev_pncalls = 0; |
2820 | 0 | } |
2821 | 0 | } |
2822 | 0 |
|
2823 | 0 | if (ev->ev_flags & EVLIST_TIMEOUT) { |
2824 | 0 | /* NOTE: We never need to notify the main thread because of a |
2825 | 0 | * deleted timeout event: all that could happen if we don't is |
2826 | 0 | * that the dispatch loop might wake up too early. But the |
2827 | 0 | * point of notifying the main thread _is_ to wake up the |
2828 | 0 | * dispatch loop early anyway, so we wouldn't gain anything by |
2829 | 0 | * doing it. |
2830 | 0 | */ |
2831 | 0 | event_queue_remove_timeout(base, ev); |
2832 | 0 | } |
2833 | 0 |
|
2834 | 0 | if (ev->ev_flags & EVLIST_ACTIVE) |
2835 | 0 | event_queue_remove_active(base, event_to_event_callback(ev)); |
2836 | 0 | else if (ev->ev_flags & EVLIST_ACTIVE_LATER) |
2837 | 0 | event_queue_remove_active_later(base, event_to_event_callback(ev)); |
2838 | 0 |
|
2839 | 0 | if (ev->ev_flags & EVLIST_INSERTED) { |
2840 | 0 | event_queue_remove_inserted(base, ev); |
2841 | 0 | if (ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)) |
2842 | 0 | res = evmap_io_del_(base, ev->ev_fd, ev); |
2843 | 0 | else |
2844 | 0 | res = evmap_signal_del_(base, (int)ev->ev_fd, ev); |
2845 | 0 | if (res == 1) { |
2846 | 0 | /* evmap says we need to notify the main thread. */ |
2847 | 0 | notify = 1; |
2848 | 0 | res = 0; |
2849 | 0 | } |
2850 | 0 | /* If we do not have events, let's notify event base so it can |
2851 | 0 | * exit without waiting */ |
2852 | 0 | if (!event_haveevents(base) && !N_ACTIVE_CALLBACKS(base)) |
2853 | 0 | notify = 1; |
2854 | 0 | } |
2855 | 0 |
|
2856 | 0 | /* if we are not in the right thread, we need to wake up the loop */ |
2857 | 0 | if (res != -1 && notify && EVBASE_NEED_NOTIFY(base)) |
2858 | 0 | evthread_notify_base(base); |
2859 | 0 |
|
2860 | 0 | event_debug_note_del_(ev); |
2861 | 0 |
|
2862 | 0 | /* If the main thread is currently executing this event's callback, |
2863 | 0 | * and we are not the main thread, then we want to wait until the |
2864 | 0 | * callback is done before returning. That way, when this function |
2865 | 0 | * returns, it will be safe to free the user-supplied argument. |
2866 | 0 | */ |
2867 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
2868 | 0 | if (blocking != EVENT_DEL_NOBLOCK && |
2869 | 0 | base->current_event == event_to_event_callback(ev) && |
2870 | 0 | !EVBASE_IN_THREAD(base) && |
2871 | 0 | (blocking == EVENT_DEL_BLOCK || !(ev->ev_events & EV_FINALIZE))) { |
2872 | 0 | ++base->current_event_waiters; |
2873 | 0 | EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); |
2874 | 0 | } |
2875 | 0 | #endif |
2876 | 0 |
|
2877 | 0 | return (res); |
2878 | 0 | } |
2879 | | |
2880 | | void |
2881 | | event_active(struct event *ev, int res, short ncalls) |
2882 | 0 | { |
2883 | 0 | if (EVUTIL_FAILURE_CHECK(!ev->ev_base)) { |
2884 | 0 | event_warnx("%s: event has no event_base set.", __func__); |
2885 | 0 | return; |
2886 | 0 | } |
2887 | 0 |
|
2888 | 0 | EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); |
2889 | 0 |
|
2890 | 0 | event_debug_assert_is_setup_(ev); |
2891 | 0 |
|
2892 | 0 | event_active_nolock_(ev, res, ncalls); |
2893 | 0 |
|
2894 | 0 | EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); |
2895 | 0 | } |
2896 | | |
2897 | | |
2898 | | void |
2899 | | event_active_nolock_(struct event *ev, int res, short ncalls) |
2900 | 0 | { |
2901 | 0 | struct event_base *base; |
2902 | 0 |
|
2903 | 0 | event_debug(("event_active: %p (fd "EV_SOCK_FMT"), res %d, callback %p", |
2904 | 0 | ev, EV_SOCK_ARG(ev->ev_fd), (int)res, ev->ev_callback)); |
2905 | 0 |
|
2906 | 0 | base = ev->ev_base; |
2907 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
2908 | 0 |
|
2909 | 0 | if (ev->ev_flags & EVLIST_FINALIZING) { |
2910 | 0 | /* XXXX debug */ |
2911 | 0 | return; |
2912 | 0 | } |
2913 | 0 | |
2914 | 0 | switch ((ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { |
2915 | 0 | default: |
2916 | 0 | case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER: |
2917 | 0 | EVUTIL_ASSERT(0); |
2918 | 0 | break; |
2919 | 0 | case EVLIST_ACTIVE: |
2920 | 0 | /* We get different kinds of events, add them together */ |
2921 | 0 | ev->ev_res |= res; |
2922 | 0 | return; |
2923 | 0 | case EVLIST_ACTIVE_LATER: |
2924 | 0 | ev->ev_res |= res; |
2925 | 0 | break; |
2926 | 0 | case 0: |
2927 | 0 | ev->ev_res = res; |
2928 | 0 | break; |
2929 | 0 | } |
2930 | 0 | |
2931 | 0 | if (ev->ev_pri < base->event_running_priority) |
2932 | 0 | base->event_continue = 1; |
2933 | 0 |
|
2934 | 0 | if (ev->ev_events & EV_SIGNAL) { |
2935 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
2936 | 0 | if (base->current_event == event_to_event_callback(ev) && |
2937 | 0 | !EVBASE_IN_THREAD(base)) { |
2938 | 0 | ++base->current_event_waiters; |
2939 | 0 | EVTHREAD_COND_WAIT(base->current_event_cond, base->th_base_lock); |
2940 | 0 | } |
2941 | 0 | #endif |
2942 | 0 | ev->ev_ncalls = ncalls; |
2943 | 0 | ev->ev_pncalls = NULL; |
2944 | 0 | } |
2945 | 0 |
|
2946 | 0 | event_callback_activate_nolock_(base, event_to_event_callback(ev)); |
2947 | 0 | } |
2948 | | |
2949 | | void |
2950 | | event_active_later_(struct event *ev, int res) |
2951 | 0 | { |
2952 | 0 | EVBASE_ACQUIRE_LOCK(ev->ev_base, th_base_lock); |
2953 | 0 | event_active_later_nolock_(ev, res); |
2954 | 0 | EVBASE_RELEASE_LOCK(ev->ev_base, th_base_lock); |
2955 | 0 | } |
2956 | | |
2957 | | void |
2958 | | event_active_later_nolock_(struct event *ev, int res) |
2959 | 0 | { |
2960 | 0 | struct event_base *base = ev->ev_base; |
2961 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
2962 | 0 |
|
2963 | 0 | if (ev->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) { |
2964 | 0 | /* We get different kinds of events, add them together */ |
2965 | 0 | ev->ev_res |= res; |
2966 | 0 | return; |
2967 | 0 | } |
2968 | 0 | |
2969 | 0 | ev->ev_res = res; |
2970 | 0 |
|
2971 | 0 | event_callback_activate_later_nolock_(base, event_to_event_callback(ev)); |
2972 | 0 | } |
2973 | | |
2974 | | int |
2975 | | event_callback_activate_(struct event_base *base, |
2976 | | struct event_callback *evcb) |
2977 | 0 | { |
2978 | 0 | int r; |
2979 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
2980 | 0 | r = event_callback_activate_nolock_(base, evcb); |
2981 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
2982 | 0 | return r; |
2983 | 0 | } |
2984 | | |
2985 | | int |
2986 | | event_callback_activate_nolock_(struct event_base *base, |
2987 | | struct event_callback *evcb) |
2988 | 0 | { |
2989 | 0 | int r = 1; |
2990 | 0 |
|
2991 | 0 | if (evcb->evcb_flags & EVLIST_FINALIZING) |
2992 | 0 | return 0; |
2993 | 0 | |
2994 | 0 | switch (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) { |
2995 | 0 | default: |
2996 | 0 | EVUTIL_ASSERT(0); |
2997 | 0 | EVUTIL_FALLTHROUGH; |
2998 | 0 | case EVLIST_ACTIVE_LATER: |
2999 | 0 | event_queue_remove_active_later(base, evcb); |
3000 | 0 | r = 0; |
3001 | 0 | break; |
3002 | 0 | case EVLIST_ACTIVE: |
3003 | 0 | return 0; |
3004 | 0 | case 0: |
3005 | 0 | break; |
3006 | 0 | } |
3007 | 0 | |
3008 | 0 | event_queue_insert_active(base, evcb); |
3009 | 0 |
|
3010 | 0 | if (EVBASE_NEED_NOTIFY(base)) |
3011 | 0 | evthread_notify_base(base); |
3012 | 0 |
|
3013 | 0 | return r; |
3014 | 0 | } |
3015 | | |
3016 | | int |
3017 | | event_callback_activate_later_nolock_(struct event_base *base, |
3018 | | struct event_callback *evcb) |
3019 | 0 | { |
3020 | 0 | if (evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) |
3021 | 0 | return 0; |
3022 | 0 | |
3023 | 0 | event_queue_insert_active_later(base, evcb); |
3024 | 0 | if (EVBASE_NEED_NOTIFY(base)) |
3025 | 0 | evthread_notify_base(base); |
3026 | 0 | return 1; |
3027 | 0 | } |
3028 | | |
3029 | | void |
3030 | | event_callback_init_(struct event_base *base, |
3031 | | struct event_callback *cb) |
3032 | 0 | { |
3033 | 0 | memset(cb, 0, sizeof(*cb)); |
3034 | 0 | cb->evcb_pri = base->nactivequeues - 1; |
3035 | 0 | } |
3036 | | |
3037 | | int |
3038 | | event_callback_cancel_(struct event_base *base, |
3039 | | struct event_callback *evcb) |
3040 | 0 | { |
3041 | 0 | int r; |
3042 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3043 | 0 | r = event_callback_cancel_nolock_(base, evcb, 0); |
3044 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3045 | 0 | return r; |
3046 | 0 | } |
3047 | | |
3048 | | int |
3049 | | event_callback_cancel_nolock_(struct event_base *base, |
3050 | | struct event_callback *evcb, int even_if_finalizing) |
3051 | 0 | { |
3052 | 0 | if ((evcb->evcb_flags & EVLIST_FINALIZING) && !even_if_finalizing) |
3053 | 0 | return 0; |
3054 | 0 | |
3055 | 0 | if (evcb->evcb_flags & EVLIST_INIT) |
3056 | 0 | return event_del_nolock_(event_callback_to_event(evcb), |
3057 | 0 | even_if_finalizing ? EVENT_DEL_EVEN_IF_FINALIZING : EVENT_DEL_AUTOBLOCK); |
3058 | 0 |
|
3059 | 0 | switch ((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) { |
3060 | 0 | default: |
3061 | 0 | case EVLIST_ACTIVE|EVLIST_ACTIVE_LATER: |
3062 | 0 | EVUTIL_ASSERT(0); |
3063 | 0 | break; |
3064 | 0 | case EVLIST_ACTIVE: |
3065 | 0 | /* We get different kinds of events, add them together */ |
3066 | 0 | event_queue_remove_active(base, evcb); |
3067 | 0 | return 0; |
3068 | 0 | case EVLIST_ACTIVE_LATER: |
3069 | 0 | event_queue_remove_active_later(base, evcb); |
3070 | 0 | break; |
3071 | 0 | case 0: |
3072 | 0 | break; |
3073 | 0 | } |
3074 | 0 | |
3075 | 0 | return 0; |
3076 | 0 | } |
3077 | | |
3078 | | void |
3079 | | event_deferred_cb_init_(struct event_callback *cb, ev_uint8_t priority, deferred_cb_fn fn, void *arg) |
3080 | 0 | { |
3081 | 0 | memset(cb, 0, sizeof(*cb)); |
3082 | 0 | cb->evcb_cb_union.evcb_selfcb = fn; |
3083 | 0 | cb->evcb_arg = arg; |
3084 | 0 | cb->evcb_pri = priority; |
3085 | 0 | cb->evcb_closure = EV_CLOSURE_CB_SELF; |
3086 | 0 | } |
3087 | | |
3088 | | void |
3089 | | event_deferred_cb_set_priority_(struct event_callback *cb, ev_uint8_t priority) |
3090 | 0 | { |
3091 | 0 | cb->evcb_pri = priority; |
3092 | 0 | } |
3093 | | |
3094 | | void |
3095 | | event_deferred_cb_cancel_(struct event_base *base, struct event_callback *cb) |
3096 | 0 | { |
3097 | 0 | if (!base) |
3098 | 0 | base = current_base; |
3099 | 0 | event_callback_cancel_(base, cb); |
3100 | 0 | } |
3101 | | |
3102 | 0 | #define MAX_DEFERREDS_QUEUED 32 |
3103 | | int |
3104 | | event_deferred_cb_schedule_(struct event_base *base, struct event_callback *cb) |
3105 | 0 | { |
3106 | 0 | int r = 1; |
3107 | 0 | if (!base) |
3108 | 0 | base = current_base; |
3109 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3110 | 0 | if (base->n_deferreds_queued > MAX_DEFERREDS_QUEUED) { |
3111 | 0 | r = event_callback_activate_later_nolock_(base, cb); |
3112 | 0 | } else { |
3113 | 0 | r = event_callback_activate_nolock_(base, cb); |
3114 | 0 | if (r) { |
3115 | 0 | ++base->n_deferreds_queued; |
3116 | 0 | } |
3117 | 0 | } |
3118 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3119 | 0 | return r; |
3120 | 0 | } |
3121 | | |
3122 | | static int |
3123 | | timeout_next(struct event_base *base, struct timeval **tv_p) |
3124 | 0 | { |
3125 | 0 | /* Caller must hold th_base_lock */ |
3126 | 0 | struct timeval now; |
3127 | 0 | struct event *ev; |
3128 | 0 | struct timeval *tv = *tv_p; |
3129 | 0 | int res = 0; |
3130 | 0 |
|
3131 | 0 | ev = min_heap_top_(&base->timeheap); |
3132 | 0 |
|
3133 | 0 | if (ev == NULL) { |
3134 | 0 | /* if no time-based events are active wait for I/O */ |
3135 | 0 | *tv_p = NULL; |
3136 | 0 | goto out; |
3137 | 0 | } |
3138 | 0 | |
3139 | 0 | if (gettime(base, &now) == -1) { |
3140 | 0 | res = -1; |
3141 | 0 | goto out; |
3142 | 0 | } |
3143 | 0 | |
3144 | 0 | if (evutil_timercmp(&ev->ev_timeout, &now, <=)) { |
3145 | 0 | evutil_timerclear(tv); |
3146 | 0 | goto out; |
3147 | 0 | } |
3148 | 0 |
|
3149 | 0 | evutil_timersub(&ev->ev_timeout, &now, tv); |
3150 | 0 |
|
3151 | 0 | EVUTIL_ASSERT(tv->tv_sec >= 0); |
3152 | 0 | EVUTIL_ASSERT(tv->tv_usec >= 0); |
3153 | 0 | event_debug(("timeout_next: event: %p, in %d seconds, %d useconds", ev, (int)tv->tv_sec, (int)tv->tv_usec)); |
3154 | 0 |
|
3155 | 0 | out: |
3156 | 0 | return (res); |
3157 | 0 | } |
3158 | | |
3159 | | /* Activate every event whose timeout has elapsed. */ |
3160 | | static void |
3161 | | timeout_process(struct event_base *base) |
3162 | 0 | { |
3163 | 0 | /* Caller must hold lock. */ |
3164 | 0 | struct timeval now; |
3165 | 0 | struct event *ev; |
3166 | 0 |
|
3167 | 0 | if (min_heap_empty_(&base->timeheap)) { |
3168 | 0 | return; |
3169 | 0 | } |
3170 | 0 | |
3171 | 0 | gettime(base, &now); |
3172 | 0 |
|
3173 | 0 | while ((ev = min_heap_top_(&base->timeheap))) { |
3174 | 0 | if (evutil_timercmp(&ev->ev_timeout, &now, >)) |
3175 | 0 | break; |
3176 | 0 | |
3177 | 0 | /* delete this event from the I/O queues */ |
3178 | 0 | event_del_nolock_(ev, EVENT_DEL_NOBLOCK); |
3179 | 0 |
|
3180 | 0 | event_debug(("timeout_process: event: %p, call %p", |
3181 | 0 | ev, ev->ev_callback)); |
3182 | 0 | event_active_nolock_(ev, EV_TIMEOUT, 1); |
3183 | 0 | } |
3184 | 0 | } |
3185 | | |
3186 | | #ifndef MAX |
3187 | 0 | #define MAX(a,b) (((a)>(b))?(a):(b)) |
3188 | | #endif |
3189 | | |
3190 | 0 | #define MAX_EVENT_COUNT(var, v) var = MAX(var, v) |
3191 | | |
3192 | | /* These are a fancy way to spell |
3193 | | if (~flags & EVLIST_INTERNAL) |
3194 | | base->event_count--/++; |
3195 | | */ |
3196 | | #define DECR_EVENT_COUNT(base,flags) \ |
3197 | 0 | ((base)->event_count -= !((flags) & EVLIST_INTERNAL)) |
3198 | 0 | #define INCR_EVENT_COUNT(base,flags) do { \ |
3199 | 0 | ((base)->event_count += !((flags) & EVLIST_INTERNAL)); \ |
3200 | 0 | MAX_EVENT_COUNT((base)->event_count_max, (base)->event_count); \ |
3201 | 0 | } while (0) |
3202 | | |
3203 | | static void |
3204 | | event_queue_remove_inserted(struct event_base *base, struct event *ev) |
3205 | 0 | { |
3206 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3207 | 0 | if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_INSERTED))) { |
3208 | 0 | event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__, |
3209 | 0 | ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_INSERTED); |
3210 | 0 | return; |
3211 | 0 | } |
3212 | 0 | DECR_EVENT_COUNT(base, ev->ev_flags); |
3213 | 0 | ev->ev_flags &= ~EVLIST_INSERTED; |
3214 | 0 | } |
3215 | | static void |
3216 | | event_queue_remove_active(struct event_base *base, struct event_callback *evcb) |
3217 | 0 | { |
3218 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3219 | 0 | if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE))) { |
3220 | 0 | event_errx(1, "%s: %p not on queue %x", __func__, |
3221 | 0 | evcb, EVLIST_ACTIVE); |
3222 | 0 | return; |
3223 | 0 | } |
3224 | 0 | DECR_EVENT_COUNT(base, evcb->evcb_flags); |
3225 | 0 | evcb->evcb_flags &= ~EVLIST_ACTIVE; |
3226 | 0 | base->event_count_active--; |
3227 | 0 |
|
3228 | 0 | TAILQ_REMOVE(&base->activequeues[evcb->evcb_pri], |
3229 | 0 | evcb, evcb_active_next); |
3230 | 0 | } |
3231 | | static void |
3232 | | event_queue_remove_active_later(struct event_base *base, struct event_callback *evcb) |
3233 | 0 | { |
3234 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3235 | 0 | if (EVUTIL_FAILURE_CHECK(!(evcb->evcb_flags & EVLIST_ACTIVE_LATER))) { |
3236 | 0 | event_errx(1, "%s: %p not on queue %x", __func__, |
3237 | 0 | evcb, EVLIST_ACTIVE_LATER); |
3238 | 0 | return; |
3239 | 0 | } |
3240 | 0 | DECR_EVENT_COUNT(base, evcb->evcb_flags); |
3241 | 0 | evcb->evcb_flags &= ~EVLIST_ACTIVE_LATER; |
3242 | 0 | base->event_count_active--; |
3243 | 0 |
|
3244 | 0 | TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next); |
3245 | 0 | } |
3246 | | static void |
3247 | | event_queue_remove_timeout(struct event_base *base, struct event *ev) |
3248 | 0 | { |
3249 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3250 | 0 | if (EVUTIL_FAILURE_CHECK(!(ev->ev_flags & EVLIST_TIMEOUT))) { |
3251 | 0 | event_errx(1, "%s: %p(fd "EV_SOCK_FMT") not on queue %x", __func__, |
3252 | 0 | ev, EV_SOCK_ARG(ev->ev_fd), EVLIST_TIMEOUT); |
3253 | 0 | return; |
3254 | 0 | } |
3255 | 0 | DECR_EVENT_COUNT(base, ev->ev_flags); |
3256 | 0 | ev->ev_flags &= ~EVLIST_TIMEOUT; |
3257 | 0 |
|
3258 | 0 | if (is_common_timeout(&ev->ev_timeout, base)) { |
3259 | 0 | struct common_timeout_list *ctl = |
3260 | 0 | get_common_timeout_list(base, &ev->ev_timeout); |
3261 | 0 | TAILQ_REMOVE(&ctl->events, ev, |
3262 | 0 | ev_timeout_pos.ev_next_with_common_timeout); |
3263 | 0 | } else { |
3264 | 0 | min_heap_erase_(&base->timeheap, ev); |
3265 | 0 | } |
3266 | 0 | } |
3267 | | |
3268 | | #ifdef USE_REINSERT_TIMEOUT |
3269 | | /* Remove and reinsert 'ev' into the timeout queue. */ |
3270 | | static void |
3271 | | event_queue_reinsert_timeout(struct event_base *base, struct event *ev, |
3272 | | int was_common, int is_common, int old_timeout_idx) |
3273 | | { |
3274 | | struct common_timeout_list *ctl; |
3275 | | if (!(ev->ev_flags & EVLIST_TIMEOUT)) { |
3276 | | event_queue_insert_timeout(base, ev); |
3277 | | return; |
3278 | | } |
3279 | | |
3280 | | switch ((was_common<<1) | is_common) { |
3281 | | case 3: /* Changing from one common timeout to another */ |
3282 | | ctl = base->common_timeout_queues[old_timeout_idx]; |
3283 | | TAILQ_REMOVE(&ctl->events, ev, |
3284 | | ev_timeout_pos.ev_next_with_common_timeout); |
3285 | | ctl = get_common_timeout_list(base, &ev->ev_timeout); |
3286 | | insert_common_timeout_inorder(ctl, ev); |
3287 | | break; |
3288 | | case 2: /* Was common; is no longer common */ |
3289 | | ctl = base->common_timeout_queues[old_timeout_idx]; |
3290 | | TAILQ_REMOVE(&ctl->events, ev, |
3291 | | ev_timeout_pos.ev_next_with_common_timeout); |
3292 | | min_heap_push_(&base->timeheap, ev); |
3293 | | break; |
3294 | | case 1: /* Wasn't common; has become common. */ |
3295 | | min_heap_erase_(&base->timeheap, ev); |
3296 | | ctl = get_common_timeout_list(base, &ev->ev_timeout); |
3297 | | insert_common_timeout_inorder(ctl, ev); |
3298 | | break; |
3299 | | case 0: /* was in heap; is still on heap. */ |
3300 | | min_heap_adjust_(&base->timeheap, ev); |
3301 | | break; |
3302 | | default: |
3303 | | EVUTIL_ASSERT(0); /* unreachable */ |
3304 | | break; |
3305 | | } |
3306 | | } |
3307 | | #endif |
3308 | | |
3309 | | /* Add 'ev' to the common timeout list in 'ev'. */ |
3310 | | static void |
3311 | | insert_common_timeout_inorder(struct common_timeout_list *ctl, |
3312 | | struct event *ev) |
3313 | 0 | { |
3314 | 0 | struct event *e; |
3315 | 0 | /* By all logic, we should just be able to append 'ev' to the end of |
3316 | 0 | * ctl->events, since the timeout on each 'ev' is set to {the common |
3317 | 0 | * timeout} + {the time when we add the event}, and so the events |
3318 | 0 | * should arrive in order of their timeeouts. But just in case |
3319 | 0 | * there's some wacky threading issue going on, we do a search from |
3320 | 0 | * the end of 'ev' to find the right insertion point. |
3321 | 0 | */ |
3322 | 0 | TAILQ_FOREACH_REVERSE(e, &ctl->events, |
3323 | 0 | event_list, ev_timeout_pos.ev_next_with_common_timeout) { |
3324 | 0 | /* This timercmp is a little sneaky, since both ev and e have |
3325 | 0 | * magic values in tv_usec. Fortunately, they ought to have |
3326 | 0 | * the _same_ magic values in tv_usec. Let's assert for that. |
3327 | 0 | */ |
3328 | 0 | EVUTIL_ASSERT( |
3329 | 0 | is_same_common_timeout(&e->ev_timeout, &ev->ev_timeout)); |
3330 | 0 | if (evutil_timercmp(&ev->ev_timeout, &e->ev_timeout, >=)) { |
3331 | 0 | TAILQ_INSERT_AFTER(&ctl->events, e, ev, |
3332 | 0 | ev_timeout_pos.ev_next_with_common_timeout); |
3333 | 0 | return; |
3334 | 0 | } |
3335 | 0 | } |
3336 | 0 | TAILQ_INSERT_HEAD(&ctl->events, ev, |
3337 | 0 | ev_timeout_pos.ev_next_with_common_timeout); |
3338 | 0 | } |
3339 | | |
3340 | | static void |
3341 | | event_queue_insert_inserted(struct event_base *base, struct event *ev) |
3342 | 0 | { |
3343 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3344 | 0 |
|
3345 | 0 | if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_INSERTED)) { |
3346 | 0 | event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already inserted", __func__, |
3347 | 0 | ev, EV_SOCK_ARG(ev->ev_fd)); |
3348 | 0 | return; |
3349 | 0 | } |
3350 | 0 | |
3351 | 0 | INCR_EVENT_COUNT(base, ev->ev_flags); |
3352 | 0 |
|
3353 | 0 | ev->ev_flags |= EVLIST_INSERTED; |
3354 | 0 | } |
3355 | | |
3356 | | static void |
3357 | | event_queue_insert_active(struct event_base *base, struct event_callback *evcb) |
3358 | 0 | { |
3359 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3360 | 0 |
|
3361 | 0 | if (evcb->evcb_flags & EVLIST_ACTIVE) { |
3362 | 0 | /* Double insertion is possible for active events */ |
3363 | 0 | return; |
3364 | 0 | } |
3365 | 0 | |
3366 | 0 | INCR_EVENT_COUNT(base, evcb->evcb_flags); |
3367 | 0 |
|
3368 | 0 | evcb->evcb_flags |= EVLIST_ACTIVE; |
3369 | 0 |
|
3370 | 0 | base->event_count_active++; |
3371 | 0 | MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active); |
3372 | 0 | EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); |
3373 | 0 | TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], |
3374 | 0 | evcb, evcb_active_next); |
3375 | 0 | } |
3376 | | |
3377 | | static void |
3378 | | event_queue_insert_active_later(struct event_base *base, struct event_callback *evcb) |
3379 | 0 | { |
3380 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3381 | 0 | if (evcb->evcb_flags & (EVLIST_ACTIVE_LATER|EVLIST_ACTIVE)) { |
3382 | 0 | /* Double insertion is possible */ |
3383 | 0 | return; |
3384 | 0 | } |
3385 | 0 | |
3386 | 0 | INCR_EVENT_COUNT(base, evcb->evcb_flags); |
3387 | 0 | evcb->evcb_flags |= EVLIST_ACTIVE_LATER; |
3388 | 0 | base->event_count_active++; |
3389 | 0 | MAX_EVENT_COUNT(base->event_count_active_max, base->event_count_active); |
3390 | 0 | EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); |
3391 | 0 | TAILQ_INSERT_TAIL(&base->active_later_queue, evcb, evcb_active_next); |
3392 | 0 | } |
3393 | | |
3394 | | static void |
3395 | | event_queue_insert_timeout(struct event_base *base, struct event *ev) |
3396 | 0 | { |
3397 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3398 | 0 |
|
3399 | 0 | if (EVUTIL_FAILURE_CHECK(ev->ev_flags & EVLIST_TIMEOUT)) { |
3400 | 0 | event_errx(1, "%s: %p(fd "EV_SOCK_FMT") already on timeout", __func__, |
3401 | 0 | ev, EV_SOCK_ARG(ev->ev_fd)); |
3402 | 0 | return; |
3403 | 0 | } |
3404 | 0 | |
3405 | 0 | INCR_EVENT_COUNT(base, ev->ev_flags); |
3406 | 0 |
|
3407 | 0 | ev->ev_flags |= EVLIST_TIMEOUT; |
3408 | 0 |
|
3409 | 0 | if (is_common_timeout(&ev->ev_timeout, base)) { |
3410 | 0 | struct common_timeout_list *ctl = |
3411 | 0 | get_common_timeout_list(base, &ev->ev_timeout); |
3412 | 0 | insert_common_timeout_inorder(ctl, ev); |
3413 | 0 | } else { |
3414 | 0 | min_heap_push_(&base->timeheap, ev); |
3415 | 0 | } |
3416 | 0 | } |
3417 | | |
3418 | | static void |
3419 | | event_queue_make_later_events_active(struct event_base *base) |
3420 | 0 | { |
3421 | 0 | struct event_callback *evcb; |
3422 | 0 | EVENT_BASE_ASSERT_LOCKED(base); |
3423 | 0 |
|
3424 | 0 | while ((evcb = TAILQ_FIRST(&base->active_later_queue))) { |
3425 | 0 | TAILQ_REMOVE(&base->active_later_queue, evcb, evcb_active_next); |
3426 | 0 | evcb->evcb_flags = (evcb->evcb_flags & ~EVLIST_ACTIVE_LATER) | EVLIST_ACTIVE; |
3427 | 0 | EVUTIL_ASSERT(evcb->evcb_pri < base->nactivequeues); |
3428 | 0 | TAILQ_INSERT_TAIL(&base->activequeues[evcb->evcb_pri], evcb, evcb_active_next); |
3429 | 0 | base->n_deferreds_queued += (evcb->evcb_closure == EV_CLOSURE_CB_SELF); |
3430 | 0 | } |
3431 | 0 | } |
3432 | | |
3433 | | /* Functions for debugging */ |
3434 | | |
3435 | | const char * |
3436 | | event_get_version(void) |
3437 | 0 | { |
3438 | 0 | return (EVENT__VERSION); |
3439 | 0 | } |
3440 | | |
3441 | | ev_uint32_t |
3442 | | event_get_version_number(void) |
3443 | 0 | { |
3444 | 0 | return (EVENT__NUMERIC_VERSION); |
3445 | 0 | } |
3446 | | |
3447 | | /* |
3448 | | * No thread-safe interface needed - the information should be the same |
3449 | | * for all threads. |
3450 | | */ |
3451 | | |
3452 | | const char * |
3453 | | event_get_method(void) |
3454 | 0 | { |
3455 | 0 | return (current_base->evsel->name); |
3456 | 0 | } |
3457 | | |
3458 | | #ifndef EVENT__DISABLE_MM_REPLACEMENT |
3459 | | static void *(*mm_malloc_fn_)(size_t sz) = NULL; |
3460 | | static void *(*mm_realloc_fn_)(void *p, size_t sz) = NULL; |
3461 | | static void (*mm_free_fn_)(void *p) = NULL; |
3462 | | |
3463 | | void * |
3464 | | event_mm_malloc_(size_t sz) |
3465 | 0 | { |
3466 | 0 | if (sz == 0) |
3467 | 0 | return NULL; |
3468 | 0 | |
3469 | 0 | if (mm_malloc_fn_) |
3470 | 0 | return mm_malloc_fn_(sz); |
3471 | 0 | else |
3472 | 0 | return malloc(sz); |
3473 | 0 | } |
3474 | | |
3475 | | void * |
3476 | | event_mm_calloc_(size_t count, size_t size) |
3477 | 0 | { |
3478 | 0 | if (count == 0 || size == 0) |
3479 | 0 | return NULL; |
3480 | 0 | |
3481 | 0 | if (mm_malloc_fn_) { |
3482 | 0 | size_t sz = count * size; |
3483 | 0 | void *p = NULL; |
3484 | 0 | if (count > EV_SIZE_MAX / size) |
3485 | 0 | goto error; |
3486 | 0 | p = mm_malloc_fn_(sz); |
3487 | 0 | if (p) |
3488 | 0 | return memset(p, 0, sz); |
3489 | 0 | } else { |
3490 | 0 | void *p = calloc(count, size); |
3491 | | #ifdef _WIN32 |
3492 | | /* Windows calloc doesn't reliably set ENOMEM */ |
3493 | | if (p == NULL) |
3494 | | goto error; |
3495 | | #endif |
3496 | | return p; |
3497 | 0 | } |
3498 | 0 |
|
3499 | 0 | error: |
3500 | 0 | errno = ENOMEM; |
3501 | 0 | return NULL; |
3502 | 0 | } |
3503 | | |
3504 | | char * |
3505 | | event_mm_strdup_(const char *str) |
3506 | 0 | { |
3507 | 0 | if (!str) { |
3508 | 0 | errno = EINVAL; |
3509 | 0 | return NULL; |
3510 | 0 | } |
3511 | 0 | |
3512 | 0 | if (mm_malloc_fn_) { |
3513 | 0 | size_t ln = strlen(str); |
3514 | 0 | void *p = NULL; |
3515 | 0 | if (ln == EV_SIZE_MAX) |
3516 | 0 | goto error; |
3517 | 0 | p = mm_malloc_fn_(ln+1); |
3518 | 0 | if (p) |
3519 | 0 | return memcpy(p, str, ln+1); |
3520 | 0 | } else |
3521 | | #ifdef _WIN32 |
3522 | | return _strdup(str); |
3523 | | #else |
3524 | 0 | return strdup(str); |
3525 | 0 | #endif |
3526 | 0 | |
3527 | 0 | error: |
3528 | 0 | errno = ENOMEM; |
3529 | 0 | return NULL; |
3530 | 0 | } |
3531 | | |
3532 | | void * |
3533 | | event_mm_realloc_(void *ptr, size_t sz) |
3534 | 0 | { |
3535 | 0 | if (mm_realloc_fn_) |
3536 | 0 | return mm_realloc_fn_(ptr, sz); |
3537 | 0 | else |
3538 | 0 | return realloc(ptr, sz); |
3539 | 0 | } |
3540 | | |
3541 | | void |
3542 | | event_mm_free_(void *ptr) |
3543 | 0 | { |
3544 | 0 | if (mm_free_fn_) |
3545 | 0 | mm_free_fn_(ptr); |
3546 | 0 | else |
3547 | 0 | free(ptr); |
3548 | 0 | } |
3549 | | |
3550 | | void |
3551 | | event_set_mem_functions(void *(*malloc_fn)(size_t sz), |
3552 | | void *(*realloc_fn)(void *ptr, size_t sz), |
3553 | | void (*free_fn)(void *ptr)) |
3554 | 0 | { |
3555 | 0 | mm_malloc_fn_ = malloc_fn; |
3556 | 0 | mm_realloc_fn_ = realloc_fn; |
3557 | 0 | mm_free_fn_ = free_fn; |
3558 | 0 | } |
3559 | | #endif |
3560 | | |
3561 | | #ifdef EVENT__HAVE_EVENTFD |
3562 | | static void |
3563 | | evthread_notify_drain_eventfd(evutil_socket_t fd, short what, void *arg) |
3564 | 0 | { |
3565 | 0 | ev_uint64_t msg; |
3566 | 0 | ev_ssize_t r; |
3567 | 0 | struct event_base *base = arg; |
3568 | 0 |
|
3569 | 0 | r = read(fd, (void*) &msg, sizeof(msg)); |
3570 | 0 | if (r<0 && errno != EAGAIN) { |
3571 | 0 | event_sock_warn(fd, "Error reading from eventfd"); |
3572 | 0 | } |
3573 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3574 | 0 | base->is_notify_pending = 0; |
3575 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3576 | 0 | } |
3577 | | #endif |
3578 | | |
3579 | | static void |
3580 | | evthread_notify_drain_default(evutil_socket_t fd, short what, void *arg) |
3581 | 0 | { |
3582 | 0 | unsigned char buf[1024]; |
3583 | 0 | struct event_base *base = arg; |
3584 | | #ifdef _WIN32 |
3585 | | while (recv(fd, (char*)buf, sizeof(buf), 0) > 0) |
3586 | | ; |
3587 | | #else |
3588 | 0 | while (read(fd, (char*)buf, sizeof(buf)) > 0) |
3589 | 0 | ; |
3590 | 0 | #endif |
3591 | 0 |
|
3592 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3593 | 0 | base->is_notify_pending = 0; |
3594 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3595 | 0 | } |
3596 | | |
3597 | | int |
3598 | | evthread_make_base_notifiable(struct event_base *base) |
3599 | 0 | { |
3600 | 0 | int r; |
3601 | 0 | if (!base) |
3602 | 0 | return -1; |
3603 | 0 | |
3604 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3605 | 0 | r = evthread_make_base_notifiable_nolock_(base); |
3606 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3607 | 0 | return r; |
3608 | 0 | } |
3609 | | |
3610 | | static int |
3611 | | evthread_make_base_notifiable_nolock_(struct event_base *base) |
3612 | 0 | { |
3613 | 0 | void (*cb)(evutil_socket_t, short, void *); |
3614 | 0 | int (*notify)(struct event_base *); |
3615 | 0 |
|
3616 | 0 | if (base->th_notify_fn != NULL) { |
3617 | 0 | /* The base is already notifiable: we're doing fine. */ |
3618 | 0 | return 0; |
3619 | 0 | } |
3620 | 0 | |
3621 | | #if defined(EVENT__HAVE_WORKING_KQUEUE) |
3622 | | if (base->evsel == &kqops && event_kq_add_notify_event_(base) == 0) { |
3623 | | base->th_notify_fn = event_kq_notify_base_; |
3624 | | /* No need to add an event here; the backend can wake |
3625 | | * itself up just fine. */ |
3626 | | return 0; |
3627 | | } |
3628 | | #endif |
3629 | | |
3630 | 0 | #ifdef EVENT__HAVE_EVENTFD |
3631 | 0 | base->th_notify_fd[0] = evutil_eventfd_(0, |
3632 | 0 | EVUTIL_EFD_CLOEXEC|EVUTIL_EFD_NONBLOCK); |
3633 | 0 | if (base->th_notify_fd[0] >= 0) { |
3634 | 0 | base->th_notify_fd[1] = -1; |
3635 | 0 | notify = evthread_notify_base_eventfd; |
3636 | 0 | cb = evthread_notify_drain_eventfd; |
3637 | 0 | } else |
3638 | 0 | #endif |
3639 | 0 | if (evutil_make_internal_pipe_(base->th_notify_fd) == 0) { |
3640 | 0 | notify = evthread_notify_base_default; |
3641 | 0 | cb = evthread_notify_drain_default; |
3642 | 0 | } else { |
3643 | 0 | return -1; |
3644 | 0 | } |
3645 | 0 | |
3646 | 0 | base->th_notify_fn = notify; |
3647 | 0 |
|
3648 | 0 | /* prepare an event that we can use for wakeup */ |
3649 | 0 | event_assign(&base->th_notify, base, base->th_notify_fd[0], |
3650 | 0 | EV_READ|EV_PERSIST, cb, base); |
3651 | 0 |
|
3652 | 0 | /* we need to mark this as internal event */ |
3653 | 0 | base->th_notify.ev_flags |= EVLIST_INTERNAL; |
3654 | 0 | event_priority_set(&base->th_notify, 0); |
3655 | 0 |
|
3656 | 0 | return event_add_nolock_(&base->th_notify, NULL, 0); |
3657 | 0 | } |
3658 | | |
3659 | | int |
3660 | | event_base_foreach_event_nolock_(struct event_base *base, |
3661 | | event_base_foreach_event_cb fn, void *arg) |
3662 | 0 | { |
3663 | 0 | int r, i; |
3664 | 0 | unsigned u; |
3665 | 0 | struct event *ev; |
3666 | 0 |
|
3667 | 0 | /* Start out with all the EVLIST_INSERTED events. */ |
3668 | 0 | if ((r = evmap_foreach_event_(base, fn, arg))) |
3669 | 0 | return r; |
3670 | 0 | |
3671 | 0 | /* Okay, now we deal with those events that have timeouts and are in |
3672 | 0 | * the min-heap. */ |
3673 | 0 | for (u = 0; u < base->timeheap.n; ++u) { |
3674 | 0 | ev = base->timeheap.p[u]; |
3675 | 0 | if (ev->ev_flags & EVLIST_INSERTED) { |
3676 | 0 | /* we already processed this one */ |
3677 | 0 | continue; |
3678 | 0 | } |
3679 | 0 | if ((r = fn(base, ev, arg))) |
3680 | 0 | return r; |
3681 | 0 | } |
3682 | 0 |
|
3683 | 0 | /* Now for the events in one of the timeout queues. |
3684 | 0 | * the min-heap. */ |
3685 | 0 | for (i = 0; i < base->n_common_timeouts; ++i) { |
3686 | 0 | struct common_timeout_list *ctl = |
3687 | 0 | base->common_timeout_queues[i]; |
3688 | 0 | TAILQ_FOREACH(ev, &ctl->events, |
3689 | 0 | ev_timeout_pos.ev_next_with_common_timeout) { |
3690 | 0 | if (ev->ev_flags & EVLIST_INSERTED) { |
3691 | 0 | /* we already processed this one */ |
3692 | 0 | continue; |
3693 | 0 | } |
3694 | 0 | if ((r = fn(base, ev, arg))) |
3695 | 0 | return r; |
3696 | 0 | } |
3697 | 0 | } |
3698 | 0 |
|
3699 | 0 | /* Finally, we deal wit all the active events that we haven't touched |
3700 | 0 | * yet. */ |
3701 | 0 | for (i = 0; i < base->nactivequeues; ++i) { |
3702 | 0 | struct event_callback *evcb; |
3703 | 0 | TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) { |
3704 | 0 | if ((evcb->evcb_flags & (EVLIST_INIT|EVLIST_INSERTED|EVLIST_TIMEOUT)) != EVLIST_INIT) { |
3705 | 0 | /* This isn't an event (evlist_init clear), or |
3706 | 0 | * we already processed it. (inserted or |
3707 | 0 | * timeout set */ |
3708 | 0 | continue; |
3709 | 0 | } |
3710 | 0 | ev = event_callback_to_event(evcb); |
3711 | 0 | if ((r = fn(base, ev, arg))) |
3712 | 0 | return r; |
3713 | 0 | } |
3714 | 0 | } |
3715 | 0 |
|
3716 | 0 | return 0; |
3717 | 0 | } |
3718 | | |
3719 | | /* Helper for event_base_dump_events: called on each event in the event base; |
3720 | | * dumps only the inserted events. */ |
3721 | | static int |
3722 | | dump_inserted_event_fn(const struct event_base *base, const struct event *e, void *arg) |
3723 | 0 | { |
3724 | 0 | FILE *output = arg; |
3725 | 0 | const char *gloss = (e->ev_events & EV_SIGNAL) ? |
3726 | 0 | "sig" : "fd "; |
3727 | 0 |
|
3728 | 0 | if (! (e->ev_flags & (EVLIST_INSERTED|EVLIST_TIMEOUT))) |
3729 | 0 | return 0; |
3730 | 0 | |
3731 | 0 | fprintf(output, " %p [%s "EV_SOCK_FMT"]%s%s%s%s%s%s", |
3732 | 0 | (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), |
3733 | 0 | (e->ev_events&EV_READ)?" Read":"", |
3734 | 0 | (e->ev_events&EV_WRITE)?" Write":"", |
3735 | 0 | (e->ev_events&EV_CLOSED)?" EOF":"", |
3736 | 0 | (e->ev_events&EV_SIGNAL)?" Signal":"", |
3737 | 0 | (e->ev_events&EV_PERSIST)?" Persist":"", |
3738 | 0 | (e->ev_flags&EVLIST_INTERNAL)?" Internal":""); |
3739 | 0 | if (e->ev_flags & EVLIST_TIMEOUT) { |
3740 | 0 | struct timeval tv; |
3741 | 0 | tv.tv_sec = e->ev_timeout.tv_sec; |
3742 | 0 | tv.tv_usec = e->ev_timeout.tv_usec & MICROSECONDS_MASK; |
3743 | 0 | evutil_timeradd(&tv, &base->tv_clock_diff, &tv); |
3744 | 0 | fprintf(output, " Timeout=%ld.%06d", |
3745 | 0 | (long)tv.tv_sec, (int)(tv.tv_usec & MICROSECONDS_MASK)); |
3746 | 0 | } |
3747 | 0 | fputc('\n', output); |
3748 | 0 |
|
3749 | 0 | return 0; |
3750 | 0 | } |
3751 | | |
3752 | | /* Helper for event_base_dump_events: called on each event in the event base; |
3753 | | * dumps only the active events. */ |
3754 | | static int |
3755 | | dump_active_event_fn(const struct event_base *base, const struct event *e, void *arg) |
3756 | 0 | { |
3757 | 0 | FILE *output = arg; |
3758 | 0 | const char *gloss = (e->ev_events & EV_SIGNAL) ? |
3759 | 0 | "sig" : "fd "; |
3760 | 0 |
|
3761 | 0 | if (! (e->ev_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER))) |
3762 | 0 | return 0; |
3763 | 0 | |
3764 | 0 | fprintf(output, " %p [%s "EV_SOCK_FMT", priority=%d]%s%s%s%s%s active%s%s\n", |
3765 | 0 | (void*)e, gloss, EV_SOCK_ARG(e->ev_fd), e->ev_pri, |
3766 | 0 | (e->ev_res&EV_READ)?" Read":"", |
3767 | 0 | (e->ev_res&EV_WRITE)?" Write":"", |
3768 | 0 | (e->ev_res&EV_CLOSED)?" EOF":"", |
3769 | 0 | (e->ev_res&EV_SIGNAL)?" Signal":"", |
3770 | 0 | (e->ev_res&EV_TIMEOUT)?" Timeout":"", |
3771 | 0 | (e->ev_flags&EVLIST_INTERNAL)?" [Internal]":"", |
3772 | 0 | (e->ev_flags&EVLIST_ACTIVE_LATER)?" [NextTime]":""); |
3773 | 0 |
|
3774 | 0 | return 0; |
3775 | 0 | } |
3776 | | |
3777 | | int |
3778 | | event_base_foreach_event(struct event_base *base, |
3779 | | event_base_foreach_event_cb fn, void *arg) |
3780 | 0 | { |
3781 | 0 | int r; |
3782 | 0 | if ((!fn) || (!base)) { |
3783 | 0 | return -1; |
3784 | 0 | } |
3785 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3786 | 0 | r = event_base_foreach_event_nolock_(base, fn, arg); |
3787 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3788 | 0 | return r; |
3789 | 0 | } |
3790 | | |
3791 | | |
3792 | | void |
3793 | | event_base_dump_events(struct event_base *base, FILE *output) |
3794 | 0 | { |
3795 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3796 | 0 | fprintf(output, "Inserted events:\n"); |
3797 | 0 | event_base_foreach_event_nolock_(base, dump_inserted_event_fn, output); |
3798 | 0 |
|
3799 | 0 | fprintf(output, "Active events:\n"); |
3800 | 0 | event_base_foreach_event_nolock_(base, dump_active_event_fn, output); |
3801 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3802 | 0 | } |
3803 | | |
3804 | | void |
3805 | | event_base_active_by_fd(struct event_base *base, evutil_socket_t fd, short events) |
3806 | 0 | { |
3807 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3808 | 0 | evmap_io_active_(base, fd, events & (EV_READ|EV_WRITE|EV_CLOSED)); |
3809 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3810 | 0 | } |
3811 | | |
3812 | | void |
3813 | | event_base_active_by_signal(struct event_base *base, int sig) |
3814 | 0 | { |
3815 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3816 | 0 | evmap_signal_active_(base, sig, 1); |
3817 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3818 | 0 | } |
3819 | | |
3820 | | |
3821 | | void |
3822 | | event_base_add_virtual_(struct event_base *base) |
3823 | 0 | { |
3824 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3825 | 0 | base->virtual_event_count++; |
3826 | 0 | MAX_EVENT_COUNT(base->virtual_event_count_max, base->virtual_event_count); |
3827 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3828 | 0 | } |
3829 | | |
3830 | | void |
3831 | | event_base_del_virtual_(struct event_base *base) |
3832 | 0 | { |
3833 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3834 | 0 | EVUTIL_ASSERT(base->virtual_event_count > 0); |
3835 | 0 | base->virtual_event_count--; |
3836 | 0 | if (base->virtual_event_count == 0 && EVBASE_NEED_NOTIFY(base)) |
3837 | 0 | evthread_notify_base(base); |
3838 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3839 | 0 | } |
3840 | | |
3841 | | static void |
3842 | | event_free_debug_globals_locks(void) |
3843 | 0 | { |
3844 | 0 | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
3845 | 0 | #ifndef EVENT__DISABLE_DEBUG_MODE |
3846 | 0 | if (event_debug_map_lock_ != NULL) { |
3847 | 0 | EVTHREAD_FREE_LOCK(event_debug_map_lock_, 0); |
3848 | 0 | event_debug_map_lock_ = NULL; |
3849 | 0 | evthreadimpl_disable_lock_debugging_(); |
3850 | 0 | } |
3851 | 0 | #endif /* EVENT__DISABLE_DEBUG_MODE */ |
3852 | 0 | #endif /* EVENT__DISABLE_THREAD_SUPPORT */ |
3853 | 0 | return; |
3854 | 0 | } |
3855 | | |
3856 | | static void |
3857 | | event_free_debug_globals(void) |
3858 | 0 | { |
3859 | 0 | event_free_debug_globals_locks(); |
3860 | 0 | } |
3861 | | |
3862 | | static void |
3863 | | event_free_evsig_globals(void) |
3864 | 0 | { |
3865 | 0 | evsig_free_globals_(); |
3866 | 0 | } |
3867 | | |
3868 | | static void |
3869 | | event_free_evutil_globals(void) |
3870 | 0 | { |
3871 | 0 | evutil_free_globals_(); |
3872 | 0 | } |
3873 | | |
3874 | | static void |
3875 | | event_free_globals(void) |
3876 | 0 | { |
3877 | 0 | event_free_debug_globals(); |
3878 | 0 | event_free_evsig_globals(); |
3879 | 0 | event_free_evutil_globals(); |
3880 | 0 | } |
3881 | | |
3882 | | void |
3883 | | libevent_global_shutdown(void) |
3884 | 0 | { |
3885 | 0 | event_disable_debug_mode(); |
3886 | 0 | event_free_globals(); |
3887 | 0 | } |
3888 | | |
3889 | | #ifndef EVENT__DISABLE_THREAD_SUPPORT |
3890 | | int |
3891 | | event_global_setup_locks_(const int enable_locks) |
3892 | 0 | { |
3893 | 0 | #ifndef EVENT__DISABLE_DEBUG_MODE |
3894 | 0 | EVTHREAD_SETUP_GLOBAL_LOCK(event_debug_map_lock_, 0); |
3895 | 0 | #endif |
3896 | 0 | if (evsig_global_setup_locks_(enable_locks) < 0) |
3897 | 0 | return -1; |
3898 | 0 | if (evutil_global_setup_locks_(enable_locks) < 0) |
3899 | 0 | return -1; |
3900 | 0 | if (evutil_secure_rng_global_setup_locks_(enable_locks) < 0) |
3901 | 0 | return -1; |
3902 | 0 | return 0; |
3903 | 0 | } |
3904 | | #endif |
3905 | | |
3906 | | void |
3907 | | event_base_assert_ok_(struct event_base *base) |
3908 | 0 | { |
3909 | 0 | EVBASE_ACQUIRE_LOCK(base, th_base_lock); |
3910 | 0 | event_base_assert_ok_nolock_(base); |
3911 | 0 | EVBASE_RELEASE_LOCK(base, th_base_lock); |
3912 | 0 | } |
3913 | | |
3914 | | void |
3915 | | event_base_assert_ok_nolock_(struct event_base *base) |
3916 | 0 | { |
3917 | 0 | int i; |
3918 | 0 | int count; |
3919 | 0 |
|
3920 | 0 | /* First do checks on the per-fd and per-signal lists */ |
3921 | 0 | evmap_check_integrity_(base); |
3922 | 0 |
|
3923 | 0 | /* Check the heap property */ |
3924 | 0 | for (i = 1; i < (int)base->timeheap.n; ++i) { |
3925 | 0 | int parent = (i - 1) / 2; |
3926 | 0 | struct event *ev, *p_ev; |
3927 | 0 | ev = base->timeheap.p[i]; |
3928 | 0 | p_ev = base->timeheap.p[parent]; |
3929 | 0 | EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT); |
3930 | 0 | EVUTIL_ASSERT(evutil_timercmp(&p_ev->ev_timeout, &ev->ev_timeout, <=)); |
3931 | 0 | EVUTIL_ASSERT(ev->ev_timeout_pos.min_heap_idx == i); |
3932 | 0 | } |
3933 | 0 |
|
3934 | 0 | /* Check that the common timeouts are fine */ |
3935 | 0 | for (i = 0; i < base->n_common_timeouts; ++i) { |
3936 | 0 | struct common_timeout_list *ctl = base->common_timeout_queues[i]; |
3937 | 0 | struct event *last=NULL, *ev; |
3938 | 0 |
|
3939 | 0 | EVUTIL_ASSERT_TAILQ_OK(&ctl->events, event, ev_timeout_pos.ev_next_with_common_timeout); |
3940 | 0 |
|
3941 | 0 | TAILQ_FOREACH(ev, &ctl->events, ev_timeout_pos.ev_next_with_common_timeout) { |
3942 | 0 | if (last) |
3943 | 0 | EVUTIL_ASSERT(evutil_timercmp(&last->ev_timeout, &ev->ev_timeout, <=)); |
3944 | 0 | EVUTIL_ASSERT(ev->ev_flags & EVLIST_TIMEOUT); |
3945 | 0 | EVUTIL_ASSERT(is_common_timeout(&ev->ev_timeout,base)); |
3946 | 0 | EVUTIL_ASSERT(COMMON_TIMEOUT_IDX(&ev->ev_timeout) == i); |
3947 | 0 | last = ev; |
3948 | 0 | } |
3949 | 0 | } |
3950 | 0 |
|
3951 | 0 | /* Check the active queues. */ |
3952 | 0 | count = 0; |
3953 | 0 | for (i = 0; i < base->nactivequeues; ++i) { |
3954 | 0 | struct event_callback *evcb; |
3955 | 0 | EVUTIL_ASSERT_TAILQ_OK(&base->activequeues[i], event_callback, evcb_active_next); |
3956 | 0 | TAILQ_FOREACH(evcb, &base->activequeues[i], evcb_active_next) { |
3957 | 0 | EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE); |
3958 | 0 | EVUTIL_ASSERT(evcb->evcb_pri == i); |
3959 | 0 | ++count; |
3960 | 0 | } |
3961 | 0 | } |
3962 | 0 |
|
3963 | 0 | { |
3964 | 0 | struct event_callback *evcb; |
3965 | 0 | TAILQ_FOREACH(evcb, &base->active_later_queue, evcb_active_next) { |
3966 | 0 | EVUTIL_ASSERT((evcb->evcb_flags & (EVLIST_ACTIVE|EVLIST_ACTIVE_LATER)) == EVLIST_ACTIVE_LATER); |
3967 | 0 | ++count; |
3968 | 0 | } |
3969 | 0 | } |
3970 | 0 | EVUTIL_ASSERT(count == base->event_count_active); |
3971 | 0 | } |