Applied PR feedback

This commit is contained in:
Istvan Haller
2021-08-25 13:35:13 +01:00
parent 33a358e4fe
commit e8cc3af6e5
3 changed files with 13 additions and 21 deletions

View File

@@ -13,7 +13,7 @@ namespace snmalloc
* initialised. This singleton class is designed to not depend on the
* runtime.
*/
template<class Object, Object init() noexcept>
template<class Object, void init(Object*) noexcept>
class Singleton
{
inline static std::atomic_flag flag;
@@ -36,7 +36,7 @@ namespace snmalloc
FlagLock lock(flag);
if (!initialised)
{
obj = init();
init(&obj);
initialised.store(true, std::memory_order_release);
if (first != nullptr)
*first = true;

View File

@@ -45,14 +45,6 @@ namespace snmalloc
template<typename T, typename SharedStateHandle>
class SingletonPoolState
{
static inline PoolState<T> state;
/**
* Thread-local initialization marker for a contention-free way to skip
* initialization. SharedStateHandle::ensure_init allows for multiple calls,
* but it uses a reentrency-safe check which is more expensive.
*/
static thread_local inline bool initialized;
/**
* SFINAE helper. Matched only if `T` implements `ensure_init`. Calls it
* if it exists.
@@ -78,6 +70,13 @@ namespace snmalloc
call_ensure_init(nullptr, 0);
}
static void make_pool(PoolState<T>*) noexcept
{
ensure_init();
// Default initializer already called on PoolState, no need to use
// placement new.
}
public:
/**
* Returns a reference for the global PoolState for the given type.
@@ -85,12 +84,7 @@ namespace snmalloc
*/
SNMALLOC_FAST_PATH static PoolState<T>& pool()
{
if (unlikely(!initialized))
{
ensure_init();
initialized = true;
}
return state;
return Singleton<PoolState<T>, &make_pool>::get();
}
};
@@ -102,7 +96,7 @@ namespace snmalloc
* For the pool of allocators, refer to the AllocPool alias defined in
* corealloc.h.
*
* For a pool of another type it is recommended to leave the
* For a pool of another type, it is recommended to leave the
* third template argument with its default value. The SingletonPoolState
* class is used as a helper to provide a default PoolState management for
* this use case.

View File

@@ -106,11 +106,9 @@ namespace snmalloc
/**
* Used to give correct signature to the pthread call for the Singleton class.
*/
inline pthread_key_t pthread_create() noexcept
inline void pthread_create(pthread_key_t* key) noexcept
{
pthread_key_t key;
pthread_key_create(&key, &pthread_cleanup);
return key;
pthread_key_create(key, &pthread_cleanup);
}
/**
* Performs thread local teardown for the allocator using the pthread library.