blob: 3771877c9de14ce557490e726b6afcd11b743104 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Elliott Hughes76b61672012-12-12 17:47:30 -080022#include "base/mutex.h"
Elliott Hughes1aa246d2012-12-13 09:29:36 -080023#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080024#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010025#include "base/time_utils.h"
jeffhao33dc7712011-11-09 17:54:24 -080026#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Sebastien Hertz0f7c9332015-11-05 15:57:30 +010028#include "dex_instruction-inl.h"
Ian Rogersd9c4fc92013-10-01 19:45:43 -070029#include "lock_word-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Ian Rogers05f30572013-02-20 12:13:11 -080031#include "mirror/object-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/object_array-inl.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033#include "scoped_thread_state_change.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070034#include "thread.h"
Elliott Hughes8e4aac52011-09-26 17:03:36 -070035#include "thread_list.h"
Elliott Hughes08fc03a2012-06-26 17:34:00 -070036#include "verifier/method_verifier.h"
Elliott Hughes044288f2012-06-25 14:46:39 -070037#include "well_known_classes.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070038
39namespace art {
40
Mathieu Chartierb9001ab2014-10-03 13:28:46 -070041static constexpr uint64_t kLongWaitMs = 100;
42
Elliott Hughes5f791332011-09-15 17:45:30 -070043/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -070044 * Every Object has a monitor associated with it, but not every Object is actually locked. Even
45 * the ones that are locked do not need a full-fledged monitor until a) there is actual contention
46 * or b) wait() is called on the Object.
Elliott Hughes5f791332011-09-15 17:45:30 -070047 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070048 * For Android, we have implemented a scheme similar to the one described in Bacon et al.'s
49 * "Thin locks: featherweight synchronization for Java" (ACM 1998). Things are even easier for us,
50 * though, because we have a full 32 bits to work with.
Elliott Hughes5f791332011-09-15 17:45:30 -070051 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070052 * The two states of an Object's lock are referred to as "thin" and "fat". A lock may transition
53 * from the "thin" state to the "fat" state and this transition is referred to as inflation. Once
54 * a lock has been inflated it remains in the "fat" state indefinitely.
Elliott Hughes5f791332011-09-15 17:45:30 -070055 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070056 * The lock value itself is stored in mirror::Object::monitor_ and the representation is described
57 * in the LockWord value type.
Elliott Hughes54e7df12011-09-16 11:47:04 -070058 *
Elliott Hughes5f791332011-09-15 17:45:30 -070059 * Monitors provide:
60 * - mutually exclusive access to resources
61 * - a way for multiple threads to wait for notification
62 *
63 * In effect, they fill the role of both mutexes and condition variables.
64 *
Ian Rogersd9c4fc92013-10-01 19:45:43 -070065 * Only one thread can own the monitor at any time. There may be several threads waiting on it
66 * (the wait call unlocks it). One or more waiting threads may be getting interrupted or notified
67 * at any given time.
Elliott Hughes5f791332011-09-15 17:45:30 -070068 */
Elliott Hughes54e7df12011-09-16 11:47:04 -070069
Elliott Hughesfc861622011-10-17 17:57:47 -070070uint32_t Monitor::lock_profiling_threshold_ = 0;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070071
Calin Juravleb2771b42016-04-07 17:09:25 +010072void Monitor::Init(uint32_t lock_profiling_threshold) {
Elliott Hughesfc861622011-10-17 17:57:47 -070073 lock_profiling_threshold_ = lock_profiling_threshold;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -070074}
75
Ian Rogersef7d42f2014-01-06 12:55:46 -080076Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070077 : monitor_lock_("a monitor lock", kMonitorLock),
Ian Rogersd9c4fc92013-10-01 19:45:43 -070078 monitor_contenders_("monitor contenders", monitor_lock_),
Mathieu Chartier46bc7782013-11-12 17:03:02 -080079 num_waiters_(0),
Ian Rogers00f7d0e2012-07-19 15:28:27 -070080 owner_(owner),
Elliott Hughes5f791332011-09-15 17:45:30 -070081 lock_count_(0),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070082 obj_(GcRoot<mirror::Object>(obj)),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070083 wait_set_(nullptr),
Mathieu Chartierad2541a2013-10-25 10:05:23 -070084 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 locking_method_(nullptr),
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 locking_dex_pc_(0),
Andreas Gampe74240812014-04-17 10:35:09 -070087 monitor_id_(MonitorPool::ComputeMonitorId(this, self)) {
88#ifdef __LP64__
89 DCHECK(false) << "Should not be reached in 64b";
90 next_free_ = nullptr;
91#endif
92 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
93 // with the owner unlocking the thin-lock.
94 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
95 // The identity hash code is set for the life time of the monitor.
96}
97
98Monitor::Monitor(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code,
99 MonitorId id)
100 : monitor_lock_("a monitor lock", kMonitorLock),
101 monitor_contenders_("monitor contenders", monitor_lock_),
102 num_waiters_(0),
103 owner_(owner),
104 lock_count_(0),
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700105 obj_(GcRoot<mirror::Object>(obj)),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106 wait_set_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700107 hash_code_(hash_code),
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700108 locking_method_(nullptr),
Andreas Gampe74240812014-04-17 10:35:09 -0700109 locking_dex_pc_(0),
110 monitor_id_(id) {
111#ifdef __LP64__
112 next_free_ = nullptr;
113#endif
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700114 // We should only inflate a lock if the owner is ourselves or suspended. This avoids a race
115 // with the owner unlocking the thin-lock.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 CHECK(owner == nullptr || owner == self || owner->IsSuspended());
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700117 // The identity hash code is set for the life time of the monitor.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700118}
119
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700120int32_t Monitor::GetHashCode() {
121 while (!HasHashCode()) {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700122 if (hash_code_.CompareExchangeWeakRelaxed(0, mirror::Object::GenerateIdentityHashCode())) {
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700123 break;
124 }
125 }
126 DCHECK(HasHashCode());
Ian Rogers3e5cf302014-05-20 16:40:37 -0700127 return hash_code_.LoadRelaxed();
Mathieu Chartier4e6a31e2013-10-31 10:35:05 -0700128}
129
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700130bool Monitor::Install(Thread* self) {
131 MutexLock mu(self, monitor_lock_); // Uncontended mutex acquisition as monitor isn't yet public.
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700132 CHECK(owner_ == nullptr || owner_ == self || owner_->IsSuspended());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 // Propagate the lock state.
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -0700134 LockWord lw(GetObject()->GetLockWord(false));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700135 switch (lw.GetState()) {
136 case LockWord::kThinLocked: {
137 CHECK_EQ(owner_->GetThreadId(), lw.ThinLockOwner());
138 lock_count_ = lw.ThinLockCount();
139 break;
140 }
141 case LockWord::kHashCode: {
Ian Rogers3e5cf302014-05-20 16:40:37 -0700142 CHECK_EQ(hash_code_.LoadRelaxed(), static_cast<int32_t>(lw.GetHashCode()));
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700143 break;
144 }
145 case LockWord::kFatLocked: {
146 // The owner_ is suspended but another thread beat us to install a monitor.
147 return false;
148 }
149 case LockWord::kUnlocked: {
150 LOG(FATAL) << "Inflating unlocked lock word";
151 break;
152 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700153 default: {
154 LOG(FATAL) << "Invalid monitor state " << lw.GetState();
155 return false;
156 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700157 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800158 LockWord fat(this, lw.ReadBarrierState());
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700159 // Publish the updated lock word, which may race with other threads.
Ian Rogers228602f2014-07-10 02:07:54 -0700160 bool success = GetObject()->CasLockWordWeakSequentiallyConsistent(lw, fat);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700161 // Lock profiling.
Mathieu Chartier9728f912013-10-30 09:45:13 -0700162 if (success && owner_ != nullptr && lock_profiling_threshold_ != 0) {
Andreas Gampe6ec8ebd2014-07-25 13:36:56 -0700163 // Do not abort on dex pc errors. This can easily happen when we want to dump a stack trace on
164 // abort.
165 locking_method_ = owner_->GetCurrentMethod(&locking_dex_pc_, false);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700166 }
167 return success;
Elliott Hughes5f791332011-09-15 17:45:30 -0700168}
169
170Monitor::~Monitor() {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171 // Deflated monitors have a null object.
Elliott Hughes5f791332011-09-15 17:45:30 -0700172}
173
Elliott Hughes5f791332011-09-15 17:45:30 -0700174void Monitor::AppendToWaitSet(Thread* thread) {
175 DCHECK(owner_ == Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700176 DCHECK(thread != nullptr);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700177 DCHECK(thread->GetWaitNext() == nullptr) << thread->GetWaitNext();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700178 if (wait_set_ == nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700179 wait_set_ = thread;
180 return;
181 }
182
183 // push_back.
184 Thread* t = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700185 while (t->GetWaitNext() != nullptr) {
186 t = t->GetWaitNext();
Elliott Hughes5f791332011-09-15 17:45:30 -0700187 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700188 t->SetWaitNext(thread);
Elliott Hughes5f791332011-09-15 17:45:30 -0700189}
190
Elliott Hughes5f791332011-09-15 17:45:30 -0700191void Monitor::RemoveFromWaitSet(Thread *thread) {
192 DCHECK(owner_ == Thread::Current());
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700193 DCHECK(thread != nullptr);
194 if (wait_set_ == nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700195 return;
196 }
197 if (wait_set_ == thread) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700198 wait_set_ = thread->GetWaitNext();
199 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700200 return;
201 }
202
203 Thread* t = wait_set_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700204 while (t->GetWaitNext() != nullptr) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700205 if (t->GetWaitNext() == thread) {
206 t->SetWaitNext(thread->GetWaitNext());
207 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700208 return;
209 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700210 t = t->GetWaitNext();
Elliott Hughes5f791332011-09-15 17:45:30 -0700211 }
212}
213
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700214void Monitor::SetObject(mirror::Object* object) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700215 obj_ = GcRoot<mirror::Object>(object);
Mathieu Chartier6aa3df92013-09-17 15:17:28 -0700216}
217
Andreas Gampefc689872016-04-25 20:08:55 -0700218// Note: Adapted from CurrentMethodVisitor in thread.cc. We must not resolve here.
219
220struct NthCallerWithDexPcVisitor FINAL : public StackVisitor {
221 explicit NthCallerWithDexPcVisitor(Thread* thread, size_t frame)
222 SHARED_REQUIRES(Locks::mutator_lock_)
223 : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFramesNoResolve),
224 method_(nullptr),
225 dex_pc_(0),
226 current_frame_number_(0),
227 wanted_frame_number_(frame) {}
228 bool VisitFrame() OVERRIDE SHARED_REQUIRES(Locks::mutator_lock_) {
229 ArtMethod* m = GetMethod();
230 if (m == nullptr || m->IsRuntimeMethod()) {
231 // Runtime method, upcall, or resolution issue. Skip.
232 return true;
233 }
234
235 // Is this the requested frame?
236 if (current_frame_number_ == wanted_frame_number_) {
237 method_ = m;
238 dex_pc_ = GetDexPc(false /* abort_on_error*/);
239 return false;
240 }
241
242 // Look for more.
243 current_frame_number_++;
244 return true;
245 }
246
247 ArtMethod* method_;
248 uint32_t dex_pc_;
249
250 private:
251 size_t current_frame_number_;
252 const size_t wanted_frame_number_;
253};
254
255// This function is inlined and just helps to not have the VLOG and ATRACE check at all the
256// potential tracing points.
257void Monitor::AtraceMonitorLock(Thread* self, mirror::Object* obj, bool is_wait) {
258 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging) && ATRACE_ENABLED())) {
259 AtraceMonitorLockImpl(self, obj, is_wait);
260 }
261}
262
263void Monitor::AtraceMonitorLockImpl(Thread* self, mirror::Object* obj, bool is_wait) {
264 // Wait() requires a deeper call stack to be useful. Otherwise you'll see "Waiting at
265 // Object.java". Assume that we'll wait a nontrivial amount, so it's OK to do a longer
266 // stack walk than if !is_wait.
267 NthCallerWithDexPcVisitor visitor(self, is_wait ? 1U : 0U);
268 visitor.WalkStack(false);
269 const char* prefix = is_wait ? "Waiting on " : "Locking ";
270
271 const char* filename;
272 int32_t line_number;
273 TranslateLocation(visitor.method_, visitor.dex_pc_, &filename, &line_number);
274
275 // It would be nice to have a stable "ID" for the object here. However, the only stable thing
276 // would be the identity hashcode. But we cannot use IdentityHashcode here: For one, there are
277 // times when it is unsafe to make that call (see stack dumping for an explanation). More
278 // importantly, we would have to give up on thin-locking when adding systrace locks, as the
279 // identity hashcode is stored in the lockword normally (so can't be used with thin-locks).
280 //
281 // Because of thin-locks we also cannot use the monitor id (as there is no monitor). Monitor ids
282 // also do not have to be stable, as the monitor may be deflated.
283 std::string tmp = StringPrintf("%s %d at %s:%d",
284 prefix,
285 (obj == nullptr ? -1 : static_cast<int32_t>(reinterpret_cast<uintptr_t>(obj))),
286 (filename != nullptr ? filename : "null"),
287 line_number);
288 ATRACE_BEGIN(tmp.c_str());
289}
290
291void Monitor::AtraceMonitorUnlock() {
292 if (UNLIKELY(VLOG_IS_ON(systrace_lock_logging))) {
293 ATRACE_END();
294 }
295}
296
Mathieu Chartierdee19e32016-04-19 13:46:03 -0700297std::string Monitor::PrettyContentionInfo(const std::string& owner_name,
298 pid_t owner_tid,
Mathieu Chartierdc3c6302016-04-15 19:11:45 -0700299 ArtMethod* owners_method,
300 uint32_t owners_dex_pc,
301 size_t num_waiters) {
Mathieu Chartierdc3c6302016-04-15 19:11:45 -0700302 const char* owners_filename;
303 int32_t owners_line_number;
Mathieu Chartierdc3c6302016-04-15 19:11:45 -0700304 if (owners_method != nullptr) {
305 TranslateLocation(owners_method, owners_dex_pc, &owners_filename, &owners_line_number);
306 }
307 std::ostringstream oss;
Mathieu Chartierdee19e32016-04-19 13:46:03 -0700308 oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")";
Mathieu Chartierdc3c6302016-04-15 19:11:45 -0700309 if (owners_method != nullptr) {
Mathieu Chartier8f264112016-04-28 17:21:08 -0700310 oss << " at " << PrettyMethod(owners_method);
311 oss << "(" << owners_filename << ":" << owners_line_number << ")";
Mathieu Chartierdc3c6302016-04-15 19:11:45 -0700312 }
313 oss << " waiters=" << num_waiters;
314 return oss.str();
315}
316
Mathieu Chartier23da0262016-07-29 16:26:01 -0700317bool Monitor::TryLockLocked(Thread* self) {
318 if (owner_ == nullptr) { // Unowned.
319 owner_ = self;
320 CHECK_EQ(lock_count_, 0);
321 // When debugging, save the current monitor holder for future
322 // acquisition failures to use in sampled logging.
323 if (lock_profiling_threshold_ != 0) {
324 locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
325 }
326 } else if (owner_ == self) { // Recursive.
327 lock_count_++;
328 } else {
329 return false;
330 }
331 AtraceMonitorLock(self, GetObject(), false /* is_wait */);
332 return true;
333}
334
335bool Monitor::TryLock(Thread* self) {
336 MutexLock mu(self, monitor_lock_);
337 return TryLockLocked(self);
338}
339
Elliott Hughes5f791332011-09-15 17:45:30 -0700340void Monitor::Lock(Thread* self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700341 MutexLock mu(self, monitor_lock_);
342 while (true) {
Mathieu Chartier23da0262016-07-29 16:26:01 -0700343 if (TryLockLocked(self)) {
344 return;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700345 }
346 // Contended.
347 const bool log_contention = (lock_profiling_threshold_ != 0);
Xin Guanb894a192014-08-22 11:55:37 -0500348 uint64_t wait_start_ms = log_contention ? MilliTime() : 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 ArtMethod* owners_method = locking_method_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700350 uint32_t owners_dex_pc = locking_dex_pc_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700351 // Do this before releasing the lock so that we don't get deflated.
Mathieu Chartierb9001ab2014-10-03 13:28:46 -0700352 size_t num_waiters = num_waiters_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700353 ++num_waiters_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700354 monitor_lock_.Unlock(self); // Let go of locks in order.
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700355 self->SetMonitorEnterObject(GetObject());
Elliott Hughes5f791332011-09-15 17:45:30 -0700356 {
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700357 uint32_t original_owner_thread_id = 0u;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700358 ScopedThreadStateChange tsc(self, kBlocked); // Change to blocked and give up mutator_lock_.
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700359 {
360 // Reacquire monitor_lock_ without mutator_lock_ for Wait.
361 MutexLock mu2(self, monitor_lock_);
362 if (owner_ != nullptr) { // Did the owner_ give the lock up?
363 original_owner_thread_id = owner_->GetThreadId();
364 if (ATRACE_ENABLED()) {
365 std::ostringstream oss;
Mathieu Chartierdee19e32016-04-19 13:46:03 -0700366 std::string name;
367 owner_->GetThreadName(name);
368 oss << PrettyContentionInfo(name,
369 owner_->GetTid(),
370 owners_method,
371 owners_dex_pc,
372 num_waiters);
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700373 // Add info for contending thread.
374 uint32_t pc;
375 ArtMethod* m = self->GetCurrentMethod(&pc);
376 const char* filename;
377 int32_t line_number;
378 TranslateLocation(m, pc, &filename, &line_number);
Mathieu Chartier8f264112016-04-28 17:21:08 -0700379 oss << " blocking from "
380 << PrettyMethod(m) << "(" << (filename != nullptr ? filename : "null") << ":"
381 << line_number << ")";
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700382 ATRACE_BEGIN(oss.str().c_str());
383 }
384 monitor_contenders_.Wait(self); // Still contended so wait.
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800385 }
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700386 }
387 if (original_owner_thread_id != 0u) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700388 // Woken from contention.
389 if (log_contention) {
Mathieu Chartierdee19e32016-04-19 13:46:03 -0700390 uint32_t original_owner_tid = 0;
391 std::string original_owner_name;
392 {
393 MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_);
394 // Re-find the owner in case the thread got killed.
395 Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId(
396 original_owner_thread_id);
397 // Do not do any work that requires the mutator lock.
398 if (original_owner != nullptr) {
399 original_owner_tid = original_owner->GetTid();
400 original_owner->GetThreadName(original_owner_name);
401 }
402 }
403
404 if (original_owner_tid != 0u) {
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700405 uint64_t wait_ms = MilliTime() - wait_start_ms;
406 uint32_t sample_percent;
407 if (wait_ms >= lock_profiling_threshold_) {
408 sample_percent = 100;
409 } else {
410 sample_percent = 100 * wait_ms / lock_profiling_threshold_;
Mathieu Chartierdc3c6302016-04-15 19:11:45 -0700411 }
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700412 if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
413 if (wait_ms > kLongWaitMs && owners_method != nullptr) {
Mathieu Chartier8f264112016-04-28 17:21:08 -0700414 uint32_t pc;
415 ArtMethod* m = self->GetCurrentMethod(&pc);
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700416 // TODO: We should maybe check that original_owner is still a live thread.
417 LOG(WARNING) << "Long "
Mathieu Chartierdee19e32016-04-19 13:46:03 -0700418 << PrettyContentionInfo(original_owner_name,
419 original_owner_tid,
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700420 owners_method,
421 owners_dex_pc,
422 num_waiters)
Mathieu Chartier8f264112016-04-28 17:21:08 -0700423 << " in " << PrettyMethod(m) << " for " << PrettyDuration(MsToNs(wait_ms));
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700424 }
425 const char* owners_filename;
426 int32_t owners_line_number;
427 TranslateLocation(owners_method,
428 owners_dex_pc,
429 &owners_filename,
430 &owners_line_number);
431 LogContentionEvent(self,
432 wait_ms,
433 sample_percent,
434 owners_filename,
435 owners_line_number);
436 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700437 }
438 }
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800439 ATRACE_END();
Elliott Hughesfc861622011-10-17 17:57:47 -0700440 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700441 }
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700442 self->SetMonitorEnterObject(nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700443 monitor_lock_.Lock(self); // Reacquire locks in order.
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700444 --num_waiters_;
Elliott Hughesfc861622011-10-17 17:57:47 -0700445 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700446}
447
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800448static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
449 __attribute__((format(printf, 1, 2)));
450
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700451static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
Mathieu Chartier90443472015-07-16 20:32:27 -0700452 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800453 va_list args;
454 va_start(args, fmt);
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000456 self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700457 if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
Brian Carlstrom64277f32012-03-26 23:53:34 -0700458 std::ostringstream ss;
Ian Rogers62d6c772013-02-27 08:32:07 -0800459 self->Dump(ss);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700460 LOG(Runtime::Current()->IsStarted() ? INFO : ERROR)
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000461 << self->GetException()->Dump() << "\n" << ss.str();
Brian Carlstrom64277f32012-03-26 23:53:34 -0700462 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800463 va_end(args);
464}
465
Elliott Hughesd4237412012-02-21 11:24:45 -0800466static std::string ThreadToString(Thread* thread) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700467 if (thread == nullptr) {
468 return "nullptr";
Elliott Hughesd4237412012-02-21 11:24:45 -0800469 }
470 std::ostringstream oss;
471 // TODO: alternatively, we could just return the thread's name.
472 oss << *thread;
473 return oss.str();
474}
475
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700476void Monitor::FailedUnlock(mirror::Object* o,
477 uint32_t expected_owner_thread_id,
478 uint32_t found_owner_thread_id,
Elliott Hughesffb465f2012-03-01 18:46:05 -0800479 Monitor* monitor) {
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700480 // Acquire thread list lock so threads won't disappear from under us.
Elliott Hughesffb465f2012-03-01 18:46:05 -0800481 std::string current_owner_string;
482 std::string expected_owner_string;
483 std::string found_owner_string;
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700484 uint32_t current_owner_thread_id = 0u;
Elliott Hughesffb465f2012-03-01 18:46:05 -0800485 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700486 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700487 ThreadList* const thread_list = Runtime::Current()->GetThreadList();
488 Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id);
489 Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id);
490
Elliott Hughesffb465f2012-03-01 18:46:05 -0800491 // Re-read owner now that we hold lock.
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700492 Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr;
493 if (current_owner != nullptr) {
494 current_owner_thread_id = current_owner->GetThreadId();
495 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800496 // Get short descriptions of the threads involved.
497 current_owner_string = ThreadToString(current_owner);
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700498 expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed";
499 found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed";
Elliott Hughesffb465f2012-03-01 18:46:05 -0800500 }
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700501
502 if (current_owner_thread_id == 0u) {
503 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800504 ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
505 " on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800506 PrettyTypeOf(o).c_str(),
507 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800508 } else {
509 // Race: the original read found an owner but now there is none
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800510 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
511 " (where now the monitor appears unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800512 found_owner_string.c_str(),
513 PrettyTypeOf(o).c_str(),
514 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800515 }
516 } else {
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700517 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800518 // Race: originally there was no owner, there is now
519 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
520 " (originally believed to be unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800521 current_owner_string.c_str(),
522 PrettyTypeOf(o).c_str(),
523 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800524 } else {
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700525 if (found_owner_thread_id != current_owner_thread_id) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800526 // Race: originally found and current owner have changed
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800527 ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
528 " owned by '%s') on object of type '%s' on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800529 found_owner_string.c_str(),
530 current_owner_string.c_str(),
531 PrettyTypeOf(o).c_str(),
532 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800533 } else {
534 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
535 " on thread '%s",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800536 current_owner_string.c_str(),
537 PrettyTypeOf(o).c_str(),
538 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800539 }
540 }
541 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700542}
543
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700544bool Monitor::Unlock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700545 DCHECK(self != nullptr);
Mathieu Chartierdee19e32016-04-19 13:46:03 -0700546 uint32_t owner_thread_id = 0u;
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700547 {
548 MutexLock mu(self, monitor_lock_);
549 Thread* owner = owner_;
Mathieu Chartierdee19e32016-04-19 13:46:03 -0700550 if (owner != nullptr) {
551 owner_thread_id = owner->GetThreadId();
552 }
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700553 if (owner == self) {
554 // We own the monitor, so nobody else can be in here.
Andreas Gampefc689872016-04-25 20:08:55 -0700555 AtraceMonitorUnlock();
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700556 if (lock_count_ == 0) {
557 owner_ = nullptr;
558 locking_method_ = nullptr;
559 locking_dex_pc_ = 0;
560 // Wake a contender.
561 monitor_contenders_.Signal(self);
562 } else {
563 --lock_count_;
564 }
565 return true;
Elliott Hughes5f791332011-09-15 17:45:30 -0700566 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700567 }
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700568 // We don't own this, so we're not allowed to unlock it.
569 // The JNI spec says that we should throw IllegalMonitorStateException in this case.
570 FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this);
571 return false;
Elliott Hughes5f791332011-09-15 17:45:30 -0700572}
573
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800574void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
575 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700576 DCHECK(self != nullptr);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800577 DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
Elliott Hughes5f791332011-09-15 17:45:30 -0700578
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700579 monitor_lock_.Lock(self);
580
Elliott Hughes5f791332011-09-15 17:45:30 -0700581 // Make sure that we hold the lock.
582 if (owner_ != self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700583 monitor_lock_.Unlock(self);
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700584 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700585 return;
586 }
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800587
Elliott Hughesdf42c482013-01-09 12:49:02 -0800588 // We need to turn a zero-length timed wait into a regular wait because
589 // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
590 if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
591 why = kWaiting;
592 }
593
Elliott Hughes5f791332011-09-15 17:45:30 -0700594 // Enforce the timeout range.
595 if (ms < 0 || ns < 0 || ns > 999999) {
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700596 monitor_lock_.Unlock(self);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000597 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800598 "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
Elliott Hughes5f791332011-09-15 17:45:30 -0700599 return;
600 }
601
Elliott Hughes5f791332011-09-15 17:45:30 -0700602 /*
603 * Add ourselves to the set of threads waiting on this monitor, and
604 * release our hold. We need to let it go even if we're a few levels
605 * deep in a recursive lock, and we need to restore that later.
606 *
607 * We append to the wait set ahead of clearing the count and owner
608 * fields so the subroutine can check that the calling thread owns
609 * the monitor. Aside from that, the order of member updates is
610 * not order sensitive as we hold the pthread mutex.
611 */
612 AppendToWaitSet(self);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700613 ++num_waiters_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700614 int prev_lock_count = lock_count_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700615 lock_count_ = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700616 owner_ = nullptr;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700617 ArtMethod* saved_method = locking_method_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700618 locking_method_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700619 uintptr_t saved_dex_pc = locking_dex_pc_;
620 locking_dex_pc_ = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700621
Andreas Gampefc689872016-04-25 20:08:55 -0700622 AtraceMonitorUnlock(); // For the implict Unlock() just above. This will only end the deepest
623 // nesting, but that is enough for the visualization, and corresponds to
624 // the single Lock() we do afterwards.
625 AtraceMonitorLock(self, GetObject(), true /* is_wait */);
626
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800627 bool was_interrupted = false;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700629 // Update thread state. If the GC wakes up, it'll ignore us, knowing
630 // that we won't touch any references in this state, and we'll check
631 // our suspend mode before we transition out.
632 ScopedThreadSuspension sts(self, why);
633
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700634 // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700635 MutexLock mu(self, *self->GetWaitMutex());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700636
637 // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700638 // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 // up.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700640 DCHECK(self->GetWaitMonitor() == nullptr);
641 self->SetWaitMonitor(this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700642
643 // Release the monitor lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700644 monitor_contenders_.Signal(self);
645 monitor_lock_.Unlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800647 // Handle the case where the thread was interrupted before we called wait().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700648 if (self->IsInterruptedLocked()) {
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800649 was_interrupted = true;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700650 } else {
651 // Wait for a notification or a timeout to occur.
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800652 if (why == kWaiting) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700653 self->GetWaitConditionVariable()->Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 } else {
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800655 DCHECK(why == kTimedWaiting || why == kSleeping) << why;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700656 self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700657 }
Hans Boehm328c5dc2015-11-11 16:13:57 -0800658 was_interrupted = self->IsInterruptedLocked();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700659 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700660 }
661
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800662 {
663 // We reset the thread's wait_monitor_ field after transitioning back to runnable so
664 // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
665 // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
666 // are waiting on "null".)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700667 MutexLock mu(self, *self->GetWaitMutex());
668 DCHECK(self->GetWaitMonitor() != nullptr);
669 self->SetWaitMonitor(nullptr);
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800670 }
671
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800672 // Allocate the interrupted exception not holding the monitor lock since it may cause a GC.
673 // If the GC requires acquiring the monitor for enqueuing cleared references, this would
674 // cause a deadlock if the monitor is held.
675 if (was_interrupted && interruptShouldThrow) {
676 /*
677 * We were interrupted while waiting, or somebody interrupted an
678 * un-interruptible thread earlier and we're bailing out immediately.
679 *
680 * The doc sayeth: "The interrupted status of the current thread is
681 * cleared when this exception is thrown."
682 */
683 {
684 MutexLock mu(self, *self->GetWaitMutex());
685 self->SetInterruptedLocked(false);
686 }
687 self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr);
688 }
689
Andreas Gampefc689872016-04-25 20:08:55 -0700690 AtraceMonitorUnlock(); // End Wait().
691
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700692 // Re-acquire the monitor and lock.
Elliott Hughes5f791332011-09-15 17:45:30 -0700693 Lock(self);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700694 monitor_lock_.Lock(self);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700695 self->GetWaitMutex()->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700696
Elliott Hughes5f791332011-09-15 17:45:30 -0700697 /*
698 * We remove our thread from wait set after restoring the count
699 * and owner fields so the subroutine can check that the calling
700 * thread owns the monitor. Aside from that, the order of member
701 * updates is not order sensitive as we hold the pthread mutex.
702 */
703 owner_ = self;
Ian Rogers0399dde2012-06-06 17:09:28 -0700704 lock_count_ = prev_lock_count;
705 locking_method_ = saved_method;
706 locking_dex_pc_ = saved_dex_pc;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700707 --num_waiters_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700708 RemoveFromWaitSet(self);
709
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700710 monitor_lock_.Unlock(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700711}
712
713void Monitor::Notify(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700714 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700715 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700716 // Make sure that we hold the lock.
717 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800718 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700719 return;
720 }
721 // Signal the first waiting thread in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700722 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700723 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700724 wait_set_ = thread->GetWaitNext();
725 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700726
727 // Check to see if the thread is still waiting.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800728 MutexLock wait_mu(self, *thread->GetWaitMutex());
Ian Rogersdd7624d2014-03-14 17:43:00 -0700729 if (thread->GetWaitMonitor() != nullptr) {
730 thread->GetWaitConditionVariable()->Signal(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700731 return;
732 }
733 }
734}
735
736void Monitor::NotifyAll(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700737 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700738 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700739 // Make sure that we hold the lock.
740 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800741 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700742 return;
743 }
744 // Signal all threads in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700745 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700746 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700747 wait_set_ = thread->GetWaitNext();
748 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700749 thread->Notify();
750 }
751}
752
Mathieu Chartier590fee92013-09-13 13:46:47 -0700753bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
754 DCHECK(obj != nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700755 // Don't need volatile since we only deflate with mutators suspended.
756 LockWord lw(obj->GetLockWord(false));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700757 // If the lock isn't an inflated monitor, then we don't need to deflate anything.
758 if (lw.GetState() == LockWord::kFatLocked) {
759 Monitor* monitor = lw.FatLockMonitor();
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700760 DCHECK(monitor != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700761 MutexLock mu(self, monitor->monitor_lock_);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700762 // Can't deflate if we have anybody waiting on the CV.
763 if (monitor->num_waiters_ > 0) {
764 return false;
765 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700766 Thread* owner = monitor->owner_;
767 if (owner != nullptr) {
768 // Can't deflate if we are locked and have a hash code.
769 if (monitor->HasHashCode()) {
770 return false;
771 }
772 // Can't deflate if our lock count is too high.
773 if (monitor->lock_count_ > LockWord::kThinLockMaxCount) {
774 return false;
775 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700776 // Deflate to a thin lock.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800777 LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_,
778 lw.ReadBarrierState());
779 // Assume no concurrent read barrier state changes as mutators are suspended.
780 obj->SetLockWord(new_lw, false);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700781 VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
782 << monitor->lock_count_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700783 } else if (monitor->HasHashCode()) {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800784 LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.ReadBarrierState());
785 // Assume no concurrent read barrier state changes as mutators are suspended.
786 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700787 VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700788 } else {
789 // No lock and no hash, just put an empty lock word inside the object.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800790 LockWord new_lw = LockWord::FromDefault(lw.ReadBarrierState());
791 // Assume no concurrent read barrier state changes as mutators are suspended.
792 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700793 VLOG(monitor) << "Deflated" << obj << " to empty lock word";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700794 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700795 // 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 -0700796 // next GC.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700797 monitor->obj_ = GcRoot<mirror::Object>(nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700798 }
799 return true;
800}
801
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700802void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
Andreas Gampe74240812014-04-17 10:35:09 -0700803 DCHECK(self != nullptr);
804 DCHECK(obj != nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700805 // Allocate and acquire a new monitor.
Andreas Gampe74240812014-04-17 10:35:09 -0700806 Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code);
807 DCHECK(m != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700808 if (m->Install(self)) {
Haifeng Li86ab7912014-05-16 10:47:59 +0800809 if (owner != nullptr) {
810 VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
Andreas Gampe74240812014-04-17 10:35:09 -0700811 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800812 } else {
813 VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
Andreas Gampe74240812014-04-17 10:35:09 -0700814 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800815 }
Andreas Gampe74240812014-04-17 10:35:09 -0700816 Runtime::Current()->GetMonitorList()->Add(m);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700817 CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
Andreas Gampe74240812014-04-17 10:35:09 -0700818 } else {
819 MonitorPool::ReleaseMonitor(self, m);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700820 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700821}
822
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700823void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700824 uint32_t hash_code) {
825 DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
826 uint32_t owner_thread_id = lock_word.ThinLockOwner();
827 if (owner_thread_id == self->GetThreadId()) {
828 // We own the monitor, we can easily inflate it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700829 Inflate(self, self, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700830 } else {
831 ThreadList* thread_list = Runtime::Current()->GetThreadList();
832 // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700833 self->SetMonitorEnterObject(obj.Get());
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700834 bool timed_out;
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700835 Thread* owner;
836 {
837 ScopedThreadSuspension sts(self, kBlocked);
838 owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out);
839 }
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700840 if (owner != nullptr) {
841 // We succeeded in suspending the thread, check the lock's status didn't change.
842 lock_word = obj->GetLockWord(true);
843 if (lock_word.GetState() == LockWord::kThinLocked &&
844 lock_word.ThinLockOwner() == owner_thread_id) {
845 // Go ahead and inflate the lock.
846 Inflate(self, owner, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700847 }
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700848 thread_list->Resume(owner, false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700849 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700850 self->SetMonitorEnterObject(nullptr);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700851 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700852}
853
Ian Rogers719d1a32014-03-06 12:13:39 -0800854// Fool annotalysis into thinking that the lock on obj is acquired.
855static mirror::Object* FakeLock(mirror::Object* obj)
856 EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
857 return obj;
858}
859
860// Fool annotalysis into thinking that the lock on obj is release.
861static mirror::Object* FakeUnlock(mirror::Object* obj)
862 UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
863 return obj;
864}
865
Mathieu Chartier23da0262016-07-29 16:26:01 -0700866mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj, bool trylock) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700867 DCHECK(self != nullptr);
868 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -0700869 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -0800870 obj = FakeLock(obj);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700871 uint32_t thread_id = self->GetThreadId();
872 size_t contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700873 StackHandleScope<1> hs(self);
874 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700875 while (true) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700876 LockWord lock_word = h_obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700877 switch (lock_word.GetState()) {
878 case LockWord::kUnlocked: {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800879 LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.ReadBarrierState()));
Ian Rogers228602f2014-07-10 02:07:54 -0700880 if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) {
Andreas Gampefc689872016-04-25 20:08:55 -0700881 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hans Boehm30359612014-05-21 17:46:23 -0700882 // CasLockWord enforces more than the acquire ordering we need here.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700883 return h_obj.Get(); // Success!
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700884 }
885 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700886 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700887 case LockWord::kThinLocked: {
888 uint32_t owner_thread_id = lock_word.ThinLockOwner();
889 if (owner_thread_id == thread_id) {
890 // We own the lock, increase the recursion count.
891 uint32_t new_count = lock_word.ThinLockCount() + 1;
892 if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800893 LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count,
894 lock_word.ReadBarrierState()));
895 if (!kUseReadBarrier) {
896 h_obj->SetLockWord(thin_locked, true);
Andreas Gampefc689872016-04-25 20:08:55 -0700897 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800898 return h_obj.Get(); // Success!
899 } else {
900 // Use CAS to preserve the read barrier state.
901 if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) {
Andreas Gampefc689872016-04-25 20:08:55 -0700902 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800903 return h_obj.Get(); // Success!
904 }
905 }
906 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700907 } else {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700908 // We'd overflow the recursion count, so inflate the monitor.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700909 InflateThinLocked(self, h_obj, lock_word, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700910 }
911 } else {
Mathieu Chartier23da0262016-07-29 16:26:01 -0700912 if (trylock) {
913 return nullptr;
914 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700915 // Contention.
916 contention_count++;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700917 Runtime* runtime = Runtime::Current();
918 if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) {
Mathieu Chartierb363f662014-07-16 13:28:58 -0700919 // TODO: Consider switching the thread state to kBlocked when we are yielding.
Mathieu Chartier251755c2014-07-15 18:10:25 -0700920 // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the
921 // parameter you pass in. This can cause thread suspension to take excessively long
Mathieu Chartierb363f662014-07-16 13:28:58 -0700922 // and make long pauses. See b/16307460.
Mathieu Chartier251755c2014-07-15 18:10:25 -0700923 sched_yield();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700924 } else {
925 contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700926 InflateThinLocked(self, h_obj, lock_word, 0);
Elliott Hughes5f791332011-09-15 17:45:30 -0700927 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700928 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700929 continue; // Start from the beginning.
Elliott Hughes5f791332011-09-15 17:45:30 -0700930 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700931 case LockWord::kFatLocked: {
932 Monitor* mon = lock_word.FatLockMonitor();
Mathieu Chartier23da0262016-07-29 16:26:01 -0700933 if (trylock) {
934 return mon->TryLock(self) ? h_obj.Get() : nullptr;
935 } else {
936 mon->Lock(self);
937 return h_obj.Get(); // Success!
938 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700939 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800940 case LockWord::kHashCode:
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700941 // Inflate with the existing hashcode.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700942 Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
Ian Rogers719d1a32014-03-06 12:13:39 -0800943 continue; // Start from the beginning.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700944 default: {
945 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Andreas Gampefc689872016-04-25 20:08:55 -0700946 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700947 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700948 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700949 }
950}
951
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800952bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700953 DCHECK(self != nullptr);
954 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -0700955 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -0800956 obj = FakeUnlock(obj);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700957 StackHandleScope<1> hs(self);
958 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800959 while (true) {
960 LockWord lock_word = obj->GetLockWord(true);
961 switch (lock_word.GetState()) {
962 case LockWord::kHashCode:
963 // Fall-through.
964 case LockWord::kUnlocked:
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700965 FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700966 return false; // Failure.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800967 case LockWord::kThinLocked: {
968 uint32_t thread_id = self->GetThreadId();
969 uint32_t owner_thread_id = lock_word.ThinLockOwner();
970 if (owner_thread_id != thread_id) {
Mathieu Chartier81c170f2016-04-18 11:43:29 -0700971 FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800972 return false; // Failure.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700973 } else {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800974 // We own the lock, decrease the recursion count.
975 LockWord new_lw = LockWord::Default();
976 if (lock_word.ThinLockCount() != 0) {
977 uint32_t new_count = lock_word.ThinLockCount() - 1;
978 new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.ReadBarrierState());
979 } else {
980 new_lw = LockWord::FromDefault(lock_word.ReadBarrierState());
981 }
982 if (!kUseReadBarrier) {
983 DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
984 h_obj->SetLockWord(new_lw, true);
Andreas Gampe825ab1c2016-05-17 10:13:10 -0700985 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800986 // Success!
987 return true;
988 } else {
989 // Use CAS to preserve the read barrier state.
990 if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, new_lw)) {
Andreas Gampe825ab1c2016-05-17 10:13:10 -0700991 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800992 // Success!
993 return true;
994 }
995 }
996 continue; // Go again.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700997 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700998 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800999 case LockWord::kFatLocked: {
1000 Monitor* mon = lock_word.FatLockMonitor();
1001 return mon->Unlock(self);
1002 }
1003 default: {
1004 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1005 return false;
1006 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001007 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001008 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001009}
1010
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001011void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
Elliott Hughes4cd121e2013-01-07 17:35:41 -08001012 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001013 DCHECK(self != nullptr);
1014 DCHECK(obj != nullptr);
1015 LockWord lock_word = obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -07001016 while (lock_word.GetState() != LockWord::kFatLocked) {
1017 switch (lock_word.GetState()) {
1018 case LockWord::kHashCode:
1019 // Fall-through.
1020 case LockWord::kUnlocked:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001021 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1022 return; // Failure.
Ian Rogers43c69cc2014-08-15 11:09:28 -07001023 case LockWord::kThinLocked: {
1024 uint32_t thread_id = self->GetThreadId();
1025 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1026 if (owner_thread_id != thread_id) {
1027 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1028 return; // Failure.
1029 } else {
1030 // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
1031 // re-load.
1032 Inflate(self, self, obj, 0);
1033 lock_word = obj->GetLockWord(true);
1034 }
1035 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001036 }
Ian Rogers43c69cc2014-08-15 11:09:28 -07001037 case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through.
1038 default: {
1039 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1040 return;
1041 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001042 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001043 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001044 Monitor* mon = lock_word.FatLockMonitor();
1045 mon->Wait(self, ms, ns, interruptShouldThrow, why);
Elliott Hughes5f791332011-09-15 17:45:30 -07001046}
1047
Ian Rogers13c479e2013-10-11 07:59:01 -07001048void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001049 DCHECK(self != nullptr);
1050 DCHECK(obj != nullptr);
1051 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001052 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001053 case LockWord::kHashCode:
1054 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001055 case LockWord::kUnlocked:
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001056 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001057 return; // Failure.
1058 case LockWord::kThinLocked: {
1059 uint32_t thread_id = self->GetThreadId();
1060 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1061 if (owner_thread_id != thread_id) {
1062 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
1063 return; // Failure.
1064 } else {
1065 // We own the lock but there's no Monitor and therefore no waiters.
1066 return; // Success.
1067 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001068 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001069 case LockWord::kFatLocked: {
1070 Monitor* mon = lock_word.FatLockMonitor();
1071 if (notify_all) {
1072 mon->NotifyAll(self);
1073 } else {
1074 mon->Notify(self);
1075 }
1076 return; // Success.
1077 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001078 default: {
1079 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1080 return;
1081 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001082 }
1083}
1084
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001085uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001086 DCHECK(obj != nullptr);
1087 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001088 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001089 case LockWord::kHashCode:
1090 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001091 case LockWord::kUnlocked:
1092 return ThreadList::kInvalidThreadId;
1093 case LockWord::kThinLocked:
1094 return lock_word.ThinLockOwner();
1095 case LockWord::kFatLocked: {
1096 Monitor* mon = lock_word.FatLockMonitor();
1097 return mon->GetOwnerThreadId();
Elliott Hughes5f791332011-09-15 17:45:30 -07001098 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001099 default: {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001100 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001101 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001102 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001103 }
1104}
1105
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001106void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001107 // Determine the wait message and object we're waiting or blocked upon.
1108 mirror::Object* pretty_object = nullptr;
1109 const char* wait_message = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001110 uint32_t lock_owner = ThreadList::kInvalidThreadId;
Ian Rogersd803bc72014-04-01 15:33:03 -07001111 ThreadState state = thread->GetState();
Elliott Hughesb4e94fd2013-01-08 14:41:26 -08001112 if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001113 wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on ";
1114 Thread* self = Thread::Current();
1115 MutexLock mu(self, *thread->GetWaitMutex());
1116 Monitor* monitor = thread->GetWaitMonitor();
1117 if (monitor != nullptr) {
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001118 pretty_object = monitor->GetObject();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001119 }
Elliott Hughes34e06962012-04-09 13:55:55 -07001120 } else if (state == kBlocked) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001121 wait_message = " - waiting to lock ";
1122 pretty_object = thread->GetMonitorEnterObject();
1123 if (pretty_object != nullptr) {
1124 lock_owner = pretty_object->GetLockOwnerThreadId();
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001125 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001126 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001127
Ian Rogersd803bc72014-04-01 15:33:03 -07001128 if (wait_message != nullptr) {
1129 if (pretty_object == nullptr) {
1130 os << wait_message << "an unknown object";
1131 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001132 if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
Ian Rogersd803bc72014-04-01 15:33:03 -07001133 Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
1134 // Getting the identity hashcode here would result in lock inflation and suspension of the
1135 // current thread, which isn't safe if this is the only runnable thread.
1136 os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
1137 reinterpret_cast<intptr_t>(pretty_object),
1138 PrettyTypeOf(pretty_object).c_str());
1139 } else {
1140 // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
Mathieu Chartier49361592015-01-22 16:36:10 -08001141 // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
1142 // suspension and move pretty_object.
1143 const std::string pretty_type(PrettyTypeOf(pretty_object));
Ian Rogersd803bc72014-04-01 15:33:03 -07001144 os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
Mathieu Chartier49361592015-01-22 16:36:10 -08001145 pretty_type.c_str());
Ian Rogersd803bc72014-04-01 15:33:03 -07001146 }
1147 }
1148 // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
1149 if (lock_owner != ThreadList::kInvalidThreadId) {
1150 os << " held by thread " << lock_owner;
1151 }
1152 os << "\n";
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001153 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001154}
1155
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001156mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001157 // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
1158 // definition of contended that includes a monitor a thread is trying to enter...
Ian Rogersdd7624d2014-03-14 17:43:00 -07001159 mirror::Object* result = thread->GetMonitorEnterObject();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001160 if (result == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001161 // ...but also a monitor that the thread is waiting on.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001162 MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
1163 Monitor* monitor = thread->GetWaitMonitor();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001164 if (monitor != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001165 result = monitor->GetObject();
Elliott Hughesf9501702013-01-11 11:22:27 -08001166 }
1167 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001168 return result;
Elliott Hughesf9501702013-01-11 11:22:27 -08001169}
1170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001171void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
Andreas Gampe760172c2014-08-16 13:41:10 -07001172 void* callback_context, bool abort_on_failure) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001173 ArtMethod* m = stack_visitor->GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001174 CHECK(m != nullptr);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001175
1176 // Native methods are an easy special case.
1177 // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
1178 if (m->IsNative()) {
1179 if (m->IsSynchronized()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001180 mirror::Object* jni_this =
1181 stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001182 callback(jni_this, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001183 }
1184 return;
1185 }
1186
jeffhao61f916c2012-10-25 17:48:51 -07001187 // Proxy methods should not be synchronized.
1188 if (m->IsProxyMethod()) {
1189 CHECK(!m->IsSynchronized());
1190 return;
1191 }
1192
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001193 // Is there any reason to believe there's any synchronization in this method?
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001194 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001195 CHECK(code_item != nullptr) << PrettyMethod(m);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001196 if (code_item->tries_size_ == 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001197 return; // No "tries" implies no synchronization, so no held locks to report.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001198 }
1199
Andreas Gampe760172c2014-08-16 13:41:10 -07001200 // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
1201 // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
1202 // inconsistent stack anyways.
1203 uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
1204 if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) {
1205 LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m);
1206 return;
1207 }
1208
Elliott Hughes80537bb2013-01-04 16:37:26 -08001209 // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
1210 // the locks held in this stack frame.
1211 std::vector<uint32_t> monitor_enter_dex_pcs;
Andreas Gampe760172c2014-08-16 13:41:10 -07001212 verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs);
Mathieu Chartiere6a8eec2015-01-06 14:17:57 -08001213 for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) {
Elliott Hughes80537bb2013-01-04 16:37:26 -08001214 // The verifier works in terms of the dex pcs of the monitor-enter instructions.
1215 // We want the registers used by those instructions (so we can read the values out of them).
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001216 const Instruction* monitor_enter_instruction =
1217 Instruction::At(&code_item->insns_[monitor_dex_pc]);
Elliott Hughes80537bb2013-01-04 16:37:26 -08001218
1219 // Quick sanity check.
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001220 CHECK_EQ(monitor_enter_instruction->Opcode(), Instruction::MONITOR_ENTER)
1221 << "expected monitor-enter @" << monitor_dex_pc << "; was "
1222 << reinterpret_cast<const void*>(monitor_enter_instruction);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001223
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001224 uint16_t monitor_register = monitor_enter_instruction->VRegA();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001225 uint32_t value;
1226 bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value);
1227 CHECK(success) << "Failed to read v" << monitor_register << " of kind "
1228 << kReferenceVReg << " in method " << PrettyMethod(m);
1229 mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001230 callback(o, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001231 }
1232}
1233
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001234bool Monitor::IsValidLockWord(LockWord lock_word) {
1235 switch (lock_word.GetState()) {
1236 case LockWord::kUnlocked:
1237 // Nothing to check.
1238 return true;
1239 case LockWord::kThinLocked:
1240 // Basic sanity check of owner.
1241 return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1242 case LockWord::kFatLocked: {
1243 // Check the monitor appears in the monitor list.
1244 Monitor* mon = lock_word.FatLockMonitor();
1245 MonitorList* list = Runtime::Current()->GetMonitorList();
1246 MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1247 for (Monitor* list_mon : list->list_) {
1248 if (mon == list_mon) {
1249 return true; // Found our monitor.
1250 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001251 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001252 return false; // Fail - unowned monitor in an object.
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001253 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001254 case LockWord::kHashCode:
1255 return true;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001256 default:
1257 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001258 UNREACHABLE();
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001259 }
1260}
1261
Mathieu Chartier90443472015-07-16 20:32:27 -07001262bool Monitor::IsLocked() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001263 MutexLock mu(Thread::Current(), monitor_lock_);
1264 return owner_ != nullptr;
1265}
1266
Mathieu Chartierdc3c6302016-04-15 19:11:45 -07001267void Monitor::TranslateLocation(ArtMethod* method,
1268 uint32_t dex_pc,
1269 const char** source_file,
1270 int32_t* line_number) {
jeffhao33dc7712011-11-09 17:54:24 -08001271 // If method is null, location is unknown
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001272 if (method == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001273 *source_file = "";
1274 *line_number = 0;
jeffhao33dc7712011-11-09 17:54:24 -08001275 return;
1276 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001277 *source_file = method->GetDeclaringClassSourceFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001278 if (*source_file == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001279 *source_file = "";
Elliott Hughes12c51e32012-01-17 20:25:05 -08001280 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001281 *line_number = method->GetLineNumFromDexPC(dex_pc);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001282}
1283
1284uint32_t Monitor::GetOwnerThreadId() {
1285 MutexLock mu(Thread::Current(), monitor_lock_);
1286 Thread* owner = owner_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001287 if (owner != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001288 return owner->GetThreadId();
1289 } else {
1290 return ThreadList::kInvalidThreadId;
1291 }
jeffhao33dc7712011-11-09 17:54:24 -08001292}
1293
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001294MonitorList::MonitorList()
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001295 : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001296 monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001297}
1298
1299MonitorList::~MonitorList() {
Andreas Gampe74240812014-04-17 10:35:09 -07001300 Thread* self = Thread::Current();
1301 MutexLock mu(self, monitor_list_lock_);
1302 // Release all monitors to the pool.
1303 // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
1304 // clear faster in the pool.
1305 MonitorPool::ReleaseMonitors(self, &list_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001306}
1307
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001308void MonitorList::DisallowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001309 CHECK(!kUseReadBarrier);
Ian Rogers50b35e22012-10-04 10:09:15 -07001310 MutexLock mu(Thread::Current(), monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001311 allow_new_monitors_ = false;
1312}
1313
1314void MonitorList::AllowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001315 CHECK(!kUseReadBarrier);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001316 Thread* self = Thread::Current();
1317 MutexLock mu(self, monitor_list_lock_);
1318 allow_new_monitors_ = true;
1319 monitor_add_condition_.Broadcast(self);
1320}
1321
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001322void MonitorList::BroadcastForNewMonitors() {
1323 CHECK(kUseReadBarrier);
1324 Thread* self = Thread::Current();
1325 MutexLock mu(self, monitor_list_lock_);
1326 monitor_add_condition_.Broadcast(self);
1327}
1328
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001329void MonitorList::Add(Monitor* m) {
1330 Thread* self = Thread::Current();
1331 MutexLock mu(self, monitor_list_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001332 while (UNLIKELY((!kUseReadBarrier && !allow_new_monitors_) ||
1333 (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001334 monitor_add_condition_.WaitHoldingLocks(self);
1335 }
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001336 list_.push_front(m);
1337}
1338
Mathieu Chartier97509952015-07-13 14:35:43 -07001339void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
Andreas Gampe74240812014-04-17 10:35:09 -07001340 Thread* self = Thread::Current();
1341 MutexLock mu(self, monitor_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001342 for (auto it = list_.begin(); it != list_.end(); ) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001343 Monitor* m = *it;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001344 // Disable the read barrier in GetObject() as this is called by GC.
1345 mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001346 // The object of a monitor can be null if we have deflated it.
Mathieu Chartier97509952015-07-13 14:35:43 -07001347 mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001348 if (new_obj == nullptr) {
1349 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001350 << obj;
Andreas Gampe74240812014-04-17 10:35:09 -07001351 MonitorPool::ReleaseMonitor(self, m);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001352 it = list_.erase(it);
1353 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001354 m->SetObject(new_obj);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001355 ++it;
1356 }
1357 }
1358}
1359
Mathieu Chartier97509952015-07-13 14:35:43 -07001360class MonitorDeflateVisitor : public IsMarkedVisitor {
1361 public:
1362 MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
1363
1364 virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001365 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -07001366 if (Monitor::Deflate(self_, object)) {
1367 DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1368 ++deflate_count_;
1369 // If we deflated, return null so that the monitor gets removed from the array.
1370 return nullptr;
1371 }
1372 return object; // Monitor was not deflated.
1373 }
1374
1375 Thread* const self_;
1376 size_t deflate_count_;
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001377};
1378
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001379size_t MonitorList::DeflateMonitors() {
Mathieu Chartier97509952015-07-13 14:35:43 -07001380 MonitorDeflateVisitor visitor;
1381 Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
1382 SweepMonitorList(&visitor);
1383 return visitor.deflate_count_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001384}
1385
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001386MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001387 DCHECK(obj != nullptr);
1388 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001389 switch (lock_word.GetState()) {
1390 case LockWord::kUnlocked:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001391 // Fall-through.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001392 case LockWord::kForwardingAddress:
1393 // Fall-through.
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001394 case LockWord::kHashCode:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001395 break;
1396 case LockWord::kThinLocked:
1397 owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1398 entry_count_ = 1 + lock_word.ThinLockCount();
1399 // Thin locks have no waiters.
1400 break;
1401 case LockWord::kFatLocked: {
1402 Monitor* mon = lock_word.FatLockMonitor();
1403 owner_ = mon->owner_;
1404 entry_count_ = 1 + mon->lock_count_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001405 for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001406 waiters_.push_back(waiter);
1407 }
1408 break;
Elliott Hughesf327e072013-01-09 16:01:26 -08001409 }
1410 }
1411}
1412
Elliott Hughes5f791332011-09-15 17:45:30 -07001413} // namespace art