2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/ThreadLocal.h>
24 #include <sys/types.h>
29 #include <condition_variable>
35 #include <unordered_map>
37 #include <glog/logging.h>
39 #include <folly/Baton.h>
40 #include <folly/Memory.h>
41 #include <folly/ThreadId.h>
42 #include <folly/experimental/io/FsUtil.h>
43 #include <folly/portability/GTest.h>
44 #include <folly/portability/Unistd.h>
46 using namespace folly;
55 static void customDeleter(Widget* w, TLPDestructionMode mode) {
56 totalVal_ += (mode == TLPDestructionMode::ALL_THREADS) ? 1000 : 1;
60 int Widget::totalVal_ = 0;
62 TEST(ThreadLocalPtr, BasicDestructor) {
63 Widget::totalVal_ = 0;
64 ThreadLocalPtr<Widget> w;
66 w.reset(new Widget());
69 EXPECT_EQ(10, Widget::totalVal_);
72 TEST(ThreadLocalPtr, CustomDeleter1) {
73 Widget::totalVal_ = 0;
75 ThreadLocalPtr<Widget> w;
77 w.reset(new Widget(), Widget::customDeleter);
80 EXPECT_EQ(11, Widget::totalVal_);
82 EXPECT_EQ(11, Widget::totalVal_);
85 TEST(ThreadLocalPtr, CustomDeleterOwnershipTransfer) {
86 Widget::totalVal_ = 0;
88 ThreadLocalPtr<Widget> w;
89 auto deleter = [](Widget* ptr) {
90 Widget::customDeleter(ptr, TLPDestructionMode::THIS_THREAD);
92 std::unique_ptr<Widget, decltype(deleter)> source(new Widget(), deleter);
93 std::thread([&w, &source]() {
94 w.reset(std::move(source));
97 EXPECT_EQ(11, Widget::totalVal_);
99 EXPECT_EQ(11, Widget::totalVal_);
102 TEST(ThreadLocalPtr, DefaultDeleterOwnershipTransfer) {
103 Widget::totalVal_ = 0;
105 ThreadLocalPtr<Widget> w;
106 auto source = folly::make_unique<Widget>();
107 std::thread([&w, &source]() {
108 w.reset(std::move(source));
111 EXPECT_EQ(10, Widget::totalVal_);
113 EXPECT_EQ(10, Widget::totalVal_);
116 TEST(ThreadLocalPtr, resetNull) {
117 ThreadLocalPtr<int> tl;
119 tl.reset(new int(4));
120 EXPECT_TRUE(static_cast<bool>(tl));
121 EXPECT_EQ(*tl.get(), 4);
126 TEST(ThreadLocalPtr, TestRelease) {
127 Widget::totalVal_ = 0;
128 ThreadLocalPtr<Widget> w;
129 std::unique_ptr<Widget> wPtr;
130 std::thread([&w, &wPtr]() {
131 w.reset(new Widget());
134 wPtr.reset(w.release());
136 EXPECT_EQ(0, Widget::totalVal_);
138 EXPECT_EQ(10, Widget::totalVal_);
141 TEST(ThreadLocalPtr, CreateOnThreadExit) {
142 Widget::totalVal_ = 0;
143 ThreadLocal<Widget> w;
144 ThreadLocalPtr<int> tl;
148 [&](int* ptr, TLPDestructionMode /* mode */) {
150 // This test ensures Widgets allocated here are not leaked.
152 ThreadLocal<Widget> wl;
156 EXPECT_EQ(2, Widget::totalVal_);
159 // Test deleting the ThreadLocalPtr object
160 TEST(ThreadLocalPtr, CustomDeleter2) {
161 Widget::totalVal_ = 0;
164 std::condition_variable cv;
170 State state = State::START;
172 ThreadLocalPtr<Widget> w;
173 t = std::thread([&]() {
174 w.reset(new Widget(), Widget::customDeleter);
177 // Notify main thread that we're done
179 std::unique_lock<std::mutex> lock(mutex);
184 // Wait for main thread to allow us to exit
186 std::unique_lock<std::mutex> lock(mutex);
187 while (state != State::EXIT) {
193 // Wait for main thread to start (and set w.get()->val_)
195 std::unique_lock<std::mutex> lock(mutex);
196 while (state != State::DONE) {
201 // Thread started but hasn't exited yet
202 EXPECT_EQ(0, Widget::totalVal_);
204 // Destroy ThreadLocalPtr<Widget> (by letting it go out of scope)
207 EXPECT_EQ(1010, Widget::totalVal_);
209 // Allow thread to exit
211 std::unique_lock<std::mutex> lock(mutex);
217 EXPECT_EQ(1010, Widget::totalVal_);
220 TEST(ThreadLocal, BasicDestructor) {
221 Widget::totalVal_ = 0;
222 ThreadLocal<Widget> w;
223 std::thread([&w]() { w->val_ += 10; }).join();
224 EXPECT_EQ(10, Widget::totalVal_);
227 TEST(ThreadLocal, SimpleRepeatDestructor) {
228 Widget::totalVal_ = 0;
230 ThreadLocal<Widget> w;
234 ThreadLocal<Widget> w;
237 EXPECT_EQ(20, Widget::totalVal_);
240 TEST(ThreadLocal, InterleavedDestructors) {
241 Widget::totalVal_ = 0;
242 std::unique_ptr<ThreadLocal<Widget>> w;
244 const int wVersionMax = 2;
247 auto th = std::thread([&]() {
248 int wVersionPrev = 0;
251 std::lock_guard<std::mutex> g(lock);
252 if (wVersion > wVersionMax) {
255 if (wVersion > wVersionPrev) {
256 // We have a new version of w, so it should be initialized to zero
257 EXPECT_EQ((*w)->val_, 0);
261 std::lock_guard<std::mutex> g(lock);
262 wVersionPrev = wVersion;
267 FOR_EACH_RANGE(i, 0, wVersionMax) {
270 std::lock_guard<std::mutex> g(lock);
272 w.reset(new ThreadLocal<Widget>());
276 std::lock_guard<std::mutex> g(lock);
277 if (thIter > thIterPrev) {
283 std::lock_guard<std::mutex> g(lock);
284 wVersion = wVersionMax + 1;
287 EXPECT_EQ(wVersionMax * 10, Widget::totalVal_);
290 class SimpleThreadCachedInt {
293 ThreadLocal<int,NewTag> val_;
302 for (const auto& i : val_.accessAllThreads()) {
309 TEST(ThreadLocalPtr, AccessAllThreadsCounter) {
310 const int kNumThreads = 10;
311 SimpleThreadCachedInt stci;
312 std::atomic<bool> run(true);
313 std::atomic<int> totalAtomic(0);
314 std::vector<std::thread> threads;
315 for (int i = 0; i < kNumThreads; ++i) {
316 threads.push_back(std::thread([&,i]() {
318 totalAtomic.fetch_add(1);
319 while (run.load()) { usleep(100); }
322 while (totalAtomic.load() != kNumThreads) { usleep(100); }
323 EXPECT_EQ(kNumThreads, stci.read());
325 for (auto& t : threads) {
330 TEST(ThreadLocal, resetNull) {
332 tl.reset(new int(4));
333 EXPECT_EQ(*tl.get(), 4);
335 EXPECT_EQ(*tl.get(), 0);
336 tl.reset(new int(5));
337 EXPECT_EQ(*tl.get(), 5);
344 folly::ThreadLocal<int, Tag> tl;
348 TEST(ThreadLocal, Movable1) {
351 EXPECT_TRUE(a.tl.get() != b.tl.get());
355 EXPECT_TRUE(a.tl.get() != b.tl.get());
358 TEST(ThreadLocal, Movable2) {
359 std::map<int, Foo> map;
367 for (auto& m : map) {
368 tls.insert(m.second.tl.get());
371 // Make sure that we have 4 different instances of *tl
372 EXPECT_EQ(4, tls.size());
377 constexpr size_t kFillObjectSize = 300;
379 std::atomic<uint64_t> gDestroyed;
382 * Fill a chunk of memory with a unique-ish pattern that includes the thread id
383 * (so deleting one of these from another thread would cause a failure)
385 * Verify it explicitly and on destruction.
389 explicit FillObject(uint64_t idx) : idx_(idx) {
391 for (size_t i = 0; i < kFillObjectSize; ++i) {
398 for (size_t i = 0; i < kFillObjectSize; ++i) {
399 CHECK_EQ(v, data_[i]);
408 uint64_t val() const {
409 return (idx_ << 40) | folly::getCurrentThreadID();
413 uint64_t data_[kFillObjectSize];
418 TEST(ThreadLocal, Stress) {
419 static constexpr size_t numFillObjects = 250;
420 std::array<ThreadLocalPtr<FillObject>, numFillObjects> objects;
422 static constexpr size_t numThreads = 32;
423 static constexpr size_t numReps = 20;
425 std::vector<std::thread> threads;
426 threads.reserve(numThreads);
428 for (size_t k = 0; k < numThreads; ++k) {
429 threads.emplace_back([&objects] {
430 for (size_t rep = 0; rep < numReps; ++rep) {
431 for (size_t i = 0; i < objects.size(); ++i) {
432 objects[i].reset(new FillObject(rep * objects.size() + i));
433 std::this_thread::sleep_for(std::chrono::microseconds(100));
435 for (size_t i = 0; i < objects.size(); ++i) {
442 for (auto& t : threads) {
446 EXPECT_EQ(numFillObjects * numThreads * numReps, gDestroyed);
449 // Yes, threads and fork don't mix
450 // (http://cppwisdom.quora.com/Why-threads-and-fork-dont-mix) but if you're
451 // stupid or desperate enough to try, we shouldn't stand in your way.
455 HoldsOne() : value_(1) { }
456 // Do an actual access to catch the buggy case where this == nullptr
457 int value() const { return value_; }
462 struct HoldsOneTag {};
464 ThreadLocal<HoldsOne, HoldsOneTag> ptr;
468 for (auto& p : ptr.accessAllThreads()) {
476 #ifdef FOLLY_HAVE_PTHREAD_ATFORK
477 TEST(ThreadLocal, Fork) {
478 EXPECT_EQ(1, ptr->value()); // ensure created
479 EXPECT_EQ(1, totalValue());
480 // Spawn a new thread
483 bool started = false;
484 std::condition_variable startedCond;
485 bool stopped = false;
486 std::condition_variable stoppedCond;
488 std::thread t([&] () {
489 EXPECT_EQ(1, ptr->value()); // ensure created
491 std::unique_lock<std::mutex> lock(mutex);
493 startedCond.notify_all();
496 std::unique_lock<std::mutex> lock(mutex);
498 stoppedCond.wait(lock);
504 std::unique_lock<std::mutex> lock(mutex);
506 startedCond.wait(lock);
510 EXPECT_EQ(2, totalValue());
515 int v = totalValue();
517 // exit successfully if v == 1 (one thread)
518 // diagnostic error code otherwise :)
524 } else if (pid > 0) {
527 EXPECT_EQ(pid, waitpid(pid, &status, 0));
528 EXPECT_TRUE(WIFEXITED(status));
529 EXPECT_EQ(0, WEXITSTATUS(status));
531 EXPECT_TRUE(false) << "fork failed";
534 EXPECT_EQ(2, totalValue());
537 std::unique_lock<std::mutex> lock(mutex);
539 stoppedCond.notify_all();
544 EXPECT_EQ(1, totalValue());
549 struct HoldsOneTag2 {};
551 TEST(ThreadLocal, Fork2) {
552 // A thread-local tag that was used in the parent from a *different* thread
553 // (but not the forking thread) would cause the child to hang in a
554 // ThreadLocalPtr's object destructor. Yeah.
555 ThreadLocal<HoldsOne, HoldsOneTag2> p;
557 // use tag in different thread
558 std::thread t([&p] { p.get(); });
564 ThreadLocal<HoldsOne, HoldsOneTag2> q;
568 } else if (pid > 0) {
570 EXPECT_EQ(pid, waitpid(pid, &status, 0));
571 EXPECT_TRUE(WIFEXITED(status));
572 EXPECT_EQ(0, WEXITSTATUS(status));
574 EXPECT_TRUE(false) << "fork failed";
578 // Elide this test when using any sanitizer. Otherwise, the dlopen'ed code
579 // would end up running without e.g., ASAN-initialized data structures and
580 // failing right away.
581 #if !defined FOLLY_SANITIZE_ADDRESS && !defined UNDEFINED_SANITIZER && \
582 !defined FOLLY_SANITIZE_THREAD
584 TEST(ThreadLocal, SharedLibrary) {
585 auto exe = fs::executable_path();
586 auto lib = exe.parent_path() / "thread_local_test_lib.so";
587 auto handle = dlopen(lib.string().c_str(), RTLD_LAZY);
588 EXPECT_NE(nullptr, handle);
590 typedef void (*useA_t)();
592 useA_t useA = (useA_t) dlsym(handle, "useA");
594 const char *dlsym_error = dlerror();
595 EXPECT_EQ(nullptr, dlsym_error);
599 folly::Baton<> b11, b12, b21, b22;
601 std::thread t1([&]() {
607 std::thread t2([&]() {
628 namespace folly { namespace threadlocal_detail {
629 struct PthreadKeyUnregisterTester {
630 PthreadKeyUnregister p;
631 constexpr PthreadKeyUnregisterTester() = default;
635 TEST(ThreadLocal, UnregisterClassHasConstExprCtor) {
636 folly::threadlocal_detail::PthreadKeyUnregisterTester x;