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