Commit e2be2a4a authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Refactor StaticSingletonManager

Summary:
[Folly] Refactor `StaticSingletonManager`.

* Move implementation to `.cpp`.
* Remove all `#include`s possible from `.h`.
* Control inlining.

Differential Revision: D9734721

fbshipit-source-id: 82a1cda187674a6bb62e14a80431aeb07c65156c
parent ef7558cd
...@@ -13,15 +13,49 @@ ...@@ -13,15 +13,49 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
#include <folly/detail/StaticSingletonManager.h> #include <folly/detail/StaticSingletonManager.h>
#include <mutex>
#include <typeindex>
#include <unordered_map>
namespace folly { namespace folly {
namespace detail { namespace detail {
// This implementation should always live in .cpp file. namespace {
StaticSingletonManager& StaticSingletonManager::instance() {
static StaticSingletonManager* instance = new StaticSingletonManager(); class StaticSingletonManagerImpl {
return *instance; public:
void* create(std::type_info const& key, void* (*make)(void*), void* ctx) {
auto& e = entry(key);
std::lock_guard<std::mutex> lock(e.mutex);
return e.ptr ? e.ptr : (e.ptr = make(ctx));
}
private:
struct Entry {
void* ptr{};
std::mutex mutex;
};
Entry& entry(std::type_info const& key) {
std::lock_guard<std::mutex> lock(mutex_);
auto& e = map_[key];
return e ? *e : *(e = new Entry());
}
std::unordered_map<std::type_index, Entry*> map_;
std::mutex mutex_;
};
} // namespace
void* StaticSingletonManager::create_(Key const& key, Make* make, void* ctx) {
// This Leaky Meyers Singleton must always live in the .cpp file.
static auto& instance = *new StaticSingletonManagerImpl();
return instance.create(key, make, ctx);
} }
} // namespace detail } // namespace detail
} // namespace folly } // namespace folly
...@@ -16,10 +16,9 @@ ...@@ -16,10 +16,9 @@
#pragma once #pragma once
#include <cassert> #include <typeinfo>
#include <mutex>
#include <typeindex> #include <folly/CPortability.h>
#include <unordered_map>
namespace folly { namespace folly {
namespace detail { namespace detail {
...@@ -30,59 +29,52 @@ namespace detail { ...@@ -30,59 +29,52 @@ namespace detail {
// dynamically. // dynamically.
class StaticSingletonManager { class StaticSingletonManager {
public: public:
static StaticSingletonManager& instance();
template <typename T, typename Tag, typename F> template <typename T, typename Tag, typename F>
inline T* create(F&& creator) { FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN static T* create(
auto& entry = [&]() mutable -> Entry<T>& { F&& creator) {
std::lock_guard<std::mutex> lg(mutex_); return static_cast<T*>(create_<T, Tag>(creator));
auto& id = typeid(TypePair<T, Tag>);
auto& entryPtr = map_[id];
if (!entryPtr) {
entryPtr = new Entry<T>();
}
assert(dynamic_cast<Entry<T>*>(entryPtr) != nullptr);
return *static_cast<Entry<T>*>(entryPtr);
}();
std::lock_guard<std::mutex> lg(entry.mutex);
if (!entry.ptr) {
entry.ptr = creator();
}
return entry.ptr;
} }
private: private:
template <typename A, typename B> template <typename A, typename B>
class TypePair {}; struct TypePair {};
StaticSingletonManager() {} using Key = std::type_info;
using Make = void*(void*);
struct EntryIf { template <typename F>
virtual ~EntryIf() {} struct Creator {
static void* create(void* f) {
return static_cast<void*>((*static_cast<F*>(f))());
}
}; };
template <typename T> template <typename T, typename Tag, typename F>
struct Entry : public EntryIf { FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN static void* create_(
T* ptr{nullptr}; F& creator) {
std::mutex mutex; auto const& key = typeid(TypePair<T, Tag>);
}; return create_(key, &Creator<F>::create, &creator);
}
std::unordered_map<std::type_index, EntryIf*> map_; template <typename T, typename Tag, typename F>
std::mutex mutex_; FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN static void* create_(
F const& creator) {
auto const& key = typeid(TypePair<T, Tag>);
return create_(key, &Creator<F const>::create, const_cast<F*>(&creator));
}
FOLLY_NOINLINE static void* create_(Key const& key, Make* make, void* ctx);
}; };
template <typename T, typename Tag, typename F> template <typename T, typename Tag, typename F>
inline T* createGlobal(F&& creator) { FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN T* createGlobal(F&& creator) {
return StaticSingletonManager::instance().create<T, Tag>( return StaticSingletonManager::create<T, Tag>(static_cast<F&&>(creator));
std::forward<F>(creator));
} }
template <typename T, typename Tag> template <typename T, typename Tag>
inline T* createGlobal() { FOLLY_ALWAYS_INLINE FOLLY_ATTR_VISIBILITY_HIDDEN T* createGlobal() {
return createGlobal<T, Tag>([]() { return new T(); }); return StaticSingletonManager::create<T, Tag>([]() { return new T(); });
} }
} // namespace detail } // namespace detail
} // namespace folly } // namespace folly
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