blob: 9a59dca6a21a2257984aadc65715be0b793b9b6d [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
22#include "macros.h"
23
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,
36 kUnexpectedSignalLock = 1,
37 kThreadSuspendCountLock = 2,
38 kAbortLock = 3,
39 kDefaultMutexLevel = 4,
jeffhaoa77f0f62012-12-05 17:19:31 -080040 kAllocSpaceLock = 5,
41 kLoadLibraryLock = 6,
42 kClassLinkerClassesLock = 7,
jeffhao09bfc6a2012-12-11 18:11:43 -080043 kBreakpointLock = 8,
44 kThreadListLock = 9,
45 kJdwpEventListLock = 10,
46 kJdwpAttachLock = 11,
47 kJdwpStartLock = 12,
48 kJdwpSerialLock = 13,
49 kRuntimeShutdownLock = 14,
50 kHeapBitmapLock = 15,
51 kMonitorLock = 16,
52 kMutatorLock = 17,
53 kZygoteCreationLock = 18,
jeffhao0dfbb7e2012-11-28 15:26:03 -080054 kMaxMutexLevel = kZygoteCreationLock,
Ian Rogers81d425b2012-09-27 16:03:43 -070055};
56std::ostream& operator<<(std::ostream& os, const LockLevel& rhs);
57
58// Global mutexes corresponding to the levels above.
59class Locks {
60 public:
61 static void Init();
62
63 // The mutator_lock_ is used to allow mutators to execute in a shared (reader) mode or to block
64 // mutators by having an exclusive (writer) owner. In normal execution each mutator thread holds
65 // a share on the mutator_lock_. The garbage collector may also execute with shared access but
66 // at times requires exclusive access to the heap (not to be confused with the heap meta-data
67 // guarded by the heap_lock_ below). When the garbage collector requires exclusive access it asks
68 // the mutators to suspend themselves which also involves usage of the thread_suspend_count_lock_
69 // to cover weaknesses in using ReaderWriterMutexes with ConditionVariables. We use a condition
70 // variable to wait upon in the suspension logic as releasing and then re-acquiring a share on
71 // the mutator lock doesn't necessarily allow the exclusive user (e.g the garbage collector)
72 // chance to acquire the lock.
73 //
74 // Thread suspension:
75 // Shared users | Exclusive user
76 // (holding mutator lock and in kRunnable state) | .. running ..
77 // .. running .. | Request thread suspension by:
78 // .. running .. | - acquiring thread_suspend_count_lock_
79 // .. running .. | - incrementing Thread::suspend_count_ on
80 // .. running .. | all mutator threads
81 // .. running .. | - releasing thread_suspend_count_lock_
82 // .. running .. | Block trying to acquire exclusive mutator lock
83 // Poll Thread::suspend_count_ and enter full | .. blocked ..
84 // suspend code. | .. blocked ..
85 // Change state to kSuspended | .. blocked ..
86 // x: Release share on mutator_lock_ | Carry out exclusive access
87 // Acquire thread_suspend_count_lock_ | .. exclusive ..
88 // while Thread::suspend_count_ > 0 | .. exclusive ..
89 // - wait on Thread::resume_cond_ | .. exclusive ..
90 // (releases thread_suspend_count_lock_) | .. exclusive ..
91 // .. waiting .. | Release mutator_lock_
92 // .. waiting .. | Request thread resumption by:
93 // .. waiting .. | - acquiring thread_suspend_count_lock_
94 // .. waiting .. | - decrementing Thread::suspend_count_ on
95 // .. waiting .. | all mutator threads
96 // .. waiting .. | - notifying on Thread::resume_cond_
97 // - re-acquire thread_suspend_count_lock_ | - releasing thread_suspend_count_lock_
98 // Release thread_suspend_count_lock_ | .. running ..
99 // Acquire share on mutator_lock_ | .. running ..
100 // - This could block but the thread still | .. running ..
101 // has a state of kSuspended and so this | .. running ..
102 // isn't an issue. | .. running ..
103 // Acquire thread_suspend_count_lock_ | .. running ..
104 // - we poll here as we're transitioning into | .. running ..
105 // kRunnable and an individual thread suspend | .. running ..
106 // request (e.g for debugging) won't try | .. running ..
107 // to acquire the mutator lock (which would | .. running ..
108 // block as we hold the mutator lock). This | .. running ..
109 // poll ensures that if the suspender thought | .. running ..
110 // we were suspended by incrementing our | .. running ..
111 // Thread::suspend_count_ and then reading | .. running ..
112 // our state we go back to waiting on | .. running ..
113 // Thread::resume_cond_. | .. running ..
114 // can_go_runnable = Thread::suspend_count_ == 0 | .. running ..
115 // Release thread_suspend_count_lock_ | .. running ..
116 // if can_go_runnable | .. running ..
117 // Change state to kRunnable | .. running ..
118 // else | .. running ..
119 // Goto x | .. running ..
120 // .. running .. | .. running ..
121 static ReaderWriterMutex* mutator_lock_;
122
123 // Allow reader-writer mutual exclusion on the mark and live bitmaps of the heap.
124 static ReaderWriterMutex* heap_bitmap_lock_ ACQUIRED_AFTER(mutator_lock_);
125
Ian Rogers120f1c72012-09-28 17:17:10 -0700126 // Guards shutdown of the runtime.
127 static Mutex* runtime_shutdown_lock_ ACQUIRED_AFTER(heap_bitmap_lock_);
128
Ian Rogers81d425b2012-09-27 16:03:43 -0700129 // The thread_list_lock_ guards ThreadList::list_. It is also commonly held to stop threads
130 // attaching and detaching.
Ian Rogers120f1c72012-09-28 17:17:10 -0700131 static Mutex* thread_list_lock_ ACQUIRED_AFTER(runtime_shutdown_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700132
jeffhao09bfc6a2012-12-11 18:11:43 -0800133 // Guards breakpoints and single-stepping.
134 static Mutex* breakpoint_lock_ ACQUIRED_AFTER(thread_list_lock_);
135
Ian Rogers81d425b2012-09-27 16:03:43 -0700136 // Guards lists of classes within the class linker.
jeffhao09bfc6a2012-12-11 18:11:43 -0800137 static Mutex* classlinker_classes_lock_ ACQUIRED_AFTER(breakpoint_lock_);
Ian Rogers81d425b2012-09-27 16:03:43 -0700138
139 // When declaring any Mutex add DEFAULT_MUTEX_ACQUIRED_AFTER to use annotalysis to check the code
140 // doesn't try to hold a higher level Mutex.
141 #define DEFAULT_MUTEX_ACQUIRED_AFTER ACQUIRED_AFTER(classlinker_classes_lock_)
142
143 // Have an exclusive aborting thread.
144 static Mutex* abort_lock_ ACQUIRED_AFTER(classlinker_classes_lock_);
145
146 // Allow mutual exclusion when manipulating Thread::suspend_count_.
147 // TODO: Does the trade-off of a per-thread lock make sense?
148 static Mutex* thread_suspend_count_lock_ ACQUIRED_AFTER(abort_lock_);
149
150 // One unexpected signal at a time lock.
151 static Mutex* unexpected_signal_lock_ ACQUIRED_AFTER(thread_suspend_count_lock_);
152
153 // Have an exclusive logging thread.
154 static Mutex* logging_lock_ ACQUIRED_AFTER(unexpected_signal_lock_);
155};
156
157} // namespace art
158
159#endif // ART_SRC_LOCKS_H_