Add loopOnce() method to folly::EventBase
authorStepan Palamarchuk <stepan@fb.com>
Mon, 31 Mar 2014 21:19:54 +0000 (14:19 -0700)
committerSara Golemon <sgolemon@fb.com>
Fri, 4 Apr 2014 02:54:15 +0000 (19:54 -0700)
Summary: Add loopOnce() method which provides functionality similar to event_base_loop(base, EVLOOP_ONCE);

Test Plan: fbmake

Reviewed By: simpkins@fb.com

FB internal diff: D1249753

folly/io/async/EventBase.cpp
folly/io/async/EventBase.h

index 68a42f58ef2306cbfda8322abab5ce27a34ce363..6e860406473f0d47c78b96a40c6d8855f5d3004a 100644 (file)
@@ -223,6 +223,14 @@ void EventBase::waitUntilRunning() {
 
 // enters the event_base loop -- will only exit when forced to
 bool EventBase::loop() {
 
 // enters the event_base loop -- will only exit when forced to
 bool EventBase::loop() {
+  return loopBody();
+}
+
+bool EventBase::loopOnce() {
+  return loopBody(true);
+}
+
+bool EventBase::loopBody(bool once) {
   VLOG(5) << "EventBase(): Starting loop.";
   int res = 0;
   bool ranLoopCallbacks;
   VLOG(5) << "EventBase(): Starting loop.";
   int res = 0;
   bool ranLoopCallbacks;
@@ -302,6 +310,10 @@ bool EventBase::loop() {
 
     VLOG(5) << "EventBase " << this << " loop time: " <<
       getTimeDelta(&prev).count();
 
     VLOG(5) << "EventBase " << this << " loop time: " <<
       getTimeDelta(&prev).count();
+
+    if (once) {
+      break;
+    }
   }
   // Reset stop_ so loop() can be called again
   stop_ = false;
   }
   // Reset stop_ so loop() can be called again
   stop_ = false;
index fa9a3fe5df685ca7d29599f7ea6928d5e9c994c4..46c84b8d9a803bb2a1d287f22ca7452ff76a090c 100644 (file)
@@ -143,6 +143,15 @@ class EventBase : private boost::noncopyable, public TimeoutManager {
    */
   bool loop();
 
    */
   bool loop();
 
+  /**
+   * Wait for some events to become active, run them, then return.
+   *
+   * This is useful for callers that want to run the loop manually.
+   *
+   * Returns the same result as loop().
+   */
+  bool loopOnce();
+
   /**
    * Runs the event loop.
    *
   /**
    * Runs the event loop.
    *
@@ -479,6 +488,8 @@ class EventBase : private boost::noncopyable, public TimeoutManager {
   typedef LoopCallback::List LoopCallbackList;
   class FunctionRunner;
 
   typedef LoopCallback::List LoopCallbackList;
   class FunctionRunner;
 
+  bool loopBody(bool once = false);
+
   // executes any callbacks queued by runInLoop(); returns false if none found
   bool runLoopCallbacks(bool setContext = true);
 
   // executes any callbacks queued by runInLoop(); returns false if none found
   bool runLoopCallbacks(bool setContext = true);