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 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 26 | #include "atomic.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 | |
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 |
Chris Dearman | c014178 | 2013-11-14 17:29:21 -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 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 46 | class SHARED_LOCKABLE ReaderWriterMutex; |
| 47 | class SHARED_LOCKABLE MutatorMutex; |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 48 | class ScopedContentionRecorder; |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 49 | class Thread; |
| 50 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 51 | // LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or |
| 52 | // equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free |
| 53 | // partial ordering and thereby cause deadlock situations to fail checks. |
| 54 | // |
| 55 | // [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163 |
| 56 | enum LockLevel { |
| 57 | kLoggingLock = 0, |
Raghu Gandham | 7de77dd | 2014-06-13 15:16:31 -0700 | [diff] [blame] | 58 | kSwapMutexesLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 59 | kUnexpectedSignalLock, |
| 60 | kThreadSuspendCountLock, |
| 61 | kAbortLock, |
Tao Wu | d0a160d | 2016-12-13 18:32:17 -0800 | [diff] [blame] | 62 | kJdwpAdbStateLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 63 | kJdwpSocketLock, |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 64 | kRegionSpaceRegionLock, |
Mathieu Chartier | 2994605 | 2015-12-21 13:02:14 -0800 | [diff] [blame] | 65 | kRosAllocGlobalLock, |
| 66 | kRosAllocBracketLock, |
| 67 | kRosAllocBulkFreeLock, |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 68 | kMarkSweepMarkStackLock, |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 69 | kTransactionLogLock, |
Andreas Gampe | c808954 | 2017-01-16 12:41:12 -0800 | [diff] [blame] | 70 | kJniFunctionTableLock, |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 71 | kJniWeakGlobalsLock, |
Andreas Gampe | 05a364c | 2016-10-14 13:27:12 -0700 | [diff] [blame] | 72 | kJniGlobalsLock, |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 73 | kReferenceQueueSoftReferencesLock, |
| 74 | kReferenceQueuePhantomReferencesLock, |
| 75 | kReferenceQueueFinalizerReferencesLock, |
| 76 | kReferenceQueueWeakReferencesLock, |
| 77 | kReferenceQueueClearedReferencesLock, |
| 78 | kReferenceProcessorLock, |
David Srbecky | 5cc349f | 2015-12-18 15:04:48 +0000 | [diff] [blame] | 79 | kJitDebugInterfaceLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 80 | kAllocSpaceLock, |
Hiroshi Yamauchi | 2cd334a | 2015-01-09 14:03:35 -0800 | [diff] [blame] | 81 | kBumpPointerSpaceBlockLock, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 82 | kArenaPoolLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 83 | kInternTableLock, |
Vladimir Marko | 3f5838d | 2014-08-07 18:07:18 +0100 | [diff] [blame] | 84 | kOatFileSecondaryLookupLock, |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 85 | kHostDlOpenHandlesLock, |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 86 | kVerifierDepsLock, |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 87 | kOatFileManagerLock, |
Andreas Gampe | 7526d78 | 2015-06-22 22:53:45 -0700 | [diff] [blame] | 88 | kTracingUniqueMethodsLock, |
| 89 | kTracingStreamingLock, |
Mathieu Chartier | b8aa1e4 | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 90 | kDeoptimizedMethodsLock, |
| 91 | kClassLoaderClassesLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 92 | kDefaultMutexLevel, |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 93 | kDexLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 94 | kMarkSweepLargeObjectLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 95 | kJdwpObjectRegistryLock, |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 96 | kModifyLdtLock, |
| 97 | kAllocatedThreadIdsLock, |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 98 | kMonitorPoolLock, |
Mathieu Chartier | b8aa1e4 | 2016-04-05 14:36:57 -0700 | [diff] [blame] | 99 | kClassLinkerClassesLock, // TODO rename. |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 100 | kJitCodeCacheLock, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 101 | kCHALock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 102 | kBreakpointLock, |
| 103 | kMonitorLock, |
Mathieu Chartier | 440e4ce | 2014-03-31 16:36:35 -0700 | [diff] [blame] | 104 | kMonitorListLock, |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 105 | kJniLoadLibraryLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 106 | kThreadListLock, |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 107 | kAllocTrackerLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 108 | kDeoptimizationLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 109 | kProfilerLock, |
Sebastien Hertz | 4e5b208 | 2015-03-24 19:03:40 +0100 | [diff] [blame] | 110 | kJdwpShutdownLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 111 | kJdwpEventListLock, |
| 112 | kJdwpAttachLock, |
| 113 | kJdwpStartLock, |
| 114 | kRuntimeShutdownLock, |
Jeff Hao | 69dbec6 | 2014-09-15 18:03:41 -0700 | [diff] [blame] | 115 | kTraceLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 116 | kHeapBitmapLock, |
| 117 | kMutatorLock, |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 118 | kInstrumentEntrypointsLock, |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 119 | kZygoteCreationLock, |
| 120 | |
| 121 | kLockLevelCount // Must come last. |
| 122 | }; |
| 123 | std::ostream& operator<<(std::ostream& os, const LockLevel& rhs); |
| 124 | |
Brian Carlstrom | 2e250c8 | 2013-08-14 18:08:52 -0700 | [diff] [blame] | 125 | const bool kDebugLocking = kIsDebugBuild; |
Ian Rogers | 25fd14b | 2012-09-05 10:56:38 -0700 | [diff] [blame] | 126 | |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 127 | // Record Log contention information, dumpable via SIGQUIT. |
| 128 | #ifdef ART_USE_FUTEXES |
Jeff Hao | 08f2e7b | 2013-09-09 16:44:02 -0700 | [diff] [blame] | 129 | // To enable lock contention logging, set this to true. |
| 130 | const bool kLogLockContentions = false; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 131 | #else |
| 132 | // Keep this false as lock contention logging is supported only with |
| 133 | // futex. |
| 134 | const bool kLogLockContentions = false; |
| 135 | #endif |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 136 | const size_t kContentionLogSize = 4; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 137 | const size_t kContentionLogDataSize = kLogLockContentions ? 1 : 0; |
| 138 | const size_t kAllMutexDataSize = kLogLockContentions ? 1 : 0; |
| 139 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 140 | // Base class for all Mutex implementations |
| 141 | class BaseMutex { |
| 142 | public: |
Ian Rogers | bab7496 | 2013-04-19 10:04:10 -0700 | [diff] [blame] | 143 | const char* GetName() const { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 144 | return name_; |
| 145 | } |
| 146 | |
| 147 | virtual bool IsMutex() const { return false; } |
| 148 | virtual bool IsReaderWriterMutex() const { return false; } |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 149 | virtual bool IsMutatorMutex() const { return false; } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 150 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 151 | virtual void Dump(std::ostream& os) const = 0; |
| 152 | |
| 153 | static void DumpAll(std::ostream& os); |
| 154 | |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 155 | bool ShouldRespondToEmptyCheckpointRequest() const { |
| 156 | return should_respond_to_empty_checkpoint_request_; |
| 157 | } |
| 158 | |
| 159 | void SetShouldRespondToEmptyCheckpointRequest(bool value) { |
| 160 | should_respond_to_empty_checkpoint_request_ = value; |
| 161 | } |
| 162 | |
| 163 | virtual void WakeupToRespondToEmptyCheckpoint() = 0; |
| 164 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 165 | protected: |
| 166 | friend class ConditionVariable; |
| 167 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 168 | BaseMutex(const char* name, LockLevel level); |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 169 | virtual ~BaseMutex(); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 170 | void RegisterAsLocked(Thread* self); |
| 171 | void RegisterAsUnlocked(Thread* self); |
| 172 | void CheckSafeToWait(Thread* self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 173 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 174 | friend class ScopedContentionRecorder; |
| 175 | |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 176 | 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] | 177 | void DumpContention(std::ostream& os) const; |
| 178 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 179 | const LockLevel level_; // Support for lock hierarchy. |
Ian Rogers | bab7496 | 2013-04-19 10:04:10 -0700 | [diff] [blame] | 180 | const char* const name_; |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 181 | bool should_respond_to_empty_checkpoint_request_; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 182 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 183 | // A log entry that records contention but makes no guarantee that either tid will be held live. |
| 184 | struct ContentionLogEntry { |
| 185 | ContentionLogEntry() : blocked_tid(0), owner_tid(0) {} |
| 186 | uint64_t blocked_tid; |
| 187 | uint64_t owner_tid; |
| 188 | AtomicInteger count; |
| 189 | }; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 190 | struct ContentionLogData { |
| 191 | ContentionLogEntry contention_log[kContentionLogSize]; |
| 192 | // The next entry in the contention log to be updated. Value ranges from 0 to |
| 193 | // kContentionLogSize - 1. |
| 194 | AtomicInteger cur_content_log_entry; |
| 195 | // Number of times the Mutex has been contended. |
| 196 | AtomicInteger contention_count; |
| 197 | // Sum of time waited by all contenders in ns. |
Ian Rogers | 37f3c96 | 2014-07-17 11:25:30 -0700 | [diff] [blame] | 198 | Atomic<uint64_t> wait_time; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 199 | void AddToWaitTime(uint64_t value); |
| 200 | ContentionLogData() : wait_time(0) {} |
| 201 | }; |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 202 | ContentionLogData contention_log_data_[kContentionLogDataSize]; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 203 | |
| 204 | public: |
| 205 | bool HasEverContended() const { |
| 206 | if (kLogLockContentions) { |
Ian Rogers | 3e5cf30 | 2014-05-20 16:40:37 -0700 | [diff] [blame] | 207 | return contention_log_data_->contention_count.LoadSequentiallyConsistent() > 0; |
Hiroshi Yamauchi | 1afde13 | 2013-08-06 17:09:30 -0700 | [diff] [blame] | 208 | } |
| 209 | return false; |
| 210 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | // A Mutex is used to achieve mutual exclusion between threads. A Mutex can be used to gain |
| 214 | // exclusive access to what it guards. A Mutex can be in one of two states: |
| 215 | // - Free - not owned by any thread, |
| 216 | // - Exclusive - owned by a single thread. |
| 217 | // |
| 218 | // The effect of locking and unlocking operations on the state is: |
| 219 | // State | ExclusiveLock | ExclusiveUnlock |
| 220 | // ------------------------------------------- |
| 221 | // Free | Exclusive | error |
| 222 | // Exclusive | Block* | Free |
| 223 | // * Mutex is not reentrant and so an attempt to ExclusiveLock on the same thread will result in |
| 224 | // an error. Being non-reentrant simplifies Waiting on ConditionVariables. |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 225 | std::ostream& operator<<(std::ostream& os, const Mutex& mu); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 226 | class LOCKABLE Mutex : public BaseMutex { |
| 227 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 228 | explicit Mutex(const char* name, LockLevel level = kDefaultMutexLevel, bool recursive = false); |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 229 | ~Mutex(); |
| 230 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 231 | virtual bool IsMutex() const { return true; } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 232 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 233 | // Block until mutex is free then acquire exclusive access. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 234 | void ExclusiveLock(Thread* self) ACQUIRE(); |
| 235 | void Lock(Thread* self) ACQUIRE() { ExclusiveLock(self); } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 236 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 237 | // Returns true if acquires exclusive access, false otherwise. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 238 | bool ExclusiveTryLock(Thread* self) TRY_ACQUIRE(true); |
| 239 | bool TryLock(Thread* self) TRY_ACQUIRE(true) { return ExclusiveTryLock(self); } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 240 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 241 | // Release exclusive access. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 242 | void ExclusiveUnlock(Thread* self) RELEASE(); |
| 243 | void Unlock(Thread* self) RELEASE() { ExclusiveUnlock(self); } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 244 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 245 | // Is the current thread the exclusive holder of the Mutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 246 | bool IsExclusiveHeld(const Thread* self) const; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 247 | |
| 248 | // Assert that the Mutex is exclusively held by the current thread. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 249 | void AssertExclusiveHeld(const Thread* self) ASSERT_CAPABILITY(this) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 250 | if (kDebugLocking && (gAborting == 0)) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 251 | CHECK(IsExclusiveHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 254 | void AssertHeld(const Thread* self) ASSERT_CAPABILITY(this) { AssertExclusiveHeld(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 255 | |
| 256 | // Assert that the Mutex is not held by the current thread. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 257 | void AssertNotHeldExclusive(const Thread* self) ASSERT_CAPABILITY(!*this) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 258 | if (kDebugLocking && (gAborting == 0)) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 259 | CHECK(!IsExclusiveHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 260 | } |
| 261 | } |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 262 | void AssertNotHeld(const Thread* self) ASSERT_CAPABILITY(!*this) { |
| 263 | AssertNotHeldExclusive(self); |
| 264 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 265 | |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame] | 266 | // Id associated with exclusive owner. No memory ordering semantics if called from a thread other |
| 267 | // than the owner. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 268 | uint64_t GetExclusiveOwnerTid() const; |
| 269 | |
| 270 | // Returns how many times this Mutex has been locked, it is better to use AssertHeld/NotHeld. |
| 271 | unsigned int GetDepth() const { |
| 272 | return recursion_count_; |
| 273 | } |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 274 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 275 | virtual void Dump(std::ostream& os) const; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 276 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 277 | // For negative capabilities in clang annotations. |
| 278 | const Mutex& operator!() const { return *this; } |
| 279 | |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 280 | void WakeupToRespondToEmptyCheckpoint() OVERRIDE; |
| 281 | |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 282 | private: |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 283 | #if ART_USE_FUTEXES |
| 284 | // 0 is unheld, 1 is held. |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame] | 285 | AtomicInteger state_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 286 | // Exclusive owner. |
| 287 | volatile uint64_t exclusive_owner_; |
| 288 | // Number of waiting contenders. |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 289 | AtomicInteger num_contenders_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 290 | #else |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 291 | pthread_mutex_t mutex_; |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 292 | volatile uint64_t exclusive_owner_; // Guarded by mutex_. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 293 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 294 | const bool recursive_; // Can the lock be recursively held? |
| 295 | unsigned int recursion_count_; |
Elliott Hughes | f149843 | 2012-03-28 19:34:27 -0700 | [diff] [blame] | 296 | friend class ConditionVariable; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 297 | DISALLOW_COPY_AND_ASSIGN(Mutex); |
| 298 | }; |
| 299 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 300 | // A ReaderWriterMutex is used to achieve mutual exclusion between threads, similar to a Mutex. |
| 301 | // Unlike a Mutex a ReaderWriterMutex can be used to gain exclusive (writer) or shared (reader) |
| 302 | // access to what it guards. A flaw in relation to a Mutex is that it cannot be used with a |
| 303 | // condition variable. A ReaderWriterMutex can be in one of three states: |
| 304 | // - Free - not owned by any thread, |
| 305 | // - Exclusive - owned by a single thread, |
| 306 | // - Shared(n) - shared amongst n threads. |
| 307 | // |
| 308 | // The effect of locking and unlocking operations on the state is: |
| 309 | // |
| 310 | // State | ExclusiveLock | ExclusiveUnlock | SharedLock | SharedUnlock |
| 311 | // ---------------------------------------------------------------------------- |
| 312 | // Free | Exclusive | error | SharedLock(1) | error |
| 313 | // Exclusive | Block | Free | Block | error |
| 314 | // Shared(n) | Block | error | SharedLock(n+1)* | Shared(n-1) or Free |
| 315 | // * for large values of n the SharedLock may block. |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 316 | std::ostream& operator<<(std::ostream& os, const ReaderWriterMutex& mu); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 317 | class SHARED_LOCKABLE ReaderWriterMutex : public BaseMutex { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 318 | public: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 319 | explicit ReaderWriterMutex(const char* name, LockLevel level = kDefaultMutexLevel); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 320 | ~ReaderWriterMutex(); |
| 321 | |
| 322 | virtual bool IsReaderWriterMutex() const { return true; } |
| 323 | |
| 324 | // Block until ReaderWriterMutex is free then acquire exclusive access. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 325 | void ExclusiveLock(Thread* self) ACQUIRE(); |
| 326 | void WriterLock(Thread* self) ACQUIRE() { ExclusiveLock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 327 | |
| 328 | // Release exclusive access. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 329 | void ExclusiveUnlock(Thread* self) RELEASE(); |
| 330 | void WriterUnlock(Thread* self) RELEASE() { ExclusiveUnlock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 331 | |
| 332 | // Block until ReaderWriterMutex is free and acquire exclusive access. Returns true on success |
| 333 | // or false if timeout is reached. |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 334 | #if HAVE_TIMED_RWLOCK |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 335 | bool ExclusiveLockWithTimeout(Thread* self, int64_t ms, int32_t ns) |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 336 | EXCLUSIVE_TRYLOCK_FUNCTION(true); |
Ian Rogers | 66aee5c | 2012-08-15 17:17:47 -0700 | [diff] [blame] | 337 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 338 | |
| 339 | // Block until ReaderWriterMutex is shared or free then acquire a share on the access. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 340 | void SharedLock(Thread* self) ACQUIRE_SHARED() ALWAYS_INLINE; |
| 341 | void ReaderLock(Thread* self) ACQUIRE_SHARED() { SharedLock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 342 | |
| 343 | // Try to acquire share of ReaderWriterMutex. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 344 | bool SharedTryLock(Thread* self) SHARED_TRYLOCK_FUNCTION(true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 345 | |
| 346 | // Release a share of the access. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 347 | void SharedUnlock(Thread* self) RELEASE_SHARED() ALWAYS_INLINE; |
| 348 | void ReaderUnlock(Thread* self) RELEASE_SHARED() { SharedUnlock(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 349 | |
| 350 | // Is the current thread the exclusive holder of the ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 351 | bool IsExclusiveHeld(const Thread* self) const; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 352 | |
| 353 | // Assert the current thread has exclusive access to the ReaderWriterMutex. |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 354 | void AssertExclusiveHeld(const Thread* self) ASSERT_CAPABILITY(this) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 355 | if (kDebugLocking && (gAborting == 0)) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 356 | CHECK(IsExclusiveHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 359 | void AssertWriterHeld(const Thread* self) ASSERT_CAPABILITY(this) { AssertExclusiveHeld(self); } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 360 | |
| 361 | // Assert the current thread doesn't have exclusive access to the ReaderWriterMutex. |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 362 | void AssertNotExclusiveHeld(const Thread* self) ASSERT_CAPABILITY(!this) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 363 | if (kDebugLocking && (gAborting == 0)) { |
Ian Rogers | e3359f7 | 2013-06-11 15:14:11 -0700 | [diff] [blame] | 364 | CHECK(!IsExclusiveHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 367 | void AssertNotWriterHeld(const Thread* self) ASSERT_CAPABILITY(!this) { |
| 368 | AssertNotExclusiveHeld(self); |
| 369 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 370 | |
| 371 | // Is the current thread a shared holder of the ReaderWriterMutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 372 | bool IsSharedHeld(const Thread* self) const; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 373 | |
| 374 | // Assert the current thread has shared access to the ReaderWriterMutex. |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 375 | void AssertSharedHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 376 | if (kDebugLocking && (gAborting == 0)) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 377 | // TODO: we can only assert this well when self != null. |
| 378 | CHECK(IsSharedHeld(self) || self == nullptr) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 381 | void AssertReaderHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(this) { |
| 382 | AssertSharedHeld(self); |
| 383 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 384 | |
| 385 | // Assert the current thread doesn't hold this ReaderWriterMutex either in shared or exclusive |
| 386 | // mode. |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 387 | void AssertNotHeld(const Thread* self) ASSERT_SHARED_CAPABILITY(!this) { |
Nicolas Geoffray | db97871 | 2014-12-09 13:33:38 +0000 | [diff] [blame] | 388 | if (kDebugLocking && (gAborting == 0)) { |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 389 | CHECK(!IsSharedHeld(self)) << *this; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 390 | } |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame] | 393 | // Id associated with exclusive owner. No memory ordering semantics if called from a thread other |
| 394 | // than the owner. |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 395 | uint64_t GetExclusiveOwnerTid() const; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 396 | |
Ian Rogers | 56edc43 | 2013-01-18 16:51:51 -0800 | [diff] [blame] | 397 | virtual void Dump(std::ostream& os) const; |
Ian Rogers | 01ae580 | 2012-09-28 16:14:01 -0700 | [diff] [blame] | 398 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 399 | // For negative capabilities in clang annotations. |
| 400 | const ReaderWriterMutex& operator!() const { return *this; } |
| 401 | |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 402 | void WakeupToRespondToEmptyCheckpoint() OVERRIDE; |
| 403 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 404 | private: |
Ian Rogers | 51d212e | 2014-10-23 17:48:20 -0700 | [diff] [blame] | 405 | #if ART_USE_FUTEXES |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 406 | // Out-of-inline path for handling contention for a SharedLock. |
| 407 | void HandleSharedLockContention(Thread* self, int32_t cur_state); |
| 408 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 409 | // -1 implies held exclusive, +ve shared held by state_ many owners. |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame] | 410 | AtomicInteger state_; |
| 411 | // Exclusive owner. Modification guarded by this mutex. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 412 | volatile uint64_t exclusive_owner_; |
Ian Rogers | c719069 | 2014-07-08 23:50:26 -0700 | [diff] [blame] | 413 | // Number of contenders waiting for a reader share. |
| 414 | AtomicInteger num_pending_readers_; |
| 415 | // Number of contenders waiting to be the writer. |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 416 | AtomicInteger num_pending_writers_; |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 417 | #else |
| 418 | pthread_rwlock_t rwlock_; |
Ian Rogers | c5f1773 | 2014-06-05 20:48:42 -0700 | [diff] [blame] | 419 | volatile uint64_t exclusive_owner_; // Guarded by rwlock_. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 420 | #endif |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 421 | DISALLOW_COPY_AND_ASSIGN(ReaderWriterMutex); |
| 422 | }; |
| 423 | |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 424 | // MutatorMutex is a special kind of ReaderWriterMutex created specifically for the |
| 425 | // Locks::mutator_lock_ mutex. The behaviour is identical to the ReaderWriterMutex except that |
| 426 | // thread state changes also play a part in lock ownership. The mutator_lock_ will not be truly |
| 427 | // held by any mutator threads. However, a thread in the kRunnable state is considered to have |
| 428 | // shared ownership of the mutator lock and therefore transitions in and out of the kRunnable |
| 429 | // state have associated implications on lock ownership. Extra methods to handle the state |
| 430 | // transitions have been added to the interface but are only accessible to the methods dealing |
| 431 | // with state transitions. The thread state and flags attributes are used to ensure thread state |
| 432 | // transitions are consistent with the permitted behaviour of the mutex. |
| 433 | // |
| 434 | // *) The most important consequence of this behaviour is that all threads must be in one of the |
| 435 | // suspended states before exclusive ownership of the mutator mutex is sought. |
| 436 | // |
| 437 | std::ostream& operator<<(std::ostream& os, const MutatorMutex& mu); |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 438 | class SHARED_LOCKABLE MutatorMutex : public ReaderWriterMutex { |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 439 | public: |
| 440 | explicit MutatorMutex(const char* name, LockLevel level = kDefaultMutexLevel) |
| 441 | : ReaderWriterMutex(name, level) {} |
| 442 | ~MutatorMutex() {} |
| 443 | |
| 444 | virtual bool IsMutatorMutex() const { return true; } |
| 445 | |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 446 | // For negative capabilities in clang annotations. |
| 447 | const MutatorMutex& operator!() const { return *this; } |
| 448 | |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 449 | private: |
| 450 | friend class Thread; |
| 451 | void TransitionFromRunnableToSuspended(Thread* self) UNLOCK_FUNCTION() ALWAYS_INLINE; |
| 452 | void TransitionFromSuspendedToRunnable(Thread* self) SHARED_LOCK_FUNCTION() ALWAYS_INLINE; |
| 453 | |
| 454 | DISALLOW_COPY_AND_ASSIGN(MutatorMutex); |
| 455 | }; |
| 456 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 457 | // ConditionVariables allow threads to queue and sleep. Threads may then be resumed individually |
| 458 | // (Signal) or all at once (Broadcast). |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 459 | class ConditionVariable { |
| 460 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 461 | ConditionVariable(const char* name, Mutex& mutex); |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 462 | ~ConditionVariable(); |
| 463 | |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 464 | void Broadcast(Thread* self); |
| 465 | void Signal(Thread* self); |
| 466 | // TODO: No thread safety analysis on Wait and TimedWait as they call mutex operations via their |
| 467 | // pointer copy, thereby defeating annotalysis. |
| 468 | void Wait(Thread* self) NO_THREAD_SAFETY_ANALYSIS; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 469 | bool 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] | 470 | // Variant of Wait that should be used with caution. Doesn't validate that no mutexes are held |
| 471 | // when waiting. |
| 472 | // TODO: remove this. |
| 473 | void WaitHoldingLocks(Thread* self) NO_THREAD_SAFETY_ANALYSIS; |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 474 | |
| 475 | private: |
Ian Rogers | 23055dc | 2013-04-18 16:29:16 -0700 | [diff] [blame] | 476 | const char* const name_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 477 | // The Mutex being used by waiters. It is an error to mix condition variables between different |
| 478 | // Mutexes. |
| 479 | Mutex& guard_; |
| 480 | #if ART_USE_FUTEXES |
| 481 | // 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] | 482 | // their Mutex and another thread takes it and signals, the waiting thread observes that sequence_ |
| 483 | // changed and doesn't enter the wait. Modified while holding guard_, but is read by futex wait |
| 484 | // without guard_ held. |
Ian Rogers | b122a4b | 2013-11-19 18:00:50 -0800 | [diff] [blame] | 485 | AtomicInteger sequence_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 486 | // 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] | 487 | // waiters may have been requeued onto guard_. Guarded by guard_. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 488 | volatile int32_t num_waiters_; |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 489 | #else |
| 490 | pthread_cond_t cond_; |
| 491 | #endif |
Elliott Hughes | 5f79133 | 2011-09-15 17:45:30 -0700 | [diff] [blame] | 492 | DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 493 | }; |
| 494 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 495 | // Scoped locker/unlocker for a regular Mutex that acquires mu upon construction and releases it |
| 496 | // upon destruction. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 497 | class SCOPED_CAPABILITY MutexLock { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 498 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 499 | MutexLock(Thread* self, Mutex& mu) ACQUIRE(mu) : self_(self), mu_(mu) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 500 | mu_.ExclusiveLock(self_); |
| 501 | } |
| 502 | |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 503 | ~MutexLock() RELEASE() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 504 | mu_.ExclusiveUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | private: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 508 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 509 | Mutex& mu_; |
| 510 | DISALLOW_COPY_AND_ASSIGN(MutexLock); |
| 511 | }; |
| 512 | // Catch bug where variable name is omitted. "MutexLock (lock);" instead of "MutexLock mu(lock)". |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 513 | #define MutexLock(x) static_assert(0, "MutexLock declaration missing variable name") |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 514 | |
| 515 | // Scoped locker/unlocker for a ReaderWriterMutex that acquires read access to mu upon |
| 516 | // construction and releases it upon destruction. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 517 | class SCOPED_CAPABILITY ReaderMutexLock { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 518 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 519 | ReaderMutexLock(Thread* self, ReaderWriterMutex& mu) ACQUIRE(mu) : |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 520 | self_(self), mu_(mu) { |
| 521 | mu_.SharedLock(self_); |
| 522 | } |
| 523 | |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 524 | ~ReaderMutexLock() RELEASE() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 525 | mu_.SharedUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | private: |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 529 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 530 | ReaderWriterMutex& mu_; |
| 531 | DISALLOW_COPY_AND_ASSIGN(ReaderMutexLock); |
| 532 | }; |
| 533 | // Catch bug where variable name is omitted. "ReaderMutexLock (lock);" instead of |
| 534 | // "ReaderMutexLock mu(lock)". |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 535 | #define ReaderMutexLock(x) static_assert(0, "ReaderMutexLock declaration missing variable name") |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 536 | |
| 537 | // Scoped locker/unlocker for a ReaderWriterMutex that acquires write access to mu upon |
| 538 | // construction and releases it upon destruction. |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 539 | class SCOPED_CAPABILITY WriterMutexLock { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 540 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 541 | WriterMutexLock(Thread* self, ReaderWriterMutex& mu) EXCLUSIVE_LOCK_FUNCTION(mu) : |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 542 | self_(self), mu_(mu) { |
| 543 | mu_.ExclusiveLock(self_); |
| 544 | } |
| 545 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 546 | ~WriterMutexLock() UNLOCK_FUNCTION() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 547 | mu_.ExclusiveUnlock(self_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | private: |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 551 | Thread* const self_; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 552 | ReaderWriterMutex& mu_; |
| 553 | DISALLOW_COPY_AND_ASSIGN(WriterMutexLock); |
| 554 | }; |
| 555 | // Catch bug where variable name is omitted. "WriterMutexLock (lock);" instead of |
| 556 | // "WriterMutexLock mu(lock)". |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 557 | #define WriterMutexLock(x) static_assert(0, "WriterMutexLock declaration missing variable name") |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 558 | |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 559 | // For StartNoThreadSuspension and EndNoThreadSuspension. |
| 560 | class CAPABILITY("role") Role { |
| 561 | public: |
| 562 | void Acquire() ACQUIRE() {} |
| 563 | void Release() RELEASE() {} |
| 564 | const Role& operator!() const { return *this; } |
| 565 | }; |
| 566 | |
| 567 | class Uninterruptible : public Role { |
| 568 | }; |
| 569 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 570 | // Global mutexes corresponding to the levels above. |
| 571 | class Locks { |
| 572 | public: |
| 573 | static void Init(); |
Mathieu Chartier | 91e5669 | 2015-03-03 13:51:04 -0800 | [diff] [blame] | 574 | static void InitConditions() NO_THREAD_SAFETY_ANALYSIS; // Condition variables. |
David Sehr | f42eb2c | 2016-10-19 13:20:45 -0700 | [diff] [blame] | 575 | |
| 576 | // Destroying various lock types can emit errors that vary depending upon |
| 577 | // whether the client (art::Runtime) is currently active. Allow the client |
| 578 | // to set a callback that is used to check when it is acceptable to call |
| 579 | // Abort. The default behavior is that the client *is not* able to call |
| 580 | // Abort if no callback is established. |
| 581 | using ClientCallback = bool(); |
| 582 | static void SetClientCallback(ClientCallback* is_safe_to_call_abort_cb) NO_THREAD_SAFETY_ANALYSIS; |
| 583 | // Checks for whether it is safe to call Abort() without using locks. |
| 584 | static bool IsSafeToCallAbortRacy() NO_THREAD_SAFETY_ANALYSIS; |
| 585 | |
| 586 | |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 587 | // Guards allocation entrypoint instrumenting. |
Ian Rogers | 4ad5cd3 | 2014-11-11 23:08:07 -0800 | [diff] [blame] | 588 | static Mutex* instrument_entrypoints_lock_; |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 589 | |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 590 | // A barrier is used to synchronize the GC/Debugger thread with mutator threads. When GC/Debugger |
| 591 | // thread wants to suspend all mutator threads, it needs to wait for all mutator threads to pass |
| 592 | // a barrier. Threads that are already suspended will get their barrier passed by the GC/Debugger |
| 593 | // thread; threads in the runnable state will pass the barrier when they transit to the suspended |
| 594 | // state. GC/Debugger thread will be woken up when all mutator threads are suspended. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 595 | // |
| 596 | // Thread suspension: |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 597 | // mutator thread | GC/Debugger |
| 598 | // .. running .. | .. running .. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 599 | // .. running .. | Request thread suspension by: |
| 600 | // .. running .. | - acquiring thread_suspend_count_lock_ |
| 601 | // .. running .. | - incrementing Thread::suspend_count_ on |
| 602 | // .. running .. | all mutator threads |
| 603 | // .. running .. | - releasing thread_suspend_count_lock_ |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 604 | // .. running .. | Block wait for all threads to pass a barrier |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 605 | // Poll Thread::suspend_count_ and enter full | .. blocked .. |
| 606 | // suspend code. | .. blocked .. |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 607 | // Change state to kSuspended (pass the barrier) | Wake up when all threads pass the barrier |
| 608 | // x: Acquire thread_suspend_count_lock_ | .. running .. |
| 609 | // while Thread::suspend_count_ > 0 | .. running .. |
| 610 | // - wait on Thread::resume_cond_ | .. running .. |
| 611 | // (releases thread_suspend_count_lock_) | .. running .. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 612 | // .. waiting .. | Request thread resumption by: |
| 613 | // .. waiting .. | - acquiring thread_suspend_count_lock_ |
| 614 | // .. waiting .. | - decrementing Thread::suspend_count_ on |
| 615 | // .. waiting .. | all mutator threads |
| 616 | // .. waiting .. | - notifying on Thread::resume_cond_ |
| 617 | // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ |
| 618 | // Release thread_suspend_count_lock_ | .. running .. |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 619 | // Change to kRunnable | .. running .. |
| 620 | // - this uses a CAS operation to ensure the | .. running .. |
| 621 | // suspend request flag isn't raised as the | .. running .. |
| 622 | // state is changed | .. running .. |
| 623 | // - if the CAS operation fails then goto x | .. running .. |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 624 | // .. running .. | .. running .. |
Yu Li | eac4424 | 2015-06-29 10:50:03 +0800 | [diff] [blame] | 625 | static MutatorMutex* mutator_lock_ ACQUIRED_AFTER(instrument_entrypoints_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 626 | |
| 627 | // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. |
| 628 | static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); |
| 629 | |
| 630 | // Guards shutdown of the runtime. |
| 631 | static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); |
| 632 | |
Hiroshi Yamauchi | 35b456a | 2017-02-22 18:06:55 -0800 | [diff] [blame] | 633 | static Mutex* jdwp_event_list_lock_ |
| 634 | ACQUIRED_AFTER(runtime_shutdown_lock_) |
| 635 | ACQUIRED_BEFORE(breakpoint_lock_); |
| 636 | |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 637 | // Guards background profiler global state. |
Hiroshi Yamauchi | 35b456a | 2017-02-22 18:06:55 -0800 | [diff] [blame] | 638 | static Mutex* profiler_lock_ ACQUIRED_AFTER(jdwp_event_list_lock_); |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 639 | |
| 640 | // Guards trace (ie traceview) requests. |
| 641 | static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_); |
| 642 | |
Brian Carlstrom | 306db81 | 2014-09-05 13:01:41 -0700 | [diff] [blame] | 643 | // Guards debugger recent allocation records. |
| 644 | static Mutex* alloc_tracker_lock_ ACQUIRED_AFTER(trace_lock_); |
| 645 | |
| 646 | // Guards updates to instrumentation to ensure mutual exclusion of |
| 647 | // events like deoptimization requests. |
| 648 | // TODO: improve name, perhaps instrumentation_update_lock_. |
| 649 | static Mutex* deoptimization_lock_ ACQUIRED_AFTER(alloc_tracker_lock_); |
| 650 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 651 | // Guards Class Hierarchy Analysis (CHA). |
| 652 | static Mutex* cha_lock_ ACQUIRED_AFTER(deoptimization_lock_); |
| 653 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 654 | // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads |
| 655 | // attaching and detaching. |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 656 | static Mutex* thread_list_lock_ ACQUIRED_AFTER(cha_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 657 | |
Mathieu Chartier | 91e5669 | 2015-03-03 13:51:04 -0800 | [diff] [blame] | 658 | // Signaled when threads terminate. Used to determine when all non-daemons have terminated. |
| 659 | static ConditionVariable* thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_); |
| 660 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 661 | // Guards maintaining loading library data structures. |
| 662 | static Mutex* jni_libraries_lock_ ACQUIRED_AFTER(thread_list_lock_); |
| 663 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 664 | // Guards breakpoints. |
Sebastien Hertz | ed2be17 | 2014-08-19 15:33:43 +0200 | [diff] [blame] | 665 | static ReaderWriterMutex* breakpoint_lock_ ACQUIRED_AFTER(jni_libraries_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 666 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 667 | // Guards lists of classes within the class linker. |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 668 | static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 669 | |
| 670 | // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code |
| 671 | // doesn't try to hold a higher level Mutex. |
Mathieu Chartier | 12d625f | 2015-03-13 11:33:37 -0700 | [diff] [blame] | 672 | #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(Locks::classlinker_classes_lock_) |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 673 | |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 674 | static Mutex* allocated_monitor_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); |
| 675 | |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 676 | // Guard the allocation/deallocation of thread ids. |
Andreas Gampe | 7424081 | 2014-04-17 10:35:09 -0700 | [diff] [blame] | 677 | static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(allocated_monitor_ids_lock_); |
Chao-ying Fu | 9e36931 | 2014-05-21 11:20:52 -0700 | [diff] [blame] | 678 | |
| 679 | // Guards modification of the LDT on x86. |
| 680 | static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_); |
| 681 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 682 | static ReaderWriterMutex* dex_lock_ ACQUIRED_AFTER(modify_ldt_lock_); |
| 683 | |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 684 | // Guards opened oat files in OatFileManager. |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 685 | static ReaderWriterMutex* oat_file_manager_lock_ ACQUIRED_AFTER(dex_lock_); |
Mathieu Chartier | f9c6fc6 | 2015-10-07 11:44:05 -0700 | [diff] [blame] | 686 | |
Nicolas Geoffray | 340dafa | 2016-11-18 16:03:10 +0000 | [diff] [blame] | 687 | // Guards extra string entries for VerifierDeps. |
| 688 | static ReaderWriterMutex* verifier_deps_lock_ ACQUIRED_AFTER(oat_file_manager_lock_); |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 689 | |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 690 | // Guards dlopen_handles_ in DlOpenOatFile. |
David Brazdil | ca3c8c3 | 2016-09-06 14:04:48 +0100 | [diff] [blame] | 691 | static Mutex* host_dlopen_handles_lock_ ACQUIRED_AFTER(verifier_deps_lock_); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 692 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 693 | // Guards intern table. |
Richard Uhler | a206c74 | 2016-05-24 15:04:22 -0700 | [diff] [blame] | 694 | static Mutex* intern_table_lock_ ACQUIRED_AFTER(host_dlopen_handles_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 695 | |
Mathieu Chartier | a5a53ef | 2014-09-12 12:58:05 -0700 | [diff] [blame] | 696 | // Guards reference processor. |
| 697 | static Mutex* reference_processor_lock_ ACQUIRED_AFTER(intern_table_lock_); |
| 698 | |
| 699 | // Guards cleared references queue. |
| 700 | static Mutex* reference_queue_cleared_references_lock_ ACQUIRED_AFTER(reference_processor_lock_); |
| 701 | |
| 702 | // Guards weak references queue. |
| 703 | static Mutex* reference_queue_weak_references_lock_ ACQUIRED_AFTER(reference_queue_cleared_references_lock_); |
| 704 | |
| 705 | // Guards finalizer references queue. |
| 706 | static Mutex* reference_queue_finalizer_references_lock_ ACQUIRED_AFTER(reference_queue_weak_references_lock_); |
| 707 | |
| 708 | // Guards phantom references queue. |
| 709 | static Mutex* reference_queue_phantom_references_lock_ ACQUIRED_AFTER(reference_queue_finalizer_references_lock_); |
| 710 | |
| 711 | // Guards soft references queue. |
| 712 | static Mutex* reference_queue_soft_references_lock_ ACQUIRED_AFTER(reference_queue_phantom_references_lock_); |
| 713 | |
Andreas Gampe | 05a364c | 2016-10-14 13:27:12 -0700 | [diff] [blame] | 714 | // Guard accesses to the JNI Global Reference table. |
| 715 | static ReaderWriterMutex* jni_globals_lock_ ACQUIRED_AFTER(reference_queue_soft_references_lock_); |
| 716 | |
| 717 | // Guard accesses to the JNI Weak Global Reference table. |
| 718 | static Mutex* jni_weak_globals_lock_ ACQUIRED_AFTER(jni_globals_lock_); |
| 719 | |
Andreas Gampe | c808954 | 2017-01-16 12:41:12 -0800 | [diff] [blame] | 720 | // Guard accesses to the JNI function table override. |
| 721 | static Mutex* jni_function_table_lock_ ACQUIRED_AFTER(jni_weak_globals_lock_); |
| 722 | |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 723 | // Have an exclusive aborting thread. |
Andreas Gampe | c808954 | 2017-01-16 12:41:12 -0800 | [diff] [blame] | 724 | static Mutex* abort_lock_ ACQUIRED_AFTER(jni_function_table_lock_); |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 725 | |
| 726 | // Allow mutual exclusion when manipulating Thread::suspend_count_. |
| 727 | // TODO: Does the trade-off of a per-thread lock make sense? |
| 728 | static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); |
| 729 | |
| 730 | // One unexpected signal at a time lock. |
| 731 | static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); |
| 732 | |
| 733 | // Have an exclusive logging thread. |
| 734 | static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); |
Hiroshi Yamauchi | a222404 | 2017-02-08 16:35:45 -0800 | [diff] [blame] | 735 | |
| 736 | // List of mutexes that we expect a thread may hold when accessing weak refs. This is used to |
| 737 | // avoid a deadlock in the empty checkpoint while weak ref access is disabled (b/34964016). If we |
| 738 | // encounter an unexpected mutex on accessing weak refs, |
| 739 | // Thread::CheckEmptyCheckpointFromWeakRefAccess will detect it. |
| 740 | static std::vector<BaseMutex*> expected_mutexes_on_weak_ref_access_; |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 741 | }; |
| 742 | |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 743 | class Roles { |
| 744 | public: |
Mathieu Chartier | 90ef3db | 2015-08-04 15:19:41 -0700 | [diff] [blame] | 745 | // Uninterruptible means that the thread may not become suspended. |
Mathieu Chartier | 4e2cb09 | 2015-07-22 16:17:51 -0700 | [diff] [blame] | 746 | static Uninterruptible uninterruptible_; |
| 747 | }; |
| 748 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 749 | } // namespace art |
| 750 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 751 | #endif // ART_RUNTIME_BASE_MUTEX_H_ |