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