Port Android relocation packer to chromium build
[chromium-blink-merge.git] / base / threading / thread_local_storage_unittest.cc
blobb53f57771580cf4cabca85e02c3f8e80cbc52314
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #if defined(OS_WIN)
6 #include <windows.h>
7 #include <process.h>
8 #endif
10 #include "base/threading/simple_thread.h"
11 #include "base/threading/thread_local_storage.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #if defined(OS_WIN)
15 // Ignore warnings about ptr->int conversions that we use when
16 // storing ints into ThreadLocalStorage.
17 #pragma warning(disable : 4311 4312)
18 #endif
20 namespace base {
22 namespace {
24 const int kInitialTlsValue = 0x5555;
25 const int kFinalTlsValue = 0x7777;
26 // How many times must a destructor be called before we really are done.
27 const int kNumberDestructorCallRepetitions = 3;
29 static ThreadLocalStorage::StaticSlot tls_slot = TLS_INITIALIZER;
31 class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate {
32 public:
33 explicit ThreadLocalStorageRunner(int* tls_value_ptr)
34 : tls_value_ptr_(tls_value_ptr) {}
36 ~ThreadLocalStorageRunner() override {}
38 void Run() override {
39 *tls_value_ptr_ = kInitialTlsValue;
40 tls_slot.Set(tls_value_ptr_);
42 int *ptr = static_cast<int*>(tls_slot.Get());
43 EXPECT_EQ(ptr, tls_value_ptr_);
44 EXPECT_EQ(*ptr, kInitialTlsValue);
45 *tls_value_ptr_ = 0;
47 ptr = static_cast<int*>(tls_slot.Get());
48 EXPECT_EQ(ptr, tls_value_ptr_);
49 EXPECT_EQ(*ptr, 0);
51 *ptr = kFinalTlsValue + kNumberDestructorCallRepetitions;
54 private:
55 int* tls_value_ptr_;
56 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner);
60 void ThreadLocalStorageCleanup(void *value) {
61 int *ptr = reinterpret_cast<int*>(value);
62 // Destructors should never be called with a NULL.
63 ASSERT_NE(reinterpret_cast<int*>(NULL), ptr);
64 if (*ptr == kFinalTlsValue)
65 return; // We've been called enough times.
66 ASSERT_LT(kFinalTlsValue, *ptr);
67 ASSERT_GE(kFinalTlsValue + kNumberDestructorCallRepetitions, *ptr);
68 --*ptr; // Move closer to our target.
69 // Tell tls that we're not done with this thread, and still need destruction.
70 tls_slot.Set(value);
73 } // namespace
75 TEST(ThreadLocalStorageTest, Basics) {
76 ThreadLocalStorage::Slot slot;
77 slot.Set(reinterpret_cast<void*>(123));
78 int value = reinterpret_cast<intptr_t>(slot.Get());
79 EXPECT_EQ(value, 123);
82 #if defined(THREAD_SANITIZER) || \
83 (defined(OS_WIN) && defined(ARCH_CPU_X86_64) && \
84 defined(INCREMENTAL_LINKING))
85 // Do not run the test under ThreadSanitizer. Because this test iterates its
86 // own TSD destructor for the maximum possible number of times, TSan can't jump
87 // in after the last destructor invocation, therefore the destructor remains
88 // unsynchronized with the following users of the same TSD slot. This results
89 // in race reports between the destructor and functions in other tests.
91 // It is disabled on Win x64 with incremental linking pending resolution of
92 // http://crbug.com/251251.
93 #define MAYBE_TLSDestructors DISABLED_TLSDestructors
94 #else
95 #define MAYBE_TLSDestructors TLSDestructors
96 #endif
97 TEST(ThreadLocalStorageTest, MAYBE_TLSDestructors) {
98 // Create a TLS index with a destructor. Create a set of
99 // threads that set the TLS, while the destructor cleans it up.
100 // After the threads finish, verify that the value is cleaned up.
101 const int kNumThreads = 5;
102 int values[kNumThreads];
103 ThreadLocalStorageRunner* thread_delegates[kNumThreads];
104 DelegateSimpleThread* threads[kNumThreads];
106 tls_slot.Initialize(ThreadLocalStorageCleanup);
108 // Spawn the threads.
109 for (int index = 0; index < kNumThreads; index++) {
110 values[index] = kInitialTlsValue;
111 thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]);
112 threads[index] = new DelegateSimpleThread(thread_delegates[index],
113 "tls thread");
114 threads[index]->Start();
117 // Wait for the threads to finish.
118 for (int index = 0; index < kNumThreads; index++) {
119 threads[index]->Join();
120 delete threads[index];
121 delete thread_delegates[index];
123 // Verify that the destructor was called and that we reset.
124 EXPECT_EQ(values[index], kFinalTlsValue);
126 tls_slot.Free(); // Stop doing callbacks to cleanup threads.
129 } // namespace base