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