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