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.
4
5#if defined(OS_WIN)
6#include <windows.h>
7#include <process.h>
8#endif
9
10#include "base/macros.h"
11#include "base/threading/simple_thread.h"
12#include "base/threading/thread_local_storage.h"
13#include "build/build_config.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16#if defined(OS_WIN)
17// Ignore warnings about ptr->int conversions that we use when
18// storing ints into ThreadLocalStorage.
19#pragma warning(disable : 4311 4312)
20#endif
21
22namespace base {
23
24namespace {
25
26const int kInitialTlsValue = 0x5555;
27const int kFinalTlsValue = 0x7777;
28// How many times must a destructor be called before we really are done.
29const int kNumberDestructorCallRepetitions = 3;
30
31static ThreadLocalStorage::StaticSlot tls_slot = TLS_INITIALIZER;
32
33class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate {
34 public:
35  explicit ThreadLocalStorageRunner(int* tls_value_ptr)
36      : tls_value_ptr_(tls_value_ptr) {}
37
38  ~ThreadLocalStorageRunner() override {}
39
40  void Run() override {
41    *tls_value_ptr_ = kInitialTlsValue;
42    tls_slot.Set(tls_value_ptr_);
43
44    int *ptr = static_cast<int*>(tls_slot.Get());
45    EXPECT_EQ(ptr, tls_value_ptr_);
46    EXPECT_EQ(*ptr, kInitialTlsValue);
47    *tls_value_ptr_ = 0;
48
49    ptr = static_cast<int*>(tls_slot.Get());
50    EXPECT_EQ(ptr, tls_value_ptr_);
51    EXPECT_EQ(*ptr, 0);
52
53    *ptr = kFinalTlsValue + kNumberDestructorCallRepetitions;
54  }
55
56 private:
57  int* tls_value_ptr_;
58  DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner);
59};
60
61
62void ThreadLocalStorageCleanup(void *value) {
63  int *ptr = reinterpret_cast<int*>(value);
64  // Destructors should never be called with a NULL.
65  ASSERT_NE(reinterpret_cast<int*>(NULL), ptr);
66  if (*ptr == kFinalTlsValue)
67    return;  // We've been called enough times.
68  ASSERT_LT(kFinalTlsValue, *ptr);
69  ASSERT_GE(kFinalTlsValue + kNumberDestructorCallRepetitions, *ptr);
70  --*ptr;  // Move closer to our target.
71  // Tell tls that we're not done with this thread, and still need destruction.
72  tls_slot.Set(value);
73}
74
75}  // namespace
76
77TEST(ThreadLocalStorageTest, Basics) {
78  ThreadLocalStorage::Slot slot;
79  slot.Set(reinterpret_cast<void*>(123));
80  int value = reinterpret_cast<intptr_t>(slot.Get());
81  EXPECT_EQ(value, 123);
82}
83
84#if defined(THREAD_SANITIZER) || \
85    (defined(OS_WIN) && defined(ARCH_CPU_X86_64) && !defined(NDEBUG))
86// Do not run the test under ThreadSanitizer. Because this test iterates its
87// own TSD destructor for the maximum possible number of times, TSan can't jump
88// in after the last destructor invocation, therefore the destructor remains
89// unsynchronized with the following users of the same TSD slot. This results
90// in race reports between the destructor and functions in other tests.
91//
92// It is disabled on Win x64 with incremental linking (i.e. "Debug") pending
93// resolution of http://crbug.com/251251.
94#define MAYBE_TLSDestructors DISABLED_TLSDestructors
95#else
96#define MAYBE_TLSDestructors TLSDestructors
97#endif
98TEST(ThreadLocalStorageTest, MAYBE_TLSDestructors) {
99  // Create a TLS index with a destructor.  Create a set of
100  // threads that set the TLS, while the destructor cleans it up.
101  // After the threads finish, verify that the value is cleaned up.
102  const int kNumThreads = 5;
103  int values[kNumThreads];
104  ThreadLocalStorageRunner* thread_delegates[kNumThreads];
105  DelegateSimpleThread* threads[kNumThreads];
106
107  tls_slot.Initialize(ThreadLocalStorageCleanup);
108
109  // Spawn the threads.
110  for (int index = 0; index < kNumThreads; index++) {
111    values[index] = kInitialTlsValue;
112    thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]);
113    threads[index] = new DelegateSimpleThread(thread_delegates[index],
114                                              "tls thread");
115    threads[index]->Start();
116  }
117
118  // Wait for the threads to finish.
119  for (int index = 0; index < kNumThreads; index++) {
120    threads[index]->Join();
121    delete threads[index];
122    delete thread_delegates[index];
123
124    // Verify that the destructor was called and that we reset.
125    EXPECT_EQ(values[index], kFinalTlsValue);
126  }
127  tls_slot.Free();  // Stop doing callbacks to cleanup threads.
128}
129
130TEST(ThreadLocalStorageTest, TLSReclaim) {
131  // Creates and destroys many TLS slots and ensures they all zero-inited.
132  for (int i = 0; i < 1000; ++i) {
133    ThreadLocalStorage::Slot slot(nullptr);
134    EXPECT_EQ(nullptr, slot.Get());
135    slot.Set(reinterpret_cast<void*>(0xBAADF00D));
136    EXPECT_EQ(reinterpret_cast<void*>(0xBAADF00D), slot.Get());
137  }
138}
139
140}  // namespace base
141