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