blob: 2f9810d05bae0d7d5a84eb50822ab3bbae6d3891 [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,
Elliott Hughes0f827162013-02-26 12:12:58 -080049 kDefaultMutexLevel,
Ian Rogers62d6c772013-02-27 08:32:07 -080050 kMarkSweepLargeObjectLock,
51 kPinTableLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080052 kLoadLibraryLock,
Jeff Hao9eef5b32013-04-11 17:28:08 -070053 kJdwpObjectRegistryLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080054 kClassLinkerClassesLock,
55 kBreakpointLock,
Mathieu Chartier0b6b18e2014-01-16 13:53:40 -080056 kMonitorLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080057 kThreadListLock,
58 kBreakpointInvokeLock,
Sebastien Hertz138dbfc2013-12-04 18:15:25 +010059 kDeoptimizationLock,
Ian Rogers62d6c772013-02-27 08:32:07 -080060 kTraceLock,
Dave Allison0aded082013-11-07 13:15:11 -080061 kProfilerLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080062 kJdwpEventListLock,
63 kJdwpAttachLock,
64 kJdwpStartLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080065 kRuntimeShutdownLock,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070066 kHeapBitmapLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080067 kMutatorLock,
68 kZygoteCreationLock,
69
Brian Carlstrom7934ac22013-07-26 10:54:15 -070070 kLockLevelCount // Must come last.
Ian Rogers81d425b2012-09-27 16:03:43 -070071};
72std::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
73
74// Global mutexes corresponding to the levels above.
75class 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 Rogers120f1c72012-09-28 17:17:10 -0700142 // Guards shutdown of the runtime.
143 static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
144
Ian Rogers81d425b2012-09-27 16:03:43 -0700145 // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
146 // attaching and detaching.
Ian Rogers120f1c72012-09-28 17:17:10 -0700147 static Mutex* thread_list_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700148
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100149 // Guards breakpoints.
jeffhao09bfc6a2012-12-11 18:11:43 -0800150 static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
151
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100152 // Guards deoptimization requests.
153 static Mutex* deoptimization_lock_ ACQUIRED_AFTER(breakpoint_lock_);
154
Ian Rogers62d6c772013-02-27 08:32:07 -0800155 // Guards trace requests.
Sebastien Hertz138dbfc2013-12-04 18:15:25 +0100156 static Mutex* trace_lock_ ACQUIRED_AFTER(deoptimization_lock_);
Ian Rogers62d6c772013-02-27 08:32:07 -0800157
Dave Allison0aded082013-11-07 13:15:11 -0800158 // Guards profile objects.
159 static Mutex* profiler_lock_ ACQUIRED_AFTER(trace_lock_);
160
Ian Rogers81d425b2012-09-27 16:03:43 -0700161 // Guards lists of classes within the class linker.
Dave Allison0aded082013-11-07 13:15:11 -0800162 static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(profiler_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700163
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 Hertzd2fe10a2014-01-15 10:20:56 +0100168 // Guards intern table.
169 static Mutex* intern_table_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
170
Ian Rogers81d425b2012-09-27 16:03:43 -0700171 // 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 Carlstromfc0e3212013-07-17 14:40:12 -0700187#endif // ART_RUNTIME_LOCKS_H_