blob: 341319c136805266fd05b030c6bfd00028596bf6 [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,
Elliott Hughes0f827162013-02-26 12:12:58 -080047 kDefaultMutexLevel,
Ian Rogers62d6c772013-02-27 08:32:07 -080048 kMarkSweepLargeObjectLock,
49 kPinTableLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080050 kLoadLibraryLock,
Jeff Hao9eef5b32013-04-11 17:28:08 -070051 kJdwpObjectRegistryLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080052 kClassLinkerClassesLock,
53 kBreakpointLock,
54 kThreadListLock,
55 kBreakpointInvokeLock,
Ian Rogers62d6c772013-02-27 08:32:07 -080056 kTraceLock,
Dave Allison0aded082013-11-07 13:15:11 -080057 kProfilerLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080058 kJdwpEventListLock,
59 kJdwpAttachLock,
60 kJdwpStartLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080061 kRuntimeShutdownLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080062 kMonitorLock,
Mathieu Chartierad2541a2013-10-25 10:05:23 -070063 kHeapBitmapLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080064 kMutatorLock,
65 kZygoteCreationLock,
66
Brian Carlstrom7934ac22013-07-26 10:54:15 -070067 kLockLevelCount // Must come last.
Ian Rogers81d425b2012-09-27 16:03:43 -070068};
69std::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
70
71// Global mutexes corresponding to the levels above.
72class Locks {
73 public:
74 static void Init();
75
76 // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block
77 // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds
78 // a share on the mutator_lock_. The garbage collector may also execute with shared access but
79 // at times requires exclusive access to the heap (not to be confused with the heap meta-data
80 // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks
81 // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_
82 // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition
83 // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on
84 // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector)
85 // chance to acquire the lock.
86 //
87 // Thread suspension:
88 // Shared users | Exclusive user
89 // (holding mutator lock and in kRunnable state) | .. running ..
90 // .. running .. | Request thread suspension by:
91 // .. running .. | - acquiring thread_suspend_count_lock_
92 // .. running .. | - incrementing Thread::suspend_count_ on
93 // .. running .. | all mutator threads
94 // .. running .. | - releasing thread_suspend_count_lock_
95 // .. running .. | Block trying to acquire exclusive mutator lock
96 // Poll Thread::suspend_count_ and enter full | .. blocked ..
97 // suspend code. | .. blocked ..
98 // Change state to kSuspended | .. blocked ..
99 // x: Release share on mutator_lock_ | Carry out exclusive access
100 // Acquire thread_suspend_count_lock_ | .. exclusive ..
101 // while Thread::suspend_count_ > 0 | .. exclusive ..
102 // - wait on Thread::resume_cond_ | .. exclusive ..
103 // (releases thread_suspend_count_lock_) | .. exclusive ..
104 // .. waiting .. | Release mutator_lock_
105 // .. waiting .. | Request thread resumption by:
106 // .. waiting .. | - acquiring thread_suspend_count_lock_
107 // .. waiting .. | - decrementing Thread::suspend_count_ on
108 // .. waiting .. | all mutator threads
109 // .. waiting .. | - notifying on Thread::resume_cond_
110 // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_
111 // Release thread_suspend_count_lock_ | .. running ..
112 // Acquire share on mutator_lock_ | .. running ..
113 // - This could block but the thread still | .. running ..
114 // has a state of kSuspended and so this | .. running ..
115 // isn't an issue. | .. running ..
116 // Acquire thread_suspend_count_lock_ | .. running ..
117 // - we poll here as we're transitioning into | .. running ..
118 // kRunnable and an individual thread suspend | .. running ..
119 // request (e.g for debugging) won't try | .. running ..
120 // to acquire the mutator lock (which would | .. running ..
121 // block as we hold the mutator lock). This | .. running ..
122 // poll ensures that if the suspender thought | .. running ..
123 // we were suspended by incrementing our | .. running ..
124 // Thread::suspend_count_ and then reading | .. running ..
125 // our state we go back to waiting on | .. running ..
126 // Thread::resume_cond_. | .. running ..
127 // can_go_runnable = Thread::suspend_count_ == 0 | .. running ..
128 // Release thread_suspend_count_lock_ | .. running ..
129 // if can_go_runnable | .. running ..
130 // Change state to kRunnable | .. running ..
131 // else | .. running ..
132 // Goto x | .. running ..
133 // .. running .. | .. running ..
134 static ReaderWriterMutex* mutator_lock_;
135
136 // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
137 static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
138
Ian Rogers120f1c72012-09-28 17:17:10 -0700139 // Guards shutdown of the runtime.
140 static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
141
Ian Rogers81d425b2012-09-27 16:03:43 -0700142 // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
143 // attaching and detaching.
Ian Rogers120f1c72012-09-28 17:17:10 -0700144 static Mutex* thread_list_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700145
jeffhao09bfc6a2012-12-11 18:11:43 -0800146 // Guards breakpoints and single-stepping.
147 static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
148
Ian Rogers62d6c772013-02-27 08:32:07 -0800149 // Guards trace requests.
150 static Mutex* trace_lock_ ACQUIRED_AFTER(breakpoint_lock_);
151
Dave Allison0aded082013-11-07 13:15:11 -0800152 // Guards profile objects.
153 static Mutex* profiler_lock_ ACQUIRED_AFTER(trace_lock_);
154
Ian Rogers81d425b2012-09-27 16:03:43 -0700155 // Guards lists of classes within the class linker.
Dave Allison0aded082013-11-07 13:15:11 -0800156 static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(profiler_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700157
158 // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
159 // doesn't try to hold a higher level Mutex.
160 #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(classlinker_classes_lock_)
161
162 // Have an exclusive aborting thread.
163 static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
164
165 // Allow mutual exclusion when manipulating Thread::suspend_count_.
166 // TODO: Does the trade-off of a per-thread lock make sense?
167 static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
168
169 // One unexpected signal at a time lock.
170 static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
171
172 // Have an exclusive logging thread.
173 static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
174};
175
176} // namespace art
177
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700178#endif // ART_RUNTIME_LOCKS_H_