- // Restore the notification queue internal flag
- fnRunner_->stopConsuming();
- fnRunner_->startConsumingInternal(this, queue_.get());
+void EventBase::applyLoopKeepAlive() {
+ auto keepAliveCount = loopKeepAliveCount();
+ // Make sure default VirtualEventBase won't hold EventBase::loop() forever.
+ if (virtualEventBase_ && virtualEventBase_->keepAliveCount() == 1) {
+ --keepAliveCount;
+ }
+
+ if (loopKeepAliveActive_ && keepAliveCount == 0) {
+ // Restore the notification queue internal flag
+ fnRunner_->stopConsuming();
+ fnRunner_->startConsumingInternal(this, queue_.get());
+ loopKeepAliveActive_ = false;
+ } else if (!loopKeepAliveActive_ && keepAliveCount > 0) {
+ // Update the notification queue event to treat it as a normal
+ // (non-internal) event. The notification queue event always remains
+ // installed, and the main loop won't exit with it installed.
+ fnRunner_->stopConsuming();
+ fnRunner_->startConsuming(this, queue_.get());
+ loopKeepAliveActive_ = true;
+ }
+}
+
+void EventBase::loopForever() {
+ bool ret;
+ {
+ SCOPE_EXIT {
+ applyLoopKeepAlive();
+ };
+ // Make sure notification queue events are treated as normal events.
+ // We can't use loopKeepAlive() here since LoopKeepAlive token can only be
+ // released inside a loop.
+ ++loopKeepAliveCount_;
+ SCOPE_EXIT {
+ --loopKeepAliveCount_;
+ };
+ ret = loop();
+ }