text
stringlengths 0
2.2M
|
---|
std::memory_order_release,
|
std::memory_order_acquire)) {
|
break;
|
}
|
assume(
|
state == State::OnlyCallback ||
|
state == State::OnlyCallbackAllowInline);
|
FOLLY_FALLTHROUGH;
|
case State::OnlyCallback:
|
case State::OnlyCallbackAllowInline:
|
proxyCallback(state);
|
break;
|
case State::OnlyResult:
|
case State::Proxy:
|
case State::Done:
|
case State::Empty:
|
default:
|
terminate_with<std::logic_error>("setCallback unexpected state");
|
}
|
detachOne();
|
}
|
// May be called at most once.
|
void CoreBase::doCallback(
|
Executor::KeepAlive<>&& completingKA, State priorState) {
|
DCHECK(state_ == State::Done);
|
auto executor = std::exchange(executor_, KeepAliveOrDeferred{});
|
// Customise inline behaviour
|
// If addCompletingKA is non-null, then we are allowing inline execution
|
auto doAdd = [](Executor::KeepAlive<>&& addCompletingKA,
|
KeepAliveOrDeferred&& currentExecutor,
|
auto&& keepAliveFunc) mutable {
|
if (auto deferredExecutorPtr = currentExecutor.getDeferredExecutor()) {
|
deferredExecutorPtr->addFrom(
|
std::move(addCompletingKA), std::move(keepAliveFunc));
|
} else {
|
// If executors match call inline
|
auto currentKeepAlive = std::move(currentExecutor).stealKeepAlive();
|
if (addCompletingKA.get() == currentKeepAlive.get()) {
|
keepAliveFunc(std::move(currentKeepAlive));
|
} else {
|
std::move(currentKeepAlive).add(std::move(keepAliveFunc));
|
}
|
}
|
};
|
if (executor) {
|
// If we are not allowing inline, clear the completing KA to disallow
|
if (!(priorState == State::OnlyCallbackAllowInline)) {
|
completingKA = Executor::KeepAlive<>{};
|
}
|
exception_wrapper ew;
|
// We need to reset `callback_` after it was executed (which can happen
|
// through the executor or, if `Executor::add` throws, below). The
|
// executor might discard the function without executing it (now or
|
// later), in which case `callback_` also needs to be reset.
|
// The `Core` has to be kept alive throughout that time, too. Hence we
|
// increment `attached_` and `callbackReferences_` by two, and construct
|
// exactly two `CoreAndCallbackReference` objects, which call
|
// `derefCallback` and `detachOne` in their destructor. One will guard
|
// this scope, the other one will guard the lambda passed to the executor.
|
attached_.fetch_add(2, std::memory_order_relaxed);
|
callbackReferences_.fetch_add(2, std::memory_order_relaxed);
|
CoreAndCallbackReference guard_local_scope(this);
|
CoreAndCallbackReference guard_lambda(this);
|
try {
|
doAdd(
|
std::move(completingKA),
|
std::move(executor),
|
[core_ref =
|
std::move(guard_lambda)](Executor::KeepAlive<>&& ka) mutable {
|
auto cr = std::move(core_ref);
|
CoreBase* const core = cr.getCore();
|
RequestContextScopeGuard rctx(std::move(core->context_));
|
core->callback_(*core, std::move(ka), nullptr);
|
});
|
} catch (...) {
|
ew = exception_wrapper(std::current_exception());
|
}
|
if (ew) {
|
RequestContextScopeGuard rctx(std::move(context_));
|
callback_(*this, Executor::KeepAlive<>{}, &ew);
|
}
|
} else {
|
attached_.fetch_add(1, std::memory_order_relaxed);
|
SCOPE_EXIT {
|
context_.~Context();
|
callback_.~Callback();
|
detachOne();
|
};
|
RequestContextScopeGuard rctx(std::move(context_));
|
callback_(*this, std::move(completingKA), nullptr);
|
}
|
}
|
void CoreBase::proxyCallback(State priorState) {
|
Subsets and Splits