text stringlengths 0 2.2M |
|---|
void SingletonVault::registrationComplete() {
|
std::atexit([]() { SingletonVault::singleton()->destroyInstances(); });
|
auto state = state_.wlock();
|
state->check(detail::SingletonVaultState::Type::Running);
|
if (state->registrationComplete) {
|
return;
|
}
|
auto singletons = singletons_.rlock();
|
if (type_.load(std::memory_order_relaxed) == Type::Strict) {
|
for (const auto& p : *singletons) {
|
if (p.second->hasLiveInstance()) {
|
throw std::runtime_error(
|
"Singleton " + p.first.name() +
|
" created before registration was complete.");
|
}
|
}
|
}
|
state->registrationComplete = true;
|
}
|
void SingletonVault::doEagerInit() {
|
{
|
auto state = state_.rlock();
|
state->check(detail::SingletonVaultState::Type::Running);
|
if (UNLIKELY(!state->registrationComplete)) {
|
throw std::logic_error("registrationComplete() not yet called");
|
}
|
}
|
auto eagerInitSingletons = eagerInitSingletons_.rlock();
|
for (auto* single : *eagerInitSingletons) {
|
single->createInstance();
|
}
|
}
|
void SingletonVault::doEagerInitVia(Executor& exe, folly::Baton<>* done) {
|
{
|
auto state = state_.rlock();
|
state->check(detail::SingletonVaultState::Type::Running);
|
if (UNLIKELY(!state->registrationComplete)) {
|
throw std::logic_error("registrationComplete() not yet called");
|
}
|
}
|
auto eagerInitSingletons = eagerInitSingletons_.rlock();
|
auto countdown =
|
std::make_shared<std::atomic<size_t>>(eagerInitSingletons->size());
|
for (auto* single : *eagerInitSingletons) {
|
// countdown is retained by shared_ptr, and will be alive until last lambda
|
// is done. notifyBaton is provided by the caller, and expected to remain
|
// present (if it's non-nullptr). singletonSet can go out of scope but
|
// its values, which are SingletonHolderBase pointers, are alive as long as
|
// SingletonVault is not being destroyed.
|
exe.add([=] {
|
// decrement counter and notify if requested, whether initialization
|
// was successful, was skipped (already initialized), or exception thrown.
|
SCOPE_EXIT {
|
if (--(*countdown) == 0) {
|
if (done != nullptr) {
|
done->post();
|
}
|
}
|
};
|
// if initialization is in progress in another thread, don't try to init
|
// here. Otherwise the current thread will block on 'createInstance'.
|
if (!single->creationStarted()) {
|
single->createInstance();
|
}
|
});
|
}
|
}
|
void SingletonVault::destroyInstances() {
|
auto stateW = state_.wlock();
|
if (stateW->state == detail::SingletonVaultState::Type::Quiescing) {
|
return;
|
}
|
stateW->state = detail::SingletonVaultState::Type::Quiescing;
|
auto stateR = stateW.moveFromWriteToRead();
|
{
|
auto singletons = singletons_.rlock();
|
auto creationOrder = creationOrder_.rlock();
|
CHECK_GE(singletons->size(), creationOrder->size());
|
// Release all ReadMostlyMainPtrs at once
|
{
|
ReadMostlyMainPtrDeleter<> deleter;
|
for (auto& singleton_type : *creationOrder) {
|
singletons->at(singleton_type)->preDestroyInstance(deleter);
|
}
|
}
|
for (auto type_iter = creationOrder->rbegin();
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.