}
private:
- class CountedReference {
+ // Helper class that stores a pointer to the `Core` object and calls
+ // `derefCallback` and `detachOne` in the destructor.
+ class CoreAndCallbackReference {
public:
- ~CountedReference() {
- if (core_) {
- core_->detachOne();
- core_ = nullptr;
- }
- }
-
- explicit CountedReference(Core* core) noexcept : core_(core) {
- // do not construct a CountedReference from nullptr!
- DCHECK(core);
+ explicit CoreAndCallbackReference(Core* core) noexcept : core_(core) {}
- ++core_->attached_;
- }
-
- // CountedReference must be copy-constructable as long as
- // folly::Executor::add takes a std::function
- CountedReference(CountedReference const& o) noexcept : core_(o.core_) {
+ ~CoreAndCallbackReference() {
if (core_) {
- ++core_->attached_;
+ core_->derefCallback();
+ core_->detachOne();
}
}
- CountedReference& operator=(CountedReference const& o) noexcept {
- ~CountedReference();
- new (this) CountedReference(o);
- return *this;
- }
+ CoreAndCallbackReference(CoreAndCallbackReference const& o) = delete;
+ CoreAndCallbackReference& operator=(CoreAndCallbackReference const& o) =
+ delete;
- CountedReference(CountedReference&& o) noexcept {
+ CoreAndCallbackReference(CoreAndCallbackReference&& o) noexcept {
std::swap(core_, o.core_);
}
- CountedReference& operator=(CountedReference&& o) noexcept {
- ~CountedReference();
- new (this) CountedReference(std::move(o));
- return *this;
- }
-
Core* getCore() const noexcept {
return core_;
}
if (x) {
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_ += 2;
+ callbackReferences_ += 2;
+ CoreAndCallbackReference guard_local_scope(this);
+ CoreAndCallbackReference guard_lambda(this);
try {
if (LIKELY(x->getNumPriorities() == 1)) {
- x->add([core_ref = CountedReference(this)]() mutable {
+ x->add([core_ref = std::move(guard_lambda)]() mutable {
auto cr = std::move(core_ref);
Core* const core = cr.getCore();
RequestContextScopeGuard rctx(core->context_);
- SCOPE_EXIT { core->callback_ = {}; };
core->callback_(std::move(*core->result_));
});
} else {
- x->addWithPriority([core_ref = CountedReference(this)]() mutable {
- auto cr = std::move(core_ref);
- Core* const core = cr.getCore();
- RequestContextScopeGuard rctx(core->context_);
- SCOPE_EXIT { core->callback_ = {}; };
- core->callback_(std::move(*core->result_));
- }, priority);
+ x->addWithPriority(
+ [core_ref = std::move(guard_lambda)]() mutable {
+ auto cr = std::move(core_ref);
+ Core* const core = cr.getCore();
+ RequestContextScopeGuard rctx(core->context_);
+ core->callback_(std::move(*core->result_));
+ },
+ priority);
}
} catch (const std::exception& e) {
ew = exception_wrapper(std::current_exception(), e);
ew = exception_wrapper(std::current_exception());
}
if (ew) {
- CountedReference core_ref(this);
RequestContextScopeGuard rctx(context_);
result_ = Try<T>(std::move(ew));
- SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_));
}
} else {
- CountedReference core_ref(this);
+ attached_++;
+ SCOPE_EXIT {
+ callback_ = {};
+ detachOne();
+ };
RequestContextScopeGuard rctx(context_);
- SCOPE_EXIT { callback_ = {}; };
callback_(std::move(*result_));
}
}
}
}
+ void derefCallback() {
+ if (--callbackReferences_ == 0) {
+ callback_ = {};
+ }
+ }
folly::Function<void(Try<T>&&)> callback_;
// place result_ next to increase the likelihood that the value will be
folly::Optional<Try<T>> result_;
FSM<State> fsm_;
std::atomic<unsigned char> attached_;
+ std::atomic<unsigned char> callbackReferences_{0};
std::atomic<bool> active_ {true};
std::atomic<bool> interruptHandlerSet_ {false};
folly::MicroSpinLock interruptLock_ {0};