X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FSingleton-inl.h;h=e9a1963a480233f41b1422c6d0376b5924e73583;hb=9103c1fe9b6c4798f2fd345e867c939d9b8aa897;hp=e946e8a51ba632b695f7c2af962100ad769a8770;hpb=e4527fb5d04f5fec823bd6a2402b620a6e1a64e3;p=folly.git diff --git a/folly/Singleton-inl.h b/folly/Singleton-inl.h index e946e8a5..e9a1963a 100644 --- a/folly/Singleton-inl.h +++ b/folly/Singleton-inl.h @@ -76,7 +76,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(); @@ -93,13 +94,34 @@ 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(); } return instance_weak_; } +template +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 TypeDescriptor SingletonHolder::type() { return type_; @@ -196,7 +218,7 @@ void SingletonHolder::createInstance() { 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] (T* instance_ptr) mutable { @@ -224,8 +246,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);