1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_BASE_LOCKS_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_BASE_LOCKS_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include <stdint.h> 21*795d594fSAndroid Build Coastguard Worker 22*795d594fSAndroid Build Coastguard Worker #include <iosfwd> 23*795d594fSAndroid Build Coastguard Worker #include <vector> 24*795d594fSAndroid Build Coastguard Worker 25*795d594fSAndroid Build Coastguard Worker #include "base/atomic.h" 26*795d594fSAndroid Build Coastguard Worker #include "base/macros.h" 27*795d594fSAndroid Build Coastguard Worker 28*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN { 29*795d594fSAndroid Build Coastguard Worker 30*795d594fSAndroid Build Coastguard Worker class BaseMutex; 31*795d594fSAndroid Build Coastguard Worker class ConditionVariable; 32*795d594fSAndroid Build Coastguard Worker class SHARED_LOCKABLE ReaderWriterMutex; 33*795d594fSAndroid Build Coastguard Worker class SHARED_LOCKABLE MutatorMutex; 34*795d594fSAndroid Build Coastguard Worker class LOCKABLE Mutex; 35*795d594fSAndroid Build Coastguard Worker class Thread; 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker // LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or 38*795d594fSAndroid Build Coastguard Worker // equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free 39*795d594fSAndroid Build Coastguard Worker // partial ordering and thereby cause deadlock situations to fail checks. 40*795d594fSAndroid Build Coastguard Worker // 41*795d594fSAndroid Build Coastguard Worker // [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163 42*795d594fSAndroid Build Coastguard Worker enum LockLevel : uint8_t { 43*795d594fSAndroid Build Coastguard Worker kLoggingLock = 0, 44*795d594fSAndroid Build Coastguard Worker kSwapMutexesLock, 45*795d594fSAndroid Build Coastguard Worker kUnexpectedSignalLock, 46*795d594fSAndroid Build Coastguard Worker kThreadSuspendCountLock, 47*795d594fSAndroid Build Coastguard Worker kAbortLock, 48*795d594fSAndroid Build Coastguard Worker kJniIdLock, 49*795d594fSAndroid Build Coastguard Worker kNativeDebugInterfaceLock, 50*795d594fSAndroid Build Coastguard Worker kSignalHandlingLock, 51*795d594fSAndroid Build Coastguard Worker // A generic lock level for mutexes that should not allow any additional mutexes to be gained 52*795d594fSAndroid Build Coastguard Worker // after acquiring it. 53*795d594fSAndroid Build Coastguard Worker kGenericBottomLock, 54*795d594fSAndroid Build Coastguard Worker // Tracks the second acquisition at the same lock level for kThreadWaitLock. This is an exception 55*795d594fSAndroid Build Coastguard Worker // to the normal lock ordering, used to implement Monitor::Wait - while holding one kThreadWait 56*795d594fSAndroid Build Coastguard Worker // level lock, it is permitted to acquire a second one - with internal safeguards to ensure that 57*795d594fSAndroid Build Coastguard Worker // the second lock acquisition does not result in deadlock. This is implemented in the lock 58*795d594fSAndroid Build Coastguard Worker // order by treating the second acquisition of a kThreadWaitLock as a kThreadWaitWakeLock 59*795d594fSAndroid Build Coastguard Worker // acquisition. Thus, acquiring kThreadWaitWakeLock requires holding kThreadWaitLock. This entry 60*795d594fSAndroid Build Coastguard Worker // is here near the bottom of the hierarchy because other locks should not be 61*795d594fSAndroid Build Coastguard Worker // acquired while it is held. kThreadWaitLock cannot be moved here because GC 62*795d594fSAndroid Build Coastguard Worker // activity acquires locks while holding the wait lock. 63*795d594fSAndroid Build Coastguard Worker kThreadWaitWakeLock, 64*795d594fSAndroid Build Coastguard Worker kJdwpAdbStateLock, 65*795d594fSAndroid Build Coastguard Worker kJdwpSocketLock, 66*795d594fSAndroid Build Coastguard Worker kRegionSpaceRegionLock, 67*795d594fSAndroid Build Coastguard Worker kMarkSweepMarkStackLock, 68*795d594fSAndroid Build Coastguard Worker // Can be held while GC related work is done, and thus must be above kMarkSweepMarkStackLock 69*795d594fSAndroid Build Coastguard Worker kThreadWaitLock, 70*795d594fSAndroid Build Coastguard Worker kJitCodeCacheMutatorAndCHALock, 71*795d594fSAndroid Build Coastguard Worker kRosAllocGlobalLock, 72*795d594fSAndroid Build Coastguard Worker kRosAllocBracketLock, 73*795d594fSAndroid Build Coastguard Worker kRosAllocBulkFreeLock, 74*795d594fSAndroid Build Coastguard Worker kAllocSpaceLock, 75*795d594fSAndroid Build Coastguard Worker kTaggingLockLevel, 76*795d594fSAndroid Build Coastguard Worker kJitCodeCacheLock, 77*795d594fSAndroid Build Coastguard Worker kTransactionLogLock, 78*795d594fSAndroid Build Coastguard Worker kCustomTlsLock, 79*795d594fSAndroid Build Coastguard Worker kJniFunctionTableLock, 80*795d594fSAndroid Build Coastguard Worker kJniWeakGlobalsLock, 81*795d594fSAndroid Build Coastguard Worker kJniGlobalsLock, 82*795d594fSAndroid Build Coastguard Worker kReferenceQueueSoftReferencesLock, 83*795d594fSAndroid Build Coastguard Worker kReferenceQueuePhantomReferencesLock, 84*795d594fSAndroid Build Coastguard Worker kReferenceQueueFinalizerReferencesLock, 85*795d594fSAndroid Build Coastguard Worker kReferenceQueueWeakReferencesLock, 86*795d594fSAndroid Build Coastguard Worker kReferenceQueueClearedReferencesLock, 87*795d594fSAndroid Build Coastguard Worker kReferenceProcessorLock, 88*795d594fSAndroid Build Coastguard Worker kJitDebugInterfaceLock, 89*795d594fSAndroid Build Coastguard Worker kBumpPointerSpaceBlockLock, 90*795d594fSAndroid Build Coastguard Worker kArenaPoolLock, 91*795d594fSAndroid Build Coastguard Worker kInternTableLock, 92*795d594fSAndroid Build Coastguard Worker kOatFileSecondaryLookupLock, 93*795d594fSAndroid Build Coastguard Worker kHostDlOpenHandlesLock, 94*795d594fSAndroid Build Coastguard Worker kVerifierDepsLock, 95*795d594fSAndroid Build Coastguard Worker kOatFileManagerLock, 96*795d594fSAndroid Build Coastguard Worker kTracingUniqueMethodsLock, 97*795d594fSAndroid Build Coastguard Worker kTracingStreamingLock, 98*795d594fSAndroid Build Coastguard Worker kJniLoadLibraryLock, 99*795d594fSAndroid Build Coastguard Worker kClassLoaderClassesLock, 100*795d594fSAndroid Build Coastguard Worker kDefaultMutexLevel, 101*795d594fSAndroid Build Coastguard Worker kDexCacheLock, 102*795d594fSAndroid Build Coastguard Worker kDexLock, 103*795d594fSAndroid Build Coastguard Worker kMarkSweepLargeObjectLock, 104*795d594fSAndroid Build Coastguard Worker kJdwpObjectRegistryLock, 105*795d594fSAndroid Build Coastguard Worker kModifyLdtLock, 106*795d594fSAndroid Build Coastguard Worker kAllocatedThreadIdsLock, 107*795d594fSAndroid Build Coastguard Worker kMonitorPoolLock, 108*795d594fSAndroid Build Coastguard Worker kClassLinkerClassesLock, // TODO rename. 109*795d594fSAndroid Build Coastguard Worker kSubtypeCheckLock, 110*795d594fSAndroid Build Coastguard Worker kBreakpointLock, 111*795d594fSAndroid Build Coastguard Worker kMonitorListLock, 112*795d594fSAndroid Build Coastguard Worker kThreadListLock, 113*795d594fSAndroid Build Coastguard Worker kAllocTrackerLock, 114*795d594fSAndroid Build Coastguard Worker kDeoptimizationLock, 115*795d594fSAndroid Build Coastguard Worker kProfilerLock, 116*795d594fSAndroid Build Coastguard Worker kJdwpShutdownLock, 117*795d594fSAndroid Build Coastguard Worker kJdwpEventListLock, 118*795d594fSAndroid Build Coastguard Worker kJdwpAttachLock, 119*795d594fSAndroid Build Coastguard Worker kJdwpStartLock, 120*795d594fSAndroid Build Coastguard Worker kRuntimeThreadPoolLock, 121*795d594fSAndroid Build Coastguard Worker kRuntimeShutdownLock, 122*795d594fSAndroid Build Coastguard Worker kTraceLock, 123*795d594fSAndroid Build Coastguard Worker kHeapBitmapLock, 124*795d594fSAndroid Build Coastguard Worker // This is a generic lock level for a lock meant to be gained after having a 125*795d594fSAndroid Build Coastguard Worker // monitor lock. 126*795d594fSAndroid Build Coastguard Worker kPostMonitorLock, 127*795d594fSAndroid Build Coastguard Worker kMonitorLock, 128*795d594fSAndroid Build Coastguard Worker // This is a generic lock level for a top-level lock meant to be gained after having the 129*795d594fSAndroid Build Coastguard Worker // mutator_lock_. 130*795d594fSAndroid Build Coastguard Worker kPostMutatorTopLockLevel, 131*795d594fSAndroid Build Coastguard Worker 132*795d594fSAndroid Build Coastguard Worker kMutatorLock, 133*795d594fSAndroid Build Coastguard Worker kInstrumentEntrypointsLock, 134*795d594fSAndroid Build Coastguard Worker // This is a generic lock level for a top-level lock meant to be gained after having the 135*795d594fSAndroid Build Coastguard Worker // UserCodeSuspensionLock. 136*795d594fSAndroid Build Coastguard Worker kPostUserCodeSuspensionTopLevelLock, 137*795d594fSAndroid Build Coastguard Worker kUserCodeSuspensionLock, 138*795d594fSAndroid Build Coastguard Worker kZygoteCreationLock, 139*795d594fSAndroid Build Coastguard Worker 140*795d594fSAndroid Build Coastguard Worker // The highest valid lock level. Use this for locks that should only be acquired with no 141*795d594fSAndroid Build Coastguard Worker // other locks held. Since this is the highest lock level we also allow it to be held even if the 142*795d594fSAndroid Build Coastguard Worker // runtime or current thread is not fully set-up yet (for example during thread attach). Note that 143*795d594fSAndroid Build Coastguard Worker // this lock also has special behavior around the mutator_lock_. Since the mutator_lock_ is not 144*795d594fSAndroid Build Coastguard Worker // really a 'real' lock we allow this to be locked when the mutator_lock_ is held exclusive. 145*795d594fSAndroid Build Coastguard Worker // Furthermore, the mutator_lock_ may not be acquired in any form when a lock of this level is 146*795d594fSAndroid Build Coastguard Worker // held. Since the mutator_lock_ being held strong means that all other threads are suspended this 147*795d594fSAndroid Build Coastguard Worker // will prevent deadlocks while still allowing this lock level to function as a "highest" level. 148*795d594fSAndroid Build Coastguard Worker kTopLockLevel, 149*795d594fSAndroid Build Coastguard Worker 150*795d594fSAndroid Build Coastguard Worker kLockLevelCount // Must come last. 151*795d594fSAndroid Build Coastguard Worker }; 152*795d594fSAndroid Build Coastguard Worker EXPORT std::ostream& operator<<(std::ostream& os, LockLevel rhs); 153*795d594fSAndroid Build Coastguard Worker 154*795d594fSAndroid Build Coastguard Worker // For StartNoThreadSuspension and EndNoThreadSuspension. 155*795d594fSAndroid Build Coastguard Worker class CAPABILITY("role") Role { 156*795d594fSAndroid Build Coastguard Worker public: Acquire()157*795d594fSAndroid Build Coastguard Worker void Acquire() ACQUIRE() {} Release()158*795d594fSAndroid Build Coastguard Worker void Release() RELEASE() {} 159*795d594fSAndroid Build Coastguard Worker const Role& operator!() const { return *this; } 160*795d594fSAndroid Build Coastguard Worker }; 161*795d594fSAndroid Build Coastguard Worker 162*795d594fSAndroid Build Coastguard Worker class Uninterruptible : public Role { 163*795d594fSAndroid Build Coastguard Worker }; 164*795d594fSAndroid Build Coastguard Worker 165*795d594fSAndroid Build Coastguard Worker // Global mutexes corresponding to the levels above. 166*795d594fSAndroid Build Coastguard Worker class EXPORT Locks { 167*795d594fSAndroid Build Coastguard Worker public: 168*795d594fSAndroid Build Coastguard Worker static void Init(); 169*795d594fSAndroid Build Coastguard Worker static void InitConditions() NO_THREAD_SAFETY_ANALYSIS; // Condition variables. 170*795d594fSAndroid Build Coastguard Worker 171*795d594fSAndroid Build Coastguard Worker // Destroying various lock types can emit errors that vary depending upon 172*795d594fSAndroid Build Coastguard Worker // whether the client (art::Runtime) is currently active. Allow the client 173*795d594fSAndroid Build Coastguard Worker // to set a callback that is used to check when it is acceptable to call 174*795d594fSAndroid Build Coastguard Worker // Abort. The default behavior is that the client *is not* able to call 175*795d594fSAndroid Build Coastguard Worker // Abort if no callback is established. 176*795d594fSAndroid Build Coastguard Worker using ClientCallback = bool(); 177*795d594fSAndroid Build Coastguard Worker static void SetClientCallback(ClientCallback* is_safe_to_call_abort_cb) NO_THREAD_SAFETY_ANALYSIS; 178*795d594fSAndroid Build Coastguard Worker // Checks for whether it is safe to call Abort() without using locks. 179*795d594fSAndroid Build Coastguard Worker static bool IsSafeToCallAbortRacy() NO_THREAD_SAFETY_ANALYSIS; 180*795d594fSAndroid Build Coastguard Worker 181*795d594fSAndroid Build Coastguard Worker // Add a mutex to expected_mutexes_on_weak_ref_access_. 182*795d594fSAndroid Build Coastguard Worker static void AddToExpectedMutexesOnWeakRefAccess(BaseMutex* mutex, bool need_lock = true); 183*795d594fSAndroid Build Coastguard Worker // Remove a mutex from expected_mutexes_on_weak_ref_access_. 184*795d594fSAndroid Build Coastguard Worker static void RemoveFromExpectedMutexesOnWeakRefAccess(BaseMutex* mutex, bool need_lock = true); 185*795d594fSAndroid Build Coastguard Worker // Check if the given mutex is in expected_mutexes_on_weak_ref_access_. 186*795d594fSAndroid Build Coastguard Worker static bool IsExpectedOnWeakRefAccess(BaseMutex* mutex); 187*795d594fSAndroid Build Coastguard Worker 188*795d594fSAndroid Build Coastguard Worker // Guards code that deals with user-code suspension. This mutex must be held when suspending or 189*795d594fSAndroid Build Coastguard Worker // resuming threads with SuspendReason::kForUserCode. It may be held by a suspended thread, but 190*795d594fSAndroid Build Coastguard Worker // only if the suspension is not due to SuspendReason::kForUserCode. 191*795d594fSAndroid Build Coastguard Worker static Mutex* user_code_suspension_lock_; 192*795d594fSAndroid Build Coastguard Worker 193*795d594fSAndroid Build Coastguard Worker // Guards allocation entrypoint instrumenting. 194*795d594fSAndroid Build Coastguard Worker static Mutex* instrument_entrypoints_lock_ ACQUIRED_AFTER(user_code_suspension_lock_); 195*795d594fSAndroid Build Coastguard Worker 196*795d594fSAndroid Build Coastguard Worker // A barrier is used to synchronize the GC/Debugger thread with mutator threads. When GC/Debugger 197*795d594fSAndroid Build Coastguard Worker // thread wants to suspend all mutator threads, it needs to wait for all mutator threads to pass 198*795d594fSAndroid Build Coastguard Worker // a barrier. Threads that are already suspended will get their barrier passed by the GC/Debugger 199*795d594fSAndroid Build Coastguard Worker // thread; threads in the runnable state will pass the barrier when they transit to the suspended 200*795d594fSAndroid Build Coastguard Worker // state. GC/Debugger thread will be woken up when all mutator threads are suspended. 201*795d594fSAndroid Build Coastguard Worker // 202*795d594fSAndroid Build Coastguard Worker // Thread suspension: 203*795d594fSAndroid Build Coastguard Worker // mutator thread | GC/Debugger 204*795d594fSAndroid Build Coastguard Worker // .. running .. | .. running .. 205*795d594fSAndroid Build Coastguard Worker // .. running .. | Request thread suspension by: 206*795d594fSAndroid Build Coastguard Worker // .. running .. | - acquiring thread_suspend_count_lock_ 207*795d594fSAndroid Build Coastguard Worker // .. running .. | - incrementing Thread::suspend_count_ on 208*795d594fSAndroid Build Coastguard Worker // .. running .. | all mutator threads 209*795d594fSAndroid Build Coastguard Worker // .. running .. | - releasing thread_suspend_count_lock_ 210*795d594fSAndroid Build Coastguard Worker // .. running .. | Block wait for all threads to pass a barrier 211*795d594fSAndroid Build Coastguard Worker // Poll Thread::suspend_count_ and enter full | .. blocked .. 212*795d594fSAndroid Build Coastguard Worker // suspend code. | .. blocked .. 213*795d594fSAndroid Build Coastguard Worker // Change state to kSuspended (pass the barrier) | Wake up when all threads pass the barrier 214*795d594fSAndroid Build Coastguard Worker // x: Acquire thread_suspend_count_lock_ | .. running .. 215*795d594fSAndroid Build Coastguard Worker // while Thread::suspend_count_ > 0 | .. running .. 216*795d594fSAndroid Build Coastguard Worker // - wait on Thread::resume_cond_ | .. running .. 217*795d594fSAndroid Build Coastguard Worker // (releases thread_suspend_count_lock_) | .. running .. 218*795d594fSAndroid Build Coastguard Worker // .. waiting .. | Request thread resumption by: 219*795d594fSAndroid Build Coastguard Worker // .. waiting .. | - acquiring thread_suspend_count_lock_ 220*795d594fSAndroid Build Coastguard Worker // .. waiting .. | - decrementing Thread::suspend_count_ on 221*795d594fSAndroid Build Coastguard Worker // .. waiting .. | all mutator threads 222*795d594fSAndroid Build Coastguard Worker // .. waiting .. | - notifying on Thread::resume_cond_ 223*795d594fSAndroid Build Coastguard Worker // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ 224*795d594fSAndroid Build Coastguard Worker // Release thread_suspend_count_lock_ | .. running .. 225*795d594fSAndroid Build Coastguard Worker // Change to kRunnable | .. running .. 226*795d594fSAndroid Build Coastguard Worker // - this uses a CAS operation to ensure the | .. running .. 227*795d594fSAndroid Build Coastguard Worker // suspend request flag isn't raised as the | .. running .. 228*795d594fSAndroid Build Coastguard Worker // state is changed | .. running .. 229*795d594fSAndroid Build Coastguard Worker // - if the CAS operation fails then goto x | .. running .. 230*795d594fSAndroid Build Coastguard Worker // .. running .. | .. running .. 231*795d594fSAndroid Build Coastguard Worker static MutatorMutex* mutator_lock_ ACQUIRED_AFTER(instrument_entrypoints_lock_); 232*795d594fSAndroid Build Coastguard Worker 233*795d594fSAndroid Build Coastguard Worker // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. 234*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); 235*795d594fSAndroid Build Coastguard Worker 236*795d594fSAndroid Build Coastguard Worker // Guards shutdown of the runtime. 237*795d594fSAndroid Build Coastguard Worker static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); 238*795d594fSAndroid Build Coastguard Worker 239*795d594fSAndroid Build Coastguard Worker // Runtime thread pool lock. 240*795d594fSAndroid Build Coastguard Worker static Mutex* runtime_thread_pool_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_); 241*795d594fSAndroid Build Coastguard Worker 242*795d594fSAndroid Build Coastguard Worker // Guards background profiler global state. 243*795d594fSAndroid Build Coastguard Worker static Mutex* profiler_lock_ ACQUIRED_AFTER(runtime_thread_pool_lock_); 244*795d594fSAndroid Build Coastguard Worker 245*795d594fSAndroid Build Coastguard Worker // Guards trace (ie traceview) requests. 246*795d594fSAndroid Build Coastguard Worker static Mutex* trace_lock_ ACQUIRED_AFTER(profiler_lock_); 247*795d594fSAndroid Build Coastguard Worker 248*795d594fSAndroid Build Coastguard Worker // Guards debugger recent allocation records. 249*795d594fSAndroid Build Coastguard Worker static Mutex* alloc_tracker_lock_ ACQUIRED_AFTER(trace_lock_); 250*795d594fSAndroid Build Coastguard Worker 251*795d594fSAndroid Build Coastguard Worker // Guards updates to instrumentation to ensure mutual exclusion of 252*795d594fSAndroid Build Coastguard Worker // events like deoptimization requests. 253*795d594fSAndroid Build Coastguard Worker // TODO: improve name, perhaps instrumentation_update_lock_. 254*795d594fSAndroid Build Coastguard Worker static Mutex* deoptimization_lock_ ACQUIRED_AFTER(alloc_tracker_lock_); 255*795d594fSAndroid Build Coastguard Worker 256*795d594fSAndroid Build Coastguard Worker // Guard the update of the SubtypeCheck data stores in each Class::status_ field. 257*795d594fSAndroid Build Coastguard Worker // This lock is used in SubtypeCheck methods which are the interface for 258*795d594fSAndroid Build Coastguard Worker // any SubtypeCheck-mutating methods. 259*795d594fSAndroid Build Coastguard Worker // In Class::IsSubClass, the lock is not required since it does not update the SubtypeCheck data. 260*795d594fSAndroid Build Coastguard Worker static Mutex* subtype_check_lock_ ACQUIRED_AFTER(deoptimization_lock_); 261*795d594fSAndroid Build Coastguard Worker 262*795d594fSAndroid Build Coastguard Worker // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads 263*795d594fSAndroid Build Coastguard Worker // attaching and detaching. 264*795d594fSAndroid Build Coastguard Worker static Mutex* thread_list_lock_ ACQUIRED_AFTER(subtype_check_lock_); 265*795d594fSAndroid Build Coastguard Worker 266*795d594fSAndroid Build Coastguard Worker // Signaled when threads terminate. Used to determine when all non-daemons have terminated. 267*795d594fSAndroid Build Coastguard Worker static ConditionVariable* thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_); 268*795d594fSAndroid Build Coastguard Worker 269*795d594fSAndroid Build Coastguard Worker // Guards maintaining loading library data structures. 270*795d594fSAndroid Build Coastguard Worker static Mutex* jni_libraries_lock_ ACQUIRED_AFTER(thread_list_lock_); 271*795d594fSAndroid Build Coastguard Worker 272*795d594fSAndroid Build Coastguard Worker // Guards breakpoints. 273*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* breakpoint_lock_ ACQUIRED_AFTER(jni_libraries_lock_); 274*795d594fSAndroid Build Coastguard Worker 275*795d594fSAndroid Build Coastguard Worker // Guards lists of classes within the class linker. 276*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_); 277*795d594fSAndroid Build Coastguard Worker 278*795d594fSAndroid Build Coastguard Worker // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code 279*795d594fSAndroid Build Coastguard Worker // doesn't try to hold a higher level Mutex. 280*795d594fSAndroid Build Coastguard Worker #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(art::Locks::classlinker_classes_lock_) 281*795d594fSAndroid Build Coastguard Worker 282*795d594fSAndroid Build Coastguard Worker static Mutex* allocated_monitor_ids_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); 283*795d594fSAndroid Build Coastguard Worker 284*795d594fSAndroid Build Coastguard Worker // Guard the allocation/deallocation of thread ids. 285*795d594fSAndroid Build Coastguard Worker static Mutex* allocated_thread_ids_lock_ ACQUIRED_AFTER(allocated_monitor_ids_lock_); 286*795d594fSAndroid Build Coastguard Worker 287*795d594fSAndroid Build Coastguard Worker // Guards modification of the LDT on x86. 288*795d594fSAndroid Build Coastguard Worker static Mutex* modify_ldt_lock_ ACQUIRED_AFTER(allocated_thread_ids_lock_); 289*795d594fSAndroid Build Coastguard Worker 290*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* dex_lock_ ACQUIRED_AFTER(modify_ldt_lock_); 291*795d594fSAndroid Build Coastguard Worker 292*795d594fSAndroid Build Coastguard Worker static Mutex* dex_cache_lock_ ACQUIRED_AFTER(dex_lock_); 293*795d594fSAndroid Build Coastguard Worker 294*795d594fSAndroid Build Coastguard Worker // Guards opened oat files in OatFileManager. 295*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* oat_file_manager_lock_ ACQUIRED_AFTER(dex_lock_); 296*795d594fSAndroid Build Coastguard Worker 297*795d594fSAndroid Build Coastguard Worker // Guards extra string entries for VerifierDeps. 298*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* verifier_deps_lock_ ACQUIRED_AFTER(oat_file_manager_lock_); 299*795d594fSAndroid Build Coastguard Worker 300*795d594fSAndroid Build Coastguard Worker // Guards dlopen_handles_ in DlOpenOatFile. 301*795d594fSAndroid Build Coastguard Worker static Mutex* host_dlopen_handles_lock_ ACQUIRED_AFTER(verifier_deps_lock_); 302*795d594fSAndroid Build Coastguard Worker 303*795d594fSAndroid Build Coastguard Worker // Guards intern table. 304*795d594fSAndroid Build Coastguard Worker static Mutex* intern_table_lock_ ACQUIRED_AFTER(host_dlopen_handles_lock_); 305*795d594fSAndroid Build Coastguard Worker 306*795d594fSAndroid Build Coastguard Worker // Guards reference processor. 307*795d594fSAndroid Build Coastguard Worker static Mutex* reference_processor_lock_ ACQUIRED_AFTER(intern_table_lock_); 308*795d594fSAndroid Build Coastguard Worker 309*795d594fSAndroid Build Coastguard Worker // Guards cleared references queue. 310*795d594fSAndroid Build Coastguard Worker static Mutex* reference_queue_cleared_references_lock_ ACQUIRED_AFTER(reference_processor_lock_); 311*795d594fSAndroid Build Coastguard Worker 312*795d594fSAndroid Build Coastguard Worker // Guards weak references queue. 313*795d594fSAndroid Build Coastguard Worker static Mutex* reference_queue_weak_references_lock_ ACQUIRED_AFTER(reference_queue_cleared_references_lock_); 314*795d594fSAndroid Build Coastguard Worker 315*795d594fSAndroid Build Coastguard Worker // Guards finalizer references queue. 316*795d594fSAndroid Build Coastguard Worker static Mutex* reference_queue_finalizer_references_lock_ ACQUIRED_AFTER(reference_queue_weak_references_lock_); 317*795d594fSAndroid Build Coastguard Worker 318*795d594fSAndroid Build Coastguard Worker // Guards phantom references queue. 319*795d594fSAndroid Build Coastguard Worker static Mutex* reference_queue_phantom_references_lock_ ACQUIRED_AFTER(reference_queue_finalizer_references_lock_); 320*795d594fSAndroid Build Coastguard Worker 321*795d594fSAndroid Build Coastguard Worker // Guards soft references queue. 322*795d594fSAndroid Build Coastguard Worker static Mutex* reference_queue_soft_references_lock_ ACQUIRED_AFTER(reference_queue_phantom_references_lock_); 323*795d594fSAndroid Build Coastguard Worker 324*795d594fSAndroid Build Coastguard Worker // Guard accesses to the JNI Global Reference table. 325*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* jni_globals_lock_ ACQUIRED_AFTER(reference_queue_soft_references_lock_); 326*795d594fSAndroid Build Coastguard Worker 327*795d594fSAndroid Build Coastguard Worker // Guard accesses to the JNI Weak Global Reference table. 328*795d594fSAndroid Build Coastguard Worker static Mutex* jni_weak_globals_lock_ ACQUIRED_AFTER(jni_globals_lock_); 329*795d594fSAndroid Build Coastguard Worker 330*795d594fSAndroid Build Coastguard Worker // Guard accesses to the JNI function table override. 331*795d594fSAndroid Build Coastguard Worker static Mutex* jni_function_table_lock_ ACQUIRED_AFTER(jni_weak_globals_lock_); 332*795d594fSAndroid Build Coastguard Worker 333*795d594fSAndroid Build Coastguard Worker // Guard accesses to the Thread::custom_tls_. We use this to allow the TLS of other threads to be 334*795d594fSAndroid Build Coastguard Worker // read (the reader must hold the ThreadListLock or have some other way of ensuring the thread 335*795d594fSAndroid Build Coastguard Worker // will not die in that case though). This is useful for (eg) the implementation of 336*795d594fSAndroid Build Coastguard Worker // GetThreadLocalStorage. 337*795d594fSAndroid Build Coastguard Worker static Mutex* custom_tls_lock_ ACQUIRED_AFTER(jni_function_table_lock_); 338*795d594fSAndroid Build Coastguard Worker 339*795d594fSAndroid Build Coastguard Worker // Guard access to JIT data structures mostly used by the JIT thread. 340*795d594fSAndroid Build Coastguard Worker static Mutex* jit_lock_ ACQUIRED_AFTER(custom_tls_lock_); 341*795d594fSAndroid Build Coastguard Worker 342*795d594fSAndroid Build Coastguard Worker // Guard access to any JIT data structure that mutators can also access. 343*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* jit_mutator_lock_ ACQUIRED_AFTER(custom_tls_lock_); 344*795d594fSAndroid Build Coastguard Worker 345*795d594fSAndroid Build Coastguard Worker // Guards Class Hierarchy Analysis (CHA). 346*795d594fSAndroid Build Coastguard Worker static Mutex* cha_lock_ ACQUIRED_AFTER(jit_lock_); 347*795d594fSAndroid Build Coastguard Worker 348*795d594fSAndroid Build Coastguard Worker // When declaring any Mutex add BOTTOM_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code 349*795d594fSAndroid Build Coastguard Worker // doesn't try to acquire a higher level Mutex. NB Due to the way the annotalysis works this 350*795d594fSAndroid Build Coastguard Worker // actually only encodes the mutex being below jni_function_table_lock_ although having 351*795d594fSAndroid Build Coastguard Worker // kGenericBottomLock level is lower than this. 352*795d594fSAndroid Build Coastguard Worker #define BOTTOM_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(art::Locks::cha_lock_) 353*795d594fSAndroid Build Coastguard Worker 354*795d594fSAndroid Build Coastguard Worker // Have an exclusive aborting thread. 355*795d594fSAndroid Build Coastguard Worker static Mutex* abort_lock_ ACQUIRED_AFTER(custom_tls_lock_); 356*795d594fSAndroid Build Coastguard Worker 357*795d594fSAndroid Build Coastguard Worker // Allow mutual exclusion when manipulating Thread::suspend_count_. 358*795d594fSAndroid Build Coastguard Worker // TODO: Does the trade-off of a per-thread lock make sense? 359*795d594fSAndroid Build Coastguard Worker static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); 360*795d594fSAndroid Build Coastguard Worker 361*795d594fSAndroid Build Coastguard Worker // One unexpected signal at a time lock. 362*795d594fSAndroid Build Coastguard Worker static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); 363*795d594fSAndroid Build Coastguard Worker 364*795d594fSAndroid Build Coastguard Worker // Guards the magic global variables used by native tools (e.g. libunwind). 365*795d594fSAndroid Build Coastguard Worker static Mutex* native_debug_interface_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); 366*795d594fSAndroid Build Coastguard Worker 367*795d594fSAndroid Build Coastguard Worker // Guards the data structures responsible for keeping track of the JNI 368*795d594fSAndroid Build Coastguard Worker // jmethodID/jfieldID <-> ArtMethod/ArtField mapping when using index-ids. 369*795d594fSAndroid Build Coastguard Worker static ReaderWriterMutex* jni_id_lock_ ACQUIRED_AFTER(native_debug_interface_lock_); 370*795d594fSAndroid Build Coastguard Worker 371*795d594fSAndroid Build Coastguard Worker // Have an exclusive logging thread. 372*795d594fSAndroid Build Coastguard Worker static Mutex* logging_lock_ ACQUIRED_AFTER(jni_id_lock_); 373*795d594fSAndroid Build Coastguard Worker 374*795d594fSAndroid Build Coastguard Worker // List of mutexes that we expect a thread may hold when accessing weak refs. This is used to 375*795d594fSAndroid Build Coastguard Worker // avoid a deadlock in the empty checkpoint while weak ref access is disabled (b/34964016). If we 376*795d594fSAndroid Build Coastguard Worker // encounter an unexpected mutex on accessing weak refs, 377*795d594fSAndroid Build Coastguard Worker // Thread::CheckEmptyCheckpointFromWeakRefAccess will detect it. 378*795d594fSAndroid Build Coastguard Worker static std::vector<BaseMutex*> expected_mutexes_on_weak_ref_access_; 379*795d594fSAndroid Build Coastguard Worker static Atomic<const BaseMutex*> expected_mutexes_on_weak_ref_access_guard_; 380*795d594fSAndroid Build Coastguard Worker class ScopedExpectedMutexesOnWeakRefAccessLock; 381*795d594fSAndroid Build Coastguard Worker }; 382*795d594fSAndroid Build Coastguard Worker 383*795d594fSAndroid Build Coastguard Worker class Roles { 384*795d594fSAndroid Build Coastguard Worker public: 385*795d594fSAndroid Build Coastguard Worker // Uninterruptible means that the thread may not become suspended. 386*795d594fSAndroid Build Coastguard Worker EXPORT static Uninterruptible uninterruptible_; 387*795d594fSAndroid Build Coastguard Worker }; 388*795d594fSAndroid Build Coastguard Worker 389*795d594fSAndroid Build Coastguard Worker } // namespace art 390*795d594fSAndroid Build Coastguard Worker 391*795d594fSAndroid Build Coastguard Worker #endif // ART_RUNTIME_BASE_LOCKS_H_ 392