blob: 71c866f3d6ec6dbbb2e170015b52de54dc5bb257 [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 Gampec7ed09b2016-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 Chartier0ffdc9c2016-04-19 13:46:03 -0700297std::string Monitor::PrettyContentionInfo(const std::string& owner_name,
298 pid_t owner_tid,
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700299 ArtMethod* owners_method,
300 uint32_t owners_dex_pc,
301 size_t num_waiters) {
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700302 const char* owners_filename;
Goran Jakovljevic49c882b2016-04-19 10:27:21 +0200303 int32_t owners_line_number = 0;
Mathieu Chartier74b3c8f2016-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 Chartier0ffdc9c2016-04-19 13:46:03 -0700308 oss << "monitor contention with owner " << owner_name << " (" << owner_tid << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700309 if (owners_method != nullptr) {
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700310 oss << " at " << PrettyMethod(owners_method);
311 oss << "(" << owners_filename << ":" << owners_line_number << ")";
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700312 }
313 oss << " waiters=" << num_waiters;
314 return oss.str();
315}
316
Elliott Hughes5f791332011-09-15 17:45:30 -0700317void Monitor::Lock(Thread* self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700318 MutexLock mu(self, monitor_lock_);
319 while (true) {
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700320 if (owner_ == nullptr) { // Unowned.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700321 owner_ = self;
322 CHECK_EQ(lock_count_, 0);
323 // When debugging, save the current monitor holder for future
324 // acquisition failures to use in sampled logging.
325 if (lock_profiling_threshold_ != 0) {
326 locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
327 }
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700328 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700329 } else if (owner_ == self) { // Recursive.
330 lock_count_++;
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700331 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700332 }
333 // Contended.
334 const bool log_contention = (lock_profiling_threshold_ != 0);
Xin Guanb894a192014-08-22 11:55:37 -0500335 uint64_t wait_start_ms = log_contention ? MilliTime() : 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 ArtMethod* owners_method = locking_method_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700337 uint32_t owners_dex_pc = locking_dex_pc_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700338 // Do this before releasing the lock so that we don't get deflated.
Mathieu Chartierb9001ab2014-10-03 13:28:46 -0700339 size_t num_waiters = num_waiters_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700340 ++num_waiters_;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700341 monitor_lock_.Unlock(self); // Let go of locks in order.
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700342 self->SetMonitorEnterObject(GetObject());
Elliott Hughes5f791332011-09-15 17:45:30 -0700343 {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700344 uint32_t original_owner_thread_id = 0u;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700345 ScopedThreadStateChange tsc(self, kBlocked); // Change to blocked and give up mutator_lock_.
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700346 {
347 // Reacquire monitor_lock_ without mutator_lock_ for Wait.
348 MutexLock mu2(self, monitor_lock_);
349 if (owner_ != nullptr) { // Did the owner_ give the lock up?
350 original_owner_thread_id = owner_->GetThreadId();
351 if (ATRACE_ENABLED()) {
352 std::ostringstream oss;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700353 std::string name;
354 owner_->GetThreadName(name);
355 oss << PrettyContentionInfo(name,
356 owner_->GetTid(),
357 owners_method,
358 owners_dex_pc,
359 num_waiters);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700360 // Add info for contending thread.
361 uint32_t pc;
362 ArtMethod* m = self->GetCurrentMethod(&pc);
363 const char* filename;
364 int32_t line_number;
365 TranslateLocation(m, pc, &filename, &line_number);
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700366 oss << " blocking from "
367 << PrettyMethod(m) << "(" << (filename != nullptr ? filename : "null") << ":"
368 << line_number << ")";
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700369 ATRACE_BEGIN(oss.str().c_str());
370 }
371 monitor_contenders_.Wait(self); // Still contended so wait.
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800372 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700373 }
374 if (original_owner_thread_id != 0u) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700375 // Woken from contention.
376 if (log_contention) {
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700377 uint32_t original_owner_tid = 0;
378 std::string original_owner_name;
379 {
380 MutexLock mu2(Thread::Current(), *Locks::thread_list_lock_);
381 // Re-find the owner in case the thread got killed.
382 Thread* original_owner = Runtime::Current()->GetThreadList()->FindThreadByThreadId(
383 original_owner_thread_id);
384 // Do not do any work that requires the mutator lock.
385 if (original_owner != nullptr) {
386 original_owner_tid = original_owner->GetTid();
387 original_owner->GetThreadName(original_owner_name);
388 }
389 }
390
391 if (original_owner_tid != 0u) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700392 uint64_t wait_ms = MilliTime() - wait_start_ms;
393 uint32_t sample_percent;
394 if (wait_ms >= lock_profiling_threshold_) {
395 sample_percent = 100;
396 } else {
397 sample_percent = 100 * wait_ms / lock_profiling_threshold_;
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -0700398 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700399 if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
400 if (wait_ms > kLongWaitMs && owners_method != nullptr) {
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700401 uint32_t pc;
402 ArtMethod* m = self->GetCurrentMethod(&pc);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700403 // TODO: We should maybe check that original_owner is still a live thread.
404 LOG(WARNING) << "Long "
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700405 << PrettyContentionInfo(original_owner_name,
406 original_owner_tid,
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700407 owners_method,
408 owners_dex_pc,
409 num_waiters)
Mathieu Chartier36891fe2016-04-28 17:21:08 -0700410 << " in " << PrettyMethod(m) << " for " << PrettyDuration(MsToNs(wait_ms));
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700411 }
412 const char* owners_filename;
413 int32_t owners_line_number;
414 TranslateLocation(owners_method,
415 owners_dex_pc,
416 &owners_filename,
417 &owners_line_number);
418 LogContentionEvent(self,
419 wait_ms,
420 sample_percent,
421 owners_filename,
422 owners_line_number);
423 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700424 }
425 }
Mathieu Chartierf0dc8b52014-12-17 10:13:30 -0800426 ATRACE_END();
Elliott Hughesfc861622011-10-17 17:57:47 -0700427 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700428 }
Mathieu Chartiera6e7f082014-05-22 14:43:37 -0700429 self->SetMonitorEnterObject(nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700430 monitor_lock_.Lock(self); // Reacquire locks in order.
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700431 --num_waiters_;
Elliott Hughesfc861622011-10-17 17:57:47 -0700432 }
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700433
434 AtraceMonitorLock(self, GetObject(), false /* is_wait */);
Elliott Hughes5f791332011-09-15 17:45:30 -0700435}
436
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800437static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
438 __attribute__((format(printf, 1, 2)));
439
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700440static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
Mathieu Chartier90443472015-07-16 20:32:27 -0700441 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800442 va_list args;
443 va_start(args, fmt);
Ian Rogers62d6c772013-02-27 08:32:07 -0800444 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000445 self->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700446 if (!Runtime::Current()->IsStarted() || VLOG_IS_ON(monitor)) {
Brian Carlstrom64277f32012-03-26 23:53:34 -0700447 std::ostringstream ss;
Ian Rogers62d6c772013-02-27 08:32:07 -0800448 self->Dump(ss);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700449 LOG(Runtime::Current()->IsStarted() ? INFO : ERROR)
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000450 << self->GetException()->Dump() << "\n" << ss.str();
Brian Carlstrom64277f32012-03-26 23:53:34 -0700451 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800452 va_end(args);
453}
454
Elliott Hughesd4237412012-02-21 11:24:45 -0800455static std::string ThreadToString(Thread* thread) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700456 if (thread == nullptr) {
457 return "nullptr";
Elliott Hughesd4237412012-02-21 11:24:45 -0800458 }
459 std::ostringstream oss;
460 // TODO: alternatively, we could just return the thread's name.
461 oss << *thread;
462 return oss.str();
463}
464
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700465void Monitor::FailedUnlock(mirror::Object* o,
466 uint32_t expected_owner_thread_id,
467 uint32_t found_owner_thread_id,
Elliott Hughesffb465f2012-03-01 18:46:05 -0800468 Monitor* monitor) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700469 // Acquire thread list lock so threads won't disappear from under us.
Elliott Hughesffb465f2012-03-01 18:46:05 -0800470 std::string current_owner_string;
471 std::string expected_owner_string;
472 std::string found_owner_string;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700473 uint32_t current_owner_thread_id = 0u;
Elliott Hughesffb465f2012-03-01 18:46:05 -0800474 {
Ian Rogers50b35e22012-10-04 10:09:15 -0700475 MutexLock mu(Thread::Current(), *Locks::thread_list_lock_);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700476 ThreadList* const thread_list = Runtime::Current()->GetThreadList();
477 Thread* expected_owner = thread_list->FindThreadByThreadId(expected_owner_thread_id);
478 Thread* found_owner = thread_list->FindThreadByThreadId(found_owner_thread_id);
479
Elliott Hughesffb465f2012-03-01 18:46:05 -0800480 // Re-read owner now that we hold lock.
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700481 Thread* current_owner = (monitor != nullptr) ? monitor->GetOwner() : nullptr;
482 if (current_owner != nullptr) {
483 current_owner_thread_id = current_owner->GetThreadId();
484 }
Elliott Hughesffb465f2012-03-01 18:46:05 -0800485 // Get short descriptions of the threads involved.
486 current_owner_string = ThreadToString(current_owner);
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700487 expected_owner_string = expected_owner != nullptr ? ThreadToString(expected_owner) : "unnamed";
488 found_owner_string = found_owner != nullptr ? ThreadToString(found_owner) : "unnamed";
Elliott Hughesffb465f2012-03-01 18:46:05 -0800489 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700490
491 if (current_owner_thread_id == 0u) {
492 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800493 ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
494 " on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800495 PrettyTypeOf(o).c_str(),
496 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800497 } else {
498 // Race: the original read found an owner but now there is none
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800499 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
500 " (where now the monitor appears unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800501 found_owner_string.c_str(),
502 PrettyTypeOf(o).c_str(),
503 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800504 }
505 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700506 if (found_owner_thread_id == 0u) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800507 // Race: originally there was no owner, there is now
508 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
509 " (originally believed to be unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800510 current_owner_string.c_str(),
511 PrettyTypeOf(o).c_str(),
512 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800513 } else {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700514 if (found_owner_thread_id != current_owner_thread_id) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800515 // Race: originally found and current owner have changed
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800516 ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
517 " owned by '%s') on object of type '%s' on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800518 found_owner_string.c_str(),
519 current_owner_string.c_str(),
520 PrettyTypeOf(o).c_str(),
521 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800522 } else {
523 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
524 " on thread '%s",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800525 current_owner_string.c_str(),
526 PrettyTypeOf(o).c_str(),
527 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800528 }
529 }
530 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700531}
532
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700533bool Monitor::Unlock(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700534 DCHECK(self != nullptr);
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700535 uint32_t owner_thread_id = 0u;
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700536 {
537 MutexLock mu(self, monitor_lock_);
538 Thread* owner = owner_;
Mathieu Chartier0ffdc9c2016-04-19 13:46:03 -0700539 if (owner != nullptr) {
540 owner_thread_id = owner->GetThreadId();
541 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700542 if (owner == self) {
543 // We own the monitor, so nobody else can be in here.
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700544 AtraceMonitorUnlock();
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700545 if (lock_count_ == 0) {
546 owner_ = nullptr;
547 locking_method_ = nullptr;
548 locking_dex_pc_ = 0;
549 // Wake a contender.
550 monitor_contenders_.Signal(self);
551 } else {
552 --lock_count_;
553 }
554 return true;
Elliott Hughes5f791332011-09-15 17:45:30 -0700555 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700556 }
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700557 // We don't own this, so we're not allowed to unlock it.
558 // The JNI spec says that we should throw IllegalMonitorStateException in this case.
559 FailedUnlock(GetObject(), self->GetThreadId(), owner_thread_id, this);
560 return false;
Elliott Hughes5f791332011-09-15 17:45:30 -0700561}
562
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800563void Monitor::Wait(Thread* self, int64_t ms, int32_t ns,
564 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700565 DCHECK(self != nullptr);
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800566 DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping);
Elliott Hughes5f791332011-09-15 17:45:30 -0700567
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700568 monitor_lock_.Lock(self);
569
Elliott Hughes5f791332011-09-15 17:45:30 -0700570 // Make sure that we hold the lock.
571 if (owner_ != self) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700572 monitor_lock_.Unlock(self);
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700573 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700574 return;
575 }
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800576
Elliott Hughesdf42c482013-01-09 12:49:02 -0800577 // We need to turn a zero-length timed wait into a regular wait because
578 // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait().
579 if (why == kTimedWaiting && (ms == 0 && ns == 0)) {
580 why = kWaiting;
581 }
582
Elliott Hughes5f791332011-09-15 17:45:30 -0700583 // Enforce the timeout range.
584 if (ms < 0 || ns < 0 || ns > 999999) {
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700585 monitor_lock_.Unlock(self);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000586 self->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800587 "timeout arguments out of range: ms=%" PRId64 " ns=%d", ms, ns);
Elliott Hughes5f791332011-09-15 17:45:30 -0700588 return;
589 }
590
Elliott Hughes5f791332011-09-15 17:45:30 -0700591 /*
592 * Add ourselves to the set of threads waiting on this monitor, and
593 * release our hold. We need to let it go even if we're a few levels
594 * deep in a recursive lock, and we need to restore that later.
595 *
596 * We append to the wait set ahead of clearing the count and owner
597 * fields so the subroutine can check that the calling thread owns
598 * the monitor. Aside from that, the order of member updates is
599 * not order sensitive as we hold the pthread mutex.
600 */
601 AppendToWaitSet(self);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700602 ++num_waiters_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700603 int prev_lock_count = lock_count_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700604 lock_count_ = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700605 owner_ = nullptr;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700606 ArtMethod* saved_method = locking_method_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700607 locking_method_ = nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700608 uintptr_t saved_dex_pc = locking_dex_pc_;
609 locking_dex_pc_ = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700610
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700611 AtraceMonitorUnlock(); // For the implict Unlock() just above. This will only end the deepest
612 // nesting, but that is enough for the visualization, and corresponds to
613 // the single Lock() we do afterwards.
614 AtraceMonitorLock(self, GetObject(), true /* is_wait */);
615
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800616 bool was_interrupted = false;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700617 {
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700618 // Update thread state. If the GC wakes up, it'll ignore us, knowing
619 // that we won't touch any references in this state, and we'll check
620 // our suspend mode before we transition out.
621 ScopedThreadSuspension sts(self, why);
622
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700624 MutexLock mu(self, *self->GetWaitMutex());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700625
626 // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700627 // non-null a notifying or interrupting thread must signal the thread's wait_cond_ to wake it
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700628 // up.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700629 DCHECK(self->GetWaitMonitor() == nullptr);
630 self->SetWaitMonitor(this);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700631
632 // Release the monitor lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700633 monitor_contenders_.Signal(self);
634 monitor_lock_.Unlock(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700635
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800636 // Handle the case where the thread was interrupted before we called wait().
Ian Rogersdd7624d2014-03-14 17:43:00 -0700637 if (self->IsInterruptedLocked()) {
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800638 was_interrupted = true;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700639 } else {
640 // Wait for a notification or a timeout to occur.
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800641 if (why == kWaiting) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700642 self->GetWaitConditionVariable()->Wait(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700643 } else {
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800644 DCHECK(why == kTimedWaiting || why == kSleeping) << why;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700645 self->GetWaitConditionVariable()->TimedWait(self, ms, ns);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700646 }
Hans Boehm328c5dc2015-11-11 16:13:57 -0800647 was_interrupted = self->IsInterruptedLocked();
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700649 }
650
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800651 {
652 // We reset the thread's wait_monitor_ field after transitioning back to runnable so
653 // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging
654 // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads
655 // are waiting on "null".)
Ian Rogersdd7624d2014-03-14 17:43:00 -0700656 MutexLock mu(self, *self->GetWaitMutex());
657 DCHECK(self->GetWaitMonitor() != nullptr);
658 self->SetWaitMonitor(nullptr);
Elliott Hughesb4e94fd2013-01-08 14:41:26 -0800659 }
660
Mathieu Chartierdaed5d82016-03-10 10:49:35 -0800661 // Allocate the interrupted exception not holding the monitor lock since it may cause a GC.
662 // If the GC requires acquiring the monitor for enqueuing cleared references, this would
663 // cause a deadlock if the monitor is held.
664 if (was_interrupted && interruptShouldThrow) {
665 /*
666 * We were interrupted while waiting, or somebody interrupted an
667 * un-interruptible thread earlier and we're bailing out immediately.
668 *
669 * The doc sayeth: "The interrupted status of the current thread is
670 * cleared when this exception is thrown."
671 */
672 {
673 MutexLock mu(self, *self->GetWaitMutex());
674 self->SetInterruptedLocked(false);
675 }
676 self->ThrowNewException("Ljava/lang/InterruptedException;", nullptr);
677 }
678
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700679 AtraceMonitorUnlock(); // End Wait().
680
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700681 // Re-acquire the monitor and lock.
Elliott Hughes5f791332011-09-15 17:45:30 -0700682 Lock(self);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700683 monitor_lock_.Lock(self);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700684 self->GetWaitMutex()->AssertNotHeld(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700685
Elliott Hughes5f791332011-09-15 17:45:30 -0700686 /*
687 * We remove our thread from wait set after restoring the count
688 * and owner fields so the subroutine can check that the calling
689 * thread owns the monitor. Aside from that, the order of member
690 * updates is not order sensitive as we hold the pthread mutex.
691 */
692 owner_ = self;
Ian Rogers0399dde2012-06-06 17:09:28 -0700693 lock_count_ = prev_lock_count;
694 locking_method_ = saved_method;
695 locking_dex_pc_ = saved_dex_pc;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700696 --num_waiters_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700697 RemoveFromWaitSet(self);
698
Elena Sayapina1af6a1f2014-06-20 16:58:37 +0700699 monitor_lock_.Unlock(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700700}
701
702void Monitor::Notify(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700703 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700704 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700705 // Make sure that we hold the lock.
706 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800707 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700708 return;
709 }
710 // Signal the first waiting thread in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700711 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700712 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700713 wait_set_ = thread->GetWaitNext();
714 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700715
716 // Check to see if the thread is still waiting.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800717 MutexLock wait_mu(self, *thread->GetWaitMutex());
Ian Rogersdd7624d2014-03-14 17:43:00 -0700718 if (thread->GetWaitMonitor() != nullptr) {
719 thread->GetWaitConditionVariable()->Signal(self);
Elliott Hughes5f791332011-09-15 17:45:30 -0700720 return;
721 }
722 }
723}
724
725void Monitor::NotifyAll(Thread* self) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700726 DCHECK(self != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700727 MutexLock mu(self, monitor_lock_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700728 // Make sure that we hold the lock.
729 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800730 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700731 return;
732 }
733 // Signal all threads in the wait set.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700734 while (wait_set_ != nullptr) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700735 Thread* thread = wait_set_;
Ian Rogersdd7624d2014-03-14 17:43:00 -0700736 wait_set_ = thread->GetWaitNext();
737 thread->SetWaitNext(nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700738 thread->Notify();
739 }
740}
741
Mathieu Chartier590fee92013-09-13 13:46:47 -0700742bool Monitor::Deflate(Thread* self, mirror::Object* obj) {
743 DCHECK(obj != nullptr);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700744 // Don't need volatile since we only deflate with mutators suspended.
745 LockWord lw(obj->GetLockWord(false));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700746 // If the lock isn't an inflated monitor, then we don't need to deflate anything.
747 if (lw.GetState() == LockWord::kFatLocked) {
748 Monitor* monitor = lw.FatLockMonitor();
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700749 DCHECK(monitor != nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700750 MutexLock mu(self, monitor->monitor_lock_);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700751 // Can't deflate if we have anybody waiting on the CV.
752 if (monitor->num_waiters_ > 0) {
753 return false;
754 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700755 Thread* owner = monitor->owner_;
756 if (owner != nullptr) {
757 // Can't deflate if we are locked and have a hash code.
758 if (monitor->HasHashCode()) {
759 return false;
760 }
761 // Can't deflate if our lock count is too high.
762 if (monitor->lock_count_ > LockWord::kThinLockMaxCount) {
763 return false;
764 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700765 // Deflate to a thin lock.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800766 LockWord new_lw = LockWord::FromThinLockId(owner->GetThreadId(), monitor->lock_count_,
767 lw.ReadBarrierState());
768 // Assume no concurrent read barrier state changes as mutators are suspended.
769 obj->SetLockWord(new_lw, false);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700770 VLOG(monitor) << "Deflated " << obj << " to thin lock " << owner->GetTid() << " / "
771 << monitor->lock_count_;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700772 } else if (monitor->HasHashCode()) {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800773 LockWord new_lw = LockWord::FromHashCode(monitor->GetHashCode(), lw.ReadBarrierState());
774 // Assume no concurrent read barrier state changes as mutators are suspended.
775 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700776 VLOG(monitor) << "Deflated " << obj << " to hash monitor " << monitor->GetHashCode();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700777 } else {
778 // No lock and no hash, just put an empty lock word inside the object.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800779 LockWord new_lw = LockWord::FromDefault(lw.ReadBarrierState());
780 // Assume no concurrent read barrier state changes as mutators are suspended.
781 obj->SetLockWord(new_lw, false);
Mathieu Chartier440e4ce2014-03-31 16:36:35 -0700782 VLOG(monitor) << "Deflated" << obj << " to empty lock word";
Mathieu Chartier590fee92013-09-13 13:46:47 -0700783 }
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700784 // 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 -0700785 // next GC.
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700786 monitor->obj_ = GcRoot<mirror::Object>(nullptr);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700787 }
788 return true;
789}
790
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700791void Monitor::Inflate(Thread* self, Thread* owner, mirror::Object* obj, int32_t hash_code) {
Andreas Gampe74240812014-04-17 10:35:09 -0700792 DCHECK(self != nullptr);
793 DCHECK(obj != nullptr);
Elliott Hughes5f791332011-09-15 17:45:30 -0700794 // Allocate and acquire a new monitor.
Andreas Gampe74240812014-04-17 10:35:09 -0700795 Monitor* m = MonitorPool::CreateMonitor(self, owner, obj, hash_code);
796 DCHECK(m != nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700797 if (m->Install(self)) {
Haifeng Li86ab7912014-05-16 10:47:59 +0800798 if (owner != nullptr) {
799 VLOG(monitor) << "monitor: thread" << owner->GetThreadId()
Andreas Gampe74240812014-04-17 10:35:09 -0700800 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800801 } else {
802 VLOG(monitor) << "monitor: Inflate with hashcode " << hash_code
Andreas Gampe74240812014-04-17 10:35:09 -0700803 << " created monitor " << m << " for object " << obj;
Haifeng Li86ab7912014-05-16 10:47:59 +0800804 }
Andreas Gampe74240812014-04-17 10:35:09 -0700805 Runtime::Current()->GetMonitorList()->Add(m);
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700806 CHECK_EQ(obj->GetLockWord(true).GetState(), LockWord::kFatLocked);
Andreas Gampe74240812014-04-17 10:35:09 -0700807 } else {
808 MonitorPool::ReleaseMonitor(self, m);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700809 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700810}
811
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700812void Monitor::InflateThinLocked(Thread* self, Handle<mirror::Object> obj, LockWord lock_word,
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700813 uint32_t hash_code) {
814 DCHECK_EQ(lock_word.GetState(), LockWord::kThinLocked);
815 uint32_t owner_thread_id = lock_word.ThinLockOwner();
816 if (owner_thread_id == self->GetThreadId()) {
817 // We own the monitor, we can easily inflate it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700818 Inflate(self, self, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700819 } else {
820 ThreadList* thread_list = Runtime::Current()->GetThreadList();
821 // Suspend the owner, inflate. First change to blocked and give up mutator_lock_.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700822 self->SetMonitorEnterObject(obj.Get());
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700823 bool timed_out;
Mathieu Chartierf1d666e2015-09-03 16:13:34 -0700824 Thread* owner;
825 {
826 ScopedThreadSuspension sts(self, kBlocked);
827 owner = thread_list->SuspendThreadByThreadId(owner_thread_id, false, &timed_out);
828 }
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700829 if (owner != nullptr) {
830 // We succeeded in suspending the thread, check the lock's status didn't change.
831 lock_word = obj->GetLockWord(true);
832 if (lock_word.GetState() == LockWord::kThinLocked &&
833 lock_word.ThinLockOwner() == owner_thread_id) {
834 // Go ahead and inflate the lock.
835 Inflate(self, owner, obj.Get(), hash_code);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700836 }
Mathieu Chartiera1ee14f2014-05-14 16:51:03 -0700837 thread_list->Resume(owner, false);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700838 }
Ian Rogersdd7624d2014-03-14 17:43:00 -0700839 self->SetMonitorEnterObject(nullptr);
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700840 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700841}
842
Ian Rogers719d1a32014-03-06 12:13:39 -0800843// Fool annotalysis into thinking that the lock on obj is acquired.
844static mirror::Object* FakeLock(mirror::Object* obj)
845 EXCLUSIVE_LOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
846 return obj;
847}
848
849// Fool annotalysis into thinking that the lock on obj is release.
850static mirror::Object* FakeUnlock(mirror::Object* obj)
851 UNLOCK_FUNCTION(obj) NO_THREAD_SAFETY_ANALYSIS {
852 return obj;
853}
854
Mathieu Chartiere7e8a5f2014-02-14 16:59:41 -0800855mirror::Object* Monitor::MonitorEnter(Thread* self, mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700856 DCHECK(self != nullptr);
857 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -0700858 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -0800859 obj = FakeLock(obj);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700860 uint32_t thread_id = self->GetThreadId();
861 size_t contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700862 StackHandleScope<1> hs(self);
863 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700864 while (true) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700865 LockWord lock_word = h_obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700866 switch (lock_word.GetState()) {
867 case LockWord::kUnlocked: {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800868 LockWord thin_locked(LockWord::FromThinLockId(thread_id, 0, lock_word.ReadBarrierState()));
Ian Rogers228602f2014-07-10 02:07:54 -0700869 if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700870 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hans Boehm30359612014-05-21 17:46:23 -0700871 // CasLockWord enforces more than the acquire ordering we need here.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700872 return h_obj.Get(); // Success!
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700873 }
874 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700875 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700876 case LockWord::kThinLocked: {
877 uint32_t owner_thread_id = lock_word.ThinLockOwner();
878 if (owner_thread_id == thread_id) {
879 // We own the lock, increase the recursion count.
880 uint32_t new_count = lock_word.ThinLockCount() + 1;
881 if (LIKELY(new_count <= LockWord::kThinLockMaxCount)) {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800882 LockWord thin_locked(LockWord::FromThinLockId(thread_id, new_count,
883 lock_word.ReadBarrierState()));
884 if (!kUseReadBarrier) {
885 h_obj->SetLockWord(thin_locked, true);
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700886 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800887 return h_obj.Get(); // Success!
888 } else {
889 // Use CAS to preserve the read barrier state.
890 if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, thin_locked)) {
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700891 AtraceMonitorLock(self, h_obj.Get(), false /* is_wait */);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800892 return h_obj.Get(); // Success!
893 }
894 }
895 continue; // Go again.
Elliott Hughes5f791332011-09-15 17:45:30 -0700896 } else {
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700897 // We'd overflow the recursion count, so inflate the monitor.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700898 InflateThinLocked(self, h_obj, lock_word, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700899 }
900 } else {
901 // Contention.
902 contention_count++;
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700903 Runtime* runtime = Runtime::Current();
904 if (contention_count <= runtime->GetMaxSpinsBeforeThinkLockInflation()) {
Mathieu Chartierb363f662014-07-16 13:28:58 -0700905 // TODO: Consider switching the thread state to kBlocked when we are yielding.
Mathieu Chartier251755c2014-07-15 18:10:25 -0700906 // Use sched_yield instead of NanoSleep since NanoSleep can wait much longer than the
907 // parameter you pass in. This can cause thread suspension to take excessively long
Mathieu Chartierb363f662014-07-16 13:28:58 -0700908 // and make long pauses. See b/16307460.
Mathieu Chartier251755c2014-07-15 18:10:25 -0700909 sched_yield();
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700910 } else {
911 contention_count = 0;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700912 InflateThinLocked(self, h_obj, lock_word, 0);
Elliott Hughes5f791332011-09-15 17:45:30 -0700913 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700914 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700915 continue; // Start from the beginning.
Elliott Hughes5f791332011-09-15 17:45:30 -0700916 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700917 case LockWord::kFatLocked: {
918 Monitor* mon = lock_word.FatLockMonitor();
919 mon->Lock(self);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700920 return h_obj.Get(); // Success!
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700921 }
Ian Rogers719d1a32014-03-06 12:13:39 -0800922 case LockWord::kHashCode:
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700923 // Inflate with the existing hashcode.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700924 Inflate(self, nullptr, h_obj.Get(), lock_word.GetHashCode());
Ian Rogers719d1a32014-03-06 12:13:39 -0800925 continue; // Start from the beginning.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700926 default: {
927 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
Andreas Gampec7ed09b2016-04-25 20:08:55 -0700928 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700929 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700930 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700931 }
932}
933
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800934bool Monitor::MonitorExit(Thread* self, mirror::Object* obj) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700935 DCHECK(self != nullptr);
936 DCHECK(obj != nullptr);
Mathieu Chartier2d096c92015-10-12 16:18:20 -0700937 self->AssertThreadSuspensionIsAllowable();
Ian Rogers719d1a32014-03-06 12:13:39 -0800938 obj = FakeUnlock(obj);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700939 StackHandleScope<1> hs(self);
940 Handle<mirror::Object> h_obj(hs.NewHandle(obj));
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800941 while (true) {
942 LockWord lock_word = obj->GetLockWord(true);
943 switch (lock_word.GetState()) {
944 case LockWord::kHashCode:
945 // Fall-through.
946 case LockWord::kUnlocked:
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700947 FailedUnlock(h_obj.Get(), self->GetThreadId(), 0u, nullptr);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700948 return false; // Failure.
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800949 case LockWord::kThinLocked: {
950 uint32_t thread_id = self->GetThreadId();
951 uint32_t owner_thread_id = lock_word.ThinLockOwner();
952 if (owner_thread_id != thread_id) {
Mathieu Chartier61b3cd42016-04-18 11:43:29 -0700953 FailedUnlock(h_obj.Get(), thread_id, owner_thread_id, nullptr);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800954 return false; // Failure.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700955 } else {
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800956 // We own the lock, decrease the recursion count.
957 LockWord new_lw = LockWord::Default();
958 if (lock_word.ThinLockCount() != 0) {
959 uint32_t new_count = lock_word.ThinLockCount() - 1;
960 new_lw = LockWord::FromThinLockId(thread_id, new_count, lock_word.ReadBarrierState());
961 } else {
962 new_lw = LockWord::FromDefault(lock_word.ReadBarrierState());
963 }
964 if (!kUseReadBarrier) {
965 DCHECK_EQ(new_lw.ReadBarrierState(), 0U);
966 h_obj->SetLockWord(new_lw, true);
Andreas Gampe6e759ad2016-05-17 10:13:10 -0700967 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800968 // Success!
969 return true;
970 } else {
971 // Use CAS to preserve the read barrier state.
972 if (h_obj->CasLockWordWeakSequentiallyConsistent(lock_word, new_lw)) {
Andreas Gampe6e759ad2016-05-17 10:13:10 -0700973 AtraceMonitorUnlock();
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800974 // Success!
975 return true;
976 }
977 }
978 continue; // Go again.
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700979 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700980 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800981 case LockWord::kFatLocked: {
982 Monitor* mon = lock_word.FatLockMonitor();
983 return mon->Unlock(self);
984 }
985 default: {
986 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
987 return false;
988 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700989 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700990 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700991}
992
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800993void Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns,
Elliott Hughes4cd121e2013-01-07 17:35:41 -0800994 bool interruptShouldThrow, ThreadState why) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -0700995 DCHECK(self != nullptr);
996 DCHECK(obj != nullptr);
997 LockWord lock_word = obj->GetLockWord(true);
Ian Rogers43c69cc2014-08-15 11:09:28 -0700998 while (lock_word.GetState() != LockWord::kFatLocked) {
999 switch (lock_word.GetState()) {
1000 case LockWord::kHashCode:
1001 // Fall-through.
1002 case LockWord::kUnlocked:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001003 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1004 return; // Failure.
Ian Rogers43c69cc2014-08-15 11:09:28 -07001005 case LockWord::kThinLocked: {
1006 uint32_t thread_id = self->GetThreadId();
1007 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1008 if (owner_thread_id != thread_id) {
1009 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
1010 return; // Failure.
1011 } else {
1012 // We own the lock, inflate to enqueue ourself on the Monitor. May fail spuriously so
1013 // re-load.
1014 Inflate(self, self, obj, 0);
1015 lock_word = obj->GetLockWord(true);
1016 }
1017 break;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001018 }
Ian Rogers43c69cc2014-08-15 11:09:28 -07001019 case LockWord::kFatLocked: // Unreachable given the loop condition above. Fall-through.
1020 default: {
1021 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1022 return;
1023 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001024 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001025 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001026 Monitor* mon = lock_word.FatLockMonitor();
1027 mon->Wait(self, ms, ns, interruptShouldThrow, why);
Elliott Hughes5f791332011-09-15 17:45:30 -07001028}
1029
Ian Rogers13c479e2013-10-11 07:59:01 -07001030void Monitor::DoNotify(Thread* self, mirror::Object* obj, bool notify_all) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001031 DCHECK(self != nullptr);
1032 DCHECK(obj != nullptr);
1033 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001034 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001035 case LockWord::kHashCode:
1036 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001037 case LockWord::kUnlocked:
Ian Rogers6d0b13e2012-02-07 09:25:29 -08001038 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001039 return; // Failure.
1040 case LockWord::kThinLocked: {
1041 uint32_t thread_id = self->GetThreadId();
1042 uint32_t owner_thread_id = lock_word.ThinLockOwner();
1043 if (owner_thread_id != thread_id) {
1044 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
1045 return; // Failure.
1046 } else {
1047 // We own the lock but there's no Monitor and therefore no waiters.
1048 return; // Success.
1049 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001050 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001051 case LockWord::kFatLocked: {
1052 Monitor* mon = lock_word.FatLockMonitor();
1053 if (notify_all) {
1054 mon->NotifyAll(self);
1055 } else {
1056 mon->Notify(self);
1057 }
1058 return; // Success.
1059 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001060 default: {
1061 LOG(FATAL) << "Invalid monitor state " << lock_word.GetState();
1062 return;
1063 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001064 }
1065}
1066
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001067uint32_t Monitor::GetLockOwnerThreadId(mirror::Object* obj) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001068 DCHECK(obj != nullptr);
1069 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001070 switch (lock_word.GetState()) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001071 case LockWord::kHashCode:
1072 // Fall-through.
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001073 case LockWord::kUnlocked:
1074 return ThreadList::kInvalidThreadId;
1075 case LockWord::kThinLocked:
1076 return lock_word.ThinLockOwner();
1077 case LockWord::kFatLocked: {
1078 Monitor* mon = lock_word.FatLockMonitor();
1079 return mon->GetOwnerThreadId();
Elliott Hughes5f791332011-09-15 17:45:30 -07001080 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07001081 default: {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001082 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001083 UNREACHABLE();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001084 }
Elliott Hughes5f791332011-09-15 17:45:30 -07001085 }
1086}
1087
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001088void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001089 // Determine the wait message and object we're waiting or blocked upon.
1090 mirror::Object* pretty_object = nullptr;
1091 const char* wait_message = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001092 uint32_t lock_owner = ThreadList::kInvalidThreadId;
Ian Rogersd803bc72014-04-01 15:33:03 -07001093 ThreadState state = thread->GetState();
Elliott Hughesb4e94fd2013-01-08 14:41:26 -08001094 if (state == kWaiting || state == kTimedWaiting || state == kSleeping) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001095 wait_message = (state == kSleeping) ? " - sleeping on " : " - waiting on ";
1096 Thread* self = Thread::Current();
1097 MutexLock mu(self, *thread->GetWaitMutex());
1098 Monitor* monitor = thread->GetWaitMonitor();
1099 if (monitor != nullptr) {
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001100 pretty_object = monitor->GetObject();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001101 }
Elliott Hughes34e06962012-04-09 13:55:55 -07001102 } else if (state == kBlocked) {
Ian Rogersd803bc72014-04-01 15:33:03 -07001103 wait_message = " - waiting to lock ";
1104 pretty_object = thread->GetMonitorEnterObject();
1105 if (pretty_object != nullptr) {
1106 lock_owner = pretty_object->GetLockOwnerThreadId();
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001107 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001108 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001109
Ian Rogersd803bc72014-04-01 15:33:03 -07001110 if (wait_message != nullptr) {
1111 if (pretty_object == nullptr) {
1112 os << wait_message << "an unknown object";
1113 } else {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001114 if ((pretty_object->GetLockWord(true).GetState() == LockWord::kThinLocked) &&
Ian Rogersd803bc72014-04-01 15:33:03 -07001115 Locks::mutator_lock_->IsExclusiveHeld(Thread::Current())) {
1116 // Getting the identity hashcode here would result in lock inflation and suspension of the
1117 // current thread, which isn't safe if this is the only runnable thread.
1118 os << wait_message << StringPrintf("<@addr=0x%" PRIxPTR "> (a %s)",
1119 reinterpret_cast<intptr_t>(pretty_object),
1120 PrettyTypeOf(pretty_object).c_str());
1121 } else {
1122 // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
Mathieu Chartier49361592015-01-22 16:36:10 -08001123 // Call PrettyTypeOf before IdentityHashCode since IdentityHashCode can cause thread
1124 // suspension and move pretty_object.
1125 const std::string pretty_type(PrettyTypeOf(pretty_object));
Ian Rogersd803bc72014-04-01 15:33:03 -07001126 os << wait_message << StringPrintf("<0x%08x> (a %s)", pretty_object->IdentityHashCode(),
Mathieu Chartier49361592015-01-22 16:36:10 -08001127 pretty_type.c_str());
Ian Rogersd803bc72014-04-01 15:33:03 -07001128 }
1129 }
1130 // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5
1131 if (lock_owner != ThreadList::kInvalidThreadId) {
1132 os << " held by thread " << lock_owner;
1133 }
1134 os << "\n";
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001135 }
Elliott Hughes8e4aac52011-09-26 17:03:36 -07001136}
1137
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001138mirror::Object* Monitor::GetContendedMonitor(Thread* thread) {
Elliott Hughesf9501702013-01-11 11:22:27 -08001139 // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre
1140 // definition of contended that includes a monitor a thread is trying to enter...
Ian Rogersdd7624d2014-03-14 17:43:00 -07001141 mirror::Object* result = thread->GetMonitorEnterObject();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001142 if (result == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001143 // ...but also a monitor that the thread is waiting on.
Ian Rogersdd7624d2014-03-14 17:43:00 -07001144 MutexLock mu(Thread::Current(), *thread->GetWaitMutex());
1145 Monitor* monitor = thread->GetWaitMonitor();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001146 if (monitor != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001147 result = monitor->GetObject();
Elliott Hughesf9501702013-01-11 11:22:27 -08001148 }
1149 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001150 return result;
Elliott Hughesf9501702013-01-11 11:22:27 -08001151}
1152
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001153void Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*),
Andreas Gampe760172c2014-08-16 13:41:10 -07001154 void* callback_context, bool abort_on_failure) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001155 ArtMethod* m = stack_visitor->GetMethod();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001156 CHECK(m != nullptr);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001157
1158 // Native methods are an easy special case.
1159 // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too.
1160 if (m->IsNative()) {
1161 if (m->IsSynchronized()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001162 mirror::Object* jni_this =
1163 stack_visitor->GetCurrentHandleScope(sizeof(void*))->GetReference(0);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001164 callback(jni_this, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001165 }
1166 return;
1167 }
1168
jeffhao61f916c2012-10-25 17:48:51 -07001169 // Proxy methods should not be synchronized.
1170 if (m->IsProxyMethod()) {
1171 CHECK(!m->IsSynchronized());
1172 return;
1173 }
1174
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001175 // Is there any reason to believe there's any synchronization in this method?
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001176 const DexFile::CodeItem* code_item = m->GetCodeItem();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001177 CHECK(code_item != nullptr) << PrettyMethod(m);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001178 if (code_item->tries_size_ == 0) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001179 return; // No "tries" implies no synchronization, so no held locks to report.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001180 }
1181
Andreas Gampe760172c2014-08-16 13:41:10 -07001182 // Get the dex pc. If abort_on_failure is false, GetDexPc will not abort in the case it cannot
1183 // find the dex pc, and instead return kDexNoIndex. Then bail out, as it indicates we have an
1184 // inconsistent stack anyways.
1185 uint32_t dex_pc = stack_visitor->GetDexPc(abort_on_failure);
1186 if (!abort_on_failure && dex_pc == DexFile::kDexNoIndex) {
1187 LOG(ERROR) << "Could not find dex_pc for " << PrettyMethod(m);
1188 return;
1189 }
1190
Elliott Hughes80537bb2013-01-04 16:37:26 -08001191 // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to
1192 // the locks held in this stack frame.
1193 std::vector<uint32_t> monitor_enter_dex_pcs;
Andreas Gampe760172c2014-08-16 13:41:10 -07001194 verifier::MethodVerifier::FindLocksAtDexPc(m, dex_pc, &monitor_enter_dex_pcs);
Mathieu Chartiere6a8eec2015-01-06 14:17:57 -08001195 for (uint32_t monitor_dex_pc : monitor_enter_dex_pcs) {
Elliott Hughes80537bb2013-01-04 16:37:26 -08001196 // The verifier works in terms of the dex pcs of the monitor-enter instructions.
1197 // We want the registers used by those instructions (so we can read the values out of them).
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001198 const Instruction* monitor_enter_instruction =
1199 Instruction::At(&code_item->insns_[monitor_dex_pc]);
Elliott Hughes80537bb2013-01-04 16:37:26 -08001200
1201 // Quick sanity check.
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001202 CHECK_EQ(monitor_enter_instruction->Opcode(), Instruction::MONITOR_ENTER)
1203 << "expected monitor-enter @" << monitor_dex_pc << "; was "
1204 << reinterpret_cast<const void*>(monitor_enter_instruction);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001205
Sebastien Hertz0f7c9332015-11-05 15:57:30 +01001206 uint16_t monitor_register = monitor_enter_instruction->VRegA();
Nicolas Geoffray15b9d522015-03-12 15:05:13 +00001207 uint32_t value;
1208 bool success = stack_visitor->GetVReg(m, monitor_register, kReferenceVReg, &value);
1209 CHECK(success) << "Failed to read v" << monitor_register << " of kind "
1210 << kReferenceVReg << " in method " << PrettyMethod(m);
1211 mirror::Object* o = reinterpret_cast<mirror::Object*>(value);
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001212 callback(o, callback_context);
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001213 }
1214}
1215
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001216bool Monitor::IsValidLockWord(LockWord lock_word) {
1217 switch (lock_word.GetState()) {
1218 case LockWord::kUnlocked:
1219 // Nothing to check.
1220 return true;
1221 case LockWord::kThinLocked:
1222 // Basic sanity check of owner.
1223 return lock_word.ThinLockOwner() != ThreadList::kInvalidThreadId;
1224 case LockWord::kFatLocked: {
1225 // Check the monitor appears in the monitor list.
1226 Monitor* mon = lock_word.FatLockMonitor();
1227 MonitorList* list = Runtime::Current()->GetMonitorList();
1228 MutexLock mu(Thread::Current(), list->monitor_list_lock_);
1229 for (Monitor* list_mon : list->list_) {
1230 if (mon == list_mon) {
1231 return true; // Found our monitor.
1232 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001233 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001234 return false; // Fail - unowned monitor in an object.
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001235 }
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001236 case LockWord::kHashCode:
1237 return true;
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001238 default:
1239 LOG(FATAL) << "Unreachable";
Ian Rogers2c4257b2014-10-24 14:20:06 -07001240 UNREACHABLE();
Ian Rogers7dfb28c2013-08-22 08:18:36 -07001241 }
1242}
1243
Mathieu Chartier90443472015-07-16 20:32:27 -07001244bool Monitor::IsLocked() SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001245 MutexLock mu(Thread::Current(), monitor_lock_);
1246 return owner_ != nullptr;
1247}
1248
Mathieu Chartier74b3c8f2016-04-15 19:11:45 -07001249void Monitor::TranslateLocation(ArtMethod* method,
1250 uint32_t dex_pc,
1251 const char** source_file,
1252 int32_t* line_number) {
jeffhao33dc7712011-11-09 17:54:24 -08001253 // If method is null, location is unknown
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001254 if (method == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001255 *source_file = "";
1256 *line_number = 0;
jeffhao33dc7712011-11-09 17:54:24 -08001257 return;
1258 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001259 *source_file = method->GetDeclaringClassSourceFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001260 if (*source_file == nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001261 *source_file = "";
Elliott Hughes12c51e32012-01-17 20:25:05 -08001262 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07001263 *line_number = method->GetLineNumFromDexPC(dex_pc);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001264}
1265
1266uint32_t Monitor::GetOwnerThreadId() {
1267 MutexLock mu(Thread::Current(), monitor_lock_);
1268 Thread* owner = owner_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001269 if (owner != nullptr) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001270 return owner->GetThreadId();
1271 } else {
1272 return ThreadList::kInvalidThreadId;
1273 }
jeffhao33dc7712011-11-09 17:54:24 -08001274}
1275
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001276MonitorList::MonitorList()
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001277 : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock", kMonitorListLock),
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001278 monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001279}
1280
1281MonitorList::~MonitorList() {
Andreas Gampe74240812014-04-17 10:35:09 -07001282 Thread* self = Thread::Current();
1283 MutexLock mu(self, monitor_list_lock_);
1284 // Release all monitors to the pool.
1285 // TODO: Is it an invariant that *all* open monitors are in the list? Then we could
1286 // clear faster in the pool.
1287 MonitorPool::ReleaseMonitors(self, &list_);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001288}
1289
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001290void MonitorList::DisallowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001291 CHECK(!kUseReadBarrier);
Ian Rogers50b35e22012-10-04 10:09:15 -07001292 MutexLock mu(Thread::Current(), monitor_list_lock_);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001293 allow_new_monitors_ = false;
1294}
1295
1296void MonitorList::AllowNewMonitors() {
Hiroshi Yamauchifdbd13c2015-09-02 16:16:58 -07001297 CHECK(!kUseReadBarrier);
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001298 Thread* self = Thread::Current();
1299 MutexLock mu(self, monitor_list_lock_);
1300 allow_new_monitors_ = true;
1301 monitor_add_condition_.Broadcast(self);
1302}
1303
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001304void MonitorList::BroadcastForNewMonitors() {
1305 CHECK(kUseReadBarrier);
1306 Thread* self = Thread::Current();
1307 MutexLock mu(self, monitor_list_lock_);
1308 monitor_add_condition_.Broadcast(self);
1309}
1310
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001311void MonitorList::Add(Monitor* m) {
1312 Thread* self = Thread::Current();
1313 MutexLock mu(self, monitor_list_lock_);
Hiroshi Yamauchi0b713572015-06-16 18:29:23 -07001314 while (UNLIKELY((!kUseReadBarrier && !allow_new_monitors_) ||
1315 (kUseReadBarrier && !self->GetWeakRefAccessEnabled()))) {
Mathieu Chartierc11d9b82013-09-19 10:01:59 -07001316 monitor_add_condition_.WaitHoldingLocks(self);
1317 }
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001318 list_.push_front(m);
1319}
1320
Mathieu Chartier97509952015-07-13 14:35:43 -07001321void MonitorList::SweepMonitorList(IsMarkedVisitor* visitor) {
Andreas Gampe74240812014-04-17 10:35:09 -07001322 Thread* self = Thread::Current();
1323 MutexLock mu(self, monitor_list_lock_);
Mathieu Chartier02e25112013-08-14 16:14:24 -07001324 for (auto it = list_.begin(); it != list_.end(); ) {
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001325 Monitor* m = *it;
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001326 // Disable the read barrier in GetObject() as this is called by GC.
1327 mirror::Object* obj = m->GetObject<kWithoutReadBarrier>();
Mathieu Chartier590fee92013-09-13 13:46:47 -07001328 // The object of a monitor can be null if we have deflated it.
Mathieu Chartier97509952015-07-13 14:35:43 -07001329 mirror::Object* new_obj = obj != nullptr ? visitor->IsMarked(obj) : nullptr;
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001330 if (new_obj == nullptr) {
1331 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object "
Hiroshi Yamauchi4cba0d92014-05-21 21:10:23 -07001332 << obj;
Andreas Gampe74240812014-04-17 10:35:09 -07001333 MonitorPool::ReleaseMonitor(self, m);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001334 it = list_.erase(it);
1335 } else {
Mathieu Chartier6aa3df92013-09-17 15:17:28 -07001336 m->SetObject(new_obj);
Elliott Hughesc33a32b2011-10-11 18:18:07 -07001337 ++it;
1338 }
1339 }
1340}
1341
Mathieu Chartier97509952015-07-13 14:35:43 -07001342class MonitorDeflateVisitor : public IsMarkedVisitor {
1343 public:
1344 MonitorDeflateVisitor() : self_(Thread::Current()), deflate_count_(0) {}
1345
1346 virtual mirror::Object* IsMarked(mirror::Object* object) OVERRIDE
Mathieu Chartier90443472015-07-16 20:32:27 -07001347 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier97509952015-07-13 14:35:43 -07001348 if (Monitor::Deflate(self_, object)) {
1349 DCHECK_NE(object->GetLockWord(true).GetState(), LockWord::kFatLocked);
1350 ++deflate_count_;
1351 // If we deflated, return null so that the monitor gets removed from the array.
1352 return nullptr;
1353 }
1354 return object; // Monitor was not deflated.
1355 }
1356
1357 Thread* const self_;
1358 size_t deflate_count_;
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001359};
1360
Mathieu Chartier48ab6872014-06-24 11:21:59 -07001361size_t MonitorList::DeflateMonitors() {
Mathieu Chartier97509952015-07-13 14:35:43 -07001362 MonitorDeflateVisitor visitor;
1363 Locks::mutator_lock_->AssertExclusiveHeld(visitor.self_);
1364 SweepMonitorList(&visitor);
1365 return visitor.deflate_count_;
Mathieu Chartier440e4ce2014-03-31 16:36:35 -07001366}
1367
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001368MonitorInfo::MonitorInfo(mirror::Object* obj) : owner_(nullptr), entry_count_(0) {
Mathieu Chartier4d7f61d2014-04-17 14:43:39 -07001369 DCHECK(obj != nullptr);
1370 LockWord lock_word = obj->GetLockWord(true);
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001371 switch (lock_word.GetState()) {
1372 case LockWord::kUnlocked:
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001373 // Fall-through.
Mathieu Chartier590fee92013-09-13 13:46:47 -07001374 case LockWord::kForwardingAddress:
1375 // Fall-through.
Mathieu Chartierad2541a2013-10-25 10:05:23 -07001376 case LockWord::kHashCode:
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001377 break;
1378 case LockWord::kThinLocked:
1379 owner_ = Runtime::Current()->GetThreadList()->FindThreadByThreadId(lock_word.ThinLockOwner());
1380 entry_count_ = 1 + lock_word.ThinLockCount();
1381 // Thin locks have no waiters.
1382 break;
1383 case LockWord::kFatLocked: {
1384 Monitor* mon = lock_word.FatLockMonitor();
1385 owner_ = mon->owner_;
1386 entry_count_ = 1 + mon->lock_count_;
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001387 for (Thread* waiter = mon->wait_set_; waiter != nullptr; waiter = waiter->GetWaitNext()) {
Ian Rogersd9c4fc92013-10-01 19:45:43 -07001388 waiters_.push_back(waiter);
1389 }
1390 break;
Elliott Hughesf327e072013-01-09 16:01:26 -08001391 }
1392 }
1393}
1394
Elliott Hughes5f791332011-09-15 17:45:30 -07001395} // namespace art