blob: 202fa025a3d885066cc5501ecd8837ee7cb26f1e [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
17#ifndef ART_SRC_LOCKS_H_
18#define ART_SRC_LOCKS_H_
19
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 Chartier51033222013-06-26 13:37:27 -070039 kAllocSpaceLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080040 kDefaultMutexLevel,
Ian Rogers62d6c772013-02-27 08:32:07 -080041 kJdwpSerialLock,
Ian Rogers62d6c772013-02-27 08:32:07 -080042 kMarkSweepLargeObjectLock,
43 kPinTableLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080044 kLoadLibraryLock,
Jeff Hao9eef5b32013-04-11 17:28:08 -070045 kJdwpObjectRegistryLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080046 kClassLinkerClassesLock,
47 kBreakpointLock,
48 kThreadListLock,
49 kBreakpointInvokeLock,
Ian Rogers62d6c772013-02-27 08:32:07 -080050 kTraceLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080051 kJdwpEventListLock,
52 kJdwpAttachLock,
53 kJdwpStartLock,
Elliott Hughes0f827162013-02-26 12:12:58 -080054 kRuntimeShutdownLock,
55 kHeapBitmapLock,
56 kMonitorLock,
57 kMutatorLock,
58 kZygoteCreationLock,
59
60 kLockLevelCount // Must come last.
Ian Rogers81d425b2012-09-27 16:03:43 -070061};
62std::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
63
64// Global mutexes corresponding to the levels above.
65class Locks {
66 public:
67 static void Init();
68
69 // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block
70 // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds
71 // a share on the mutator_lock_. The garbage collector may also execute with shared access but
72 // at times requires exclusive access to the heap (not to be confused with the heap meta-data
73 // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks
74 // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_
75 // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition
76 // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on
77 // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector)
78 // chance to acquire the lock.
79 //
80 // Thread suspension:
81 // Shared users | Exclusive user
82 // (holding mutator lock and in kRunnable state) | .. running ..
83 // .. running .. | Request thread suspension by:
84 // .. running .. | - acquiring thread_suspend_count_lock_
85 // .. running .. | - incrementing Thread::suspend_count_ on
86 // .. running .. | all mutator threads
87 // .. running .. | - releasing thread_suspend_count_lock_
88 // .. running .. | Block trying to acquire exclusive mutator lock
89 // Poll Thread::suspend_count_ and enter full | .. blocked ..
90 // suspend code. | .. blocked ..
91 // Change state to kSuspended | .. blocked ..
92 // x: Release share on mutator_lock_ | Carry out exclusive access
93 // Acquire thread_suspend_count_lock_ | .. exclusive ..
94 // while Thread::suspend_count_ > 0 | .. exclusive ..
95 // - wait on Thread::resume_cond_ | .. exclusive ..
96 // (releases thread_suspend_count_lock_) | .. exclusive ..
97 // .. waiting .. | Release mutator_lock_
98 // .. waiting .. | Request thread resumption by:
99 // .. waiting .. | - acquiring thread_suspend_count_lock_
100 // .. waiting .. | - decrementing Thread::suspend_count_ on
101 // .. waiting .. | all mutator threads
102 // .. waiting .. | - notifying on Thread::resume_cond_
103 // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_
104 // Release thread_suspend_count_lock_ | .. running ..
105 // Acquire share on mutator_lock_ | .. running ..
106 // - This could block but the thread still | .. running ..
107 // has a state of kSuspended and so this | .. running ..
108 // isn't an issue. | .. running ..
109 // Acquire thread_suspend_count_lock_ | .. running ..
110 // - we poll here as we're transitioning into | .. running ..
111 // kRunnable and an individual thread suspend | .. running ..
112 // request (e.g for debugging) won't try | .. running ..
113 // to acquire the mutator lock (which would | .. running ..
114 // block as we hold the mutator lock). This | .. running ..
115 // poll ensures that if the suspender thought | .. running ..
116 // we were suspended by incrementing our | .. running ..
117 // Thread::suspend_count_ and then reading | .. running ..
118 // our state we go back to waiting on | .. running ..
119 // Thread::resume_cond_. | .. running ..
120 // can_go_runnable = Thread::suspend_count_ == 0 | .. running ..
121 // Release thread_suspend_count_lock_ | .. running ..
122 // if can_go_runnable | .. running ..
123 // Change state to kRunnable | .. running ..
124 // else | .. running ..
125 // Goto x | .. running ..
126 // .. running .. | .. running ..
127 static ReaderWriterMutex* mutator_lock_;
128
129 // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
130 static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
131
Ian Rogers120f1c72012-09-28 17:17:10 -0700132 // Guards shutdown of the runtime.
133 static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
134
Ian Rogers81d425b2012-09-27 16:03:43 -0700135 // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
136 // attaching and detaching.
Ian Rogers120f1c72012-09-28 17:17:10 -0700137 static Mutex* thread_list_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700138
jeffhao09bfc6a2012-12-11 18:11:43 -0800139 // Guards breakpoints and single-stepping.
140 static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
141
Ian Rogers62d6c772013-02-27 08:32:07 -0800142 // Guards trace requests.
143 static Mutex* trace_lock_ ACQUIRED_AFTER(breakpoint_lock_);
144
Ian Rogers81d425b2012-09-27 16:03:43 -0700145 // Guards lists of classes within the class linker.
Ian Rogers1bf8d4d2013-05-30 00:18:49 -0700146 static ReaderWriterMutex* classlinker_classes_lock_ ACQUIRED_AFTER(trace_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700147
148 // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
149 // doesn't try to hold a higher level Mutex.
150 #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(classlinker_classes_lock_)
151
152 // Have an exclusive aborting thread.
153 static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
154
155 // Allow mutual exclusion when manipulating Thread::suspend_count_.
156 // TODO: Does the trade-off of a per-thread lock make sense?
157 static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
158
159 // One unexpected signal at a time lock.
160 static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
161
162 // Have an exclusive logging thread.
163 static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
164};
165
166} // namespace art
167
168#endif // ART_SRC_LOCKS_H_