blob: 50deba6665e408700d9845b4d2412d1ba4520266 [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
19#include <errno.h>
20#include <fcntl.h>
21#include <pthread.h>
22#include <stdlib.h>
23#include <sys/time.h>
24#include <time.h>
25#include <unistd.h>
26
jeffhao33dc7712011-11-09 17:54:24 -080027#include "class_linker.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070028#include "mutex.h"
29#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.h"
Elliott Hughes88c5c352012-03-15 18:49:48 -070031#include "scoped_thread_list_lock.h"
Elliott Hughesc33a32b2011-10-11 18:18:07 -070032#include "stl_util.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070033#include "thread.h"
Elliott Hughes8e4aac52011-09-26 17:03:36 -070034#include "thread_list.h"
Elliott Hughes5f791332011-09-15 17:45:30 -070035
36namespace art {
37
38/*
39 * Every Object has a monitor associated with it, but not every Object is
40 * actually locked. Even the ones that are locked do not need a
41 * full-fledged monitor until a) there is actual contention or b) wait()
42 * is called on the Object.
43 *
44 * For Android, we have implemented a scheme similar to the one described
45 * in Bacon et al.'s "Thin locks: featherweight synchronization for Java"
46 * (ACM 1998). Things are even easier for us, though, because we have
47 * a full 32 bits to work with.
48 *
49 * The two states of an Object's lock are referred to as "thin" and
50 * "fat". A lock may transition from the "thin" state to the "fat"
51 * state and this transition is referred to as inflation. Once a lock
52 * has been inflated it remains in the "fat" state indefinitely.
53 *
54 * The lock value itself is stored in Object.lock. The LSB of the
55 * lock encodes its state. When cleared, the lock is in the "thin"
56 * state and its bits are formatted as follows:
57 *
58 * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0]
59 * lock count thread id hash state 0
60 *
61 * When set, the lock is in the "fat" state and its bits are formatted
62 * as follows:
63 *
64 * [31 ---- 3] [2 ---- 1] [0]
65 * pointer hash state 1
66 *
67 * For an in-depth description of the mechanics of thin-vs-fat locking,
68 * read the paper referred to above.
Elliott Hughes54e7df12011-09-16 11:47:04 -070069 *
Elliott Hughes5f791332011-09-15 17:45:30 -070070 * Monitors provide:
71 * - mutually exclusive access to resources
72 * - a way for multiple threads to wait for notification
73 *
74 * In effect, they fill the role of both mutexes and condition variables.
75 *
76 * Only one thread can own the monitor at any time. There may be several
77 * threads waiting on it (the wait call unlocks it). One or more waiting
78 * threads may be getting interrupted or notified at any given time.
79 *
80 * TODO: the various members of monitor are not SMP-safe.
81 */
Elliott Hughes54e7df12011-09-16 11:47:04 -070082
83
84/*
85 * Monitor accessor. Extracts a monitor structure pointer from a fat
86 * lock. Performs no error checking.
87 */
88#define LW_MONITOR(x) \
Elliott Hughes398f64b2012-03-26 18:05:48 -070089 (reinterpret_cast<Monitor*>((x) & ~((LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT) | LW_SHAPE_MASK)))
Elliott Hughes54e7df12011-09-16 11:47:04 -070090
91/*
92 * Lock recursion count field. Contains a count of the number of times
93 * a lock has been recursively acquired.
94 */
95#define LW_LOCK_COUNT_MASK 0x1fff
96#define LW_LOCK_COUNT_SHIFT 19
97#define LW_LOCK_COUNT(x) (((x) >> LW_LOCK_COUNT_SHIFT) & LW_LOCK_COUNT_MASK)
98
Elliott Hughesfc861622011-10-17 17:57:47 -070099bool (*Monitor::is_sensitive_thread_hook_)() = NULL;
Elliott Hughesfc861622011-10-17 17:57:47 -0700100uint32_t Monitor::lock_profiling_threshold_ = 0;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700101
Elliott Hughesfc861622011-10-17 17:57:47 -0700102bool Monitor::IsSensitiveThread() {
103 if (is_sensitive_thread_hook_ != NULL) {
104 return (*is_sensitive_thread_hook_)();
105 }
106 return false;
107}
108
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800109void Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) {
Elliott Hughesfc861622011-10-17 17:57:47 -0700110 lock_profiling_threshold_ = lock_profiling_threshold;
111 is_sensitive_thread_hook_ = is_sensitive_thread_hook;
Elliott Hughes32d6e1e2011-10-11 14:47:44 -0700112}
113
Elliott Hughes5f791332011-09-15 17:45:30 -0700114Monitor::Monitor(Object* obj)
115 : owner_(NULL),
116 lock_count_(0),
117 obj_(obj),
118 wait_set_(NULL),
119 lock_("a monitor lock"),
jeffhao33dc7712011-11-09 17:54:24 -0800120 locking_method_(NULL),
Ian Rogers0399dde2012-06-06 17:09:28 -0700121 locking_dex_pc_(0) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700122}
123
124Monitor::~Monitor() {
125 DCHECK(obj_ != NULL);
126 DCHECK_EQ(LW_SHAPE(*obj_->GetRawLockWordAddress()), LW_SHAPE_FAT);
Elliott Hughes5f791332011-09-15 17:45:30 -0700127}
128
129/*
130 * Links a thread into a monitor's wait set. The monitor lock must be
131 * held by the caller of this routine.
132 */
133void Monitor::AppendToWaitSet(Thread* thread) {
134 DCHECK(owner_ == Thread::Current());
135 DCHECK(thread != NULL);
Elliott Hughesdc33ad52011-09-16 19:46:51 -0700136 DCHECK(thread->wait_next_ == NULL) << thread->wait_next_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700137 if (wait_set_ == NULL) {
138 wait_set_ = thread;
139 return;
140 }
141
142 // push_back.
143 Thread* t = wait_set_;
144 while (t->wait_next_ != NULL) {
145 t = t->wait_next_;
146 }
147 t->wait_next_ = thread;
148}
149
150/*
151 * Unlinks a thread from a monitor's wait set. The monitor lock must
152 * be held by the caller of this routine.
153 */
154void Monitor::RemoveFromWaitSet(Thread *thread) {
155 DCHECK(owner_ == Thread::Current());
156 DCHECK(thread != NULL);
157 if (wait_set_ == NULL) {
158 return;
159 }
160 if (wait_set_ == thread) {
161 wait_set_ = thread->wait_next_;
162 thread->wait_next_ = NULL;
163 return;
164 }
165
166 Thread* t = wait_set_;
167 while (t->wait_next_ != NULL) {
168 if (t->wait_next_ == thread) {
169 t->wait_next_ = thread->wait_next_;
170 thread->wait_next_ = NULL;
171 return;
172 }
173 t = t->wait_next_;
174 }
175}
176
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700177Object* Monitor::GetObject() {
178 return obj_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700179}
180
Elliott Hughes5f791332011-09-15 17:45:30 -0700181void Monitor::Lock(Thread* self) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700182 if (owner_ == self) {
183 lock_count_++;
184 return;
185 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700186
Elliott Hughes5f791332011-09-15 17:45:30 -0700187 if (!lock_.TryLock()) {
Mathieu Chartier2542d662012-06-21 17:14:11 -0700188 uint64_t waitStart = 0;
189 uint64_t waitEnd = 0;
Elliott Hughesfc861622011-10-17 17:57:47 -0700190 uint32_t wait_threshold = lock_profiling_threshold_;
jeffhao33dc7712011-11-09 17:54:24 -0800191 const Method* current_locking_method = NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -0700192 uint32_t current_locking_dex_pc = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700193 {
Elliott Hughes34e06962012-04-09 13:55:55 -0700194 ScopedThreadStateChange tsc(self, kBlocked);
Elliott Hughesfc861622011-10-17 17:57:47 -0700195 if (wait_threshold != 0) {
196 waitStart = NanoTime() / 1000;
197 }
jeffhao33dc7712011-11-09 17:54:24 -0800198 current_locking_method = locking_method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700199 current_locking_dex_pc = locking_dex_pc_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700200
201 lock_.Lock();
Elliott Hughesfc861622011-10-17 17:57:47 -0700202 if (wait_threshold != 0) {
203 waitEnd = NanoTime() / 1000;
204 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700205 }
Elliott Hughesfc861622011-10-17 17:57:47 -0700206
207 if (wait_threshold != 0) {
208 uint64_t wait_ms = (waitEnd - waitStart) / 1000;
209 uint32_t sample_percent;
210 if (wait_ms >= wait_threshold) {
211 sample_percent = 100;
212 } else {
213 sample_percent = 100 * wait_ms / wait_threshold;
214 }
215 if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) {
jeffhao33dc7712011-11-09 17:54:24 -0800216 const char* current_locking_filename;
217 uint32_t current_locking_line_number;
Ian Rogers0399dde2012-06-06 17:09:28 -0700218 TranslateLocation(current_locking_method, current_locking_dex_pc,
jeffhao33dc7712011-11-09 17:54:24 -0800219 current_locking_filename, current_locking_line_number);
220 LogContentionEvent(self, wait_ms, sample_percent, current_locking_filename, current_locking_line_number);
Elliott Hughesfc861622011-10-17 17:57:47 -0700221 }
222 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700223 }
224 owner_ = self;
225 DCHECK_EQ(lock_count_, 0);
226
227 // When debugging, save the current monitor holder for future
228 // acquisition failures to use in sampled logging.
Elliott Hughesfc861622011-10-17 17:57:47 -0700229 if (lock_profiling_threshold_ != 0) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700230 locking_method_ = self->GetCurrentMethod(&locking_dex_pc_);
Elliott Hughesfc861622011-10-17 17:57:47 -0700231 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700232}
233
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800234static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...)
235 __attribute__((format(printf, 1, 2)));
236
237static void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) {
238 va_list args;
239 va_start(args, fmt);
240 Thread::Current()->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args);
Brian Carlstrom64277f32012-03-26 23:53:34 -0700241 if (!Runtime::Current()->IsStarted()) {
242 std::ostringstream ss;
243 Thread::Current()->Dump(ss);
244 std::string str(ss.str());
245 LOG(ERROR) << "IllegalMonitorStateException: " << str;
246 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800247 va_end(args);
248}
249
Elliott Hughesd4237412012-02-21 11:24:45 -0800250static std::string ThreadToString(Thread* thread) {
251 if (thread == NULL) {
252 return "NULL";
253 }
254 std::ostringstream oss;
255 // TODO: alternatively, we could just return the thread's name.
256 oss << *thread;
257 return oss.str();
258}
259
Elliott Hughesffb465f2012-03-01 18:46:05 -0800260void Monitor::FailedUnlock(Object* o, Thread* expected_owner, Thread* found_owner,
261 Monitor* monitor) {
262 Thread* current_owner = NULL;
263 std::string current_owner_string;
264 std::string expected_owner_string;
265 std::string found_owner_string;
266 {
267 // TODO: isn't this too late to prevent threads from disappearing?
268 // Acquire thread list lock so threads won't disappear from under us.
269 ScopedThreadListLock thread_list_lock;
270 // Re-read owner now that we hold lock.
271 current_owner = (monitor != NULL) ? monitor->owner_ : NULL;
272 // Get short descriptions of the threads involved.
273 current_owner_string = ThreadToString(current_owner);
274 expected_owner_string = ThreadToString(expected_owner);
275 found_owner_string = ThreadToString(found_owner);
276 }
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800277 if (current_owner == NULL) {
278 if (found_owner == NULL) {
279 ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'"
280 " on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800281 PrettyTypeOf(o).c_str(),
282 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800283 } else {
284 // Race: the original read found an owner but now there is none
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800285 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
286 " (where now the monitor appears unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800287 found_owner_string.c_str(),
288 PrettyTypeOf(o).c_str(),
289 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800290 }
291 } else {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800292 if (found_owner == NULL) {
293 // Race: originally there was no owner, there is now
294 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
295 " (originally believed to be unowned) on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800296 current_owner_string.c_str(),
297 PrettyTypeOf(o).c_str(),
298 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800299 } else {
300 if (found_owner != current_owner) {
301 // Race: originally found and current owner have changed
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800302 ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now"
303 " owned by '%s') on object of type '%s' on thread '%s'",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800304 found_owner_string.c_str(),
305 current_owner_string.c_str(),
306 PrettyTypeOf(o).c_str(),
307 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800308 } else {
309 ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'"
310 " on thread '%s",
Elliott Hughesffb465f2012-03-01 18:46:05 -0800311 current_owner_string.c_str(),
312 PrettyTypeOf(o).c_str(),
313 expected_owner_string.c_str());
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800314 }
315 }
316 }
Elliott Hughes5f791332011-09-15 17:45:30 -0700317}
318
319bool Monitor::Unlock(Thread* self) {
320 DCHECK(self != NULL);
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800321 Thread* owner = owner_;
322 if (owner == self) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700323 // We own the monitor, so nobody else can be in here.
324 if (lock_count_ == 0) {
325 owner_ = NULL;
jeffhao33dc7712011-11-09 17:54:24 -0800326 locking_method_ = NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -0700327 locking_dex_pc_ = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700328 lock_.Unlock();
329 } else {
330 --lock_count_;
331 }
332 } else {
333 // We don't own this, so we're not allowed to unlock it.
334 // The JNI spec says that we should throw IllegalMonitorStateException
335 // in this case.
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800336 FailedUnlock(obj_, self, owner, this);
Elliott Hughes5f791332011-09-15 17:45:30 -0700337 return false;
338 }
339 return true;
340}
341
Elliott Hughes8f751ab2012-05-15 17:12:40 -0700342// Converts the given waiting time (relative to "now") into an absolute time in 'ts'.
343static void ToAbsoluteTime(int64_t ms, int32_t ns, timespec* ts) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700344 int64_t endSec;
345
346#ifdef HAVE_TIMEDWAIT_MONOTONIC
347 clock_gettime(CLOCK_MONOTONIC, ts);
348#else
349 {
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700350 timeval tv;
Elliott Hughes5f791332011-09-15 17:45:30 -0700351 gettimeofday(&tv, NULL);
352 ts->tv_sec = tv.tv_sec;
353 ts->tv_nsec = tv.tv_usec * 1000;
354 }
355#endif
356 endSec = ts->tv_sec + ms / 1000;
357 if (endSec >= 0x7fffffff) {
Elliott Hughes8f751ab2012-05-15 17:12:40 -0700358 std::ostringstream ss;
359 Thread::Current()->Dump(ss);
360 LOG(INFO) << "Note: end time exceeds epoch: " << ss.str();
Elliott Hughes5f791332011-09-15 17:45:30 -0700361 endSec = 0x7ffffffe;
362 }
363 ts->tv_sec = endSec;
364 ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns;
365
366 // Catch rollover.
367 if (ts->tv_nsec >= 1000000000L) {
368 ts->tv_sec++;
369 ts->tv_nsec -= 1000000000L;
370 }
371}
372
Elliott Hughes5f791332011-09-15 17:45:30 -0700373/*
374 * Wait on a monitor until timeout, interrupt, or notification. Used for
375 * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join().
376 *
377 * If another thread calls Thread.interrupt(), we throw InterruptedException
378 * and return immediately if one of the following are true:
379 * - blocked in wait(), wait(long), or wait(long, int) methods of Object
380 * - blocked in join(), join(long), or join(long, int) methods of Thread
381 * - blocked in sleep(long), or sleep(long, int) methods of Thread
382 * Otherwise, we set the "interrupted" flag.
383 *
384 * Checks to make sure that "ns" is in the range 0-999999
385 * (i.e. fractions of a millisecond) and throws the appropriate
386 * exception if it isn't.
387 *
388 * The spec allows "spurious wakeups", and recommends that all code using
389 * Object.wait() do so in a loop. This appears to derive from concerns
390 * about pthread_cond_wait() on multiprocessor systems. Some commentary
391 * on the web casts doubt on whether these can/should occur.
392 *
393 * Since we're allowed to wake up "early", we clamp extremely long durations
394 * to return at the end of the 32-bit time epoch.
395 */
396void Monitor::Wait(Thread* self, int64_t ms, int32_t ns, bool interruptShouldThrow) {
397 DCHECK(self != NULL);
398
399 // Make sure that we hold the lock.
400 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800401 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700402 return;
403 }
404
405 // Enforce the timeout range.
406 if (ms < 0 || ns < 0 || ns > 999999) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700407 Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;",
Elliott Hughes5f791332011-09-15 17:45:30 -0700408 "timeout arguments out of range: ms=%lld ns=%d", ms, ns);
409 return;
410 }
411
412 // Compute absolute wakeup time, if necessary.
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700413 timespec ts;
Elliott Hughes5f791332011-09-15 17:45:30 -0700414 bool timed = false;
415 if (ms != 0 || ns != 0) {
416 ToAbsoluteTime(ms, ns, &ts);
417 timed = true;
418 }
419
420 /*
421 * Add ourselves to the set of threads waiting on this monitor, and
422 * release our hold. We need to let it go even if we're a few levels
423 * deep in a recursive lock, and we need to restore that later.
424 *
425 * We append to the wait set ahead of clearing the count and owner
426 * fields so the subroutine can check that the calling thread owns
427 * the monitor. Aside from that, the order of member updates is
428 * not order sensitive as we hold the pthread mutex.
429 */
430 AppendToWaitSet(self);
Ian Rogers0399dde2012-06-06 17:09:28 -0700431 int prev_lock_count = lock_count_;
Elliott Hughes5f791332011-09-15 17:45:30 -0700432 lock_count_ = 0;
433 owner_ = NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -0700434 const Method* saved_method = locking_method_;
jeffhao33dc7712011-11-09 17:54:24 -0800435 locking_method_ = NULL;
Ian Rogers0399dde2012-06-06 17:09:28 -0700436 uintptr_t saved_dex_pc = locking_dex_pc_;
437 locking_dex_pc_ = 0;
Elliott Hughes5f791332011-09-15 17:45:30 -0700438
439 /*
440 * Update thread status. If the GC wakes up, it'll ignore us, knowing
441 * that we won't touch any references in this state, and we'll check
442 * our suspend mode before we transition out.
443 */
444 if (timed) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700445 self->SetState(kTimedWaiting);
Elliott Hughes5f791332011-09-15 17:45:30 -0700446 } else {
Elliott Hughes34e06962012-04-09 13:55:55 -0700447 self->SetState(kWaiting);
Elliott Hughes5f791332011-09-15 17:45:30 -0700448 }
449
Elliott Hughes85d15452011-09-16 17:33:01 -0700450 self->wait_mutex_->Lock();
Elliott Hughes5f791332011-09-15 17:45:30 -0700451
452 /*
453 * Set wait_monitor_ to the monitor object we will be waiting on.
454 * When wait_monitor_ is non-NULL a notifying or interrupting thread
455 * must signal the thread's wait_cond_ to wake it up.
456 */
457 DCHECK(self->wait_monitor_ == NULL);
458 self->wait_monitor_ = this;
459
460 /*
461 * Handle the case where the thread was interrupted before we called
462 * wait().
463 */
464 bool wasInterrupted = false;
465 if (self->interrupted_) {
466 wasInterrupted = true;
467 self->wait_monitor_ = NULL;
Elliott Hughes85d15452011-09-16 17:33:01 -0700468 self->wait_mutex_->Unlock();
Elliott Hughes5f791332011-09-15 17:45:30 -0700469 goto done;
470 }
471
472 /*
473 * Release the monitor lock and wait for a notification or
474 * a timeout to occur.
475 */
476 lock_.Unlock();
477
478 if (!timed) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700479 self->wait_cond_->Wait(*self->wait_mutex_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700480 } else {
Elliott Hughes85d15452011-09-16 17:33:01 -0700481 self->wait_cond_->TimedWait(*self->wait_mutex_, ts);
Elliott Hughes5f791332011-09-15 17:45:30 -0700482 }
483 if (self->interrupted_) {
484 wasInterrupted = true;
485 }
486
487 self->interrupted_ = false;
488 self->wait_monitor_ = NULL;
Elliott Hughes85d15452011-09-16 17:33:01 -0700489 self->wait_mutex_->Unlock();
Elliott Hughes5f791332011-09-15 17:45:30 -0700490
491 // Reacquire the monitor lock.
492 Lock(self);
493
Elliott Hughesa21039c2012-06-21 12:09:25 -0700494 done:
Elliott Hughes5f791332011-09-15 17:45:30 -0700495 /*
496 * We remove our thread from wait set after restoring the count
497 * and owner fields so the subroutine can check that the calling
498 * thread owns the monitor. Aside from that, the order of member
499 * updates is not order sensitive as we hold the pthread mutex.
500 */
501 owner_ = self;
Ian Rogers0399dde2012-06-06 17:09:28 -0700502 lock_count_ = prev_lock_count;
503 locking_method_ = saved_method;
504 locking_dex_pc_ = saved_dex_pc;
Elliott Hughes5f791332011-09-15 17:45:30 -0700505 RemoveFromWaitSet(self);
506
Elliott Hughes34e06962012-04-09 13:55:55 -0700507 /* set self->status back to kRunnable, and self-suspend if needed */
508 self->SetState(kRunnable);
Elliott Hughes5f791332011-09-15 17:45:30 -0700509
510 if (wasInterrupted) {
511 /*
512 * We were interrupted while waiting, or somebody interrupted an
513 * un-interruptible thread earlier and we're bailing out immediately.
514 *
515 * The doc sayeth: "The interrupted status of the current thread is
516 * cleared when this exception is thrown."
517 */
518 self->interrupted_ = false;
519 if (interruptShouldThrow) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700520 Thread::Current()->ThrowNewException("Ljava/lang/InterruptedException;", NULL);
Elliott Hughes5f791332011-09-15 17:45:30 -0700521 }
522 }
523}
524
525void Monitor::Notify(Thread* self) {
526 DCHECK(self != NULL);
527
528 // Make sure that we hold the lock.
529 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800530 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700531 return;
532 }
533 // Signal the first waiting thread in the wait set.
534 while (wait_set_ != NULL) {
535 Thread* thread = wait_set_;
536 wait_set_ = thread->wait_next_;
537 thread->wait_next_ = NULL;
538
539 // Check to see if the thread is still waiting.
Elliott Hughes85d15452011-09-16 17:33:01 -0700540 MutexLock mu(*thread->wait_mutex_);
Elliott Hughes5f791332011-09-15 17:45:30 -0700541 if (thread->wait_monitor_ != NULL) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700542 thread->wait_cond_->Signal();
Elliott Hughes5f791332011-09-15 17:45:30 -0700543 return;
544 }
545 }
546}
547
548void Monitor::NotifyAll(Thread* self) {
549 DCHECK(self != NULL);
550
551 // Make sure that we hold the lock.
552 if (owner_ != self) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800553 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700554 return;
555 }
556 // Signal all threads in the wait set.
557 while (wait_set_ != NULL) {
558 Thread* thread = wait_set_;
559 wait_set_ = thread->wait_next_;
560 thread->wait_next_ = NULL;
561 thread->Notify();
562 }
563}
564
565/*
566 * Changes the shape of a monitor from thin to fat, preserving the
567 * internal lock state. The calling thread must own the lock.
568 */
569void Monitor::Inflate(Thread* self, Object* obj) {
570 DCHECK(self != NULL);
571 DCHECK(obj != NULL);
572 DCHECK_EQ(LW_SHAPE(*obj->GetRawLockWordAddress()), LW_SHAPE_THIN);
Elliott Hughesf8e01272011-10-17 11:29:05 -0700573 DCHECK_EQ(LW_LOCK_OWNER(*obj->GetRawLockWordAddress()), static_cast<int32_t>(self->GetThinLockId()));
Elliott Hughes5f791332011-09-15 17:45:30 -0700574
575 // Allocate and acquire a new monitor.
576 Monitor* m = new Monitor(obj);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800577 VLOG(monitor) << "monitor: thread " << self->GetThinLockId()
578 << " created monitor " << m << " for object " << obj;
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700579 Runtime::Current()->GetMonitorList()->Add(m);
Elliott Hughes5f791332011-09-15 17:45:30 -0700580 m->Lock(self);
581 // Propagate the lock state.
582 uint32_t thin = *obj->GetRawLockWordAddress();
583 m->lock_count_ = LW_LOCK_COUNT(thin);
584 thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT;
585 thin |= reinterpret_cast<uint32_t>(m) | LW_SHAPE_FAT;
586 // Publish the updated lock word.
587 android_atomic_release_store(thin, obj->GetRawLockWordAddress());
588}
589
590void Monitor::MonitorEnter(Thread* self, Object* obj) {
591 volatile int32_t* thinp = obj->GetRawLockWordAddress();
Elliott Hughes7b9d9962012-04-20 18:48:18 -0700592 timespec tm;
Elliott Hughes398f64b2012-03-26 18:05:48 -0700593 uint32_t sleepDelayNs;
594 uint32_t minSleepDelayNs = 1000000; /* 1 millisecond */
595 uint32_t maxSleepDelayNs = 1000000000; /* 1 second */
Elliott Hughesf8e01272011-10-17 11:29:05 -0700596 uint32_t thin, newThin;
Elliott Hughes5f791332011-09-15 17:45:30 -0700597
Elliott Hughes4681c802011-09-25 18:04:37 -0700598 DCHECK(self != NULL);
599 DCHECK(obj != NULL);
Elliott Hughesf8e01272011-10-17 11:29:05 -0700600 uint32_t threadId = self->GetThinLockId();
Elliott Hughesa21039c2012-06-21 12:09:25 -0700601 retry:
Elliott Hughes5f791332011-09-15 17:45:30 -0700602 thin = *thinp;
603 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
604 /*
605 * The lock is a thin lock. The owner field is used to
606 * determine the acquire method, ordered by cost.
607 */
608 if (LW_LOCK_OWNER(thin) == threadId) {
609 /*
610 * The calling thread owns the lock. Increment the
611 * value of the recursion count field.
612 */
613 *thinp += 1 << LW_LOCK_COUNT_SHIFT;
614 if (LW_LOCK_COUNT(*thinp) == LW_LOCK_COUNT_MASK) {
615 /*
616 * The reacquisition limit has been reached. Inflate
617 * the lock so the next acquire will not overflow the
618 * recursion count field.
619 */
620 Inflate(self, obj);
621 }
622 } else if (LW_LOCK_OWNER(thin) == 0) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700623 // The lock is unowned. Install the thread id of the calling thread into the owner field.
624 // This is the common case: compiled code will have tried this before calling back into
625 // the runtime.
Elliott Hughes5f791332011-09-15 17:45:30 -0700626 newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
627 if (android_atomic_acquire_cas(thin, newThin, thinp) != 0) {
628 // The acquire failed. Try again.
629 goto retry;
630 }
631 } else {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800632 VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p (a %s) owned by %d",
Elliott Hughes81ff3182012-03-23 20:35:56 -0700633 threadId, thinp, PrettyTypeOf(obj).c_str(), LW_LOCK_OWNER(thin));
634 // The lock is owned by another thread. Notify the runtime that we are about to wait.
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700635 self->monitor_enter_object_ = obj;
Elliott Hughes34e06962012-04-09 13:55:55 -0700636 ThreadState oldStatus = self->SetState(kBlocked);
Elliott Hughes5f791332011-09-15 17:45:30 -0700637 // Spin until the thin lock is released or inflated.
638 sleepDelayNs = 0;
639 for (;;) {
640 thin = *thinp;
641 // Check the shape of the lock word. Another thread
642 // may have inflated the lock while we were waiting.
643 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
644 if (LW_LOCK_OWNER(thin) == 0) {
645 // The lock has been released. Install the thread id of the
646 // calling thread into the owner field.
647 newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT);
648 if (android_atomic_acquire_cas(thin, newThin, thinp) == 0) {
649 // The acquire succeed. Break out of the loop and proceed to inflate the lock.
650 break;
651 }
652 } else {
653 // The lock has not been released. Yield so the owning thread can run.
654 if (sleepDelayNs == 0) {
655 sched_yield();
656 sleepDelayNs = minSleepDelayNs;
657 } else {
658 tm.tv_sec = 0;
659 tm.tv_nsec = sleepDelayNs;
660 nanosleep(&tm, NULL);
661 // Prepare the next delay value. Wrap to avoid once a second polls for eternity.
662 if (sleepDelayNs < maxSleepDelayNs / 2) {
663 sleepDelayNs *= 2;
664 } else {
665 sleepDelayNs = minSleepDelayNs;
666 }
667 }
668 }
669 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700670 // The thin lock was inflated by another thread. Let the runtime know we are no longer
Elliott Hughes5f791332011-09-15 17:45:30 -0700671 // waiting and try again.
Elliott Hughes398f64b2012-03-26 18:05:48 -0700672 VLOG(monitor) << StringPrintf("monitor: thread %d found lock %p surprise-fattened by another thread", threadId, thinp);
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700673 self->monitor_enter_object_ = NULL;
Elliott Hughes5f791332011-09-15 17:45:30 -0700674 self->SetState(oldStatus);
675 goto retry;
676 }
677 }
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800678 VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p done", threadId, thinp);
Elliott Hughes81ff3182012-03-23 20:35:56 -0700679 // We have acquired the thin lock. Let the runtime know that we are no longer waiting.
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700680 self->monitor_enter_object_ = NULL;
Elliott Hughes5f791332011-09-15 17:45:30 -0700681 self->SetState(oldStatus);
682 // Fatten the lock.
683 Inflate(self, obj);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800684 VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p", threadId, thinp);
Elliott Hughes5f791332011-09-15 17:45:30 -0700685 }
686 } else {
687 // The lock is a fat lock.
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800688 VLOG(monitor) << StringPrintf("monitor: thread %d locking fat lock %p (%p) %p on a %s",
Elliott Hughes398f64b2012-03-26 18:05:48 -0700689 threadId, thinp, LW_MONITOR(*thinp),
690 reinterpret_cast<void*>(*thinp), PrettyTypeOf(obj).c_str());
Elliott Hughes5f791332011-09-15 17:45:30 -0700691 DCHECK(LW_MONITOR(*thinp) != NULL);
692 LW_MONITOR(*thinp)->Lock(self);
693 }
694}
695
696bool Monitor::MonitorExit(Thread* self, Object* obj) {
697 volatile int32_t* thinp = obj->GetRawLockWordAddress();
698
699 DCHECK(self != NULL);
Elliott Hughes34e06962012-04-09 13:55:55 -0700700 //DCHECK_EQ(self->GetState(), kRunnable);
Elliott Hughes5f791332011-09-15 17:45:30 -0700701 DCHECK(obj != NULL);
702
703 /*
704 * Cache the lock word as its value can change while we are
705 * examining its state.
706 */
707 uint32_t thin = *thinp;
708 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
709 /*
710 * The lock is thin. We must ensure that the lock is owned
711 * by the given thread before unlocking it.
712 */
Elliott Hughesf8e01272011-10-17 11:29:05 -0700713 if (LW_LOCK_OWNER(thin) == self->GetThinLockId()) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700714 /*
715 * We are the lock owner. It is safe to update the lock
716 * without CAS as lock ownership guards the lock itself.
717 */
718 if (LW_LOCK_COUNT(thin) == 0) {
719 /*
720 * The lock was not recursively acquired, the common
721 * case. Unlock by clearing all bits except for the
722 * hash state.
723 */
724 thin &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT);
725 android_atomic_release_store(thin, thinp);
726 } else {
727 /*
728 * The object was recursively acquired. Decrement the
729 * lock recursion count field.
730 */
731 *thinp -= 1 << LW_LOCK_COUNT_SHIFT;
732 }
733 } else {
734 /*
735 * We do not own the lock. The JVM spec requires that we
736 * throw an exception in this case.
737 */
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800738 FailedUnlock(obj, self, NULL, NULL);
Elliott Hughes5f791332011-09-15 17:45:30 -0700739 return false;
740 }
741 } else {
742 /*
743 * The lock is fat. We must check to see if Unlock has
744 * raised any exceptions before continuing.
745 */
746 DCHECK(LW_MONITOR(*thinp) != NULL);
747 if (!LW_MONITOR(*thinp)->Unlock(self)) {
748 // An exception has been raised. Do not fall through.
749 return false;
750 }
751 }
752 return true;
753}
754
755/*
756 * Object.wait(). Also called for class init.
757 */
758void Monitor::Wait(Thread* self, Object *obj, int64_t ms, int32_t ns, bool interruptShouldThrow) {
759 volatile int32_t* thinp = obj->GetRawLockWordAddress();
760
761 // If the lock is still thin, we need to fatten it.
762 uint32_t thin = *thinp;
763 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
764 // Make sure that 'self' holds the lock.
Elliott Hughesf8e01272011-10-17 11:29:05 -0700765 if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800766 ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700767 return;
768 }
769
770 /* This thread holds the lock. We need to fatten the lock
771 * so 'self' can block on it. Don't update the object lock
772 * field yet, because 'self' needs to acquire the lock before
773 * any other thread gets a chance.
774 */
775 Inflate(self, obj);
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800776 VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p by wait()", self->GetThinLockId(), thinp);
Elliott Hughes5f791332011-09-15 17:45:30 -0700777 }
778 LW_MONITOR(*thinp)->Wait(self, ms, ns, interruptShouldThrow);
779}
780
781void Monitor::Notify(Thread* self, Object *obj) {
782 uint32_t thin = *obj->GetRawLockWordAddress();
783
784 // If the lock is still thin, there aren't any waiters;
785 // waiting on an object forces lock fattening.
786 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
787 // Make sure that 'self' holds the lock.
Elliott Hughesf8e01272011-10-17 11:29:05 -0700788 if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800789 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700790 return;
791 }
792 // no-op; there are no waiters to notify.
793 } else {
794 // It's a fat lock.
795 LW_MONITOR(thin)->Notify(self);
796 }
797}
798
799void Monitor::NotifyAll(Thread* self, Object *obj) {
800 uint32_t thin = *obj->GetRawLockWordAddress();
801
802 // If the lock is still thin, there aren't any waiters;
803 // waiting on an object forces lock fattening.
804 if (LW_SHAPE(thin) == LW_SHAPE_THIN) {
805 // Make sure that 'self' holds the lock.
Elliott Hughesf8e01272011-10-17 11:29:05 -0700806 if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) {
Ian Rogers6d0b13e2012-02-07 09:25:29 -0800807 ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()");
Elliott Hughes5f791332011-09-15 17:45:30 -0700808 return;
809 }
810 // no-op; there are no waiters to notify.
811 } else {
812 // It's a fat lock.
813 LW_MONITOR(thin)->NotifyAll(self);
814 }
815}
816
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700817uint32_t Monitor::GetThinLockId(uint32_t raw_lock_word) {
Elliott Hughes5f791332011-09-15 17:45:30 -0700818 if (LW_SHAPE(raw_lock_word) == LW_SHAPE_THIN) {
819 return LW_LOCK_OWNER(raw_lock_word);
820 } else {
821 Thread* owner = LW_MONITOR(raw_lock_word)->owner_;
822 return owner ? owner->GetThinLockId() : 0;
823 }
824}
825
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700826void Monitor::DescribeWait(std::ostream& os, const Thread* thread) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700827 ThreadState state = thread->GetState();
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700828
829 Object* object = NULL;
830 uint32_t lock_owner = ThreadList::kInvalidId;
Elliott Hughes34e06962012-04-09 13:55:55 -0700831 if (state == kWaiting || state == kTimedWaiting) {
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700832 os << " - waiting on ";
833 Monitor* monitor = thread->wait_monitor_;
834 if (monitor != NULL) {
835 object = monitor->obj_;
836 }
837 lock_owner = Thread::LockOwnerFromThreadLock(object);
Elliott Hughes34e06962012-04-09 13:55:55 -0700838 } else if (state == kBlocked) {
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700839 os << " - waiting to lock ";
840 object = thread->monitor_enter_object_;
841 if (object != NULL) {
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700842 lock_owner = object->GetThinLockId();
Elliott Hughes8e4aac52011-09-26 17:03:36 -0700843 }
844 } else {
845 // We're not waiting on anything.
846 return;
847 }
848 os << "<" << object << ">";
849
850 // - waiting on <0x613f83d8> (a java.lang.ThreadLock) held by thread 5
851 // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>)
852 os << " (a " << PrettyTypeOf(object) << ")";
853
854 if (lock_owner != ThreadList::kInvalidId) {
855 os << " held by thread " << lock_owner;
856 }
857
858 os << "\n";
859}
860
Ian Rogers0399dde2012-06-06 17:09:28 -0700861void Monitor::TranslateLocation(const Method* method, uint32_t dex_pc,
jeffhao33dc7712011-11-09 17:54:24 -0800862 const char*& source_file, uint32_t& line_number) const {
863 // If method is null, location is unknown
864 if (method == NULL) {
Elliott Hughes12c51e32012-01-17 20:25:05 -0800865 source_file = "";
jeffhao33dc7712011-11-09 17:54:24 -0800866 line_number = 0;
867 return;
868 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800869 MethodHelper mh(method);
870 source_file = mh.GetDeclaringClassSourceFile();
Elliott Hughes12c51e32012-01-17 20:25:05 -0800871 if (source_file == NULL) {
872 source_file = "";
873 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700874 line_number = mh.GetLineNumFromDexPC(dex_pc);
jeffhao33dc7712011-11-09 17:54:24 -0800875}
876
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700877MonitorList::MonitorList() : lock_("MonitorList lock") {
878}
879
880MonitorList::~MonitorList() {
881 MutexLock mu(lock_);
882 STLDeleteElements(&list_);
883}
884
885void MonitorList::Add(Monitor* m) {
886 MutexLock mu(lock_);
887 list_.push_front(m);
888}
889
890void MonitorList::SweepMonitorList(Heap::IsMarkedTester is_marked, void* arg) {
891 MutexLock mu(lock_);
892 typedef std::list<Monitor*>::iterator It; // TODO: C++0x auto
893 It it = list_.begin();
894 while (it != list_.end()) {
895 Monitor* m = *it;
896 if (!is_marked(m->GetObject(), arg)) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800897 VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " << m->GetObject();
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700898 delete m;
899 it = list_.erase(it);
900 } else {
901 ++it;
902 }
903 }
904}
905
Elliott Hughes5f791332011-09-15 17:45:30 -0700906} // namespace art