monitor.cc revision 34e069606d6f1698cd3c33b39e72b79ae27e1c7b
15f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 25f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Copyright (C) 2008 The Android Open Source Project 35f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 45f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 55f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * you may not use this file except in compliance with the License. 65f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * You may obtain a copy of the License at 75f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 85f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 95f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unless required by applicable law or agreed to in writing, software 115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * See the License for the specific language governing permissions and 145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * limitations under the License. 155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#include "monitor.h" 185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include <errno.h> 205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include <fcntl.h> 215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include <pthread.h> 225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include <stdlib.h> 235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include <sys/time.h> 245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include <time.h> 255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include <unistd.h> 265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao#include "class_linker.h" 285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "mutex.h" 295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "object.h" 306d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 3188c5c355fc3d881f905564911d746b2313d5fc89Elliott Hughes#include "scoped_thread_list_lock.h" 32c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes#include "stl_util.h" 335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "thread.h" 348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "thread_list.h" 355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesnamespace art { 375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Every Object has a monitor associated with it, but not every Object is 405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * actually locked. Even the ones that are locked do not need a 415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * full-fledged monitor until a) there is actual contention or b) wait() 425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * is called on the Object. 435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * For Android, we have implemented a scheme similar to the one described 455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * in Bacon et al.'s "Thin locks: featherweight synchronization for Java" 465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (ACM 1998). Things are even easier for us, though, because we have 475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * a full 32 bits to work with. 485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The two states of an Object's lock are referred to as "thin" and 505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * "fat". A lock may transition from the "thin" state to the "fat" 515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * state and this transition is referred to as inflation. Once a lock 525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * has been inflated it remains in the "fat" state indefinitely. 535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock value itself is stored in Object.lock. The LSB of the 555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock encodes its state. When cleared, the lock is in the "thin" 565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * state and its bits are formatted as follows: 575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0] 595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock count thread id hash state 0 605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * When set, the lock is in the "fat" state and its bits are formatted 625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * as follows: 635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * [31 ---- 3] [2 ---- 1] [0] 655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * pointer hash state 1 665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * For an in-depth description of the mechanics of thin-vs-fat locking, 685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * read the paper referred to above. 6954e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * 705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Monitors provide: 715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - mutually exclusive access to resources 725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - a way for multiple threads to wait for notification 735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * In effect, they fill the role of both mutexes and condition variables. 755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Only one thread can own the monitor at any time. There may be several 775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * threads waiting on it (the wait call unlocks it). One or more waiting 785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * threads may be getting interrupted or notified at any given time. 795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * TODO: the various members of monitor are not SMP-safe. 815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 8254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 8354e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 8454e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes/* 8554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * Monitor accessor. Extracts a monitor structure pointer from a fat 8654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * lock. Performs no error checking. 8754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes */ 8854e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_MONITOR(x) \ 89398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes (reinterpret_cast<Monitor*>((x) & ~((LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT) | LW_SHAPE_MASK))) 9054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 9154e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes/* 9254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * Lock recursion count field. Contains a count of the number of times 9354e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * a lock has been recursively acquired. 9454e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes */ 9554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_LOCK_COUNT_MASK 0x1fff 9654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_LOCK_COUNT_SHIFT 19 9754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_LOCK_COUNT(x) (((x) >> LW_LOCK_COUNT_SHIFT) & LW_LOCK_COUNT_MASK) 9854e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 99fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool (*Monitor::is_sensitive_thread_hook_)() = NULL; 100fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesuint32_t Monitor::lock_profiling_threshold_ = 0; 10132d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes 102fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool Monitor::IsSensitiveThread() { 103fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (is_sensitive_thread_hook_ != NULL) { 104fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return (*is_sensitive_thread_hook_)(); 105fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 106fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return false; 107fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 108fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 1094dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughesvoid Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) { 110fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes lock_profiling_threshold_ = lock_profiling_threshold; 111fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes is_sensitive_thread_hook_ = is_sensitive_thread_hook; 11232d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes} 11332d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes 1145f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::Monitor(Object* obj) 1155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes : owner_(NULL), 1165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_(0), 1175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes obj_(obj), 1185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_(NULL), 1195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_("a monitor lock"), 12033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_(NULL), 12133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_pc_(0) { 1225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1245f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::~Monitor() { 1255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj_ != NULL); 1265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(LW_SHAPE(*obj_->GetRawLockWordAddress()), LW_SHAPE_FAT); 1275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 1305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Links a thread into a monitor's wait set. The monitor lock must be 1315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * held by the caller of this routine. 1325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 1335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::AppendToWaitSet(Thread* thread) { 1345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(thread != NULL); 136dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes DCHECK(thread->wait_next_ == NULL) << thread->wait_next_; 1375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == NULL) { 1385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread; 1395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // push_back. 1435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 1445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (t->wait_next_ != NULL) { 1455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t = t->wait_next_; 1465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t->wait_next_ = thread; 1485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 1515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unlinks a thread from a monitor's wait set. The monitor lock must 1525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * be held by the caller of this routine. 1535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 1545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::RemoveFromWaitSet(Thread *thread) { 1555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(thread != NULL); 1575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == NULL) { 1585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == thread) { 1615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 1625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 1635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 1675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (t->wait_next_ != NULL) { 1685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (t->wait_next_ == thread) { 1695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t->wait_next_ = thread->wait_next_; 1705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 1715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t = t->wait_next_; 1745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 177c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesObject* Monitor::GetObject() { 178c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes return obj_; 1795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Lock(Thread* self) { 1825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ == self) { 1835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_++; 1845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 186fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 187fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint64_t waitStart, waitEnd; 1885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (!lock_.TryLock()) { 189fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint32_t wait_threshold = lock_profiling_threshold_; 19033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const Method* current_locking_method = NULL; 191e65a6c99b0fd8908484b61c3c8f5a43cf0081f3cElliott Hughes uintptr_t current_locking_pc = 0; 1925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes { 19334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(self, kBlocked); 194fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 195fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes waitStart = NanoTime() / 1000; 196fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 19733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao current_locking_method = locking_method_; 19833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao current_locking_pc = locking_pc_; 1995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_.Lock(); 201fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 202fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes waitEnd = NanoTime() / 1000; 203fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 204fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 205fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 206fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 207fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint64_t wait_ms = (waitEnd - waitStart) / 1000; 208fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint32_t sample_percent; 209fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_ms >= wait_threshold) { 210fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes sample_percent = 100; 211fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } else { 212fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes sample_percent = 100 * wait_ms / wait_threshold; 213fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 214fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { 21533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const char* current_locking_filename; 21633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao uint32_t current_locking_line_number; 21733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao TranslateLocation(current_locking_method, current_locking_pc, 21833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao current_locking_filename, current_locking_line_number); 21933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao LogContentionEvent(self, wait_ms, sample_percent, current_locking_filename, current_locking_line_number); 220fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = self; 2245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(lock_count_, 0); 2255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // When debugging, save the current monitor holder for future 2275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // acquisition failures to use in sampled logging. 228fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (lock_profiling_threshold_ != 0) { 229d07986fad0d08cdf05505cf9230714a2cf0dd9aeElliott Hughes locking_method_ = self->GetCurrentMethod(&locking_pc_); 230fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 2325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2336d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) 2346d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers __attribute__((format(printf, 1, 2))); 2356d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 2366d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) { 2376d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_list args; 2386d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_start(args, fmt); 2396d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers Thread::Current()->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args); 24064277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom if (!Runtime::Current()->IsStarted()) { 24164277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom std::ostringstream ss; 24264277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom Thread::Current()->Dump(ss); 24364277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom std::string str(ss.str()); 24464277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom LOG(ERROR) << "IllegalMonitorStateException: " << str; 24564277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom } 2466d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_end(args); 2476d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers} 2486d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 249d423741f91526cada9d081c64ee295ec9863032dElliott Hughesstatic std::string ThreadToString(Thread* thread) { 250d423741f91526cada9d081c64ee295ec9863032dElliott Hughes if (thread == NULL) { 251d423741f91526cada9d081c64ee295ec9863032dElliott Hughes return "NULL"; 252d423741f91526cada9d081c64ee295ec9863032dElliott Hughes } 253d423741f91526cada9d081c64ee295ec9863032dElliott Hughes std::ostringstream oss; 254d423741f91526cada9d081c64ee295ec9863032dElliott Hughes // TODO: alternatively, we could just return the thread's name. 255d423741f91526cada9d081c64ee295ec9863032dElliott Hughes oss << *thread; 256d423741f91526cada9d081c64ee295ec9863032dElliott Hughes return oss.str(); 257d423741f91526cada9d081c64ee295ec9863032dElliott Hughes} 258d423741f91526cada9d081c64ee295ec9863032dElliott Hughes 259ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughesvoid Monitor::FailedUnlock(Object* o, Thread* expected_owner, Thread* found_owner, 260ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes Monitor* monitor) { 261ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes Thread* current_owner = NULL; 262ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string current_owner_string; 263ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string expected_owner_string; 264ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string found_owner_string; 265ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes { 266ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // TODO: isn't this too late to prevent threads from disappearing? 267ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Acquire thread list lock so threads won't disappear from under us. 268ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes ScopedThreadListLock thread_list_lock; 269ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Re-read owner now that we hold lock. 270ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner = (monitor != NULL) ? monitor->owner_ : NULL; 271ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Get short descriptions of the threads involved. 272ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string = ThreadToString(current_owner); 273ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string = ThreadToString(expected_owner); 274ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string = ThreadToString(found_owner); 275ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 2766d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (current_owner == NULL) { 2776d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner == NULL) { 2786d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" 2796d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s'", 280ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 281ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 2826d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 2836d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: the original read found an owner but now there is none 2846d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 2856d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (where now the monitor appears unowned) on thread '%s'", 286ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 287ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 288ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 2896d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 2906d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 2916d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner == NULL) { 2926d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally there was no owner, there is now 2936d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 2946d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (originally believed to be unowned) on thread '%s'", 295ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 296ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 297ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 2986d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 2996d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner != current_owner) { 3006d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally found and current owner have changed 3016d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" 3026d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " owned by '%s') on object of type '%s' on thread '%s'", 303ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 304ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 305ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 306ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3076d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3086d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3096d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s", 310ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 311ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 312ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3136d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3146d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3156d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Monitor::Unlock(Thread* self) { 3195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 3206d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers Thread* owner = owner_; 3216d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (owner == self) { 3225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We own the monitor, so nobody else can be in here. 3235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock_count_ == 0) { 3245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = NULL; 32533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_ = NULL; 32633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_pc_ = 0; 3275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_.Unlock(); 3285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes --lock_count_; 3305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We don't own this, so we're not allowed to unlock it. 3335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The JNI spec says that we should throw IllegalMonitorStateException 3345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // in this case. 3356d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers FailedUnlock(obj_, self, owner, this); 3365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 3375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return true; 3395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 3425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Converts the given relative waiting time into an absolute time. 3435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 344b8d2eeb212d55483db97a510656da6114a0e6006Elliott Hughesstatic void ToAbsoluteTime(int64_t ms, int32_t ns, struct timespec *ts) { 3455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes int64_t endSec; 3465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#ifdef HAVE_TIMEDWAIT_MONOTONIC 3485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes clock_gettime(CLOCK_MONOTONIC, ts); 3495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#else 3505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes { 3515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes struct timeval tv; 3525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes gettimeofday(&tv, NULL); 3535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ts->tv_sec = tv.tv_sec; 3545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ts->tv_nsec = tv.tv_usec * 1000; 3555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#endif 3575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes endSec = ts->tv_sec + ms / 1000; 3585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (endSec >= 0x7fffffff) { 3595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LOG(INFO) << "Note: end time exceeds epoch"; 3605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes endSec = 0x7ffffffe; 3615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ts->tv_sec = endSec; 3635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ts->tv_nsec = (ts->tv_nsec + (ms % 1000) * 1000000) + ns; 3645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Catch rollover. 3665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (ts->tv_nsec >= 1000000000L) { 3675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ts->tv_sec++; 3685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ts->tv_nsec -= 1000000000L; 3695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 3735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Wait on a monitor until timeout, interrupt, or notification. Used for 3745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join(). 3755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * If another thread calls Thread.interrupt(), we throw InterruptedException 3775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and return immediately if one of the following are true: 3785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in wait(), wait(long), or wait(long, int) methods of Object 3795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in join(), join(long), or join(long, int) methods of Thread 3805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in sleep(long), or sleep(long, int) methods of Thread 3815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Otherwise, we set the "interrupted" flag. 3825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Checks to make sure that "ns" is in the range 0-999999 3845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (i.e. fractions of a millisecond) and throws the appropriate 3855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * exception if it isn't. 3865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The spec allows "spurious wakeups", and recommends that all code using 3885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() do so in a loop. This appears to derive from concerns 3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * about pthread_cond_wait() on multiprocessor systems. Some commentary 3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * on the web casts doubt on whether these can/should occur. 3915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Since we're allowed to wake up "early", we clamp extremely long durations 3935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * to return at the end of the 32-bit time epoch. 3945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 3955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Wait(Thread* self, int64_t ms, int32_t ns, bool interruptShouldThrow) { 3965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 3975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 3995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 4006d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 4015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 4025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Enforce the timeout range. 4055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (ms < 0 || ns < 0 || ns > 999999) { 4065cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", 4075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes "timeout arguments out of range: ms=%lld ns=%d", ms, ns); 4085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 4095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Compute absolute wakeup time, if necessary. 4125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes struct timespec ts; 4135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes bool timed = false; 4145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (ms != 0 || ns != 0) { 4155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes ToAbsoluteTime(ms, ns, &ts); 4165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes timed = true; 4175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Add ourselves to the set of threads waiting on this monitor, and 4215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * release our hold. We need to let it go even if we're a few levels 4225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * deep in a recursive lock, and we need to restore that later. 4235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 4245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We append to the wait set ahead of clearing the count and owner 4255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * fields so the subroutine can check that the calling thread owns 4265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * the monitor. Aside from that, the order of member updates is 4275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * not order sensitive as we hold the pthread mutex. 4285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes AppendToWaitSet(self); 4305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes int prevLockCount = lock_count_; 4315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_ = 0; 4325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = NULL; 43333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const Method* savedMethod = locking_method_; 43433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_ = NULL; 435e65a6c99b0fd8908484b61c3c8f5a43cf0081f3cElliott Hughes uintptr_t savedPc = locking_pc_; 43633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_pc_ = 0; 4375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Update thread status. If the GC wakes up, it'll ignore us, knowing 4405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * that we won't touch any references in this state, and we'll check 4415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * our suspend mode before we transition out. 4425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (timed) { 44434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes self->SetState(kTimedWaiting); 4455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 44634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes self->SetState(kWaiting); 4475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 44985d1545e985ac689db4bad7849880e843707c862Elliott Hughes self->wait_mutex_->Lock(); 4505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Set wait_monitor_ to the monitor object we will be waiting on. 4535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * When wait_monitor_ is non-NULL a notifying or interrupting thread 4545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * must signal the thread's wait_cond_ to wake it up. 4555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self->wait_monitor_ == NULL); 4575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes self->wait_monitor_ = this; 4585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Handle the case where the thread was interrupted before we called 4615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * wait(). 4625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes bool wasInterrupted = false; 4645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (self->interrupted_) { 4655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wasInterrupted = true; 4665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes self->wait_monitor_ = NULL; 46785d1545e985ac689db4bad7849880e843707c862Elliott Hughes self->wait_mutex_->Unlock(); 4685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes goto done; 4695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Release the monitor lock and wait for a notification or 4735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * a timeout to occur. 4745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_.Unlock(); 4765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (!timed) { 47885d1545e985ac689db4bad7849880e843707c862Elliott Hughes self->wait_cond_->Wait(*self->wait_mutex_); 4795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 48085d1545e985ac689db4bad7849880e843707c862Elliott Hughes self->wait_cond_->TimedWait(*self->wait_mutex_, ts); 4815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (self->interrupted_) { 4835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wasInterrupted = true; 4845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes self->interrupted_ = false; 4875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes self->wait_monitor_ = NULL; 48885d1545e985ac689db4bad7849880e843707c862Elliott Hughes self->wait_mutex_->Unlock(); 4895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Reacquire the monitor lock. 4915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Lock(self); 4925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesdone: 4945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We remove our thread from wait set after restoring the count 4965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and owner fields so the subroutine can check that the calling 4975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * thread owns the monitor. Aside from that, the order of member 4985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * updates is not order sensitive as we hold the pthread mutex. 4995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 5005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = self; 5015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_ = prevLockCount; 50233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_ = savedMethod; 50333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_pc_ = savedPc; 5045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes RemoveFromWaitSet(self); 5055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 50634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes /* set self->status back to kRunnable, and self-suspend if needed */ 50734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes self->SetState(kRunnable); 5085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wasInterrupted) { 5105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 5115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We were interrupted while waiting, or somebody interrupted an 5125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * un-interruptible thread earlier and we're bailing out immediately. 5135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 5145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The doc sayeth: "The interrupted status of the current thread is 5155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * cleared when this exception is thrown." 5165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 5175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes self->interrupted_ = false; 5185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (interruptShouldThrow) { 5195cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes Thread::Current()->ThrowNewException("Ljava/lang/InterruptedException;", NULL); 5205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self) { 5255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 5285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 5296d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 5305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal the first waiting thread in the wait set. 5335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (wait_set_ != NULL) { 5345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 5355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 5365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 5375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Check to see if the thread is still waiting. 53985d1545e985ac689db4bad7849880e843707c862Elliott Hughes MutexLock mu(*thread->wait_mutex_); 5405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (thread->wait_monitor_ != NULL) { 54185d1545e985ac689db4bad7849880e843707c862Elliott Hughes thread->wait_cond_->Signal(); 5425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self) { 5485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 5515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 5526d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); 5535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal all threads in the wait set. 5565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (wait_set_ != NULL) { 5575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 5585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 5595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 5605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->Notify(); 5615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 5655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Changes the shape of a monitor from thin to fat, preserving the 5665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * internal lock state. The calling thread must own the lock. 5675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 5685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Inflate(Thread* self, Object* obj) { 5695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj != NULL); 5715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(LW_SHAPE(*obj->GetRawLockWordAddress()), LW_SHAPE_THIN); 572f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes DCHECK_EQ(LW_LOCK_OWNER(*obj->GetRawLockWordAddress()), static_cast<int32_t>(self->GetThinLockId())); 5735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Allocate and acquire a new monitor. 5755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Monitor* m = new Monitor(obj); 5764dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << "monitor: thread " << self->GetThinLockId() 5774dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes << " created monitor " << m << " for object " << obj; 578c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes Runtime::Current()->GetMonitorList()->Add(m); 5795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes m->Lock(self); 5805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Propagate the lock state. 5815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *obj->GetRawLockWordAddress(); 5825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes m->lock_count_ = LW_LOCK_COUNT(thin); 5835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT; 5845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin |= reinterpret_cast<uint32_t>(m) | LW_SHAPE_FAT; 5855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Publish the updated lock word. 5865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes android_atomic_release_store(thin, obj->GetRawLockWordAddress()); 5875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::MonitorEnter(Thread* self, Object* obj) { 5905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 5915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes struct timespec tm; 592398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t sleepDelayNs; 593398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t minSleepDelayNs = 1000000; /* 1 millisecond */ 594398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t maxSleepDelayNs = 1000000000; /* 1 second */ 595f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes uint32_t thin, newThin; 5965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5974681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes DCHECK(self != NULL); 5984681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes DCHECK(obj != NULL); 599f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes uint32_t threadId = self->GetThinLockId(); 6005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesretry: 6015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin = *thinp; 6025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 6035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is a thin lock. The owner field is used to 6055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * determine the acquire method, ordered by cost. 6065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_OWNER(thin) == threadId) { 6085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The calling thread owns the lock. Increment the 6105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * value of the recursion count field. 6115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *thinp += 1 << LW_LOCK_COUNT_SHIFT; 6135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_COUNT(*thinp) == LW_LOCK_COUNT_MASK) { 6145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The reacquisition limit has been reached. Inflate 6165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * the lock so the next acquire will not overflow the 6175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * recursion count field. 6185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 6205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else if (LW_LOCK_OWNER(thin) == 0) { 62281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The lock is unowned. Install the thread id of the calling thread into the owner field. 62381ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // This is the common case: compiled code will have tried this before calling back into 62481ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // the runtime. 6255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); 6265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (android_atomic_acquire_cas(thin, newThin, thinp) != 0) { 6275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The acquire failed. Try again. 6285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes goto retry; 6295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6314dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p (a %s) owned by %d", 63281ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes threadId, thinp, PrettyTypeOf(obj).c_str(), LW_LOCK_OWNER(thin)); 63381ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The lock is owned by another thread. Notify the runtime that we are about to wait. 6348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = obj; 63534e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ThreadState oldStatus = self->SetState(kBlocked); 6365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Spin until the thin lock is released or inflated. 6375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = 0; 6385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes for (;;) { 6395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin = *thinp; 6405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Check the shape of the lock word. Another thread 6415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // may have inflated the lock while we were waiting. 6425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 6435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_OWNER(thin) == 0) { 6445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock has been released. Install the thread id of the 6455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // calling thread into the owner field. 6465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); 6475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (android_atomic_acquire_cas(thin, newThin, thinp) == 0) { 6485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The acquire succeed. Break out of the loop and proceed to inflate the lock. 6495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes break; 6505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock has not been released. Yield so the owning thread can run. 6535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (sleepDelayNs == 0) { 6545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sched_yield(); 6555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = minSleepDelayNs; 6565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes tm.tv_sec = 0; 6585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes tm.tv_nsec = sleepDelayNs; 6595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes nanosleep(&tm, NULL); 6605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Prepare the next delay value. Wrap to avoid once a second polls for eternity. 6615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (sleepDelayNs < maxSleepDelayNs / 2) { 6625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs *= 2; 6635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = minSleepDelayNs; 6655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 66981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The thin lock was inflated by another thread. Let the runtime know we are no longer 6705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting and try again. 671398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d found lock %p surprise-fattened by another thread", threadId, thinp); 6728e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = NULL; 6735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes self->SetState(oldStatus); 6745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes goto retry; 6755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6774dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p done", threadId, thinp); 67881ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // We have acquired the thin lock. Let the runtime know that we are no longer waiting. 6798e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = NULL; 6805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes self->SetState(oldStatus); 6815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Fatten the lock. 6825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 6834dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p", threadId, thinp); 6845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock is a fat lock. 6874dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d locking fat lock %p (%p) %p on a %s", 688398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes threadId, thinp, LW_MONITOR(*thinp), 689398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes reinterpret_cast<void*>(*thinp), PrettyTypeOf(obj).c_str()); 6905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(LW_MONITOR(*thinp) != NULL); 6915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(*thinp)->Lock(self); 6925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 6945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 6955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Monitor::MonitorExit(Thread* self, Object* obj) { 6965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 6975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 6985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 69934e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes //DCHECK_EQ(self->GetState(), kRunnable); 7005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj != NULL); 7015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Cache the lock word as its value can change while we are 7045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * examining its state. 7055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *thinp; 7075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is thin. We must ensure that the lock is owned 7105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * by the given thread before unlocking it. 7115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 712f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) == self->GetThinLockId()) { 7135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We are the lock owner. It is safe to update the lock 7155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * without CAS as lock ownership guards the lock itself. 7165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_COUNT(thin) == 0) { 7185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock was not recursively acquired, the common 7205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * case. Unlock by clearing all bits except for the 7215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * hash state. 7225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT); 7245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes android_atomic_release_store(thin, thinp); 7255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The object was recursively acquired. Decrement the 7285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock recursion count field. 7295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *thinp -= 1 << LW_LOCK_COUNT_SHIFT; 7315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We do not own the lock. The JVM spec requires that we 7355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * throw an exception in this case. 7365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7376d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers FailedUnlock(obj, self, NULL, NULL); 7385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 7395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is fat. We must check to see if Unlock has 7435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * raised any exceptions before continuing. 7445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(LW_MONITOR(*thinp) != NULL); 7465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (!LW_MONITOR(*thinp)->Unlock(self)) { 7475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // An exception has been raised. Do not fall through. 7485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 7495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return true; 7525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 7555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait(). Also called for class init. 7565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Wait(Thread* self, Object *obj, int64_t ms, int32_t ns, bool interruptShouldThrow) { 7585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 7595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, we need to fatten it. 7615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *thinp; 7625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 764f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 7656d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 7665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 7675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* This thread holds the lock. We need to fatten the lock 7705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * so 'self' can block on it. Don't update the object lock 7715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * field yet, because 'self' needs to acquire the lock before 7725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * any other thread gets a chance. 7735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 7754dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p by wait()", self->GetThinLockId(), thinp); 7765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(*thinp)->Wait(self, ms, ns, interruptShouldThrow); 7785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self, Object *obj) { 7815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *obj->GetRawLockWordAddress(); 7825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, there aren't any waiters; 7845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting on an object forces lock fattening. 7855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 787f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 7886d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 7895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 7905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // no-op; there are no waiters to notify. 7925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // It's a fat lock. 7945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(thin)->Notify(self); 7955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self, Object *obj) { 7995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *obj->GetRawLockWordAddress(); 8005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 8015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, there aren't any waiters; 8025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting on an object forces lock fattening. 8035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 8045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 805f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 8066d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); 8075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 8085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // no-op; there are no waiters to notify. 8105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 8115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // It's a fat lock. 8125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(thin)->NotifyAll(self); 8135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 81624a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstromuint32_t Monitor::GetThinLockId(uint32_t raw_lock_word) { 8175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(raw_lock_word) == LW_SHAPE_THIN) { 8185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return LW_LOCK_OWNER(raw_lock_word); 8195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 8205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* owner = LW_MONITOR(raw_lock_word)->owner_; 8215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return owner ? owner->GetThinLockId() : 0; 8225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 8258e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesvoid Monitor::DescribeWait(std::ostream& os, const Thread* thread) { 82634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ThreadState state = thread->GetState(); 8278e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8288e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* object = NULL; 8298e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uint32_t lock_owner = ThreadList::kInvalidId; 83034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes if (state == kWaiting || state == kTimedWaiting) { 8318e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " - waiting on "; 8328e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Monitor* monitor = thread->wait_monitor_; 8338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (monitor != NULL) { 8348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes object = monitor->obj_; 8358e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8368e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes lock_owner = Thread::LockOwnerFromThreadLock(object); 83734e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes } else if (state == kBlocked) { 8388e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " - waiting to lock "; 8398e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes object = thread->monitor_enter_object_; 8408e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (object != NULL) { 84124a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom lock_owner = object->GetThinLockId(); 8428e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8438e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } else { 8448e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // We're not waiting on anything. 8458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return; 8468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << "<" << object << ">"; 8488e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // - waiting on <0x613f83d8> (a java.lang.ThreadLock) held by thread 5 8508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) 8518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " (a " << PrettyTypeOf(object) << ")"; 8528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8538e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (lock_owner != ThreadList::kInvalidId) { 8548e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " held by thread " << lock_owner; 8558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8578e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << "\n"; 8588e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 8598e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 86033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhaovoid Monitor::TranslateLocation(const Method* method, uint32_t pc, 86133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const char*& source_file, uint32_t& line_number) const { 86233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao // If method is null, location is unknown 86333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao if (method == NULL) { 86412c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes source_file = ""; 86533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao line_number = 0; 86633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao return; 86733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao } 8686d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh(method); 8696d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_file = mh.GetDeclaringClassSourceFile(); 87012c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes if (source_file == NULL) { 87112c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes source_file = ""; 87212c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes } 8736d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers line_number = mh.GetLineNumFromNativePC(pc); 87433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao} 87533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 876c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::MonitorList() : lock_("MonitorList lock") { 877c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 878c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 879c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::~MonitorList() { 880c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes MutexLock mu(lock_); 881c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes STLDeleteElements(&list_); 882c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 883c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 884c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughesvoid MonitorList::Add(Monitor* m) { 885c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes MutexLock mu(lock_); 886c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes list_.push_front(m); 887c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 888c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 889c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughesvoid MonitorList::SweepMonitorList(Heap::IsMarkedTester is_marked, void* arg) { 890c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes MutexLock mu(lock_); 891c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes typedef std::list<Monitor*>::iterator It; // TODO: C++0x auto 892c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes It it = list_.begin(); 893c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes while (it != list_.end()) { 894c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes Monitor* m = *it; 895c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes if (!is_marked(m->GetObject(), arg)) { 8964dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " << m->GetObject(); 897c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes delete m; 898c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes it = list_.erase(it); 899c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } else { 900c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes ++it; 901c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 902c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 903c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 904c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 9055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} // namespace art 906