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