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