text
stringlengths
0
2.2M
type_iter != creationOrder->rend();
++type_iter) {
singletons->at(*type_iter)->destroyInstance();
}
for (auto& singleton_type : *creationOrder) {
auto instance = singletons->at(singleton_type);
if (!instance->hasLiveInstance()) {
continue;
}
fatalHelper.leakedSingletons_.push_back(instance->type());
}
}
{
auto creationOrder = creationOrder_.wlock();
creationOrder->clear();
}
}
void SingletonVault::reenableInstances() {
auto state = state_.wlock();
state->check(detail::SingletonVaultState::Type::Quiescing);
state->state = detail::SingletonVaultState::Type::Running;
}
void SingletonVault::scheduleDestroyInstances() {
// Add a dependency on folly::ThreadLocal to make sure all its static
// singletons are initalized first.
threadlocal_detail::StaticMeta<void, void>::instance();
std::atexit([] {
SingletonVault::singleton()->startShutdownTimer();
SingletonVault::singleton()->destroyInstances();
});
}
void SingletonVault::addToShutdownLog(std::string message) {
shutdownLog_.wlock()->push_back(std::move(message));
}
#if FOLLY_HAVE_LIBRT
namespace {
[[noreturn]] void fireShutdownSignalHelper(sigval_t sigval) {
static_cast<SingletonVault*>(sigval.sival_ptr)->fireShutdownTimer();
}
} // namespace
#endif
void SingletonVault::startShutdownTimer() {
#if FOLLY_HAVE_LIBRT
if (shutdownTimerStarted_.exchange(true)) {
return;
}
if (!shutdownTimeout_.count()) {
return;
}
struct sigevent sig;
sig.sigev_notify = SIGEV_THREAD;
sig.sigev_notify_function = fireShutdownSignalHelper;
sig.sigev_value.sival_ptr = this;
sig.sigev_notify_attributes = nullptr;
timer_t timerId;
PCHECK(timer_create(CLOCK_MONOTONIC, &sig, &timerId) == 0);
struct itimerspec newValue, oldValue;
newValue.it_value.tv_sec =
std::chrono::milliseconds(shutdownTimeout_).count() / 1000;
newValue.it_value.tv_nsec =
std::chrono::milliseconds(shutdownTimeout_).count() % 1000 * 1000000;
newValue.it_interval.tv_sec = 0;
newValue.it_interval.tv_nsec = 0;
PCHECK(timer_settime(timerId, 0, &newValue, &oldValue) == 0);
#endif
}
[[noreturn]] void SingletonVault::fireShutdownTimer() {
std::string shutdownLog;
for (auto& logMessage : shutdownLog_.copy()) {
shutdownLog += logMessage + "\n";
}
auto msg = folly::to<std::string>(
"Failed to complete shutdown within ",
std::chrono::milliseconds(shutdownTimeout_).count(),
"ms. Shutdown log:\n",
shutdownLog);
folly::terminate_with<std::runtime_error>(msg);
}
} // namespace folly
/*
* Copyright 2015 The Android Open Source Project
*