blob: 3e3eaae13ab2ddb196b733a7dbc2ef4a678b3e5d [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 Light46f93402017-06-29 11:59:50 -0700913 thread_list->Resume(owner, SuspendReason::kInternal);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700914 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700915 self->SetMonitorEnterObject(nullptr);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700916 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700917}
918
Ian Rogers719d1a32014-03-06 12:13:39 -0800919// Fool annotalysis into thinking that the lock on obj is acquired.
920static mirror::Object* FakeLock(mirror::Object* obj)
921 EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
922 return obj;
923}
924
925// Fool annotalysis into thinking that the lock on obj is release.
926static mirror::Object* FakeUnlock(mirror::Object* obj)
927 UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
928 return obj;
929}
930
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700931mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj, bool trylock) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700932 DCHECK(self != nullptr);
933 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -0700934 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -0800935 obj = FakeLock(obj);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700936 uint32_t thread_id = self->GetThreadId();
937 size_t contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700938 StackHandleScope<1> hs(self);
939 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700940 while (true) {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800941 // We initially read the lockword with ordinary Java/relaxed semantics. When stronger
942 // semantics are needed, we address it below. Since GetLockWord bottoms out to a relaxed load,
943 // we can fix it later, in an infrequently executed case, with a fence.
944 LockWord lock_word = h_obj->GetLockWord(false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700945 switch (lock_word.GetState()) {
946 case LockWord::kUnlocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800947 // No ordering required for preceding lockword read, since we retest.
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700948 LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.GCState()));
Hans Boehmb3da36c2016-12-15 13:12:59 -0800949 if (h_obj->CasLockWordWeakAcquire(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700950 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700951 return h_obj.Get(); // Success!
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700952 }
953 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700954 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700955 case LockWord::kThinLocked: {
956 uint32_t owner_thread_id = lock_word.ThinLockOwner();
957 if (owner_thread_id == thread_id) {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800958 // No ordering required for initial lockword read.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700959 // We own the lock, increase the recursion count.
960 uint32_t new_count = lock_word.ThinLockCount() + 1;
961 if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
Mathieu Chartier36a270a2016-07-28 18:08:51 -0700962 LockWord thin_locked(LockWord::FromThinLockId(thread_id,
963 new_count,
964 lock_word.GCState()));
Hans Boehmb3da36c2016-12-15 13:12:59 -0800965 // Only this thread pays attention to the count. Thus there is no need for stronger
966 // than relaxed memory ordering.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800967 if (!kUseReadBarrier) {
Hans Boehmb3da36c2016-12-15 13:12:59 -0800968 h_obj->SetLockWord(thin_locked, false /* volatile */);
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700969 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800970 return h_obj.Get(); // Success!
971 } else {
972 // Use CAS to preserve the read barrier state.
Hans Boehmb3da36c2016-12-15 13:12:59 -0800973 if (h_obj->CasLockWordWeakRelaxed(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700974 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800975 return h_obj.Get(); // Success!
976 }
977 }
978 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700979 } else {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700980 // We'd overflow the recursion count, so inflate the monitor.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700981 InflateThinLocked(self, h_obj, lock_word, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700982 }
983 } else {
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -0700984 if (trylock) {
985 return nullptr;
986 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700987 // Contention.
988 contention_count++;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700989 Runtime* runtime = Runtime::Current();
Hans Boehmb3da36c2016-12-15 13:12:59 -0800990 if (contention_count <= runtime->GetMaxSpinsBeforeThinLockInflation()) {
Mathieu Chartierb363f662014-07-16 13:28:58 -0700991 // TODO: Consider switching the thread state to kBlocked when we are yielding.
Mathieu Chartier251755c2014-07-15 18:10:25 -0700992 // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the
993 // parameter you pass in. This can cause thread suspension to take excessively long
Mathieu Chartierb363f662014-07-16 13:28:58 -0700994 // and make long pauses. See b/16307460.
Hans Boehmb3da36c2016-12-15 13:12:59 -0800995 // TODO: We should literally spin first, without sched_yield. Sched_yield either does
996 // nothing (at significant expense), or guarantees that we wait at least microseconds.
997 // If the owner is running, I would expect the median lock hold time to be hundreds
998 // of nanoseconds or less.
Mathieu Chartier251755c2014-07-15 18:10:25 -0700999 sched_yield();
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001000 } else {
1001 contention_count = 0;
Hans Boehmb3da36c2016-12-15 13:12:59 -08001002 // No ordering required for initial lockword read. Install rereads it anyway.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001003 InflateThinLocked(self, h_obj, lock_word, 0);
Elliott Hughes5f791332011-09-15 17:45:30 -07001004 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001005 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001006 continue; // Start from the beginning.
Elliott Hughes5f791332011-09-15 17:45:30 -07001007 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001008 case LockWord::kFatLocked: {
Hans Boehmb3da36c2016-12-15 13:12:59 -08001009 // We should have done an acquire read of the lockword initially, to ensure
1010 // visibility of the monitor data structure. Use an explicit fence instead.
1011 QuasiAtomic::ThreadFenceAcquire();
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001012 Monitor* mon = lock_word.FatLockMonitor();
Mathieu Chartier4b0ef1c2016-07-29 16:26:01 -07001013 if (trylock) {
1014 return mon->TryLock(self) ? h_obj.Get() : nullptr;
1015 } else {
1016 mon->Lock(self);
1017 return h_obj.Get(); // Success!
1018 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001019 }
Ian Rogers719d1a32014-03-06 12:13:39 -08001020 case LockWord::kHashCode:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001021 // Inflate with the existing hashcode.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001022 // Again no ordering required for initial lockword read, since we don't rely
1023 // on the visibility of any prior computation.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001024 Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
Ian Rogers719d1a32014-03-06 12:13:39 -08001025 continue; // Start from the beginning.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001026 default: {
1027 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Andreas Gampec7ed09b2016-04-25 20:08:55 -07001028 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001029 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001030 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001031 }
1032}
1033
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001034bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001035 DCHECK(self != nullptr);
1036 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -07001037 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -08001038 obj = FakeUnlock(obj);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001039 StackHandleScope<1> hs(self);
1040 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001041 while (true) {
1042 LockWord lock_word = obj->GetLockWord(true);
1043 switch (lock_word.GetState()) {
1044 case LockWord::kHashCode:
1045 // Fall-through.
1046 case LockWord::kUnlocked:
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001047 FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001048 return false; // Failure.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001049 case LockWord::kThinLocked: {
1050 uint32_t thread_id = self->GetThreadId();
1051 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1052 if (owner_thread_id != thread_id) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -07001053 FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001054 return false; // Failure.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001055 } else {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001056 // We own the lock, decrease the recursion count.
1057 LockWord new_lw = LockWord::Default();
1058 if (lock_word.ThinLockCount() != 0) {
1059 uint32_t new_count = lock_word.ThinLockCount() - 1;
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001060 new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001061 } else {
Mathieu Chartier36a270a2016-07-28 18:08:51 -07001062 new_lw = LockWord::FromDefault(lock_word.GCState());
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001063 }
1064 if (!kUseReadBarrier) {
1065 DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
Hans Boehmb3da36c2016-12-15 13:12:59 -08001066 // TODO: This really only needs memory_order_release, but we currently have
1067 // no way to specify that. In fact there seem to be no legitimate uses of SetLockWord
1068 // with a final argument of true. This slows down x86 and ARMv7, but probably not v8.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001069 h_obj->SetLockWord(new_lw, true);
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001070 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001071 // Success!
1072 return true;
1073 } else {
1074 // Use CAS to preserve the read barrier state.
Hans Boehmb3da36c2016-12-15 13:12:59 -08001075 if (h_obj->CasLockWordWeakRelease(lock_word, new_lw)) {
Andreas Gampe6e759ad2016-05-17 10:13:10 -07001076 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001077 // Success!
1078 return true;
1079 }
1080 }
1081 continue; // Go again.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001082 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001083 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -08001084 case LockWord::kFatLocked: {
1085 Monitor* mon = lock_word.FatLockMonitor();
1086 return mon->Unlock(self);
1087 }
1088 default: {
1089 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1090 return false;
1091 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001092 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001093 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001094}
1095
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001096void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
Elliott Hughes4cd121e2013-01-07 17:35:41 -08001097 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001098 DCHECK(self != nullptr);
1099 DCHECK(obj != nullptr);
1100 LockWord lock_word = obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -07001101 while (lock_word.GetState() != LockWord::kFatLocked) {
1102 switch (lock_word.GetState()) {
1103 case LockWord::kHashCode:
1104 // Fall-through.
1105 case LockWord::kUnlocked:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001106 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1107 return; // Failure.
Ian Rogers43c69cc2014-08-15 11:09:28 -07001108 case LockWord::kThinLocked: {
1109 uint32_t thread_id = self->GetThreadId();
1110 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1111 if (owner_thread_id != thread_id) {
1112 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1113 return; // Failure.
1114 } else {
1115 // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
1116 // re-load.
1117 Inflate(self, self, obj, 0);
1118 lock_word = obj->GetLockWord(true);
1119 }
1120 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001121 }
Ian Rogers43c69cc2014-08-15 11:09:28 -07001122 case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through.
1123 default: {
1124 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1125 return;
1126 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001127 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001128 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001129 Monitor* mon = lock_word.FatLockMonitor();
1130 mon->Wait(self, ms, ns, interruptShouldThrow, why);
Elliott Hughes5f791332011-09-15 17:45:30 -07001131}
1132
Ian Rogers13c479e2013-10-11 07:59:01 -07001133void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001134 DCHECK(self != nullptr);
1135 DCHECK(obj != nullptr);
1136 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001137 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001138 case LockWord::kHashCode:
1139 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001140 case LockWord::kUnlocked:
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001141 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001142 return; // Failure.
1143 case LockWord::kThinLocked: {
1144 uint32_t thread_id = self->GetThreadId();
1145 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1146 if (owner_thread_id != thread_id) {
1147 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
1148 return; // Failure.
1149 } else {
1150 // We own the lock but there's no Monitor and therefore no waiters.
1151 return; // Success.
1152 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001153 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001154 case LockWord::kFatLocked: {
1155 Monitor* mon = lock_word.FatLockMonitor();
1156 if (notify_all) {
1157 mon->NotifyAll(self);
1158 } else {
1159 mon->Notify(self);
1160 }
1161 return; // Success.
1162 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001163 default: {
1164 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1165 return;
1166 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001167 }
1168}
1169
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001170uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001171 DCHECK(obj != nullptr);
1172 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001173 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001174 case LockWord::kHashCode:
1175 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001176 case LockWord::kUnlocked:
1177 return ThreadList::kInvalidThreadId;
1178 case LockWord::kThinLocked:
1179 return lock_word.ThinLockOwner();
1180 case LockWord::kFatLocked: {
1181 Monitor* mon = lock_word.FatLockMonitor();
1182 return mon->GetOwnerThreadId();
Elliott Hughes5f791332011-09-15 17:45:30 -07001183 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001184 default: {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001185 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001186 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001187 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001188 }
1189}
1190
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001191void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001192 // Determine the wait message and object we're waiting or blocked upon.
1193 mirror::Object* pretty_object = nullptr;
1194 const char* wait_message = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001195 uint32_t lock_owner = ThreadList::kInvalidThreadId;
Ian Rogersd803bc72014-04-01 15:33:03 -07001196 ThreadState state = thread->GetState();
Elliott Hughesb4e94fd2013-01-08 14:41:26 -08001197 if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001198 wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on ";
1199 Thread* self = Thread::Current();
1200 MutexLock mu(self, *thread->GetWaitMutex());
1201 Monitor* monitor = thread->GetWaitMonitor();
1202 if (monitor != nullptr) {
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001203 pretty_object = monitor->GetObject();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001204 }
Elliott Hughes34e06962012-04-09 13:55:55 -07001205 } else if (state == kBlocked) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001206 wait_message = " - waiting to lock ";
1207 pretty_object = thread->GetMonitorEnterObject();
1208 if (pretty_object != nullptr) {
Hiroshi Yamauchi7b08ae42016-10-04 15:20:36 -07001209 if (kUseReadBarrier && Thread::Current()->GetIsGcMarking()) {
1210 // We may call Thread::Dump() in the middle of the CC thread flip and this thread's stack
1211 // may have not been flipped yet and "pretty_object" may be a from-space (stale) ref, in
1212 // which case the GetLockOwnerThreadId() call below will crash. So explicitly mark/forward
1213 // it here.
1214 pretty_object = ReadBarrier::Mark(pretty_object);
1215 }
Ian Rogersd803bc72014-04-01 15:33:03 -07001216 lock_owner = pretty_object->GetLockOwnerThreadId();
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001217 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001218 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001219
Ian Rogersd803bc72014-04-01 15:33:03 -07001220 if (wait_message != nullptr) {
1221 if (pretty_object == nullptr) {
1222 os << wait_message << "an unknown object";
1223 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001224 if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
Ian Rogersd803bc72014-04-01 15:33:03 -07001225 Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
1226 // Getting the identity hashcode here would result in lock inflation and suspension of the
1227 // current thread, which isn't safe if this is the only runnable thread.
1228 os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
1229 reinterpret_cast<intptr_t>(pretty_object),
David Sehr709b0702016-10-13 09:12:37 -07001230 pretty_object->PrettyTypeOf().c_str());
Ian Rogersd803bc72014-04-01 15:33:03 -07001231 } else {
1232 // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
Mathieu Chartier49361592015-01-22 16:36:10 -08001233 // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
1234 // suspension and move pretty_object.
David Sehr709b0702016-10-13 09:12:37 -07001235 const std::string pretty_type(pretty_object->PrettyTypeOf());
Ian Rogersd803bc72014-04-01 15:33:03 -07001236 os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
Mathieu Chartier49361592015-01-22 16:36:10 -08001237 pretty_type.c_str());
Ian Rogersd803bc72014-04-01 15:33:03 -07001238 }
1239 }
1240 // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
1241 if (lock_owner != ThreadList::kInvalidThreadId) {
1242 os << " held by thread " << lock_owner;
1243 }
1244 os << "\n";
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001245 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001246}
1247
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001248mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001249 // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
1250 // definition of contended that includes a monitor a thread is trying to enter...
Ian Rogersdd7624d2014-03-14 17:43:00 -07001251 mirror::Object* result = thread->GetMonitorEnterObject();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001252 if (result == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001253 // ...but also a monitor that the thread is waiting on.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001254 MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
1255 Monitor* monitor = thread->GetWaitMonitor();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001256 if (monitor != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001257 result = monitor->GetObject();
Elliott Hughesf9501702013-01-11 11:22:27 -08001258 }
1259 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001260 return result;
Elliott Hughesf9501702013-01-11 11:22:27 -08001261}
1262
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001263void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
Andreas Gampe760172c2014-08-16 13:41:10 -07001264 void* callback_context, bool abort_on_failure) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001265 ArtMethod* m = stack_visitor->GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001266 CHECK(m != nullptr);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001267
1268 // Native methods are an easy special case.
1269 // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
1270 if (m->IsNative()) {
1271 if (m->IsSynchronized()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001272 mirror::Object* jni_this =
1273 stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001274 callback(jni_this, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001275 }
1276 return;
1277 }
1278
jeffhao61f916c2012-10-25 17:48:51 -07001279 // Proxy methods should not be synchronized.
1280 if (m->IsProxyMethod()) {
1281 CHECK(!m->IsSynchronized());
1282 return;
1283 }
1284
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001285 // Is there any reason to believe there's any synchronization in this method?
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001286 const DexFile::CodeItem* code_item = m->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -07001287 CHECK(code_item != nullptr) << m->PrettyMethod();
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001288 if (code_item->tries_size_ == 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001289 return; // No "tries" implies no synchronization, so no held locks to report.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001290 }
1291
Andreas Gampe760172c2014-08-16 13:41:10 -07001292 // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
1293 // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
1294 // inconsistent stack anyways.
1295 uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
1296 if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) {
David Sehr709b0702016-10-13 09:12:37 -07001297 LOG(ERROR) << "Could not find dex_pc for " << m->PrettyMethod();
Andreas Gampe760172c2014-08-16 13:41:10 -07001298 return;
1299 }
1300
Elliott Hughes80537bb2013-01-04 16:37:26 -08001301 // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
1302 // the locks held in this stack frame.
1303 std::vector<uint32_t> monitor_enter_dex_pcs;
Andreas Gampe760172c2014-08-16 13:41:10 -07001304 verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs);
Mathieu Chartiere6a8eec2015-01-06 14:17:57 -08001305 for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) {
Elliott Hughes80537bb2013-01-04 16:37:26 -08001306 // The verifier works in terms of the dex pcs of the monitor-enter instructions.
1307 // We want the registers used by those instructions (so we can read the values out of them).
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001308 const Instruction* monitor_enter_instruction =
1309 Instruction::At(&code_item->insns_[monitor_dex_pc]);
Elliott Hughes80537bb2013-01-04 16:37:26 -08001310
1311 // Quick sanity check.
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001312 CHECK_EQ(monitor_enter_instruction->Opcode(), Instruction::MONITOR_ENTER)
1313 << "expected monitor-enter @" << monitor_dex_pc << "; was "
1314 << reinterpret_cast<const void*>(monitor_enter_instruction);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001315
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001316 uint16_t monitor_register = monitor_enter_instruction->VRegA();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001317 uint32_t value;
1318 bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value);
1319 CHECK(success) << "Failed to read v" << monitor_register << " of kind "
David Sehr709b0702016-10-13 09:12:37 -07001320 << kReferenceVReg << " in method " << m->PrettyMethod();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001321 mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001322 callback(o, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001323 }
1324}
1325
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001326bool Monitor::IsValidLockWord(LockWord lock_word) {
1327 switch (lock_word.GetState()) {
1328 case LockWord::kUnlocked:
1329 // Nothing to check.
1330 return true;
1331 case LockWord::kThinLocked:
1332 // Basic sanity check of owner.
1333 return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1334 case LockWord::kFatLocked: {
1335 // Check the monitor appears in the monitor list.
1336 Monitor* mon = lock_word.FatLockMonitor();
1337 MonitorList* list = Runtime::Current()->GetMonitorList();
1338 MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1339 for (Monitor* list_mon : list->list_) {
1340 if (mon == list_mon) {
1341 return true; // Found our monitor.
1342 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001343 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001344 return false; // Fail - unowned monitor in an object.
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001345 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001346 case LockWord::kHashCode:
1347 return true;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001348 default:
1349 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001350 UNREACHABLE();
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001351 }
1352}
1353
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001354bool Monitor::IsLocked() REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001355 MutexLock mu(Thread::Current(), monitor_lock_);
1356 return owner_ != nullptr;
1357}
1358
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -07001359void Monitor::TranslateLocation(ArtMethod* method,
1360 uint32_t dex_pc,
1361 const char** source_file,
1362 int32_t* line_number) {
jeffhao33dc7712011-11-09 17:54:24 -08001363 // If method is null, location is unknown
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001364 if (method == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001365 *source_file = "";
1366 *line_number = 0;
jeffhao33dc7712011-11-09 17:54:24 -08001367 return;
1368 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001369 *source_file = method->GetDeclaringClassSourceFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001370 if (*source_file == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001371 *source_file = "";
Elliott Hughes12c51e32012-01-17 20:25:05 -08001372 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001373 *line_number = method->GetLineNumFromDexPC(dex_pc);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001374}
1375
1376uint32_t Monitor::GetOwnerThreadId() {
1377 MutexLock mu(Thread::Current(), monitor_lock_);
1378 Thread* owner = owner_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001379 if (owner != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001380 return owner->GetThreadId();
1381 } else {
1382 return ThreadList::kInvalidThreadId;
1383 }
jeffhao33dc7712011-11-09 17:54:24 -08001384}
1385
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001386MonitorList::MonitorList()
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001387 : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001388 monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001389}
1390
1391MonitorList::~MonitorList() {
Andreas Gampe74240812014-04-17 10:35:09 -07001392 Thread* self = Thread::Current();
1393 MutexLock mu(self, monitor_list_lock_);
1394 // Release all monitors to the pool.
1395 // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
1396 // clear faster in the pool.
1397 MonitorPool::ReleaseMonitors(self, &list_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001398}
1399
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001400void MonitorList::DisallowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001401 CHECK(!kUseReadBarrier);
Ian Rogers50b35e22012-10-04 10:09:15 -07001402 MutexLock mu(Thread::Current(), monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001403 allow_new_monitors_ = false;
1404}
1405
1406void MonitorList::AllowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001407 CHECK(!kUseReadBarrier);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001408 Thread* self = Thread::Current();
1409 MutexLock mu(self, monitor_list_lock_);
1410 allow_new_monitors_ = true;
1411 monitor_add_condition_.Broadcast(self);
1412}
1413
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001414void MonitorList::BroadcastForNewMonitors() {
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001415 Thread* self = Thread::Current();
1416 MutexLock mu(self, monitor_list_lock_);
1417 monitor_add_condition_.Broadcast(self);
1418}
1419
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001420void MonitorList::Add(Monitor* m) {
1421 Thread* self = Thread::Current();
1422 MutexLock mu(self, monitor_list_lock_);
Hiroshi Yamauchif1c6f872017-01-06 12:23:47 -08001423 // CMS needs this to block for concurrent reference processing because an object allocated during
1424 // the GC won't be marked and concurrent reference processing would incorrectly clear the JNI weak
1425 // ref. But CC (kUseReadBarrier == true) doesn't because of the to-space invariant.
1426 while (!kUseReadBarrier && UNLIKELY(!allow_new_monitors_)) {
Hiroshi Yamauchi30493242016-11-03 13:06:52 -07001427 // Check and run the empty checkpoint before blocking so the empty checkpoint will work in the
1428 // presence of threads blocking for weak ref access.
Hiroshi Yamauchia2224042017-02-08 16:35:45 -08001429 self->CheckEmptyCheckpointFromWeakRefAccess(&monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001430 monitor_add_condition_.WaitHoldingLocks(self);
1431 }
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001432 list_.push_front(m);
1433}
1434
Mathieu Chartier97509952015-07-13 14:35:43 -07001435void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
Andreas Gampe74240812014-04-17 10:35:09 -07001436 Thread* self = Thread::Current();
1437 MutexLock mu(self, monitor_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001438 for (auto it = list_.begin(); it != list_.end(); ) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001439 Monitor* m = *it;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001440 // Disable the read barrier in GetObject() as this is called by GC.
1441 mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001442 // The object of a monitor can be null if we have deflated it.
Mathieu Chartier97509952015-07-13 14:35:43 -07001443 mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001444 if (new_obj == nullptr) {
1445 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001446 << obj;
Andreas Gampe74240812014-04-17 10:35:09 -07001447 MonitorPool::ReleaseMonitor(self, m);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001448 it = list_.erase(it);
1449 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001450 m->SetObject(new_obj);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001451 ++it;
1452 }
1453 }
1454}
1455
Hans Boehm6fe97e02016-05-04 18:35:57 -07001456size_t MonitorList::Size() {
1457 Thread* self = Thread::Current();
1458 MutexLock mu(self, monitor_list_lock_);
1459 return list_.size();
1460}
1461
Mathieu Chartier97509952015-07-13 14:35:43 -07001462class MonitorDeflateVisitor : public IsMarkedVisitor {
1463 public:
1464 MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
1465
1466 virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001467 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -07001468 if (Monitor::Deflate(self_, object)) {
1469 DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1470 ++deflate_count_;
1471 // If we deflated, return null so that the monitor gets removed from the array.
1472 return nullptr;
1473 }
1474 return object; // Monitor was not deflated.
1475 }
1476
1477 Thread* const self_;
1478 size_t deflate_count_;
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001479};
1480
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001481size_t MonitorList::DeflateMonitors() {
Mathieu Chartier97509952015-07-13 14:35:43 -07001482 MonitorDeflateVisitor visitor;
1483 Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
1484 SweepMonitorList(&visitor);
1485 return visitor.deflate_count_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001486}
1487
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001488MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001489 DCHECK(obj != nullptr);
1490 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001491 switch (lock_word.GetState()) {
1492 case LockWord::kUnlocked:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001493 // Fall-through.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001494 case LockWord::kForwardingAddress:
1495 // Fall-through.
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001496 case LockWord::kHashCode:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001497 break;
1498 case LockWord::kThinLocked:
1499 owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1500 entry_count_ = 1 + lock_word.ThinLockCount();
1501 // Thin locks have no waiters.
1502 break;
1503 case LockWord::kFatLocked: {
1504 Monitor* mon = lock_word.FatLockMonitor();
1505 owner_ = mon->owner_;
1506 entry_count_ = 1 + mon->lock_count_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001507 for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001508 waiters_.push_back(waiter);
1509 }
1510 break;
Elliott Hughesf327e072013-01-09 16:01:26 -08001511 }
1512 }
1513}
1514
Elliott Hughes5f791332011-09-15 17:45:30 -07001515} // namespace art