X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FSingleton-inl.h;h=2656084e81bae8abedb032d2b54ddacd815c388e;hb=f57ddfc76b3835033a36343df22ae821168dcc0f;hp=e946e8a51ba632b695f7c2af962100ad769a8770;hpb=e4527fb5d04f5fec823bd6a2402b620a6e1a64e3;p=folly.git diff --git a/folly/Singleton-inl.h b/folly/Singleton-inl.h index e946e8a5..2656084e 100644 --- a/folly/Singleton-inl.h +++ b/folly/Singleton-inl.h @@ -1,5 +1,5 @@ /* - * Copyright 2015 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,12 +21,17 @@ namespace detail { template template SingletonHolder& SingletonHolder::singleton() { - static auto entry = new SingletonHolder( - {typeid(T), typeid(Tag)}, - *SingletonVault::singleton()); + /* library-local */ static auto entry = + createGlobal, std::pair>([]() { + return new SingletonHolder({typeid(T), typeid(Tag)}, + *SingletonVault::singleton()); + }); return *entry; } +[[noreturn]] void singletonWarnDoubleRegistrationAndAbort( + const TypeDescriptor& type); + template void SingletonHolder::registerSingleton(CreateFunc c, TeardownFunc t) { std::lock_guard entry_lock(mutex_); @@ -49,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); @@ -63,11 +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(); + LOG(FATAL) << "Registering mock before singleton was registered: " + << type().name(); } destroyInstance(); + { + auto creationOrder = vault_.creationOrder_.wlock(); + + auto it = std::find(creationOrder->begin(), creationOrder->end(), type()); + if (it != creationOrder->end()) { + creationOrder->erase(it); + } + } + std::lock_guard entry_lock(mutex_); create_ = std::move(c); @@ -76,7 +88,8 @@ void SingletonHolder::registerSingletonMock(CreateFunc c, TeardownFunc t) { template T* SingletonHolder::get() { - if (LIKELY(state_ == SingletonHolderState::Living)) { + if (LIKELY(state_.load(std::memory_order_acquire) == + SingletonHolderState::Living)) { return instance_ptr_; } createInstance(); @@ -85,7 +98,7 @@ T* SingletonHolder::get() { throw std::runtime_error( "Raw pointer to a singleton requested after its destruction." " Singleton type is: " + - type_.name()); + type().name()); } return instance_ptr_; @@ -93,7 +106,8 @@ T* SingletonHolder::get() { template std::weak_ptr SingletonHolder::get_weak() { - if (UNLIKELY(state_ != SingletonHolderState::Living)) { + if (UNLIKELY(state_.load(std::memory_order_acquire) != + SingletonHolderState::Living)) { createInstance(); } @@ -101,8 +115,23 @@ std::weak_ptr SingletonHolder::get_weak() { } template -TypeDescriptor SingletonHolder::type() { - return type_; +std::shared_ptr SingletonHolder::try_get() { + if (UNLIKELY(state_.load(std::memory_order_acquire) != + SingletonHolderState::Living)) { + createInstance(); + } + + return instance_weak_.lock(); +} + +template +folly::ReadMostlySharedPtr SingletonHolder::try_get_fast() { + if (UNLIKELY(state_.load(std::memory_order_acquire) != + SingletonHolderState::Living)) { + createInstance(); + } + + return instance_weak_fast_.lock(); } template @@ -110,16 +139,25 @@ bool SingletonHolder::hasLiveInstance() { return !instance_weak_.expired(); } +template +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_) { + constexpr std::chrono::seconds kDestroyWaitTime{5}; auto wait_result = destroy_baton_->timed_wait( std::chrono::steady_clock::now() + kDestroyWaitTime); if (!wait_result) { print_destructor_stack_trace_->store(true); - LOG(ERROR) << "Singleton of type " << type_.name() << " has a " + 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. " @@ -130,10 +168,10 @@ void SingletonHolder::destroyInstance() { } 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() { @@ -155,7 +193,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(); + LOG(FATAL) << "circular singleton dependency: " << type().name(); } std::lock_guard entry_lock(mutex_); @@ -166,9 +204,10 @@ void SingletonHolder::createInstance() { SingletonHolderState::NotRegistered) { auto ptr = SingletonVault::stackTraceGetter().load(); LOG(FATAL) << "Creating instance for unregistered singleton: " - << type_.name() << "\n" + << type().name() << "\n" << "Stacktrace:" - << "\n" << (ptr ? (*ptr)() : "(not available)"); + << "\n" + << (ptr ? (*ptr)() : "(not available)"); } if (state_.load(std::memory_order_acquire) == SingletonHolderState::Living) { @@ -184,8 +223,8 @@ 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 (state->state == SingletonVault::SingletonVaultState::Quiescing) { return; } @@ -193,17 +232,16 @@ void SingletonHolder::createInstance() { 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. - instance_ = std::shared_ptr( + std::shared_ptr instance( create_(), - [destroy_baton, print_destructor_stack_trace, teardown, type_name] + [destroy_baton, print_destructor_stack_trace, teardown, type = type()] (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"; + 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() : ""; @@ -224,8 +262,11 @@ void SingletonHolder::createInstance() { // constructor SingletonVault::scheduleDestroyInstances(); - instance_weak_ = instance_; - instance_ptr_ = instance_.get(); + instance_weak_ = instance; + instance_ptr_ = instance.get(); + instance_.reset(std::move(instance)); + instance_weak_fast_ = instance_; + destroy_baton_ = std::move(destroy_baton); print_destructor_stack_trace_ = std::move(print_destructor_stack_trace); @@ -233,10 +274,7 @@ 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()); } }