2 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "event2/event-config.h"
27 #include "evconfig-private.h"
31 #define WIN32_LEAN_AND_MEAN
33 #undef WIN32_LEAN_AND_MEAN
35 #include <sys/types.h>
36 #if !defined(_WIN32) && defined(EVENT__HAVE_SYS_TIME_H)
39 #include <sys/queue.h>
50 #include "event-internal.h"
51 #include "evmap-internal.h"
52 #include "mm-internal.h"
53 #include "changelist-internal.h"
55 /** An entry for an evmap_io list: notes all the events that want to read or
56 write on a given fd, and the number of each.
59 struct event_dlist events;
65 /* An entry for an evmap_signal list: notes all the events that want to know
66 when a signal triggers. */
68 struct event_dlist events;
71 /* On some platforms, fds start at 0 and increment by 1 as they are
72 allocated, and old numbers get used. For these platforms, we
73 implement io maps just like signal maps: as an array of pointers to
74 struct evmap_io. But on other platforms (windows), sockets are not
75 0-indexed, not necessarily consecutive, and not necessarily reused.
76 There, we use a hashtable to implement evmap_io.
79 struct event_map_entry {
80 HT_ENTRY(event_map_entry) map_node;
82 union { /* This is a union in case we need to make more things that can
83 be in the hashtable. */
84 struct evmap_io evmap_io;
88 /* Helper used by the event_io_map hashtable code; tries to return a good hash
89 * of the fd in e->fd. */
90 static inline unsigned
91 hashsocket(struct event_map_entry *e)
93 /* On win32, in practice, the low 2-3 bits of a SOCKET seem not to
94 * matter. Our hashtable implementation really likes low-order bits,
95 * though, so let's do the rotate-and-add trick. */
96 unsigned h = (unsigned) e->fd;
97 h += (h >> 2) | (h << 30);
101 /* Helper used by the event_io_map hashtable code; returns true iff e1 and e2
102 * have the same e->fd. */
104 eqsocket(struct event_map_entry *e1, struct event_map_entry *e2)
106 return e1->fd == e2->fd;
109 HT_PROTOTYPE(event_io_map, event_map_entry, map_node, hashsocket, eqsocket)
110 HT_GENERATE(event_io_map, event_map_entry, map_node, hashsocket, eqsocket,
111 0.5, mm_malloc, mm_realloc, mm_free)
113 #define GET_IO_SLOT(x, map, slot, type) \
115 struct event_map_entry key_, *ent_; \
117 ent_ = HT_FIND(event_io_map, map, &key_); \
118 (x) = ent_ ? &ent_->ent.type : NULL; \
121 #define GET_IO_SLOT_AND_CTOR(x, map, slot, type, ctor, fdinfo_len) \
123 struct event_map_entry key_, *ent_; \
125 HT_FIND_OR_INSERT_(event_io_map, map_node, hashsocket, map, \
126 event_map_entry, &key_, ptr, \
131 ent_ = mm_calloc(1,sizeof(struct event_map_entry)+fdinfo_len); \
132 if (EVUTIL_UNLIKELY(ent_ == NULL)) \
135 (ctor)(&ent_->ent.type); \
136 HT_FOI_INSERT_(map_node, map, &key_, ent_, ptr) \
138 (x) = &ent_->ent.type; \
141 void evmap_io_initmap_(struct event_io_map *ctx)
143 HT_INIT(event_io_map, ctx);
146 void evmap_io_clear_(struct event_io_map *ctx)
148 struct event_map_entry **ent, **next, *this;
149 for (ent = HT_START(event_io_map, ctx); ent; ent = next) {
151 next = HT_NEXT_RMV(event_io_map, ctx, ent);
154 HT_CLEAR(event_io_map, ctx); /* remove all storage held by the ctx. */
158 /* Set the variable 'x' to the field in event_map 'map' with fields of type
159 'struct type *' corresponding to the fd or signal 'slot'. Set 'x' to NULL
160 if there are no entries for 'slot'. Does no bounds-checking. */
161 #define GET_SIGNAL_SLOT(x, map, slot, type) \
162 (x) = (struct type *)((map)->entries[slot])
163 /* As GET_SLOT, but construct the entry for 'slot' if it is not present,
164 by allocating enough memory for a 'struct type', and initializing the new
165 value by calling the function 'ctor' on it. Makes the function
166 return -1 on allocation failure.
168 #define GET_SIGNAL_SLOT_AND_CTOR(x, map, slot, type, ctor, fdinfo_len) \
170 if ((map)->entries[slot] == NULL) { \
171 (map)->entries[slot] = \
172 mm_calloc(1,sizeof(struct type)+fdinfo_len); \
173 if (EVUTIL_UNLIKELY((map)->entries[slot] == NULL)) \
175 (ctor)((struct type *)(map)->entries[slot]); \
177 (x) = (struct type *)((map)->entries[slot]); \
180 /* If we aren't using hashtables, then define the IO_SLOT macros and functions
181 as thin aliases over the SIGNAL_SLOT versions. */
183 #define GET_IO_SLOT(x,map,slot,type) GET_SIGNAL_SLOT(x,map,slot,type)
184 #define GET_IO_SLOT_AND_CTOR(x,map,slot,type,ctor,fdinfo_len) \
185 GET_SIGNAL_SLOT_AND_CTOR(x,map,slot,type,ctor,fdinfo_len)
186 #define FDINFO_OFFSET sizeof(struct evmap_io)
188 evmap_io_initmap_(struct event_io_map* ctx)
190 evmap_signal_initmap_(ctx);
193 evmap_io_clear_(struct event_io_map* ctx)
195 evmap_signal_clear_(ctx);
200 /** Expand 'map' with new entries of width 'msize' until it is big enough
201 to store a value in 'slot'.
204 evmap_make_space(struct event_signal_map *map, int slot, int msize)
206 if (map->nentries <= slot) {
207 int nentries = map->nentries ? map->nentries : 32;
210 while (nentries <= slot)
213 tmp = (void **)mm_realloc(map->entries, nentries * msize);
217 memset(&tmp[map->nentries], 0,
218 (nentries - map->nentries) * msize);
220 map->nentries = nentries;
228 evmap_signal_initmap_(struct event_signal_map *ctx)
235 evmap_signal_clear_(struct event_signal_map *ctx)
237 if (ctx->entries != NULL) {
239 for (i = 0; i < ctx->nentries; ++i) {
240 if (ctx->entries[i] != NULL)
241 mm_free(ctx->entries[i]);
243 mm_free(ctx->entries);
250 /* code specific to file descriptors */
252 /** Constructor for struct evmap_io */
254 evmap_io_init(struct evmap_io *entry)
256 LIST_INIT(&entry->events);
263 /* return -1 on error, 0 on success if nothing changed in the event backend,
264 * and 1 on success if something did. */
266 evmap_io_add_(struct event_base *base, evutil_socket_t fd, struct event *ev)
268 const struct eventop *evsel = base->evsel;
269 struct event_io_map *io = &base->io;
270 struct evmap_io *ctx = NULL;
271 int nread, nwrite, nclose, retval = 0;
272 short res = 0, old = 0;
273 struct event *old_ev;
275 EVUTIL_ASSERT(fd == ev->ev_fd);
281 if (fd >= io->nentries) {
282 if (evmap_make_space(io, fd, sizeof(struct evmap_io *)) == -1)
286 GET_IO_SLOT_AND_CTOR(ctx, io, fd, evmap_io, evmap_io_init,
290 nwrite = ctx->nwrite;
291 nclose = ctx->nclose;
300 if (ev->ev_events & EV_READ) {
304 if (ev->ev_events & EV_WRITE) {
308 if (ev->ev_events & EV_CLOSED) {
312 if (EVUTIL_UNLIKELY(nread > 0xffff || nwrite > 0xffff || nclose > 0xffff)) {
313 event_warnx("Too many events reading or writing on fd %d",
317 if (EVENT_DEBUG_MODE_IS_ON() &&
318 (old_ev = LIST_FIRST(&ctx->events)) &&
319 (old_ev->ev_events&EV_ET) != (ev->ev_events&EV_ET)) {
320 event_warnx("Tried to mix edge-triggered and non-edge-triggered"
321 " events on fd %d", (int)fd);
326 void *extra = ((char*)ctx) + sizeof(struct evmap_io);
327 /* XXX(niels): we cannot mix edge-triggered and
328 * level-triggered, we should probably assert on
330 if (evsel->add(base, ev->ev_fd,
331 old, (ev->ev_events & EV_ET) | res, extra) == -1)
336 ctx->nread = (ev_uint16_t) nread;
337 ctx->nwrite = (ev_uint16_t) nwrite;
338 ctx->nclose = (ev_uint16_t) nclose;
339 LIST_INSERT_HEAD(&ctx->events, ev, ev_io_next);
344 /* return -1 on error, 0 on success if nothing changed in the event backend,
345 * and 1 on success if something did. */
347 evmap_io_del_(struct event_base *base, evutil_socket_t fd, struct event *ev)
349 const struct eventop *evsel = base->evsel;
350 struct event_io_map *io = &base->io;
351 struct evmap_io *ctx;
352 int nread, nwrite, nclose, retval = 0;
353 short res = 0, old = 0;
358 EVUTIL_ASSERT(fd == ev->ev_fd);
361 if (fd >= io->nentries)
365 GET_IO_SLOT(ctx, io, fd, evmap_io);
368 nwrite = ctx->nwrite;
369 nclose = ctx->nclose;
378 if (ev->ev_events & EV_READ) {
381 EVUTIL_ASSERT(nread >= 0);
383 if (ev->ev_events & EV_WRITE) {
386 EVUTIL_ASSERT(nwrite >= 0);
388 if (ev->ev_events & EV_CLOSED) {
391 EVUTIL_ASSERT(nclose >= 0);
395 void *extra = ((char*)ctx) + sizeof(struct evmap_io);
396 if (evsel->del(base, ev->ev_fd, old, res, extra) == -1) {
404 ctx->nwrite = nwrite;
405 ctx->nclose = nclose;
406 LIST_REMOVE(ev, ev_io_next);
412 evmap_io_active_(struct event_base *base, evutil_socket_t fd, short events)
414 struct event_io_map *io = &base->io;
415 struct evmap_io *ctx;
419 if (fd < 0 || fd >= io->nentries)
422 GET_IO_SLOT(ctx, io, fd, evmap_io);
426 LIST_FOREACH(ev, &ctx->events, ev_io_next) {
427 if (ev->ev_events & events)
428 event_active_nolock_(ev, ev->ev_events & events, 1);
432 /* code specific to signals */
435 evmap_signal_init(struct evmap_signal *entry)
437 LIST_INIT(&entry->events);
442 evmap_signal_add_(struct event_base *base, int sig, struct event *ev)
444 const struct eventop *evsel = base->evsigsel;
445 struct event_signal_map *map = &base->sigmap;
446 struct evmap_signal *ctx = NULL;
448 if (sig >= map->nentries) {
449 if (evmap_make_space(
450 map, sig, sizeof(struct evmap_signal *)) == -1)
453 GET_SIGNAL_SLOT_AND_CTOR(ctx, map, sig, evmap_signal, evmap_signal_init,
454 base->evsigsel->fdinfo_len);
456 if (LIST_EMPTY(&ctx->events)) {
457 if (evsel->add(base, ev->ev_fd, 0, EV_SIGNAL, NULL)
462 LIST_INSERT_HEAD(&ctx->events, ev, ev_signal_next);
468 evmap_signal_del_(struct event_base *base, int sig, struct event *ev)
470 const struct eventop *evsel = base->evsigsel;
471 struct event_signal_map *map = &base->sigmap;
472 struct evmap_signal *ctx;
474 if (sig >= map->nentries)
477 GET_SIGNAL_SLOT(ctx, map, sig, evmap_signal);
479 LIST_REMOVE(ev, ev_signal_next);
481 if (LIST_FIRST(&ctx->events) == NULL) {
482 if (evsel->del(base, ev->ev_fd, 0, EV_SIGNAL, NULL) == -1)
490 evmap_signal_active_(struct event_base *base, evutil_socket_t sig, int ncalls)
492 struct event_signal_map *map = &base->sigmap;
493 struct evmap_signal *ctx;
496 if (sig < 0 || sig >= map->nentries)
498 GET_SIGNAL_SLOT(ctx, map, sig, evmap_signal);
502 LIST_FOREACH(ev, &ctx->events, ev_signal_next)
503 event_active_nolock_(ev, EV_SIGNAL, ncalls);
507 evmap_io_get_fdinfo_(struct event_io_map *map, evutil_socket_t fd)
509 struct evmap_io *ctx;
510 GET_IO_SLOT(ctx, map, fd, evmap_io);
512 return ((char*)ctx) + sizeof(struct evmap_io);
517 /* Callback type for evmap_io_foreach_fd */
518 typedef int (*evmap_io_foreach_fd_cb)(
519 struct event_base *, evutil_socket_t, struct evmap_io *, void *);
521 /* Multipurpose helper function: Iterate over every file descriptor event_base
522 * for which we could have EV_READ or EV_WRITE events. For each such fd, call
523 * fn(base, signum, evmap_io, arg), where fn is the user-provided
524 * function, base is the event_base, signum is the signal number, evmap_io
525 * is an evmap_io structure containing a list of events pending on the
526 * file descriptor, and arg is the user-supplied argument.
528 * If fn returns 0, continue on to the next signal. Otherwise, return the same
529 * value that fn returned.
531 * Note that there is no guarantee that the file descriptors will be processed
532 * in any particular order.
535 evmap_io_foreach_fd(struct event_base *base,
536 evmap_io_foreach_fd_cb fn,
540 struct event_io_map *iomap = &base->io;
543 struct event_map_entry **mapent;
544 HT_FOREACH(mapent, event_io_map, iomap) {
545 struct evmap_io *ctx = &(*mapent)->ent.evmap_io;
548 for (fd = 0; fd < iomap->nentries; ++fd) {
549 struct evmap_io *ctx = iomap->entries[fd];
553 if ((r = fn(base, fd, ctx, arg)))
559 /* Callback type for evmap_signal_foreach_signal */
560 typedef int (*evmap_signal_foreach_signal_cb)(
561 struct event_base *, int, struct evmap_signal *, void *);
563 /* Multipurpose helper function: Iterate over every signal number in the
564 * event_base for which we could have signal events. For each such signal,
565 * call fn(base, signum, evmap_signal, arg), where fn is the user-provided
566 * function, base is the event_base, signum is the signal number, evmap_signal
567 * is an evmap_signal structure containing a list of events pending on the
568 * signal, and arg is the user-supplied argument.
570 * If fn returns 0, continue on to the next signal. Otherwise, return the same
571 * value that fn returned.
574 evmap_signal_foreach_signal(struct event_base *base,
575 evmap_signal_foreach_signal_cb fn,
578 struct event_signal_map *sigmap = &base->sigmap;
582 for (signum = 0; signum < sigmap->nentries; ++signum) {
583 struct evmap_signal *ctx = sigmap->entries[signum];
586 if ((r = fn(base, signum, ctx, arg)))
592 /* Helper for evmap_reinit_: tell the backend to add every fd for which we have
593 * pending events, with the appropriate combination of EV_READ, EV_WRITE, and
596 evmap_io_reinit_iter_fn(struct event_base *base, evutil_socket_t fd,
597 struct evmap_io *ctx, void *arg)
599 const struct eventop *evsel = base->evsel;
606 extra = ((char*)ctx) + sizeof(struct evmap_io);
613 if (evsel->fdinfo_len)
614 memset(extra, 0, evsel->fdinfo_len);
616 (ev = LIST_FIRST(&ctx->events)) &&
617 (ev->ev_events & EV_ET))
619 if (evsel->add(base, fd, 0, events, extra) == -1)
625 /* Helper for evmap_reinit_: tell the backend to add every signal for which we
626 * have pending events. */
628 evmap_signal_reinit_iter_fn(struct event_base *base,
629 int signum, struct evmap_signal *ctx, void *arg)
631 const struct eventop *evsel = base->evsigsel;
634 if (!LIST_EMPTY(&ctx->events)) {
635 if (evsel->add(base, signum, 0, EV_SIGNAL, NULL) == -1)
642 evmap_reinit_(struct event_base *base)
646 evmap_io_foreach_fd(base, evmap_io_reinit_iter_fn, &result);
649 evmap_signal_foreach_signal(base, evmap_signal_reinit_iter_fn, &result);
655 /* Helper for evmap_delete_all_: delete every event in an event_dlist. */
657 delete_all_in_dlist(struct event_dlist *dlist)
660 while ((ev = LIST_FIRST(dlist)))
665 /* Helper for evmap_delete_all_: delete every event pending on an fd. */
667 evmap_io_delete_all_iter_fn(struct event_base *base, evutil_socket_t fd,
668 struct evmap_io *io_info, void *arg)
670 return delete_all_in_dlist(&io_info->events);
673 /* Helper for evmap_delete_all_: delete every event pending on a signal. */
675 evmap_signal_delete_all_iter_fn(struct event_base *base, int signum,
676 struct evmap_signal *sig_info, void *arg)
678 return delete_all_in_dlist(&sig_info->events);
682 evmap_delete_all_(struct event_base *base)
684 evmap_signal_foreach_signal(base, evmap_signal_delete_all_iter_fn, NULL);
685 evmap_io_foreach_fd(base, evmap_io_delete_all_iter_fn, NULL);
688 /** Per-fd structure for use with changelists. It keeps track, for each fd or
689 * signal using the changelist, of where its entry in the changelist is.
691 struct event_changelist_fdinfo {
692 int idxplus1; /* this is the index +1, so that memset(0) will make it
693 * a no-such-element */
697 event_changelist_init_(struct event_changelist *changelist)
699 changelist->changes = NULL;
700 changelist->changes_size = 0;
701 changelist->n_changes = 0;
704 /** Helper: return the changelist_fdinfo corresponding to a given change. */
705 static inline struct event_changelist_fdinfo *
706 event_change_get_fdinfo(struct event_base *base,
707 const struct event_change *change)
710 if (change->read_change & EV_CHANGE_SIGNAL) {
711 struct evmap_signal *ctx;
712 GET_SIGNAL_SLOT(ctx, &base->sigmap, change->fd, evmap_signal);
713 ptr = ((char*)ctx) + sizeof(struct evmap_signal);
715 struct evmap_io *ctx;
716 GET_IO_SLOT(ctx, &base->io, change->fd, evmap_io);
717 ptr = ((char*)ctx) + sizeof(struct evmap_io);
722 /** Callback helper for event_changelist_assert_ok */
724 event_changelist_assert_ok_foreach_iter_fn(
725 struct event_base *base,
726 evutil_socket_t fd, struct evmap_io *io, void *arg)
728 struct event_changelist *changelist = &base->changelist;
729 struct event_changelist_fdinfo *f;
731 ( ((char*)io) + sizeof(struct evmap_io) );
733 struct event_change *c = &changelist->changes[f->idxplus1 - 1];
734 EVUTIL_ASSERT(c->fd == fd);
739 /** Make sure that the changelist is consistent with the evmap structures. */
741 event_changelist_assert_ok(struct event_base *base)
744 struct event_changelist *changelist = &base->changelist;
746 EVUTIL_ASSERT(changelist->changes_size >= changelist->n_changes);
747 for (i = 0; i < changelist->n_changes; ++i) {
748 struct event_change *c = &changelist->changes[i];
749 struct event_changelist_fdinfo *f;
750 EVUTIL_ASSERT(c->fd >= 0);
751 f = event_change_get_fdinfo(base, c);
753 EVUTIL_ASSERT(f->idxplus1 == i + 1);
756 evmap_io_foreach_fd(base,
757 event_changelist_assert_ok_foreach_iter_fn,
761 #ifdef DEBUG_CHANGELIST
762 #define event_changelist_check(base) event_changelist_assert_ok((base))
764 #define event_changelist_check(base) ((void)0)
768 event_changelist_remove_all_(struct event_changelist *changelist,
769 struct event_base *base)
773 event_changelist_check(base);
775 for (i = 0; i < changelist->n_changes; ++i) {
776 struct event_change *ch = &changelist->changes[i];
777 struct event_changelist_fdinfo *fdinfo =
778 event_change_get_fdinfo(base, ch);
779 EVUTIL_ASSERT(fdinfo->idxplus1 == i + 1);
780 fdinfo->idxplus1 = 0;
783 changelist->n_changes = 0;
785 event_changelist_check(base);
789 event_changelist_freemem_(struct event_changelist *changelist)
791 if (changelist->changes)
792 mm_free(changelist->changes);
793 event_changelist_init_(changelist); /* zero it all out. */
796 /** Increase the size of 'changelist' to hold more changes. */
798 event_changelist_grow(struct event_changelist *changelist)
801 struct event_change *new_changes;
802 if (changelist->changes_size < 64)
805 new_size = changelist->changes_size * 2;
807 new_changes = mm_realloc(changelist->changes,
808 new_size * sizeof(struct event_change));
810 if (EVUTIL_UNLIKELY(new_changes == NULL))
813 changelist->changes = new_changes;
814 changelist->changes_size = new_size;
819 /** Return a pointer to the changelist entry for the file descriptor or signal
820 * 'fd', whose fdinfo is 'fdinfo'. If none exists, construct it, setting its
821 * old_events field to old_events.
823 static struct event_change *
824 event_changelist_get_or_construct(struct event_changelist *changelist,
827 struct event_changelist_fdinfo *fdinfo)
829 struct event_change *change;
831 if (fdinfo->idxplus1 == 0) {
833 EVUTIL_ASSERT(changelist->n_changes <= changelist->changes_size);
835 if (changelist->n_changes == changelist->changes_size) {
836 if (event_changelist_grow(changelist) < 0)
840 idx = changelist->n_changes++;
841 change = &changelist->changes[idx];
842 fdinfo->idxplus1 = idx + 1;
844 memset(change, 0, sizeof(struct event_change));
846 change->old_events = old_events;
848 change = &changelist->changes[fdinfo->idxplus1 - 1];
849 EVUTIL_ASSERT(change->fd == fd);
855 event_changelist_add_(struct event_base *base, evutil_socket_t fd, short old, short events,
858 struct event_changelist *changelist = &base->changelist;
859 struct event_changelist_fdinfo *fdinfo = p;
860 struct event_change *change;
862 event_changelist_check(base);
864 change = event_changelist_get_or_construct(changelist, fd, old, fdinfo);
868 /* An add replaces any previous delete, but doesn't result in a no-op,
869 * since the delete might fail (because the fd had been closed since
870 * the last add, for instance. */
872 if (events & (EV_READ|EV_SIGNAL)) {
873 change->read_change = EV_CHANGE_ADD |
874 (events & (EV_ET|EV_PERSIST|EV_SIGNAL));
876 if (events & EV_WRITE) {
877 change->write_change = EV_CHANGE_ADD |
878 (events & (EV_ET|EV_PERSIST|EV_SIGNAL));
880 if (events & EV_CLOSED) {
881 change->close_change = EV_CHANGE_ADD |
882 (events & (EV_ET|EV_PERSIST|EV_SIGNAL));
885 event_changelist_check(base);
890 event_changelist_del_(struct event_base *base, evutil_socket_t fd, short old, short events,
893 struct event_changelist *changelist = &base->changelist;
894 struct event_changelist_fdinfo *fdinfo = p;
895 struct event_change *change;
897 event_changelist_check(base);
898 change = event_changelist_get_or_construct(changelist, fd, old, fdinfo);
899 event_changelist_check(base);
903 /* A delete on an event set that doesn't contain the event to be
904 deleted produces a no-op. This effectively emoves any previous
905 uncommitted add, rather than replacing it: on those platforms where
906 "add, delete, dispatch" is not the same as "no-op, dispatch", we
907 want the no-op behavior.
909 If we have a no-op item, we could remove it it from the list
910 entirely, but really there's not much point: skipping the no-op
911 change when we do the dispatch later is far cheaper than rejuggling
914 As this stands, it also lets through deletions of events that are
918 if (events & (EV_READ|EV_SIGNAL)) {
919 if (!(change->old_events & (EV_READ | EV_SIGNAL)))
920 change->read_change = 0;
922 change->read_change = EV_CHANGE_DEL;
924 if (events & EV_WRITE) {
925 if (!(change->old_events & EV_WRITE))
926 change->write_change = 0;
928 change->write_change = EV_CHANGE_DEL;
930 if (events & EV_CLOSED) {
931 if (!(change->old_events & EV_CLOSED))
932 change->close_change = 0;
934 change->close_change = EV_CHANGE_DEL;
937 event_changelist_check(base);
941 /* Helper for evmap_check_integrity_: verify that all of the events pending on
942 * given fd are set up correctly, and that the nread and nwrite counts on that
945 evmap_io_check_integrity_fn(struct event_base *base, evutil_socket_t fd,
946 struct evmap_io *io_info, void *arg)
949 int n_read = 0, n_write = 0, n_close = 0;
951 /* First, make sure the list itself isn't corrupt. Otherwise,
952 * running LIST_FOREACH could be an exciting adventure. */
953 EVUTIL_ASSERT_LIST_OK(&io_info->events, event, ev_io_next);
955 LIST_FOREACH(ev, &io_info->events, ev_io_next) {
956 EVUTIL_ASSERT(ev->ev_flags & EVLIST_INSERTED);
957 EVUTIL_ASSERT(ev->ev_fd == fd);
958 EVUTIL_ASSERT(!(ev->ev_events & EV_SIGNAL));
959 EVUTIL_ASSERT((ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)));
960 if (ev->ev_events & EV_READ)
962 if (ev->ev_events & EV_WRITE)
964 if (ev->ev_events & EV_CLOSED)
968 EVUTIL_ASSERT(n_read == io_info->nread);
969 EVUTIL_ASSERT(n_write == io_info->nwrite);
970 EVUTIL_ASSERT(n_close == io_info->nclose);
975 /* Helper for evmap_check_integrity_: verify that all of the events pending
976 * on given signal are set up correctly. */
978 evmap_signal_check_integrity_fn(struct event_base *base,
979 int signum, struct evmap_signal *sig_info, void *arg)
982 /* First, make sure the list itself isn't corrupt. */
983 EVUTIL_ASSERT_LIST_OK(&sig_info->events, event, ev_signal_next);
985 LIST_FOREACH(ev, &sig_info->events, ev_io_next) {
986 EVUTIL_ASSERT(ev->ev_flags & EVLIST_INSERTED);
987 EVUTIL_ASSERT(ev->ev_fd == signum);
988 EVUTIL_ASSERT((ev->ev_events & EV_SIGNAL));
989 EVUTIL_ASSERT(!(ev->ev_events & (EV_READ|EV_WRITE|EV_CLOSED)));
995 evmap_check_integrity_(struct event_base *base)
997 evmap_io_foreach_fd(base, evmap_io_check_integrity_fn, NULL);
998 evmap_signal_foreach_signal(base, evmap_signal_check_integrity_fn, NULL);
1000 if (base->evsel->add == event_changelist_add_)
1001 event_changelist_assert_ok(base);
1004 /* Helper type for evmap_foreach_event_: Bundles a function to call on every
1005 * event, and the user-provided void* to use as its third argument. */
1006 struct evmap_foreach_event_helper {
1007 event_base_foreach_event_cb fn;
1011 /* Helper for evmap_foreach_event_: calls a provided function on every event
1012 * pending on a given fd. */
1014 evmap_io_foreach_event_fn(struct event_base *base, evutil_socket_t fd,
1015 struct evmap_io *io_info, void *arg)
1017 struct evmap_foreach_event_helper *h = arg;
1020 LIST_FOREACH(ev, &io_info->events, ev_io_next) {
1021 if ((r = h->fn(base, ev, h->arg)))
1027 /* Helper for evmap_foreach_event_: calls a provided function on every event
1028 * pending on a given signal. */
1030 evmap_signal_foreach_event_fn(struct event_base *base, int signum,
1031 struct evmap_signal *sig_info, void *arg)
1034 struct evmap_foreach_event_helper *h = arg;
1036 LIST_FOREACH(ev, &sig_info->events, ev_signal_next) {
1037 if ((r = h->fn(base, ev, h->arg)))
1044 evmap_foreach_event_(struct event_base *base,
1045 event_base_foreach_event_cb fn, void *arg)
1047 struct evmap_foreach_event_helper h;
1051 if ((r = evmap_io_foreach_fd(base, evmap_io_foreach_event_fn, &h)))
1053 return evmap_signal_foreach_signal(base, evmap_signal_foreach_event_fn, &h);