Commit 9dd08403 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by facebook-github-bot-4

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
parent 2739d937
...@@ -170,14 +170,23 @@ class StaticSingletonManager { ...@@ -170,14 +170,23 @@ class StaticSingletonManager {
template <typename T, typename Tag, typename F> template <typename T, typename Tag, typename F>
inline T* create(F&& creator) { inline T* create(F&& creator) {
std::lock_guard<std::mutex> lg(mutex_); auto& entry = [&]() mutable -> Entry<T>& {
std::lock_guard<std::mutex> lg(mutex_);
auto& id = typeid(TypePair<T, Tag>); auto& id = typeid(TypePair<T, Tag>);
auto& ptr = reinterpret_cast<T*&>(map_[id]); auto& entryPtr = reinterpret_cast<Entry<T>*&>(map_[id]);
if (!ptr) { if (!entryPtr) {
ptr = creator(); entryPtr = new Entry<T>();
}
return *entryPtr;
}();
std::lock_guard<std::mutex> lg(entry.mutex);
if (!entry.ptr) {
entry.ptr = creator();
} }
return ptr; return entry.ptr;
} }
private: private:
...@@ -186,6 +195,12 @@ class StaticSingletonManager { ...@@ -186,6 +195,12 @@ class StaticSingletonManager {
StaticSingletonManager() {} StaticSingletonManager() {}
template <typename T>
struct Entry {
T* ptr{nullptr};
std::mutex mutex;
};
std::unordered_map<std::type_index, intptr_t> map_; std::unordered_map<std::type_index, intptr_t> map_;
std::mutex mutex_; std::mutex mutex_;
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment