Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: base/threading/thread_local_unittest.cc

Issue 1124763003: Update from https://crrev.com/327068 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: update nacl, buildtools, fix display_change_notifier_unittest Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/logging.h" 5 #include "base/logging.h"
6 #include "base/threading/simple_thread.h" 6 #include "base/threading/simple_thread.h"
7 #include "base/threading/thread_local.h" 7 #include "base/threading/thread_local.h"
8 #include "base/synchronization/waitable_event.h" 8 #include "base/synchronization/waitable_event.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 namespace { 13 namespace {
14 14
15 class ThreadLocalTesterBase : public base::DelegateSimpleThreadPool::Delegate { 15 class ThreadLocalTesterBase : public base::DelegateSimpleThreadPool::Delegate {
16 public: 16 public:
17 typedef base::ThreadLocalPointer<ThreadLocalTesterBase> TLPType; 17 typedef base::ThreadLocalPointer<char> TLPType;
18 18
19 ThreadLocalTesterBase(TLPType* tlp, base::WaitableEvent* done) 19 ThreadLocalTesterBase(TLPType* tlp, base::WaitableEvent* done)
20 : tlp_(tlp), 20 : tlp_(tlp),
21 done_(done) { 21 done_(done) {
22 } 22 }
23 ~ThreadLocalTesterBase() override {} 23 ~ThreadLocalTesterBase() override {}
24 24
25 protected: 25 protected:
26 TLPType* tlp_; 26 TLPType* tlp_;
27 base::WaitableEvent* done_; 27 base::WaitableEvent* done_;
28 }; 28 };
29 29
30 class SetThreadLocal : public ThreadLocalTesterBase { 30 class SetThreadLocal : public ThreadLocalTesterBase {
31 public: 31 public:
32 SetThreadLocal(TLPType* tlp, base::WaitableEvent* done) 32 SetThreadLocal(TLPType* tlp, base::WaitableEvent* done)
33 : ThreadLocalTesterBase(tlp, done), 33 : ThreadLocalTesterBase(tlp, done),
34 val_(NULL) { 34 val_(NULL) {
35 } 35 }
36 ~SetThreadLocal() override {} 36 ~SetThreadLocal() override {}
37 37
38 void set_value(ThreadLocalTesterBase* val) { val_ = val; } 38 void set_value(char* val) { val_ = val; }
39 39
40 void Run() override { 40 void Run() override {
41 DCHECK(!done_->IsSignaled()); 41 DCHECK(!done_->IsSignaled());
42 tlp_->Set(val_); 42 tlp_->Set(val_);
43 done_->Signal(); 43 done_->Signal();
44 } 44 }
45 45
46 private: 46 private:
47 ThreadLocalTesterBase* val_; 47 char* val_;
48 }; 48 };
49 49
50 class GetThreadLocal : public ThreadLocalTesterBase { 50 class GetThreadLocal : public ThreadLocalTesterBase {
51 public: 51 public:
52 GetThreadLocal(TLPType* tlp, base::WaitableEvent* done) 52 GetThreadLocal(TLPType* tlp, base::WaitableEvent* done)
53 : ThreadLocalTesterBase(tlp, done), 53 : ThreadLocalTesterBase(tlp, done),
54 ptr_(NULL) { 54 ptr_(NULL) {
55 } 55 }
56 ~GetThreadLocal() override {} 56 ~GetThreadLocal() override {}
57 57
58 void set_ptr(ThreadLocalTesterBase** ptr) { ptr_ = ptr; } 58 void set_ptr(char** ptr) { ptr_ = ptr; }
59 59
60 void Run() override { 60 void Run() override {
61 DCHECK(!done_->IsSignaled()); 61 DCHECK(!done_->IsSignaled());
62 *ptr_ = tlp_->Get(); 62 *ptr_ = tlp_->Get();
63 done_->Signal(); 63 done_->Signal();
64 } 64 }
65 65
66 private: 66 private:
67 ThreadLocalTesterBase** ptr_; 67 char** ptr_;
68 }; 68 };
69 69
70 } // namespace 70 } // namespace
71 71
72 // In this test, we start 2 threads which will access a ThreadLocalPointer. We 72 // In this test, we start 2 threads which will access a ThreadLocalPointer. We
73 // make sure the default is NULL, and the pointers are unique to the threads. 73 // make sure the default is NULL, and the pointers are unique to the threads.
74 TEST(ThreadLocalTest, Pointer) { 74 TEST(ThreadLocalTest, Pointer) {
75 base::DelegateSimpleThreadPool tp1("ThreadLocalTest tp1", 1); 75 base::DelegateSimpleThreadPool tp1("ThreadLocalTest tp1", 1);
76 base::DelegateSimpleThreadPool tp2("ThreadLocalTest tp1", 1); 76 base::DelegateSimpleThreadPool tp2("ThreadLocalTest tp1", 1);
77 tp1.Start(); 77 tp1.Start();
78 tp2.Start(); 78 tp2.Start();
79 79
80 base::ThreadLocalPointer<ThreadLocalTesterBase> tlp; 80 base::ThreadLocalPointer<char> tlp;
81 81
82 static ThreadLocalTesterBase* const kBogusPointer = 82 static char* const kBogusPointer = reinterpret_cast<char*>(0x1234);
83 reinterpret_cast<ThreadLocalTesterBase*>(0x1234);
84 83
85 ThreadLocalTesterBase* tls_val; 84 char* tls_val;
86 base::WaitableEvent done(true, false); 85 base::WaitableEvent done(true, false);
87 86
88 GetThreadLocal getter(&tlp, &done); 87 GetThreadLocal getter(&tlp, &done);
89 getter.set_ptr(&tls_val); 88 getter.set_ptr(&tls_val);
90 89
91 // Check that both threads defaulted to NULL. 90 // Check that both threads defaulted to NULL.
92 tls_val = kBogusPointer; 91 tls_val = kBogusPointer;
93 done.Reset(); 92 done.Reset();
94 tp1.AddWork(&getter); 93 tp1.AddWork(&getter);
95 done.Wait(); 94 done.Wait();
96 EXPECT_EQ(static_cast<ThreadLocalTesterBase*>(NULL), tls_val); 95 EXPECT_EQ(static_cast<char*>(NULL), tls_val);
97 96
98 tls_val = kBogusPointer; 97 tls_val = kBogusPointer;
99 done.Reset(); 98 done.Reset();
100 tp2.AddWork(&getter); 99 tp2.AddWork(&getter);
101 done.Wait(); 100 done.Wait();
102 EXPECT_EQ(static_cast<ThreadLocalTesterBase*>(NULL), tls_val); 101 EXPECT_EQ(static_cast<char*>(NULL), tls_val);
103 102
104 103
105 SetThreadLocal setter(&tlp, &done); 104 SetThreadLocal setter(&tlp, &done);
106 setter.set_value(kBogusPointer); 105 setter.set_value(kBogusPointer);
107 106
108 // Have thread 1 set their pointer value to kBogusPointer. 107 // Have thread 1 set their pointer value to kBogusPointer.
109 done.Reset(); 108 done.Reset();
110 tp1.AddWork(&setter); 109 tp1.AddWork(&setter);
111 done.Wait(); 110 done.Wait();
112 111
113 tls_val = NULL; 112 tls_val = NULL;
114 done.Reset(); 113 done.Reset();
115 tp1.AddWork(&getter); 114 tp1.AddWork(&getter);
116 done.Wait(); 115 done.Wait();
117 EXPECT_EQ(kBogusPointer, tls_val); 116 EXPECT_EQ(kBogusPointer, tls_val);
118 117
119 // Make sure thread 2 is still NULL 118 // Make sure thread 2 is still NULL
120 tls_val = kBogusPointer; 119 tls_val = kBogusPointer;
121 done.Reset(); 120 done.Reset();
122 tp2.AddWork(&getter); 121 tp2.AddWork(&getter);
123 done.Wait(); 122 done.Wait();
124 EXPECT_EQ(static_cast<ThreadLocalTesterBase*>(NULL), tls_val); 123 EXPECT_EQ(static_cast<char*>(NULL), tls_val);
125 124
126 // Set thread 2 to kBogusPointer + 1. 125 // Set thread 2 to kBogusPointer + 1.
127 setter.set_value(kBogusPointer + 1); 126 setter.set_value(kBogusPointer + 1);
128 127
129 done.Reset(); 128 done.Reset();
130 tp2.AddWork(&setter); 129 tp2.AddWork(&setter);
131 done.Wait(); 130 done.Wait();
132 131
133 tls_val = NULL; 132 tls_val = NULL;
134 done.Reset(); 133 done.Reset();
(...skipping 25 matching lines...) Expand all
160 } 159 }
161 160
162 // Our slot should have been freed, we're all reset. 161 // Our slot should have been freed, we're all reset.
163 { 162 {
164 base::ThreadLocalBoolean tlb; 163 base::ThreadLocalBoolean tlb;
165 EXPECT_FALSE(tlb.Get()); 164 EXPECT_FALSE(tlb.Get());
166 } 165 }
167 } 166 }
168 167
169 } // namespace base 168 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698