OLD | NEW |
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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 | 122 |
123 } // namespace | 123 } // namespace |
124 | 124 |
125 TEST(SimpleThreadTest, CreateAndJoin) { | 125 TEST(SimpleThreadTest, CreateAndJoin) { |
126 int stack_int = 0; | 126 int stack_int = 0; |
127 | 127 |
128 SetIntRunner runner(&stack_int, 7); | 128 SetIntRunner runner(&stack_int, 7); |
129 EXPECT_EQ(0, stack_int); | 129 EXPECT_EQ(0, stack_int); |
130 | 130 |
131 DelegateSimpleThread thread(&runner, "int_setter"); | 131 DelegateSimpleThread thread(&runner, "int_setter"); |
| 132 EXPECT_FALSE(thread.HasBeenStarted()); |
| 133 EXPECT_FALSE(thread.HasBeenJoined()); |
132 EXPECT_EQ(0, stack_int); | 134 EXPECT_EQ(0, stack_int); |
133 | 135 |
134 thread.Start(); | 136 thread.Start(); |
| 137 EXPECT_TRUE(thread.HasBeenStarted()); |
| 138 EXPECT_FALSE(thread.HasBeenJoined()); |
| 139 |
135 thread.Join(); | 140 thread.Join(); |
136 | 141 EXPECT_TRUE(thread.HasBeenStarted()); |
| 142 EXPECT_TRUE(thread.HasBeenJoined()); |
137 EXPECT_EQ(7, stack_int); | 143 EXPECT_EQ(7, stack_int); |
138 } | 144 } |
139 | 145 |
140 TEST(SimpleThreadTest, WaitForEvent) { | 146 TEST(SimpleThreadTest, WaitForEvent) { |
141 // Create a thread, and wait for it to signal us. | 147 // Create a thread, and wait for it to signal us. |
142 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, | 148 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, |
143 WaitableEvent::InitialState::NOT_SIGNALED); | 149 WaitableEvent::InitialState::NOT_SIGNALED); |
144 | 150 |
145 WaitEventRunner runner(&event); | 151 WaitEventRunner runner(&event); |
146 DelegateSimpleThread thread(&runner, "event_waiter"); | 152 DelegateSimpleThread thread(&runner, "event_waiter"); |
147 | 153 |
148 EXPECT_FALSE(event.IsSignaled()); | 154 EXPECT_FALSE(event.IsSignaled()); |
149 thread.Start(); | 155 thread.Start(); |
150 event.Wait(); | 156 event.Wait(); |
151 EXPECT_TRUE(event.IsSignaled()); | 157 EXPECT_TRUE(event.IsSignaled()); |
152 thread.Join(); | 158 thread.Join(); |
153 } | 159 } |
154 | 160 |
155 TEST(SimpleThreadTest, NonJoinableStartAndDieOnJoin) { | 161 TEST(SimpleThreadTest, NonJoinableStartAndDieOnJoin) { |
156 ControlledRunner runner; | 162 ControlledRunner runner; |
157 | 163 |
158 SimpleThread::Options options; | 164 SimpleThread::Options options; |
159 options.joinable = false; | 165 options.joinable = false; |
160 DelegateSimpleThread thread(&runner, "non_joinable", options); | 166 DelegateSimpleThread thread(&runner, "non_joinable", options); |
161 | 167 |
| 168 EXPECT_FALSE(thread.HasBeenStarted()); |
162 thread.Start(); | 169 thread.Start(); |
| 170 EXPECT_TRUE(thread.HasBeenStarted()); |
163 | 171 |
164 // Wait until Run() is invoked. | 172 // Note: this is not quite the same as |thread.HasBeenStarted()| which |
| 173 // represents ThreadMain() getting ready to invoke Run() whereas |
| 174 // |runner.WaitUntilStarted()| ensures Run() was actually invoked. |
165 runner.WaitUntilStarted(); | 175 runner.WaitUntilStarted(); |
166 | 176 |
| 177 EXPECT_FALSE(thread.HasBeenJoined()); |
167 EXPECT_DCHECK_DEATH({ thread.Join(); }); | 178 EXPECT_DCHECK_DEATH({ thread.Join(); }); |
168 } | 179 } |
169 | 180 |
170 TEST(SimpleThreadTest, NonJoinableInactiveDelegateDestructionIsOkay) { | 181 TEST(SimpleThreadTest, NonJoinableInactiveDelegateDestructionIsOkay) { |
171 std::unique_ptr<ControlledRunner> runner(new ControlledRunner); | 182 std::unique_ptr<ControlledRunner> runner(new ControlledRunner); |
172 | 183 |
173 SimpleThread::Options options; | 184 SimpleThread::Options options; |
174 options.joinable = false; | 185 options.joinable = false; |
175 std::unique_ptr<DelegateSimpleThread> thread( | 186 std::unique_ptr<DelegateSimpleThread> thread( |
176 new DelegateSimpleThread(runner.get(), "non_joinable", options)); | 187 new DelegateSimpleThread(runner.get(), "non_joinable", options)); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 VerifyPoolRunner verifier(&seq2, 9, &event); | 225 VerifyPoolRunner verifier(&seq2, 9, &event); |
215 pool.Start(); | 226 pool.Start(); |
216 | 227 |
217 pool.AddWork(&verifier, 10); | 228 pool.AddWork(&verifier, 10); |
218 | 229 |
219 pool.JoinAll(); | 230 pool.JoinAll(); |
220 EXPECT_EQ(seq2.GetNext(), 10); | 231 EXPECT_EQ(seq2.GetNext(), 10); |
221 } | 232 } |
222 | 233 |
223 } // namespace base | 234 } // namespace base |
OLD | NEW |