X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FSingleton-inl.h;h=12612acc2c678280c88c74ae7c41fbbf643ee1c4;hp=83ef3926a6dfe1c9e26d05251d94ef26e45f3750;hb=e50d7df41cf5371584281bbf4208fd8c54bbf7f5;hpb=f390a90d567a2abe56fecca39ca880a998cbe24a diff --git a/folly/Singleton-inl.h b/folly/Singleton-inl.h index 83ef3926..12612acc 100644 --- a/folly/Singleton-inl.h +++ b/folly/Singleton-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ namespace detail { template template SingletonHolder& SingletonHolder::singleton() { - static auto entry = + /* library-local */ static auto entry = createGlobal, std::pair>([]() { return new SingletonHolder({typeid(T), typeid(Tag)}, *SingletonVault::singleton()); @@ -29,6 +29,9 @@ SingletonHolder& SingletonHolder::singleton() { return *entry; } +[[noreturn]] void singletonWarnDoubleRegistrationAndAbort( + const TypeDescriptor& type); + template void SingletonHolder::registerSingleton(CreateFunc c, TeardownFunc t) { std::lock_guard entry_lock(mutex_); @@ -51,9 +54,7 @@ void SingletonHolder::registerSingleton(CreateFunc c, TeardownFunc t) { * Singleton b([] { return new int(4); }); * */ - LOG(FATAL) << "Double registration of singletons of the same " - << "underlying type; check for multiple definitions " - << "of type folly::Singleton<" + type_.name() + ">"; + singletonWarnDoubleRegistrationAndAbort(type()); } create_ = std::move(c); @@ -65,10 +66,20 @@ void SingletonHolder::registerSingleton(CreateFunc c, TeardownFunc t) { template void SingletonHolder::registerSingletonMock(CreateFunc c, TeardownFunc t) { if (state_ == SingletonHolderState::NotRegistered) { - LOG(FATAL) - << "Registering mock before singleton was registered: " << type_.name(); + detail::singletonWarnRegisterMockEarlyAndAbort(type()); + } + if (state_ == SingletonHolderState::Living) { + destroyInstance(); + } + + { + auto creationOrder = vault_.creationOrder_.wlock(); + + auto it = std::find(creationOrder->begin(), creationOrder->end(), type()); + if (it != creationOrder->end()) { + creationOrder->erase(it); + } } - destroyInstance(); std::lock_guard entry_lock(mutex_); @@ -85,10 +96,7 @@ T* SingletonHolder::get() { createInstance(); if (instance_weak_.expired()) { - throw std::runtime_error( - "Raw pointer to a singleton requested after its destruction." - " Singleton type is: " + - type_.name()); + detail::singletonThrowGetInvokedAfterDestruction(type()); } return instance_ptr_; @@ -125,39 +133,40 @@ folly::ReadMostlySharedPtr SingletonHolder::try_get_fast() { } template -TypeDescriptor SingletonHolder::type() { - return type_; +bool SingletonHolder::hasLiveInstance() { + return !instance_weak_.expired(); } template -bool SingletonHolder::hasLiveInstance() { - return !instance_weak_.expired(); +void SingletonHolder::preDestroyInstance( + ReadMostlyMainPtrDeleter<>& deleter) { + instance_copy_ = instance_; + deleter.add(std::move(instance_)); } template void SingletonHolder::destroyInstance() { state_ = SingletonHolderState::Dead; instance_.reset(); + instance_copy_.reset(); if (destroy_baton_) { - auto wait_result = destroy_baton_->timed_wait( - std::chrono::steady_clock::now() + kDestroyWaitTime); - if (!wait_result) { + constexpr std::chrono::seconds kDestroyWaitTime{5}; + auto last_reference_released = + destroy_baton_->try_wait_for(kDestroyWaitTime); + if (last_reference_released) { + teardown_(instance_ptr_); + } else { print_destructor_stack_trace_->store(true); - LOG(ERROR) << "Singleton of type " << type_.name() << " has a " - << "living reference at destroyInstances time; beware! Raw " - << "pointer is " << instance_ptr_ << ". It is very likely " - << "that some other singleton is holding a shared_ptr to it. " - << "Make sure dependencies between these singletons are " - << "properly defined."; + detail::singletonWarnDestroyInstanceLeak(type(), instance_ptr_); } } } template -SingletonHolder::SingletonHolder(TypeDescriptor type__, - SingletonVault& vault) : - type_(type__), vault_(vault) { -} +SingletonHolder::SingletonHolder( + TypeDescriptor typeDesc, + SingletonVault& vault) + : SingletonHolderBase(typeDesc), vault_(vault) {} template bool SingletonHolder::creationStarted() { @@ -179,7 +188,7 @@ template void SingletonHolder::createInstance() { if (creating_thread_.load(std::memory_order_acquire) == std::this_thread::get_id()) { - LOG(FATAL) << "circular singleton dependency: " << type_.name(); + detail::singletonWarnCreateCircularDependencyAndAbort(type()); } std::lock_guard entry_lock(mutex_); @@ -188,11 +197,7 @@ void SingletonHolder::createInstance() { } if (state_.load(std::memory_order_acquire) == SingletonHolderState::NotRegistered) { - auto ptr = SingletonVault::stackTraceGetter().load(); - LOG(FATAL) << "Creating instance for unregistered singleton: " - << type_.name() << "\n" - << "Stacktrace:" - << "\n" << (ptr ? (*ptr)() : "(not available)"); + detail::singletonWarnCreateUnregisteredAndAbort(type()); } if (state_.load(std::memory_order_acquire) == SingletonHolderState::Living) { @@ -208,39 +213,29 @@ void SingletonHolder::createInstance() { creating_thread_.store(std::this_thread::get_id(), std::memory_order_release); - RWSpinLock::ReadHolder rh(&vault_.stateMutex_); - if (vault_.state_ == SingletonVault::SingletonVaultState::Quiescing) { + auto state = vault_.state_.rlock(); + if (vault_.type_ != SingletonVault::Type::Relaxed && + !state->registrationComplete) { + detail::singletonWarnCreateBeforeRegistrationCompleteAndAbort(type()); + } + if (state->state == detail::SingletonVaultState::Type::Quiescing) { return; } auto destroy_baton = std::make_shared>(); auto print_destructor_stack_trace = std::make_shared>(false); - auto teardown = teardown_; - auto type_name = type_.name(); // Can't use make_shared -- no support for a custom deleter, sadly. std::shared_ptr instance( - create_(), - [destroy_baton, print_destructor_stack_trace, teardown, type_name] - (T* instance_ptr) mutable { - teardown(instance_ptr); - destroy_baton->post(); - if (print_destructor_stack_trace->load()) { - std::string output = "Singleton " + type_name + " was destroyed.\n"; - - auto stack_trace_getter = SingletonVault::stackTraceGetter().load(); - auto stack_trace = stack_trace_getter ? stack_trace_getter() : ""; - if (stack_trace.empty()) { - output += "Failed to get destructor stack trace."; - } else { - output += "Destructor stack trace:\n"; - output += stack_trace; + create_(), + [ destroy_baton, print_destructor_stack_trace, type = type() ]( + T*) mutable { + destroy_baton->post(); + if (print_destructor_stack_trace->load()) { + detail::singletonPrintDestructionStackTrace(type); } - - LOG(ERROR) << output; - } - }); + }); // We should schedule destroyInstances() only after the singleton was // created. This will ensure it will be destroyed before singletons, @@ -260,12 +255,9 @@ void SingletonHolder::createInstance() { // may access instance and instance_weak w/o synchronization. state_.store(SingletonHolderState::Living, std::memory_order_release); - { - RWSpinLock::WriteHolder wh(&vault_.mutex_); - vault_.creation_order_.push_back(type_); - } + vault_.creationOrder_.wlock()->push_back(type()); } -} +} // namespace detail -} +} // namespace folly