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