OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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_SYNCHRONIZATION_LOCK_H_ |
| 6 #define BASE_SYNCHRONIZATION_LOCK_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/synchronization/lock_impl.h" |
| 10 #include "base/threading/platform_thread.h" |
| 11 |
| 12 namespace base { |
| 13 |
| 14 // A convenient wrapper for an OS specific critical section. The only real |
| 15 // intelligence in this class is in debug mode for the support for the |
| 16 // AssertAcquired() method. |
| 17 class Lock { |
| 18 public: |
| 19 #if defined(NDEBUG) // Optimized wrapper implementation |
| 20 Lock() : lock_() {} |
| 21 ~Lock() {} |
| 22 void Acquire() { lock_.Lock(); } |
| 23 void Release() { lock_.Unlock(); } |
| 24 |
| 25 // If the lock is not held, take it and return true. If the lock is already |
| 26 // held by another thread, immediately return false. This must not be called |
| 27 // by a thread already holding the lock (what happens is undefined and an |
| 28 // assertion may fail). |
| 29 bool Try() { return lock_.Try(); } |
| 30 |
| 31 // Null implementation if not debug. |
| 32 void AssertAcquired() const {} |
| 33 #else |
| 34 Lock(); |
| 35 ~Lock() {} |
| 36 |
| 37 // NOTE: Although windows critical sections support recursive locks, we do not |
| 38 // allow this, and we will commonly fire a DCHECK() if a thread attempts to |
| 39 // acquire the lock a second time (while already holding it). |
| 40 void Acquire() { |
| 41 lock_.Lock(); |
| 42 CheckUnheldAndMark(); |
| 43 } |
| 44 void Release() { |
| 45 CheckHeldAndUnmark(); |
| 46 lock_.Unlock(); |
| 47 } |
| 48 |
| 49 bool Try() { |
| 50 bool rv = lock_.Try(); |
| 51 if (rv) { |
| 52 CheckUnheldAndMark(); |
| 53 } |
| 54 return rv; |
| 55 } |
| 56 |
| 57 void AssertAcquired() const; |
| 58 #endif // NDEBUG |
| 59 |
| 60 #if defined(OS_POSIX) |
| 61 // The posix implementation of ConditionVariable needs to be able |
| 62 // to see our lock and tweak our debugging counters, as it releases |
| 63 // and acquires locks inside of pthread_cond_{timed,}wait. |
| 64 // Windows doesn't need to do this as it calls the Lock::* methods. |
| 65 friend class ConditionVariable; |
| 66 #endif |
| 67 |
| 68 private: |
| 69 #if !defined(NDEBUG) |
| 70 // Members and routines taking care of locks assertions. |
| 71 // Note that this checks for recursive locks and allows them |
| 72 // if the variable is set. This is allowed by the underlying implementation |
| 73 // on windows but not on Posix, so we're doing unneeded checks on Posix. |
| 74 // It's worth it to share the code. |
| 75 void CheckHeldAndUnmark(); |
| 76 void CheckUnheldAndMark(); |
| 77 |
| 78 // All private data is implicitly protected by lock_. |
| 79 // Be VERY careful to only access members under that lock. |
| 80 |
| 81 // Determines validity of owning_thread_id_. Needed as we don't have |
| 82 // a null owning_thread_id_ value. |
| 83 bool owned_by_thread_; |
| 84 base::PlatformThreadId owning_thread_id_; |
| 85 #endif // NDEBUG |
| 86 |
| 87 // Platform specific underlying lock implementation. |
| 88 internal::LockImpl lock_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(Lock); |
| 91 }; |
| 92 |
| 93 // A helper class that acquires the given Lock while the AutoLock is in scope. |
| 94 class AutoLock { |
| 95 public: |
| 96 explicit AutoLock(Lock& lock) : lock_(lock) { |
| 97 lock_.Acquire(); |
| 98 } |
| 99 |
| 100 ~AutoLock() { |
| 101 lock_.AssertAcquired(); |
| 102 lock_.Release(); |
| 103 } |
| 104 |
| 105 private: |
| 106 Lock& lock_; |
| 107 DISALLOW_COPY_AND_ASSIGN(AutoLock); |
| 108 }; |
| 109 |
| 110 // AutoUnlock is a helper that will Release() the |lock| argument in the |
| 111 // constructor, and re-Acquire() it in the destructor. |
| 112 class AutoUnlock { |
| 113 public: |
| 114 explicit AutoUnlock(Lock& lock) : lock_(lock) { |
| 115 // We require our caller to have the lock. |
| 116 lock_.AssertAcquired(); |
| 117 lock_.Release(); |
| 118 } |
| 119 |
| 120 ~AutoUnlock() { |
| 121 lock_.Acquire(); |
| 122 } |
| 123 |
| 124 private: |
| 125 Lock& lock_; |
| 126 DISALLOW_COPY_AND_ASSIGN(AutoUnlock); |
| 127 }; |
| 128 |
| 129 } // namespace base |
| 130 |
| 131 #endif // BASE_SYNCHRONIZATION_LOCK_H_ |
OLD | NEW |