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