blob: 322524b10e1e442284268323907165d69271dd61 [file] [log] [blame]
[email protected]444b8a3c2012-01-30 16:52:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]fe236c92008-09-11 23:37:545#if defined(OS_WIN)
initial.commitd7cae122008-07-26 21:49:386#include <windows.h>
7#include <process.h>
[email protected]fe236c92008-09-11 23:37:548#endif
initial.commitd7cae122008-07-26 21:49:389
avi9ceb8b82015-12-24 21:53:5910#include "base/macros.h"
[email protected]ac9ba8fe2010-12-30 18:08:3611#include "base/threading/simple_thread.h"
[email protected]1357c322010-12-30 22:18:5612#include "base/threading/thread_local_storage.h"
avi9ceb8b82015-12-24 21:53:5913#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3814#include "testing/gtest/include/gtest/gtest.h"
15
[email protected]fe236c92008-09-11 23:37:5416#if defined(OS_WIN)
17// Ignore warnings about ptr->int conversions that we use when
initial.commitd7cae122008-07-26 21:49:3818// storing ints into ThreadLocalStorage.
19#pragma warning(disable : 4311 4312)
[email protected]fe236c92008-09-11 23:37:5420#endif
initial.commitd7cae122008-07-26 21:49:3821
[email protected]1357c322010-12-30 22:18:5622namespace base {
23
24namespace {
25
[email protected]fe236c92008-09-11 23:37:5426const int kInitialTlsValue = 0x5555;
[email protected]e517d202011-11-29 21:38:2627const int kFinalTlsValue = 0x7777;
28// How many times must a destructor be called before we really are done.
29const int kNumberDestructorCallRepetitions = 3;
30
[email protected]444b8a3c2012-01-30 16:52:0931static ThreadLocalStorage::StaticSlot tls_slot = TLS_INITIALIZER;
[email protected]fe236c92008-09-11 23:37:5432
[email protected]1357c322010-12-30 22:18:5633class ThreadLocalStorageRunner : public DelegateSimpleThread::Delegate {
[email protected]fe236c92008-09-11 23:37:5434 public:
35 explicit ThreadLocalStorageRunner(int* tls_value_ptr)
36 : tls_value_ptr_(tls_value_ptr) {}
37
dcheng56488182014-10-21 10:54:5138 ~ThreadLocalStorageRunner() override {}
[email protected]fe236c92008-09-11 23:37:5439
dcheng56488182014-10-21 10:54:5140 void Run() override {
[email protected]fe236c92008-09-11 23:37:5441 *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);
[email protected]e517d202011-11-29 21:38:2652
53 *ptr = kFinalTlsValue + kNumberDestructorCallRepetitions;
[email protected]fe236c92008-09-11 23:37:5454 }
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);
[email protected]e517d202011-11-29 21:38:2664 // 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);
initial.commitd7cae122008-07-26 21:49:3873}
74
[email protected]1357c322010-12-30 22:18:5675} // namespace
initial.commitd7cae122008-07-26 21:49:3876
77TEST(ThreadLocalStorageTest, Basics) {
[email protected]6a6e6572008-08-20 22:54:5278 ThreadLocalStorage::Slot slot;
79 slot.Set(reinterpret_cast<void*>(123));
[email protected]cf762142009-08-03 15:59:2280 int value = reinterpret_cast<intptr_t>(slot.Get());
initial.commitd7cae122008-07-26 21:49:3881 EXPECT_EQ(value, 123);
82}
83
brettw9a19ddb2015-04-24 19:30:5084#if defined(THREAD_SANITIZER) || \
85 (defined(OS_WIN) && defined(ARCH_CPU_X86_64) && !defined(NDEBUG))
[email protected]18280ae42012-12-10 15:40:5886// 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.
scottmga3f851d2014-09-19 20:28:3791//
brettw9a19ddb2015-04-24 19:30:5092// It is disabled on Win x64 with incremental linking (i.e. "Debug") pending
93// resolution of http://crbug.com/251251.
[email protected]18280ae42012-12-10 15:40:5894#define MAYBE_TLSDestructors DISABLED_TLSDestructors
95#else
96#define MAYBE_TLSDestructors TLSDestructors
97#endif
98TEST(ThreadLocalStorageTest, MAYBE_TLSDestructors) {
initial.commitd7cae122008-07-26 21:49:3899 // 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;
initial.commitd7cae122008-07-26 21:49:38103 int values[kNumThreads];
[email protected]fe236c92008-09-11 23:37:54104 ThreadLocalStorageRunner* thread_delegates[kNumThreads];
[email protected]1357c322010-12-30 22:18:56105 DelegateSimpleThread* threads[kNumThreads];
initial.commitd7cae122008-07-26 21:49:38106
[email protected]6a6e6572008-08-20 22:54:52107 tls_slot.Initialize(ThreadLocalStorageCleanup);
initial.commitd7cae122008-07-26 21:49:38108
109 // Spawn the threads.
[email protected]fe236c92008-09-11 23:37:54110 for (int index = 0; index < kNumThreads; index++) {
initial.commitd7cae122008-07-26 21:49:38111 values[index] = kInitialTlsValue;
[email protected]fe236c92008-09-11 23:37:54112 thread_delegates[index] = new ThreadLocalStorageRunner(&values[index]);
[email protected]1357c322010-12-30 22:18:56113 threads[index] = new DelegateSimpleThread(thread_delegates[index],
114 "tls thread");
[email protected]fe236c92008-09-11 23:37:54115 threads[index]->Start();
initial.commitd7cae122008-07-26 21:49:38116 }
117
118 // Wait for the threads to finish.
119 for (int index = 0; index < kNumThreads; index++) {
[email protected]fe236c92008-09-11 23:37:54120 threads[index]->Join();
121 delete threads[index];
122 delete thread_delegates[index];
initial.commitd7cae122008-07-26 21:49:38123
[email protected]fe236c92008-09-11 23:37:54124 // Verify that the destructor was called and that we reset.
[email protected]e517d202011-11-29 21:38:26125 EXPECT_EQ(values[index], kFinalTlsValue);
initial.commitd7cae122008-07-26 21:49:38126 }
[email protected]8a79a212011-11-10 17:14:05127 tls_slot.Free(); // Stop doing callbacks to cleanup threads.
initial.commitd7cae122008-07-26 21:49:38128}
[email protected]1357c322010-12-30 22:18:56129
130} // namespace base