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