| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/lock.h" | |
| 7 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.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" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 // '' : local class member function does not have a body | 13 // '' : local class member function does not have a body |
| 14 MSVC_PUSH_DISABLE_WARNING(4822) | 14 MSVC_PUSH_DISABLE_WARNING(4822) |
| 15 | 15 |
| 16 | 16 |
| 17 #if defined(NDEBUG) | 17 #if defined(NDEBUG) |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 private: | 259 private: |
| 260 DFAKE_MUTEX(push_pop_); | 260 DFAKE_MUTEX(push_pop_); |
| 261 | 261 |
| 262 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 262 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
| 263 }; | 263 }; |
| 264 | 264 |
| 265 // This time the QueueUser class protects the non thread safe queue with | 265 // This time the QueueUser class protects the non thread safe queue with |
| 266 // a lock. | 266 // a lock. |
| 267 class QueueUser : public base::DelegateSimpleThread::Delegate { | 267 class QueueUser : public base::DelegateSimpleThread::Delegate { |
| 268 public: | 268 public: |
| 269 QueueUser(NonThreadSafeQueue& queue, Lock& lock) | 269 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) |
| 270 : queue_(queue), | 270 : queue_(queue), |
| 271 lock_(lock) {} | 271 lock_(lock) {} |
| 272 | 272 |
| 273 virtual void Run() { | 273 virtual void Run() { |
| 274 { | 274 { |
| 275 AutoLock auto_lock(lock_); | 275 base::AutoLock auto_lock(lock_); |
| 276 queue_.push(0); | 276 queue_.push(0); |
| 277 } | 277 } |
| 278 { | 278 { |
| 279 AutoLock auto_lock(lock_); | 279 base::AutoLock auto_lock(lock_); |
| 280 queue_.pop(); | 280 queue_.pop(); |
| 281 } | 281 } |
| 282 } | 282 } |
| 283 private: | 283 private: |
| 284 NonThreadSafeQueue& queue_; | 284 NonThreadSafeQueue& queue_; |
| 285 Lock& lock_; | 285 base::Lock& lock_; |
| 286 }; | 286 }; |
| 287 | 287 |
| 288 AssertReporter* local_reporter = new AssertReporter(); | 288 AssertReporter* local_reporter = new AssertReporter(); |
| 289 | 289 |
| 290 NonThreadSafeQueue queue(local_reporter); | 290 NonThreadSafeQueue queue(local_reporter); |
| 291 | 291 |
| 292 Lock lock; | 292 base::Lock lock; |
| 293 | 293 |
| 294 QueueUser queue_user_a(queue, lock); | 294 QueueUser queue_user_a(queue, lock); |
| 295 QueueUser queue_user_b(queue, lock); | 295 QueueUser queue_user_b(queue, lock); |
| 296 | 296 |
| 297 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); | 297 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); |
| 298 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); | 298 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); |
| 299 | 299 |
| 300 thread_a.Start(); | 300 thread_a.Start(); |
| 301 thread_b.Start(); | 301 thread_b.Start(); |
| 302 | 302 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 333 private: | 333 private: |
| 334 DFAKE_MUTEX(push_pop_); | 334 DFAKE_MUTEX(push_pop_); |
| 335 | 335 |
| 336 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 336 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
| 337 }; | 337 }; |
| 338 | 338 |
| 339 // This time the QueueUser class protects the non thread safe queue with | 339 // This time the QueueUser class protects the non thread safe queue with |
| 340 // a lock. | 340 // a lock. |
| 341 class QueueUser : public base::DelegateSimpleThread::Delegate { | 341 class QueueUser : public base::DelegateSimpleThread::Delegate { |
| 342 public: | 342 public: |
| 343 QueueUser(NonThreadSafeQueue& queue, Lock& lock) | 343 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) |
| 344 : queue_(queue), | 344 : queue_(queue), |
| 345 lock_(lock) {} | 345 lock_(lock) {} |
| 346 | 346 |
| 347 virtual void Run() { | 347 virtual void Run() { |
| 348 { | 348 { |
| 349 AutoLock auto_lock(lock_); | 349 base::AutoLock auto_lock(lock_); |
| 350 queue_.push(0); | 350 queue_.push(0); |
| 351 } | 351 } |
| 352 { | 352 { |
| 353 AutoLock auto_lock(lock_); | 353 base::AutoLock auto_lock(lock_); |
| 354 queue_.bar(); | 354 queue_.bar(); |
| 355 } | 355 } |
| 356 { | 356 { |
| 357 AutoLock auto_lock(lock_); | 357 base::AutoLock auto_lock(lock_); |
| 358 queue_.pop(); | 358 queue_.pop(); |
| 359 } | 359 } |
| 360 } | 360 } |
| 361 private: | 361 private: |
| 362 NonThreadSafeQueue& queue_; | 362 NonThreadSafeQueue& queue_; |
| 363 Lock& lock_; | 363 base::Lock& lock_; |
| 364 }; | 364 }; |
| 365 | 365 |
| 366 AssertReporter* local_reporter = new AssertReporter(); | 366 AssertReporter* local_reporter = new AssertReporter(); |
| 367 | 367 |
| 368 NonThreadSafeQueue queue(local_reporter); | 368 NonThreadSafeQueue queue(local_reporter); |
| 369 | 369 |
| 370 Lock lock; | 370 base::Lock lock; |
| 371 | 371 |
| 372 QueueUser queue_user_a(queue, lock); | 372 QueueUser queue_user_a(queue, lock); |
| 373 QueueUser queue_user_b(queue, lock); | 373 QueueUser queue_user_b(queue, lock); |
| 374 | 374 |
| 375 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); | 375 base::DelegateSimpleThread thread_a(&queue_user_a, "queue_user_thread_a"); |
| 376 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); | 376 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); |
| 377 | 377 |
| 378 thread_a.Start(); | 378 thread_a.Start(); |
| 379 thread_b.Start(); | 379 thread_b.Start(); |
| 380 | 380 |
| 381 thread_a.Join(); | 381 thread_a.Join(); |
| 382 thread_b.Join(); | 382 thread_b.Join(); |
| 383 | 383 |
| 384 EXPECT_FALSE(local_reporter->fail_state()); | 384 EXPECT_FALSE(local_reporter->fail_state()); |
| 385 } | 385 } |
| OLD | NEW |