blob: e1107633005f5f8f75c4fae059aae2d05909f587 [file] [log] [blame]
Elliott Hughes5f791332011-09-15 17:45:30 -07001/*
2 * Copyright (C) 2008 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
Elliott Hughes54e7df12011-09-16 11:47:04 -070017#include "monitor.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070018
Elliott Hughes08fc03a2012-06-26 17:34:00 -070019#include <vector>
20
Andreas Gampe46ee31b2016-12-14 10:11:49 -080021#include "android-base/stringprintf.h"
22
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include "art_method-inl.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080024#include "base/logging.h" // For VLOG.
Elliott Hughes76b61672012-12-12 17:47:30 -080025#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080026#include "base/quasi_atomic.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080027#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080028#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010029#include "base/time_utils.h"
jeffhao33dc7712011-11-09 17:54:24 -080030#include "class_linker.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file-inl.h"
32#include "dex/dex_file_types.h"
33#include "dex/dex_instruction-inl.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070034#include "lock_word-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070035#include "mirror/class-inl.h"
Ian Rogers05f30572013-02-20 12:13:11 -080036#include "mirror/object-inl.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070037#include "object_callbacks.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070038#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070039#include "stack.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070040#include "thread.h"
Elliott Hughes8e4aac52011-09-26 17:03:36 -070041#include "thread_list.h"
Elliott Hughes08fc03a2012-06-26 17:34:00 -070042#include "verifier/method_verifier.h"
Elliott Hughes044288f2012-06-25 14:46:39 -070043#include "well_known_classes.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070044
45namespace art {
46
Andreas Gampe46ee31b2016-12-14 10:11:49 -080047using android::base::StringPrintf;
48
Andreas Gampe5d689142017-10-19 13:03:29 -070049static constexpr uint64_t kDebugThresholdFudgeFactor = kIsDebugBuild ? 10 : 1;
50static constexpr uint64_t kLongWaitMs = 100 * kDebugThresholdFudgeFactor;
Mathieu Chartierb9001ab2014-10-03 13:28:46 -070051
Elliott Hughes5f791332011-09-15 17:45:30 -070052/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -070053 * Every Object has a monitor associated with it, but not every Object is actually locked. Even
54 * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
55 * or b) wait() is called on the Object.
Elliott Hughes5f791332011-09-15 17:45:30 -070056 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070057 * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
58 * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us,
59 * though, because we have a full 32 bits to work with.
Elliott Hughes5f791332011-09-15 17:45:30 -070060 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070061 * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition
62 * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
63 * a lock has been inflated it remains in the "fat" state indefinitely.
Elliott Hughes5f791332011-09-15 17:45:30 -070064 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070065 * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
66 * in the LockWord value type.
Elliott Hughes54e7df12011-09-16 11:47:04 -070067 *
Elliott Hughes5f791332011-09-15 17:45:30 -070068 * Monitors provide:
69 * - mutually exclusive access to resources
70 * - a way for multiple threads to wait for notification
71 *
72 * In effect, they fill the role of both mutexes and condition variables.
73 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070074 * Only one thread can own the monitor at any time. There may be several threads waiting on it
75 * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified
76 * at any given time.
Elliott Hughes5f791332011-09-15 17:45:30 -070077 */
Elliott Hughes54e7df12011-09-16 11:47:04 -070078
Elliott Hughesfc861622011-10-17 17:57:47 -070079uint32_t Monitor::lock_profiling_threshold_ = 0;
Andreas Gamped0210e52017-06-23 13:38:09 -070080uint32_t Monitor::stack_dump_lock_profiling_threshold_ = 0;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070081
Andreas Gamped0210e52017-06-23 13:38:09 -070082void Monitor::Init(uint32_t lock_profiling_threshold,
83 uint32_t stack_dump_lock_profiling_threshold) {
Andreas Gampe5d689142017-10-19 13:03:29 -070084 // It isn't great to always include the debug build fudge factor for command-
85 // line driven arguments, but it's easier to adjust here than in the build.
86 lock_profiling_threshold_ =
87 lock_profiling_threshold * kDebugThresholdFudgeFactor;
88 stack_dump_lock_profiling_threshold_ =
89 stack_dump_lock_profiling_threshold * kDebugThresholdFudgeFactor;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070090}
91
Ian Rogersef7d42f2014-01-06 12:55:46 -080092Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 : monitor_lock_("a monitor lock", kMonitorLock),
Ian Rogersd9c4fc92013-10-01 19:45:43 -070094 monitor_contenders_("monitor contenders", monitor_lock_),
Mathieu Chartier46bc7782013-11-12 17:03:02 -080095 num_waiters_(0),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 owner_(owner),
Elliott Hughes5f791332011-09-15 17:45:30 -070097 lock_count_(0),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070098 obj_(GcRoot<mirror::Object>(obj)),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070099 wait_set_(nullptr),
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700100 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700101 locking_method_(nullptr),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 locking_dex_pc_(0),
Andreas Gampe74240812014-04-17 10:35:09 -0700103 monitor_id_(MonitorPool::ComputeMonitorId(this, self)) {
104#ifdef __LP64__
105 DCHECK(false) << "Should not be reached in 64b";
106 next_free_ = nullptr;
107#endif
108 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
109 // with the owner unlocking the thin-lock.
110 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
111 // The identity hash code is set for the life time of the monitor.
112}
113
114Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code,
115 MonitorId id)
116 : monitor_lock_("a monitor lock", kMonitorLock),
117 monitor_contenders_("monitor contenders", monitor_lock_),
118 num_waiters_(0),
119 owner_(owner),
120 lock_count_(0),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700121 obj_(GcRoot<mirror::Object>(obj)),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122 wait_set_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700123 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700124 locking_method_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700125 locking_dex_pc_(0),
126 monitor_id_(id) {
127#ifdef __LP64__
128 next_free_ = nullptr;
129#endif
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700130 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
131 // with the owner unlocking the thin-lock.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700133 // The identity hash code is set for the life time of the monitor.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700134}
135
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700136int32_t Monitor::GetHashCode() {
137 while (!HasHashCode()) {
Orion Hodson4557b382018-01-03 11:47:54 +0000138 if (hash_code_.CompareAndSetWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) {
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700139 break;
140 }
141 }
142 DCHECK(HasHashCode());
Orion Hodson88591fe2018-03-06 13:35:43 +0000143 return hash_code_.load(std::memory_order_relaxed);
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700144}
145
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700146bool Monitor::Install(Thread* self) {
147 MutexLock mu(self, monitor_lock_); // Uncontended mutex acquisition as monitor isn't yet public.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700148 CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700149 // Propagate the lock state.
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -0700150 LockWord lw(GetObject()->GetLockWord(false));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700151 switch (lw.GetState()) {
152 case LockWord::kThinLocked: {
153 CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
154 lock_count_ = lw.ThinLockCount();
155 break;
156 }
157 case LockWord::kHashCode: {
Orion Hodson88591fe2018-03-06 13:35:43 +0000158 CHECK_EQ(hash_code_.load(std::memory_order_relaxed), static_cast<int32_t>(lw.GetHashCode()));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700159 break;
160 }
161 case LockWord::kFatLocked: {
162 // The owner_ is suspended but another thread beat us to install a monitor.
163 return false;
164 }
165 case LockWord::kUnlocked: {
166 LOG(FATAL) << "Inflating unlocked lock word";
167 break;
168 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700169 default: {
170 LOG(FATAL) << "Invalid monitor state " << lw.GetState();
171 return false;
172 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700173 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700174 LockWord fat(this, lw.GCState());
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700175 // Publish the updated lock word, which may race with other threads.
Hans Boehmb3da36c2016-12-15 13:12:59 -0800176 bool success = GetObject()->CasLockWordWeakRelease(lw, fat);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700177 // Lock profiling.
Mathieu Chartier9728f912013-10-30 09:45:13 -0700178 if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
Andreas Gampe6ec8ebd2014-07-25 13:36:56 -0700179 // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on
180 // abort.
181 locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false);
Andreas Gampe5a387272017-11-06 19:47:16 -0800182 if (locking_method_ != nullptr && UNLIKELY(locking_method_->IsProxyMethod())) {
183 // Grab another frame. Proxy methods are not helpful for lock profiling. This should be rare
184 // enough that it's OK to walk the stack twice.
185 struct NextMethodVisitor FINAL : public StackVisitor {
186 explicit NextMethodVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
187 : StackVisitor(thread,
188 nullptr,
189 StackVisitor::StackWalkKind::kIncludeInlinedFrames,
190 false),
191 count_(0),
192 method_(nullptr),
193 dex_pc_(0) {}
194 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
195 ArtMethod* m = GetMethod();
196 if (m->IsRuntimeMethod()) {
197 // Continue if this is a runtime method.
198 return true;
199 }
200 count_++;
201 if (count_ == 2u) {
202 method_ = m;
203 dex_pc_ = GetDexPc(false);
204 return false;
205 }
206 return true;
207 }
208 size_t count_;
209 ArtMethod* method_;
210 uint32_t dex_pc_;
211 };
212 NextMethodVisitor nmv(owner_);
213 nmv.WalkStack();
214 locking_method_ = nmv.method_;
215 locking_dex_pc_ = nmv.dex_pc_;
216 }
217 DCHECK(locking_method_ == nullptr || !locking_method_->IsProxyMethod());
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700218 }
219 return success;
Elliott Hughes5f791332011-09-15 17:45:30 -0700220}
221
222Monitor::~Monitor() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700223 // Deflated monitors have a null object.
Elliott Hughes5f791332011-09-15 17:45:30 -0700224}
225
Elliott Hughes5f791332011-09-15 17:45:30 -0700226void Monitor::AppendToWaitSet(Thread* thread) {
227 DCHECK(owner_ == Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700228 DCHECK(thread != nullptr);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700229 DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700230 if (wait_set_ == nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700231 wait_set_ = thread;
232 return;
233 }
234
235 // push_back.
236 Thread* t = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700237 while (t->GetWaitNext() != nullptr) {
238 t = t->GetWaitNext();
Elliott Hughes5f791332011-09-15 17:45:30 -0700239 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700240 t->SetWaitNext(thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700241}
242
Elliott Hughes5f791332011-09-15 17:45:30 -0700243void Monitor::RemoveFromWaitSet(Thread *thread) {
244 DCHECK(owner_ == Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700245 DCHECK(thread != nullptr);
246 if (wait_set_ == nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700247 return;
248 }
249 if (wait_set_ == thread) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700250 wait_set_ = thread->GetWaitNext();
251 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700252 return;
253 }
254
255 Thread* t = wait_set_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700256 while (t->GetWaitNext() != nullptr) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700257 if (t->GetWaitNext() == thread) {
258 t->SetWaitNext(thread->GetWaitNext());
259 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700260 return;
261 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700262 t = t->GetWaitNext();
Elliott Hughes5f791332011-09-15 17:45:30 -0700263 }
264}
265
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700266void Monitor::SetObject(mirror::Object* object) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700267 obj_ = GcRoot<mirror::Object>(object);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700268}
269
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700270// Note: Adapted from CurrentMethodVisitor in thread.cc. We must not resolve here.
271
272struct NthCallerWithDexPcVisitor FINAL : public StackVisitor {
273 explicit NthCallerWithDexPcVisitor(Thread* thread, size_t frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700274 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100275 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700276 method_(nullptr),
277 dex_pc_(0),
278 current_frame_number_(0),
279 wanted_frame_number_(frame) {}
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700280 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700281 ArtMethod* m = GetMethod();
282 if (m == nullptr || m->IsRuntimeMethod()) {
283 // Runtime method, upcall, or resolution issue. Skip.
284 return true;
285 }
286
287 // Is this the requested frame?
288 if (current_frame_number_ == wanted_frame_number_) {
289 method_ = m;
290 dex_pc_ = GetDexPc(false /* abort_on_error*/);
291 return false;
292 }
293
294 // Look for more.
295 current_frame_number_++;
296 return true;
297 }
298
299 ArtMethod* method_;
300 uint32_t dex_pc_;
301
302 private:
303 size_t current_frame_number_;
304 const size_t wanted_frame_number_;
305};
306
307// This function is inlined and just helps to not have the VLOG and ATRACE check at all the
308// potential tracing points.
309void Monitor::AtraceMonitorLock(Thread* self, mirror::Object* obj, bool is_wait) {
310 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging) && ATRACE_ENABLED())) {
311 AtraceMonitorLockImpl(self, obj, is_wait);
312 }
313}
314
315void Monitor::AtraceMonitorLockImpl(Thread* self, mirror::Object* obj, bool is_wait) {
316 // Wait() requires a deeper call stack to be useful. Otherwise you'll see "Waiting at
317 // Object.java". Assume that we'll wait a nontrivial amount, so it's OK to do a longer
318 // stack walk than if !is_wait.
319 NthCallerWithDexPcVisitor visitor(self, is_wait ? 1U : 0U);
320 visitor.WalkStack(false);
321 const char* prefix = is_wait ? "Waiting on " : "Locking ";
322
323 const char* filename;
324 int32_t line_number;
325 TranslateLocation(visitor.method_, visitor.dex_pc_, &filename, &line_number);
326
327 // It would be nice to have a stable "ID" for the object here. However, the only stable thing
328 // would be the identity hashcode. But we cannot use IdentityHashcode here: For one, there are
329 // times when it is unsafe to make that call (see stack dumping for an explanation). More
330 // importantly, we would have to give up on thin-locking when adding systrace locks, as the
331 // identity hashcode is stored in the lockword normally (so can't be used with thin-locks).
332 //
333 // Because of thin-locks we also cannot use the monitor id (as there is no monitor). Monitor ids
334 // also do not have to be stable, as the monitor may be deflated.
335 std::string tmp = StringPrintf("%s %d at %s:%d",
336 prefix,
337 (obj == nullptr ? -1 : static_cast<int32_t>(reinterpret_cast<uintptr_t>(obj))),
338 (filename != nullptr ? filename : "null"),
339 line_number);
340 ATRACE_BEGIN(tmp.c_str());
341}
342
343void Monitor::AtraceMonitorUnlock() {
344 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging))) {
345 ATRACE_END();
346 }
347}
348
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700349std::string Monitor::PrettyContentionInfo(const std::string& owner_name,
350 pid_t owner_tid,
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700351 ArtMethod* owners_method,
352 uint32_t owners_dex_pc,
353 size_t num_waiters) {
Hiroshi Yamauchi71cd68d2017-01-25 18:28:12 -0800354 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700355 const char* owners_filename;
Goran Jakovljevic49c882b2016-04-19 10:27:21 +0200356 int32_t owners_line_number = 0;
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700357 if (owners_method != nullptr) {
358 TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
359 }
360 std::ostringstream oss;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700361 oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700362 if (owners_method != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700363 oss << " at " << owners_method->PrettyMethod();
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700364 oss << "(" << owners_filename << ":" << owners_line_number << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700365 }
366 oss << " waiters=" << num_waiters;
367 return oss.str();
368}
369
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700370bool Monitor::TryLockLocked(Thread* self) {
371 if (owner_ == nullptr) { // Unowned.
372 owner_ = self;
373 CHECK_EQ(lock_count_, 0);
374 // When debugging, save the current monitor holder for future
375 // acquisition failures to use in sampled logging.
376 if (lock_profiling_threshold_ != 0) {
377 locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
Andreas Gampe5a387272017-11-06 19:47:16 -0800378 // We don't expect a proxy method here.
379 DCHECK(locking_method_ == nullptr || !locking_method_->IsProxyMethod());
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700380 }
381 } else if (owner_ == self) { // Recursive.
382 lock_count_++;
383 } else {
384 return false;
385 }
386 AtraceMonitorLock(self, GetObject(), false /* is_wait */);
387 return true;
388}
389
390bool Monitor::TryLock(Thread* self) {
391 MutexLock mu(self, monitor_lock_);
392 return TryLockLocked(self);
393}
394
Alex Light77fee872017-09-05 14:51:49 -0700395// Asserts that a mutex isn't held when the class comes into and out of scope.
396class ScopedAssertNotHeld {
397 public:
398 ScopedAssertNotHeld(Thread* self, Mutex& mu) : self_(self), mu_(mu) {
399 mu_.AssertNotHeld(self_);
400 }
401
402 ~ScopedAssertNotHeld() {
403 mu_.AssertNotHeld(self_);
404 }
405
406 private:
407 Thread* const self_;
408 Mutex& mu_;
409 DISALLOW_COPY_AND_ASSIGN(ScopedAssertNotHeld);
410};
411
412template <LockReason reason>
Elliott Hughes5f791332011-09-15 17:45:30 -0700413void Monitor::Lock(Thread* self) {
Alex Light77fee872017-09-05 14:51:49 -0700414 ScopedAssertNotHeld sanh(self, monitor_lock_);
415 bool called_monitors_callback = false;
416 monitor_lock_.Lock(self);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700417 while (true) {
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700418 if (TryLockLocked(self)) {
Alex Light77fee872017-09-05 14:51:49 -0700419 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700420 }
421 // Contended.
422 const bool log_contention = (lock_profiling_threshold_ != 0);
Xin Guanb894a192014-08-22 11:55:37 -0500423 uint64_t wait_start_ms = log_contention ? MilliTime() : 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 ArtMethod* owners_method = locking_method_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700425 uint32_t owners_dex_pc = locking_dex_pc_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700426 // Do this before releasing the lock so that we don't get deflated.
Mathieu Chartierb9001ab2014-10-03 13:28:46 -0700427 size_t num_waiters = num_waiters_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700428 ++num_waiters_;
Andreas Gampe2702d562017-02-06 09:48:00 -0800429
430 // If systrace logging is enabled, first look at the lock owner. Acquiring the monitor's
431 // lock and then re-acquiring the mutator lock can deadlock.
432 bool started_trace = false;
433 if (ATRACE_ENABLED()) {
434 if (owner_ != nullptr) { // Did the owner_ give the lock up?
435 std::ostringstream oss;
436 std::string name;
437 owner_->GetThreadName(name);
438 oss << PrettyContentionInfo(name,
439 owner_->GetTid(),
440 owners_method,
441 owners_dex_pc,
442 num_waiters);
443 // Add info for contending thread.
444 uint32_t pc;
445 ArtMethod* m = self->GetCurrentMethod(&pc);
446 const char* filename;
447 int32_t line_number;
448 TranslateLocation(m, pc, &filename, &line_number);
449 oss << " blocking from "
450 << ArtMethod::PrettyMethod(m) << "(" << (filename != nullptr ? filename : "null")
451 << ":" << line_number << ")";
452 ATRACE_BEGIN(oss.str().c_str());
453 started_trace = true;
454 }
455 }
456
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700457 monitor_lock_.Unlock(self); // Let go of locks in order.
Alex Light77fee872017-09-05 14:51:49 -0700458 // Call the contended locking cb once and only once. Also only call it if we are locking for
459 // the first time, not during a Wait wakeup.
460 if (reason == LockReason::kForLock && !called_monitors_callback) {
461 called_monitors_callback = true;
462 Runtime::Current()->GetRuntimeCallbacks()->MonitorContendedLocking(this);
463 }
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700464 self->SetMonitorEnterObject(GetObject());
Elliott Hughes5f791332011-09-15 17:45:30 -0700465 {
Hiroshi Yamauchi71cd68d2017-01-25 18:28:12 -0800466 ScopedThreadSuspension tsc(self, kBlocked); // Change to blocked and give up mutator_lock_.
Andreas Gampe2702d562017-02-06 09:48:00 -0800467 uint32_t original_owner_thread_id = 0u;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700468 {
469 // Reacquire monitor_lock_ without mutator_lock_ for Wait.
470 MutexLock mu2(self, monitor_lock_);
471 if (owner_ != nullptr) { // Did the owner_ give the lock up?
472 original_owner_thread_id = owner_->GetThreadId();
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700473 monitor_contenders_.Wait(self); // Still contended so wait.
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800474 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700475 }
476 if (original_owner_thread_id != 0u) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700477 // Woken from contention.
478 if (log_contention) {
Andreas Gampe111b1092017-06-22 20:28:23 -0700479 uint64_t wait_ms = MilliTime() - wait_start_ms;
480 uint32_t sample_percent;
481 if (wait_ms >= lock_profiling_threshold_) {
482 sample_percent = 100;
483 } else {
484 sample_percent = 100 * wait_ms / lock_profiling_threshold_;
485 }
486 if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
487 // Reacquire mutator_lock_ for logging.
488 ScopedObjectAccess soa(self);
Andreas Gampe111b1092017-06-22 20:28:23 -0700489
Andreas Gamped0210e52017-06-23 13:38:09 -0700490 bool owner_alive = false;
491 pid_t original_owner_tid = 0;
492 std::string original_owner_name;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700493
Andreas Gamped0210e52017-06-23 13:38:09 -0700494 const bool should_dump_stacks = stack_dump_lock_profiling_threshold_ > 0 &&
495 wait_ms > stack_dump_lock_profiling_threshold_;
496 std::string owner_stack_dump;
Andreas Gampe111b1092017-06-22 20:28:23 -0700497
Andreas Gamped0210e52017-06-23 13:38:09 -0700498 // Acquire thread-list lock to find thread and keep it from dying until we've got all
499 // the info we need.
500 {
Alex Lightb1e31a82017-10-04 16:57:36 -0700501 Locks::thread_list_lock_->ExclusiveLock(Thread::Current());
Andreas Gamped0210e52017-06-23 13:38:09 -0700502
503 // Re-find the owner in case the thread got killed.
504 Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId(
505 original_owner_thread_id);
506
507 if (original_owner != nullptr) {
508 owner_alive = true;
509 original_owner_tid = original_owner->GetTid();
510 original_owner->GetThreadName(original_owner_name);
511
512 if (should_dump_stacks) {
513 // Very long contention. Dump stacks.
514 struct CollectStackTrace : public Closure {
515 void Run(art::Thread* thread) OVERRIDE
516 REQUIRES_SHARED(art::Locks::mutator_lock_) {
517 thread->DumpJavaStack(oss);
518 }
519
520 std::ostringstream oss;
521 };
522 CollectStackTrace owner_trace;
Alex Lightb1e31a82017-10-04 16:57:36 -0700523 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its
524 // execution.
Andreas Gamped0210e52017-06-23 13:38:09 -0700525 original_owner->RequestSynchronousCheckpoint(&owner_trace);
526 owner_stack_dump = owner_trace.oss.str();
Alex Lightb1e31a82017-10-04 16:57:36 -0700527 } else {
528 Locks::thread_list_lock_->ExclusiveUnlock(Thread::Current());
Andreas Gamped0210e52017-06-23 13:38:09 -0700529 }
Alex Lightb1e31a82017-10-04 16:57:36 -0700530 } else {
531 Locks::thread_list_lock_->ExclusiveUnlock(Thread::Current());
Andreas Gamped0210e52017-06-23 13:38:09 -0700532 }
533 // This is all the data we need. Now drop the thread-list lock, it's OK for the
534 // owner to go away now.
535 }
536
537 // If we found the owner (and thus have owner data), go and log now.
538 if (owner_alive) {
539 // Give the detailed traces for really long contention.
540 if (should_dump_stacks) {
541 // This must be here (and not above) because we cannot hold the thread-list lock
542 // while running the checkpoint.
543 std::ostringstream self_trace_oss;
544 self->DumpJavaStack(self_trace_oss);
545
546 uint32_t pc;
547 ArtMethod* m = self->GetCurrentMethod(&pc);
548
549 LOG(WARNING) << "Long "
550 << PrettyContentionInfo(original_owner_name,
551 original_owner_tid,
552 owners_method,
553 owners_dex_pc,
554 num_waiters)
555 << " in " << ArtMethod::PrettyMethod(m) << " for "
556 << PrettyDuration(MsToNs(wait_ms)) << "\n"
557 << "Current owner stack:\n" << owner_stack_dump
558 << "Contender stack:\n" << self_trace_oss.str();
559 } else if (wait_ms > kLongWaitMs && owners_method != nullptr) {
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700560 uint32_t pc;
561 ArtMethod* m = self->GetCurrentMethod(&pc);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700562 // TODO: We should maybe check that original_owner is still a live thread.
563 LOG(WARNING) << "Long "
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700564 << PrettyContentionInfo(original_owner_name,
565 original_owner_tid,
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700566 owners_method,
567 owners_dex_pc,
568 num_waiters)
David Sehr709b0702016-10-13 09:12:37 -0700569 << " in " << ArtMethod::PrettyMethod(m) << " for "
570 << PrettyDuration(MsToNs(wait_ms));
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700571 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700572 LogContentionEvent(self,
Alex Light77fee872017-09-05 14:51:49 -0700573 wait_ms,
574 sample_percent,
575 owners_method,
576 owners_dex_pc);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700577 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700578 }
579 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700580 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700581 }
Andreas Gampe2702d562017-02-06 09:48:00 -0800582 if (started_trace) {
583 ATRACE_END();
584 }
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700585 self->SetMonitorEnterObject(nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700586 monitor_lock_.Lock(self); // Reacquire locks in order.
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700587 --num_waiters_;
Elliott Hughesfc861622011-10-17 17:57:47 -0700588 }
Alex Light77fee872017-09-05 14:51:49 -0700589 monitor_lock_.Unlock(self);
590 // We need to pair this with a single contended locking call. NB we match the RI behavior and call
591 // this even if MonitorEnter failed.
592 if (called_monitors_callback) {
593 CHECK(reason == LockReason::kForLock);
594 Runtime::Current()->GetRuntimeCallbacks()->MonitorContendedLocked(this);
595 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700596}
597
Alex Light77fee872017-09-05 14:51:49 -0700598template void Monitor::Lock<LockReason::kForLock>(Thread* self);
599template void Monitor::Lock<LockReason::kForWait>(Thread* self);
600
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800601static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
602 __attribute__((format(printf, 1, 2)));
603
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700604static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700605 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800606 va_list args;
607 va_start(args, fmt);
Ian Rogers62d6c772013-02-27 08:32:07 -0800608 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000609 self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700610 if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
Brian Carlstrom64277f32012-03-26 23:53:34 -0700611 std::ostringstream ss;
Ian Rogers62d6c772013-02-27 08:32:07 -0800612 self->Dump(ss);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700613 LOG(Runtime::Current()->IsStarted() ? ::android::base::INFO : ::android::base::ERROR)
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000614 << self->GetException()->Dump() << "\n" << ss.str();
Brian Carlstrom64277f32012-03-26 23:53:34 -0700615 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800616 va_end(args);
617}
618
Elliott Hughesd4237412012-02-21 11:24:45 -0800619static std::string ThreadToString(Thread* thread) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700620 if (thread == nullptr) {
621 return "nullptr";
Elliott Hughesd4237412012-02-21 11:24:45 -0800622 }
623 std::ostringstream oss;
624 // TODO: alternatively, we could just return the thread's name.
625 oss << *thread;
626 return oss.str();
627}
628
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700629void Monitor::FailedUnlock(mirror::Object* o,
630 uint32_t expected_owner_thread_id,
631 uint32_t found_owner_thread_id,
Elliott Hughesffb465f2012-03-01 18:46:05 -0800632 Monitor* monitor) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700633 // Acquire thread list lock so threads won't disappear from under us.
Elliott Hughesffb465f2012-03-01 18:46:05 -0800634 std::string current_owner_string;
635 std::string expected_owner_string;
636 std::string found_owner_string;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700637 uint32_t current_owner_thread_id = 0u;
Elliott Hughesffb465f2012-03-01 18:46:05 -0800638 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700639 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700640 ThreadList* const thread_list = Runtime::Current()->GetThreadList();
641 Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id);
642 Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id);
643
Elliott Hughesffb465f2012-03-01 18:46:05 -0800644 // Re-read owner now that we hold lock.
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700645 Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr;
646 if (current_owner != nullptr) {
647 current_owner_thread_id = current_owner->GetThreadId();
648 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800649 // Get short descriptions of the threads involved.
650 current_owner_string = ThreadToString(current_owner);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700651 expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed";
652 found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed";
Elliott Hughesffb465f2012-03-01 18:46:05 -0800653 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700654
655 if (current_owner_thread_id == 0u) {
656 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800657 ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
658 " on thread '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700659 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800660 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800661 } else {
662 // Race: the original read found an owner but now there is none
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800663 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
664 " (where now the monitor appears unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800665 found_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700666 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800667 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800668 }
669 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700670 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800671 // Race: originally there was no owner, there is now
672 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
673 " (originally believed to be unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800674 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700675 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800676 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800677 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700678 if (found_owner_thread_id != current_owner_thread_id) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800679 // Race: originally found and current owner have changed
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800680 ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
681 " owned by '%s') on object of type '%s' on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800682 found_owner_string.c_str(),
683 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700684 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800685 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800686 } else {
687 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
688 " on thread '%s",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800689 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700690 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800691 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800692 }
693 }
694 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700695}
696
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700697bool Monitor::Unlock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700698 DCHECK(self != nullptr);
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700699 uint32_t owner_thread_id = 0u;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700700 {
701 MutexLock mu(self, monitor_lock_);
702 Thread* owner = owner_;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700703 if (owner != nullptr) {
704 owner_thread_id = owner->GetThreadId();
705 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700706 if (owner == self) {
707 // We own the monitor, so nobody else can be in here.
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700708 AtraceMonitorUnlock();
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700709 if (lock_count_ == 0) {
710 owner_ = nullptr;
711 locking_method_ = nullptr;
712 locking_dex_pc_ = 0;
713 // Wake a contender.
714 monitor_contenders_.Signal(self);
715 } else {
716 --lock_count_;
717 }
718 return true;
Elliott Hughes5f791332011-09-15 17:45:30 -0700719 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700720 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700721 // We don't own this, so we're not allowed to unlock it.
722 // The JNI spec says that we should throw IllegalMonitorStateException in this case.
723 FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this);
724 return false;
Elliott Hughes5f791332011-09-15 17:45:30 -0700725}
726
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800727void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
728 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700729 DCHECK(self != nullptr);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800730 DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
Elliott Hughes5f791332011-09-15 17:45:30 -0700731
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700732 monitor_lock_.Lock(self);
733
Elliott Hughes5f791332011-09-15 17:45:30 -0700734 // Make sure that we hold the lock.
735 if (owner_ != self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700736 monitor_lock_.Unlock(self);
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700737 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700738 return;
739 }
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800740
Elliott Hughesdf42c482013-01-09 12:49:02 -0800741 // We need to turn a zero-length timed wait into a regular wait because
742 // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
743 if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
744 why = kWaiting;
745 }
746
Elliott Hughes5f791332011-09-15 17:45:30 -0700747 // Enforce the timeout range.
748 if (ms < 0 || ns < 0 || ns > 999999) {
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700749 monitor_lock_.Unlock(self);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000750 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800751 "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
Elliott Hughes5f791332011-09-15 17:45:30 -0700752 return;
753 }
754
Elliott Hughes5f791332011-09-15 17:45:30 -0700755 /*
756 * Add ourselves to the set of threads waiting on this monitor, and
757 * release our hold. We need to let it go even if we're a few levels
758 * deep in a recursive lock, and we need to restore that later.
759 *
760 * We append to the wait set ahead of clearing the count and owner
761 * fields so the subroutine can check that the calling thread owns
762 * the monitor. Aside from that, the order of member updates is
763 * not order sensitive as we hold the pthread mutex.
764 */
765 AppendToWaitSet(self);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700766 ++num_waiters_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700767 int prev_lock_count = lock_count_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700768 lock_count_ = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700769 owner_ = nullptr;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700770 ArtMethod* saved_method = locking_method_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700771 locking_method_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700772 uintptr_t saved_dex_pc = locking_dex_pc_;
773 locking_dex_pc_ = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700774
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700775 AtraceMonitorUnlock(); // For the implict Unlock() just above. This will only end the deepest
776 // nesting, but that is enough for the visualization, and corresponds to
777 // the single Lock() we do afterwards.
778 AtraceMonitorLock(self, GetObject(), true /* is_wait */);
779
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800780 bool was_interrupted = false;
Alex Light77fee872017-09-05 14:51:49 -0700781 bool timed_out = false;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700782 {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700783 // Update thread state. If the GC wakes up, it'll ignore us, knowing
784 // that we won't touch any references in this state, and we'll check
785 // our suspend mode before we transition out.
786 ScopedThreadSuspension sts(self, why);
787
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700788 // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700789 MutexLock mu(self, *self->GetWaitMutex());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700790
791 // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700792 // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700793 // up.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700794 DCHECK(self->GetWaitMonitor() == nullptr);
795 self->SetWaitMonitor(this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700796
797 // Release the monitor lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700798 monitor_contenders_.Signal(self);
799 monitor_lock_.Unlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700800
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800801 // Handle the case where the thread was interrupted before we called wait().
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000802 if (self->IsInterrupted()) {
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800803 was_interrupted = true;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700804 } else {
805 // Wait for a notification or a timeout to occur.
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800806 if (why == kWaiting) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700807 self->GetWaitConditionVariable()->Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700808 } else {
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800809 DCHECK(why == kTimedWaiting || why == kSleeping) << why;
Alex Light77fee872017-09-05 14:51:49 -0700810 timed_out = self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700811 }
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000812 was_interrupted = self->IsInterrupted();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700813 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700814 }
815
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800816 {
817 // We reset the thread's wait_monitor_ field after transitioning back to runnable so
818 // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
819 // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
820 // are waiting on "null".)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700821 MutexLock mu(self, *self->GetWaitMutex());
822 DCHECK(self->GetWaitMonitor() != nullptr);
823 self->SetWaitMonitor(nullptr);
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800824 }
825
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800826 // Allocate the interrupted exception not holding the monitor lock since it may cause a GC.
827 // If the GC requires acquiring the monitor for enqueuing cleared references, this would
828 // cause a deadlock if the monitor is held.
829 if (was_interrupted && interruptShouldThrow) {
830 /*
831 * We were interrupted while waiting, or somebody interrupted an
832 * un-interruptible thread earlier and we're bailing out immediately.
833 *
834 * The doc sayeth: "The interrupted status of the current thread is
835 * cleared when this exception is thrown."
836 */
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000837 self->SetInterrupted(false);
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800838 self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr);
839 }
840
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700841 AtraceMonitorUnlock(); // End Wait().
842
Alex Light77fee872017-09-05 14:51:49 -0700843 // We just slept, tell the runtime callbacks about this.
844 Runtime::Current()->GetRuntimeCallbacks()->MonitorWaitFinished(this, timed_out);
845
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700846 // Re-acquire the monitor and lock.
Alex Light77fee872017-09-05 14:51:49 -0700847 Lock<LockReason::kForWait>(self);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700848 monitor_lock_.Lock(self);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700849 self->GetWaitMutex()->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700850
Elliott Hughes5f791332011-09-15 17:45:30 -0700851 /*
852 * We remove our thread from wait set after restoring the count
853 * and owner fields so the subroutine can check that the calling
854 * thread owns the monitor. Aside from that, the order of member
855 * updates is not order sensitive as we hold the pthread mutex.
856 */
857 owner_ = self;
Ian Rogers0399dde2012-06-06 17:09:28 -0700858 lock_count_ = prev_lock_count;
859 locking_method_ = saved_method;
860 locking_dex_pc_ = saved_dex_pc;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700861 --num_waiters_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700862 RemoveFromWaitSet(self);
863
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700864 monitor_lock_.Unlock(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700865}
866
867void Monitor::Notify(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700868 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700869 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700870 // Make sure that we hold the lock.
871 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800872 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700873 return;
874 }
875 // Signal the first waiting thread in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700876 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700877 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700878 wait_set_ = thread->GetWaitNext();
879 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700880
881 // Check to see if the thread is still waiting.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800882 MutexLock wait_mu(self, *thread->GetWaitMutex());
Ian Rogersdd7624d2014-03-14 17:43:00 -0700883 if (thread->GetWaitMonitor() != nullptr) {
884 thread->GetWaitConditionVariable()->Signal(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700885 return;
886 }
887 }
888}
889
890void Monitor::NotifyAll(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700891 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700892 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700893 // Make sure that we hold the lock.
894 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800895 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700896 return;
897 }
898 // Signal all threads in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700899 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700900 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700901 wait_set_ = thread->GetWaitNext();
902 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700903 thread->Notify();
904 }
905}
906
Mathieu Chartier590fee92013-09-13 13:46:47 -0700907bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
908 DCHECK(obj != nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700909 // Don't need volatile since we only deflate with mutators suspended.
910 LockWord lw(obj->GetLockWord(false));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700911 // If the lock isn't an inflated monitor, then we don't need to deflate anything.
912 if (lw.GetState() == LockWord::kFatLocked) {
913 Monitor* monitor = lw.FatLockMonitor();
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700914 DCHECK(monitor != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700915 MutexLock mu(self, monitor->monitor_lock_);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700916 // Can't deflate if we have anybody waiting on the CV.
917 if (monitor->num_waiters_ > 0) {
918 return false;
919 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700920 Thread* owner = monitor->owner_;
921 if (owner != nullptr) {
922 // Can't deflate if we are locked and have a hash code.
923 if (monitor->HasHashCode()) {
924 return false;
925 }
926 // Can't deflate if our lock count is too high.
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700927 if (static_cast<uint32_t>(monitor->lock_count_) > LockWord::kThinLockMaxCount) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700928 return false;
929 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700930 // Deflate to a thin lock.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700931 LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(),
932 monitor->lock_count_,
933 lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800934 // Assume no concurrent read barrier state changes as mutators are suspended.
935 obj->SetLockWord(new_lw, false);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700936 VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
937 << monitor->lock_count_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700938 } else if (monitor->HasHashCode()) {
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700939 LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800940 // Assume no concurrent read barrier state changes as mutators are suspended.
941 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700942 VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700943 } else {
944 // No lock and no hash, just put an empty lock word inside the object.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700945 LockWord new_lw = LockWord::FromDefault(lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800946 // Assume no concurrent read barrier state changes as mutators are suspended.
947 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700948 VLOG(monitor) << "Deflated" << obj << " to empty lock word";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700949 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700950 // The monitor is deflated, mark the object as null so that we know to delete it during the
Mathieu Chartier590fee92013-09-13 13:46:47 -0700951 // next GC.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700952 monitor->obj_ = GcRoot<mirror::Object>(nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700953 }
954 return true;
955}
956
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700957void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
Andreas Gampe74240812014-04-17 10:35:09 -0700958 DCHECK(self != nullptr);
959 DCHECK(obj != nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700960 // Allocate and acquire a new monitor.
Andreas Gampe74240812014-04-17 10:35:09 -0700961 Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code);
962 DCHECK(m != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700963 if (m->Install(self)) {
Haifeng Li86ab7912014-05-16 10:47:59 +0800964 if (owner != nullptr) {
965 VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
Andreas Gampe74240812014-04-17 10:35:09 -0700966 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800967 } else {
968 VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
Andreas Gampe74240812014-04-17 10:35:09 -0700969 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800970 }
Andreas Gampe74240812014-04-17 10:35:09 -0700971 Runtime::Current()->GetMonitorList()->Add(m);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700972 CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
Andreas Gampe74240812014-04-17 10:35:09 -0700973 } else {
974 MonitorPool::ReleaseMonitor(self, m);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700975 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700976}
977
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700978void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700979 uint32_t hash_code) {
980 DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
981 uint32_t owner_thread_id = lock_word.ThinLockOwner();
982 if (owner_thread_id == self->GetThreadId()) {
983 // We own the monitor, we can easily inflate it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700984 Inflate(self, self, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700985 } else {
986 ThreadList* thread_list = Runtime::Current()->GetThreadList();
987 // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700988 self->SetMonitorEnterObject(obj.Get());
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700989 bool timed_out;
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700990 Thread* owner;
991 {
Alex Light77fee872017-09-05 14:51:49 -0700992 ScopedThreadSuspension sts(self, kWaitingForLockInflation);
Alex Light46f93402017-06-29 11:59:50 -0700993 owner = thread_list->SuspendThreadByThreadId(owner_thread_id,
994 SuspendReason::kInternal,
995 &timed_out);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700996 }
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700997 if (owner != nullptr) {
998 // We succeeded in suspending the thread, check the lock's status didn't change.
999 lock_word = obj->GetLockWord(true);
1000 if (lock_word.GetState() == LockWord::kThinLocked &&
1001 lock_word.ThinLockOwner() == owner_thread_id) {
1002 // Go ahead and inflate the lock.
1003 Inflate(self, owner, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001004 }
Alex Light88fd7202017-06-30 08:31:59 -07001005 bool resumed = thread_list->Resume(owner, SuspendReason::kInternal);
1006 DCHECK(resumed);
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001007 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001008 self->SetMonitorEnterObject(nullptr);
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001009 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001010}
1011
Ian Rogers719d1a32014-03-06 12:13:39 -08001012// Fool annotalysis into thinking that the lock on obj is acquired.
1013static mirror::Object* FakeLock(mirror::Object* obj)
1014 EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
1015 return obj;
1016}
1017
1018// Fool annotalysis into thinking that the lock on obj is release.
1019static mirror::Object* FakeUnlock(mirror::Object* obj)
1020 UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
1021 return obj;
1022}
1023
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001024mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj, bool trylock) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001025 DCHECK(self != nullptr);
1026 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -07001027 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -08001028 obj = FakeLock(obj);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001029 uint32_t thread_id = self->GetThreadId();
1030 size_t contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001031 StackHandleScope<1> hs(self);
1032 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001033 while (true) {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001034 // We initially read the lockword with ordinary Java/relaxed semantics. When stronger
1035 // semantics are needed, we address it below. Since GetLockWord bottoms out to a relaxed load,
1036 // we can fix it later, in an infrequently executed case, with a fence.
1037 LockWord lock_word = h_obj->GetLockWord(false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001038 switch (lock_word.GetState()) {
1039 case LockWord::kUnlocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001040 // No ordering required for preceding lockword read, since we retest.
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001041 LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.GCState()));
Hans Boehmb3da36c2016-12-15 13:12:59 -08001042 if (h_obj->CasLockWordWeakAcquire(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -07001043 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001044 return h_obj.Get(); // Success!
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001045 }
1046 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -07001047 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001048 case LockWord::kThinLocked: {
1049 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1050 if (owner_thread_id == thread_id) {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001051 // No ordering required for initial lockword read.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001052 // We own the lock, increase the recursion count.
1053 uint32_t new_count = lock_word.ThinLockCount() + 1;
1054 if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001055 LockWord thin_locked(LockWord::FromThinLockId(thread_id,
1056 new_count,
1057 lock_word.GCState()));
Hans Boehmb3da36c2016-12-15 13:12:59 -08001058 // Only this thread pays attention to the count. Thus there is no need for stronger
1059 // than relaxed memory ordering.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001060 if (!kUseReadBarrier) {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001061 h_obj->SetLockWord(thin_locked, false /* volatile */);
Andreas Gampec7ed09b2016-04-25 20:08:55 -07001062 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001063 return h_obj.Get(); // Success!
1064 } else {
1065 // Use CAS to preserve the read barrier state.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001066 if (h_obj->CasLockWordWeakRelaxed(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -07001067 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001068 return h_obj.Get(); // Success!
1069 }
1070 }
1071 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -07001072 } else {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001073 // We'd overflow the recursion count, so inflate the monitor.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001074 InflateThinLocked(self, h_obj, lock_word, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001075 }
1076 } else {
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001077 if (trylock) {
1078 return nullptr;
1079 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001080 // Contention.
1081 contention_count++;
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001082 Runtime* runtime = Runtime::Current();
Hans Boehmb3da36c2016-12-15 13:12:59 -08001083 if (contention_count <= runtime->GetMaxSpinsBeforeThinLockInflation()) {
Alex Light77fee872017-09-05 14:51:49 -07001084 // TODO: Consider switching the thread state to kWaitingForLockInflation when we are
1085 // yielding. Use sched_yield instead of NanoSleep since NanoSleep can wait much longer
1086 // than the parameter you pass in. This can cause thread suspension to take excessively
1087 // long and make long pauses. See b/16307460.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001088 // TODO: We should literally spin first, without sched_yield. Sched_yield either does
1089 // nothing (at significant expense), or guarantees that we wait at least microseconds.
1090 // If the owner is running, I would expect the median lock hold time to be hundreds
1091 // of nanoseconds or less.
Mathieu Chartier251755c2014-07-15 18:10:25 -07001092 sched_yield();
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001093 } else {
1094 contention_count = 0;
Hans Boehmb3da36c2016-12-15 13:12:59 -08001095 // No ordering required for initial lockword read. Install rereads it anyway.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001096 InflateThinLocked(self, h_obj, lock_word, 0);
Elliott Hughes5f791332011-09-15 17:45:30 -07001097 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001098 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001099 continue; // Start from the beginning.
Elliott Hughes5f791332011-09-15 17:45:30 -07001100 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001101 case LockWord::kFatLocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001102 // We should have done an acquire read of the lockword initially, to ensure
1103 // visibility of the monitor data structure. Use an explicit fence instead.
1104 QuasiAtomic::ThreadFenceAcquire();
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001105 Monitor* mon = lock_word.FatLockMonitor();
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001106 if (trylock) {
1107 return mon->TryLock(self) ? h_obj.Get() : nullptr;
1108 } else {
1109 mon->Lock(self);
1110 return h_obj.Get(); // Success!
1111 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001112 }
Ian Rogers719d1a32014-03-06 12:13:39 -08001113 case LockWord::kHashCode:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001114 // Inflate with the existing hashcode.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001115 // Again no ordering required for initial lockword read, since we don't rely
1116 // on the visibility of any prior computation.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001117 Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
Ian Rogers719d1a32014-03-06 12:13:39 -08001118 continue; // Start from the beginning.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001119 default: {
1120 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Andreas Gampec7ed09b2016-04-25 20:08:55 -07001121 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001122 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001123 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001124 }
1125}
1126
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001127bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001128 DCHECK(self != nullptr);
1129 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -07001130 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -08001131 obj = FakeUnlock(obj);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001132 StackHandleScope<1> hs(self);
1133 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001134 while (true) {
1135 LockWord lock_word = obj->GetLockWord(true);
1136 switch (lock_word.GetState()) {
1137 case LockWord::kHashCode:
1138 // Fall-through.
1139 case LockWord::kUnlocked:
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001140 FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001141 return false; // Failure.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001142 case LockWord::kThinLocked: {
1143 uint32_t thread_id = self->GetThreadId();
1144 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1145 if (owner_thread_id != thread_id) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001146 FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001147 return false; // Failure.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001148 } else {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001149 // We own the lock, decrease the recursion count.
1150 LockWord new_lw = LockWord::Default();
1151 if (lock_word.ThinLockCount() != 0) {
1152 uint32_t new_count = lock_word.ThinLockCount() - 1;
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001153 new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001154 } else {
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001155 new_lw = LockWord::FromDefault(lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001156 }
1157 if (!kUseReadBarrier) {
1158 DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
Hans Boehmb3da36c2016-12-15 13:12:59 -08001159 // TODO: This really only needs memory_order_release, but we currently have
1160 // no way to specify that. In fact there seem to be no legitimate uses of SetLockWord
1161 // with a final argument of true. This slows down x86 and ARMv7, but probably not v8.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001162 h_obj->SetLockWord(new_lw, true);
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001163 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001164 // Success!
1165 return true;
1166 } else {
1167 // Use CAS to preserve the read barrier state.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001168 if (h_obj->CasLockWordWeakRelease(lock_word, new_lw)) {
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001169 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001170 // Success!
1171 return true;
1172 }
1173 }
1174 continue; // Go again.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001175 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001176 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001177 case LockWord::kFatLocked: {
1178 Monitor* mon = lock_word.FatLockMonitor();
1179 return mon->Unlock(self);
1180 }
1181 default: {
1182 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1183 return false;
1184 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001185 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001186 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001187}
1188
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001189void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
Elliott Hughes4cd121e2013-01-07 17:35:41 -08001190 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001191 DCHECK(self != nullptr);
1192 DCHECK(obj != nullptr);
Alex Light77fee872017-09-05 14:51:49 -07001193 StackHandleScope<1> hs(self);
1194 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
1195
1196 Runtime::Current()->GetRuntimeCallbacks()->ObjectWaitStart(h_obj, ms);
Alex Light848574c2017-09-25 16:59:39 -07001197 if (UNLIKELY(self->ObserveAsyncException() || self->IsExceptionPending())) {
Alex Light77fee872017-09-05 14:51:49 -07001198 // See b/65558434 for information on handling of exceptions here.
1199 return;
1200 }
1201
1202 LockWord lock_word = h_obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -07001203 while (lock_word.GetState() != LockWord::kFatLocked) {
1204 switch (lock_word.GetState()) {
1205 case LockWord::kHashCode:
1206 // Fall-through.
1207 case LockWord::kUnlocked:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001208 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1209 return; // Failure.
Ian Rogers43c69cc2014-08-15 11:09:28 -07001210 case LockWord::kThinLocked: {
1211 uint32_t thread_id = self->GetThreadId();
1212 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1213 if (owner_thread_id != thread_id) {
1214 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1215 return; // Failure.
1216 } else {
1217 // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
1218 // re-load.
Alex Light77fee872017-09-05 14:51:49 -07001219 Inflate(self, self, h_obj.Get(), 0);
1220 lock_word = h_obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -07001221 }
1222 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001223 }
Ian Rogers43c69cc2014-08-15 11:09:28 -07001224 case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through.
1225 default: {
1226 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1227 return;
1228 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001229 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001230 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001231 Monitor* mon = lock_word.FatLockMonitor();
1232 mon->Wait(self, ms, ns, interruptShouldThrow, why);
Elliott Hughes5f791332011-09-15 17:45:30 -07001233}
1234
Ian Rogers13c479e2013-10-11 07:59:01 -07001235void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001236 DCHECK(self != nullptr);
1237 DCHECK(obj != nullptr);
1238 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001239 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001240 case LockWord::kHashCode:
1241 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001242 case LockWord::kUnlocked:
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001243 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001244 return; // Failure.
1245 case LockWord::kThinLocked: {
1246 uint32_t thread_id = self->GetThreadId();
1247 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1248 if (owner_thread_id != thread_id) {
1249 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
1250 return; // Failure.
1251 } else {
1252 // We own the lock but there's no Monitor and therefore no waiters.
1253 return; // Success.
1254 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001255 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001256 case LockWord::kFatLocked: {
1257 Monitor* mon = lock_word.FatLockMonitor();
1258 if (notify_all) {
1259 mon->NotifyAll(self);
1260 } else {
1261 mon->Notify(self);
1262 }
1263 return; // Success.
1264 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001265 default: {
1266 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1267 return;
1268 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001269 }
1270}
1271
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001272uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001273 DCHECK(obj != nullptr);
1274 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001275 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001276 case LockWord::kHashCode:
1277 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001278 case LockWord::kUnlocked:
1279 return ThreadList::kInvalidThreadId;
1280 case LockWord::kThinLocked:
1281 return lock_word.ThinLockOwner();
1282 case LockWord::kFatLocked: {
1283 Monitor* mon = lock_word.FatLockMonitor();
1284 return mon->GetOwnerThreadId();
Elliott Hughes5f791332011-09-15 17:45:30 -07001285 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001286 default: {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001287 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001288 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001289 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001290 }
1291}
1292
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001293ThreadState Monitor::FetchState(const Thread* thread,
1294 /* out */ mirror::Object** monitor_object,
1295 /* out */ uint32_t* lock_owner_tid) {
1296 DCHECK(monitor_object != nullptr);
1297 DCHECK(lock_owner_tid != nullptr);
1298
1299 *monitor_object = nullptr;
1300 *lock_owner_tid = ThreadList::kInvalidThreadId;
1301
1302 ThreadState state = thread->GetState();
1303
1304 switch (state) {
1305 case kWaiting:
1306 case kTimedWaiting:
1307 case kSleeping:
1308 {
1309 Thread* self = Thread::Current();
1310 MutexLock mu(self, *thread->GetWaitMutex());
1311 Monitor* monitor = thread->GetWaitMonitor();
1312 if (monitor != nullptr) {
1313 *monitor_object = monitor->GetObject();
Ian Rogersd803bc72014-04-01 15:33:03 -07001314 }
1315 }
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001316 break;
1317
1318 case kBlocked:
1319 case kWaitingForLockInflation:
1320 {
1321 mirror::Object* lock_object = thread->GetMonitorEnterObject();
1322 if (lock_object != nullptr) {
1323 if (kUseReadBarrier && Thread::Current()->GetIsGcMarking()) {
1324 // We may call Thread::Dump() in the middle of the CC thread flip and this thread's stack
1325 // may have not been flipped yet and "pretty_object" may be a from-space (stale) ref, in
1326 // which case the GetLockOwnerThreadId() call below will crash. So explicitly mark/forward
1327 // it here.
1328 lock_object = ReadBarrier::Mark(lock_object);
1329 }
1330 *monitor_object = lock_object;
1331 *lock_owner_tid = lock_object->GetLockOwnerThreadId();
1332 }
Ian Rogersd803bc72014-04-01 15:33:03 -07001333 }
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001334 break;
1335
1336 default:
1337 break;
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001338 }
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001339
1340 return state;
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001341}
1342
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001343mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001344 // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
1345 // definition of contended that includes a monitor a thread is trying to enter...
Ian Rogersdd7624d2014-03-14 17:43:00 -07001346 mirror::Object* result = thread->GetMonitorEnterObject();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001347 if (result == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001348 // ...but also a monitor that the thread is waiting on.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001349 MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
1350 Monitor* monitor = thread->GetWaitMonitor();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001351 if (monitor != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001352 result = monitor->GetObject();
Elliott Hughesf9501702013-01-11 11:22:27 -08001353 }
1354 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001355 return result;
Elliott Hughesf9501702013-01-11 11:22:27 -08001356}
1357
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001358void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
Andreas Gampe760172c2014-08-16 13:41:10 -07001359 void* callback_context, bool abort_on_failure) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001360 ArtMethod* m = stack_visitor->GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001361 CHECK(m != nullptr);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001362
1363 // Native methods are an easy special case.
1364 // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
1365 if (m->IsNative()) {
1366 if (m->IsSynchronized()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001367 mirror::Object* jni_this =
1368 stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001369 callback(jni_this, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001370 }
1371 return;
1372 }
1373
jeffhao61f916c2012-10-25 17:48:51 -07001374 // Proxy methods should not be synchronized.
1375 if (m->IsProxyMethod()) {
1376 CHECK(!m->IsSynchronized());
1377 return;
1378 }
1379
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001380 // Is there any reason to believe there's any synchronization in this method?
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001381 CHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +00001382 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001383 if (accessor.TriesSize() == 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001384 return; // No "tries" implies no synchronization, so no held locks to report.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001385 }
1386
Andreas Gampe760172c2014-08-16 13:41:10 -07001387 // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
1388 // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
1389 // inconsistent stack anyways.
1390 uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
Andreas Gampee2abbc62017-09-15 11:59:26 -07001391 if (!abort_on_failure && dex_pc == dex::kDexNoIndex) {
David Sehr709b0702016-10-13 09:12:37 -07001392 LOG(ERROR) << "Could not find dex_pc for " << m->PrettyMethod();
Andreas Gampe760172c2014-08-16 13:41:10 -07001393 return;
1394 }
1395
Elliott Hughes80537bb2013-01-04 16:37:26 -08001396 // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
1397 // the locks held in this stack frame.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001398 std::vector<verifier::MethodVerifier::DexLockInfo> monitor_enter_dex_pcs;
Andreas Gampe760172c2014-08-16 13:41:10 -07001399 verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs);
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001400 for (verifier::MethodVerifier::DexLockInfo& dex_lock_info : monitor_enter_dex_pcs) {
1401 // As a debug check, check that dex PC corresponds to a monitor-enter.
1402 if (kIsDebugBuild) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001403 const Instruction& monitor_enter_instruction = accessor.InstructionAt(dex_lock_info.dex_pc);
1404 CHECK_EQ(monitor_enter_instruction.Opcode(), Instruction::MONITOR_ENTER)
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001405 << "expected monitor-enter @" << dex_lock_info.dex_pc << "; was "
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001406 << reinterpret_cast<const void*>(&monitor_enter_instruction);
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001407 }
Elliott Hughes80537bb2013-01-04 16:37:26 -08001408
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001409 // Iterate through the set of dex registers, as the compiler may not have held all of them
1410 // live.
1411 bool success = false;
1412 for (uint32_t dex_reg : dex_lock_info.dex_registers) {
1413 uint32_t value;
1414 success = stack_visitor->GetVReg(m, dex_reg, kReferenceVReg, &value);
1415 if (success) {
1416 mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
1417 callback(o, callback_context);
1418 break;
1419 }
1420 }
1421 DCHECK(success) << "Failed to find/read reference for monitor-enter at dex pc "
1422 << dex_lock_info.dex_pc
1423 << " in method "
1424 << m->PrettyMethod();
1425 if (!success) {
1426 LOG(WARNING) << "Had a lock reported for dex pc " << dex_lock_info.dex_pc
1427 << " but was not able to fetch a corresponding object!";
1428 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001429 }
1430}
1431
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001432bool Monitor::IsValidLockWord(LockWord lock_word) {
1433 switch (lock_word.GetState()) {
1434 case LockWord::kUnlocked:
1435 // Nothing to check.
1436 return true;
1437 case LockWord::kThinLocked:
1438 // Basic sanity check of owner.
1439 return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1440 case LockWord::kFatLocked: {
1441 // Check the monitor appears in the monitor list.
1442 Monitor* mon = lock_word.FatLockMonitor();
1443 MonitorList* list = Runtime::Current()->GetMonitorList();
1444 MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1445 for (Monitor* list_mon : list->list_) {
1446 if (mon == list_mon) {
1447 return true; // Found our monitor.
1448 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001449 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001450 return false; // Fail - unowned monitor in an object.
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001451 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001452 case LockWord::kHashCode:
1453 return true;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001454 default:
1455 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001456 UNREACHABLE();
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001457 }
1458}
1459
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001460bool Monitor::IsLocked() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001461 MutexLock mu(Thread::Current(), monitor_lock_);
1462 return owner_ != nullptr;
1463}
1464
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -07001465void Monitor::TranslateLocation(ArtMethod* method,
1466 uint32_t dex_pc,
1467 const char** source_file,
1468 int32_t* line_number) {
jeffhao33dc7712011-11-09 17:54:24 -08001469 // If method is null, location is unknown
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001470 if (method == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001471 *source_file = "";
1472 *line_number = 0;
jeffhao33dc7712011-11-09 17:54:24 -08001473 return;
1474 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001475 *source_file = method->GetDeclaringClassSourceFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001476 if (*source_file == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001477 *source_file = "";
Elliott Hughes12c51e32012-01-17 20:25:05 -08001478 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001479 *line_number = method->GetLineNumFromDexPC(dex_pc);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001480}
1481
1482uint32_t Monitor::GetOwnerThreadId() {
1483 MutexLock mu(Thread::Current(), monitor_lock_);
1484 Thread* owner = owner_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001485 if (owner != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001486 return owner->GetThreadId();
1487 } else {
1488 return ThreadList::kInvalidThreadId;
1489 }
jeffhao33dc7712011-11-09 17:54:24 -08001490}
1491
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001492MonitorList::MonitorList()
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001493 : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001494 monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001495}
1496
1497MonitorList::~MonitorList() {
Andreas Gampe74240812014-04-17 10:35:09 -07001498 Thread* self = Thread::Current();
1499 MutexLock mu(self, monitor_list_lock_);
1500 // Release all monitors to the pool.
1501 // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
1502 // clear faster in the pool.
1503 MonitorPool::ReleaseMonitors(self, &list_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001504}
1505
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001506void MonitorList::DisallowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001507 CHECK(!kUseReadBarrier);
Ian Rogers50b35e22012-10-04 10:09:15 -07001508 MutexLock mu(Thread::Current(), monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001509 allow_new_monitors_ = false;
1510}
1511
1512void MonitorList::AllowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001513 CHECK(!kUseReadBarrier);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001514 Thread* self = Thread::Current();
1515 MutexLock mu(self, monitor_list_lock_);
1516 allow_new_monitors_ = true;
1517 monitor_add_condition_.Broadcast(self);
1518}
1519
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001520void MonitorList::BroadcastForNewMonitors() {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001521 Thread* self = Thread::Current();
1522 MutexLock mu(self, monitor_list_lock_);
1523 monitor_add_condition_.Broadcast(self);
1524}
1525
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001526void MonitorList::Add(Monitor* m) {
1527 Thread* self = Thread::Current();
1528 MutexLock mu(self, monitor_list_lock_);
Hiroshi Yamauchif1c6f872017-01-06 12:23:47 -08001529 // CMS needs this to block for concurrent reference processing because an object allocated during
1530 // the GC won't be marked and concurrent reference processing would incorrectly clear the JNI weak
1531 // ref. But CC (kUseReadBarrier == true) doesn't because of the to-space invariant.
1532 while (!kUseReadBarrier && UNLIKELY(!allow_new_monitors_)) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -07001533 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
1534 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -08001535 self->CheckEmptyCheckpointFromWeakRefAccess(&monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001536 monitor_add_condition_.WaitHoldingLocks(self);
1537 }
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001538 list_.push_front(m);
1539}
1540
Mathieu Chartier97509952015-07-13 14:35:43 -07001541void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
Andreas Gampe74240812014-04-17 10:35:09 -07001542 Thread* self = Thread::Current();
1543 MutexLock mu(self, monitor_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001544 for (auto it = list_.begin(); it != list_.end(); ) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001545 Monitor* m = *it;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001546 // Disable the read barrier in GetObject() as this is called by GC.
1547 mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001548 // The object of a monitor can be null if we have deflated it.
Mathieu Chartier97509952015-07-13 14:35:43 -07001549 mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001550 if (new_obj == nullptr) {
1551 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001552 << obj;
Andreas Gampe74240812014-04-17 10:35:09 -07001553 MonitorPool::ReleaseMonitor(self, m);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001554 it = list_.erase(it);
1555 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001556 m->SetObject(new_obj);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001557 ++it;
1558 }
1559 }
1560}
1561
Hans Boehm6fe97e02016-05-04 18:35:57 -07001562size_t MonitorList::Size() {
1563 Thread* self = Thread::Current();
1564 MutexLock mu(self, monitor_list_lock_);
1565 return list_.size();
1566}
1567
Mathieu Chartier97509952015-07-13 14:35:43 -07001568class MonitorDeflateVisitor : public IsMarkedVisitor {
1569 public:
1570 MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
1571
1572 virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001573 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -07001574 if (Monitor::Deflate(self_, object)) {
1575 DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1576 ++deflate_count_;
1577 // If we deflated, return null so that the monitor gets removed from the array.
1578 return nullptr;
1579 }
1580 return object; // Monitor was not deflated.
1581 }
1582
1583 Thread* const self_;
1584 size_t deflate_count_;
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001585};
1586
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001587size_t MonitorList::DeflateMonitors() {
Mathieu Chartier97509952015-07-13 14:35:43 -07001588 MonitorDeflateVisitor visitor;
1589 Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
1590 SweepMonitorList(&visitor);
1591 return visitor.deflate_count_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001592}
1593
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001594MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001595 DCHECK(obj != nullptr);
1596 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001597 switch (lock_word.GetState()) {
1598 case LockWord::kUnlocked:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001599 // Fall-through.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001600 case LockWord::kForwardingAddress:
1601 // Fall-through.
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001602 case LockWord::kHashCode:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001603 break;
1604 case LockWord::kThinLocked:
1605 owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
Alex Lightce568642017-09-05 16:54:25 -07001606 DCHECK(owner_ != nullptr) << "Thin-locked without owner!";
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001607 entry_count_ = 1 + lock_word.ThinLockCount();
1608 // Thin locks have no waiters.
1609 break;
1610 case LockWord::kFatLocked: {
1611 Monitor* mon = lock_word.FatLockMonitor();
1612 owner_ = mon->owner_;
Alex Lightce568642017-09-05 16:54:25 -07001613 // Here it is okay for the owner to be null since we don't reset the LockWord back to
1614 // kUnlocked until we get a GC. In cases where this hasn't happened yet we will have a fat
1615 // lock without an owner.
1616 if (owner_ != nullptr) {
1617 entry_count_ = 1 + mon->lock_count_;
1618 } else {
1619 DCHECK_EQ(mon->lock_count_, 0) << "Monitor is fat-locked without any owner!";
1620 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001621 for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001622 waiters_.push_back(waiter);
1623 }
1624 break;
Elliott Hughesf327e072013-01-09 16:01:26 -08001625 }
1626 }
1627}
1628
Elliott Hughes5f791332011-09-15 17:45:30 -07001629} // namespace art