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
-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;
VLOG(5) << "EventBase(): Starting loop.";
int res = 0;
bool ranLoopCallbacks;
+ bool blocking = !(flags & EVLOOP_NONBLOCK);
+ bool once = (flags & EVLOOP_ONCE);
loopThread_.store(pthread_self(), std::memory_order_release);
loopThread_.store(pthread_self(), std::memory_order_release);
// nobody can add loop callbacks from within this thread if
// we don't have to handle anything to start with...
// 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<std::chrono::microseconds>(
ranLoopCallbacks = runLoopCallbacks();
int64_t busy = std::chrono::duration_cast<std::chrono::microseconds>(
/**
* Wait for some events to become active, run them, then return.
*
/**
* 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().
*/
* This is useful for callers that want to run the loop manually.
*
* Returns the same result as loop().
*/
+ bool loopOnce(int flags = 0);
/**
* Runs the event loop.
/**
* Runs the event loop.
typedef LoopCallback::List LoopCallbackList;
class FunctionRunner;
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);
// executes any callbacks queued by runInLoop(); returns false if none found
bool runLoopCallbacks(bool setContext = true);