OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_LOCK_H |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_LOCK_H |
| 7 |
| 8 #include "base/base_export.h" |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/synchronization/condition_variable.h" |
| 12 #include "base/synchronization/lock.h" |
| 13 #include "base/task_scheduler/scheduler_lock_impl.h" |
| 14 |
| 15 namespace base { |
| 16 namespace internal { |
| 17 |
| 18 // SchedulerLock should be used anywhere a lock would be used in the scheduler. |
| 19 // When DCHECK_IS_ON(), lock checking occurs. Otherwise, SchedulerLock is |
| 20 // equivalent to base::Lock. |
| 21 // |
| 22 // The shape of SchedulerLock is as follows: |
| 23 // SchedulerLock() |
| 24 // Default constructor, no predecessor lock. |
| 25 // DCHECKs |
| 26 // On Acquisition if any scheduler lock is acquired on this thread. |
| 27 // |
| 28 // SchedulerLock(const SchedulerLock* predecessor) |
| 29 // Constructor that specifies an allowed predecessor for that lock. |
| 30 // DCHECKs |
| 31 // On Construction if |predecessor| forms a predecessor lock cycle. |
| 32 // On Acquisition if the previous lock acquired on the thread is not |
| 33 // |predecessor|. Okay if there was no previous lock acquired. |
| 34 // |
| 35 // void Acquire() |
| 36 // Acquires the lock. |
| 37 // |
| 38 // void Release() |
| 39 // Releases the lock. |
| 40 // |
| 41 // void AssertAcquired(). |
| 42 // DCHECKs if the lock is not acquired. |
| 43 // |
| 44 // scoped_ptr<ConditionVariable> CreateConditionVariable() |
| 45 // Creates a condition variable using this as a lock. |
| 46 |
| 47 #if DCHECK_IS_ON() |
| 48 class SchedulerLock : public SchedulerLockImpl { |
| 49 public: |
| 50 SchedulerLock() = default; |
| 51 explicit SchedulerLock(const SchedulerLock* predecessor) |
| 52 : SchedulerLockImpl(predecessor) {} |
| 53 }; |
| 54 #else // DCHECK_IS_ON() |
| 55 class SchedulerLock : public Lock { |
| 56 public: |
| 57 SchedulerLock() = default; |
| 58 explicit SchedulerLock(const SchedulerLock*) {} |
| 59 |
| 60 scoped_ptr<ConditionVariable> CreateConditionVariable() { |
| 61 return scoped_ptr<ConditionVariable>(new ConditionVariable(this)); |
| 62 } |
| 63 }; |
| 64 #endif // DCHECK_IS_ON() |
| 65 |
| 66 // Provides the same functionality as base::AutoLock for SchedulerLock. |
| 67 class AutoSchedulerLock { |
| 68 public: |
| 69 explicit AutoSchedulerLock(SchedulerLock& lock) : lock_(lock) { |
| 70 lock_.Acquire(); |
| 71 } |
| 72 |
| 73 ~AutoSchedulerLock() { |
| 74 lock_.AssertAcquired(); |
| 75 lock_.Release(); |
| 76 } |
| 77 |
| 78 private: |
| 79 SchedulerLock& lock_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(AutoSchedulerLock); |
| 82 }; |
| 83 |
| 84 } // namespace internal |
| 85 } // namespace base |
| 86 |
| 87 #endif // BASE_TASK_SCHEDULER_SCHEDULER_LOCK_H |
OLD | NEW |