From 9dd084035d147c2918c3526e03502252038587a5 Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Tue, 23 Feb 2016 19:49:07 -0800 Subject: [PATCH] Fix double definition for templated folly::Singletons Summary: Allow createGlobal() to create dependent objects. Reviewed By: yfeldblum Differential Revision: D2963111 fb-gh-sync-id: 8e4da48a7a1000934963396b423e8eff98a8aade shipit-source-id: 8e4da48a7a1000934963396b423e8eff98a8aade --- folly/Singleton.h | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/folly/Singleton.h b/folly/Singleton.h index e083e35b..bb04862d 100644 --- a/folly/Singleton.h +++ b/folly/Singleton.h @@ -170,14 +170,23 @@ class StaticSingletonManager { template inline T* create(F&& creator) { - std::lock_guard lg(mutex_); + auto& entry = [&]() mutable -> Entry& { + std::lock_guard lg(mutex_); - auto& id = typeid(TypePair); - auto& ptr = reinterpret_cast(map_[id]); - if (!ptr) { - ptr = creator(); + auto& id = typeid(TypePair); + auto& entryPtr = reinterpret_cast*&>(map_[id]); + if (!entryPtr) { + entryPtr = new Entry(); + } + return *entryPtr; + }(); + + std::lock_guard lg(entry.mutex); + + if (!entry.ptr) { + entry.ptr = creator(); } - return ptr; + return entry.ptr; } private: @@ -186,6 +195,12 @@ class StaticSingletonManager { StaticSingletonManager() {} + template + struct Entry { + T* ptr{nullptr}; + std::mutex mutex; + }; + std::unordered_map map_; std::mutex mutex_; }; -- 2.34.1