From 2d08baf60cbd36d4bd0a112b574a8b755eeaa638 Mon Sep 17 00:00:00 2001 From: Stepan Palamarchuk Date: Mon, 31 Mar 2014 14:19:54 -0700 Subject: [PATCH] Add loopOnce() method to folly::EventBase 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 | 12 ++++++++++++ folly/io/async/EventBase.h | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp index 68a42f58..6e860406 100644 --- a/folly/io/async/EventBase.cpp +++ b/folly/io/async/EventBase.cpp @@ -223,6 +223,14 @@ void EventBase::waitUntilRunning() { // 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; @@ -302,6 +310,10 @@ bool EventBase::loop() { VLOG(5) << "EventBase " << this << " loop time: " << getTimeDelta(&prev).count(); + + if (once) { + break; + } } // Reset stop_ so loop() can be called again stop_ = false; diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index fa9a3fe5..46c84b8d 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -143,6 +143,15 @@ class EventBase : private boost::noncopyable, public TimeoutManager { */ 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. * @@ -479,6 +488,8 @@ class EventBase : private boost::noncopyable, public TimeoutManager { 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); -- 2.34.1