OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" | |
6 #include "base/threading/platform_thread.h" | 5 #include "base/threading/platform_thread.h" |
7 | 6 |
8 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
9 | 8 |
10 namespace base { | 9 namespace base { |
11 | 10 |
12 // Trivial tests that thread runs and doesn't crash on create and join --------- | 11 // Trivial tests that thread runs and doesn't crash on create and join --------- |
13 | 12 |
14 class TrivialThread : public PlatformThread::Delegate { | 13 class TrivialThread : public PlatformThread::Delegate { |
15 public: | 14 public: |
16 TrivialThread() : did_run_(false) {} | 15 TrivialThread() : did_run_(false) {} |
17 | 16 |
18 virtual void ThreadMain() OVERRIDE { | 17 virtual void ThreadMain() { |
19 did_run_ = true; | 18 did_run_ = true; |
20 } | 19 } |
21 | 20 |
22 bool did_run() const { return did_run_; } | 21 bool did_run() const { return did_run_; } |
23 | 22 |
24 private: | 23 private: |
25 bool did_run_; | 24 bool did_run_; |
26 | 25 |
27 DISALLOW_COPY_AND_ASSIGN(TrivialThread); | 26 DISALLOW_COPY_AND_ASSIGN(TrivialThread); |
28 }; | 27 }; |
(...skipping 21 matching lines...) Expand all Loading... |
50 for (size_t n = 0; n < arraysize(thread); n++) | 49 for (size_t n = 0; n < arraysize(thread); n++) |
51 ASSERT_TRUE(thread[n].did_run()); | 50 ASSERT_TRUE(thread[n].did_run()); |
52 } | 51 } |
53 | 52 |
54 // Tests of basic thread functions --------------------------------------------- | 53 // Tests of basic thread functions --------------------------------------------- |
55 | 54 |
56 class FunctionTestThread : public TrivialThread { | 55 class FunctionTestThread : public TrivialThread { |
57 public: | 56 public: |
58 FunctionTestThread() : thread_id_(0) {} | 57 FunctionTestThread() : thread_id_(0) {} |
59 | 58 |
60 virtual void ThreadMain() OVERRIDE { | 59 virtual void ThreadMain() { |
61 thread_id_ = PlatformThread::CurrentId(); | 60 thread_id_ = PlatformThread::CurrentId(); |
62 PlatformThread::YieldCurrentThread(); | 61 PlatformThread::YieldCurrentThread(); |
63 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50)); | 62 PlatformThread::Sleep(TimeDelta::FromMilliseconds(50)); |
64 | 63 |
65 // Make sure that the thread ID is the same across calls. | |
66 EXPECT_EQ(thread_id_, PlatformThread::CurrentId()); | |
67 | |
68 TrivialThread::ThreadMain(); | 64 TrivialThread::ThreadMain(); |
69 } | 65 } |
70 | 66 |
71 PlatformThreadId thread_id() const { return thread_id_; } | 67 PlatformThreadId thread_id() const { return thread_id_; } |
72 | 68 |
73 private: | 69 private: |
74 PlatformThreadId thread_id_; | 70 PlatformThreadId thread_id_; |
75 | 71 |
76 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); | 72 DISALLOW_COPY_AND_ASSIGN(FunctionTestThread); |
77 }; | 73 }; |
78 | 74 |
79 TEST(PlatformThreadTest, Function) { | 75 TEST(PlatformThreadTest, Function) { |
80 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); | 76 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); |
81 | 77 |
82 FunctionTestThread thread; | 78 FunctionTestThread thread; |
83 PlatformThreadHandle handle = kNullThreadHandle; | 79 PlatformThreadHandle handle = kNullThreadHandle; |
84 | 80 |
85 ASSERT_FALSE(thread.did_run()); | 81 ASSERT_FALSE(thread.did_run()); |
86 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); | 82 ASSERT_TRUE(PlatformThread::Create(0, &thread, &handle)); |
87 PlatformThread::Join(handle); | 83 PlatformThread::Join(handle); |
88 ASSERT_TRUE(thread.did_run()); | 84 ASSERT_TRUE(thread.did_run()); |
89 EXPECT_NE(thread.thread_id(), main_thread_id); | 85 EXPECT_NE(thread.thread_id(), main_thread_id); |
90 | |
91 // Make sure that the thread ID is the same across calls. | |
92 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | |
93 } | 86 } |
94 | 87 |
95 TEST(PlatformThreadTest, FunctionTimesTen) { | 88 TEST(PlatformThreadTest, FunctionTimesTen) { |
96 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); | 89 PlatformThreadId main_thread_id = PlatformThread::CurrentId(); |
97 | 90 |
98 FunctionTestThread thread[10]; | 91 FunctionTestThread thread[10]; |
99 PlatformThreadHandle handle[arraysize(thread)]; | 92 PlatformThreadHandle handle[arraysize(thread)]; |
100 | 93 |
101 for (size_t n = 0; n < arraysize(thread); n++) | 94 for (size_t n = 0; n < arraysize(thread); n++) |
102 ASSERT_FALSE(thread[n].did_run()); | 95 ASSERT_FALSE(thread[n].did_run()); |
103 for (size_t n = 0; n < arraysize(thread); n++) | 96 for (size_t n = 0; n < arraysize(thread); n++) |
104 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); | 97 ASSERT_TRUE(PlatformThread::Create(0, &thread[n], &handle[n])); |
105 for (size_t n = 0; n < arraysize(thread); n++) | 98 for (size_t n = 0; n < arraysize(thread); n++) |
106 PlatformThread::Join(handle[n]); | 99 PlatformThread::Join(handle[n]); |
107 for (size_t n = 0; n < arraysize(thread); n++) { | 100 for (size_t n = 0; n < arraysize(thread); n++) { |
108 ASSERT_TRUE(thread[n].did_run()); | 101 ASSERT_TRUE(thread[n].did_run()); |
109 EXPECT_NE(thread[n].thread_id(), main_thread_id); | 102 EXPECT_NE(thread[n].thread_id(), main_thread_id); |
110 | |
111 // Make sure no two threads get the same ID. | |
112 for (size_t i = 0; i < n; ++i) { | |
113 EXPECT_NE(thread[i].thread_id(), thread[n].thread_id()); | |
114 } | |
115 } | 103 } |
116 | |
117 // Make sure that the thread ID is the same across calls. | |
118 EXPECT_EQ(main_thread_id, PlatformThread::CurrentId()); | |
119 } | 104 } |
120 | 105 |
121 } // namespace base | 106 } // namespace base |
OLD | NEW |