diff --git a/src/ds/helpers.h b/src/ds/helpers.h index e34387d..f8ad92c 100644 --- a/src/ds/helpers.h +++ b/src/ds/helpers.h @@ -13,7 +13,7 @@ namespace snmalloc * initialised. This singleton class is designed to not depend on the * runtime. */ - template + template 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; diff --git a/src/mem/pool.h b/src/mem/pool.h index aecc550..c57b525 100644 --- a/src/mem/pool.h +++ b/src/mem/pool.h @@ -45,14 +45,6 @@ namespace snmalloc template class SingletonPoolState { - static inline PoolState 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*) 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& pool() { - if (unlikely(!initialized)) - { - ensure_init(); - initialized = true; - } - return state; + return Singleton, &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. diff --git a/src/mem/threadalloc.h b/src/mem/threadalloc.h index dc9cee6..3777d44 100644 --- a/src/mem/threadalloc.h +++ b/src/mem/threadalloc.h @@ -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.