From: Yunqi Zhang Date: Thu, 19 Jun 2014 01:40:19 +0000 (-0700) Subject: Expose EVLOOP_NONBLOCK X-Git-Tag: v0.22.0~505 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=51e98fa77aceb9e5bba5cff4907c1b315d40d8db;p=folly.git Expose EVLOOP_NONBLOCK Summary: This diff allows users to loop through EventBase without blocking if there are not any events to process. This is useful for sending and receiving requests on network, where users just want to try if there are any events and do not want to block if not. https://phabricator.fb.com/D1373887 is an example where we find this feature useful, otherwise we have to add an empty callback before loop. event_base_.runInLoop([] {}); event_base_.loopOnce(); @davejwatson, @fugalh, @simpkins, @stepan: Could you please take a look at the proposed changes and let me know if there is any better ways of doing this. Thank you! Test Plan: I think this would not break anything, but we might want to do some performance profiling if needed. Reviewed By: hans@fb.com Subscribers: simpkins, davejwatson, fugalh, stepan, folly@lists FB internal diff: D1383401 --- diff --git a/folly/io/async/EventBase.cpp b/folly/io/async/EventBase.cpp index ab34b7c3..b548d177 100644 --- a/folly/io/async/EventBase.cpp +++ b/folly/io/async/EventBase.cpp @@ -246,15 +246,16 @@ bool EventBase::loop() { return loopBody(); } -bool EventBase::loopOnce() { - return loopBody(true); +bool EventBase::loopOnce(int flags) { + return loopBody(flags | EVLOOP_ONCE); } -bool EventBase::loopBody(bool once) { +bool EventBase::loopBody(int flags) { VLOG(5) << "EventBase(): Starting loop."; int res = 0; bool ranLoopCallbacks; - int nonBlocking; + bool blocking = !(flags & EVLOOP_NONBLOCK); + bool once = (flags & EVLOOP_ONCE); loopThread_.store(pthread_self(), std::memory_order_release); @@ -272,8 +273,11 @@ bool EventBase::loopBody(bool once) { // nobody can add loop callbacks from within this thread if // we don't have to handle anything to start with... - nonBlocking = (loopCallbacks_.empty() ? 0 : EVLOOP_NONBLOCK); - res = event_base_loop(evb_, EVLOOP_ONCE | nonBlocking); + if (blocking && loopCallbacks_.empty()) { + res = event_base_loop(evb_, EVLOOP_ONCE); + } else { + res = event_base_loop(evb_, EVLOOP_ONCE | EVLOOP_NONBLOCK); + } ranLoopCallbacks = runLoopCallbacks(); int64_t busy = std::chrono::duration_cast( diff --git a/folly/io/async/EventBase.h b/folly/io/async/EventBase.h index 660b1abd..c1fd81c2 100644 --- a/folly/io/async/EventBase.h +++ b/folly/io/async/EventBase.h @@ -146,11 +146,14 @@ class EventBase : private boost::noncopyable, public TimeoutManager { /** * Wait for some events to become active, run them, then return. * + * When EVLOOP_NONBLOCK is set in flags, the loop won't block if there + * are not any events to process. + * * This is useful for callers that want to run the loop manually. * * Returns the same result as loop(). */ - bool loopOnce(); + bool loopOnce(int flags = 0); /** * Runs the event loop. @@ -502,7 +505,7 @@ class EventBase : private boost::noncopyable, public TimeoutManager { typedef LoopCallback::List LoopCallbackList; class FunctionRunner; - bool loopBody(bool once = false); + bool loopBody(int flags = 0); // executes any callbacks queued by runInLoop(); returns false if none found bool runLoopCallbacks(bool setContext = true);