| 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 23 matching lines...) Expand all Loading... |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 // This is the asserter used with ThreadCollisionWarner instead of the default | 36 // This is the asserter used with ThreadCollisionWarner instead of the default |
| 37 // DCheckAsserter. The method fail_state is used to know if a collision took | 37 // DCheckAsserter. The method fail_state is used to know if a collision took |
| 38 // place. | 38 // place. |
| 39 class AssertReporter : public base::AsserterBase { | 39 class AssertReporter : public base::AsserterBase { |
| 40 public: | 40 public: |
| 41 AssertReporter() | 41 AssertReporter() |
| 42 : failed_(false) {} | 42 : failed_(false) {} |
| 43 | 43 |
| 44 virtual void warn() OVERRIDE { | 44 void warn() override { failed_ = true; } |
| 45 failed_ = true; | |
| 46 } | |
| 47 | 45 |
| 48 virtual ~AssertReporter() {} | 46 virtual ~AssertReporter() {} |
| 49 | 47 |
| 50 bool fail_state() const { return failed_; } | 48 bool fail_state() const { return failed_; } |
| 51 void reset() { failed_ = false; } | 49 void reset() { failed_ = false; } |
| 52 | 50 |
| 53 private: | 51 private: |
| 54 bool failed_; | 52 bool failed_; |
| 55 }; | 53 }; |
| 56 | 54 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 DFAKE_MUTEX(push_pop_); | 142 DFAKE_MUTEX(push_pop_); |
| 145 | 143 |
| 146 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 144 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
| 147 }; | 145 }; |
| 148 | 146 |
| 149 class QueueUser : public base::DelegateSimpleThread::Delegate { | 147 class QueueUser : public base::DelegateSimpleThread::Delegate { |
| 150 public: | 148 public: |
| 151 explicit QueueUser(NonThreadSafeQueue& queue) | 149 explicit QueueUser(NonThreadSafeQueue& queue) |
| 152 : queue_(queue) {} | 150 : queue_(queue) {} |
| 153 | 151 |
| 154 virtual void Run() OVERRIDE { | 152 void Run() override { |
| 155 queue_.push(0); | 153 queue_.push(0); |
| 156 queue_.pop(); | 154 queue_.pop(); |
| 157 } | 155 } |
| 158 | 156 |
| 159 private: | 157 private: |
| 160 NonThreadSafeQueue& queue_; | 158 NonThreadSafeQueue& queue_; |
| 161 }; | 159 }; |
| 162 | 160 |
| 163 AssertReporter* local_reporter = new AssertReporter(); | 161 AssertReporter* local_reporter = new AssertReporter(); |
| 164 | 162 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 DFAKE_MUTEX(push_pop_); | 200 DFAKE_MUTEX(push_pop_); |
| 203 | 201 |
| 204 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); | 202 DISALLOW_COPY_AND_ASSIGN(NonThreadSafeQueue); |
| 205 }; | 203 }; |
| 206 | 204 |
| 207 class QueueUser : public base::DelegateSimpleThread::Delegate { | 205 class QueueUser : public base::DelegateSimpleThread::Delegate { |
| 208 public: | 206 public: |
| 209 explicit QueueUser(NonThreadSafeQueue& queue) | 207 explicit QueueUser(NonThreadSafeQueue& queue) |
| 210 : queue_(queue) {} | 208 : queue_(queue) {} |
| 211 | 209 |
| 212 virtual void Run() OVERRIDE { | 210 void Run() override { |
| 213 queue_.push(0); | 211 queue_.push(0); |
| 214 queue_.pop(); | 212 queue_.pop(); |
| 215 } | 213 } |
| 216 | 214 |
| 217 private: | 215 private: |
| 218 NonThreadSafeQueue& queue_; | 216 NonThreadSafeQueue& queue_; |
| 219 }; | 217 }; |
| 220 | 218 |
| 221 AssertReporter* local_reporter = new AssertReporter(); | 219 AssertReporter* local_reporter = new AssertReporter(); |
| 222 | 220 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 }; | 261 }; |
| 264 | 262 |
| 265 // This time the QueueUser class protects the non thread safe queue with | 263 // This time the QueueUser class protects the non thread safe queue with |
| 266 // a lock. | 264 // a lock. |
| 267 class QueueUser : public base::DelegateSimpleThread::Delegate { | 265 class QueueUser : public base::DelegateSimpleThread::Delegate { |
| 268 public: | 266 public: |
| 269 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) | 267 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) |
| 270 : queue_(queue), | 268 : queue_(queue), |
| 271 lock_(lock) {} | 269 lock_(lock) {} |
| 272 | 270 |
| 273 virtual void Run() OVERRIDE { | 271 void Run() override { |
| 274 { | 272 { |
| 275 base::AutoLock auto_lock(lock_); | 273 base::AutoLock auto_lock(lock_); |
| 276 queue_.push(0); | 274 queue_.push(0); |
| 277 } | 275 } |
| 278 { | 276 { |
| 279 base::AutoLock auto_lock(lock_); | 277 base::AutoLock auto_lock(lock_); |
| 280 queue_.pop(); | 278 queue_.pop(); |
| 281 } | 279 } |
| 282 } | 280 } |
| 283 private: | 281 private: |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 }; | 335 }; |
| 338 | 336 |
| 339 // This time the QueueUser class protects the non thread safe queue with | 337 // This time the QueueUser class protects the non thread safe queue with |
| 340 // a lock. | 338 // a lock. |
| 341 class QueueUser : public base::DelegateSimpleThread::Delegate { | 339 class QueueUser : public base::DelegateSimpleThread::Delegate { |
| 342 public: | 340 public: |
| 343 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) | 341 QueueUser(NonThreadSafeQueue& queue, base::Lock& lock) |
| 344 : queue_(queue), | 342 : queue_(queue), |
| 345 lock_(lock) {} | 343 lock_(lock) {} |
| 346 | 344 |
| 347 virtual void Run() OVERRIDE { | 345 void Run() override { |
| 348 { | 346 { |
| 349 base::AutoLock auto_lock(lock_); | 347 base::AutoLock auto_lock(lock_); |
| 350 queue_.push(0); | 348 queue_.push(0); |
| 351 } | 349 } |
| 352 { | 350 { |
| 353 base::AutoLock auto_lock(lock_); | 351 base::AutoLock auto_lock(lock_); |
| 354 queue_.bar(); | 352 queue_.bar(); |
| 355 } | 353 } |
| 356 { | 354 { |
| 357 base::AutoLock auto_lock(lock_); | 355 base::AutoLock auto_lock(lock_); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 376 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); | 374 base::DelegateSimpleThread thread_b(&queue_user_b, "queue_user_thread_b"); |
| 377 | 375 |
| 378 thread_a.Start(); | 376 thread_a.Start(); |
| 379 thread_b.Start(); | 377 thread_b.Start(); |
| 380 | 378 |
| 381 thread_a.Join(); | 379 thread_a.Join(); |
| 382 thread_b.Join(); | 380 thread_b.Join(); |
| 383 | 381 |
| 384 EXPECT_FALSE(local_reporter->fail_state()); | 382 EXPECT_FALSE(local_reporter->fail_state()); |
| 385 } | 383 } |
| OLD | NEW |