2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ART_RUNTIME_MONITOR_H_
18 #define ART_RUNTIME_MONITOR_H_
28 #include "base/mutex.h"
29 #include "object_callbacks.h"
30 #include "read_barrier.h"
31 #include "thread_state.h"
36 template<class T> class Handle;
39 typedef uint32_t MonitorId;
48 // The default number of spins that are done before thread suspension is used to forcibly inflate
49 // a lock word. See Runtime::max_spins_before_thin_lock_inflation_.
50 constexpr static size_t kDefaultMaxSpinsBeforeThinLockInflation = 50;
54 static bool IsSensitiveThread();
55 static void Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)());
57 // Return the thread id of the lock owner or 0 when there is no owner.
58 static uint32_t GetLockOwnerThreadId(mirror::Object* obj)
59 NO_THREAD_SAFETY_ANALYSIS; // TODO: Reading lock owner without holding lock is racy.
61 static mirror::Object* MonitorEnter(Thread* thread, mirror::Object* obj)
62 EXCLUSIVE_LOCK_FUNCTION(obj)
63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64 static bool MonitorExit(Thread* thread, mirror::Object* obj)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
68 static void Notify(Thread* self, mirror::Object* obj)
69 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
70 DoNotify(self, obj, false);
72 static void NotifyAll(Thread* self, mirror::Object* obj)
73 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 DoNotify(self, obj, true);
76 static void Wait(Thread* self, mirror::Object* obj, int64_t ms, int32_t ns,
77 bool interruptShouldThrow, ThreadState why)
78 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
80 static void DescribeWait(std::ostream& os, const Thread* thread)
81 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
82 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
84 // Used to implement JDWP's ThreadReference.CurrentContendedMonitor.
85 static mirror::Object* GetContendedMonitor(Thread* thread)
86 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
88 // Calls 'callback' once for each lock held in the single stack frame represented by
89 // the current state of 'stack_visitor'.
90 static void VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
91 void* callback_context)
92 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
94 static bool IsValidLockWord(LockWord lock_word);
96 template<ReadBarrierOption kReadBarrierOption = kWithReadBarrier>
97 mirror::Object* GetObject() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98 return ReadBarrier::BarrierForRoot<mirror::Object, kReadBarrierOption>(&obj_);
101 void SetObject(mirror::Object* object);
103 Thread* GetOwner() const NO_THREAD_SAFETY_ANALYSIS {
107 int32_t GetHashCode();
109 bool IsLocked() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111 bool HasHashCode() const {
112 return hash_code_.LoadRelaxed() != 0;
115 MonitorId GetMonitorId() const {
119 static void InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
120 uint32_t hash_code) NO_THREAD_SAFETY_ANALYSIS;
122 static bool Deflate(Thread* self, mirror::Object* obj)
123 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126 explicit Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129 // Install the monitor into its object, may fail if another thread installs a different monitor
131 bool Install(Thread* self)
132 LOCKS_EXCLUDED(monitor_lock_)
133 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135 void AppendToWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
136 void RemoveFromWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
138 static void Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
141 void LogContentionEvent(Thread* self, uint32_t wait_ms, uint32_t sample_percent,
142 const char* owner_filename, uint32_t owner_line_number)
143 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
145 static void FailedUnlock(mirror::Object* obj, Thread* expected_owner, Thread* found_owner, Monitor* mon)
146 LOCKS_EXCLUDED(Locks::thread_list_lock_)
147 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
149 void Lock(Thread* self)
150 LOCKS_EXCLUDED(monitor_lock_)
151 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
152 bool Unlock(Thread* thread)
153 LOCKS_EXCLUDED(monitor_lock_)
154 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
156 static void DoNotify(Thread* self, mirror::Object* obj, bool notify_all)
157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
159 void Notify(Thread* self)
160 LOCKS_EXCLUDED(monitor_lock_)
161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
163 void NotifyAll(Thread* self)
164 LOCKS_EXCLUDED(monitor_lock_)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
168 void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow, ThreadState why)
169 LOCKS_EXCLUDED(monitor_lock_)
170 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
172 // Translates the provided method and pc into its declaring class' source file and line number.
173 void TranslateLocation(mirror::ArtMethod* method, uint32_t pc,
174 const char** source_file, uint32_t* line_number) const
175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
177 uint32_t GetOwnerThreadId();
179 static bool (*is_sensitive_thread_hook_)();
180 static uint32_t lock_profiling_threshold_;
182 Mutex monitor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
184 ConditionVariable monitor_contenders_ GUARDED_BY(monitor_lock_);
186 // Number of people waiting on the condition.
187 size_t num_waiters_ GUARDED_BY(monitor_lock_);
189 // Which thread currently owns the lock?
190 Thread* volatile owner_ GUARDED_BY(monitor_lock_);
192 // Owner's recursive lock depth.
193 int lock_count_ GUARDED_BY(monitor_lock_);
195 // What object are we part of. This is a weak root. Do not access
196 // this directly, use GetObject() to read it so it will be guarded
197 // by a read barrier.
198 mirror::Object* obj_;
200 // Threads currently waiting on this monitor.
201 Thread* wait_set_ GUARDED_BY(monitor_lock_);
203 // Stored object hash code, generated lazily by GetHashCode.
204 AtomicInteger hash_code_;
206 // Method and dex pc where the lock owner acquired the lock, used when lock
207 // sampling is enabled. locking_method_ may be null if the lock is currently
208 // unlocked, or if the lock is acquired by the system when the stack is empty.
209 mirror::ArtMethod* locking_method_ GUARDED_BY(monitor_lock_);
210 uint32_t locking_dex_pc_ GUARDED_BY(monitor_lock_);
212 // The denser encoded version of this monitor as stored in the lock word.
213 MonitorId monitor_id_;
215 friend class MonitorInfo;
216 friend class MonitorList;
217 friend class mirror::Object;
218 DISALLOW_COPY_AND_ASSIGN(Monitor);
226 void Add(Monitor* m);
228 void SweepMonitorList(IsMarkedCallback* callback, void* arg)
229 LOCKS_EXCLUDED(monitor_list_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
230 void DisallowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_);
231 void AllowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_);
232 // Returns how many monitors were deflated.
233 size_t DeflateMonitors() LOCKS_EXCLUDED(monitor_list_lock_)
234 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
237 // During sweeping we may free an object and on a separate thread have an object created using
238 // the newly freed memory. That object may then have its lock-word inflated and a monitor created.
239 // If we allow new monitor registration during sweeping this monitor may be incorrectly freed as
240 // the object wasn't marked when sweeping began.
241 bool allow_new_monitors_ GUARDED_BY(monitor_list_lock_);
242 Mutex monitor_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
243 ConditionVariable monitor_add_condition_ GUARDED_BY(monitor_list_lock_);
244 std::list<Monitor*> list_ GUARDED_BY(monitor_list_lock_);
246 friend class Monitor;
247 DISALLOW_COPY_AND_ASSIGN(MonitorList);
250 // Collects information about the current state of an object's monitor.
251 // This is very unsafe, and must only be called when all threads are suspended.
252 // For use only by the JDWP implementation.
255 explicit MonitorInfo(mirror::Object* o) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
259 std::vector<Thread*> waiters_;
262 DISALLOW_COPY_AND_ASSIGN(MonitorInfo);
267 #endif // ART_RUNTIME_MONITOR_H_