blob: 6abc8d7f8c317e86740c786af8a1053682de4a0e [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
Daniel Colascionec3d5b842018-04-15 10:52:18 -070062 * from the "thin" state to the "fat" state and this transition is referred to as inflation. We
63 * deflate locks from time to time as part of heap trimming.
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),
Charles Mungerc665d632018-11-06 16:20:13 +0000100 wake_set_(nullptr),
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700101 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700102 locking_method_(nullptr),
Ian Rogersef7d42f2014-01-06 12:55:46 -0800103 locking_dex_pc_(0),
Andreas Gampe74240812014-04-17 10:35:09 -0700104 monitor_id_(MonitorPool::ComputeMonitorId(this, self)) {
105#ifdef __LP64__
106 DCHECK(false) << "Should not be reached in 64b";
107 next_free_ = nullptr;
108#endif
109 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
110 // with the owner unlocking the thin-lock.
111 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
112 // The identity hash code is set for the life time of the monitor.
113}
114
115Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code,
116 MonitorId id)
117 : monitor_lock_("a monitor lock", kMonitorLock),
118 monitor_contenders_("monitor contenders", monitor_lock_),
119 num_waiters_(0),
120 owner_(owner),
121 lock_count_(0),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700122 obj_(GcRoot<mirror::Object>(obj)),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700123 wait_set_(nullptr),
Charles Mungerc665d632018-11-06 16:20:13 +0000124 wake_set_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700125 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700126 locking_method_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700127 locking_dex_pc_(0),
128 monitor_id_(id) {
129#ifdef __LP64__
130 next_free_ = nullptr;
131#endif
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700132 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
133 // with the owner unlocking the thin-lock.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700135 // The identity hash code is set for the life time of the monitor.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700136}
137
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700138int32_t Monitor::GetHashCode() {
Mathieu Chartier8bb3c682018-06-18 12:53:10 -0700139 int32_t hc = hash_code_.load(std::memory_order_relaxed);
140 if (!HasHashCode()) {
141 // Use a strong CAS to prevent spurious failures since these can make the boot image
142 // non-deterministic.
143 hash_code_.CompareAndSetStrongRelaxed(0, mirror::Object::GenerateIdentityHashCode());
144 hc = hash_code_.load(std::memory_order_relaxed);
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700145 }
146 DCHECK(HasHashCode());
Mathieu Chartier8bb3c682018-06-18 12:53:10 -0700147 return hc;
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700148}
149
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700150bool Monitor::Install(Thread* self) {
151 MutexLock mu(self, monitor_lock_); // Uncontended mutex acquisition as monitor isn't yet public.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700152 CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700153 // Propagate the lock state.
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -0700154 LockWord lw(GetObject()->GetLockWord(false));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700155 switch (lw.GetState()) {
156 case LockWord::kThinLocked: {
157 CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
158 lock_count_ = lw.ThinLockCount();
159 break;
160 }
161 case LockWord::kHashCode: {
Orion Hodson88591fe2018-03-06 13:35:43 +0000162 CHECK_EQ(hash_code_.load(std::memory_order_relaxed), static_cast<int32_t>(lw.GetHashCode()));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700163 break;
164 }
165 case LockWord::kFatLocked: {
166 // The owner_ is suspended but another thread beat us to install a monitor.
167 return false;
168 }
169 case LockWord::kUnlocked: {
170 LOG(FATAL) << "Inflating unlocked lock word";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800171 UNREACHABLE();
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700172 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700173 default: {
174 LOG(FATAL) << "Invalid monitor state " << lw.GetState();
Elliott Hughesc1896c92018-11-29 11:33:18 -0800175 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700176 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700177 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700178 LockWord fat(this, lw.GCState());
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700179 // Publish the updated lock word, which may race with other threads.
Mathieu Chartier42c2e502018-06-19 12:30:56 -0700180 bool success = GetObject()->CasLockWord(lw, fat, CASMode::kWeak, std::memory_order_release);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700181 // Lock profiling.
Mathieu Chartier9728f912013-10-30 09:45:13 -0700182 if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
Andreas Gampe6ec8ebd2014-07-25 13:36:56 -0700183 // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on
184 // abort.
185 locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false);
Andreas Gampe5a387272017-11-06 19:47:16 -0800186 if (locking_method_ != nullptr && UNLIKELY(locking_method_->IsProxyMethod())) {
187 // Grab another frame. Proxy methods are not helpful for lock profiling. This should be rare
188 // enough that it's OK to walk the stack twice.
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100189 struct NextMethodVisitor final : public StackVisitor {
Andreas Gampe5a387272017-11-06 19:47:16 -0800190 explicit NextMethodVisitor(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_)
191 : StackVisitor(thread,
192 nullptr,
193 StackVisitor::StackWalkKind::kIncludeInlinedFrames,
194 false),
195 count_(0),
196 method_(nullptr),
197 dex_pc_(0) {}
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100198 bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5a387272017-11-06 19:47:16 -0800199 ArtMethod* m = GetMethod();
200 if (m->IsRuntimeMethod()) {
201 // Continue if this is a runtime method.
202 return true;
203 }
204 count_++;
205 if (count_ == 2u) {
206 method_ = m;
207 dex_pc_ = GetDexPc(false);
208 return false;
209 }
210 return true;
211 }
212 size_t count_;
213 ArtMethod* method_;
214 uint32_t dex_pc_;
215 };
216 NextMethodVisitor nmv(owner_);
217 nmv.WalkStack();
218 locking_method_ = nmv.method_;
219 locking_dex_pc_ = nmv.dex_pc_;
220 }
221 DCHECK(locking_method_ == nullptr || !locking_method_->IsProxyMethod());
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700222 }
223 return success;
Elliott Hughes5f791332011-09-15 17:45:30 -0700224}
225
226Monitor::~Monitor() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700227 // Deflated monitors have a null object.
Elliott Hughes5f791332011-09-15 17:45:30 -0700228}
229
Elliott Hughes5f791332011-09-15 17:45:30 -0700230void Monitor::AppendToWaitSet(Thread* thread) {
Charles Mungerc665d632018-11-06 16:20:13 +0000231 // Not checking that the owner is equal to this thread, since we've released
232 // the monitor by the time this method is called.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700233 DCHECK(thread != nullptr);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700234 DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700235 if (wait_set_ == nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700236 wait_set_ = thread;
237 return;
238 }
239
240 // push_back.
241 Thread* t = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700242 while (t->GetWaitNext() != nullptr) {
243 t = t->GetWaitNext();
Elliott Hughes5f791332011-09-15 17:45:30 -0700244 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700245 t->SetWaitNext(thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700246}
247
Elliott Hughes5f791332011-09-15 17:45:30 -0700248void Monitor::RemoveFromWaitSet(Thread *thread) {
249 DCHECK(owner_ == Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700250 DCHECK(thread != nullptr);
Charles Mungerc665d632018-11-06 16:20:13 +0000251 auto remove = [&](Thread*& set){
252 if (set != nullptr) {
253 if (set == thread) {
254 set = thread->GetWaitNext();
255 thread->SetWaitNext(nullptr);
256 return true;
257 }
258 Thread* t = set;
259 while (t->GetWaitNext() != nullptr) {
260 if (t->GetWaitNext() == thread) {
261 t->SetWaitNext(thread->GetWaitNext());
262 thread->SetWaitNext(nullptr);
263 return true;
264 }
265 t = t->GetWaitNext();
266 }
Roland Levillain9cec9652018-11-06 10:50:20 +0000267 }
Charles Mungerc665d632018-11-06 16:20:13 +0000268 return false;
269 };
270 if (remove(wait_set_)) {
271 return;
Roland Levillain9cec9652018-11-06 10:50:20 +0000272 }
Charles Mungerc665d632018-11-06 16:20:13 +0000273 remove(wake_set_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700274}
275
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700276void Monitor::SetObject(mirror::Object* object) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700277 obj_ = GcRoot<mirror::Object>(object);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700278}
279
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700280// This function is inlined and just helps to not have the VLOG and ATRACE check at all the
281// potential tracing points.
282void Monitor::AtraceMonitorLock(Thread* self, mirror::Object* obj, bool is_wait) {
Orion Hodson119733d2019-01-30 15:14:41 +0000283 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging) && ATraceEnabled())) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700284 AtraceMonitorLockImpl(self, obj, is_wait);
285 }
286}
287
288void Monitor::AtraceMonitorLockImpl(Thread* self, mirror::Object* obj, bool is_wait) {
289 // Wait() requires a deeper call stack to be useful. Otherwise you'll see "Waiting at
290 // Object.java". Assume that we'll wait a nontrivial amount, so it's OK to do a longer
291 // stack walk than if !is_wait.
Andreas Gampec7d878d2018-11-19 18:42:06 +0000292 const size_t wanted_frame_number = is_wait ? 1U : 0U;
293
294 ArtMethod* method = nullptr;
295 uint32_t dex_pc = 0u;
296
297 size_t current_frame_number = 0u;
298 StackVisitor::WalkStack(
299 // Note: Adapted from CurrentMethodVisitor in thread.cc. We must not resolve here.
300 [&](const art::StackVisitor* stack_visitor) REQUIRES_SHARED(Locks::mutator_lock_) {
301 ArtMethod* m = stack_visitor->GetMethod();
302 if (m == nullptr || m->IsRuntimeMethod()) {
303 // Runtime method, upcall, or resolution issue. Skip.
304 return true;
305 }
306
307 // Is this the requested frame?
308 if (current_frame_number == wanted_frame_number) {
309 method = m;
310 dex_pc = stack_visitor->GetDexPc(false /* abort_on_error*/);
311 return false;
312 }
313
314 // Look for more.
315 current_frame_number++;
316 return true;
317 },
318 self,
319 /* context= */ nullptr,
320 art::StackVisitor::StackWalkKind::kIncludeInlinedFrames);
321
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700322 const char* prefix = is_wait ? "Waiting on " : "Locking ";
323
324 const char* filename;
325 int32_t line_number;
Andreas Gampec7d878d2018-11-19 18:42:06 +0000326 TranslateLocation(method, dex_pc, &filename, &line_number);
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700327
328 // It would be nice to have a stable "ID" for the object here. However, the only stable thing
329 // would be the identity hashcode. But we cannot use IdentityHashcode here: For one, there are
330 // times when it is unsafe to make that call (see stack dumping for an explanation). More
331 // importantly, we would have to give up on thin-locking when adding systrace locks, as the
332 // identity hashcode is stored in the lockword normally (so can't be used with thin-locks).
333 //
334 // Because of thin-locks we also cannot use the monitor id (as there is no monitor). Monitor ids
335 // also do not have to be stable, as the monitor may be deflated.
336 std::string tmp = StringPrintf("%s %d at %s:%d",
337 prefix,
338 (obj == nullptr ? -1 : static_cast<int32_t>(reinterpret_cast<uintptr_t>(obj))),
339 (filename != nullptr ? filename : "null"),
340 line_number);
Orion Hodson119733d2019-01-30 15:14:41 +0000341 ATraceBegin(tmp.c_str());
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700342}
343
344void Monitor::AtraceMonitorUnlock() {
345 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging))) {
Orion Hodson119733d2019-01-30 15:14:41 +0000346 ATraceEnd();
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700347 }
348}
349
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700350std::string Monitor::PrettyContentionInfo(const std::string& owner_name,
351 pid_t owner_tid,
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700352 ArtMethod* owners_method,
353 uint32_t owners_dex_pc,
354 size_t num_waiters) {
Hiroshi Yamauchi71cd68d2017-01-25 18:28:12 -0800355 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700356 const char* owners_filename;
Goran Jakovljevic49c882b2016-04-19 10:27:21 +0200357 int32_t owners_line_number = 0;
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700358 if (owners_method != nullptr) {
359 TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
360 }
361 std::ostringstream oss;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700362 oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700363 if (owners_method != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700364 oss << " at " << owners_method->PrettyMethod();
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700365 oss << "(" << owners_filename << ":" << owners_line_number << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700366 }
367 oss << " waiters=" << num_waiters;
368 return oss.str();
369}
370
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700371bool Monitor::TryLockLocked(Thread* self) {
372 if (owner_ == nullptr) { // Unowned.
373 owner_ = self;
374 CHECK_EQ(lock_count_, 0);
375 // When debugging, save the current monitor holder for future
376 // acquisition failures to use in sampled logging.
377 if (lock_profiling_threshold_ != 0) {
378 locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
Andreas Gampe5a387272017-11-06 19:47:16 -0800379 // We don't expect a proxy method here.
380 DCHECK(locking_method_ == nullptr || !locking_method_->IsProxyMethod());
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700381 }
382 } else if (owner_ == self) { // Recursive.
383 lock_count_++;
384 } else {
385 return false;
386 }
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700387 AtraceMonitorLock(self, GetObject(), /* is_wait= */ false);
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700388 return true;
389}
390
391bool Monitor::TryLock(Thread* self) {
392 MutexLock mu(self, monitor_lock_);
393 return TryLockLocked(self);
394}
395
Alex Light77fee872017-09-05 14:51:49 -0700396// Asserts that a mutex isn't held when the class comes into and out of scope.
397class ScopedAssertNotHeld {
398 public:
399 ScopedAssertNotHeld(Thread* self, Mutex& mu) : self_(self), mu_(mu) {
400 mu_.AssertNotHeld(self_);
401 }
402
403 ~ScopedAssertNotHeld() {
404 mu_.AssertNotHeld(self_);
405 }
406
407 private:
408 Thread* const self_;
409 Mutex& mu_;
410 DISALLOW_COPY_AND_ASSIGN(ScopedAssertNotHeld);
411};
412
413template <LockReason reason>
Elliott Hughes5f791332011-09-15 17:45:30 -0700414void Monitor::Lock(Thread* self) {
Alex Light77fee872017-09-05 14:51:49 -0700415 ScopedAssertNotHeld sanh(self, monitor_lock_);
416 bool called_monitors_callback = false;
417 monitor_lock_.Lock(self);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700418 while (true) {
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700419 if (TryLockLocked(self)) {
Alex Light77fee872017-09-05 14:51:49 -0700420 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700421 }
422 // Contended.
423 const bool log_contention = (lock_profiling_threshold_ != 0);
Xin Guanb894a192014-08-22 11:55:37 -0500424 uint64_t wait_start_ms = log_contention ? MilliTime() : 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700425 ArtMethod* owners_method = locking_method_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700426 uint32_t owners_dex_pc = locking_dex_pc_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700427 // Do this before releasing the lock so that we don't get deflated.
Mathieu Chartierb9001ab2014-10-03 13:28:46 -0700428 size_t num_waiters = num_waiters_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700429 ++num_waiters_;
Andreas Gampe2702d562017-02-06 09:48:00 -0800430
431 // If systrace logging is enabled, first look at the lock owner. Acquiring the monitor's
432 // lock and then re-acquiring the mutator lock can deadlock.
433 bool started_trace = false;
Orion Hodson119733d2019-01-30 15:14:41 +0000434 if (ATraceEnabled()) {
Andreas Gampe2702d562017-02-06 09:48:00 -0800435 if (owner_ != nullptr) { // Did the owner_ give the lock up?
436 std::ostringstream oss;
437 std::string name;
438 owner_->GetThreadName(name);
439 oss << PrettyContentionInfo(name,
440 owner_->GetTid(),
441 owners_method,
442 owners_dex_pc,
443 num_waiters);
444 // Add info for contending thread.
445 uint32_t pc;
446 ArtMethod* m = self->GetCurrentMethod(&pc);
447 const char* filename;
448 int32_t line_number;
449 TranslateLocation(m, pc, &filename, &line_number);
450 oss << " blocking from "
451 << ArtMethod::PrettyMethod(m) << "(" << (filename != nullptr ? filename : "null")
452 << ":" << line_number << ")";
Orion Hodson119733d2019-01-30 15:14:41 +0000453 ATraceBegin(oss.str().c_str());
Andreas Gampe2702d562017-02-06 09:48:00 -0800454 started_trace = true;
455 }
456 }
457
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700458 monitor_lock_.Unlock(self); // Let go of locks in order.
Alex Light77fee872017-09-05 14:51:49 -0700459 // Call the contended locking cb once and only once. Also only call it if we are locking for
460 // the first time, not during a Wait wakeup.
461 if (reason == LockReason::kForLock && !called_monitors_callback) {
462 called_monitors_callback = true;
463 Runtime::Current()->GetRuntimeCallbacks()->MonitorContendedLocking(this);
464 }
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700465 self->SetMonitorEnterObject(GetObject());
Elliott Hughes5f791332011-09-15 17:45:30 -0700466 {
Hiroshi Yamauchi71cd68d2017-01-25 18:28:12 -0800467 ScopedThreadSuspension tsc(self, kBlocked); // Change to blocked and give up mutator_lock_.
Andreas Gampe2702d562017-02-06 09:48:00 -0800468 uint32_t original_owner_thread_id = 0u;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700469 {
470 // Reacquire monitor_lock_ without mutator_lock_ for Wait.
471 MutexLock mu2(self, monitor_lock_);
472 if (owner_ != nullptr) { // Did the owner_ give the lock up?
473 original_owner_thread_id = owner_->GetThreadId();
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700474 monitor_contenders_.Wait(self); // Still contended so wait.
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800475 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700476 }
477 if (original_owner_thread_id != 0u) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700478 // Woken from contention.
479 if (log_contention) {
Andreas Gampe111b1092017-06-22 20:28:23 -0700480 uint64_t wait_ms = MilliTime() - wait_start_ms;
481 uint32_t sample_percent;
482 if (wait_ms >= lock_profiling_threshold_) {
483 sample_percent = 100;
484 } else {
485 sample_percent = 100 * wait_ms / lock_profiling_threshold_;
486 }
487 if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
488 // Reacquire mutator_lock_ for logging.
489 ScopedObjectAccess soa(self);
Andreas Gampe111b1092017-06-22 20:28:23 -0700490
Andreas Gamped0210e52017-06-23 13:38:09 -0700491 bool owner_alive = false;
492 pid_t original_owner_tid = 0;
493 std::string original_owner_name;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700494
Andreas Gamped0210e52017-06-23 13:38:09 -0700495 const bool should_dump_stacks = stack_dump_lock_profiling_threshold_ > 0 &&
496 wait_ms > stack_dump_lock_profiling_threshold_;
497 std::string owner_stack_dump;
Andreas Gampe111b1092017-06-22 20:28:23 -0700498
Andreas Gamped0210e52017-06-23 13:38:09 -0700499 // Acquire thread-list lock to find thread and keep it from dying until we've got all
500 // the info we need.
501 {
Alex Lightb1e31a82017-10-04 16:57:36 -0700502 Locks::thread_list_lock_->ExclusiveLock(Thread::Current());
Andreas Gamped0210e52017-06-23 13:38:09 -0700503
504 // Re-find the owner in case the thread got killed.
505 Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId(
506 original_owner_thread_id);
507
508 if (original_owner != nullptr) {
509 owner_alive = true;
510 original_owner_tid = original_owner->GetTid();
511 original_owner->GetThreadName(original_owner_name);
512
513 if (should_dump_stacks) {
514 // Very long contention. Dump stacks.
515 struct CollectStackTrace : public Closure {
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100516 void Run(art::Thread* thread) override
Andreas Gamped0210e52017-06-23 13:38:09 -0700517 REQUIRES_SHARED(art::Locks::mutator_lock_) {
518 thread->DumpJavaStack(oss);
519 }
520
521 std::ostringstream oss;
522 };
523 CollectStackTrace owner_trace;
Alex Lightb1e31a82017-10-04 16:57:36 -0700524 // RequestSynchronousCheckpoint releases the thread_list_lock_ as a part of its
525 // execution.
Andreas Gamped0210e52017-06-23 13:38:09 -0700526 original_owner->RequestSynchronousCheckpoint(&owner_trace);
527 owner_stack_dump = owner_trace.oss.str();
Alex Lightb1e31a82017-10-04 16:57:36 -0700528 } else {
529 Locks::thread_list_lock_->ExclusiveUnlock(Thread::Current());
Andreas Gamped0210e52017-06-23 13:38:09 -0700530 }
Alex Lightb1e31a82017-10-04 16:57:36 -0700531 } else {
532 Locks::thread_list_lock_->ExclusiveUnlock(Thread::Current());
Andreas Gamped0210e52017-06-23 13:38:09 -0700533 }
534 // This is all the data we need. Now drop the thread-list lock, it's OK for the
535 // owner to go away now.
536 }
537
538 // If we found the owner (and thus have owner data), go and log now.
539 if (owner_alive) {
540 // Give the detailed traces for really long contention.
541 if (should_dump_stacks) {
542 // This must be here (and not above) because we cannot hold the thread-list lock
543 // while running the checkpoint.
544 std::ostringstream self_trace_oss;
545 self->DumpJavaStack(self_trace_oss);
546
547 uint32_t pc;
548 ArtMethod* m = self->GetCurrentMethod(&pc);
549
550 LOG(WARNING) << "Long "
551 << PrettyContentionInfo(original_owner_name,
552 original_owner_tid,
553 owners_method,
554 owners_dex_pc,
555 num_waiters)
556 << " in " << ArtMethod::PrettyMethod(m) << " for "
557 << PrettyDuration(MsToNs(wait_ms)) << "\n"
558 << "Current owner stack:\n" << owner_stack_dump
559 << "Contender stack:\n" << self_trace_oss.str();
560 } else if (wait_ms > kLongWaitMs && owners_method != nullptr) {
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700561 uint32_t pc;
562 ArtMethod* m = self->GetCurrentMethod(&pc);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700563 // TODO: We should maybe check that original_owner is still a live thread.
564 LOG(WARNING) << "Long "
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700565 << PrettyContentionInfo(original_owner_name,
566 original_owner_tid,
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700567 owners_method,
568 owners_dex_pc,
569 num_waiters)
David Sehr709b0702016-10-13 09:12:37 -0700570 << " in " << ArtMethod::PrettyMethod(m) << " for "
571 << PrettyDuration(MsToNs(wait_ms));
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700572 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700573 LogContentionEvent(self,
Alex Light77fee872017-09-05 14:51:49 -0700574 wait_ms,
575 sample_percent,
576 owners_method,
577 owners_dex_pc);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700578 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700579 }
580 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700581 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700582 }
Andreas Gampe2702d562017-02-06 09:48:00 -0800583 if (started_trace) {
Orion Hodson119733d2019-01-30 15:14:41 +0000584 ATraceEnd();
Andreas Gampe2702d562017-02-06 09:48:00 -0800585 }
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700586 self->SetMonitorEnterObject(nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700587 monitor_lock_.Lock(self); // Reacquire locks in order.
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700588 --num_waiters_;
Elliott Hughesfc861622011-10-17 17:57:47 -0700589 }
Alex Light77fee872017-09-05 14:51:49 -0700590 monitor_lock_.Unlock(self);
591 // We need to pair this with a single contended locking call. NB we match the RI behavior and call
592 // this even if MonitorEnter failed.
593 if (called_monitors_callback) {
594 CHECK(reason == LockReason::kForLock);
595 Runtime::Current()->GetRuntimeCallbacks()->MonitorContendedLocked(this);
596 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700597}
598
Alex Light77fee872017-09-05 14:51:49 -0700599template void Monitor::Lock<LockReason::kForLock>(Thread* self);
600template void Monitor::Lock<LockReason::kForWait>(Thread* self);
601
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800602static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
603 __attribute__((format(printf, 1, 2)));
604
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700605static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700606 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800607 va_list args;
608 va_start(args, fmt);
Ian Rogers62d6c772013-02-27 08:32:07 -0800609 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000610 self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700611 if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
Brian Carlstrom64277f32012-03-26 23:53:34 -0700612 std::ostringstream ss;
Ian Rogers62d6c772013-02-27 08:32:07 -0800613 self->Dump(ss);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700614 LOG(Runtime::Current()->IsStarted() ? ::android::base::INFO : ::android::base::ERROR)
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000615 << self->GetException()->Dump() << "\n" << ss.str();
Brian Carlstrom64277f32012-03-26 23:53:34 -0700616 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800617 va_end(args);
618}
619
Elliott Hughesd4237412012-02-21 11:24:45 -0800620static std::string ThreadToString(Thread* thread) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700621 if (thread == nullptr) {
622 return "nullptr";
Elliott Hughesd4237412012-02-21 11:24:45 -0800623 }
624 std::ostringstream oss;
625 // TODO: alternatively, we could just return the thread's name.
626 oss << *thread;
627 return oss.str();
628}
629
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700630void Monitor::FailedUnlock(mirror::Object* o,
631 uint32_t expected_owner_thread_id,
632 uint32_t found_owner_thread_id,
Elliott Hughesffb465f2012-03-01 18:46:05 -0800633 Monitor* monitor) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700634 // Acquire thread list lock so threads won't disappear from under us.
Elliott Hughesffb465f2012-03-01 18:46:05 -0800635 std::string current_owner_string;
636 std::string expected_owner_string;
637 std::string found_owner_string;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700638 uint32_t current_owner_thread_id = 0u;
Elliott Hughesffb465f2012-03-01 18:46:05 -0800639 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700640 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700641 ThreadList* const thread_list = Runtime::Current()->GetThreadList();
642 Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id);
643 Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id);
644
Elliott Hughesffb465f2012-03-01 18:46:05 -0800645 // Re-read owner now that we hold lock.
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700646 Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr;
647 if (current_owner != nullptr) {
648 current_owner_thread_id = current_owner->GetThreadId();
649 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800650 // Get short descriptions of the threads involved.
651 current_owner_string = ThreadToString(current_owner);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700652 expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed";
653 found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed";
Elliott Hughesffb465f2012-03-01 18:46:05 -0800654 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700655
656 if (current_owner_thread_id == 0u) {
657 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800658 ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
659 " on thread '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700660 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800661 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800662 } else {
663 // Race: the original read found an owner but now there is none
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800664 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
665 " (where now the monitor appears unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800666 found_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700667 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800668 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800669 }
670 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700671 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800672 // Race: originally there was no owner, there is now
673 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
674 " (originally believed to be unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800675 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700676 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800677 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800678 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700679 if (found_owner_thread_id != current_owner_thread_id) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800680 // Race: originally found and current owner have changed
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800681 ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
682 " owned by '%s') on object of type '%s' on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800683 found_owner_string.c_str(),
684 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700685 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800686 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800687 } else {
688 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
689 " on thread '%s",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800690 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700691 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800692 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800693 }
694 }
695 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700696}
697
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700698bool Monitor::Unlock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700699 DCHECK(self != nullptr);
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700700 uint32_t owner_thread_id = 0u;
Charles Mungerc665d632018-11-06 16:20:13 +0000701 DCHECK(!monitor_lock_.IsExclusiveHeld(self));
702 monitor_lock_.Lock(self);
703 Thread* owner = owner_;
704 if (owner != nullptr) {
705 owner_thread_id = owner->GetThreadId();
706 }
707 if (owner == self) {
708 // We own the monitor, so nobody else can be in here.
709 AtraceMonitorUnlock();
710 if (lock_count_ == 0) {
711 owner_ = nullptr;
712 locking_method_ = nullptr;
713 locking_dex_pc_ = 0;
714 SignalContendersAndReleaseMonitorLock(self);
715 return true;
716 } else {
717 --lock_count_;
718 monitor_lock_.Unlock(self);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700719 return true;
Elliott Hughes5f791332011-09-15 17:45:30 -0700720 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700721 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700722 // We don't own this, so we're not allowed to unlock it.
723 // The JNI spec says that we should throw IllegalMonitorStateException in this case.
724 FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this);
Charles Mungerc665d632018-11-06 16:20:13 +0000725 monitor_lock_.Unlock(self);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700726 return false;
Elliott Hughes5f791332011-09-15 17:45:30 -0700727}
728
Charles Mungerc665d632018-11-06 16:20:13 +0000729void Monitor::SignalContendersAndReleaseMonitorLock(Thread* self) {
730 // We want to signal one thread to wake up, to acquire the monitor that
731 // we are releasing. This could either be a Thread waiting on its own
732 // ConditionVariable, or a thread waiting on monitor_contenders_.
733 while (wake_set_ != nullptr) {
734 // No risk of waking ourselves here; since monitor_lock_ is not released until we're ready to
735 // return, notify can't move the current thread from wait_set_ to wake_set_ until this
736 // method is done checking wake_set_.
737 Thread* thread = wake_set_;
738 wake_set_ = thread->GetWaitNext();
739 thread->SetWaitNext(nullptr);
740
741 // Check to see if the thread is still waiting.
742 {
743 // In the case of wait(), we'll be acquiring another thread's GetWaitMutex with
744 // self's GetWaitMutex held. This does not risk deadlock, because we only acquire this lock
745 // for threads in the wake_set_. A thread can only enter wake_set_ from Notify or NotifyAll,
746 // and those hold monitor_lock_. Thus, the threads whose wait mutexes we acquire here must
747 // have already been released from wait(), since we have not released monitor_lock_ until
748 // after we've chosen our thread to wake, so there is no risk of the following lock ordering
749 // leading to deadlock:
750 // Thread 1 waits
751 // Thread 2 waits
752 // Thread 3 moves threads 1 and 2 from wait_set_ to wake_set_
753 // Thread 1 enters this block, and attempts to acquire Thread 2's GetWaitMutex to wake it
754 // Thread 2 enters this block, and attempts to acquire Thread 1's GetWaitMutex to wake it
755 //
756 // Since monitor_lock_ is not released until the thread-to-be-woken-up's GetWaitMutex is
757 // acquired, two threads cannot attempt to acquire each other's GetWaitMutex while holding
758 // their own and cause deadlock.
759 MutexLock wait_mu(self, *thread->GetWaitMutex());
760 if (thread->GetWaitMonitor() != nullptr) {
761 // Release the lock, so that a potentially awakened thread will not
762 // immediately contend on it. The lock ordering here is:
763 // monitor_lock_, self->GetWaitMutex, thread->GetWaitMutex
764 monitor_lock_.Unlock(self);
765 thread->GetWaitConditionVariable()->Signal(self);
766 return;
767 }
768 }
769 }
770 // If we didn't wake any threads that were originally waiting on us,
771 // wake a contender.
772 monitor_contenders_.Signal(self);
773 monitor_lock_.Unlock(self);
774}
775
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800776void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
777 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700778 DCHECK(self != nullptr);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800779 DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
Elliott Hughes5f791332011-09-15 17:45:30 -0700780
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700781 monitor_lock_.Lock(self);
782
Elliott Hughes5f791332011-09-15 17:45:30 -0700783 // Make sure that we hold the lock.
784 if (owner_ != self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700785 monitor_lock_.Unlock(self);
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700786 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700787 return;
788 }
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800789
Elliott Hughesdf42c482013-01-09 12:49:02 -0800790 // We need to turn a zero-length timed wait into a regular wait because
791 // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
792 if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
793 why = kWaiting;
794 }
795
Elliott Hughes5f791332011-09-15 17:45:30 -0700796 // Enforce the timeout range.
797 if (ms < 0 || ns < 0 || ns > 999999) {
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700798 monitor_lock_.Unlock(self);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000799 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800800 "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
Elliott Hughes5f791332011-09-15 17:45:30 -0700801 return;
802 }
803
Elliott Hughes5f791332011-09-15 17:45:30 -0700804 /*
Charles Mungerc665d632018-11-06 16:20:13 +0000805 * Release our hold - we need to let it go even if we're a few levels
Elliott Hughes5f791332011-09-15 17:45:30 -0700806 * deep in a recursive lock, and we need to restore that later.
Elliott Hughes5f791332011-09-15 17:45:30 -0700807 */
Ian Rogers0399dde2012-06-06 17:09:28 -0700808 int prev_lock_count = lock_count_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700809 lock_count_ = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700810 owner_ = nullptr;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700811 ArtMethod* saved_method = locking_method_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700812 locking_method_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700813 uintptr_t saved_dex_pc = locking_dex_pc_;
814 locking_dex_pc_ = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700815
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700816 AtraceMonitorUnlock(); // For the implict Unlock() just above. This will only end the deepest
817 // nesting, but that is enough for the visualization, and corresponds to
818 // the single Lock() we do afterwards.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700819 AtraceMonitorLock(self, GetObject(), /* is_wait= */ true);
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700820
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800821 bool was_interrupted = false;
Alex Light77fee872017-09-05 14:51:49 -0700822 bool timed_out = false;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700823 {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700824 // Update thread state. If the GC wakes up, it'll ignore us, knowing
825 // that we won't touch any references in this state, and we'll check
826 // our suspend mode before we transition out.
827 ScopedThreadSuspension sts(self, why);
828
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700829 // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700830 MutexLock mu(self, *self->GetWaitMutex());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700831
Charles Mungerc665d632018-11-06 16:20:13 +0000832 /*
833 * Add ourselves to the set of threads waiting on this monitor.
834 * It's important that we are only added to the wait set after
835 * acquiring our GetWaitMutex, so that calls to Notify() that occur after we
836 * have released monitor_lock_ will not move us from wait_set_ to wake_set_
837 * until we've signalled contenders on this monitor.
838 */
839 AppendToWaitSet(self);
840 ++num_waiters_;
841
842
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700843 // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700844 // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700845 // up.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700846 DCHECK(self->GetWaitMonitor() == nullptr);
847 self->SetWaitMonitor(this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700848
849 // Release the monitor lock.
Charles Mungerc665d632018-11-06 16:20:13 +0000850 SignalContendersAndReleaseMonitorLock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700851
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800852 // Handle the case where the thread was interrupted before we called wait().
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000853 if (self->IsInterrupted()) {
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800854 was_interrupted = true;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700855 } else {
856 // Wait for a notification or a timeout to occur.
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800857 if (why == kWaiting) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700858 self->GetWaitConditionVariable()->Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700859 } else {
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800860 DCHECK(why == kTimedWaiting || why == kSleeping) << why;
Alex Light77fee872017-09-05 14:51:49 -0700861 timed_out = self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700862 }
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000863 was_interrupted = self->IsInterrupted();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700864 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700865 }
866
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800867 {
868 // We reset the thread's wait_monitor_ field after transitioning back to runnable so
869 // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
870 // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
871 // are waiting on "null".)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700872 MutexLock mu(self, *self->GetWaitMutex());
873 DCHECK(self->GetWaitMonitor() != nullptr);
874 self->SetWaitMonitor(nullptr);
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800875 }
876
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800877 // Allocate the interrupted exception not holding the monitor lock since it may cause a GC.
878 // If the GC requires acquiring the monitor for enqueuing cleared references, this would
879 // cause a deadlock if the monitor is held.
880 if (was_interrupted && interruptShouldThrow) {
881 /*
882 * We were interrupted while waiting, or somebody interrupted an
883 * un-interruptible thread earlier and we're bailing out immediately.
884 *
885 * The doc sayeth: "The interrupted status of the current thread is
886 * cleared when this exception is thrown."
887 */
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000888 self->SetInterrupted(false);
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800889 self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr);
890 }
891
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700892 AtraceMonitorUnlock(); // End Wait().
893
Alex Light77fee872017-09-05 14:51:49 -0700894 // We just slept, tell the runtime callbacks about this.
895 Runtime::Current()->GetRuntimeCallbacks()->MonitorWaitFinished(this, timed_out);
896
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700897 // Re-acquire the monitor and lock.
Alex Light77fee872017-09-05 14:51:49 -0700898 Lock<LockReason::kForWait>(self);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700899 monitor_lock_.Lock(self);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700900 self->GetWaitMutex()->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700901
Elliott Hughes5f791332011-09-15 17:45:30 -0700902 /*
903 * We remove our thread from wait set after restoring the count
904 * and owner fields so the subroutine can check that the calling
905 * thread owns the monitor. Aside from that, the order of member
906 * updates is not order sensitive as we hold the pthread mutex.
907 */
908 owner_ = self;
Ian Rogers0399dde2012-06-06 17:09:28 -0700909 lock_count_ = prev_lock_count;
910 locking_method_ = saved_method;
911 locking_dex_pc_ = saved_dex_pc;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700912 --num_waiters_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700913 RemoveFromWaitSet(self);
914
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700915 monitor_lock_.Unlock(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700916}
917
918void Monitor::Notify(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700919 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700920 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700921 // Make sure that we hold the lock.
922 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800923 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700924 return;
925 }
Charles Mungerc665d632018-11-06 16:20:13 +0000926 // Move one thread from waiters to wake set
927 Thread* to_move = wait_set_;
928 if (to_move != nullptr) {
929 wait_set_ = to_move->GetWaitNext();
930 to_move->SetWaitNext(wake_set_);
931 wake_set_ = to_move;
Elliott Hughes5f791332011-09-15 17:45:30 -0700932 }
933}
934
935void Monitor::NotifyAll(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700936 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700937 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700938 // Make sure that we hold the lock.
939 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800940 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700941 return;
942 }
Charles Mungerc665d632018-11-06 16:20:13 +0000943
944 // Move all threads from waiters to wake set
945 Thread* to_move = wait_set_;
946 if (to_move != nullptr) {
947 wait_set_ = nullptr;
948 Thread* move_to = wake_set_;
949 if (move_to == nullptr) {
950 wake_set_ = to_move;
951 return;
952 }
953 while (move_to->GetWaitNext() != nullptr) {
954 move_to = move_to->GetWaitNext();
955 }
956 move_to->SetWaitNext(to_move);
Elliott Hughes5f791332011-09-15 17:45:30 -0700957 }
958}
959
Mathieu Chartier590fee92013-09-13 13:46:47 -0700960bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
961 DCHECK(obj != nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700962 // Don't need volatile since we only deflate with mutators suspended.
963 LockWord lw(obj->GetLockWord(false));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700964 // If the lock isn't an inflated monitor, then we don't need to deflate anything.
965 if (lw.GetState() == LockWord::kFatLocked) {
966 Monitor* monitor = lw.FatLockMonitor();
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700967 DCHECK(monitor != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700968 MutexLock mu(self, monitor->monitor_lock_);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700969 // Can't deflate if we have anybody waiting on the CV.
970 if (monitor->num_waiters_ > 0) {
971 return false;
972 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700973 Thread* owner = monitor->owner_;
974 if (owner != nullptr) {
975 // Can't deflate if we are locked and have a hash code.
976 if (monitor->HasHashCode()) {
977 return false;
978 }
979 // Can't deflate if our lock count is too high.
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700980 if (static_cast<uint32_t>(monitor->lock_count_) > LockWord::kThinLockMaxCount) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700981 return false;
982 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700983 // Deflate to a thin lock.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700984 LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(),
985 monitor->lock_count_,
986 lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800987 // Assume no concurrent read barrier state changes as mutators are suspended.
988 obj->SetLockWord(new_lw, false);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700989 VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
990 << monitor->lock_count_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700991 } else if (monitor->HasHashCode()) {
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700992 LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800993 // Assume no concurrent read barrier state changes as mutators are suspended.
994 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700995 VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700996 } else {
997 // No lock and no hash, just put an empty lock word inside the object.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700998 LockWord new_lw = LockWord::FromDefault(lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800999 // Assume no concurrent read barrier state changes as mutators are suspended.
1000 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001001 VLOG(monitor) << "Deflated" << obj << " to empty lock word";
Mathieu Chartier590fee92013-09-13 13:46:47 -07001002 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001003 // 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 -07001004 // next GC.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -07001005 monitor->obj_ = GcRoot<mirror::Object>(nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001006 }
1007 return true;
1008}
1009
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001010void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
Andreas Gampe74240812014-04-17 10:35:09 -07001011 DCHECK(self != nullptr);
1012 DCHECK(obj != nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -07001013 // Allocate and acquire a new monitor.
Andreas Gampe74240812014-04-17 10:35:09 -07001014 Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code);
1015 DCHECK(m != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001016 if (m->Install(self)) {
Haifeng Li86ab7912014-05-16 10:47:59 +08001017 if (owner != nullptr) {
1018 VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
Andreas Gampe74240812014-04-17 10:35:09 -07001019 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +08001020 } else {
1021 VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
Andreas Gampe74240812014-04-17 10:35:09 -07001022 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +08001023 }
Andreas Gampe74240812014-04-17 10:35:09 -07001024 Runtime::Current()->GetMonitorList()->Add(m);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001025 CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
Andreas Gampe74240812014-04-17 10:35:09 -07001026 } else {
1027 MonitorPool::ReleaseMonitor(self, m);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001028 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001029}
1030
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001031void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001032 uint32_t hash_code) {
1033 DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
1034 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1035 if (owner_thread_id == self->GetThreadId()) {
1036 // We own the monitor, we can easily inflate it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001037 Inflate(self, self, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001038 } else {
1039 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1040 // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001041 self->SetMonitorEnterObject(obj.Get());
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -07001042 bool timed_out;
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001043 Thread* owner;
1044 {
Alex Light77fee872017-09-05 14:51:49 -07001045 ScopedThreadSuspension sts(self, kWaitingForLockInflation);
Alex Light46f93402017-06-29 11:59:50 -07001046 owner = thread_list->SuspendThreadByThreadId(owner_thread_id,
1047 SuspendReason::kInternal,
1048 &timed_out);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -07001049 }
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -07001050 if (owner != nullptr) {
1051 // We succeeded in suspending the thread, check the lock's status didn't change.
1052 lock_word = obj->GetLockWord(true);
1053 if (lock_word.GetState() == LockWord::kThinLocked &&
1054 lock_word.ThinLockOwner() == owner_thread_id) {
1055 // Go ahead and inflate the lock.
1056 Inflate(self, owner, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001057 }
Alex Light88fd7202017-06-30 08:31:59 -07001058 bool resumed = thread_list->Resume(owner, SuspendReason::kInternal);
1059 DCHECK(resumed);
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001060 }
Ian Rogersdd7624d2014-03-14 17:43:00 -07001061 self->SetMonitorEnterObject(nullptr);
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001062 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001063}
1064
Ian Rogers719d1a32014-03-06 12:13:39 -08001065// Fool annotalysis into thinking that the lock on obj is acquired.
1066static mirror::Object* FakeLock(mirror::Object* obj)
1067 EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
1068 return obj;
1069}
1070
1071// Fool annotalysis into thinking that the lock on obj is release.
1072static mirror::Object* FakeUnlock(mirror::Object* obj)
1073 UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
1074 return obj;
1075}
1076
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001077mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj, bool trylock) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001078 DCHECK(self != nullptr);
1079 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -07001080 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -08001081 obj = FakeLock(obj);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001082 uint32_t thread_id = self->GetThreadId();
1083 size_t contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001084 StackHandleScope<1> hs(self);
1085 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001086 while (true) {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001087 // We initially read the lockword with ordinary Java/relaxed semantics. When stronger
1088 // semantics are needed, we address it below. Since GetLockWord bottoms out to a relaxed load,
1089 // we can fix it later, in an infrequently executed case, with a fence.
1090 LockWord lock_word = h_obj->GetLockWord(false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001091 switch (lock_word.GetState()) {
1092 case LockWord::kUnlocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001093 // No ordering required for preceding lockword read, since we retest.
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001094 LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.GCState()));
Mathieu Chartier42c2e502018-06-19 12:30:56 -07001095 if (h_obj->CasLockWord(lock_word, thin_locked, CASMode::kWeak, std::memory_order_acquire)) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001096 AtraceMonitorLock(self, h_obj.Get(), /* is_wait= */ false);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001097 return h_obj.Get(); // Success!
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001098 }
1099 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -07001100 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001101 case LockWord::kThinLocked: {
1102 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1103 if (owner_thread_id == thread_id) {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001104 // No ordering required for initial lockword read.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001105 // We own the lock, increase the recursion count.
1106 uint32_t new_count = lock_word.ThinLockCount() + 1;
1107 if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001108 LockWord thin_locked(LockWord::FromThinLockId(thread_id,
1109 new_count,
1110 lock_word.GCState()));
Hans Boehmb3da36c2016-12-15 13:12:59 -08001111 // Only this thread pays attention to the count. Thus there is no need for stronger
1112 // than relaxed memory ordering.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001113 if (!kUseReadBarrier) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001114 h_obj->SetLockWord(thin_locked, /* as_volatile= */ false);
1115 AtraceMonitorLock(self, h_obj.Get(), /* is_wait= */ false);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001116 return h_obj.Get(); // Success!
1117 } else {
1118 // Use CAS to preserve the read barrier state.
Mathieu Chartier42c2e502018-06-19 12:30:56 -07001119 if (h_obj->CasLockWord(lock_word,
1120 thin_locked,
1121 CASMode::kWeak,
1122 std::memory_order_relaxed)) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001123 AtraceMonitorLock(self, h_obj.Get(), /* is_wait= */ false);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001124 return h_obj.Get(); // Success!
1125 }
1126 }
1127 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -07001128 } else {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001129 // We'd overflow the recursion count, so inflate the monitor.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001130 InflateThinLocked(self, h_obj, lock_word, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001131 }
1132 } else {
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001133 if (trylock) {
1134 return nullptr;
1135 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001136 // Contention.
1137 contention_count++;
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001138 Runtime* runtime = Runtime::Current();
Hans Boehmb3da36c2016-12-15 13:12:59 -08001139 if (contention_count <= runtime->GetMaxSpinsBeforeThinLockInflation()) {
Alex Light77fee872017-09-05 14:51:49 -07001140 // TODO: Consider switching the thread state to kWaitingForLockInflation when we are
1141 // yielding. Use sched_yield instead of NanoSleep since NanoSleep can wait much longer
1142 // than the parameter you pass in. This can cause thread suspension to take excessively
1143 // long and make long pauses. See b/16307460.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001144 // TODO: We should literally spin first, without sched_yield. Sched_yield either does
1145 // nothing (at significant expense), or guarantees that we wait at least microseconds.
1146 // If the owner is running, I would expect the median lock hold time to be hundreds
1147 // of nanoseconds or less.
Mathieu Chartier251755c2014-07-15 18:10:25 -07001148 sched_yield();
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001149 } else {
1150 contention_count = 0;
Hans Boehmb3da36c2016-12-15 13:12:59 -08001151 // No ordering required for initial lockword read. Install rereads it anyway.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001152 InflateThinLocked(self, h_obj, lock_word, 0);
Elliott Hughes5f791332011-09-15 17:45:30 -07001153 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001154 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001155 continue; // Start from the beginning.
Elliott Hughes5f791332011-09-15 17:45:30 -07001156 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001157 case LockWord::kFatLocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001158 // We should have done an acquire read of the lockword initially, to ensure
1159 // visibility of the monitor data structure. Use an explicit fence instead.
Orion Hodson27b96762018-03-13 16:06:57 +00001160 std::atomic_thread_fence(std::memory_order_acquire);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001161 Monitor* mon = lock_word.FatLockMonitor();
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001162 if (trylock) {
1163 return mon->TryLock(self) ? h_obj.Get() : nullptr;
1164 } else {
1165 mon->Lock(self);
1166 return h_obj.Get(); // Success!
1167 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001168 }
Ian Rogers719d1a32014-03-06 12:13:39 -08001169 case LockWord::kHashCode:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001170 // Inflate with the existing hashcode.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001171 // Again no ordering required for initial lockword read, since we don't rely
1172 // on the visibility of any prior computation.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001173 Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
Ian Rogers719d1a32014-03-06 12:13:39 -08001174 continue; // Start from the beginning.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001175 default: {
1176 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Andreas Gampec7ed09b2016-04-25 20:08:55 -07001177 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001178 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001179 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001180 }
1181}
1182
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001183bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001184 DCHECK(self != nullptr);
1185 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -07001186 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -08001187 obj = FakeUnlock(obj);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001188 StackHandleScope<1> hs(self);
1189 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001190 while (true) {
1191 LockWord lock_word = obj->GetLockWord(true);
1192 switch (lock_word.GetState()) {
1193 case LockWord::kHashCode:
1194 // Fall-through.
1195 case LockWord::kUnlocked:
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001196 FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001197 return false; // Failure.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001198 case LockWord::kThinLocked: {
1199 uint32_t thread_id = self->GetThreadId();
1200 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1201 if (owner_thread_id != thread_id) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001202 FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001203 return false; // Failure.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001204 } else {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001205 // We own the lock, decrease the recursion count.
1206 LockWord new_lw = LockWord::Default();
1207 if (lock_word.ThinLockCount() != 0) {
1208 uint32_t new_count = lock_word.ThinLockCount() - 1;
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001209 new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001210 } else {
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001211 new_lw = LockWord::FromDefault(lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001212 }
1213 if (!kUseReadBarrier) {
1214 DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
Hans Boehmb3da36c2016-12-15 13:12:59 -08001215 // TODO: This really only needs memory_order_release, but we currently have
1216 // no way to specify that. In fact there seem to be no legitimate uses of SetLockWord
1217 // with a final argument of true. This slows down x86 and ARMv7, but probably not v8.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001218 h_obj->SetLockWord(new_lw, true);
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001219 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001220 // Success!
1221 return true;
1222 } else {
1223 // Use CAS to preserve the read barrier state.
Mathieu Chartier42c2e502018-06-19 12:30:56 -07001224 if (h_obj->CasLockWord(lock_word, new_lw, CASMode::kWeak, std::memory_order_release)) {
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001225 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001226 // Success!
1227 return true;
1228 }
1229 }
1230 continue; // Go again.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001231 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001232 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001233 case LockWord::kFatLocked: {
1234 Monitor* mon = lock_word.FatLockMonitor();
1235 return mon->Unlock(self);
1236 }
1237 default: {
1238 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Elliott Hughesc1896c92018-11-29 11:33:18 -08001239 UNREACHABLE();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001240 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001241 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001242 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001243}
1244
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001245void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
Elliott Hughes4cd121e2013-01-07 17:35:41 -08001246 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001247 DCHECK(self != nullptr);
1248 DCHECK(obj != nullptr);
Alex Light77fee872017-09-05 14:51:49 -07001249 StackHandleScope<1> hs(self);
1250 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
1251
1252 Runtime::Current()->GetRuntimeCallbacks()->ObjectWaitStart(h_obj, ms);
Alex Light848574c2017-09-25 16:59:39 -07001253 if (UNLIKELY(self->ObserveAsyncException() || self->IsExceptionPending())) {
Alex Light77fee872017-09-05 14:51:49 -07001254 // See b/65558434 for information on handling of exceptions here.
1255 return;
1256 }
1257
1258 LockWord lock_word = h_obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -07001259 while (lock_word.GetState() != LockWord::kFatLocked) {
1260 switch (lock_word.GetState()) {
1261 case LockWord::kHashCode:
1262 // Fall-through.
1263 case LockWord::kUnlocked:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001264 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1265 return; // Failure.
Ian Rogers43c69cc2014-08-15 11:09:28 -07001266 case LockWord::kThinLocked: {
1267 uint32_t thread_id = self->GetThreadId();
1268 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1269 if (owner_thread_id != thread_id) {
1270 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1271 return; // Failure.
1272 } else {
1273 // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
1274 // re-load.
Alex Light77fee872017-09-05 14:51:49 -07001275 Inflate(self, self, h_obj.Get(), 0);
1276 lock_word = h_obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -07001277 }
1278 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001279 }
Ian Rogers43c69cc2014-08-15 11:09:28 -07001280 case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through.
1281 default: {
1282 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Elliott Hughesc1896c92018-11-29 11:33:18 -08001283 UNREACHABLE();
Ian Rogers43c69cc2014-08-15 11:09:28 -07001284 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001285 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001286 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001287 Monitor* mon = lock_word.FatLockMonitor();
1288 mon->Wait(self, ms, ns, interruptShouldThrow, why);
Elliott Hughes5f791332011-09-15 17:45:30 -07001289}
1290
Ian Rogers13c479e2013-10-11 07:59:01 -07001291void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001292 DCHECK(self != nullptr);
1293 DCHECK(obj != nullptr);
1294 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001295 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001296 case LockWord::kHashCode:
1297 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001298 case LockWord::kUnlocked:
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001299 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001300 return; // Failure.
1301 case LockWord::kThinLocked: {
1302 uint32_t thread_id = self->GetThreadId();
1303 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1304 if (owner_thread_id != thread_id) {
1305 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
1306 return; // Failure.
1307 } else {
1308 // We own the lock but there's no Monitor and therefore no waiters.
1309 return; // Success.
1310 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001311 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001312 case LockWord::kFatLocked: {
1313 Monitor* mon = lock_word.FatLockMonitor();
1314 if (notify_all) {
1315 mon->NotifyAll(self);
1316 } else {
1317 mon->Notify(self);
1318 }
1319 return; // Success.
1320 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001321 default: {
1322 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Elliott Hughesc1896c92018-11-29 11:33:18 -08001323 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001324 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001325 }
1326}
1327
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001328uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001329 DCHECK(obj != nullptr);
1330 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001331 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001332 case LockWord::kHashCode:
1333 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001334 case LockWord::kUnlocked:
1335 return ThreadList::kInvalidThreadId;
1336 case LockWord::kThinLocked:
1337 return lock_word.ThinLockOwner();
1338 case LockWord::kFatLocked: {
1339 Monitor* mon = lock_word.FatLockMonitor();
1340 return mon->GetOwnerThreadId();
Elliott Hughes5f791332011-09-15 17:45:30 -07001341 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001342 default: {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001343 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001344 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001345 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001346 }
1347}
1348
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001349ThreadState Monitor::FetchState(const Thread* thread,
1350 /* out */ mirror::Object** monitor_object,
1351 /* out */ uint32_t* lock_owner_tid) {
1352 DCHECK(monitor_object != nullptr);
1353 DCHECK(lock_owner_tid != nullptr);
1354
1355 *monitor_object = nullptr;
1356 *lock_owner_tid = ThreadList::kInvalidThreadId;
1357
1358 ThreadState state = thread->GetState();
1359
1360 switch (state) {
1361 case kWaiting:
1362 case kTimedWaiting:
1363 case kSleeping:
1364 {
1365 Thread* self = Thread::Current();
1366 MutexLock mu(self, *thread->GetWaitMutex());
1367 Monitor* monitor = thread->GetWaitMonitor();
1368 if (monitor != nullptr) {
1369 *monitor_object = monitor->GetObject();
Ian Rogersd803bc72014-04-01 15:33:03 -07001370 }
1371 }
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001372 break;
1373
1374 case kBlocked:
1375 case kWaitingForLockInflation:
1376 {
1377 mirror::Object* lock_object = thread->GetMonitorEnterObject();
1378 if (lock_object != nullptr) {
1379 if (kUseReadBarrier && Thread::Current()->GetIsGcMarking()) {
1380 // We may call Thread::Dump() in the middle of the CC thread flip and this thread's stack
1381 // may have not been flipped yet and "pretty_object" may be a from-space (stale) ref, in
1382 // which case the GetLockOwnerThreadId() call below will crash. So explicitly mark/forward
1383 // it here.
1384 lock_object = ReadBarrier::Mark(lock_object);
1385 }
1386 *monitor_object = lock_object;
1387 *lock_owner_tid = lock_object->GetLockOwnerThreadId();
1388 }
Ian Rogersd803bc72014-04-01 15:33:03 -07001389 }
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001390 break;
1391
1392 default:
1393 break;
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001394 }
Andreas Gampef3ebcce2017-12-11 20:40:23 -08001395
1396 return state;
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001397}
1398
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001399mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001400 // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
1401 // definition of contended that includes a monitor a thread is trying to enter...
Ian Rogersdd7624d2014-03-14 17:43:00 -07001402 mirror::Object* result = thread->GetMonitorEnterObject();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001403 if (result == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001404 // ...but also a monitor that the thread is waiting on.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001405 MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
1406 Monitor* monitor = thread->GetWaitMonitor();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001407 if (monitor != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001408 result = monitor->GetObject();
Elliott Hughesf9501702013-01-11 11:22:27 -08001409 }
1410 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001411 return result;
Elliott Hughesf9501702013-01-11 11:22:27 -08001412}
1413
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001414void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
Andreas Gampe760172c2014-08-16 13:41:10 -07001415 void* callback_context, bool abort_on_failure) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001416 ArtMethod* m = stack_visitor->GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001417 CHECK(m != nullptr);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001418
1419 // Native methods are an easy special case.
1420 // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
1421 if (m->IsNative()) {
1422 if (m->IsSynchronized()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001423 mirror::Object* jni_this =
1424 stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001425 callback(jni_this, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001426 }
1427 return;
1428 }
1429
jeffhao61f916c2012-10-25 17:48:51 -07001430 // Proxy methods should not be synchronized.
1431 if (m->IsProxyMethod()) {
1432 CHECK(!m->IsSynchronized());
1433 return;
1434 }
1435
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001436 // Is there any reason to believe there's any synchronization in this method?
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001437 CHECK(m->GetCodeItem() != nullptr) << m->PrettyMethod();
David Sehr0225f8e2018-01-31 08:52:24 +00001438 CodeItemDataAccessor accessor(m->DexInstructionData());
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001439 if (accessor.TriesSize() == 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001440 return; // No "tries" implies no synchronization, so no held locks to report.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001441 }
1442
Andreas Gampe760172c2014-08-16 13:41:10 -07001443 // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
1444 // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
1445 // inconsistent stack anyways.
1446 uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
Andreas Gampee2abbc62017-09-15 11:59:26 -07001447 if (!abort_on_failure && dex_pc == dex::kDexNoIndex) {
David Sehr709b0702016-10-13 09:12:37 -07001448 LOG(ERROR) << "Could not find dex_pc for " << m->PrettyMethod();
Andreas Gampe760172c2014-08-16 13:41:10 -07001449 return;
1450 }
1451
Elliott Hughes80537bb2013-01-04 16:37:26 -08001452 // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
1453 // the locks held in this stack frame.
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001454 std::vector<verifier::MethodVerifier::DexLockInfo> monitor_enter_dex_pcs;
Andreas Gampe6cc23ac2018-08-24 15:22:43 -07001455 verifier::MethodVerifier::FindLocksAtDexPc(m,
1456 dex_pc,
1457 &monitor_enter_dex_pcs,
1458 Runtime::Current()->GetTargetSdkVersion());
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001459 for (verifier::MethodVerifier::DexLockInfo& dex_lock_info : monitor_enter_dex_pcs) {
1460 // As a debug check, check that dex PC corresponds to a monitor-enter.
1461 if (kIsDebugBuild) {
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001462 const Instruction& monitor_enter_instruction = accessor.InstructionAt(dex_lock_info.dex_pc);
1463 CHECK_EQ(monitor_enter_instruction.Opcode(), Instruction::MONITOR_ENTER)
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001464 << "expected monitor-enter @" << dex_lock_info.dex_pc << "; was "
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001465 << reinterpret_cast<const void*>(&monitor_enter_instruction);
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001466 }
Elliott Hughes80537bb2013-01-04 16:37:26 -08001467
Andreas Gampeaaf0d382017-11-27 14:10:21 -08001468 // Iterate through the set of dex registers, as the compiler may not have held all of them
1469 // live.
1470 bool success = false;
1471 for (uint32_t dex_reg : dex_lock_info.dex_registers) {
1472 uint32_t value;
1473 success = stack_visitor->GetVReg(m, dex_reg, kReferenceVReg, &value);
1474 if (success) {
1475 mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
1476 callback(o, callback_context);
1477 break;
1478 }
1479 }
1480 DCHECK(success) << "Failed to find/read reference for monitor-enter at dex pc "
1481 << dex_lock_info.dex_pc
1482 << " in method "
1483 << m->PrettyMethod();
1484 if (!success) {
1485 LOG(WARNING) << "Had a lock reported for dex pc " << dex_lock_info.dex_pc
1486 << " but was not able to fetch a corresponding object!";
1487 }
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001488 }
1489}
1490
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001491bool Monitor::IsValidLockWord(LockWord lock_word) {
1492 switch (lock_word.GetState()) {
1493 case LockWord::kUnlocked:
1494 // Nothing to check.
1495 return true;
1496 case LockWord::kThinLocked:
1497 // Basic sanity check of owner.
1498 return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1499 case LockWord::kFatLocked: {
1500 // Check the monitor appears in the monitor list.
1501 Monitor* mon = lock_word.FatLockMonitor();
1502 MonitorList* list = Runtime::Current()->GetMonitorList();
1503 MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1504 for (Monitor* list_mon : list->list_) {
1505 if (mon == list_mon) {
1506 return true; // Found our monitor.
1507 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001508 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001509 return false; // Fail - unowned monitor in an object.
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001510 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001511 case LockWord::kHashCode:
1512 return true;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001513 default:
1514 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001515 UNREACHABLE();
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001516 }
1517}
1518
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001519bool Monitor::IsLocked() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001520 MutexLock mu(Thread::Current(), monitor_lock_);
1521 return owner_ != nullptr;
1522}
1523
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -07001524void Monitor::TranslateLocation(ArtMethod* method,
1525 uint32_t dex_pc,
1526 const char** source_file,
1527 int32_t* line_number) {
jeffhao33dc7712011-11-09 17:54:24 -08001528 // If method is null, location is unknown
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001529 if (method == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001530 *source_file = "";
1531 *line_number = 0;
jeffhao33dc7712011-11-09 17:54:24 -08001532 return;
1533 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001534 *source_file = method->GetDeclaringClassSourceFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001535 if (*source_file == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001536 *source_file = "";
Elliott Hughes12c51e32012-01-17 20:25:05 -08001537 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001538 *line_number = method->GetLineNumFromDexPC(dex_pc);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001539}
1540
1541uint32_t Monitor::GetOwnerThreadId() {
1542 MutexLock mu(Thread::Current(), monitor_lock_);
1543 Thread* owner = owner_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001544 if (owner != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001545 return owner->GetThreadId();
1546 } else {
1547 return ThreadList::kInvalidThreadId;
1548 }
jeffhao33dc7712011-11-09 17:54:24 -08001549}
1550
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001551MonitorList::MonitorList()
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001552 : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001553 monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001554}
1555
1556MonitorList::~MonitorList() {
Andreas Gampe74240812014-04-17 10:35:09 -07001557 Thread* self = Thread::Current();
1558 MutexLock mu(self, monitor_list_lock_);
1559 // Release all monitors to the pool.
1560 // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
1561 // clear faster in the pool.
1562 MonitorPool::ReleaseMonitors(self, &list_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001563}
1564
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001565void MonitorList::DisallowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001566 CHECK(!kUseReadBarrier);
Ian Rogers50b35e22012-10-04 10:09:15 -07001567 MutexLock mu(Thread::Current(), monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001568 allow_new_monitors_ = false;
1569}
1570
1571void MonitorList::AllowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001572 CHECK(!kUseReadBarrier);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001573 Thread* self = Thread::Current();
1574 MutexLock mu(self, monitor_list_lock_);
1575 allow_new_monitors_ = true;
1576 monitor_add_condition_.Broadcast(self);
1577}
1578
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001579void MonitorList::BroadcastForNewMonitors() {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001580 Thread* self = Thread::Current();
1581 MutexLock mu(self, monitor_list_lock_);
1582 monitor_add_condition_.Broadcast(self);
1583}
1584
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001585void MonitorList::Add(Monitor* m) {
1586 Thread* self = Thread::Current();
1587 MutexLock mu(self, monitor_list_lock_);
Hiroshi Yamauchif1c6f872017-01-06 12:23:47 -08001588 // CMS needs this to block for concurrent reference processing because an object allocated during
1589 // the GC won't be marked and concurrent reference processing would incorrectly clear the JNI weak
1590 // ref. But CC (kUseReadBarrier == true) doesn't because of the to-space invariant.
1591 while (!kUseReadBarrier && UNLIKELY(!allow_new_monitors_)) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -07001592 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
1593 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -08001594 self->CheckEmptyCheckpointFromWeakRefAccess(&monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001595 monitor_add_condition_.WaitHoldingLocks(self);
1596 }
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001597 list_.push_front(m);
1598}
1599
Mathieu Chartier97509952015-07-13 14:35:43 -07001600void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
Andreas Gampe74240812014-04-17 10:35:09 -07001601 Thread* self = Thread::Current();
1602 MutexLock mu(self, monitor_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001603 for (auto it = list_.begin(); it != list_.end(); ) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001604 Monitor* m = *it;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001605 // Disable the read barrier in GetObject() as this is called by GC.
1606 mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001607 // The object of a monitor can be null if we have deflated it.
Mathieu Chartier97509952015-07-13 14:35:43 -07001608 mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001609 if (new_obj == nullptr) {
1610 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001611 << obj;
Andreas Gampe74240812014-04-17 10:35:09 -07001612 MonitorPool::ReleaseMonitor(self, m);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001613 it = list_.erase(it);
1614 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001615 m->SetObject(new_obj);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001616 ++it;
1617 }
1618 }
1619}
1620
Hans Boehm6fe97e02016-05-04 18:35:57 -07001621size_t MonitorList::Size() {
1622 Thread* self = Thread::Current();
1623 MutexLock mu(self, monitor_list_lock_);
1624 return list_.size();
1625}
1626
Mathieu Chartier97509952015-07-13 14:35:43 -07001627class MonitorDeflateVisitor : public IsMarkedVisitor {
1628 public:
1629 MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
1630
Roland Levillainf73caca2018-08-24 17:19:07 +01001631 mirror::Object* IsMarked(mirror::Object* object) override
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001632 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -07001633 if (Monitor::Deflate(self_, object)) {
1634 DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1635 ++deflate_count_;
1636 // If we deflated, return null so that the monitor gets removed from the array.
1637 return nullptr;
1638 }
1639 return object; // Monitor was not deflated.
1640 }
1641
1642 Thread* const self_;
1643 size_t deflate_count_;
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001644};
1645
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001646size_t MonitorList::DeflateMonitors() {
Mathieu Chartier97509952015-07-13 14:35:43 -07001647 MonitorDeflateVisitor visitor;
1648 Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
1649 SweepMonitorList(&visitor);
1650 return visitor.deflate_count_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001651}
1652
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001653MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001654 DCHECK(obj != nullptr);
1655 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001656 switch (lock_word.GetState()) {
1657 case LockWord::kUnlocked:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001658 // Fall-through.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001659 case LockWord::kForwardingAddress:
1660 // Fall-through.
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001661 case LockWord::kHashCode:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001662 break;
1663 case LockWord::kThinLocked:
1664 owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
Alex Lightce568642017-09-05 16:54:25 -07001665 DCHECK(owner_ != nullptr) << "Thin-locked without owner!";
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001666 entry_count_ = 1 + lock_word.ThinLockCount();
1667 // Thin locks have no waiters.
1668 break;
1669 case LockWord::kFatLocked: {
1670 Monitor* mon = lock_word.FatLockMonitor();
1671 owner_ = mon->owner_;
Alex Lightce568642017-09-05 16:54:25 -07001672 // Here it is okay for the owner to be null since we don't reset the LockWord back to
1673 // kUnlocked until we get a GC. In cases where this hasn't happened yet we will have a fat
1674 // lock without an owner.
1675 if (owner_ != nullptr) {
1676 entry_count_ = 1 + mon->lock_count_;
1677 } else {
1678 DCHECK_EQ(mon->lock_count_, 0) << "Monitor is fat-locked without any owner!";
1679 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001680 for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001681 waiters_.push_back(waiter);
1682 }
1683 break;
Elliott Hughesf327e072013-01-09 16:01:26 -08001684 }
1685 }
1686}
1687
Elliott Hughes5f791332011-09-15 17:45:30 -07001688} // namespace art