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() {
static FOLLY_TLS ThreadEntryList threadEntryListSingleton;
return &threadEntryListSingleton;
#else
static pthread_key_t pthreadKey;
static folly::once_flag onceFlag;
folly::call_once(onceFlag, [&]() {
int ret = pthread_key_create(&pthreadKey, nullptr);
checkPosixError(ret, "pthread_key_create failed");
PthreadKeyUnregister::registerKey(pthreadKey);
});
class PthreadKey {
public:
PthreadKey() {
int ret = pthread_key_create(&pthreadKey_, nullptr);
checkPosixError(ret, "pthread_key_create failed");
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 =
static_cast<ThreadEntryList*>(pthread_getspecific(pthreadKey));
static_cast<ThreadEntryList*>(pthread_getspecific(instance->get()));
if (UNLIKELY(!threadEntryList)) {
threadEntryList = new ThreadEntryList();
int ret = pthread_setspecific(pthreadKey, threadEntryList);
int ret = pthread_setspecific(instance->get(), threadEntryList);
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