Commit a65e66ab authored by Dan Melnic's avatar Dan Melnic Committed by Facebook Github Bot

Make the pthread key a singleton

Summary: Make the pthread key a singleton

Reviewed By: yfeldblum

Differential Revision: D8663589

fbshipit-source-id: 1e0c328104bd8c3d3c689dc4955a185336e74276
parent 1e274dc6
...@@ -76,20 +76,30 @@ ThreadEntryList* StaticMetaBase::getThreadEntryList() { ...@@ -76,20 +76,30 @@ ThreadEntryList* StaticMetaBase::getThreadEntryList() {
static FOLLY_TLS ThreadEntryList threadEntryListSingleton; static FOLLY_TLS ThreadEntryList threadEntryListSingleton;
return &threadEntryListSingleton; return &threadEntryListSingleton;
#else #else
static pthread_key_t pthreadKey; class PthreadKey {
static folly::once_flag onceFlag; public:
folly::call_once(onceFlag, [&]() { PthreadKey() {
int ret = pthread_key_create(&pthreadKey, nullptr); int ret = pthread_key_create(&pthreadKey_, nullptr);
checkPosixError(ret, "pthread_key_create failed"); checkPosixError(ret, "pthread_key_create failed");
PthreadKeyUnregister::registerKey(pthreadKey); PthreadKeyUnregister::registerKey(pthreadKey_);
}); }
FOLLY_ALWAYS_INLINE pthread_key_t get() const {
return pthreadKey_;
}
private:
pthread_key_t pthreadKey_;
};
static auto instance = detail::createGlobal<PthreadKey, void>();
ThreadEntryList* threadEntryList = ThreadEntryList* threadEntryList =
static_cast<ThreadEntryList*>(pthread_getspecific(pthreadKey)); static_cast<ThreadEntryList*>(pthread_getspecific(instance->get()));
if (UNLIKELY(!threadEntryList)) { if (UNLIKELY(!threadEntryList)) {
threadEntryList = new ThreadEntryList(); threadEntryList = new ThreadEntryList();
int ret = pthread_setspecific(pthreadKey, threadEntryList); int ret = pthread_setspecific(instance->get(), threadEntryList);
checkPosixError(ret, "pthread_setspecific failed"); checkPosixError(ret, "pthread_setspecific failed");
} }
......
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