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

Side by Side Diff: base/threading/platform_thread_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/compiler_specific.h" 5 #include "base/compiler_specific.h"
6 #include "base/macros.h"
7 #include "base/synchronization/waitable_event.h"
6 #include "base/threading/platform_thread.h" 8 #include "base/threading/platform_thread.h"
9 #include "testing/gtest/include/gtest/gtest.h"
7 10
8 #include "testing/gtest/include/gtest/gtest.h" 11 #if defined(OS_WIN)
12 #include <windows.h>
13 #endif
9 14
10 namespace base { 15 namespace base {
11 16
12 // Trivial tests that thread runs and doesn't crash on create and join --------- 17 // Trivial tests that thread runs and doesn't crash on create and join ---------
13 18
14 class TrivialThread : public PlatformThread::Delegate { 19 class TrivialThread : public PlatformThread::Delegate {
15 public: 20 public:
16 TrivialThread() : did_run_(false) {} 21 TrivialThread() : did_run_(false) {}
17 22
18 void ThreadMain() override { did_run_ = true; } 23 void ThreadMain() override { did_run_ = true; }
(...skipping 25 matching lines...) Expand all
44 for (size_t n = 0; n < arraysize(thread); n++) 49 for (size_t n = 0; n < arraysize(thread); n++)
45 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); 50 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
46 for (size_t n = 0; n < arraysize(thread); n++) 51 for (size_t n = 0; n < arraysize(thread); n++)
47 PlatformThread::Join(handle[n]); 52 PlatformThread::Join(handle[n]);
48 for (size_t n = 0; n < arraysize(thread); n++) 53 for (size_t n = 0; n < arraysize(thread); n++)
49 ASSERT_TRUE(thread[n].did_run()); 54 ASSERT_TRUE(thread[n].did_run());
50 } 55 }
51 56
52 // Tests of basic thread functions --------------------------------------------- 57 // Tests of basic thread functions ---------------------------------------------
53 58
54 class FunctionTestThread : public TrivialThread { 59 class FunctionTestThread : public PlatformThread::Delegate {
55 public: 60 public:
56 FunctionTestThread() : thread_id_(0) {} 61 FunctionTestThread()
62 : thread_id_(kInvalidThreadId),
63 thread_started_(true, false),
64 terminate_thread_(true, false),
65 done_(false) {}
66 ~FunctionTestThread() override {
67 EXPECT_TRUE(terminate_thread_.IsSignaled())
68 << "Need to mark thread for termination and join the underlying thread "
69 << "before destroying a FunctionTestThread as it owns the "
70 << "WaitableEvent blocking the underlying thread's main.";
71 }
57 72
73 // Grabs |thread_id_|, signals |thread_started_|, and then waits for
74 // |terminate_thread_| to be signaled before exiting.
58 void ThreadMain() override { 75 void ThreadMain() override {
59 thread_id_ = PlatformThread::CurrentId(); 76 thread_id_ = PlatformThread::CurrentId();
60 PlatformThread::YieldCurrentThread(); 77 EXPECT_NE(thread_id_, kInvalidThreadId);
61 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50));
62 78
63 // Make sure that the thread ID is the same across calls. 79 // Make sure that the thread ID is the same across calls.
64 EXPECT_EQ(thread_id_, PlatformThread::CurrentId()); 80 EXPECT_EQ(thread_id_, PlatformThread::CurrentId());
65 81
66 TrivialThread::ThreadMain(); 82 thread_started_.Signal();
83
84 terminate_thread_.Wait();
85
86 done_ = true;
67 } 87 }
68 88
69 PlatformThreadId thread_id() const { return thread_id_; } 89 PlatformThreadId thread_id() const {
90 EXPECT_TRUE(thread_started_.IsSignaled()) << "Thread ID still unknown";
91 return thread_id_;
92 }
93
94 bool IsRunning() const {
95 return thread_started_.IsSignaled() && !done_;
96 }
97
98 // Blocks until this thread is started.
99 void WaitForThreadStart() { thread_started_.Wait(); }
100
101 // Mark this thread for termination (callers must then join this thread to be
102 // guaranteed of termination).
103 void MarkForTermination() { terminate_thread_.Signal(); }
70 104
71 private: 105 private:
72 PlatformThreadId thread_id_; 106 PlatformThreadId thread_id_;
73 107
108 mutable WaitableEvent thread_started_;
109 WaitableEvent terminate_thread_;
110 bool done_;
111
74 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); 112 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread);
75 }; 113 };
76 114
77 TEST(PlatformThreadTest, Function) { 115 TEST(PlatformThreadTest, Function) {
78 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); 116 PlatformThreadId main_thread_id = PlatformThread::CurrentId();
79 117
80 FunctionTestThread thread; 118 FunctionTestThread thread;
81 PlatformThreadHandle handle; 119 PlatformThreadHandle handle;
82 120
83 ASSERT_FALSE(thread.did_run()); 121 ASSERT_FALSE(thread.IsRunning());
84 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 122 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
123 thread.WaitForThreadStart();
124 ASSERT_TRUE(thread.IsRunning());
125 EXPECT_NE(thread.thread_id(), main_thread_id);
126
127 thread.MarkForTermination();
85 PlatformThread::Join(handle); 128 PlatformThread::Join(handle);
86 ASSERT_TRUE(thread.did_run()); 129 ASSERT_FALSE(thread.IsRunning());
87 EXPECT_NE(thread.thread_id(), main_thread_id);
88 130
89 // Make sure that the thread ID is the same across calls. 131 // Make sure that the thread ID is the same across calls.
90 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); 132 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
91 } 133 }
92 134
93 TEST(PlatformThreadTest, FunctionTimesTen) { 135 TEST(PlatformThreadTest, FunctionTimesTen) {
94 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); 136 PlatformThreadId main_thread_id = PlatformThread::CurrentId();
95 137
96 FunctionTestThread thread[10]; 138 FunctionTestThread thread[10];
97 PlatformThreadHandle handle[arraysize(thread)]; 139 PlatformThreadHandle handle[arraysize(thread)];
98 140
99 for (size_t n = 0; n < arraysize(thread); n++) 141 for (size_t n = 0; n < arraysize(thread); n++)
100 ASSERT_FALSE(thread[n].did_run()); 142 ASSERT_FALSE(thread[n].IsRunning());
143
101 for (size_t n = 0; n < arraysize(thread); n++) 144 for (size_t n = 0; n < arraysize(thread); n++)
102 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); 145 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
103 for (size_t n = 0; n < arraysize(thread); n++) 146 for (size_t n = 0; n < arraysize(thread); n++)
104 PlatformThread::Join(handle[n]); 147 thread[n].WaitForThreadStart();
148
105 for (size_t n = 0; n < arraysize(thread); n++) { 149 for (size_t n = 0; n < arraysize(thread); n++) {
106 ASSERT_TRUE(thread[n].did_run()); 150 ASSERT_TRUE(thread[n].IsRunning());
107 EXPECT_NE(thread[n].thread_id(), main_thread_id); 151 EXPECT_NE(thread[n].thread_id(), main_thread_id);
108 152
109 // Make sure no two threads get the same ID. 153 // Make sure no two threads get the same ID.
110 for (size_t i = 0; i < n; ++i) { 154 for (size_t i = 0; i < n; ++i) {
111 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); 155 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id());
112 } 156 }
113 } 157 }
114 158
159 for (size_t n = 0; n < arraysize(thread); n++)
160 thread[n].MarkForTermination();
161 for (size_t n = 0; n < arraysize(thread); n++)
162 PlatformThread::Join(handle[n]);
163 for (size_t n = 0; n < arraysize(thread); n++)
164 ASSERT_FALSE(thread[n].IsRunning());
165
115 // Make sure that the thread ID is the same across calls. 166 // Make sure that the thread ID is the same across calls.
116 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); 167 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId());
117 } 168 }
118 169
170 namespace {
171
172 const ThreadPriority kThreadPriorityTestValues[] = {
173 // Disable non-normal priority toggling on POSIX as it appears to be broken
174 // (http://crbug.com/468793). This is prefered to disabling the tests altogether
175 // on POSIX as it at least provides coverage for running this code under
176 // "normal" priority.
177 #if !defined(OS_POSIX)
178 ThreadPriority::DISPLAY,
179 ThreadPriority::REALTIME_AUDIO,
180 // Keep BACKGROUND second to last to test backgrounding from other
181 // priorities.
182 ThreadPriority::BACKGROUND,
183 #endif // !defined(OS_POSIX)
184 // Keep NORMAL last to test unbackgrounding.
185 ThreadPriority::NORMAL
186 };
187
188 } // namespace
189
190 // Test changing another thread's priority.
191 // NOTE: This test is partially disabled on POSIX, see note above and
192 // http://crbug.com/468793.
193 TEST(PlatformThreadTest, ThreadPriorityOtherThread) {
194 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
195
196 // Confirm that the current thread's priority is as expected.
197 EXPECT_EQ(ThreadPriority::NORMAL,
198 PlatformThread::GetThreadPriority(current_handle));
199
200 // Create a test thread.
201 FunctionTestThread thread;
202 PlatformThreadHandle handle;
203 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
204 thread.WaitForThreadStart();
205 EXPECT_NE(thread.thread_id(), kInvalidThreadId);
206 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
207
208 // New threads should get normal priority by default.
209 EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle));
210
211 // Toggle each supported priority on the test thread and confirm it only
212 // affects it (and not the current thread).
213 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
214 SCOPED_TRACE(i);
215
216 // Alter and verify the test thread's priority.
217 PlatformThread::SetThreadPriority(handle, kThreadPriorityTestValues[i]);
218 EXPECT_EQ(kThreadPriorityTestValues[i],
219 PlatformThread::GetThreadPriority(handle));
220
221 // Make sure the current thread was otherwise unaffected.
222 EXPECT_EQ(ThreadPriority::NORMAL,
223 PlatformThread::GetThreadPriority(current_handle));
224 }
225
226 thread.MarkForTermination();
227 PlatformThread::Join(handle);
228 }
229
230 // Test changing the current thread's priority (which has different semantics on
231 // some platforms).
232 // NOTE: This test is partially disabled on POSIX, see note above and
233 // http://crbug.com/468793.
234 TEST(PlatformThreadTest, ThreadPriorityCurrentThread) {
235 PlatformThreadHandle current_handle(PlatformThread::CurrentHandle());
236
237 // Confirm that the current thread's priority is as expected.
238 EXPECT_EQ(ThreadPriority::NORMAL,
239 PlatformThread::GetThreadPriority(current_handle));
240
241 // Create a test thread for verification purposes only.
242 FunctionTestThread thread;
243 PlatformThreadHandle handle;
244 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
245 thread.WaitForThreadStart();
246 EXPECT_NE(thread.thread_id(), kInvalidThreadId);
247 EXPECT_NE(thread.thread_id(), PlatformThread::CurrentId());
248
249 // Confirm that the new thread's priority is as expected.
250 EXPECT_EQ(ThreadPriority::NORMAL, PlatformThread::GetThreadPriority(handle));
251
252 // Toggle each supported priority on the current thread and confirm it only
253 // affects it (and not the test thread).
254 for (size_t i = 0; i < arraysize(kThreadPriorityTestValues); ++i) {
255 SCOPED_TRACE(i);
256
257 // Alter and verify the current thread's priority.
258 PlatformThread::SetThreadPriority(current_handle,
259 kThreadPriorityTestValues[i]);
260 EXPECT_EQ(kThreadPriorityTestValues[i],
261 PlatformThread::GetThreadPriority(current_handle));
262
263 // Make sure the test thread was otherwise unaffected.
264 EXPECT_EQ(ThreadPriority::NORMAL,
265 PlatformThread::GetThreadPriority(handle));
266 }
267
268 // Restore current thread priority for follow-up tests.
269 PlatformThread::SetThreadPriority(current_handle, ThreadPriority::NORMAL);
270
271 thread.MarkForTermination();
272 PlatformThread::Join(handle);
273 }
274
119 } // namespace base 275 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698