Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_SRC_MUTEX_H_ |
| 18 | #define ART_SRC_MUTEX_H_ |
| 19 | |
| 20 | #include <pthread.h> |
Brian Carlstrom | cd74c4b | 2012-01-23 13:21:00 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 22 | |
| 23 | #include <iosfwd> |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 26 | #include "globals.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 27 | #include "locks.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 28 | #include "macros.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 29 | #include "thread.h" |
| 30 | |
| 31 | #define ART_USE_FUTEXES 0 |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 32 | |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 33 | // Currently Darwin doesn't support locks with timeouts. |
| 34 | #if !defined(__APPLE__) |
| 35 | #define HAVE_TIMED_RWLOCK 1 |
| 36 | #else |
| 37 | #define HAVE_TIMED_RWLOCK 0 |
| 38 | #endif |
| 39 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 40 | namespace art { |
| 41 | |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 42 | const bool kDebugLocking = kIsDebugBuild; |
| 43 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 44 | // Base class for all Mutex implementations |
| 45 | class BaseMutex { |
| 46 | public: |
| 47 | const std::string& GetName() const { |
| 48 | return name_; |
| 49 | } |
| 50 | |
| 51 | virtual bool IsMutex() const { return false; } |
| 52 | virtual bool IsReaderWriterMutex() const { return false; } |
| 53 | |
| 54 | protected: |
| 55 | friend class ConditionVariable; |
| 56 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 57 | BaseMutex(const char* name, LockLevel level); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 58 | virtual ~BaseMutex() {} |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 59 | void RegisterAsLocked(Thread* self); |
| 60 | void RegisterAsUnlocked(Thread* self); |
| 61 | void CheckSafeToWait(Thread* self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 62 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 63 | const LockLevel level_; // Support for lock hierarchy. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 64 | const std::string name_; |
| 65 | }; |
| 66 | |
| 67 | // A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain |
| 68 | // exclusive access to what it guards. A Mutex can be in one of two states: |
| 69 | // - Free - not owned by any thread, |
| 70 | // - Exclusive - owned by a single thread. |
| 71 | // |
| 72 | // The effect of locking and unlocking operations on the state is: |
| 73 | // State | ExclusiveLock | ExclusiveUnlock |
| 74 | // ------------------------------------------- |
| 75 | // Free | Exclusive | error |
| 76 | // Exclusive | Block* | Free |
| 77 | // * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in |
| 78 | // an error. Being non-reentrant simplifies Waiting on ConditionVariables. |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 79 | std::ostream& operator<<(std::ostream& os, const Mutex& mu); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 80 | class LOCKABLE Mutex : public BaseMutex { |
| 81 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 82 | explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 83 | ~Mutex(); |
| 84 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 85 | virtual bool IsMutex() const { return true; } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 86 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | // Block until mutex is free then acquire exclusive access. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 88 | void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); |
| 89 | void Lock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 90 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 91 | // Returns true if acquires exclusive access, false otherwise. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 92 | bool ExclusiveTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); |
| 93 | bool TryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true) { return ExclusiveTryLock(self); } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 94 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | // Release exclusive access. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 96 | void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); |
| 97 | void Unlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 98 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 99 | // Is the current thread the exclusive holder of the Mutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 100 | bool IsExclusiveHeld(const Thread* self) const; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 101 | |
| 102 | // Assert that the Mutex is exclusively held by the current thread. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 103 | void AssertExclusiveHeld(const Thread* self) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 104 | if (kDebugLocking) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 105 | CHECK(IsExclusiveHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 108 | void AssertHeld(const Thread* self) { AssertExclusiveHeld(self); } |
| 109 | void AssertHeld() { AssertExclusiveHeld(Thread::Current()); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 110 | |
| 111 | // Assert that the Mutex is not held by the current thread. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 112 | void AssertNotHeldExclusive(const Thread* self) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 113 | if (kDebugLocking) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 114 | CHECK(!IsExclusiveHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 117 | void AssertNotHeld(const Thread* self) { AssertNotHeldExclusive(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 118 | |
| 119 | // Id associated with exclusive owner. |
| 120 | uint64_t GetExclusiveOwnerTid() const; |
| 121 | |
| 122 | // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld. |
| 123 | unsigned int GetDepth() const { |
| 124 | return recursion_count_; |
| 125 | } |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 126 | |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 127 | std::string Dump() const; |
| 128 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 129 | private: |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 130 | pthread_mutex_t mutex_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | const bool recursive_; // Can the lock be recursively held? |
| 132 | unsigned int recursion_count_; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 133 | friend class ConditionVariable; |
Elliott Hughes | 3efb841 | 2012-03-16 16:09:38 -0700 | [diff] [blame] | 134 | friend class MutexTester; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 136 | }; |
| 137 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 138 | // A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex. |
| 139 | // Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader) |
| 140 | // access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a |
| 141 | // condition variable. A ReaderWriterMutex can be in one of three states: |
| 142 | // - Free - not owned by any thread, |
| 143 | // - Exclusive - owned by a single thread, |
| 144 | // - Shared(n) - shared amongst n threads. |
| 145 | // |
| 146 | // The effect of locking and unlocking operations on the state is: |
| 147 | // |
| 148 | // State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock |
| 149 | // ---------------------------------------------------------------------------- |
| 150 | // Free | Exclusive | error | SharedLock(1) | error |
| 151 | // Exclusive | Block | Free | Block | error |
| 152 | // Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free |
| 153 | // * for large values of n the SharedLock may block. |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 154 | std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | class LOCKABLE ReaderWriterMutex : public BaseMutex { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 156 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 157 | explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | ~ReaderWriterMutex(); |
| 159 | |
| 160 | virtual bool IsReaderWriterMutex() const { return true; } |
| 161 | |
| 162 | // Block until ReaderWriterMutex is free then acquire exclusive access. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 163 | void ExclusiveLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION(); |
| 164 | void WriterLock(Thread* self) EXCLUSIVE_LOCK_FUNCTION() { ExclusiveLock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | |
| 166 | // Release exclusive access. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 167 | void ExclusiveUnlock(Thread* self) UNLOCK_FUNCTION(); |
| 168 | void WriterUnlock(Thread* self) UNLOCK_FUNCTION() { ExclusiveUnlock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 169 | |
| 170 | // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success |
| 171 | // or false if timeout is reached. |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 172 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 173 | bool ExclusiveLockWithTimeout(Thread* self, const timespec& abs_timeout) |
| 174 | EXCLUSIVE_TRYLOCK_FUNCTION(true); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 175 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 176 | |
| 177 | // Block until ReaderWriterMutex is shared or free then acquire a share on the access. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 178 | void SharedLock(Thread* self) SHARED_LOCK_FUNCTION(); |
| 179 | void ReaderLock(Thread* self) SHARED_LOCK_FUNCTION() { SharedLock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 180 | |
| 181 | // Try to acquire share of ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 182 | bool SharedTryLock(Thread* self) EXCLUSIVE_TRYLOCK_FUNCTION(true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 183 | |
| 184 | // Release a share of the access. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 185 | void SharedUnlock(Thread* self) UNLOCK_FUNCTION(); |
| 186 | void ReaderUnlock(Thread* self) UNLOCK_FUNCTION() { SharedUnlock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 187 | |
| 188 | // Is the current thread the exclusive holder of the ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 189 | bool IsExclusiveHeld(const Thread* self) const; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 190 | |
| 191 | // Assert the current thread has exclusive access to the ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 192 | void AssertExclusiveHeld(const Thread* self) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 193 | if (kDebugLocking) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 194 | CHECK(IsExclusiveHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 197 | void AssertWriterHeld(const Thread* self) { AssertExclusiveHeld(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 198 | |
| 199 | // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 200 | void AssertNotExclusiveHeld(const Thread* self) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 201 | if (kDebugLocking) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 202 | CHECK(!IsExclusiveHeld(self)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 205 | void AssertNotWriterHeld(const Thread* self) { AssertNotExclusiveHeld(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 206 | |
| 207 | // Is the current thread a shared holder of the ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 208 | bool IsSharedHeld(const Thread* self) const; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 209 | |
| 210 | // Assert the current thread has shared access to the ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 211 | void AssertSharedHeld(const Thread* self) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 212 | if (kDebugLocking) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 213 | CHECK(IsSharedHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 216 | void AssertReaderHeld(const Thread* self) { AssertSharedHeld(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 217 | |
| 218 | // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive |
| 219 | // mode. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 220 | void AssertNotHeld(const Thread* self) { |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 221 | if (kDebugLocking) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 222 | CHECK(!IsSharedHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 223 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 226 | // Id associated with exclusive owner. |
| 227 | uint64_t GetExclusiveOwnerTid() const; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 228 | |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 229 | std::string Dump() const; |
| 230 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 231 | private: |
| 232 | #if ART_USE_FUTEXES |
| 233 | // -1 implies held exclusive, +ve shared held by state_ many owners. |
| 234 | volatile int32_t state_; |
| 235 | // Exclusive owner. |
| 236 | volatile uint64_t exclusive_owner_; |
| 237 | // Pending readers. |
| 238 | volatile int32_t num_pending_readers_; |
| 239 | // Pending writers. |
| 240 | volatile int32_t num_pending_writers_; |
| 241 | #else |
| 242 | pthread_rwlock_t rwlock_; |
| 243 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 244 | friend class MutexTester; |
| 245 | DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex); |
| 246 | }; |
| 247 | |
| 248 | // ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually |
| 249 | // (Signal) or all at once (Broadcast). |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 250 | class ConditionVariable { |
| 251 | public: |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 252 | explicit ConditionVariable(const std::string& name); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 253 | ~ConditionVariable(); |
| 254 | |
| 255 | void Broadcast(); |
| 256 | void Signal(); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 257 | void Wait(Thread* self, Mutex& mutex); |
| 258 | void TimedWait(Thread* self, Mutex& mutex, const timespec& ts); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 259 | |
| 260 | private: |
| 261 | pthread_cond_t cond_; |
| 262 | std::string name_; |
| 263 | DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 264 | }; |
| 265 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 266 | // Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it |
| 267 | // upon destruction. |
| 268 | class SCOPED_LOCKABLE MutexLock { |
| 269 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 270 | explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) { |
| 271 | mu_.ExclusiveLock(self_); |
| 272 | } |
| 273 | |
| 274 | explicit MutexLock(Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(Thread::Current()), mu_(mu) { |
| 275 | mu_.ExclusiveLock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | ~MutexLock() UNLOCK_FUNCTION() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 279 | mu_.ExclusiveUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | private: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 283 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 284 | Mutex& mu_; |
| 285 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 286 | }; |
| 287 | // Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". |
| 288 | #define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name) |
| 289 | |
| 290 | // Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon |
| 291 | // construction and releases it upon destruction. |
| 292 | class SCOPED_LOCKABLE ReaderMutexLock { |
| 293 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 294 | explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : |
| 295 | self_(self), mu_(mu) { |
| 296 | mu_.SharedLock(self_); |
| 297 | } |
| 298 | |
| 299 | explicit ReaderMutexLock(ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : |
| 300 | self_(Thread::Current()), mu_(mu) { |
| 301 | mu_.SharedLock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | ~ReaderMutexLock() UNLOCK_FUNCTION() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 305 | mu_.SharedUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | private: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 309 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 310 | ReaderWriterMutex& mu_; |
| 311 | DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); |
| 312 | }; |
| 313 | // Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of |
| 314 | // "ReaderMutexLock mu(lock)". |
| 315 | #define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name) |
| 316 | |
| 317 | // Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon |
| 318 | // construction and releases it upon destruction. |
| 319 | class SCOPED_LOCKABLE WriterMutexLock { |
| 320 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 321 | explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : |
| 322 | self_(self), mu_(mu) { |
| 323 | mu_.ExclusiveLock(self_); |
| 324 | } |
| 325 | |
| 326 | explicit WriterMutexLock(ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : |
| 327 | self_(Thread::Current()), mu_(mu) { |
| 328 | mu_.ExclusiveLock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | ~WriterMutexLock() UNLOCK_FUNCTION() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 332 | mu_.ExclusiveUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | private: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 336 | Thread* self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 337 | ReaderWriterMutex& mu_; |
| 338 | DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); |
| 339 | }; |
| 340 | // Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of |
| 341 | // "WriterMutexLock mu(lock)". |
| 342 | #define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name) |
| 343 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 344 | } // namespace art |
| 345 | |
| 346 | #endif // ART_SRC_MUTEX_H_ |