From: Nick Mathewson Date: Thu, 1 Oct 2009 15:29:08 +0000 (+0000) Subject: Apply Ka-Hing Cheung's event_base_got_[break|exit] patch, with locking and whitespace... X-Git-Tag: release-2.0.3-alpha~105 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d5b640fc16e9d9e1e709ec73eb938de9eeff3f2c;p=libevent Apply Ka-Hing Cheung's event_base_got_[break|exit] patch, with locking and whitespace fixes. svn:r1438 --- diff --git a/ChangeLog b/ChangeLog index ff45f1a5..d1a6e6c6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -23,7 +23,8 @@ Changes in 2.0.3-alpha: o Treat an event with a negative fd as valid but untriggerable by Libevent. This is useful for applications that want to manually activate events. o Fix compilation on Android, which forgot to define fd_mask in its sys/select.h o Do not drop data from evbuffer when out of memory; reported by Jacek Masiulaniec - + o New event_base_got_exit() and event_base_got_break() functions to tell whether an event loop exited because of an event_base_loopexit() or an event_base_loopbreak(). Patch from Ka-Hing Cheung. + Changes in 2.0.2-alpha: o Add a new flag to bufferevents to make all callbacks automatically deferred. diff --git a/event.c b/event.c index 6a509e3f..0884ffcc 100644 --- a/event.c +++ b/event.c @@ -830,7 +830,25 @@ event_base_loopbreak(struct event_base *event_base) } } +int +event_base_got_break(struct event_base *event_base) +{ + int res; + EVBASE_ACQUIRE_LOCK(event_base, EVTHREAD_READ, th_base_lock); + res = event_base->event_break; + EVBASE_RELEASE_LOCK(event_base, EVTHREAD_READ, th_base_lock); + return res; +} +int +event_base_got_exit(struct event_base *event_base) +{ + int res; + EVBASE_ACQUIRE_LOCK(event_base, EVTHREAD_READ, th_base_lock); + res = event_base->event_gotterm; + EVBASE_RELEASE_LOCK(event_base, EVTHREAD_READ, th_base_lock); + return res; +} /* not thread safe */ @@ -859,15 +877,15 @@ event_base_loop(struct event_base *base, int flags) base->th_owner_id = EVTHREAD_GET_ID(); #endif + base->event_gotterm = base->event_break = 0; + while (!done) { /* Terminate the loop if we have been asked to */ if (base->event_gotterm) { - base->event_gotterm = 0; break; } if (base->event_break) { - base->event_break = 0; break; } diff --git a/include/event2/event.h b/include/event2/event.h index 77cb4c5f..8fe1ba56 100644 --- a/include/event2/event.h +++ b/include/event2/event.h @@ -300,6 +300,34 @@ int event_base_loopexit(struct event_base *, const struct timeval *); */ int event_base_loopbreak(struct event_base *); +/** + Checks if the event loop was told to exit by event_loopexit(). + + This function will return true for an event_base at every point after + event_loopexit() is called, until the event loop is next entered. + + @param eb the event_base structure returned by event_init() + @return true if event_base_loopexit() was called on this event base, + or 0 otherwise + @see event_base_loopexit + @see event_base_got_break + */ +int event_base_got_exit(struct event_base *); + +/** + Checks if the event loop was told to abort immediately by event_loopbreak(). + + This function will return true for an event_base at every point after + event_loopbreak() is called, until the event loop is next entered. + + @param eb the event_base structure returned by event_init() + @return true if event_base_loopbreak() was called on this event base, + or 0 otherwise + @see event_base_loopbreak + @see event_base_got_exit + */ +int event_base_got_break(struct event_base *); + /* Flags to pass to event_set(), event_new(), event_assign(), * event_pending(), and anything else with an argument of the form * "short events" */