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_);
128 explicit Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code,
129 MonitorId id) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
131 // Install the monitor into its object, may fail if another thread installs a different monitor
133 bool Install(Thread* self)
134 LOCKS_EXCLUDED(monitor_lock_)
135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
137 void AppendToWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
138 void RemoveFromWaitSet(Thread* thread) EXCLUSIVE_LOCKS_REQUIRED(monitor_lock_);
140 static void Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
141 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
143 void LogContentionEvent(Thread* self, uint32_t wait_ms, uint32_t sample_percent,
144 const char* owner_filename, uint32_t owner_line_number)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
147 static void FailedUnlock(mirror::Object* obj, Thread* expected_owner, Thread* found_owner, Monitor* mon)
148 LOCKS_EXCLUDED(Locks::thread_list_lock_)
149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
151 void Lock(Thread* self)
152 LOCKS_EXCLUDED(monitor_lock_)
153 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
154 bool Unlock(Thread* thread)
155 LOCKS_EXCLUDED(monitor_lock_)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
158 static void DoNotify(Thread* self, mirror::Object* obj, bool notify_all)
159 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
161 void Notify(Thread* self)
162 LOCKS_EXCLUDED(monitor_lock_)
163 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
165 void NotifyAll(Thread* self)
166 LOCKS_EXCLUDED(monitor_lock_)
167 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
170 void Wait(Thread* self, int64_t msec, int32_t nsec, bool interruptShouldThrow, ThreadState why)
171 LOCKS_EXCLUDED(monitor_lock_)
172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
174 // Translates the provided method and pc into its declaring class' source file and line number.
175 void TranslateLocation(mirror::ArtMethod* method, uint32_t pc,
176 const char** source_file, uint32_t* line_number) const
177 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
179 uint32_t GetOwnerThreadId();
181 static bool (*is_sensitive_thread_hook_)();
182 static uint32_t lock_profiling_threshold_;
184 Mutex monitor_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
186 ConditionVariable monitor_contenders_ GUARDED_BY(monitor_lock_);
188 // Number of people waiting on the condition.
189 size_t num_waiters_ GUARDED_BY(monitor_lock_);
191 // Which thread currently owns the lock?
192 Thread* volatile owner_ GUARDED_BY(monitor_lock_);
194 // Owner's recursive lock depth.
195 int lock_count_ GUARDED_BY(monitor_lock_);
197 // What object are we part of. This is a weak root. Do not access
198 // this directly, use GetObject() to read it so it will be guarded
199 // by a read barrier.
200 mirror::Object* obj_;
202 // Threads currently waiting on this monitor.
203 Thread* wait_set_ GUARDED_BY(monitor_lock_);
205 // Stored object hash code, generated lazily by GetHashCode.
206 AtomicInteger hash_code_;
208 // Method and dex pc where the lock owner acquired the lock, used when lock
209 // sampling is enabled. locking_method_ may be null if the lock is currently
210 // unlocked, or if the lock is acquired by the system when the stack is empty.
211 mirror::ArtMethod* locking_method_ GUARDED_BY(monitor_lock_);
212 uint32_t locking_dex_pc_ GUARDED_BY(monitor_lock_);
214 // The denser encoded version of this monitor as stored in the lock word.
215 MonitorId monitor_id_;
218 // Free list for monitor pool.
219 Monitor* next_free_ GUARDED_BY(Locks::allocated_monitor_ids_lock_);
222 friend class MonitorInfo;
223 friend class MonitorList;
224 friend class MonitorPool;
225 friend class mirror::Object;
226 DISALLOW_COPY_AND_ASSIGN(Monitor);
234 void Add(Monitor* m);
236 void SweepMonitorList(IsMarkedCallback* callback, void* arg)
237 LOCKS_EXCLUDED(monitor_list_lock_) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
238 void DisallowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_);
239 void AllowNewMonitors() LOCKS_EXCLUDED(monitor_list_lock_);
240 // Returns how many monitors were deflated.
241 size_t DeflateMonitors() LOCKS_EXCLUDED(monitor_list_lock_)
242 EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
245 // During sweeping we may free an object and on a separate thread have an object created using
246 // the newly freed memory. That object may then have its lock-word inflated and a monitor created.
247 // If we allow new monitor registration during sweeping this monitor may be incorrectly freed as
248 // the object wasn't marked when sweeping began.
249 bool allow_new_monitors_ GUARDED_BY(monitor_list_lock_);
250 Mutex monitor_list_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
251 ConditionVariable monitor_add_condition_ GUARDED_BY(monitor_list_lock_);
252 std::list<Monitor*> list_ GUARDED_BY(monitor_list_lock_);
254 friend class Monitor;
255 DISALLOW_COPY_AND_ASSIGN(MonitorList);
258 // Collects information about the current state of an object's monitor.
259 // This is very unsafe, and must only be called when all threads are suspended.
260 // For use only by the JDWP implementation.
263 explicit MonitorInfo(mirror::Object* o) EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_);
267 std::vector<Thread*> waiters_;
270 DISALLOW_COPY_AND_ASSIGN(MonitorInfo);
275 #endif // ART_RUNTIME_MONITOR_H_