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