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/threading/simple_thread.h"
11#include "base/threading/thread_local_storage.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
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
19
20namespace base {
21
22namespace {
23
24const int kInitialTlsValue = 0x5555;
25const int kFinalTlsValue = 0x7777;
26// How many times must a destructor be called before we really are done.
27const int kNumberDestructorCallRepetitions = 3;
28
29static ThreadLocalStorage::StaticSlot tls_slot = TLS_INITIALIZER;
30
31class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate {
32 public:
33  explicit ThreadLocalStorageRunner(int* tls_value_ptr)
34      : tls_value_ptr_(tls_value_ptr) {}
35
36  virtual ~ThreadLocalStorageRunner() {}
37
38  virtual void Run() OVERRIDE {
39    *tls_value_ptr_ = kInitialTlsValue;
40    tls_slot.Set(tls_value_ptr_);
41
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;
46
47    ptr = static_cast<int*>(tls_slot.Get());
48    EXPECT_EQ(ptr, tls_value_ptr_);
49    EXPECT_EQ(*ptr, 0);
50
51    *ptr = kFinalTlsValue + kNumberDestructorCallRepetitions;
52  }
53
54 private:
55  int* tls_value_ptr_;
56  DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorageRunner);
57};
58
59
60void 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);
71}
72
73}  // namespace
74
75TEST(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);
80}
81
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.
90//
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
97TEST(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];
105
106  tls_slot.Initialize(ThreadLocalStorageCleanup);
107
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();
115  }
116
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];
122
123    // Verify that the destructor was called and that we reset.
124    EXPECT_EQ(values[index], kFinalTlsValue);
125  }
126  tls_slot.Free();  // Stop doing callbacks to cleanup threads.
127}
128
129}  // namespace base
130