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);
// 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>(
/**
* 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.
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);