text
stringlengths
0
2.2M
auto object = new exception_wrapper(std::move(e));
auto exchanged = folly::atomic_compare_exchange_strong_explicit(
&interrupt_,
&interrupt,
reinterpret_cast<uintptr_t>(object) | InterruptHasObject,
std::memory_order_release,
std::memory_order_acquire);
if (exchanged) {
return;
}
// lost the race!
e = std::move(*object);
delete object;
if (interrupt & InterruptHasObject) { // ignore all calls after the first
return;
}
assert(interrupt & InterruptHasHandler);
FOLLY_FALLTHROUGH;
}
case InterruptHasHandler: { // invoke the stored handler
auto pointer = interrupt & ~InterruptMask;
auto exchanged = interrupt_.compare_exchange_strong(
interrupt, pointer | InterruptTerminal, std::memory_order_relaxed);
if (!exchanged) { // ignore all calls after the first
return;
}
auto handler = reinterpret_cast<InterruptHandler*>(pointer);
handler->handle(e);
return;
}
case InterruptHasObject: // ignore all calls after the first
return;
case InterruptTerminal: // ignore all calls after the first
return;
}
}
void CoreBase::initCopyInterruptHandlerFrom(const CoreBase& other) {
assert(!interrupt_.load(std::memory_order_relaxed));
auto interrupt = other.interrupt_.load(std::memory_order_acquire);
switch (interrupt & InterruptMask) {
case InterruptHasHandler: { // copy the handler
auto pointer = interrupt & ~InterruptMask;
auto handler = reinterpret_cast<InterruptHandler*>(pointer);
handler->acquire();
interrupt_.store(
pointer | InterruptHasHandler, std::memory_order_release);
break;
}
case InterruptTerminal: { // copy the handler, if any
auto pointer = interrupt & ~InterruptMask;
auto handler = reinterpret_cast<InterruptHandler*>(pointer);
if (handler) {
handler->acquire();
interrupt_.store(
pointer | InterruptHasHandler, std::memory_order_release);
}
break;
}
}
}
class CoreBase::CoreAndCallbackReference {
public:
explicit CoreAndCallbackReference(CoreBase* core) noexcept : core_(core) {}
~CoreAndCallbackReference() noexcept { detach(); }
CoreAndCallbackReference(CoreAndCallbackReference const& o) = delete;
CoreAndCallbackReference& operator=(CoreAndCallbackReference const& o) =
delete;
CoreAndCallbackReference& operator=(CoreAndCallbackReference&&) = delete;
CoreAndCallbackReference(CoreAndCallbackReference&& o) noexcept
: core_(std::exchange(o.core_, nullptr)) {}
CoreBase* getCore() const noexcept { return core_; }
private:
void detach() noexcept {
if (core_) {
core_->derefCallback();
core_->detachOne();
}
}
CoreBase* core_{nullptr};
};
CoreBase::CoreBase(State state, unsigned char attached)
: state_(state), attached_(attached) {}
CoreBase::~CoreBase() {
auto interrupt = interrupt_.load(std::memory_order_acquire);
auto pointer = interrupt & ~InterruptMask;
switch (interrupt & InterruptMask) {
case InterruptHasHandler: {
auto handler = reinterpret_cast<InterruptHandler*>(pointer);
handler->release();
break;