Use thread-local in RequestContext::getStaticContext
[folly.git] / folly / io / async / Request.cpp
index f8cca8b92a866c53bf34253fbf6ea96343208425..644c208f18765c88453b55800d68732f761b469f 100644 (file)
@@ -141,10 +141,16 @@ std::shared_ptr<RequestContext> RequestContext::setContext(
 }
 
 std::shared_ptr<RequestContext>& RequestContext::getStaticContext() {
-  using SingletonT = SingletonThreadLocal<std::shared_ptr<RequestContext>>;
-  static SingletonT singleton;
-
+  using T = std::shared_ptr<RequestContext>;
+#ifdef FOLLY_TLS
+  alignas(alignof(T)) static FOLLY_TLS unsigned char storage[sizeof(T)];
+  static FOLLY_TLS T* singleton;
+  return singleton ? *singleton : *(singleton = new (storage) T());
+#else
+  struct PrivateTag {};
+  static SingletonThreadLocal<T, PrivateTag> singleton;
   return singleton.get();
+#endif
 }
 
 RequestContext* RequestContext::get() {