| 6 | #define BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |
| 7 |
| 8 | #include "base/base_export.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "base/macros.h" |
| 11 | #include "base/thread_annotations.h" |
| 12 | #include "build/build_config.h" |
| 13 |
| 14 | #if defined(OS_WIN) |
| 15 | #include "base/win/windows_types.h" |
| 16 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 17 | #include <errno.h> |
| 18 | #include <pthread.h> |
| 19 | #endif |
| 20 |
| 21 | namespace base { |
| 22 | namespace internal { |
| 23 |
| 24 | // This class implements the underlying platform-specific spin-lock mechanism |
| 25 | // used for the Lock class. Most users should not use LockImpl directly, but |
| 26 | // should instead use Lock. |
| 27 | class BASE_EXPORT LockImpl { |
| 28 | public: |
| 29 | #if defined(OS_WIN) |
| 30 | using NativeHandle = CHROME_SRWLOCK; |
| 31 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 32 | using NativeHandle = pthread_mutex_t; |
| 33 | #endif |
| 34 |
| 35 | LockImpl(); |
| 36 | ~LockImpl(); |
| 37 |
| 38 | // If the lock is not held, take it and return true. If the lock is already |
| 39 | // held by something else, immediately return false. |
| 40 | bool Try(); |
| 41 |
| 42 | // Take the lock, blocking until it is available if necessary. |
| 43 | void Lock(); |
| 44 |
| 45 | // Release the lock. This must only be called by the lock's holder: after |
| 46 | // a successful call to Try, or a call to Lock. |
| 47 | inline void Unlock(); |
| 48 |
| 49 | // Return the native underlying lock. |
| 50 | // TODO(awalker): refactor lock and condition variables so that this is |
| 51 | // unnecessary. |
| 52 | NativeHandle* native_handle() { return &native_handle_; } |
| 53 |
| 54 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 55 | // Whether this lock will attempt to use priority inheritance. |
| 56 | static bool PriorityInheritanceAvailable(); |
| 57 | #endif |
| 58 |
| 59 | private: |
| 60 | NativeHandle native_handle_; |
| 61 |
| 62 | DISALLOW_COPY_AND_ASSIGN(LockImpl); |
| 63 | }; |
| 64 |
| 65 | #if defined(OS_WIN) |
| 66 | void LockImpl::Unlock() { |
| 67 | ::ReleaseSRWLockExclusive(reinterpret_cast<PSRWLOCK>(&native_handle_)); |
| 68 | } |
| 69 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 70 | void LockImpl::Unlock() { |
| 71 | int rv = pthread_mutex_unlock(&native_handle_); |
| 72 | DCHECK_EQ(rv, 0) << ". " << strerror(rv); |
| 73 | } |
| 74 | #endif |
| 75 |
| 76 | // This is an implementation used for AutoLock templated on the lock type. |
| 77 | template <class LockType> |
| 78 | class SCOPED_LOCKABLE BasicAutoLock { |
| 79 | public: |
| 80 | struct AlreadyAcquired {}; |
| 81 |
| 82 | explicit BasicAutoLock(LockType& lock) EXCLUSIVE_LOCK_FUNCTION(lock) |
| 83 | : lock_(lock) { |
| 84 | lock_.Acquire(); |
| 85 | } |
| 86 |
| 87 | BasicAutoLock(LockType& lock, const AlreadyAcquired&) |
| 88 | EXCLUSIVE_LOCKS_REQUIRED(lock) |
| 89 | : lock_(lock) { |
| 90 | lock_.AssertAcquired(); |
| 91 | } |
| 92 |
| 93 | ~BasicAutoLock() UNLOCK_FUNCTION() { |
| 94 | lock_.AssertAcquired(); |
| 95 | lock_.Release(); |
| 96 | } |
| 97 |
| 98 | private: |
| 99 | LockType& lock_; |
| 100 | DISALLOW_COPY_AND_ASSIGN(BasicAutoLock); |
| 101 | }; |
| 102 |
| 103 | // This is an implementation used for AutoUnlock templated on the lock type. |
| 104 | template <class LockType> |
| 105 | class BasicAutoUnlock { |
| 106 | public: |
| 107 | explicit BasicAutoUnlock(LockType& lock) : lock_(lock) { |
| 108 | // We require our caller to have the lock. |
| 109 | lock_.AssertAcquired(); |
| 110 | lock_.Release(); |
| 111 | } |
| 112 |
| 113 | ~BasicAutoUnlock() { lock_.Acquire(); } |
| 114 |
| 115 | private: |
| 116 | LockType& lock_; |
| 117 | DISALLOW_COPY_AND_ASSIGN(BasicAutoUnlock); |
| 118 | }; |
| 119 |
| 120 | // This is an implementation used for AutoLockMaybe templated on the lock type. |
| 121 | template <class LockType> |
| 122 | class SCOPED_LOCKABLE BasicAutoLockMaybe { |
| 123 | public: |
| 124 | explicit BasicAutoLockMaybe(LockType* lock) EXCLUSIVE_LOCK_FUNCTION(lock) |
| 125 | : lock_(lock) { |
| 126 | if (lock_) |
| 127 | lock_->Acquire(); |
| 128 | } |
| 129 |
| 130 | ~BasicAutoLockMaybe() UNLOCK_FUNCTION() { |
| 131 | if (lock_) { |
| 132 | lock_->AssertAcquired(); |
| 133 | lock_->Release(); |
| 134 | } |
| 135 | } |
| 136 |
| 137 | private: |
| 138 | LockType* const lock_; |
| 139 | DISALLOW_COPY_AND_ASSIGN(BasicAutoLockMaybe); |
| 140 | }; |
| 141 |
| 142 | // This is an implementation used for ReleasableAutoLock templated on the lock |
| 143 | // type. |
| 144 | template <class LockType> |
| 145 | class SCOPED_LOCKABLE BasicReleasableAutoLock { |
| 146 | public: |
| 147 | explicit BasicReleasableAutoLock(LockType* lock) EXCLUSIVE_LOCK_FUNCTION(lock) |
| 148 | : lock_(lock) { |
| 149 | DCHECK(lock_); |
| 150 | lock_->Acquire(); |
| 151 | } |
| 152 |
| 153 | ~BasicReleasableAutoLock() UNLOCK_FUNCTION() { |
| 154 | if (lock_) { |
| 155 | lock_->AssertAcquired(); |
| 156 | lock_->Release(); |
| 157 | } |
| 158 | } |
| 159 |
| 160 | void Release() UNLOCK_FUNCTION() { |
| 161 | DCHECK(lock_); |
| 162 | lock_->AssertAcquired(); |
| 163 | lock_->Release(); |
| 164 | lock_ = nullptr; |
| 165 | } |
| 166 |
| 167 | private: |
| 168 | LockType* lock_; |
| 169 | DISALLOW_COPY_AND_ASSIGN(BasicReleasableAutoLock); |
| 170 | }; |
| 171 |
| 172 | } // namespace internal |
| 173 | } // namespace base |
| 174 |
| 175 | #endif // BASE_SYNCHRONIZATION_LOCK_IMPL_H_ |
| 176 |