[tor-commits] [tor/master] Add wrappers for event_base_loopexit and event_base_loopbreak.

nickm at torproject.org nickm at torproject.org
Fri Apr 6 12:51:50 UTC 2018


commit 39cb04335f9fd5c3268c808bac549f531fe389a3
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Oct 3 11:09:44 2017 -0400

    Add wrappers for event_base_loopexit and event_base_loopbreak.
---
 src/common/compat_libevent.c   | 32 ++++++++++++++++++++++++++++++++
 src/common/compat_libevent.h   |  7 ++++---
 src/or/main.c                  |  7 ++++---
 src/or/ntmain.c                |  5 ++---
 src/test/test-timers.c         |  2 +-
 src/test/test_channelpadding.c | 12 ++++++------
 src/test/test_workqueue.c      |  6 +++---
 7 files changed, 52 insertions(+), 19 deletions(-)

diff --git a/src/common/compat_libevent.c b/src/common/compat_libevent.c
index f453a8e87..cb311ea46 100644
--- a/src/common/compat_libevent.c
+++ b/src/common/compat_libevent.c
@@ -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)
diff --git a/src/common/compat_libevent.h b/src/common/compat_libevent.h
index 711c3a2eb..86bc77ba5 100644
--- a/src/common/compat_libevent.h
+++ b/src/common/compat_libevent.h
@@ -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,
diff --git a/src/or/main.c b/src/or/main.c
index 9f26732a2..9a8ac3088 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -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. */
diff --git a/src/or/ntmain.c b/src/or/ntmain.c
index ebbe0018b..e9a299807 100644
--- a/src/or/ntmain.c
+++ b/src/or/ntmain.c
@@ -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);
diff --git a/src/test/test-timers.c b/src/test/test-timers.c
index a0b5b535c..1b4466ecc 100644
--- a/src/test/test-timers.c
+++ b/src/test/test-timers.c
@@ -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());
   }
 }
 
diff --git a/src/test/test_channelpadding.c b/src/test/test_channelpadding.c
index 9e570b81a..072a347de 100644
--- a/src/test/test_channelpadding.c
+++ b/src/test/test_channelpadding.c
@@ -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;
 }
 
diff --git a/src/test/test_workqueue.c b/src/test/test_workqueue.c
index 2b0317371..de7884ff9 100644
--- a/src/test/test_workqueue.c
+++ b/src/test/test_workqueue.c
@@ -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);





More information about the tor-commits mailing list