From fd4ce7d13aa50de6023102b06b2aaa5e9b7d720f Mon Sep 17 00:00:00 2001 From: Steve O'Brien Date: Wed, 2 Dec 2015 19:13:22 -0800 Subject: [PATCH] ThreadLocalDetail: OSX fixes Summary: Change mutex to MicroSpinLock, for constexpr-ctor-ness on OSX. Fixes this error seen on mac: ThreadLocalDetail.h:202:38: note: non-literal type 'std::mutex' cannot be used in a constant expression Reviewed By: ldemailly Differential Revision: D2713511 fb-gh-sync-id: bc1c34b4a0ee21347278aa368b408f286345e050 --- folly/detail/ThreadLocalDetail.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/folly/detail/ThreadLocalDetail.h b/folly/detail/ThreadLocalDetail.h index 1c3f2c72..82e83483 100644 --- a/folly/detail/ThreadLocalDetail.h +++ b/folly/detail/ThreadLocalDetail.h @@ -29,6 +29,7 @@ #include #include #include +#include // In general, emutls cleanup is not guaranteed to play nice with the way // StaticMeta mixes direct pthread calls and the use of __thread. This has @@ -163,7 +164,7 @@ struct ThreadEntry { constexpr uint32_t kEntryIDInvalid = std::numeric_limits::max(); -struct PthreadKeyUnregisterTester; +class PthreadKeyUnregisterTester; /** * We want to disable onThreadExit call at the end of shutdown, we don't care @@ -183,7 +184,7 @@ class PthreadKeyUnregister { static constexpr size_t kMaxKeys = 1UL << 16; ~PthreadKeyUnregister() { - std::lock_guard lg(mutex_); + MSLGuard lg(lock_); while (size_) { pthread_key_delete(keys_[--size_]); } @@ -199,16 +200,18 @@ class PthreadKeyUnregister { * See also the important note at the top of this class about `constexpr` * usage. */ - constexpr PthreadKeyUnregister() : mutex_(), size_(0), keys_() { } + constexpr PthreadKeyUnregister() : lock_(), size_(0), keys_() { } friend class folly::threadlocal_detail::PthreadKeyUnregisterTester; void registerKeyImpl(pthread_key_t key) { - std::lock_guard lg(mutex_); - CHECK_LT(size_, kMaxKeys); + MSLGuard lg(lock_); + if (size_ == kMaxKeys) { + throw std::logic_error("pthread_key limit has already been reached"); + } keys_[size_++] = key; } - std::mutex mutex_; + MicroSpinLock lock_; size_t size_; pthread_key_t keys_[kMaxKeys]; -- 2.34.1