Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions folly/io/async/EventBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,29 +851,36 @@ void EventBase::terminateLoopSoon() {
queue_->putMessage([] {});
}

void EventBase::addLoopCallback(LoopCallback& callback, bool thisIteration) {
if (runOnceCallbacks_ != nullptr && thisIteration) {
runOnceCallbacks_->push_back(callback);
} else {
loopCallbacks_.push_back(callback);
// If we're adding to loopCallbacks_ while the loop is running (but not
// inside runLoopCallbacks), we need to break out of eb_event_base_loop()
// so the callback can be processed promptly. Otherwise the loop may be
// blocked in epoll_wait and won't process the callback until it times out.
if (isRunning()) {
evb_->eb_event_base_loopbreak();
}
}
}

void EventBase::runInLoop(
LoopCallback* callback,
bool thisIteration,
std::shared_ptr<RequestContext> rctx) {
dcheckIsInEventBaseThread();
callback->cancelLoopCallback();
callback->context_ = std::move(rctx);
if (runOnceCallbacks_ != nullptr && thisIteration) {
runOnceCallbacks_->push_back(*callback);
} else {
loopCallbacks_.push_back(*callback);
}
addLoopCallback(*callback, thisIteration);
}

void EventBase::runInLoop(Func cob, bool thisIteration) {
dcheckIsInEventBaseThread();
auto wrapper = new FunctionLoopCallback(std::move(cob));
wrapper->context_ = RequestContext::saveContext();
if (runOnceCallbacks_ != nullptr && thisIteration) {
runOnceCallbacks_->push_back(*wrapper);
} else {
loopCallbacks_.push_back(*wrapper);
}
addLoopCallback(*wrapper, thisIteration);
}

void EventBase::runOnDestruction(OnDestructionCallback& callback) {
Expand Down
4 changes: 4 additions & 0 deletions folly/io/async/EventBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,10 @@ class EventBase
LoopCallbackList& currentCallbacks,
const LoopCallbacksDeadline& deadline);

// Helper to add a callback to the appropriate list (runOnceCallbacks_ or
// loopCallbacks_) and break out of epoll_wait if needed.
void addLoopCallback(LoopCallback& callback, bool thisIteration);

// executes any callbacks queued by runInLoop(); returns false if none found
bool runLoopCallbacks();

Expand Down
Loading