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