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 "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/synchronization/lock.h" | 7 #include "base/synchronization/lock.h" |
8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
9 #include "base/threading/simple_thread.h" | 9 #include "base/threading/simple_thread.h" |
10 #include "base/threading/thread_collision_warner.h" | 10 #include "base/threading/thread_collision_warner.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } | 139 } |
140 | 140 |
141 private: | 141 private: |
142 DFAKE_MUTEX(push_pop_); | 142 DFAKE_MUTEX(push_pop_); |
143 | 143 |
144 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 144 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
145 }; | 145 }; |
146 | 146 |
147 class QueueUser : public base::DelegateSimpleThread::Delegate { | 147 class QueueUser : public base::DelegateSimpleThread::Delegate { |
148 public: | 148 public: |
149 explicit QueueUser(NonThreadSafeQueue& queue) | 149 explicit QueueUser(NonThreadSafeQueue* queue) : queue_(queue) {} |
150 : queue_(queue) {} | |
151 | 150 |
152 void Run() override { | 151 void Run() override { |
153 queue_.push(0); | 152 queue_->push(0); |
154 queue_.pop(); | 153 queue_->pop(); |
155 } | 154 } |
156 | 155 |
157 private: | 156 private: |
158 NonThreadSafeQueue& queue_; | 157 NonThreadSafeQueue* queue_; |
159 }; | 158 }; |
160 | 159 |
161 AssertReporter* local_reporter = new AssertReporter(); | 160 AssertReporter* local_reporter = new AssertReporter(); |
162 | 161 |
163 NonThreadSafeQueue queue(local_reporter); | 162 NonThreadSafeQueue queue(local_reporter); |
164 | 163 |
165 QueueUser queue_user_a(queue); | 164 QueueUser queue_user_a(&queue); |
166 QueueUser queue_user_b(queue); | 165 QueueUser queue_user_b(&queue); |
167 | 166 |
168 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); | 167 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); |
169 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); | 168 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); |
170 | 169 |
171 thread_a.Start(); | 170 thread_a.Start(); |
172 thread_b.Start(); | 171 thread_b.Start(); |
173 | 172 |
174 thread_a.Join(); | 173 thread_a.Join(); |
175 thread_b.Join(); | 174 thread_b.Join(); |
176 | 175 |
(...skipping 20 matching lines...) Expand all Loading... |
197 } | 196 } |
198 | 197 |
199 private: | 198 private: |
200 DFAKE_MUTEX(push_pop_); | 199 DFAKE_MUTEX(push_pop_); |
201 | 200 |
202 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 201 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
203 }; | 202 }; |
204 | 203 |
205 class QueueUser : public base::DelegateSimpleThread::Delegate { | 204 class QueueUser : public base::DelegateSimpleThread::Delegate { |
206 public: | 205 public: |
207 explicit QueueUser(NonThreadSafeQueue& queue) | 206 explicit QueueUser(NonThreadSafeQueue* queue) : queue_(queue) {} |
208 : queue_(queue) {} | |
209 | 207 |
210 void Run() override { | 208 void Run() override { |
211 queue_.push(0); | 209 queue_->push(0); |
212 queue_.pop(); | 210 queue_->pop(); |
213 } | 211 } |
214 | 212 |
215 private: | 213 private: |
216 NonThreadSafeQueue& queue_; | 214 NonThreadSafeQueue* queue_; |
217 }; | 215 }; |
218 | 216 |
219 AssertReporter* local_reporter = new AssertReporter(); | 217 AssertReporter* local_reporter = new AssertReporter(); |
220 | 218 |
221 NonThreadSafeQueue queue(local_reporter); | 219 NonThreadSafeQueue queue(local_reporter); |
222 | 220 |
223 QueueUser queue_user_a(queue); | 221 QueueUser queue_user_a(&queue); |
224 QueueUser queue_user_b(queue); | 222 QueueUser queue_user_b(&queue); |
225 | 223 |
226 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); | 224 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); |
227 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); | 225 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); |
228 | 226 |
229 thread_a.Start(); | 227 thread_a.Start(); |
230 thread_b.Start(); | 228 thread_b.Start(); |
231 | 229 |
232 thread_a.Join(); | 230 thread_a.Join(); |
233 thread_b.Join(); | 231 thread_b.Join(); |
234 | 232 |
(...skipping 22 matching lines...) Expand all Loading... |
257 private: | 255 private: |
258 DFAKE_MUTEX(push_pop_); | 256 DFAKE_MUTEX(push_pop_); |
259 | 257 |
260 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 258 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
261 }; | 259 }; |
262 | 260 |
263 // This time the QueueUser class protects the non thread safe queue with | 261 // This time the QueueUser class protects the non thread safe queue with |
264 // a lock. | 262 // a lock. |
265 class QueueUser : public base::DelegateSimpleThread::Delegate { | 263 class QueueUser : public base::DelegateSimpleThread::Delegate { |
266 public: | 264 public: |
267 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) | 265 QueueUser(NonThreadSafeQueue* queue, base::Lock* lock) |
268 : queue_(queue), | 266 : queue_(queue), lock_(lock) {} |
269 lock_(lock) {} | |
270 | 267 |
271 void Run() override { | 268 void Run() override { |
272 { | 269 { |
273 base::AutoLock auto_lock(lock_); | 270 base::AutoLock auto_lock(*lock_); |
274 queue_.push(0); | 271 queue_->push(0); |
275 } | 272 } |
276 { | 273 { |
277 base::AutoLock auto_lock(lock_); | 274 base::AutoLock auto_lock(*lock_); |
278 queue_.pop(); | 275 queue_->pop(); |
279 } | 276 } |
280 } | 277 } |
281 private: | 278 private: |
282 NonThreadSafeQueue& queue_; | 279 NonThreadSafeQueue* queue_; |
283 base::Lock& lock_; | 280 base::Lock* lock_; |
284 }; | 281 }; |
285 | 282 |
286 AssertReporter* local_reporter = new AssertReporter(); | 283 AssertReporter* local_reporter = new AssertReporter(); |
287 | 284 |
288 NonThreadSafeQueue queue(local_reporter); | 285 NonThreadSafeQueue queue(local_reporter); |
289 | 286 |
290 base::Lock lock; | 287 base::Lock lock; |
291 | 288 |
292 QueueUser queue_user_a(queue, lock); | 289 QueueUser queue_user_a(&queue, &lock); |
293 QueueUser queue_user_b(queue, lock); | 290 QueueUser queue_user_b(&queue, &lock); |
294 | 291 |
295 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); | 292 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); |
296 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); | 293 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); |
297 | 294 |
298 thread_a.Start(); | 295 thread_a.Start(); |
299 thread_b.Start(); | 296 thread_b.Start(); |
300 | 297 |
301 thread_a.Join(); | 298 thread_a.Join(); |
302 thread_b.Join(); | 299 thread_b.Join(); |
303 | 300 |
(...skipping 27 matching lines...) Expand all Loading... |
331 private: | 328 private: |
332 DFAKE_MUTEX(push_pop_); | 329 DFAKE_MUTEX(push_pop_); |
333 | 330 |
334 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 331 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
335 }; | 332 }; |
336 | 333 |
337 // This time the QueueUser class protects the non thread safe queue with | 334 // This time the QueueUser class protects the non thread safe queue with |
338 // a lock. | 335 // a lock. |
339 class QueueUser : public base::DelegateSimpleThread::Delegate { | 336 class QueueUser : public base::DelegateSimpleThread::Delegate { |
340 public: | 337 public: |
341 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) | 338 QueueUser(NonThreadSafeQueue* queue, base::Lock* lock) |
342 : queue_(queue), | 339 : queue_(queue), lock_(lock) {} |
343 lock_(lock) {} | |
344 | 340 |
345 void Run() override { | 341 void Run() override { |
346 { | 342 { |
347 base::AutoLock auto_lock(lock_); | 343 base::AutoLock auto_lock(*lock_); |
348 queue_.push(0); | 344 queue_->push(0); |
349 } | 345 } |
350 { | 346 { |
351 base::AutoLock auto_lock(lock_); | 347 base::AutoLock auto_lock(*lock_); |
352 queue_.bar(); | 348 queue_->bar(); |
353 } | 349 } |
354 { | 350 { |
355 base::AutoLock auto_lock(lock_); | 351 base::AutoLock auto_lock(*lock_); |
356 queue_.pop(); | 352 queue_->pop(); |
357 } | 353 } |
358 } | 354 } |
359 private: | 355 private: |
360 NonThreadSafeQueue& queue_; | 356 NonThreadSafeQueue* queue_; |
361 base::Lock& lock_; | 357 base::Lock* lock_; |
362 }; | 358 }; |
363 | 359 |
364 AssertReporter* local_reporter = new AssertReporter(); | 360 AssertReporter* local_reporter = new AssertReporter(); |
365 | 361 |
366 NonThreadSafeQueue queue(local_reporter); | 362 NonThreadSafeQueue queue(local_reporter); |
367 | 363 |
368 base::Lock lock; | 364 base::Lock lock; |
369 | 365 |
370 QueueUser queue_user_a(queue, lock); | 366 QueueUser queue_user_a(&queue, &lock); |
371 QueueUser queue_user_b(queue, lock); | 367 QueueUser queue_user_b(&queue, &lock); |
372 | 368 |
373 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); | 369 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); |
374 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); | 370 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); |
375 | 371 |
376 thread_a.Start(); | 372 thread_a.Start(); |
377 thread_b.Start(); | 373 thread_b.Start(); |
378 | 374 |
379 thread_a.Join(); | 375 thread_a.Join(); |
380 thread_b.Join(); | 376 thread_b.Join(); |
381 | 377 |
382 EXPECT_FALSE(local_reporter->fail_state()); | 378 EXPECT_FALSE(local_reporter->fail_state()); |
383 } | 379 } |
OLD | NEW |