monitor.cc revision f327e07b37e349b1ec5eaad6dc294a9b7a081d20
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 1908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include <vector> 2008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 2176b6167407c2b6f5d40ad895b2793a6b037f54b2Elliott Hughes#include "base/mutex.h" 221aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include "base/stl_util.h" 2333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao#include "class_linker.h" 2408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "dex_instruction.h" 255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "object.h" 266d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 2700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "thread.h" 298e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "thread_list.h" 3008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "verifier/method_verifier.h" 31044288fb8f69e154862763a6b3bfea2bac1b397eElliott Hughes#include "well_known_classes.h" 325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesnamespace art { 345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Every Object has a monitor associated with it, but not every Object is 375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * actually locked. Even the ones that are locked do not need a 385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * full-fledged monitor until a) there is actual contention or b) wait() 395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * is called on the Object. 405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * For Android, we have implemented a scheme similar to the one described 425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * in Bacon et al.'s "Thin locks: featherweight synchronization for Java" 435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (ACM 1998). Things are even easier for us, though, because we have 445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * a full 32 bits to work with. 455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The two states of an Object's lock are referred to as "thin" and 475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * "fat". A lock may transition from the "thin" state to the "fat" 485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * state and this transition is referred to as inflation. Once a lock 495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * has been inflated it remains in the "fat" state indefinitely. 505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock value itself is stored in Object.lock. The LSB of the 525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock encodes its state. When cleared, the lock is in the "thin" 535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * state and its bits are formatted as follows: 545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0] 565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock count thread id hash state 0 575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * When set, the lock is in the "fat" state and its bits are formatted 595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * as follows: 605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * [31 ---- 3] [2 ---- 1] [0] 625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * pointer hash state 1 635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * For an in-depth description of the mechanics of thin-vs-fat locking, 655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * read the paper referred to above. 6654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * 675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Monitors provide: 685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - mutually exclusive access to resources 695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - a way for multiple threads to wait for notification 705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * In effect, they fill the role of both mutexes and condition variables. 725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Only one thread can own the monitor at any time. There may be several 745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * threads waiting on it (the wait call unlocks it). One or more waiting 755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * threads may be getting interrupted or notified at any given time. 765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * TODO: the various members of monitor are not SMP-safe. 785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7954e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 80f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes// The shape is the bottom bit; either LW_SHAPE_THIN or LW_SHAPE_FAT. 81f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes#define LW_SHAPE_MASK 0x1 82f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes#define LW_SHAPE(x) static_cast<int>((x) & LW_SHAPE_MASK) 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 11400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersMonitor::Monitor(Thread* owner, Object* obj) 11500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers : monitor_lock_("a monitor lock", kMonitorLock), 11600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers owner_(owner), 1175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_(0), 1185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes obj_(obj), 1195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_(NULL), 12033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_(NULL), 1210399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_(0) { 12281d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Lock(owner); 12300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Propagate the lock state. 12400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint32_t thin = *obj->GetRawLockWordAddress(); 12500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers lock_count_ = LW_LOCK_COUNT(thin); 12600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT; 12700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers thin |= reinterpret_cast<uint32_t>(this) | LW_SHAPE_FAT; 12800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Publish the updated lock word. 12900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers android_atomic_release_store(thin, obj->GetRawLockWordAddress()); 13000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Lock profiling. 13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (lock_profiling_threshold_ != 0) { 13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers locking_method_ = owner->GetCurrentMethod(&locking_dex_pc_); 13300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 1345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1365f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::~Monitor() { 1375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj_ != NULL); 1385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(LW_SHAPE(*obj_->GetRawLockWordAddress()), LW_SHAPE_FAT); 1395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 1425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Links a thread into a monitor's wait set. The monitor lock must be 1435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * held by the caller of this routine. 1445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 1455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::AppendToWaitSet(Thread* thread) { 1465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(thread != NULL); 148dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes DCHECK(thread->wait_next_ == NULL) << thread->wait_next_; 1495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == NULL) { 1505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread; 1515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // push_back. 1555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 1565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (t->wait_next_ != NULL) { 1575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t = t->wait_next_; 1585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t->wait_next_ = thread; 1605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 1635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unlinks a thread from a monitor's wait set. The monitor lock must 1645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * be held by the caller of this routine. 1655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 1665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::RemoveFromWaitSet(Thread *thread) { 1675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(thread != NULL); 1695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == NULL) { 1705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == thread) { 1735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 1745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 1755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 1795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (t->wait_next_ != NULL) { 1805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (t->wait_next_ == thread) { 1815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t->wait_next_ = thread->wait_next_; 1825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 1835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t = t->wait_next_; 1865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 189c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesObject* Monitor::GetObject() { 190c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes return obj_; 1915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Lock(Thread* self) { 1945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ == self) { 1955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_++; 1965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 198fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 19981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (!monitor_lock_.TryLock(self)) { 2002542d6690824d27e5894a635b40237ee91a412caMathieu Chartier uint64_t waitStart = 0; 2012542d6690824d27e5894a635b40237ee91a412caMathieu Chartier uint64_t waitEnd = 0; 202fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint32_t wait_threshold = lock_profiling_threshold_; 20366f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier const AbstractMethod* current_locking_method = NULL; 2040399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uint32_t current_locking_dex_pc = 0; 2055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes { 20634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(self, kBlocked); 207fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 208fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes waitStart = NanoTime() / 1000; 209fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 21033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao current_locking_method = locking_method_; 2110399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers current_locking_dex_pc = locking_dex_pc_; 2125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 21381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Lock(self); 214fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 215fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes waitEnd = NanoTime() / 1000; 216fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 217fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 218fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 219fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 220fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint64_t wait_ms = (waitEnd - waitStart) / 1000; 221fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint32_t sample_percent; 222fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_ms >= wait_threshold) { 223fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes sample_percent = 100; 224fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } else { 225fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes sample_percent = 100 * wait_ms / wait_threshold; 226fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 227fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { 22833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const char* current_locking_filename; 22933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao uint32_t current_locking_line_number; 2300399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers TranslateLocation(current_locking_method, current_locking_dex_pc, 23133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao current_locking_filename, current_locking_line_number); 23233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao LogContentionEvent(self, wait_ms, sample_percent, current_locking_filename, current_locking_line_number); 233fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = self; 2375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(lock_count_, 0); 2385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // When debugging, save the current monitor holder for future 2405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // acquisition failures to use in sampled logging. 241fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (lock_profiling_threshold_ != 0) { 2420399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); 243fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 2455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2466d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) 2476d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers __attribute__((format(printf, 1, 2))); 2486d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 24900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) 250b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 2516d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_list args; 2526d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_start(args, fmt); 2536d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers Thread::Current()->ThrowNewExceptionV("Ljava/lang/IllegalMonitorStateException;", fmt, args); 25464277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom if (!Runtime::Current()->IsStarted()) { 25564277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom std::ostringstream ss; 25664277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom Thread::Current()->Dump(ss); 25764277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom std::string str(ss.str()); 25864277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom LOG(ERROR) << "IllegalMonitorStateException: " << str; 25964277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom } 2606d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_end(args); 2616d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers} 2626d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 263d423741f91526cada9d081c64ee295ec9863032dElliott Hughesstatic std::string ThreadToString(Thread* thread) { 264d423741f91526cada9d081c64ee295ec9863032dElliott Hughes if (thread == NULL) { 265d423741f91526cada9d081c64ee295ec9863032dElliott Hughes return "NULL"; 266d423741f91526cada9d081c64ee295ec9863032dElliott Hughes } 267d423741f91526cada9d081c64ee295ec9863032dElliott Hughes std::ostringstream oss; 268d423741f91526cada9d081c64ee295ec9863032dElliott Hughes // TODO: alternatively, we could just return the thread's name. 269d423741f91526cada9d081c64ee295ec9863032dElliott Hughes oss << *thread; 270d423741f91526cada9d081c64ee295ec9863032dElliott Hughes return oss.str(); 271d423741f91526cada9d081c64ee295ec9863032dElliott Hughes} 272d423741f91526cada9d081c64ee295ec9863032dElliott Hughes 273ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughesvoid Monitor::FailedUnlock(Object* o, Thread* expected_owner, Thread* found_owner, 274ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes Monitor* monitor) { 275ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes Thread* current_owner = NULL; 276ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string current_owner_string; 277ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string expected_owner_string; 278ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string found_owner_string; 279ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes { 280ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // TODO: isn't this too late to prevent threads from disappearing? 281ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Acquire thread list lock so threads won't disappear from under us. 28250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); 283ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Re-read owner now that we hold lock. 284ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner = (monitor != NULL) ? monitor->owner_ : NULL; 285ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Get short descriptions of the threads involved. 286ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string = ThreadToString(current_owner); 287ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string = ThreadToString(expected_owner); 288ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string = ThreadToString(found_owner); 289ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 2906d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (current_owner == NULL) { 2916d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner == NULL) { 2926d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" 2936d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s'", 294ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 295ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 2966d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 2976d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: the original read found an owner but now there is none 2986d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 2996d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (where now the monitor appears unowned) on thread '%s'", 300ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 301ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 302ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3036d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3046d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3056d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner == NULL) { 3066d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally there was no owner, there is now 3076d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3086d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (originally believed to be unowned) on thread '%s'", 309ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 310ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 311ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3126d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3136d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner != current_owner) { 3146d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally found and current owner have changed 3156d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" 3166d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " owned by '%s') on object of type '%s' on thread '%s'", 317ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 318ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 319ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 320ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3216d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3226d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3236d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s", 324ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 325ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 326ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3276d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3286d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3296d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 33200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersbool Monitor::Unlock(Thread* self, bool for_wait) { 3335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 3346d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers Thread* owner = owner_; 3356d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (owner == self) { 3365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We own the monitor, so nobody else can be in here. 3375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock_count_ == 0) { 3385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = NULL; 33933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_ = NULL; 3400399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = 0; 34181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Unlock(self); 3425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes --lock_count_; 3445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 34500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else if (for_wait) { 34600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Wait should have already cleared the fields. 34700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK_EQ(lock_count_, 0); 34800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK(owner == NULL); 34900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK(locking_method_ == NULL); 35000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK_EQ(locking_dex_pc_, 0u); 35181d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Unlock(self); 3525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We don't own this, so we're not allowed to unlock it. 3545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The JNI spec says that we should throw IllegalMonitorStateException 3555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // in this case. 3566d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers FailedUnlock(obj_, self, owner, this); 3575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 3585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return true; 3605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 3635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Wait on a monitor until timeout, interrupt, or notification. Used for 3645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join(). 3655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * If another thread calls Thread.interrupt(), we throw InterruptedException 3675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and return immediately if one of the following are true: 3685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in wait(), wait(long), or wait(long, int) methods of Object 3695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in join(), join(long), or join(long, int) methods of Thread 3705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in sleep(long), or sleep(long, int) methods of Thread 3715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Otherwise, we set the "interrupted" flag. 3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Checks to make sure that "ns" is in the range 0-999999 3745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (i.e. fractions of a millisecond) and throws the appropriate 3755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * exception if it isn't. 3765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The spec allows "spurious wakeups", and recommends that all code using 3785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() do so in a loop. This appears to derive from concerns 3795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * about pthread_cond_wait() on multiprocessor systems. Some commentary 3805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * on the web casts doubt on whether these can/should occur. 3815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Since we're allowed to wake up "early", we clamp extremely long durations 3835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * to return at the end of the 32-bit time epoch. 3845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 3854cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::Wait(Thread* self, int64_t ms, int32_t ns, 3864cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 3875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 3884cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); 3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 3915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 3926d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 3935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 3945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 39550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers monitor_lock_.AssertHeld(self); 3964cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes 397df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes // We need to turn a zero-length timed wait into a regular wait because 398df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). 399df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes if (why == kTimedWaiting && (ms == 0 && ns == 0)) { 400df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes why = kWaiting; 401df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes } 402df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes 4034cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes WaitWithLock(self, ms, ns, interruptShouldThrow, why); 40400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 4055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4064cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::WaitWithLock(Thread* self, int64_t ms, int32_t ns, 4074cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 4085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Enforce the timeout range. 4095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (ms < 0 || ns < 0 || ns > 999999) { 4105cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes Thread::Current()->ThrowNewExceptionF("Ljava/lang/IllegalArgumentException;", 4115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes "timeout arguments out of range: ms=%lld ns=%d", ms, ns); 4125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 4135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Add ourselves to the set of threads waiting on this monitor, and 4175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * release our hold. We need to let it go even if we're a few levels 4185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * deep in a recursive lock, and we need to restore that later. 4195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 4205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We append to the wait set ahead of clearing the count and owner 4215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * fields so the subroutine can check that the calling thread owns 4225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * the monitor. Aside from that, the order of member updates is 4235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * not order sensitive as we hold the pthread mutex. 4245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes AppendToWaitSet(self); 4260399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers int prev_lock_count = lock_count_; 4275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_ = 0; 4285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = NULL; 42966f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier const AbstractMethod* saved_method = locking_method_; 43033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_ = NULL; 4310399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uintptr_t saved_dex_pc = locking_dex_pc_; 4320399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = 0; 4335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4354cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes * Update thread state. If the GC wakes up, it'll ignore us, knowing 4365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * that we won't touch any references in this state, and we'll check 4375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * our suspend mode before we transition out. 4385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4394cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes self->TransitionFromRunnableToSuspended(why); 4405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 441b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes bool was_interrupted = false; 44200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 44300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock. 44450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(self, *self->wait_mutex_); 4455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 44600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is 44700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it 44800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // up. 44900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK(self->wait_monitor_ == NULL); 45000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->wait_monitor_ = this; 4515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 45200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release the monitor lock. 45300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Unlock(self, true); 45400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 455b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // Handle the case where the thread was interrupted before we called wait(). 45600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (self->interrupted_) { 457b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes was_interrupted = true; 45800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else { 45900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Wait for a notification or a timeout to occur. 4604cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes if (why == kWaiting) { 461c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers self->wait_cond_->Wait(self); 46200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else { 4634cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes DCHECK(why == kTimedWaiting || why == kSleeping) << why; 464c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers self->wait_cond_->TimedWait(self, ms, ns); 46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (self->interrupted_) { 467b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes was_interrupted = true; 46800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->interrupted_ = false; 47000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 4715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 47300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Set self->status back to kRunnable, and self-suspend if needed. 47400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromSuspendedToRunnable(); 4755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 476b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes { 477b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // We reset the thread's wait_monitor_ field after transitioning back to runnable so 478b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging 479b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads 480b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // are waiting on "null".) 481b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes MutexLock mu(self, *self->wait_mutex_); 482b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes DCHECK(self->wait_monitor_ != NULL); 483b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes self->wait_monitor_ = NULL; 484b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } 485b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes 48600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Re-acquire the monitor lock. 48700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Lock(self); 4885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 48981d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self->wait_mutex_->AssertNotHeld(self); 4905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We remove our thread from wait set after restoring the count 4935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and owner fields so the subroutine can check that the calling 4945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * thread owns the monitor. Aside from that, the order of member 4955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * updates is not order sensitive as we hold the pthread mutex. 4965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = self; 4980399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers lock_count_ = prev_lock_count; 4990399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_method_ = saved_method; 5000399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = saved_dex_pc; 5015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes RemoveFromWaitSet(self); 5025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 503b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (was_interrupted) { 5045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 5055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We were interrupted while waiting, or somebody interrupted an 5065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * un-interruptible thread earlier and we're bailing out immediately. 5075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 5085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The doc sayeth: "The interrupted status of the current thread is 5095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * cleared when this exception is thrown." 5105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 51100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 51250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(self, *self->wait_mutex_); 51300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->interrupted_ = false; 51400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 5155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (interruptShouldThrow) { 5165cb5ad27944efb08d4556b3c0d362302e37e832bElliott Hughes Thread::Current()->ThrowNewException("Ljava/lang/InterruptedException;", NULL); 5175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self) { 5225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 5245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 5256d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 5265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 52850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers monitor_lock_.AssertHeld(self); 52950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers NotifyWithLock(self); 53000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 53100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 53250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogersvoid Monitor::NotifyWithLock(Thread* self) { 5335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal the first waiting thread in the wait set. 5345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (wait_set_ != NULL) { 5355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 5365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 5375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 5385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Check to see if the thread is still waiting. 54050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(self, *thread->wait_mutex_); 5415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (thread->wait_monitor_ != NULL) { 542c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers thread->wait_cond_->Signal(self); 5435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self) { 5495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 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 } 55550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers monitor_lock_.AssertHeld(self); 55600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers NotifyAllWithLock(); 55700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 55800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 55900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Monitor::NotifyAllWithLock() { 5605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal all threads in the wait set. 5615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (wait_set_ != NULL) { 5625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 5635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 5645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 5655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->Notify(); 5665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 5705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Changes the shape of a monitor from thin to fat, preserving the 5715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * internal lock state. The calling thread must own the lock. 5725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 5735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Inflate(Thread* self, Object* obj) { 5745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj != NULL); 5765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(LW_SHAPE(*obj->GetRawLockWordAddress()), LW_SHAPE_THIN); 577f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes DCHECK_EQ(LW_LOCK_OWNER(*obj->GetRawLockWordAddress()), static_cast<int32_t>(self->GetThinLockId())); 5785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Allocate and acquire a new monitor. 58000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Monitor* m = new Monitor(self, obj); 5814dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << "monitor: thread " << self->GetThinLockId() 5824dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes << " created monitor " << m << " for object " << obj; 583c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes Runtime::Current()->GetMonitorList()->Add(m); 5845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::MonitorEnter(Thread* self, Object* obj) { 5875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 5887b9d996e4cd7d154bb1a244d67139aff0c363cf2Elliott Hughes timespec tm; 589398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t sleepDelayNs; 590398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t minSleepDelayNs = 1000000; /* 1 millisecond */ 591398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t maxSleepDelayNs = 1000000000; /* 1 second */ 592f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes uint32_t thin, newThin; 5935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5944681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes DCHECK(self != NULL); 5954681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes DCHECK(obj != NULL); 596f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes uint32_t threadId = self->GetThinLockId(); 597a21039c3ae2b20e44ceb2735251c04d0aac89afdElliott Hughes retry: 5985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin = *thinp; 5995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 6005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is a thin lock. The owner field is used to 6025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * determine the acquire method, ordered by cost. 6035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_OWNER(thin) == threadId) { 6055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The calling thread owns the lock. Increment the 6075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * value of the recursion count field. 6085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *thinp += 1 << LW_LOCK_COUNT_SHIFT; 6105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_COUNT(*thinp) == LW_LOCK_COUNT_MASK) { 6115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The reacquisition limit has been reached. Inflate 6135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * the lock so the next acquire will not overflow the 6145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * recursion count field. 6155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 6175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else if (LW_LOCK_OWNER(thin) == 0) { 61981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The lock is unowned. Install the thread id of the calling thread into the owner field. 62081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // This is the common case: compiled code will have tried this before calling back into 62181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // the runtime. 6225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); 6235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (android_atomic_acquire_cas(thin, newThin, thinp) != 0) { 6245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The acquire failed. Try again. 6255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes goto retry; 6265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6284dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p (a %s) owned by %d", 62981ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes threadId, thinp, PrettyTypeOf(obj).c_str(), LW_LOCK_OWNER(thin)); 63081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The lock is owned by another thread. Notify the runtime that we are about to wait. 6318e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = obj; 63200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromRunnableToSuspended(kBlocked); 6335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Spin until the thin lock is released or inflated. 6345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = 0; 6355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes for (;;) { 6365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin = *thinp; 6375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Check the shape of the lock word. Another thread 6385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // may have inflated the lock while we were waiting. 6395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 6405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_OWNER(thin) == 0) { 6415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock has been released. Install the thread id of the 6425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // calling thread into the owner field. 6435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); 6445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (android_atomic_acquire_cas(thin, newThin, thinp) == 0) { 6455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The acquire succeed. Break out of the loop and proceed to inflate the lock. 6465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes break; 6475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock has not been released. Yield so the owning thread can run. 6505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (sleepDelayNs == 0) { 6515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sched_yield(); 6525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = minSleepDelayNs; 6535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes tm.tv_sec = 0; 6555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes tm.tv_nsec = sleepDelayNs; 6565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes nanosleep(&tm, NULL); 6575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Prepare the next delay value. Wrap to avoid once a second polls for eternity. 6585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (sleepDelayNs < maxSleepDelayNs / 2) { 6595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs *= 2; 6605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = minSleepDelayNs; 6625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 66681ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The thin lock was inflated by another thread. Let the runtime know we are no longer 6675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting and try again. 668398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d found lock %p surprise-fattened by another thread", threadId, thinp); 6698e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = NULL; 67000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromSuspendedToRunnable(); 6715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes goto retry; 6725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6744dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p done", threadId, thinp); 67581ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // We have acquired the thin lock. Let the runtime know that we are no longer waiting. 6768e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = NULL; 67700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromSuspendedToRunnable(); 6785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Fatten the lock. 6795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 6804dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p", threadId, thinp); 6815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock is a fat lock. 6844dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d locking fat lock %p (%p) %p on a %s", 685398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes threadId, thinp, LW_MONITOR(*thinp), 686398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes reinterpret_cast<void*>(*thinp), PrettyTypeOf(obj).c_str()); 6875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(LW_MONITOR(*thinp) != NULL); 6885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(*thinp)->Lock(self); 6895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 6915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 6925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesbool Monitor::MonitorExit(Thread* self, Object* obj) { 6935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 6945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 6955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 69634e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes //DCHECK_EQ(self->GetState(), kRunnable); 6975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj != NULL); 6985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 6995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Cache the lock word as its value can change while we are 7015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * examining its state. 7025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *thinp; 7045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is thin. We must ensure that the lock is owned 7075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * by the given thread before unlocking it. 7085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 709f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) == self->GetThinLockId()) { 7105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We are the lock owner. It is safe to update the lock 7125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * without CAS as lock ownership guards the lock itself. 7135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_COUNT(thin) == 0) { 7155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock was not recursively acquired, the common 7175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * case. Unlock by clearing all bits except for the 7185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * hash state. 7195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT); 7215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes android_atomic_release_store(thin, thinp); 7225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The object was recursively acquired. Decrement the 7255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock recursion count field. 7265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *thinp -= 1 << LW_LOCK_COUNT_SHIFT; 7285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We do not own the lock. The JVM spec requires that we 7325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * throw an exception in this case. 7335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7346d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers FailedUnlock(obj, self, NULL, NULL); 7355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 7365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is fat. We must check to see if Unlock has 7405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * raised any exceptions before continuing. 7415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(LW_MONITOR(*thinp) != NULL); 74300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!LW_MONITOR(*thinp)->Unlock(self, false)) { 7445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // An exception has been raised. Do not fall through. 7455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 7465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return true; 7495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 7525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait(). Also called for class init. 7535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7544cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::Wait(Thread* self, Object *obj, int64_t ms, int32_t ns, 7554cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 7565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 7575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, we need to fatten it. 7595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *thinp; 7605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 762f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 7636d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 7645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 7655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* This thread holds the lock. We need to fatten the lock 7685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * so 'self' can block on it. Don't update the object lock 7695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * field yet, because 'self' needs to acquire the lock before 7705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * any other thread gets a chance. 7715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 7734dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p by wait()", self->GetThinLockId(), thinp); 7745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7754cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes LW_MONITOR(*thinp)->Wait(self, ms, ns, interruptShouldThrow, why); 7765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self, Object *obj) { 7795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *obj->GetRawLockWordAddress(); 7805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, there aren't any waiters; 7825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting on an object forces lock fattening. 7835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 785f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 7866d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 7875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 7885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // no-op; there are no waiters to notify. 79000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Inflate(self, obj); 7915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // It's a fat lock. 7935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(thin)->Notify(self); 7945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self, Object *obj) { 7985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *obj->GetRawLockWordAddress(); 7995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 8005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, there aren't any waiters; 8015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting on an object forces lock fattening. 8025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 8035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 804f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 8056d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); 8065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 8075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // no-op; there are no waiters to notify. 80900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Inflate(self, obj); 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) { 82600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers ThreadState state; 82750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers state = thread->GetState(); 8288e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8298e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes Object* object = NULL; 8308e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uint32_t lock_owner = ThreadList::kInvalidId; 831b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { 832b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (state == kSleeping) { 833b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes os << " - sleeping on "; 834b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } else { 835b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes os << " - waiting on "; 836b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } 83700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 83850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *thread->wait_mutex_); 839b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes Monitor* monitor = thread->wait_monitor_; 840b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (monitor != NULL) { 841b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes object = monitor->obj_; 842b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } 8438e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 84434e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes } else if (state == kBlocked) { 8458e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " - waiting to lock "; 8468e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes object = thread->monitor_enter_object_; 8478e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (object != NULL) { 84824a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom lock_owner = object->GetThinLockId(); 8498e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8508e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } else { 8518e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // We're not waiting on anything. 8528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return; 8538e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8548e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) 85608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes os << "<" << object << "> (a " << PrettyTypeOf(object) << ")"; 8578e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 858c5dc2ff2d1beb3ff84f93d2c7ebe07c46b76956aElliott Hughes // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 8598e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (lock_owner != ThreadList::kInvalidId) { 8608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " held by thread " << lock_owner; 8618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8628e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << "\n"; 8648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 8658e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 86600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void DumpLockedObject(std::ostream& os, Object* o) 867b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 86808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes os << " - locked <" << o << "> (a " << PrettyTypeOf(o) << ")\n"; 86908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes} 87008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 87108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughesvoid Monitor::DescribeLocks(std::ostream& os, StackVisitor* stack_visitor) { 87266f19258f9728d4ffe026074d8fd429d639802faMathieu Chartier AbstractMethod* m = stack_visitor->GetMethod(); 87308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes CHECK(m != NULL); 87408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 87508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Native methods are an easy special case. 87608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. 87708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (m->IsNative()) { 87808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (m->IsSynchronized()) { 87908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes Object* jni_this = stack_visitor->GetCurrentSirt()->GetReference(0); 88008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes DumpLockedObject(os, jni_this); 88108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 88208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes return; 88308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 88408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 88561f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao // Proxy methods should not be synchronized. 88661f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao if (m->IsProxyMethod()) { 88761f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao CHECK(!m->IsSynchronized()); 88861f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao return; 88961f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao } 89061f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao 89108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // <clinit> is another special case. The runtime holds the class lock while calling <clinit>. 89208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes MethodHelper mh(m); 89308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (mh.IsClassInitializer()) { 89408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes DumpLockedObject(os, m->GetDeclaringClass()); 89508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Fall through because there might be synchronization in the user code too. 89608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 89708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 89808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Is there any reason to believe there's any synchronization in this method? 89908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes const DexFile::CodeItem* code_item = mh.GetCodeItem(); 900caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes CHECK(code_item != NULL) << PrettyMethod(m); 90108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (code_item->tries_size_ == 0) { 90208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes return; // No "tries" implies no synchronization, so no held locks to report. 90308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 90408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 90580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to 90680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // the locks held in this stack frame. 90780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes std::vector<uint32_t> monitor_enter_dex_pcs; 90880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), monitor_enter_dex_pcs); 90980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes if (monitor_enter_dex_pcs.empty()) { 91080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes return; 91180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes } 91208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 91380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // Verification is an iterative process, so it can visit the same monitor-enter instruction 91480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // repeatedly with increasingly accurate type information. We don't want duplicates. 91580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // TODO: is this fixed if we share the other std::vector-returning verifier code? 91680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes STLSortAndRemoveDuplicates(&monitor_enter_dex_pcs); 91780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes 91880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) { 91980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // The verifier works in terms of the dex pcs of the monitor-enter instructions. 92080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // We want the registers used by those instructions (so we can read the values out of them). 92180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes uint32_t dex_pc = monitor_enter_dex_pcs[i]; 92280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes uint16_t monitor_enter_instruction = code_item->insns_[dex_pc]; 92308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 92480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // Quick sanity check. 92580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) { 92680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was " 92780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes << reinterpret_cast<void*>(monitor_enter_instruction); 92808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 92980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes 93080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff); 93180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes Object* o = reinterpret_cast<Object*>(stack_visitor->GetVReg(m, monitor_register, 93280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes kReferenceVReg)); 93380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes DumpLockedObject(os, o); 93408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 93508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes} 93608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 93766f19258f9728d4ffe026074d8fd429d639802faMathieu Chartiervoid Monitor::TranslateLocation(const AbstractMethod* method, uint32_t dex_pc, 93833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const char*& source_file, uint32_t& line_number) const { 93933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao // If method is null, location is unknown 94033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao if (method == NULL) { 94112c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes source_file = ""; 94233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao line_number = 0; 94333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao return; 94433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao } 9456d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh(method); 9466d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_file = mh.GetDeclaringClassSourceFile(); 94712c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes if (source_file == NULL) { 94812c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes source_file = ""; 94912c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes } 9500399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers line_number = mh.GetLineNumFromDexPC(dex_pc); 95133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao} 95233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 95300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan RogersMonitorList::MonitorList() : monitor_list_lock_("MonitorList lock") { 954c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 955c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 956c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::~MonitorList() { 95750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), monitor_list_lock_); 958c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes STLDeleteElements(&list_); 959c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 960c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 961c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughesvoid MonitorList::Add(Monitor* m) { 96250b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), monitor_list_lock_); 963c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes list_.push_front(m); 964c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 965c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 966c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughesvoid MonitorList::SweepMonitorList(Heap::IsMarkedTester is_marked, void* arg) { 96750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), monitor_list_lock_); 968c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes typedef std::list<Monitor*>::iterator It; // TODO: C++0x auto 969c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes It it = list_.begin(); 970c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes while (it != list_.end()) { 971c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes Monitor* m = *it; 972c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes if (!is_marked(m->GetObject(), arg)) { 9734dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " << m->GetObject(); 974c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes delete m; 975c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes it = list_.erase(it); 976c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } else { 977c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes ++it; 978c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 979c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 980c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 981c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 982f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott HughesMonitorInfo::MonitorInfo(Object* o) : owner(NULL), entry_count(0) { 983f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes uint32_t lock_word = *o->GetRawLockWordAddress(); 984f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes if (LW_SHAPE(lock_word) == LW_SHAPE_THIN) { 985f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes uint32_t owner_thin_lock_id = LW_LOCK_OWNER(lock_word); 986f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes if (owner_thin_lock_id != 0) { 987f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes owner = Runtime::Current()->GetThreadList()->FindThreadByThinLockId(owner_thin_lock_id); 988f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes entry_count = 1 + LW_LOCK_COUNT(lock_word); 989f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 990f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes // Thin locks have no waiters. 991f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } else { 992f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes CHECK_EQ(LW_SHAPE(lock_word), LW_SHAPE_FAT); 993f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes Monitor* monitor = LW_MONITOR(lock_word); 994f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes owner = monitor->owner_; 995f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes entry_count = 1 + monitor->lock_count_; 996f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes for (Thread* waiter = monitor->wait_set_; waiter != NULL; waiter = waiter->wait_next_) { 997f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes waiters.push_back(waiter); 998f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 999f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 1000f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes} 1001f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes 10025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} // namespace art 1003