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 | ccf1248 | 2013-06-10 23:19:30 -0700 | [diff] [blame] | 56 | const bool kDebugLocking = true || kIsDebugBuild; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 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) { |
Anwar Ghuloum | 3c539ff | 2013-06-20 08:58:23 -0700 | [diff] [blame] | 144 | if (kDebugLocking && (gAborting == 0)) { |
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) { |
Anwar Ghuloum | 3c539ff | 2013-06-20 08:58:23 -0700 | [diff] [blame] | 152 | if (kDebugLocking && (gAborting == 0)) { |
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) { |
Anwar Ghuloum | 3c539ff | 2013-06-20 08:58:23 -0700 | [diff] [blame] | 241 | if (kDebugLocking & (gAborting == 0)) { |
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) { |
Anwar Ghuloum | 3c539ff | 2013-06-20 08:58:23 -0700 | [diff] [blame] | 249 | if (kDebugLocking & (gAborting == 0)) { |
Ian Rogers | e3359f7 | 2013-06-11 15:14:11 -0700 | [diff] [blame] | 250 | CHECK(!IsExclusiveHeld(self)) << *this; |
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) { |
Anwar Ghuloum | 3c539ff | 2013-06-20 08:58:23 -0700 | [diff] [blame] | 260 | if (kDebugLocking & (gAborting == 0)) { |
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) { |
Anwar Ghuloum | 3c539ff | 2013-06-20 08:58:23 -0700 | [diff] [blame] | 270 | if (kDebugLocking && (gAborting == 0)) { |
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; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame^] | 310 | // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held |
| 311 | // when waiting. |
| 312 | // TODO: remove this. |
| 313 | void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 314 | |
| 315 | private: |
Ian Rogers | 23055dc | 2013-04-18 16:29:16 -0700 | [diff] [blame] | 316 | const char* const name_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 317 | // The Mutex being used by waiters. It is an error to mix condition variables between different |
| 318 | // Mutexes. |
| 319 | Mutex& guard_; |
| 320 | #if ART_USE_FUTEXES |
| 321 | // 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] | 322 | // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_ |
| 323 | // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait |
| 324 | // without guard_ held. |
| 325 | volatile int32_t sequence_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 326 | // 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] | 327 | // waiters may have been requeued onto guard_. Guarded by guard_. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 328 | volatile int32_t num_waiters_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 329 | #else |
| 330 | pthread_cond_t cond_; |
| 331 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 332 | DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 333 | }; |
| 334 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 335 | // Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it |
| 336 | // upon destruction. |
| 337 | class SCOPED_LOCKABLE MutexLock { |
| 338 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 339 | explicit MutexLock(Thread* self, Mutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : self_(self), mu_(mu) { |
| 340 | mu_.ExclusiveLock(self_); |
| 341 | } |
| 342 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 343 | ~MutexLock() UNLOCK_FUNCTION() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 344 | mu_.ExclusiveUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | private: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 348 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 349 | Mutex& mu_; |
| 350 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 351 | }; |
| 352 | // Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". |
| 353 | #define MutexLock(x) COMPILE_ASSERT(0, mutex_lock_declaration_missing_variable_name) |
| 354 | |
| 355 | // Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon |
| 356 | // construction and releases it upon destruction. |
| 357 | class SCOPED_LOCKABLE ReaderMutexLock { |
| 358 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 359 | explicit ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : |
| 360 | self_(self), mu_(mu) { |
| 361 | mu_.SharedLock(self_); |
| 362 | } |
| 363 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 364 | ~ReaderMutexLock() UNLOCK_FUNCTION() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 365 | mu_.SharedUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | private: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 369 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 370 | ReaderWriterMutex& mu_; |
| 371 | DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); |
| 372 | }; |
| 373 | // Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of |
| 374 | // "ReaderMutexLock mu(lock)". |
| 375 | #define ReaderMutexLock(x) COMPILE_ASSERT(0, reader_mutex_lock_declaration_missing_variable_name) |
| 376 | |
| 377 | // Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon |
| 378 | // construction and releases it upon destruction. |
| 379 | class SCOPED_LOCKABLE WriterMutexLock { |
| 380 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 381 | explicit WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : |
| 382 | self_(self), mu_(mu) { |
| 383 | mu_.ExclusiveLock(self_); |
| 384 | } |
| 385 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 386 | ~WriterMutexLock() UNLOCK_FUNCTION() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 387 | mu_.ExclusiveUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | private: |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 391 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 392 | ReaderWriterMutex& mu_; |
| 393 | DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); |
| 394 | }; |
| 395 | // Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of |
| 396 | // "WriterMutexLock mu(lock)". |
| 397 | #define WriterMutexLock(x) COMPILE_ASSERT(0, writer_mutex_lock_declaration_missing_variable_name) |
| 398 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 399 | } // namespace art |
| 400 | |
Elliott Hughes | 76b6167 | 2012-12-12 17:47:30 -0800 | [diff] [blame] | 401 | #endif // ART_SRC_BASE_MUTEX_H_ |