forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_local_unittest.cc
352 lines (273 loc) · 9.03 KB
/
thread_local_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/threading/thread_local.h"
#include "base/check_op.h"
#include "base/macros.h"
#include "base/optional.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/threading/simple_thread.h"
#include "base/threading/thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
class ThreadLocalTesterBase : public DelegateSimpleThreadPool::Delegate {
public:
typedef ThreadLocalPointer<char> TLPType;
ThreadLocalTesterBase(TLPType* tlp, WaitableEvent* done)
: tlp_(tlp), done_(done) {}
~ThreadLocalTesterBase() override = default;
protected:
TLPType* tlp_;
WaitableEvent* done_;
};
class SetThreadLocal : public ThreadLocalTesterBase {
public:
SetThreadLocal(TLPType* tlp, WaitableEvent* done)
: ThreadLocalTesterBase(tlp, done), val_(nullptr) {}
~SetThreadLocal() override = default;
void set_value(char* val) { val_ = val; }
void Run() override {
DCHECK(!done_->IsSignaled());
tlp_->Set(val_);
done_->Signal();
}
private:
char* val_;
};
class GetThreadLocal : public ThreadLocalTesterBase {
public:
GetThreadLocal(TLPType* tlp, WaitableEvent* done)
: ThreadLocalTesterBase(tlp, done), ptr_(nullptr) {}
~GetThreadLocal() override = default;
void set_ptr(char** ptr) { ptr_ = ptr; }
void Run() override {
DCHECK(!done_->IsSignaled());
*ptr_ = tlp_->Get();
done_->Signal();
}
private:
char** ptr_;
};
} // namespace
// In this test, we start 2 threads which will access a ThreadLocalPointer. We
// make sure the default is NULL, and the pointers are unique to the threads.
TEST(ThreadLocalTest, Pointer) {
DelegateSimpleThreadPool tp1("ThreadLocalTest tp1", 1);
DelegateSimpleThreadPool tp2("ThreadLocalTest tp1", 1);
tp1.Start();
tp2.Start();
ThreadLocalPointer<char> tlp;
static char* const kBogusPointer = reinterpret_cast<char*>(0x1234);
char* tls_val;
WaitableEvent done(WaitableEvent::ResetPolicy::MANUAL,
WaitableEvent::InitialState::NOT_SIGNALED);
GetThreadLocal getter(&tlp, &done);
getter.set_ptr(&tls_val);
// Check that both threads defaulted to NULL.
tls_val = kBogusPointer;
done.Reset();
tp1.AddWork(&getter);
done.Wait();
EXPECT_EQ(static_cast<char*>(nullptr), tls_val);
tls_val = kBogusPointer;
done.Reset();
tp2.AddWork(&getter);
done.Wait();
EXPECT_EQ(static_cast<char*>(nullptr), tls_val);
SetThreadLocal setter(&tlp, &done);
setter.set_value(kBogusPointer);
// Have thread 1 set their pointer value to kBogusPointer.
done.Reset();
tp1.AddWork(&setter);
done.Wait();
tls_val = nullptr;
done.Reset();
tp1.AddWork(&getter);
done.Wait();
EXPECT_EQ(kBogusPointer, tls_val);
// Make sure thread 2 is still NULL
tls_val = kBogusPointer;
done.Reset();
tp2.AddWork(&getter);
done.Wait();
EXPECT_EQ(static_cast<char*>(nullptr), tls_val);
// Set thread 2 to kBogusPointer + 1.
setter.set_value(kBogusPointer + 1);
done.Reset();
tp2.AddWork(&setter);
done.Wait();
tls_val = nullptr;
done.Reset();
tp2.AddWork(&getter);
done.Wait();
EXPECT_EQ(kBogusPointer + 1, tls_val);
// Make sure thread 1 is still kBogusPointer.
tls_val = nullptr;
done.Reset();
tp1.AddWork(&getter);
done.Wait();
EXPECT_EQ(kBogusPointer, tls_val);
tp1.JoinAll();
tp2.JoinAll();
}
namespace {
// A simple helper which sets the given boolean to true on destruction.
class SetTrueOnDestruction {
public:
SetTrueOnDestruction(bool* was_destroyed) : was_destroyed_(was_destroyed) {
CHECK(was_destroyed != nullptr);
}
~SetTrueOnDestruction() {
EXPECT_FALSE(*was_destroyed_);
*was_destroyed_ = true;
}
private:
bool* const was_destroyed_;
DISALLOW_COPY_AND_ASSIGN(SetTrueOnDestruction);
};
} // namespace
TEST(ThreadLocalTest, ThreadLocalOwnedPointerBasic) {
ThreadLocalOwnedPointer<SetTrueOnDestruction> tls_owned_pointer;
EXPECT_FALSE(tls_owned_pointer.Get());
bool was_destroyed1 = false;
tls_owned_pointer.Set(
std::make_unique<SetTrueOnDestruction>(&was_destroyed1));
EXPECT_FALSE(was_destroyed1);
EXPECT_TRUE(tls_owned_pointer.Get());
bool was_destroyed2 = false;
tls_owned_pointer.Set(
std::make_unique<SetTrueOnDestruction>(&was_destroyed2));
EXPECT_TRUE(was_destroyed1);
EXPECT_FALSE(was_destroyed2);
EXPECT_TRUE(tls_owned_pointer.Get());
tls_owned_pointer.Set(nullptr);
EXPECT_TRUE(was_destroyed1);
EXPECT_TRUE(was_destroyed2);
EXPECT_FALSE(tls_owned_pointer.Get());
}
TEST(ThreadLocalTest, ThreadLocalOwnedPointerFreedOnThreadExit) {
bool tls_was_destroyed = false;
ThreadLocalOwnedPointer<SetTrueOnDestruction> tls_owned_pointer;
Thread thread("TestThread");
thread.Start();
WaitableEvent tls_set;
thread.task_runner()->PostTask(
FROM_HERE, BindLambdaForTesting([&]() {
tls_owned_pointer.Set(
std::make_unique<SetTrueOnDestruction>(&tls_was_destroyed));
tls_set.Signal();
}));
tls_set.Wait();
EXPECT_FALSE(tls_was_destroyed);
thread.Stop();
EXPECT_TRUE(tls_was_destroyed);
}
TEST(ThreadLocalTest, ThreadLocalOwnedPointerCleansUpMainThreadOnDestruction) {
base::Optional<ThreadLocalOwnedPointer<SetTrueOnDestruction>>
tls_owned_pointer(base::in_place);
bool tls_was_destroyed_other = false;
Thread thread("TestThread");
thread.Start();
WaitableEvent tls_set;
thread.task_runner()->PostTask(
FROM_HERE, BindLambdaForTesting([&]() {
tls_owned_pointer->Set(
std::make_unique<SetTrueOnDestruction>(&tls_was_destroyed_other));
tls_set.Signal();
}));
tls_set.Wait();
bool tls_was_destroyed_main = false;
tls_owned_pointer->Set(
std::make_unique<SetTrueOnDestruction>(&tls_was_destroyed_main));
EXPECT_FALSE(tls_was_destroyed_other);
EXPECT_FALSE(tls_was_destroyed_main);
// Stopping the thread relinquishes its TLS (as in
// ThreadLocalOwnedPointerFreedOnThreadExit).
thread.Stop();
EXPECT_TRUE(tls_was_destroyed_other);
EXPECT_FALSE(tls_was_destroyed_main);
// Deleting the ThreadLocalOwnedPointer instance on the main thread is allowed
// iff that's the only thread with remaining storage (ref. disallowed use case
// in ThreadLocalOwnedPointerDeathIfDestroyedWithActiveThread below). In that
// case, the storage on the main thread is freed before releasing the TLS
// slot.
tls_owned_pointer.reset();
EXPECT_TRUE(tls_was_destroyed_main);
}
TEST(ThreadLocalTest, ThreadLocalOwnedPointerDeathIfDestroyedWithActiveThread) {
testing::FLAGS_gtest_death_test_style = "threadsafe";
base::Optional<ThreadLocalOwnedPointer<int>> tls_owned_pointer(
base::in_place);
Thread thread("TestThread");
thread.Start();
WaitableEvent tls_set;
thread.task_runner()->PostTask(
FROM_HERE, BindLambdaForTesting([&]() {
tls_owned_pointer->Set(std::make_unique<int>(1));
tls_set.Signal();
}));
tls_set.Wait();
EXPECT_DCHECK_DEATH({ tls_owned_pointer.reset(); });
}
TEST(ThreadLocalTest, ThreadLocalOwnedPointerMultiThreadedAndStaticStorage) {
constexpr int kNumThreads = 16;
static ThreadLocalOwnedPointer<SetTrueOnDestruction> tls_owned_pointer;
std::array<bool, kNumThreads> were_destroyed{};
std::array<std::unique_ptr<Thread>, kNumThreads> threads;
for (auto& thread : threads) {
thread = std::make_unique<Thread>("TestThread");
thread->Start();
}
for (const auto& thread : threads) {
// Waiting is unnecessary but enhances the likelihood of data races in the
// next steps.
thread->WaitUntilThreadStarted();
}
for (const bool was_destroyed : were_destroyed) {
EXPECT_FALSE(was_destroyed);
}
for (int i = 0; i < kNumThreads; ++i) {
threads[i]->task_runner()->PostTask(
FROM_HERE,
BindOnce(
[](bool* was_destroyed) {
tls_owned_pointer.Set(
std::make_unique<SetTrueOnDestruction>(was_destroyed));
},
&were_destroyed[i]));
}
static bool main_thread_was_destroyed = false;
// Even when the test is run multiple times in the same process: TLS should
// never be destroyed until static uninitialization.
EXPECT_FALSE(main_thread_was_destroyed);
tls_owned_pointer.Set(
std::make_unique<SetTrueOnDestruction>(&main_thread_was_destroyed));
for (const auto& thread : threads) {
thread->Stop();
}
for (const bool was_destroyed : were_destroyed) {
EXPECT_TRUE(was_destroyed);
}
// The main thread's TLS still wasn't destroyed (let the test unfold naturally
// through static uninitialization).
EXPECT_FALSE(main_thread_was_destroyed);
}
TEST(ThreadLocalTest, Boolean) {
{
ThreadLocalBoolean tlb;
EXPECT_FALSE(tlb.Get());
tlb.Set(false);
EXPECT_FALSE(tlb.Get());
tlb.Set(true);
EXPECT_TRUE(tlb.Get());
}
// Our slot should have been freed, we're all reset.
{
ThreadLocalBoolean tlb;
EXPECT_FALSE(tlb.Get());
}
}
} // namespace base