Tweak patch for event_base_foreach_event()

* Fix whitespace
* Explain return value from callback function
* Reinstate return value so that caller can tell whether forech
  exited early.
* Rename event_base_foreach_event_() to
  event_base_foreach_event_nolock_().
* Use event_base_foreach_event_cb_fn typedef in more places
* Be more dire about undefined behavior.
This commit is contained in:
Nick Mathewson 2012-09-07 09:58:24 -04:00
parent 84fd6d7506
commit 232055ef49
4 changed files with 24 additions and 20 deletions

View File

@ -400,9 +400,6 @@ void event_base_assert_ok_(struct event_base *base);
void event_base_assert_ok_nolock_(struct event_base *base); void event_base_assert_ok_nolock_(struct event_base *base);
/* Callback type for event_base_foreach_event. */
//typedef int (*event_base_foreach_event_cb)(struct event_base *base, struct event *, void *);
/* Helper function: Call 'fn' exactly once every inserted or active event in /* Helper function: Call 'fn' exactly once every inserted or active event in
* the event_base 'base'. * the event_base 'base'.
* *
@ -411,7 +408,7 @@ void event_base_assert_ok_nolock_(struct event_base *base);
* *
* Requires that 'base' be locked. * Requires that 'base' be locked.
*/ */
int event_base_foreach_event_(struct event_base *base, int event_base_foreach_event_nolock_(struct event_base *base,
event_base_foreach_event_cb cb, void *arg); event_base_foreach_event_cb cb, void *arg);
#ifdef __cplusplus #ifdef __cplusplus

12
event.c
View File

@ -3193,7 +3193,7 @@ evthread_make_base_notifiable_nolock_(struct event_base *base)
} }
int int
event_base_foreach_event_(struct event_base *base, event_base_foreach_event_nolock_(struct event_base *base,
event_base_foreach_event_cb fn, void *arg) event_base_foreach_event_cb fn, void *arg)
{ {
int r, i; int r, i;
@ -3308,16 +3308,18 @@ dump_active_event_fn(const struct event_base *base, const struct event *e, void
return 0; return 0;
} }
void int
event_base_foreach_event(struct event_base *base, event_base_foreach_event(struct event_base *base,
event_base_foreach_event_cb fn, void *arg) event_base_foreach_event_cb fn, void *arg)
{ {
int r;
if ((!fn) || (!base)) { if ((!fn) || (!base)) {
return; return;
} }
EVBASE_ACQUIRE_LOCK(base, th_base_lock); EVBASE_ACQUIRE_LOCK(base, th_base_lock);
event_base_foreach_event_(base, fn, arg); r = event_base_foreach_event_nolock_(base, fn, arg);
EVBASE_RELEASE_LOCK(base, th_base_lock); EVBASE_RELEASE_LOCK(base, th_base_lock);
return r;
} }
@ -3326,10 +3328,10 @@ event_base_dump_events(struct event_base *base, FILE *output)
{ {
EVBASE_ACQUIRE_LOCK(base, th_base_lock); EVBASE_ACQUIRE_LOCK(base, th_base_lock);
fprintf(output, "Inserted events:\n"); fprintf(output, "Inserted events:\n");
event_base_foreach_event_(base, dump_inserted_event_fn, output); event_base_foreach_event_nolock_(base, dump_inserted_event_fn, output);
fprintf(output, "Active events:\n"); fprintf(output, "Active events:\n");
event_base_foreach_event_(base, dump_active_event_fn, output); event_base_foreach_event_nolock_(base, dump_active_event_fn, output);
EVBASE_RELEASE_LOCK(base, th_base_lock); EVBASE_RELEASE_LOCK(base, th_base_lock);
} }

View File

@ -963,7 +963,7 @@ evmap_check_integrity_(struct event_base *base)
/* Helper type for evmap_foreach_event_: Bundles a function to call on every /* Helper type for evmap_foreach_event_: Bundles a function to call on every
* event, and the user-provided void* to use as its third argument. */ * event, and the user-provided void* to use as its third argument. */
struct evmap_foreach_event_helper { struct evmap_foreach_event_helper {
int (*fn)(const struct event_base *, const struct event *, void *); event_base_foreach_event_cb fn;
void *arg; void *arg;
}; };
@ -1001,7 +1001,7 @@ evmap_signal_foreach_event_fn(struct event_base *base, int signum,
int int
evmap_foreach_event_(struct event_base *base, evmap_foreach_event_(struct event_base *base,
int (*fn)(const struct event_base *, const struct event *, void *), void *arg) event_base_foreach_event_cb fn, void *arg)
{ {
struct evmap_foreach_event_helper h; struct evmap_foreach_event_helper h;
int r; int r;

View File

@ -1327,23 +1327,28 @@ void event_base_dump_events(struct event_base *, FILE *);
/** /**
* callback for iterating events in an event base via event_base_foreach_event * Callback for iterating events in an event base via event_base_foreach_event
*/ */
typedef int (*event_base_foreach_event_cb)(const struct event_base *, const struct event *, void *); typedef int (*event_base_foreach_event_cb)(const struct event_base *, const struct event *, void *);
/** /**
Iterate all current events in a given event loop. The method is an Iterate over all added or active events events in an event loop, and invoke
alternative to event_base_dump_events, but provides a native interface a given callback on each one.
towards the events.
Modification of events during iteration is an invalid operation The callback must not call any function that modifies the event base, or
and may lead to unexpected behaviour modifies any event in the event base. Doing so is unsupported and
will lead to undefined behavior.
The callback function must return 0 to continue iteration, or some other
integer to stop iterating.
@param base An event_base on which to scan the events. @param base An event_base on which to scan the events.
@param fn A callback function to receive the events. @param fn A callback function to receive the events.
@param arg An argument passed to the callback function.
@return 0 if we iterated over every event, or the value returned by the
callback function if the loop exited early.
*/ */
void event_base_foreach_event(struct event_base *base, event_base_foreach_event_cb fn, void *arg); int event_base_foreach_event(struct event_base *base, event_base_foreach_event_cb fn, void *arg);
/** Sets 'tv' to the current time (as returned by gettimeofday()), /** Sets 'tv' to the current time (as returned by gettimeofday()),