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

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

Issue 2033243002: Introduce PlatformThread::Detach (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: CR Feedback Created 4 years, 6 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
« no previous file with comments | « base/threading/platform_thread_posix.cc ('k') | base/threading/platform_thread_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/synchronization/waitable_event.h" 9 #include "base/synchronization/waitable_event.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
11 #include "build/build_config.h" 11 #include "build/build_config.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 #if defined(OS_POSIX) 14 #if defined(OS_POSIX)
15 #include <sys/types.h> 15 #include <sys/types.h>
16 #include <unistd.h> 16 #include <unistd.h>
17 #include "base/threading/platform_thread_internal_posix.h" 17 #include "base/threading/platform_thread_internal_posix.h"
18 #elif defined(OS_WIN) 18 #elif defined(OS_WIN)
19 #include <windows.h> 19 #include <windows.h>
20 #endif 20 #endif
21 21
22 namespace base { 22 namespace base {
23 23
24 // Trivial tests that thread runs and doesn't crash on create and join --------- 24 // Trivial tests that thread runs and doesn't crash on create, join, or detach -
25 25
26 namespace { 26 namespace {
27 27
28 class TrivialThread : public PlatformThread::Delegate { 28 class TrivialThread : public PlatformThread::Delegate {
29 public: 29 public:
30 TrivialThread() : did_run_(false) {} 30 TrivialThread() : run_event_(WaitableEvent::ResetPolicy::MANUAL,
31 WaitableEvent::InitialState::NOT_SIGNALED) {}
31 32
32 void ThreadMain() override { did_run_ = true; } 33 void ThreadMain() override { run_event_.Signal(); }
33 34
34 bool did_run() const { return did_run_; } 35 WaitableEvent& run_event() { return run_event_; }
35 36
36 private: 37 private:
37 bool did_run_; 38 WaitableEvent run_event_;
38 39
39 DISALLOW_COPY_AND_ASSIGN(TrivialThread); 40 DISALLOW_COPY_AND_ASSIGN(TrivialThread);
40 }; 41 };
41 42
42 } // namespace 43 } // namespace
43 44
44 TEST(PlatformThreadTest, Trivial) { 45 TEST(PlatformThreadTest, TrivialJoin) {
45 TrivialThread thread; 46 TrivialThread thread;
46 PlatformThreadHandle handle; 47 PlatformThreadHandle handle;
47 48
48 ASSERT_FALSE(thread.did_run()); 49 ASSERT_FALSE(thread.run_event().IsSignaled());
49 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); 50 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
50 PlatformThread::Join(handle); 51 PlatformThread::Join(handle);
51 ASSERT_TRUE(thread.did_run()); 52 ASSERT_TRUE(thread.run_event().IsSignaled());
52 } 53 }
53 54
54 TEST(PlatformThreadTest, TrivialTimesTen) { 55 TEST(PlatformThreadTest, TrivialJoinTimesTen) {
55 TrivialThread thread[10]; 56 TrivialThread thread[10];
56 PlatformThreadHandle handle[arraysize(thread)]; 57 PlatformThreadHandle handle[arraysize(thread)];
57 58
58 for (size_t n = 0; n < arraysize(thread); n++) 59 for (size_t n = 0; n < arraysize(thread); n++)
59 ASSERT_FALSE(thread[n].did_run()); 60 ASSERT_FALSE(thread[n].run_event().IsSignaled());
60 for (size_t n = 0; n < arraysize(thread); n++) 61 for (size_t n = 0; n < arraysize(thread); n++)
61 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); 62 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
62 for (size_t n = 0; n < arraysize(thread); n++) 63 for (size_t n = 0; n < arraysize(thread); n++)
63 PlatformThread::Join(handle[n]); 64 PlatformThread::Join(handle[n]);
64 for (size_t n = 0; n < arraysize(thread); n++) 65 for (size_t n = 0; n < arraysize(thread); n++)
65 ASSERT_TRUE(thread[n].did_run()); 66 ASSERT_TRUE(thread[n].run_event().IsSignaled());
67 }
68
69 // The following detach tests are by nature racy. The run_event approximates the
70 // end and termination of the thread, but threads could persist shortly after
71 // the test completes.
72 TEST(PlatformThreadTest, TrivialDetach) {
73 TrivialThread thread;
74 PlatformThreadHandle handle;
75
76 ASSERT_FALSE(thread.run_event().IsSignaled());
77 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle));
78 PlatformThread::Detach(handle);
79 thread.run_event().Wait();
80 }
81
82 TEST(PlatformThreadTest, TrivialDetachTimesTen) {
83 TrivialThread thread[10];
84 PlatformThreadHandle handle[arraysize(thread)];
85
86 for (size_t n = 0; n < arraysize(thread); n++)
87 ASSERT_FALSE(thread[n].run_event().IsSignaled());
88 for (size_t n = 0; n < arraysize(thread); n++) {
89 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n]));
90 PlatformThread::Detach(handle[n]);
91 }
92 for (size_t n = 0; n < arraysize(thread); n++)
93 thread[n].run_event().Wait();
66 } 94 }
67 95
68 // Tests of basic thread functions --------------------------------------------- 96 // Tests of basic thread functions ---------------------------------------------
69 97
70 namespace { 98 namespace {
71 99
72 class FunctionTestThread : public PlatformThread::Delegate { 100 class FunctionTestThread : public PlatformThread::Delegate {
73 public: 101 public:
74 FunctionTestThread() 102 FunctionTestThread()
75 : thread_id_(kInvalidThreadId), 103 : thread_id_(kInvalidThreadId),
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 EXPECT_EQ(ThreadPriority::DISPLAY, 356 EXPECT_EQ(ThreadPriority::DISPLAY,
329 NiceValueToThreadPriority(kRealtimeAudioNiceValue + 1)); 357 NiceValueToThreadPriority(kRealtimeAudioNiceValue + 1));
330 EXPECT_EQ(ThreadPriority::REALTIME_AUDIO, 358 EXPECT_EQ(ThreadPriority::REALTIME_AUDIO,
331 NiceValueToThreadPriority(kRealtimeAudioNiceValue)); 359 NiceValueToThreadPriority(kRealtimeAudioNiceValue));
332 EXPECT_EQ(ThreadPriority::REALTIME_AUDIO, 360 EXPECT_EQ(ThreadPriority::REALTIME_AUDIO,
333 NiceValueToThreadPriority(kLowestNiceValue)); 361 NiceValueToThreadPriority(kLowestNiceValue));
334 } 362 }
335 #endif 363 #endif
336 364
337 } // namespace base 365 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/platform_thread_posix.cc ('k') | base/threading/platform_thread_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698