blob: 4343ab40edabd20912555a4edd37883a381d54c9 [file] [log] [blame]
Ian Rogers81d425b2012-09-27 16:03:43 -07001/*
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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_LOCKS_H_
18#define ART_RUNTIME_LOCKS_H_
Ian Rogers81d425b2012-09-27 16:03:43 -070019
20#include <ostream>
21
Elliott Hughes76160052012-12-12 16:31:20 -080022#include "base/macros.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070023
24namespace art {
25
26class LOCKABLE Mutex;
27class 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
34enum LockLevel {
35 kLoggingLock = 0,
Elliott Hughes0f827162013-02-26 12:12:58 -080036 kUnexpectedSignalLock,
37 kThreadSuspendCountLock,
38 kAbortLock,
Mathieu Chartier4b95e8f2013-07-15 16:32:50 -070039 kJdwpSocketLock,
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070040 kRosAllocGlobalLock,
41 kRosAllocBracketLock,
42 kRosAllocBulkFreeLock,
Mathieu Chartier51033222013-06-26 13:37:27 -070043 kAllocSpaceLock,
Vladimir Markoe13717e2013-11-20 12:44:55 +000044 kDexFileMethodInlinerLock,
45 kDexFileToMethodInlinerMapLock,
Mathieu Chartier958291c2013-08-27 18:14:55 -070046 kMarkSweepMarkStackLock,
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010047 kTransactionLogLock,
48 kInternTableLock,
Ian Rogers85ae5172014-03-05 23:40:59 -080049 kMonitorPoolLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080050 kDefaultMutexLevel,
Ian Rogers62d6c772013-02-27 08:32:07 -080051 kMarkSweepLargeObjectLock,
52 kPinTableLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080053 kLoadLibraryLock,
Jeff Hao9eef5b32013-04-11 17:28:08 -070054 kJdwpObjectRegistryLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080055 kClassLinkerClassesLock,
56 kBreakpointLock,
Mathieu Chartier0b6b18e2014-01-16 13:53:40 -080057 kMonitorLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080058 kThreadListLock,
59 kBreakpointInvokeLock,
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010060 kDeoptimizationLock,
Ian Rogers62d6c772013-02-27 08:32:07 -080061 kTraceLock,
Dave Allison0aded082013-11-07 13:15:11 -080062 kProfilerLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080063 kJdwpEventListLock,
64 kJdwpAttachLock,
65 kJdwpStartLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080066 kRuntimeShutdownLock,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070067 kHeapBitmapLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080068 kMutatorLock,
69 kZygoteCreationLock,
70
Brian Carlstrom7934ac22013-07-26 10:54:15 -070071 kLockLevelCount // Must come last.
Ian Rogers81d425b2012-09-27 16:03:43 -070072};
73std::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
74
75// Global mutexes corresponding to the levels above.
76class Locks {
77 public:
78 static void Init();
79
80 // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block
81 // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds
82 // a share on the mutator_lock_. The garbage collector may also execute with shared access but
83 // at times requires exclusive access to the heap (not to be confused with the heap meta-data
84 // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks
85 // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_
86 // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition
87 // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on
88 // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector)
89 // chance to acquire the lock.
90 //
91 // Thread suspension:
92 // Shared users | Exclusive user
93 // (holding mutator lock and in kRunnable state) | .. running ..
94 // .. running .. | Request thread suspension by:
95 // .. running .. | - acquiring thread_suspend_count_lock_
96 // .. running .. | - incrementing Thread::suspend_count_ on
97 // .. running .. | all mutator threads
98 // .. running .. | - releasing thread_suspend_count_lock_
99 // .. running .. | Block trying to acquire exclusive mutator lock
100 // Poll Thread::suspend_count_ and enter full | .. blocked ..
101 // suspend code. | .. blocked ..
102 // Change state to kSuspended | .. blocked ..
103 // x: Release share on mutator_lock_ | Carry out exclusive access
104 // Acquire thread_suspend_count_lock_ | .. exclusive ..
105 // while Thread::suspend_count_ > 0 | .. exclusive ..
106 // - wait on Thread::resume_cond_ | .. exclusive ..
107 // (releases thread_suspend_count_lock_) | .. exclusive ..
108 // .. waiting .. | Release mutator_lock_
109 // .. waiting .. | Request thread resumption by:
110 // .. waiting .. | - acquiring thread_suspend_count_lock_
111 // .. waiting .. | - decrementing Thread::suspend_count_ on
112 // .. waiting .. | all mutator threads
113 // .. waiting .. | - notifying on Thread::resume_cond_
114 // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_
115 // Release thread_suspend_count_lock_ | .. running ..
116 // Acquire share on mutator_lock_ | .. running ..
117 // - This could block but the thread still | .. running ..
118 // has a state of kSuspended and so this | .. running ..
119 // isn't an issue. | .. running ..
120 // Acquire thread_suspend_count_lock_ | .. running ..
121 // - we poll here as we're transitioning into | .. running ..
122 // kRunnable and an individual thread suspend | .. running ..
123 // request (e.g for debugging) won't try | .. running ..
124 // to acquire the mutator lock (which would | .. running ..
125 // block as we hold the mutator lock). This | .. running ..
126 // poll ensures that if the suspender thought | .. running ..
127 // we were suspended by incrementing our | .. running ..
128 // Thread::suspend_count_ and then reading | .. running ..
129 // our state we go back to waiting on | .. running ..
130 // Thread::resume_cond_. | .. running ..
131 // can_go_runnable = Thread::suspend_count_ == 0 | .. running ..
132 // Release thread_suspend_count_lock_ | .. running ..
133 // if can_go_runnable | .. running ..
134 // Change state to kRunnable | .. running ..
135 // else | .. running ..
136 // Goto x | .. running ..
137 // .. running .. | .. running ..
138 static ReaderWriterMutex* mutator_lock_;
139
140 // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
141 static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
142
Ian Rogers120f1c72012-09-28 17:17:10 -0700143 // Guards shutdown of the runtime.
144 static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
145
Ian Rogers81d425b2012-09-27 16:03:43 -0700146 // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
147 // attaching and detaching.
Ian Rogers120f1c72012-09-28 17:17:10 -0700148 static Mutex* thread_list_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700149
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100150 // Guards breakpoints.
jeffhao09bfc6a2012-12-11 18:11:43 -0800151 static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
152
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100153 // Guards deoptimization requests.
154 static Mutex* deoptimization_lock_ ACQUIRED_AFTER(breakpoint_lock_);
155
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 // Guards trace requests.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100157 static Mutex* trace_lock_ ACQUIRED_AFTER(deoptimization_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800158
Dave Allison0aded082013-11-07 13:15:11 -0800159 // Guards profile objects.
160 static Mutex* profiler_lock_ ACQUIRED_AFTER(trace_lock_);
161
Ian Rogers81d425b2012-09-27 16:03:43 -0700162 // Guards lists of classes within the class linker.
Dave Allison0aded082013-11-07 13:15:11 -0800163 static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(profiler_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700164
165 // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
166 // doesn't try to hold a higher level Mutex.
167 #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(classlinker_classes_lock_)
168
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100169 // Guards intern table.
170 static Mutex* intern_table_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
171
Ian Rogers81d425b2012-09-27 16:03:43 -0700172 // Have an exclusive aborting thread.
173 static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
174
175 // Allow mutual exclusion when manipulating Thread::suspend_count_.
176 // TODO: Does the trade-off of a per-thread lock make sense?
177 static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
178
179 // One unexpected signal at a time lock.
180 static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
181
182 // Have an exclusive logging thread.
183 static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
184};
185
186} // namespace art
187
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700188#endif // ART_RUNTIME_LOCKS_H_