blob: 5c63dcad787dea6ab56b70ff06bcab3164da9e0e [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"
Elliott Hughes76b61672012-12-12 17:47:30 -080024#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080025#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080026#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/time_utils.h"
jeffhao33dc7712011-11-09 17:54:24 -080028#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Sebastien Hertz0f7c9332015-11-05 15:57:30 +010030#include "dex_instruction-inl.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070031#include "lock_word-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070032#include "mirror/class-inl.h"
Ian Rogers05f30572013-02-20 12:13:11 -080033#include "mirror/object-inl.h"
Andreas Gampe5d08fcc2017-06-05 17:56:46 -070034#include "object_callbacks.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070036#include "stack.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070037#include "thread.h"
Elliott Hughes8e4aac52011-09-26 17:03:36 -070038#include "thread_list.h"
Elliott Hughes08fc03a2012-06-26 17:34:00 -070039#include "verifier/method_verifier.h"
Elliott Hughes044288f2012-06-25 14:46:39 -070040#include "well_known_classes.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070041
42namespace art {
43
Andreas Gampe46ee31b2016-12-14 10:11:49 -080044using android::base::StringPrintf;
45
Mathieu Chartierb9001ab2014-10-03 13:28:46 -070046static constexpr uint64_t kLongWaitMs = 100;
47
Elliott Hughes5f791332011-09-15 17:45:30 -070048/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -070049 * Every Object has a monitor associated with it, but not every Object is actually locked. Even
50 * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
51 * or b) wait() is called on the Object.
Elliott Hughes5f791332011-09-15 17:45:30 -070052 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070053 * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
54 * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us,
55 * though, because we have a full 32 bits to work with.
Elliott Hughes5f791332011-09-15 17:45:30 -070056 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070057 * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition
58 * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
59 * a lock has been inflated it remains in the "fat" state indefinitely.
Elliott Hughes5f791332011-09-15 17:45:30 -070060 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070061 * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
62 * in the LockWord value type.
Elliott Hughes54e7df12011-09-16 11:47:04 -070063 *
Elliott Hughes5f791332011-09-15 17:45:30 -070064 * Monitors provide:
65 * - mutually exclusive access to resources
66 * - a way for multiple threads to wait for notification
67 *
68 * In effect, they fill the role of both mutexes and condition variables.
69 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070070 * Only one thread can own the monitor at any time. There may be several threads waiting on it
71 * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified
72 * at any given time.
Elliott Hughes5f791332011-09-15 17:45:30 -070073 */
Elliott Hughes54e7df12011-09-16 11:47:04 -070074
Elliott Hughesfc861622011-10-17 17:57:47 -070075uint32_t Monitor::lock_profiling_threshold_ = 0;
Andreas Gamped0210e52017-06-23 13:38:09 -070076uint32_t Monitor::stack_dump_lock_profiling_threshold_ = 0;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070077
Andreas Gamped0210e52017-06-23 13:38:09 -070078void Monitor::Init(uint32_t lock_profiling_threshold,
79 uint32_t stack_dump_lock_profiling_threshold) {
Elliott Hughesfc861622011-10-17 17:57:47 -070080 lock_profiling_threshold_ = lock_profiling_threshold;
Andreas Gamped0210e52017-06-23 13:38:09 -070081 stack_dump_lock_profiling_threshold_ = stack_dump_lock_profiling_threshold;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070082}
83
Ian Rogersef7d42f2014-01-06 12:55:46 -080084Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070085 : monitor_lock_("a monitor lock", kMonitorLock),
Ian Rogersd9c4fc92013-10-01 19:45:43 -070086 monitor_contenders_("monitor contenders", monitor_lock_),
Mathieu Chartier46bc7782013-11-12 17:03:02 -080087 num_waiters_(0),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 owner_(owner),
Elliott Hughes5f791332011-09-15 17:45:30 -070089 lock_count_(0),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070090 obj_(GcRoot<mirror::Object>(obj)),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070091 wait_set_(nullptr),
Mathieu Chartierad2541a2013-10-25 10:05:23 -070092 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070093 locking_method_(nullptr),
Ian Rogersef7d42f2014-01-06 12:55:46 -080094 locking_dex_pc_(0),
Andreas Gampe74240812014-04-17 10:35:09 -070095 monitor_id_(MonitorPool::ComputeMonitorId(this, self)) {
96#ifdef __LP64__
97 DCHECK(false) << "Should not be reached in 64b";
98 next_free_ = nullptr;
99#endif
100 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
101 // with the owner unlocking the thin-lock.
102 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
103 // The identity hash code is set for the life time of the monitor.
104}
105
106Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code,
107 MonitorId id)
108 : monitor_lock_("a monitor lock", kMonitorLock),
109 monitor_contenders_("monitor contenders", monitor_lock_),
110 num_waiters_(0),
111 owner_(owner),
112 lock_count_(0),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700113 obj_(GcRoot<mirror::Object>(obj)),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700114 wait_set_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700115 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 locking_method_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700117 locking_dex_pc_(0),
118 monitor_id_(id) {
119#ifdef __LP64__
120 next_free_ = nullptr;
121#endif
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700122 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
123 // with the owner unlocking the thin-lock.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700125 // The identity hash code is set for the life time of the monitor.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700126}
127
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700128int32_t Monitor::GetHashCode() {
129 while (!HasHashCode()) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700130 if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) {
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700131 break;
132 }
133 }
134 DCHECK(HasHashCode());
Ian Rogers3e5cf302014-05-20 16:40:37 -0700135 return hash_code_.LoadRelaxed();
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700136}
137
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700138bool Monitor::Install(Thread* self) {
139 MutexLock mu(self, monitor_lock_); // Uncontended mutex acquisition as monitor isn't yet public.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700140 CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 // Propagate the lock state.
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -0700142 LockWord lw(GetObject()->GetLockWord(false));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700143 switch (lw.GetState()) {
144 case LockWord::kThinLocked: {
145 CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
146 lock_count_ = lw.ThinLockCount();
147 break;
148 }
149 case LockWord::kHashCode: {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700150 CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode()));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700151 break;
152 }
153 case LockWord::kFatLocked: {
154 // The owner_ is suspended but another thread beat us to install a monitor.
155 return false;
156 }
157 case LockWord::kUnlocked: {
158 LOG(FATAL) << "Inflating unlocked lock word";
159 break;
160 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700161 default: {
162 LOG(FATAL) << "Invalid monitor state " << lw.GetState();
163 return false;
164 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700165 }
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700166 LockWord fat(this, lw.GCState());
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700167 // Publish the updated lock word, which may race with other threads.
Hans Boehmb3da36c2016-12-15 13:12:59 -0800168 bool success = GetObject()->CasLockWordWeakRelease(lw, fat);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700169 // Lock profiling.
Mathieu Chartier9728f912013-10-30 09:45:13 -0700170 if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
Andreas Gampe6ec8ebd2014-07-25 13:36:56 -0700171 // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on
172 // abort.
173 locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700174 }
175 return success;
Elliott Hughes5f791332011-09-15 17:45:30 -0700176}
177
178Monitor::~Monitor() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700179 // Deflated monitors have a null object.
Elliott Hughes5f791332011-09-15 17:45:30 -0700180}
181
Elliott Hughes5f791332011-09-15 17:45:30 -0700182void Monitor::AppendToWaitSet(Thread* thread) {
183 DCHECK(owner_ == Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700184 DCHECK(thread != nullptr);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700185 DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700186 if (wait_set_ == nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700187 wait_set_ = thread;
188 return;
189 }
190
191 // push_back.
192 Thread* t = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700193 while (t->GetWaitNext() != nullptr) {
194 t = t->GetWaitNext();
Elliott Hughes5f791332011-09-15 17:45:30 -0700195 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700196 t->SetWaitNext(thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700197}
198
Elliott Hughes5f791332011-09-15 17:45:30 -0700199void Monitor::RemoveFromWaitSet(Thread *thread) {
200 DCHECK(owner_ == Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700201 DCHECK(thread != nullptr);
202 if (wait_set_ == nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700203 return;
204 }
205 if (wait_set_ == thread) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700206 wait_set_ = thread->GetWaitNext();
207 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700208 return;
209 }
210
211 Thread* t = wait_set_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700212 while (t->GetWaitNext() != nullptr) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700213 if (t->GetWaitNext() == thread) {
214 t->SetWaitNext(thread->GetWaitNext());
215 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700216 return;
217 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700218 t = t->GetWaitNext();
Elliott Hughes5f791332011-09-15 17:45:30 -0700219 }
220}
221
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700222void Monitor::SetObject(mirror::Object* object) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700223 obj_ = GcRoot<mirror::Object>(object);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700224}
225
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700226// Note: Adapted from CurrentMethodVisitor in thread.cc. We must not resolve here.
227
228struct NthCallerWithDexPcVisitor FINAL : public StackVisitor {
229 explicit NthCallerWithDexPcVisitor(Thread* thread, size_t frame)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700230 REQUIRES_SHARED(Locks::mutator_lock_)
Nicolas Geoffrayc6df1e32016-07-04 10:15:47 +0100231 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700232 method_(nullptr),
233 dex_pc_(0),
234 current_frame_number_(0),
235 wanted_frame_number_(frame) {}
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700236 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700237 ArtMethod* m = GetMethod();
238 if (m == nullptr || m->IsRuntimeMethod()) {
239 // Runtime method, upcall, or resolution issue. Skip.
240 return true;
241 }
242
243 // Is this the requested frame?
244 if (current_frame_number_ == wanted_frame_number_) {
245 method_ = m;
246 dex_pc_ = GetDexPc(false /* abort_on_error*/);
247 return false;
248 }
249
250 // Look for more.
251 current_frame_number_++;
252 return true;
253 }
254
255 ArtMethod* method_;
256 uint32_t dex_pc_;
257
258 private:
259 size_t current_frame_number_;
260 const size_t wanted_frame_number_;
261};
262
263// This function is inlined and just helps to not have the VLOG and ATRACE check at all the
264// potential tracing points.
265void Monitor::AtraceMonitorLock(Thread* self, mirror::Object* obj, bool is_wait) {
266 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging) && ATRACE_ENABLED())) {
267 AtraceMonitorLockImpl(self, obj, is_wait);
268 }
269}
270
271void Monitor::AtraceMonitorLockImpl(Thread* self, mirror::Object* obj, bool is_wait) {
272 // Wait() requires a deeper call stack to be useful. Otherwise you'll see "Waiting at
273 // Object.java". Assume that we'll wait a nontrivial amount, so it's OK to do a longer
274 // stack walk than if !is_wait.
275 NthCallerWithDexPcVisitor visitor(self, is_wait ? 1U : 0U);
276 visitor.WalkStack(false);
277 const char* prefix = is_wait ? "Waiting on " : "Locking ";
278
279 const char* filename;
280 int32_t line_number;
281 TranslateLocation(visitor.method_, visitor.dex_pc_, &filename, &line_number);
282
283 // It would be nice to have a stable "ID" for the object here. However, the only stable thing
284 // would be the identity hashcode. But we cannot use IdentityHashcode here: For one, there are
285 // times when it is unsafe to make that call (see stack dumping for an explanation). More
286 // importantly, we would have to give up on thin-locking when adding systrace locks, as the
287 // identity hashcode is stored in the lockword normally (so can't be used with thin-locks).
288 //
289 // Because of thin-locks we also cannot use the monitor id (as there is no monitor). Monitor ids
290 // also do not have to be stable, as the monitor may be deflated.
291 std::string tmp = StringPrintf("%s %d at %s:%d",
292 prefix,
293 (obj == nullptr ? -1 : static_cast<int32_t>(reinterpret_cast<uintptr_t>(obj))),
294 (filename != nullptr ? filename : "null"),
295 line_number);
296 ATRACE_BEGIN(tmp.c_str());
297}
298
299void Monitor::AtraceMonitorUnlock() {
300 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging))) {
301 ATRACE_END();
302 }
303}
304
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700305std::string Monitor::PrettyContentionInfo(const std::string& owner_name,
306 pid_t owner_tid,
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700307 ArtMethod* owners_method,
308 uint32_t owners_dex_pc,
309 size_t num_waiters) {
Hiroshi Yamauchi71cd68d2017-01-25 18:28:12 -0800310 Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700311 const char* owners_filename;
Goran Jakovljevic49c882b2016-04-19 10:27:21 +0200312 int32_t owners_line_number = 0;
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700313 if (owners_method != nullptr) {
314 TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
315 }
316 std::ostringstream oss;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700317 oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700318 if (owners_method != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700319 oss << " at " << owners_method->PrettyMethod();
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700320 oss << "(" << owners_filename << ":" << owners_line_number << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700321 }
322 oss << " waiters=" << num_waiters;
323 return oss.str();
324}
325
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700326bool Monitor::TryLockLocked(Thread* self) {
327 if (owner_ == nullptr) { // Unowned.
328 owner_ = self;
329 CHECK_EQ(lock_count_, 0);
330 // When debugging, save the current monitor holder for future
331 // acquisition failures to use in sampled logging.
332 if (lock_profiling_threshold_ != 0) {
333 locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
334 }
335 } else if (owner_ == self) { // Recursive.
336 lock_count_++;
337 } else {
338 return false;
339 }
340 AtraceMonitorLock(self, GetObject(), false /* is_wait */);
341 return true;
342}
343
344bool Monitor::TryLock(Thread* self) {
345 MutexLock mu(self, monitor_lock_);
346 return TryLockLocked(self);
347}
348
Elliott Hughes5f791332011-09-15 17:45:30 -0700349void Monitor::Lock(Thread* self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700350 MutexLock mu(self, monitor_lock_);
351 while (true) {
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700352 if (TryLockLocked(self)) {
353 return;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700354 }
355 // Contended.
356 const bool log_contention = (lock_profiling_threshold_ != 0);
Xin Guanb894a192014-08-22 11:55:37 -0500357 uint64_t wait_start_ms = log_contention ? MilliTime() : 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 ArtMethod* owners_method = locking_method_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700359 uint32_t owners_dex_pc = locking_dex_pc_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700360 // Do this before releasing the lock so that we don't get deflated.
Mathieu Chartierb9001ab2014-10-03 13:28:46 -0700361 size_t num_waiters = num_waiters_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700362 ++num_waiters_;
Andreas Gampe2702d562017-02-06 09:48:00 -0800363
364 // If systrace logging is enabled, first look at the lock owner. Acquiring the monitor's
365 // lock and then re-acquiring the mutator lock can deadlock.
366 bool started_trace = false;
367 if (ATRACE_ENABLED()) {
368 if (owner_ != nullptr) { // Did the owner_ give the lock up?
369 std::ostringstream oss;
370 std::string name;
371 owner_->GetThreadName(name);
372 oss << PrettyContentionInfo(name,
373 owner_->GetTid(),
374 owners_method,
375 owners_dex_pc,
376 num_waiters);
377 // Add info for contending thread.
378 uint32_t pc;
379 ArtMethod* m = self->GetCurrentMethod(&pc);
380 const char* filename;
381 int32_t line_number;
382 TranslateLocation(m, pc, &filename, &line_number);
383 oss << " blocking from "
384 << ArtMethod::PrettyMethod(m) << "(" << (filename != nullptr ? filename : "null")
385 << ":" << line_number << ")";
386 ATRACE_BEGIN(oss.str().c_str());
387 started_trace = true;
388 }
389 }
390
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700391 monitor_lock_.Unlock(self); // Let go of locks in order.
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700392 self->SetMonitorEnterObject(GetObject());
Elliott Hughes5f791332011-09-15 17:45:30 -0700393 {
Hiroshi Yamauchi71cd68d2017-01-25 18:28:12 -0800394 ScopedThreadSuspension tsc(self, kBlocked); // Change to blocked and give up mutator_lock_.
Andreas Gampe2702d562017-02-06 09:48:00 -0800395 uint32_t original_owner_thread_id = 0u;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700396 {
397 // Reacquire monitor_lock_ without mutator_lock_ for Wait.
398 MutexLock mu2(self, monitor_lock_);
399 if (owner_ != nullptr) { // Did the owner_ give the lock up?
400 original_owner_thread_id = owner_->GetThreadId();
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700401 monitor_contenders_.Wait(self); // Still contended so wait.
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800402 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700403 }
404 if (original_owner_thread_id != 0u) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700405 // Woken from contention.
406 if (log_contention) {
Andreas Gampe111b1092017-06-22 20:28:23 -0700407 uint64_t wait_ms = MilliTime() - wait_start_ms;
408 uint32_t sample_percent;
409 if (wait_ms >= lock_profiling_threshold_) {
410 sample_percent = 100;
411 } else {
412 sample_percent = 100 * wait_ms / lock_profiling_threshold_;
413 }
414 if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
415 // Reacquire mutator_lock_ for logging.
416 ScopedObjectAccess soa(self);
Andreas Gampe111b1092017-06-22 20:28:23 -0700417
Andreas Gamped0210e52017-06-23 13:38:09 -0700418 bool owner_alive = false;
419 pid_t original_owner_tid = 0;
420 std::string original_owner_name;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700421
Andreas Gamped0210e52017-06-23 13:38:09 -0700422 const bool should_dump_stacks = stack_dump_lock_profiling_threshold_ > 0 &&
423 wait_ms > stack_dump_lock_profiling_threshold_;
424 std::string owner_stack_dump;
Andreas Gampe111b1092017-06-22 20:28:23 -0700425
Andreas Gamped0210e52017-06-23 13:38:09 -0700426 // Acquire thread-list lock to find thread and keep it from dying until we've got all
427 // the info we need.
428 {
429 MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_);
430
431 // Re-find the owner in case the thread got killed.
432 Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId(
433 original_owner_thread_id);
434
435 if (original_owner != nullptr) {
436 owner_alive = true;
437 original_owner_tid = original_owner->GetTid();
438 original_owner->GetThreadName(original_owner_name);
439
440 if (should_dump_stacks) {
441 // Very long contention. Dump stacks.
442 struct CollectStackTrace : public Closure {
443 void Run(art::Thread* thread) OVERRIDE
444 REQUIRES_SHARED(art::Locks::mutator_lock_) {
445 thread->DumpJavaStack(oss);
446 }
447
448 std::ostringstream oss;
449 };
450 CollectStackTrace owner_trace;
451 original_owner->RequestSynchronousCheckpoint(&owner_trace);
452 owner_stack_dump = owner_trace.oss.str();
453 }
454 }
455 // This is all the data we need. Now drop the thread-list lock, it's OK for the
456 // owner to go away now.
457 }
458
459 // If we found the owner (and thus have owner data), go and log now.
460 if (owner_alive) {
461 // Give the detailed traces for really long contention.
462 if (should_dump_stacks) {
463 // This must be here (and not above) because we cannot hold the thread-list lock
464 // while running the checkpoint.
465 std::ostringstream self_trace_oss;
466 self->DumpJavaStack(self_trace_oss);
467
468 uint32_t pc;
469 ArtMethod* m = self->GetCurrentMethod(&pc);
470
471 LOG(WARNING) << "Long "
472 << PrettyContentionInfo(original_owner_name,
473 original_owner_tid,
474 owners_method,
475 owners_dex_pc,
476 num_waiters)
477 << " in " << ArtMethod::PrettyMethod(m) << " for "
478 << PrettyDuration(MsToNs(wait_ms)) << "\n"
479 << "Current owner stack:\n" << owner_stack_dump
480 << "Contender stack:\n" << self_trace_oss.str();
481 } else if (wait_ms > kLongWaitMs && owners_method != nullptr) {
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700482 uint32_t pc;
483 ArtMethod* m = self->GetCurrentMethod(&pc);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700484 // TODO: We should maybe check that original_owner is still a live thread.
485 LOG(WARNING) << "Long "
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700486 << PrettyContentionInfo(original_owner_name,
487 original_owner_tid,
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700488 owners_method,
489 owners_dex_pc,
490 num_waiters)
David Sehr709b0702016-10-13 09:12:37 -0700491 << " in " << ArtMethod::PrettyMethod(m) << " for "
492 << PrettyDuration(MsToNs(wait_ms));
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700493 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700494 LogContentionEvent(self,
495 wait_ms,
496 sample_percent,
Andreas Gampe39b98112017-06-01 16:28:27 -0700497 owners_method,
498 owners_dex_pc);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700499 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700500 }
501 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700502 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700503 }
Andreas Gampe2702d562017-02-06 09:48:00 -0800504 if (started_trace) {
505 ATRACE_END();
506 }
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700507 self->SetMonitorEnterObject(nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700508 monitor_lock_.Lock(self); // Reacquire locks in order.
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700509 --num_waiters_;
Elliott Hughesfc861622011-10-17 17:57:47 -0700510 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700511}
512
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800513static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
514 __attribute__((format(printf, 1, 2)));
515
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700516static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700517 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800518 va_list args;
519 va_start(args, fmt);
Ian Rogers62d6c772013-02-27 08:32:07 -0800520 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000521 self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700522 if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
Brian Carlstrom64277f32012-03-26 23:53:34 -0700523 std::ostringstream ss;
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 self->Dump(ss);
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700525 LOG(Runtime::Current()->IsStarted() ? ::android::base::INFO : ::android::base::ERROR)
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000526 << self->GetException()->Dump() << "\n" << ss.str();
Brian Carlstrom64277f32012-03-26 23:53:34 -0700527 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800528 va_end(args);
529}
530
Elliott Hughesd4237412012-02-21 11:24:45 -0800531static std::string ThreadToString(Thread* thread) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700532 if (thread == nullptr) {
533 return "nullptr";
Elliott Hughesd4237412012-02-21 11:24:45 -0800534 }
535 std::ostringstream oss;
536 // TODO: alternatively, we could just return the thread's name.
537 oss << *thread;
538 return oss.str();
539}
540
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700541void Monitor::FailedUnlock(mirror::Object* o,
542 uint32_t expected_owner_thread_id,
543 uint32_t found_owner_thread_id,
Elliott Hughesffb465f2012-03-01 18:46:05 -0800544 Monitor* monitor) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700545 // Acquire thread list lock so threads won't disappear from under us.
Elliott Hughesffb465f2012-03-01 18:46:05 -0800546 std::string current_owner_string;
547 std::string expected_owner_string;
548 std::string found_owner_string;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700549 uint32_t current_owner_thread_id = 0u;
Elliott Hughesffb465f2012-03-01 18:46:05 -0800550 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700551 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700552 ThreadList* const thread_list = Runtime::Current()->GetThreadList();
553 Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id);
554 Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id);
555
Elliott Hughesffb465f2012-03-01 18:46:05 -0800556 // Re-read owner now that we hold lock.
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700557 Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr;
558 if (current_owner != nullptr) {
559 current_owner_thread_id = current_owner->GetThreadId();
560 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800561 // Get short descriptions of the threads involved.
562 current_owner_string = ThreadToString(current_owner);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700563 expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed";
564 found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed";
Elliott Hughesffb465f2012-03-01 18:46:05 -0800565 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700566
567 if (current_owner_thread_id == 0u) {
568 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800569 ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
570 " on thread '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700571 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800572 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800573 } else {
574 // Race: the original read found an owner but now there is none
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800575 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
576 " (where now the monitor appears unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800577 found_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700578 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800579 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800580 }
581 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700582 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800583 // Race: originally there was no owner, there is now
584 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
585 " (originally believed to be unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800586 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700587 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800588 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800589 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700590 if (found_owner_thread_id != current_owner_thread_id) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800591 // Race: originally found and current owner have changed
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800592 ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
593 " owned by '%s') on object of type '%s' on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800594 found_owner_string.c_str(),
595 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700596 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800597 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800598 } else {
599 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
600 " on thread '%s",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800601 current_owner_string.c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700602 mirror::Object::PrettyTypeOf(o).c_str(),
Elliott Hughesffb465f2012-03-01 18:46:05 -0800603 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800604 }
605 }
606 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700607}
608
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700609bool Monitor::Unlock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700610 DCHECK(self != nullptr);
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700611 uint32_t owner_thread_id = 0u;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700612 {
613 MutexLock mu(self, monitor_lock_);
614 Thread* owner = owner_;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700615 if (owner != nullptr) {
616 owner_thread_id = owner->GetThreadId();
617 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700618 if (owner == self) {
619 // We own the monitor, so nobody else can be in here.
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700620 AtraceMonitorUnlock();
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700621 if (lock_count_ == 0) {
622 owner_ = nullptr;
623 locking_method_ = nullptr;
624 locking_dex_pc_ = 0;
625 // Wake a contender.
626 monitor_contenders_.Signal(self);
627 } else {
628 --lock_count_;
629 }
630 return true;
Elliott Hughes5f791332011-09-15 17:45:30 -0700631 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700632 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700633 // We don't own this, so we're not allowed to unlock it.
634 // The JNI spec says that we should throw IllegalMonitorStateException in this case.
635 FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this);
636 return false;
Elliott Hughes5f791332011-09-15 17:45:30 -0700637}
638
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800639void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
640 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700641 DCHECK(self != nullptr);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800642 DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
Elliott Hughes5f791332011-09-15 17:45:30 -0700643
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700644 monitor_lock_.Lock(self);
645
Elliott Hughes5f791332011-09-15 17:45:30 -0700646 // Make sure that we hold the lock.
647 if (owner_ != self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700648 monitor_lock_.Unlock(self);
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700649 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700650 return;
651 }
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800652
Elliott Hughesdf42c482013-01-09 12:49:02 -0800653 // We need to turn a zero-length timed wait into a regular wait because
654 // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
655 if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
656 why = kWaiting;
657 }
658
Elliott Hughes5f791332011-09-15 17:45:30 -0700659 // Enforce the timeout range.
660 if (ms < 0 || ns < 0 || ns > 999999) {
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700661 monitor_lock_.Unlock(self);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000662 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800663 "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
Elliott Hughes5f791332011-09-15 17:45:30 -0700664 return;
665 }
666
Elliott Hughes5f791332011-09-15 17:45:30 -0700667 /*
668 * Add ourselves to the set of threads waiting on this monitor, and
669 * release our hold. We need to let it go even if we're a few levels
670 * deep in a recursive lock, and we need to restore that later.
671 *
672 * We append to the wait set ahead of clearing the count and owner
673 * fields so the subroutine can check that the calling thread owns
674 * the monitor. Aside from that, the order of member updates is
675 * not order sensitive as we hold the pthread mutex.
676 */
677 AppendToWaitSet(self);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700678 ++num_waiters_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700679 int prev_lock_count = lock_count_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700680 lock_count_ = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700681 owner_ = nullptr;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700682 ArtMethod* saved_method = locking_method_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700683 locking_method_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700684 uintptr_t saved_dex_pc = locking_dex_pc_;
685 locking_dex_pc_ = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700686
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700687 AtraceMonitorUnlock(); // For the implict Unlock() just above. This will only end the deepest
688 // nesting, but that is enough for the visualization, and corresponds to
689 // the single Lock() we do afterwards.
690 AtraceMonitorLock(self, GetObject(), true /* is_wait */);
691
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800692 bool was_interrupted = false;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700693 {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700694 // Update thread state. If the GC wakes up, it'll ignore us, knowing
695 // that we won't touch any references in this state, and we'll check
696 // our suspend mode before we transition out.
697 ScopedThreadSuspension sts(self, why);
698
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700699 // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700700 MutexLock mu(self, *self->GetWaitMutex());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700701
702 // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700703 // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700704 // up.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700705 DCHECK(self->GetWaitMonitor() == nullptr);
706 self->SetWaitMonitor(this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700707
708 // Release the monitor lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700709 monitor_contenders_.Signal(self);
710 monitor_lock_.Unlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700711
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800712 // Handle the case where the thread was interrupted before we called wait().
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000713 if (self->IsInterrupted()) {
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800714 was_interrupted = true;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700715 } else {
716 // Wait for a notification or a timeout to occur.
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800717 if (why == kWaiting) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700718 self->GetWaitConditionVariable()->Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700719 } else {
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800720 DCHECK(why == kTimedWaiting || why == kSleeping) << why;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700721 self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700722 }
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000723 was_interrupted = self->IsInterrupted();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700724 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700725 }
726
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800727 {
728 // We reset the thread's wait_monitor_ field after transitioning back to runnable so
729 // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
730 // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
731 // are waiting on "null".)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700732 MutexLock mu(self, *self->GetWaitMutex());
733 DCHECK(self->GetWaitMonitor() != nullptr);
734 self->SetWaitMonitor(nullptr);
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800735 }
736
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800737 // Allocate the interrupted exception not holding the monitor lock since it may cause a GC.
738 // If the GC requires acquiring the monitor for enqueuing cleared references, this would
739 // cause a deadlock if the monitor is held.
740 if (was_interrupted && interruptShouldThrow) {
741 /*
742 * We were interrupted while waiting, or somebody interrupted an
743 * un-interruptible thread earlier and we're bailing out immediately.
744 *
745 * The doc sayeth: "The interrupted status of the current thread is
746 * cleared when this exception is thrown."
747 */
Nicolas Geoffray365719c2017-03-08 13:11:50 +0000748 self->SetInterrupted(false);
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800749 self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr);
750 }
751
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700752 AtraceMonitorUnlock(); // End Wait().
753
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700754 // Re-acquire the monitor and lock.
Elliott Hughes5f791332011-09-15 17:45:30 -0700755 Lock(self);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700756 monitor_lock_.Lock(self);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700757 self->GetWaitMutex()->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700758
Elliott Hughes5f791332011-09-15 17:45:30 -0700759 /*
760 * We remove our thread from wait set after restoring the count
761 * and owner fields so the subroutine can check that the calling
762 * thread owns the monitor. Aside from that, the order of member
763 * updates is not order sensitive as we hold the pthread mutex.
764 */
765 owner_ = self;
Ian Rogers0399dde2012-06-06 17:09:28 -0700766 lock_count_ = prev_lock_count;
767 locking_method_ = saved_method;
768 locking_dex_pc_ = saved_dex_pc;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700769 --num_waiters_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700770 RemoveFromWaitSet(self);
771
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700772 monitor_lock_.Unlock(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700773}
774
775void Monitor::Notify(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700776 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700777 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700778 // Make sure that we hold the lock.
779 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800780 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700781 return;
782 }
783 // Signal the first waiting thread in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700784 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700785 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700786 wait_set_ = thread->GetWaitNext();
787 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700788
789 // Check to see if the thread is still waiting.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800790 MutexLock wait_mu(self, *thread->GetWaitMutex());
Ian Rogersdd7624d2014-03-14 17:43:00 -0700791 if (thread->GetWaitMonitor() != nullptr) {
792 thread->GetWaitConditionVariable()->Signal(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700793 return;
794 }
795 }
796}
797
798void Monitor::NotifyAll(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700799 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700800 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700801 // Make sure that we hold the lock.
802 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800803 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700804 return;
805 }
806 // Signal all threads in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700807 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700808 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700809 wait_set_ = thread->GetWaitNext();
810 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700811 thread->Notify();
812 }
813}
814
Mathieu Chartier590fee92013-09-13 13:46:47 -0700815bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
816 DCHECK(obj != nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700817 // Don't need volatile since we only deflate with mutators suspended.
818 LockWord lw(obj->GetLockWord(false));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700819 // If the lock isn't an inflated monitor, then we don't need to deflate anything.
820 if (lw.GetState() == LockWord::kFatLocked) {
821 Monitor* monitor = lw.FatLockMonitor();
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700822 DCHECK(monitor != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700823 MutexLock mu(self, monitor->monitor_lock_);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700824 // Can't deflate if we have anybody waiting on the CV.
825 if (monitor->num_waiters_ > 0) {
826 return false;
827 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700828 Thread* owner = monitor->owner_;
829 if (owner != nullptr) {
830 // Can't deflate if we are locked and have a hash code.
831 if (monitor->HasHashCode()) {
832 return false;
833 }
834 // Can't deflate if our lock count is too high.
Mathieu Chartier1cf194f2016-11-01 20:13:24 -0700835 if (static_cast<uint32_t>(monitor->lock_count_) > LockWord::kThinLockMaxCount) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700836 return false;
837 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700838 // Deflate to a thin lock.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700839 LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(),
840 monitor->lock_count_,
841 lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800842 // Assume no concurrent read barrier state changes as mutators are suspended.
843 obj->SetLockWord(new_lw, false);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700844 VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
845 << monitor->lock_count_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700846 } else if (monitor->HasHashCode()) {
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700847 LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800848 // Assume no concurrent read barrier state changes as mutators are suspended.
849 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700850 VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700851 } else {
852 // No lock and no hash, just put an empty lock word inside the object.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700853 LockWord new_lw = LockWord::FromDefault(lw.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800854 // Assume no concurrent read barrier state changes as mutators are suspended.
855 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700856 VLOG(monitor) << "Deflated" << obj << " to empty lock word";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700857 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700858 // 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 -0700859 // next GC.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700860 monitor->obj_ = GcRoot<mirror::Object>(nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700861 }
862 return true;
863}
864
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700865void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
Andreas Gampe74240812014-04-17 10:35:09 -0700866 DCHECK(self != nullptr);
867 DCHECK(obj != nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700868 // Allocate and acquire a new monitor.
Andreas Gampe74240812014-04-17 10:35:09 -0700869 Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code);
870 DCHECK(m != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700871 if (m->Install(self)) {
Haifeng Li86ab7912014-05-16 10:47:59 +0800872 if (owner != nullptr) {
873 VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
Andreas Gampe74240812014-04-17 10:35:09 -0700874 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800875 } else {
876 VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
Andreas Gampe74240812014-04-17 10:35:09 -0700877 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800878 }
Andreas Gampe74240812014-04-17 10:35:09 -0700879 Runtime::Current()->GetMonitorList()->Add(m);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700880 CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
Andreas Gampe74240812014-04-17 10:35:09 -0700881 } else {
882 MonitorPool::ReleaseMonitor(self, m);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700883 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700884}
885
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700886void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700887 uint32_t hash_code) {
888 DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
889 uint32_t owner_thread_id = lock_word.ThinLockOwner();
890 if (owner_thread_id == self->GetThreadId()) {
891 // We own the monitor, we can easily inflate it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700892 Inflate(self, self, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700893 } else {
894 ThreadList* thread_list = Runtime::Current()->GetThreadList();
895 // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700896 self->SetMonitorEnterObject(obj.Get());
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700897 bool timed_out;
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700898 Thread* owner;
899 {
900 ScopedThreadSuspension sts(self, kBlocked);
Alex Light46f93402017-06-29 11:59:50 -0700901 owner = thread_list->SuspendThreadByThreadId(owner_thread_id,
902 SuspendReason::kInternal,
903 &timed_out);
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700904 }
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700905 if (owner != nullptr) {
906 // We succeeded in suspending the thread, check the lock's status didn't change.
907 lock_word = obj->GetLockWord(true);
908 if (lock_word.GetState() == LockWord::kThinLocked &&
909 lock_word.ThinLockOwner() == owner_thread_id) {
910 // Go ahead and inflate the lock.
911 Inflate(self, owner, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700912 }
Alex Light88fd7202017-06-30 08:31:59 -0700913 bool resumed = thread_list->Resume(owner, SuspendReason::kInternal);
914 DCHECK(resumed);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700915 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700916 self->SetMonitorEnterObject(nullptr);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700917 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700918}
919
Ian Rogers719d1a32014-03-06 12:13:39 -0800920// Fool annotalysis into thinking that the lock on obj is acquired.
921static mirror::Object* FakeLock(mirror::Object* obj)
922 EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
923 return obj;
924}
925
926// Fool annotalysis into thinking that the lock on obj is release.
927static mirror::Object* FakeUnlock(mirror::Object* obj)
928 UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
929 return obj;
930}
931
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700932mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj, bool trylock) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700933 DCHECK(self != nullptr);
934 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -0700935 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -0800936 obj = FakeLock(obj);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700937 uint32_t thread_id = self->GetThreadId();
938 size_t contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700939 StackHandleScope<1> hs(self);
940 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700941 while (true) {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800942 // We initially read the lockword with ordinary Java/relaxed semantics. When stronger
943 // semantics are needed, we address it below. Since GetLockWord bottoms out to a relaxed load,
944 // we can fix it later, in an infrequently executed case, with a fence.
945 LockWord lock_word = h_obj->GetLockWord(false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700946 switch (lock_word.GetState()) {
947 case LockWord::kUnlocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800948 // No ordering required for preceding lockword read, since we retest.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700949 LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.GCState()));
Hans Boehmb3da36c2016-12-15 13:12:59 -0800950 if (h_obj->CasLockWordWeakAcquire(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700951 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700952 return h_obj.Get(); // Success!
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700953 }
954 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700955 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700956 case LockWord::kThinLocked: {
957 uint32_t owner_thread_id = lock_word.ThinLockOwner();
958 if (owner_thread_id == thread_id) {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800959 // No ordering required for initial lockword read.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700960 // We own the lock, increase the recursion count.
961 uint32_t new_count = lock_word.ThinLockCount() + 1;
962 if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700963 LockWord thin_locked(LockWord::FromThinLockId(thread_id,
964 new_count,
965 lock_word.GCState()));
Hans Boehmb3da36c2016-12-15 13:12:59 -0800966 // Only this thread pays attention to the count. Thus there is no need for stronger
967 // than relaxed memory ordering.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800968 if (!kUseReadBarrier) {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800969 h_obj->SetLockWord(thin_locked, false /* volatile */);
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700970 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800971 return h_obj.Get(); // Success!
972 } else {
973 // Use CAS to preserve the read barrier state.
Hans Boehmb3da36c2016-12-15 13:12:59 -0800974 if (h_obj->CasLockWordWeakRelaxed(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700975 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800976 return h_obj.Get(); // Success!
977 }
978 }
979 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700980 } else {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700981 // We'd overflow the recursion count, so inflate the monitor.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700982 InflateThinLocked(self, h_obj, lock_word, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700983 }
984 } else {
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700985 if (trylock) {
986 return nullptr;
987 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700988 // Contention.
989 contention_count++;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700990 Runtime* runtime = Runtime::Current();
Hans Boehmb3da36c2016-12-15 13:12:59 -0800991 if (contention_count <= runtime->GetMaxSpinsBeforeThinLockInflation()) {
Mathieu Chartierb363f662014-07-16 13:28:58 -0700992 // TODO: Consider switching the thread state to kBlocked when we are yielding.
Mathieu Chartier251755c2014-07-15 18:10:25 -0700993 // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the
994 // parameter you pass in. This can cause thread suspension to take excessively long
Mathieu Chartierb363f662014-07-16 13:28:58 -0700995 // and make long pauses. See b/16307460.
Hans Boehmb3da36c2016-12-15 13:12:59 -0800996 // TODO: We should literally spin first, without sched_yield. Sched_yield either does
997 // nothing (at significant expense), or guarantees that we wait at least microseconds.
998 // If the owner is running, I would expect the median lock hold time to be hundreds
999 // of nanoseconds or less.
Mathieu Chartier251755c2014-07-15 18:10:25 -07001000 sched_yield();
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001001 } else {
1002 contention_count = 0;
Hans Boehmb3da36c2016-12-15 13:12:59 -08001003 // No ordering required for initial lockword read. Install rereads it anyway.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001004 InflateThinLocked(self, h_obj, lock_word, 0);
Elliott Hughes5f791332011-09-15 17:45:30 -07001005 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001006 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001007 continue; // Start from the beginning.
Elliott Hughes5f791332011-09-15 17:45:30 -07001008 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001009 case LockWord::kFatLocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001010 // We should have done an acquire read of the lockword initially, to ensure
1011 // visibility of the monitor data structure. Use an explicit fence instead.
1012 QuasiAtomic::ThreadFenceAcquire();
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001013 Monitor* mon = lock_word.FatLockMonitor();
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001014 if (trylock) {
1015 return mon->TryLock(self) ? h_obj.Get() : nullptr;
1016 } else {
1017 mon->Lock(self);
1018 return h_obj.Get(); // Success!
1019 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001020 }
Ian Rogers719d1a32014-03-06 12:13:39 -08001021 case LockWord::kHashCode:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001022 // Inflate with the existing hashcode.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001023 // Again no ordering required for initial lockword read, since we don't rely
1024 // on the visibility of any prior computation.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001025 Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
Ian Rogers719d1a32014-03-06 12:13:39 -08001026 continue; // Start from the beginning.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001027 default: {
1028 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Andreas Gampec7ed09b2016-04-25 20:08:55 -07001029 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001030 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001031 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001032 }
1033}
1034
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001035bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001036 DCHECK(self != nullptr);
1037 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -07001038 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -08001039 obj = FakeUnlock(obj);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001040 StackHandleScope<1> hs(self);
1041 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001042 while (true) {
1043 LockWord lock_word = obj->GetLockWord(true);
1044 switch (lock_word.GetState()) {
1045 case LockWord::kHashCode:
1046 // Fall-through.
1047 case LockWord::kUnlocked:
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001048 FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001049 return false; // Failure.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001050 case LockWord::kThinLocked: {
1051 uint32_t thread_id = self->GetThreadId();
1052 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1053 if (owner_thread_id != thread_id) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001054 FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001055 return false; // Failure.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001056 } else {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001057 // We own the lock, decrease the recursion count.
1058 LockWord new_lw = LockWord::Default();
1059 if (lock_word.ThinLockCount() != 0) {
1060 uint32_t new_count = lock_word.ThinLockCount() - 1;
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001061 new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001062 } else {
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001063 new_lw = LockWord::FromDefault(lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001064 }
1065 if (!kUseReadBarrier) {
1066 DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
Hans Boehmb3da36c2016-12-15 13:12:59 -08001067 // TODO: This really only needs memory_order_release, but we currently have
1068 // no way to specify that. In fact there seem to be no legitimate uses of SetLockWord
1069 // with a final argument of true. This slows down x86 and ARMv7, but probably not v8.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001070 h_obj->SetLockWord(new_lw, true);
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001071 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001072 // Success!
1073 return true;
1074 } else {
1075 // Use CAS to preserve the read barrier state.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001076 if (h_obj->CasLockWordWeakRelease(lock_word, new_lw)) {
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001077 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001078 // Success!
1079 return true;
1080 }
1081 }
1082 continue; // Go again.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001083 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001084 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001085 case LockWord::kFatLocked: {
1086 Monitor* mon = lock_word.FatLockMonitor();
1087 return mon->Unlock(self);
1088 }
1089 default: {
1090 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1091 return false;
1092 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001093 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001094 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001095}
1096
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001097void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
Elliott Hughes4cd121e2013-01-07 17:35:41 -08001098 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001099 DCHECK(self != nullptr);
1100 DCHECK(obj != nullptr);
1101 LockWord lock_word = obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -07001102 while (lock_word.GetState() != LockWord::kFatLocked) {
1103 switch (lock_word.GetState()) {
1104 case LockWord::kHashCode:
1105 // Fall-through.
1106 case LockWord::kUnlocked:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001107 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1108 return; // Failure.
Ian Rogers43c69cc2014-08-15 11:09:28 -07001109 case LockWord::kThinLocked: {
1110 uint32_t thread_id = self->GetThreadId();
1111 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1112 if (owner_thread_id != thread_id) {
1113 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1114 return; // Failure.
1115 } else {
1116 // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
1117 // re-load.
1118 Inflate(self, self, obj, 0);
1119 lock_word = obj->GetLockWord(true);
1120 }
1121 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001122 }
Ian Rogers43c69cc2014-08-15 11:09:28 -07001123 case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through.
1124 default: {
1125 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1126 return;
1127 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001128 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001129 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001130 Monitor* mon = lock_word.FatLockMonitor();
1131 mon->Wait(self, ms, ns, interruptShouldThrow, why);
Elliott Hughes5f791332011-09-15 17:45:30 -07001132}
1133
Ian Rogers13c479e2013-10-11 07:59:01 -07001134void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001135 DCHECK(self != nullptr);
1136 DCHECK(obj != nullptr);
1137 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001138 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001139 case LockWord::kHashCode:
1140 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001141 case LockWord::kUnlocked:
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001142 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001143 return; // Failure.
1144 case LockWord::kThinLocked: {
1145 uint32_t thread_id = self->GetThreadId();
1146 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1147 if (owner_thread_id != thread_id) {
1148 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
1149 return; // Failure.
1150 } else {
1151 // We own the lock but there's no Monitor and therefore no waiters.
1152 return; // Success.
1153 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001154 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001155 case LockWord::kFatLocked: {
1156 Monitor* mon = lock_word.FatLockMonitor();
1157 if (notify_all) {
1158 mon->NotifyAll(self);
1159 } else {
1160 mon->Notify(self);
1161 }
1162 return; // Success.
1163 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001164 default: {
1165 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1166 return;
1167 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001168 }
1169}
1170
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001171uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001172 DCHECK(obj != nullptr);
1173 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001174 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001175 case LockWord::kHashCode:
1176 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001177 case LockWord::kUnlocked:
1178 return ThreadList::kInvalidThreadId;
1179 case LockWord::kThinLocked:
1180 return lock_word.ThinLockOwner();
1181 case LockWord::kFatLocked: {
1182 Monitor* mon = lock_word.FatLockMonitor();
1183 return mon->GetOwnerThreadId();
Elliott Hughes5f791332011-09-15 17:45:30 -07001184 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001185 default: {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001186 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001187 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001188 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001189 }
1190}
1191
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001192void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001193 // Determine the wait message and object we're waiting or blocked upon.
1194 mirror::Object* pretty_object = nullptr;
1195 const char* wait_message = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001196 uint32_t lock_owner = ThreadList::kInvalidThreadId;
Ian Rogersd803bc72014-04-01 15:33:03 -07001197 ThreadState state = thread->GetState();
Elliott Hughesb4e94fd2013-01-08 14:41:26 -08001198 if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001199 wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on ";
1200 Thread* self = Thread::Current();
1201 MutexLock mu(self, *thread->GetWaitMutex());
1202 Monitor* monitor = thread->GetWaitMonitor();
1203 if (monitor != nullptr) {
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001204 pretty_object = monitor->GetObject();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001205 }
Elliott Hughes34e06962012-04-09 13:55:55 -07001206 } else if (state == kBlocked) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001207 wait_message = " - waiting to lock ";
1208 pretty_object = thread->GetMonitorEnterObject();
1209 if (pretty_object != nullptr) {
Hiroshi Yamauchi7b08ae42016-10-04 15:20:36 -07001210 if (kUseReadBarrier && Thread::Current()->GetIsGcMarking()) {
1211 // We may call Thread::Dump() in the middle of the CC thread flip and this thread's stack
1212 // may have not been flipped yet and "pretty_object" may be a from-space (stale) ref, in
1213 // which case the GetLockOwnerThreadId() call below will crash. So explicitly mark/forward
1214 // it here.
1215 pretty_object = ReadBarrier::Mark(pretty_object);
1216 }
Ian Rogersd803bc72014-04-01 15:33:03 -07001217 lock_owner = pretty_object->GetLockOwnerThreadId();
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001218 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001219 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001220
Ian Rogersd803bc72014-04-01 15:33:03 -07001221 if (wait_message != nullptr) {
1222 if (pretty_object == nullptr) {
1223 os << wait_message << "an unknown object";
1224 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001225 if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
Ian Rogersd803bc72014-04-01 15:33:03 -07001226 Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
1227 // Getting the identity hashcode here would result in lock inflation and suspension of the
1228 // current thread, which isn't safe if this is the only runnable thread.
1229 os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
1230 reinterpret_cast<intptr_t>(pretty_object),
David Sehr709b0702016-10-13 09:12:37 -07001231 pretty_object->PrettyTypeOf().c_str());
Ian Rogersd803bc72014-04-01 15:33:03 -07001232 } else {
1233 // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
Mathieu Chartier49361592015-01-22 16:36:10 -08001234 // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
1235 // suspension and move pretty_object.
David Sehr709b0702016-10-13 09:12:37 -07001236 const std::string pretty_type(pretty_object->PrettyTypeOf());
Ian Rogersd803bc72014-04-01 15:33:03 -07001237 os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
Mathieu Chartier49361592015-01-22 16:36:10 -08001238 pretty_type.c_str());
Ian Rogersd803bc72014-04-01 15:33:03 -07001239 }
1240 }
1241 // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
1242 if (lock_owner != ThreadList::kInvalidThreadId) {
1243 os << " held by thread " << lock_owner;
1244 }
1245 os << "\n";
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001246 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001247}
1248
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001249mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001250 // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
1251 // definition of contended that includes a monitor a thread is trying to enter...
Ian Rogersdd7624d2014-03-14 17:43:00 -07001252 mirror::Object* result = thread->GetMonitorEnterObject();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001253 if (result == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001254 // ...but also a monitor that the thread is waiting on.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001255 MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
1256 Monitor* monitor = thread->GetWaitMonitor();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001257 if (monitor != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001258 result = monitor->GetObject();
Elliott Hughesf9501702013-01-11 11:22:27 -08001259 }
1260 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001261 return result;
Elliott Hughesf9501702013-01-11 11:22:27 -08001262}
1263
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001264void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
Andreas Gampe760172c2014-08-16 13:41:10 -07001265 void* callback_context, bool abort_on_failure) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001266 ArtMethod* m = stack_visitor->GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001267 CHECK(m != nullptr);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001268
1269 // Native methods are an easy special case.
1270 // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
1271 if (m->IsNative()) {
1272 if (m->IsSynchronized()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001273 mirror::Object* jni_this =
1274 stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001275 callback(jni_this, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001276 }
1277 return;
1278 }
1279
jeffhao61f916c2012-10-25 17:48:51 -07001280 // Proxy methods should not be synchronized.
1281 if (m->IsProxyMethod()) {
1282 CHECK(!m->IsSynchronized());
1283 return;
1284 }
1285
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001286 // Is there any reason to believe there's any synchronization in this method?
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001287 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -07001288 CHECK(code_item != nullptr) << m->PrettyMethod();
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001289 if (code_item->tries_size_ == 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001290 return; // No "tries" implies no synchronization, so no held locks to report.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001291 }
1292
Andreas Gampe760172c2014-08-16 13:41:10 -07001293 // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
1294 // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
1295 // inconsistent stack anyways.
1296 uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
1297 if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) {
David Sehr709b0702016-10-13 09:12:37 -07001298 LOG(ERROR) << "Could not find dex_pc for " << m->PrettyMethod();
Andreas Gampe760172c2014-08-16 13:41:10 -07001299 return;
1300 }
1301
Elliott Hughes80537bb2013-01-04 16:37:26 -08001302 // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
1303 // the locks held in this stack frame.
1304 std::vector<uint32_t> monitor_enter_dex_pcs;
Andreas Gampe760172c2014-08-16 13:41:10 -07001305 verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs);
Mathieu Chartiere6a8eec2015-01-06 14:17:57 -08001306 for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) {
Elliott Hughes80537bb2013-01-04 16:37:26 -08001307 // The verifier works in terms of the dex pcs of the monitor-enter instructions.
1308 // We want the registers used by those instructions (so we can read the values out of them).
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001309 const Instruction* monitor_enter_instruction =
1310 Instruction::At(&code_item->insns_[monitor_dex_pc]);
Elliott Hughes80537bb2013-01-04 16:37:26 -08001311
1312 // Quick sanity check.
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001313 CHECK_EQ(monitor_enter_instruction->Opcode(), Instruction::MONITOR_ENTER)
1314 << "expected monitor-enter @" << monitor_dex_pc << "; was "
1315 << reinterpret_cast<const void*>(monitor_enter_instruction);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001316
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001317 uint16_t monitor_register = monitor_enter_instruction->VRegA();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001318 uint32_t value;
1319 bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value);
1320 CHECK(success) << "Failed to read v" << monitor_register << " of kind "
David Sehr709b0702016-10-13 09:12:37 -07001321 << kReferenceVReg << " in method " << m->PrettyMethod();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001322 mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001323 callback(o, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001324 }
1325}
1326
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001327bool Monitor::IsValidLockWord(LockWord lock_word) {
1328 switch (lock_word.GetState()) {
1329 case LockWord::kUnlocked:
1330 // Nothing to check.
1331 return true;
1332 case LockWord::kThinLocked:
1333 // Basic sanity check of owner.
1334 return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1335 case LockWord::kFatLocked: {
1336 // Check the monitor appears in the monitor list.
1337 Monitor* mon = lock_word.FatLockMonitor();
1338 MonitorList* list = Runtime::Current()->GetMonitorList();
1339 MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1340 for (Monitor* list_mon : list->list_) {
1341 if (mon == list_mon) {
1342 return true; // Found our monitor.
1343 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001344 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001345 return false; // Fail - unowned monitor in an object.
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001346 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001347 case LockWord::kHashCode:
1348 return true;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001349 default:
1350 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001351 UNREACHABLE();
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001352 }
1353}
1354
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001355bool Monitor::IsLocked() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001356 MutexLock mu(Thread::Current(), monitor_lock_);
1357 return owner_ != nullptr;
1358}
1359
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -07001360void Monitor::TranslateLocation(ArtMethod* method,
1361 uint32_t dex_pc,
1362 const char** source_file,
1363 int32_t* line_number) {
jeffhao33dc7712011-11-09 17:54:24 -08001364 // If method is null, location is unknown
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001365 if (method == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001366 *source_file = "";
1367 *line_number = 0;
jeffhao33dc7712011-11-09 17:54:24 -08001368 return;
1369 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001370 *source_file = method->GetDeclaringClassSourceFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001371 if (*source_file == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001372 *source_file = "";
Elliott Hughes12c51e32012-01-17 20:25:05 -08001373 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001374 *line_number = method->GetLineNumFromDexPC(dex_pc);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001375}
1376
1377uint32_t Monitor::GetOwnerThreadId() {
1378 MutexLock mu(Thread::Current(), monitor_lock_);
1379 Thread* owner = owner_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001380 if (owner != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001381 return owner->GetThreadId();
1382 } else {
1383 return ThreadList::kInvalidThreadId;
1384 }
jeffhao33dc7712011-11-09 17:54:24 -08001385}
1386
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001387MonitorList::MonitorList()
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001388 : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001389 monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001390}
1391
1392MonitorList::~MonitorList() {
Andreas Gampe74240812014-04-17 10:35:09 -07001393 Thread* self = Thread::Current();
1394 MutexLock mu(self, monitor_list_lock_);
1395 // Release all monitors to the pool.
1396 // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
1397 // clear faster in the pool.
1398 MonitorPool::ReleaseMonitors(self, &list_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001399}
1400
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001401void MonitorList::DisallowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001402 CHECK(!kUseReadBarrier);
Ian Rogers50b35e22012-10-04 10:09:15 -07001403 MutexLock mu(Thread::Current(), monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001404 allow_new_monitors_ = false;
1405}
1406
1407void MonitorList::AllowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001408 CHECK(!kUseReadBarrier);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001409 Thread* self = Thread::Current();
1410 MutexLock mu(self, monitor_list_lock_);
1411 allow_new_monitors_ = true;
1412 monitor_add_condition_.Broadcast(self);
1413}
1414
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001415void MonitorList::BroadcastForNewMonitors() {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001416 Thread* self = Thread::Current();
1417 MutexLock mu(self, monitor_list_lock_);
1418 monitor_add_condition_.Broadcast(self);
1419}
1420
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001421void MonitorList::Add(Monitor* m) {
1422 Thread* self = Thread::Current();
1423 MutexLock mu(self, monitor_list_lock_);
Hiroshi Yamauchif1c6f872017-01-06 12:23:47 -08001424 // CMS needs this to block for concurrent reference processing because an object allocated during
1425 // the GC won't be marked and concurrent reference processing would incorrectly clear the JNI weak
1426 // ref. But CC (kUseReadBarrier == true) doesn't because of the to-space invariant.
1427 while (!kUseReadBarrier && UNLIKELY(!allow_new_monitors_)) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -07001428 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
1429 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -08001430 self->CheckEmptyCheckpointFromWeakRefAccess(&monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001431 monitor_add_condition_.WaitHoldingLocks(self);
1432 }
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001433 list_.push_front(m);
1434}
1435
Mathieu Chartier97509952015-07-13 14:35:43 -07001436void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
Andreas Gampe74240812014-04-17 10:35:09 -07001437 Thread* self = Thread::Current();
1438 MutexLock mu(self, monitor_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001439 for (auto it = list_.begin(); it != list_.end(); ) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001440 Monitor* m = *it;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001441 // Disable the read barrier in GetObject() as this is called by GC.
1442 mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001443 // The object of a monitor can be null if we have deflated it.
Mathieu Chartier97509952015-07-13 14:35:43 -07001444 mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001445 if (new_obj == nullptr) {
1446 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001447 << obj;
Andreas Gampe74240812014-04-17 10:35:09 -07001448 MonitorPool::ReleaseMonitor(self, m);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001449 it = list_.erase(it);
1450 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001451 m->SetObject(new_obj);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001452 ++it;
1453 }
1454 }
1455}
1456
Hans Boehm6fe97e02016-05-04 18:35:57 -07001457size_t MonitorList::Size() {
1458 Thread* self = Thread::Current();
1459 MutexLock mu(self, monitor_list_lock_);
1460 return list_.size();
1461}
1462
Mathieu Chartier97509952015-07-13 14:35:43 -07001463class MonitorDeflateVisitor : public IsMarkedVisitor {
1464 public:
1465 MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
1466
1467 virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001468 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -07001469 if (Monitor::Deflate(self_, object)) {
1470 DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1471 ++deflate_count_;
1472 // If we deflated, return null so that the monitor gets removed from the array.
1473 return nullptr;
1474 }
1475 return object; // Monitor was not deflated.
1476 }
1477
1478 Thread* const self_;
1479 size_t deflate_count_;
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001480};
1481
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001482size_t MonitorList::DeflateMonitors() {
Mathieu Chartier97509952015-07-13 14:35:43 -07001483 MonitorDeflateVisitor visitor;
1484 Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
1485 SweepMonitorList(&visitor);
1486 return visitor.deflate_count_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001487}
1488
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001489MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001490 DCHECK(obj != nullptr);
1491 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001492 switch (lock_word.GetState()) {
1493 case LockWord::kUnlocked:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001494 // Fall-through.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001495 case LockWord::kForwardingAddress:
1496 // Fall-through.
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001497 case LockWord::kHashCode:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001498 break;
1499 case LockWord::kThinLocked:
1500 owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1501 entry_count_ = 1 + lock_word.ThinLockCount();
1502 // Thin locks have no waiters.
1503 break;
1504 case LockWord::kFatLocked: {
1505 Monitor* mon = lock_word.FatLockMonitor();
1506 owner_ = mon->owner_;
1507 entry_count_ = 1 + mon->lock_count_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001508 for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001509 waiters_.push_back(waiter);
1510 }
1511 break;
Elliott Hughesf327e072013-01-09 16:01:26 -08001512 }
1513 }
1514}
1515
Elliott Hughes5f791332011-09-15 17:45:30 -07001516} // namespace art