Add wrappers for event_base_loopexit and event_base_loopbreak.

This commit is contained in:
Nick Mathewson 2017-10-03 11:09:44 -04:00
parent f0d2733b46
commit 39cb04335f
7 changed files with 52 additions and 19 deletions

View File

@ -363,6 +363,38 @@ tor_libevent_free_all(void)
the_event_base = NULL;
}
/**
* Run the event loop for the provided event_base, handling events until
* something stops it. If <b>once</b> is set, then just poll-and-run
* once, then exit. Return 0 on success, -1 if an error occurred, or 1
* if we exited because no events were pending or active.
*
* This isn't reentrant or multithreaded.
*/
int
tor_libevent_run_event_loop(struct event_base *base, int once)
{
const int flags = once ? EVLOOP_ONCE : 0;
return event_base_loop(base, flags);
}
/** Tell the event loop to exit after <b>delay</b>. If <b>delay</b> is NULL,
* instead exit after we're done running the currently active events. */
void
tor_libevent_exit_loop_after_delay(struct event_base *base,
const struct timeval *delay)
{
event_base_loopexit(base, delay);
}
/** Tell the event loop to exit after running whichever callback is currently
* active. */
void
tor_libevent_exit_loop_after_callback(struct event_base *base)
{
event_base_loopbreak(base);
}
#if defined(LIBEVENT_VERSION_NUMBER) && \
LIBEVENT_VERSION_NUMBER >= V(2,1,1) && \
!defined(TOR_UNIT_TESTS)

View File

@ -45,9 +45,6 @@ void mainloop_event_free_(mainloop_event_t *event);
#define mainloop_event_free(event) \
FREE_AND_NULL(mainloop_event_t, mainloop_event_free_, (event))
#define tor_event_base_loopexit event_base_loopexit
#define tor_event_base_loopbreak event_base_loopbreak
/** Defines a configuration for using libevent with Tor: passed as an argument
* to tor_libevent_initialize() to describe how we want to set up. */
typedef struct tor_libevent_cfg {
@ -75,6 +72,10 @@ void tor_gettimeofday_cache_set(const struct timeval *tv);
void tor_libevent_postfork(void);
#endif
void tor_libevent_exit_loop_after_delay(struct event_base *base,
const struct timeval *delay);
void tor_libevent_exit_loop_after_callback(struct event_base *base);
#ifdef COMPAT_LIBEVENT_PRIVATE
/** Macro: returns the number of a Libevent version as a 4-byte number,

View File

@ -719,7 +719,7 @@ tell_event_loop_to_run_external_code(void)
{
if (!called_loop_once) {
struct timeval tv = { 0, 0 };
tor_event_base_loopexit(tor_libevent_get_base(), &tv);
tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), &tv);
called_loop_once = 1; /* hack to avoid adding more exit events */
}
}
@ -779,8 +779,9 @@ tor_shutdown_event_loop_and_exit(int exitcode)
shutdown_did_not_work_callback, NULL);
event_add(shutdown_did_not_work_event, &ten_seconds);
/* Unlike loopexit, loopbreak prevents other callbacks from running. */
tor_event_base_loopbreak(tor_libevent_get_base());
/* Unlike exit_loop_after_delay(), exit_loop_after_callback
* prevents other callbacks from running. */
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
}
/** Return true iff tor_shutdown_event_loop_and_exit() has been called. */

View File

@ -24,8 +24,6 @@
#include "main.h"
#include "ntmain.h"
#include <event2/event.h>
#include <windows.h>
#define GENSRV_SERVICENAME "tor"
#define GENSRV_DISPLAYNAME "Tor Win32 Service"
@ -245,7 +243,8 @@ nt_service_control(DWORD request)
log_notice(LD_GENERAL,
"Got stop/shutdown request; shutting down cleanly.");
service_status.dwCurrentState = SERVICE_STOP_PENDING;
event_base_loopexit(tor_libevent_get_base(), &exit_now);
tor_libevent_exit_loop_after_delay(tor_libevent_get_base(),
&exit_now);
return;
}
service_fns.SetServiceStatus_fn(hStatus, &service_status);

View File

@ -50,7 +50,7 @@ timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
// printf("%d / %d\n",n_fired, N_TIMERS);
if (n_fired == n_active_timers) {
event_base_loopbreak(tor_libevent_get_base());
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
}
}

View File

@ -65,7 +65,7 @@ mock_channel_write_cell_relay2(channel_t *chan, cell_t *cell)
(void)chan;
tried_to_write_cell++;
channel_tls_handle_cell(cell, ((channel_tls_t*)relay1_relay2)->conn);
event_base_loopbreak(tor_libevent_get_base());
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
return 0;
}
@ -75,7 +75,7 @@ mock_channel_write_cell_relay1(channel_t *chan, cell_t *cell)
(void)chan;
tried_to_write_cell++;
channel_tls_handle_cell(cell, ((channel_tls_t*)relay2_relay1)->conn);
event_base_loopbreak(tor_libevent_get_base());
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
return 0;
}
@ -85,7 +85,7 @@ mock_channel_write_cell_relay3(channel_t *chan, cell_t *cell)
(void)chan;
tried_to_write_cell++;
channel_tls_handle_cell(cell, ((channel_tls_t*)client_relay3)->conn);
event_base_loopbreak(tor_libevent_get_base());
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
return 0;
}
@ -95,7 +95,7 @@ mock_channel_write_cell_client(channel_t *chan, cell_t *cell)
(void)chan;
tried_to_write_cell++;
channel_tls_handle_cell(cell, ((channel_tls_t*)relay3_client)->conn);
event_base_loopbreak(tor_libevent_get_base());
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
return 0;
}
@ -105,7 +105,7 @@ mock_channel_write_cell(channel_t *chan, cell_t *cell)
tried_to_write_cell++;
channel_tls_handle_cell(cell, ((channel_tls_t*)chan)->conn);
if (!dont_stop_libevent)
event_base_loopbreak(tor_libevent_get_base());
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
return 0;
}
@ -246,7 +246,7 @@ static void
dummy_timer_cb(tor_timer_t *t, void *arg, const monotime_t *now_mono)
{
(void)t; (void)arg; (void)now_mono;
event_base_loopbreak(tor_libevent_get_base());
tor_libevent_exit_loop_after_callback(tor_libevent_get_base());
return;
}

View File

@ -230,7 +230,7 @@ add_n_work_items(threadpool_t *tp, int n)
ent = add_work(tp);
if (! ent) {
puts("Z");
tor_event_base_loopexit(tor_libevent_get_base(), NULL);
tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), NULL);
return -1;
}
if (n_try_cancel < opt_n_cancel &&
@ -308,7 +308,7 @@ replysock_readable_cb(tor_socket_t sock, short what, void *arg)
handle_reply_shutdown, NULL);
{
struct timeval limit = { 2, 0 };
tor_event_base_loopexit(tor_libevent_get_base(), &limit);
tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), &limit);
}
}
}
@ -433,7 +433,7 @@ main(int argc, char **argv)
{
struct timeval limit = { 180, 0 };
tor_event_base_loopexit(tor_libevent_get_base(), &limit);
tor_libevent_exit_loop_after_delay(tor_libevent_get_base(), &limit);
}
event_base_loop(tor_libevent_get_base(), 0);