From ea538d3975c94c5d0a3d2eb2b0ab9f98fa182089 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <vital.had@gmail.com>
Date: Thu, 25 Jul 2024 11:10:58 +0800
Subject: [PATCH] Revert breaking commit re threadlocal

---
 folly/ThreadLocal.h                   | 8 ++++----
 folly/concurrency/memory/TLRefCount.h | 2 +-
 folly/settings/detail/SettingsImpl.h  | 2 +-
 folly/test/ThreadLocalTest.cpp        | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)

diff --git folly/ThreadLocal.h folly/ThreadLocal.h
index 114b30f20..a610d3d4f 100644
--- folly/ThreadLocal.h
+++ folly/ThreadLocal.h
@@ -67,9 +67,9 @@ class ThreadLocalPtr;
 template <class T, class Tag = void, class AccessMode = void>
 class ThreadLocal {
  public:
-  constexpr ThreadLocal() : constructor_([]() { return T(); }) {}
+  constexpr ThreadLocal() : constructor_([]() { return new T(); }) {}
 
-  template <typename F, std::enable_if_t<is_invocable_r_v<T, F>, int> = 0>
+  template <typename F, std::enable_if_t<is_invocable_r_v<T*, F>, int> = 0>
   explicit ThreadLocal(F&& constructor)
       : constructor_(std::forward<F>(constructor)) {}
 
@@ -100,13 +100,13 @@ class ThreadLocal {
   ThreadLocal& operator=(const ThreadLocal&) = delete;
 
   FOLLY_NOINLINE T* makeTlp() const {
-    auto const ptr = new T(constructor_());
+    auto const ptr = static_cast<T*>(constructor_());
     tlp_.reset(ptr);
     return ptr;
   }
 
   mutable ThreadLocalPtr<T, Tag, AccessMode> tlp_;
-  std::function<T()> constructor_;
+  std::function<void*()> constructor_;
 };
 
 /*
diff --git folly/concurrency/memory/TLRefCount.h folly/concurrency/memory/TLRefCount.h
index 88ecb2666..f0c24c72a 100644
--- folly/concurrency/memory/TLRefCount.h
+++ folly/concurrency/memory/TLRefCount.h
@@ -27,7 +27,7 @@ class TLRefCount {
   using Int = int64_t;
 
   TLRefCount()
-      : localCount_([&]() { return LocalRefCount(*this); }),
+      : localCount_([&]() { return new LocalRefCount(*this); }),
         collectGuard_(this, [](void*) {}) {}
 
   ~TLRefCount() noexcept {
diff --git folly/settings/detail/SettingsImpl.h folly/settings/detail/SettingsImpl.h
index 42ddb3048..740dfe963 100644
--- folly/settings/detail/SettingsImpl.h
+++ folly/settings/detail/SettingsImpl.h
@@ -387,7 +387,7 @@ class SettingCore : public SettingCoreBase {
         defaultValue_(std::move(defaultValue)),
         trivialStorage_(trivialStorage),
         localValue_([]() {
-          return cacheline_aligned<
+          return new cacheline_aligned<
               std::pair<Version, std::shared_ptr<Contents>>>(
               std::in_place, 0, nullptr);
         }) {
diff --git folly/test/ThreadLocalTest.cpp folly/test/ThreadLocalTest.cpp
index f9482538d..4fa1acd1f 100644
--- folly/test/ThreadLocalTest.cpp
+++ folly/test/ThreadLocalTest.cpp
@@ -261,7 +261,7 @@ TEST(ThreadLocal, NotDefaultConstructible) {
     explicit Object(int v) : value{v} {}
   };
   std::atomic<int> a{};
-  ThreadLocal<Object> o{[&a] { return Object(a++); }};
+  ThreadLocal<Object> o{[&a] { return new Object(a++); }};
   EXPECT_EQ(0, o->value);
   std::thread([&] { EXPECT_EQ(1, o->value); }).join();
 }
