Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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_LOCKS_H_ |
| 18 | #define ART_RUNTIME_LOCKS_H_ |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 19 | |
| 20 | #include <ostream> |
| 21 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | #include "base/macros.h" |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | class LOCKABLE Mutex; |
| 27 | class LOCKABLE ReaderWriterMutex; |
| 28 | |
| 29 | // LockLevel is used to impose a lock hierarchy [1] where acquisition of a Mutex at a higher or |
| 30 | // equal level to a lock a thread holds is invalid. The lock hierarchy achieves a cycle free |
| 31 | // partial ordering and thereby cause deadlock situations to fail checks. |
| 32 | // |
| 33 | // [1] http://www.drdobbs.com/parallel/use-lock-hierarchies-to-avoid-deadlock/204801163 |
| 34 | enum LockLevel { |
| 35 | kLoggingLock = 0, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 36 | kUnexpectedSignalLock, |
| 37 | kThreadSuspendCountLock, |
| 38 | kAbortLock, |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 39 | kJdwpSocketLock, |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 40 | kRosAllocGlobalLock, |
| 41 | kRosAllocBracketLock, |
| 42 | kRosAllocBulkFreeLock, |
Mathieu Chartier | 5103322 | 2013-06-26 13:37:27 -0700 | [diff] [blame] | 43 | kAllocSpaceLock, |
Vladimir Marko | e13717e | 2013-11-20 12:44:55 +0000 | [diff] [blame] | 44 | kDexFileMethodInlinerLock, |
| 45 | kDexFileToMethodInlinerMapLock, |
Mathieu Chartier | 958291c | 2013-08-27 18:14:55 -0700 | [diff] [blame] | 46 | kMarkSweepMarkStackLock, |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 47 | kTransactionLogLock, |
| 48 | kInternTableLock, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 49 | kDefaultMutexLevel, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 50 | kMarkSweepLargeObjectLock, |
| 51 | kPinTableLock, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 52 | kLoadLibraryLock, |
Jeff Hao | 9eef5b3 | 2013-04-11 17:28:08 -0700 | [diff] [blame] | 53 | kJdwpObjectRegistryLock, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 54 | kClassLinkerClassesLock, |
| 55 | kBreakpointLock, |
Mathieu Chartier | 0b6b18e | 2014-01-16 13:53:40 -0800 | [diff] [blame] | 56 | kMonitorLock, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 57 | kThreadListLock, |
| 58 | kBreakpointInvokeLock, |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 59 | kDeoptimizationLock, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 60 | kTraceLock, |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 61 | kProfilerLock, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 62 | kJdwpEventListLock, |
| 63 | kJdwpAttachLock, |
| 64 | kJdwpStartLock, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 65 | kRuntimeShutdownLock, |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 66 | kHeapBitmapLock, |
Elliott Hughes | 0f82716 | 2013-02-26 12:12:58 -0800 | [diff] [blame] | 67 | kMutatorLock, |
| 68 | kZygoteCreationLock, |
| 69 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 70 | kLockLevelCount // Must come last. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 71 | }; |
| 72 | std::ostream& operator<<(std::ostream& os, const LockLevel& rhs); |
| 73 | |
| 74 | // Global mutexes corresponding to the levels above. |
| 75 | class Locks { |
| 76 | public: |
| 77 | static void Init(); |
| 78 | |
| 79 | // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block |
| 80 | // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds |
| 81 | // a share on the mutator_lock_. The garbage collector may also execute with shared access but |
| 82 | // at times requires exclusive access to the heap (not to be confused with the heap meta-data |
| 83 | // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks |
| 84 | // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_ |
| 85 | // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition |
| 86 | // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on |
| 87 | // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector) |
| 88 | // chance to acquire the lock. |
| 89 | // |
| 90 | // Thread suspension: |
| 91 | // Shared users | Exclusive user |
| 92 | // (holding mutator lock and in kRunnable state) | .. running .. |
| 93 | // .. running .. | Request thread suspension by: |
| 94 | // .. running .. | - acquiring thread_suspend_count_lock_ |
| 95 | // .. running .. | - incrementing Thread::suspend_count_ on |
| 96 | // .. running .. | all mutator threads |
| 97 | // .. running .. | - releasing thread_suspend_count_lock_ |
| 98 | // .. running .. | Block trying to acquire exclusive mutator lock |
| 99 | // Poll Thread::suspend_count_ and enter full | .. blocked .. |
| 100 | // suspend code. | .. blocked .. |
| 101 | // Change state to kSuspended | .. blocked .. |
| 102 | // x: Release share on mutator_lock_ | Carry out exclusive access |
| 103 | // Acquire thread_suspend_count_lock_ | .. exclusive .. |
| 104 | // while Thread::suspend_count_ > 0 | .. exclusive .. |
| 105 | // - wait on Thread::resume_cond_ | .. exclusive .. |
| 106 | // (releases thread_suspend_count_lock_) | .. exclusive .. |
| 107 | // .. waiting .. | Release mutator_lock_ |
| 108 | // .. waiting .. | Request thread resumption by: |
| 109 | // .. waiting .. | - acquiring thread_suspend_count_lock_ |
| 110 | // .. waiting .. | - decrementing Thread::suspend_count_ on |
| 111 | // .. waiting .. | all mutator threads |
| 112 | // .. waiting .. | - notifying on Thread::resume_cond_ |
| 113 | // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_ |
| 114 | // Release thread_suspend_count_lock_ | .. running .. |
| 115 | // Acquire share on mutator_lock_ | .. running .. |
| 116 | // - This could block but the thread still | .. running .. |
| 117 | // has a state of kSuspended and so this | .. running .. |
| 118 | // isn't an issue. | .. running .. |
| 119 | // Acquire thread_suspend_count_lock_ | .. running .. |
| 120 | // - we poll here as we're transitioning into | .. running .. |
| 121 | // kRunnable and an individual thread suspend | .. running .. |
| 122 | // request (e.g for debugging) won't try | .. running .. |
| 123 | // to acquire the mutator lock (which would | .. running .. |
| 124 | // block as we hold the mutator lock). This | .. running .. |
| 125 | // poll ensures that if the suspender thought | .. running .. |
| 126 | // we were suspended by incrementing our | .. running .. |
| 127 | // Thread::suspend_count_ and then reading | .. running .. |
| 128 | // our state we go back to waiting on | .. running .. |
| 129 | // Thread::resume_cond_. | .. running .. |
| 130 | // can_go_runnable = Thread::suspend_count_ == 0 | .. running .. |
| 131 | // Release thread_suspend_count_lock_ | .. running .. |
| 132 | // if can_go_runnable | .. running .. |
| 133 | // Change state to kRunnable | .. running .. |
| 134 | // else | .. running .. |
| 135 | // Goto x | .. running .. |
| 136 | // .. running .. | .. running .. |
| 137 | static ReaderWriterMutex* mutator_lock_; |
| 138 | |
| 139 | // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap. |
| 140 | static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_); |
| 141 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 142 | // Guards shutdown of the runtime. |
| 143 | static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_); |
| 144 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 145 | // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads |
| 146 | // attaching and detaching. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 147 | static Mutex* thread_list_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 148 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 149 | // Guards breakpoints. |
jeffhao | 09bfc6a | 2012-12-11 18:11:43 -0800 | [diff] [blame] | 150 | static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_); |
| 151 | |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 152 | // Guards deoptimization requests. |
| 153 | static Mutex* deoptimization_lock_ ACQUIRED_AFTER(breakpoint_lock_); |
| 154 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 155 | // Guards trace requests. |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 156 | static Mutex* trace_lock_ ACQUIRED_AFTER(deoptimization_lock_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 157 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 158 | // Guards profile objects. |
| 159 | static Mutex* profiler_lock_ ACQUIRED_AFTER(trace_lock_); |
| 160 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 161 | // Guards lists of classes within the class linker. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 162 | static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(profiler_lock_); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 163 | |
| 164 | // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code |
| 165 | // doesn't try to hold a higher level Mutex. |
| 166 | #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(classlinker_classes_lock_) |
| 167 | |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 168 | // Guards intern table. |
| 169 | static Mutex* intern_table_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); |
| 170 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 171 | // Have an exclusive aborting thread. |
| 172 | static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_); |
| 173 | |
| 174 | // Allow mutual exclusion when manipulating Thread::suspend_count_. |
| 175 | // TODO: Does the trade-off of a per-thread lock make sense? |
| 176 | static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_); |
| 177 | |
| 178 | // One unexpected signal at a time lock. |
| 179 | static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_); |
| 180 | |
| 181 | // Have an exclusive logging thread. |
| 182 | static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_); |
| 183 | }; |
| 184 | |
| 185 | } // namespace art |
| 186 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 187 | #endif // ART_RUNTIME_LOCKS_H_ |