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" 244f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h" 2508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "dex_instruction.h" 26ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom#include "mirror/art_method-inl.h" 274f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "mirror/class-inl.h" 2805f3057d6a4d23d712092ccd36a531590bff323bIan Rogers#include "mirror/object-inl.h" 292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h" 306d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers#include "object_utils.h" 3100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers#include "scoped_thread_state_change.h" 325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes#include "thread.h" 338e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes#include "thread_list.h" 3408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include "verifier/method_verifier.h" 35044288fb8f69e154862763a6b3bfea2bac1b397eElliott Hughes#include "well_known_classes.h" 365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesnamespace art { 385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Every Object has a monitor associated with it, but not every Object is 415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * actually locked. Even the ones that are locked do not need a 425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * full-fledged monitor until a) there is actual contention or b) wait() 435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * is called on the Object. 445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * For Android, we have implemented a scheme similar to the one described 465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * in Bacon et al.'s "Thin locks: featherweight synchronization for Java" 475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (ACM 1998). Things are even easier for us, though, because we have 485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * a full 32 bits to work with. 495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The two states of an Object's lock are referred to as "thin" and 515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * "fat". A lock may transition from the "thin" state to the "fat" 525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * state and this transition is referred to as inflation. Once a lock 535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * has been inflated it remains in the "fat" state indefinitely. 545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock value itself is stored in Object.lock. The LSB of the 565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock encodes its state. When cleared, the lock is in the "thin" 575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * state and its bits are formatted as follows: 585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * [31 ---- 19] [18 ---- 3] [2 ---- 1] [0] 605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock count thread id hash state 0 615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * When set, the lock is in the "fat" state and its bits are formatted 635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * as follows: 645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * [31 ---- 3] [2 ---- 1] [0] 665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * pointer hash state 1 675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * For an in-depth description of the mechanics of thin-vs-fat locking, 695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * read the paper referred to above. 7054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * 715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Monitors provide: 725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - mutually exclusive access to resources 735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - a way for multiple threads to wait for notification 745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * In effect, they fill the role of both mutexes and condition variables. 765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Only one thread can own the monitor at any time. There may be several 785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * threads waiting on it (the wait call unlocks it). One or more waiting 795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * threads may be getting interrupted or notified at any given time. 805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * TODO: the various members of monitor are not SMP-safe. 825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 8354e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 84f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes// The shape is the bottom bit; either LW_SHAPE_THIN or LW_SHAPE_FAT. 85f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes#define LW_SHAPE_MASK 0x1 86f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes#define LW_SHAPE(x) static_cast<int>((x) & LW_SHAPE_MASK) 8754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 8854e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes/* 8954e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * Monitor accessor. Extracts a monitor structure pointer from a fat 9054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * lock. Performs no error checking. 9154e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes */ 9254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_MONITOR(x) \ 93398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes (reinterpret_cast<Monitor*>((x) & ~((LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT) | LW_SHAPE_MASK))) 9454e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 9554e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes/* 9654e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * Lock recursion count field. Contains a count of the number of times 9754e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes * a lock has been recursively acquired. 9854e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes */ 9954e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_LOCK_COUNT_MASK 0x1fff 10054e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_LOCK_COUNT_SHIFT 19 10154e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes#define LW_LOCK_COUNT(x) (((x) >> LW_LOCK_COUNT_SHIFT) & LW_LOCK_COUNT_MASK) 10254e7df1896a4066cbb9fe6f72249829f0b8c49c6Elliott Hughes 103fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool (*Monitor::is_sensitive_thread_hook_)() = NULL; 104fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesuint32_t Monitor::lock_profiling_threshold_ = 0; 10532d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes 106fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughesbool Monitor::IsSensitiveThread() { 107fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (is_sensitive_thread_hook_ != NULL) { 108fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return (*is_sensitive_thread_hook_)(); 109fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 110fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes return false; 111fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes} 112fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 1134dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughesvoid Monitor::Init(uint32_t lock_profiling_threshold, bool (*is_sensitive_thread_hook)()) { 114fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes lock_profiling_threshold_ = lock_profiling_threshold; 115fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes is_sensitive_thread_hook_ = is_sensitive_thread_hook; 11632d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes} 11732d6e1e5654433d7eadede89e1c770b2c839aee9Elliott Hughes 1182dd0e2cea360bc9206eb88ecc40d259e796c239dIan RogersMonitor::Monitor(Thread* owner, mirror::Object* obj) 11900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers : monitor_lock_("a monitor lock", kMonitorLock), 12000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers owner_(owner), 1215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_(0), 1225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes obj_(obj), 1235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_(NULL), 12433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_(NULL), 1250399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_(0) { 12681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Lock(owner); 12700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Propagate the lock state. 12800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers uint32_t thin = *obj->GetRawLockWordAddress(); 12900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers lock_count_ = LW_LOCK_COUNT(thin); 13000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers thin &= LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT; 13100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers thin |= reinterpret_cast<uint32_t>(this) | LW_SHAPE_FAT; 13200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Publish the updated lock word. 13300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers android_atomic_release_store(thin, obj->GetRawLockWordAddress()); 13400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Lock profiling. 13500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (lock_profiling_threshold_ != 0) { 13600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers locking_method_ = owner->GetCurrentMethod(&locking_dex_pc_); 13700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 1385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1405f79133a435ebcb20000370d56046fe01201dd80Elliott HughesMonitor::~Monitor() { 1415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj_ != NULL); 1425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(LW_SHAPE(*obj_->GetRawLockWordAddress()), LW_SHAPE_FAT); 1435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 1465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Links a thread into a monitor's wait set. The monitor lock must be 1475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * held by the caller of this routine. 1485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 1495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::AppendToWaitSet(Thread* thread) { 1505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(thread != NULL); 152dc33ad5db2dc6ed9b76d5219888626a604debbe1Elliott Hughes DCHECK(thread->wait_next_ == NULL) << thread->wait_next_; 1535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == NULL) { 1545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread; 1555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // push_back. 1595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 1605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (t->wait_next_ != NULL) { 1615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t = t->wait_next_; 1625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t->wait_next_ = thread; 1645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 1675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Unlinks a thread from a monitor's wait set. The monitor lock must 1685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * be held by the caller of this routine. 1695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 1705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::RemoveFromWaitSet(Thread *thread) { 1715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(owner_ == Thread::Current()); 1725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(thread != NULL); 1735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == NULL) { 1745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (wait_set_ == thread) { 1775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 1785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 1795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* t = wait_set_; 1835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (t->wait_next_ != NULL) { 1845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (t->wait_next_ == thread) { 1855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t->wait_next_ = thread->wait_next_; 1865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 1875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 1885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes t = t->wait_next_; 1905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 1915f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Object* Monitor::GetObject() { 194c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes return obj_; 1955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 1965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 1975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Lock(Thread* self) { 1985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ == self) { 1995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_++; 2005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 2015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 202fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 20381d425b0b232962441616f8b14f73620bffef5e5Ian Rogers if (!monitor_lock_.TryLock(self)) { 2042542d6690824d27e5894a635b40237ee91a412caMathieu Chartier uint64_t waitStart = 0; 2052542d6690824d27e5894a635b40237ee91a412caMathieu Chartier uint64_t waitEnd = 0; 206fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint32_t wait_threshold = lock_profiling_threshold_; 207ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom const mirror::ArtMethod* current_locking_method = NULL; 2080399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uint32_t current_locking_dex_pc = 0; 2095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes { 21034e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes ScopedThreadStateChange tsc(self, kBlocked); 211fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 212fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes waitStart = NanoTime() / 1000; 213fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 21433dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao current_locking_method = locking_method_; 2150399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers current_locking_dex_pc = locking_dex_pc_; 2165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 21781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Lock(self); 218fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 219fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes waitEnd = NanoTime() / 1000; 220fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 221fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 222fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes 223fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_threshold != 0) { 224fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint64_t wait_ms = (waitEnd - waitStart) / 1000; 225fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes uint32_t sample_percent; 226fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (wait_ms >= wait_threshold) { 227fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes sample_percent = 100; 228fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } else { 229fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes sample_percent = 100 * wait_ms / wait_threshold; 230fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 231fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (sample_percent != 0 && (static_cast<uint32_t>(rand() % 100) < sample_percent)) { 23233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const char* current_locking_filename; 23333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao uint32_t current_locking_line_number; 2340399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers TranslateLocation(current_locking_method, current_locking_dex_pc, 23533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao current_locking_filename, current_locking_line_number); 23633dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao LogContentionEvent(self, wait_ms, sample_percent, current_locking_filename, current_locking_line_number); 237fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 2405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = self; 2415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(lock_count_, 0); 2425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // When debugging, save the current monitor holder for future 2445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // acquisition failures to use in sampled logging. 245fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes if (lock_profiling_threshold_ != 0) { 2460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_method_ = self->GetCurrentMethod(&locking_dex_pc_); 247fc86162ce2a3467acb690e18cc8bd9b3daafc606Elliott Hughes } 2485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 2495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 2506d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) 2516d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers __attribute__((format(printf, 1, 2))); 2526d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 25300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersstatic void ThrowIllegalMonitorStateExceptionF(const char* fmt, ...) 254b726dcb581bf72da46527378ccb6889020f0e6e9Ian Rogers SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 2556d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_list args; 2566d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_start(args, fmt); 25762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers Thread* self = Thread::Current(); 25862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = self->GetCurrentLocationForThrow(); 25962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers self->ThrowNewExceptionV(throw_location, "Ljava/lang/IllegalMonitorStateException;", fmt, args); 26064277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom if (!Runtime::Current()->IsStarted()) { 26164277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom std::ostringstream ss; 26262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers self->Dump(ss); 26364277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom std::string str(ss.str()); 26464277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom LOG(ERROR) << "IllegalMonitorStateException: " << str; 26564277f38032208a0c1081ff9e466c04009b96383Brian Carlstrom } 2666d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers va_end(args); 2676d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers} 2686d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers 269d423741f91526cada9d081c64ee295ec9863032dElliott Hughesstatic std::string ThreadToString(Thread* thread) { 270d423741f91526cada9d081c64ee295ec9863032dElliott Hughes if (thread == NULL) { 271d423741f91526cada9d081c64ee295ec9863032dElliott Hughes return "NULL"; 272d423741f91526cada9d081c64ee295ec9863032dElliott Hughes } 273d423741f91526cada9d081c64ee295ec9863032dElliott Hughes std::ostringstream oss; 274d423741f91526cada9d081c64ee295ec9863032dElliott Hughes // TODO: alternatively, we could just return the thread's name. 275d423741f91526cada9d081c64ee295ec9863032dElliott Hughes oss << *thread; 276d423741f91526cada9d081c64ee295ec9863032dElliott Hughes return oss.str(); 277d423741f91526cada9d081c64ee295ec9863032dElliott Hughes} 278d423741f91526cada9d081c64ee295ec9863032dElliott Hughes 2792dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::FailedUnlock(mirror::Object* o, Thread* expected_owner, Thread* found_owner, 280ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes Monitor* monitor) { 281ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes Thread* current_owner = NULL; 282ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string current_owner_string; 283ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string expected_owner_string; 284ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes std::string found_owner_string; 285ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes { 286ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // TODO: isn't this too late to prevent threads from disappearing? 287ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Acquire thread list lock so threads won't disappear from under us. 28850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); 289ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Re-read owner now that we hold lock. 290ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner = (monitor != NULL) ? monitor->owner_ : NULL; 291ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes // Get short descriptions of the threads involved. 292ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string = ThreadToString(current_owner); 293ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string = ThreadToString(expected_owner); 294ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string = ThreadToString(found_owner); 295ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes } 2966d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (current_owner == NULL) { 2976d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner == NULL) { 2986d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of unowned monitor on object of type '%s'" 2996d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s'", 300ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 301ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3026d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3036d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: the original read found an owner but now there is none 3046d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3056d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (where now the monitor appears unowned) on thread '%s'", 306ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 307ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 308ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3096d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3106d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3116d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner == NULL) { 3126d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally there was no owner, there is now 3136d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3146d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " (originally believed to be unowned) on thread '%s'", 315ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 316ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 317ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3186d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3196d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (found_owner != current_owner) { 3206d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers // Race: originally found and current owner have changed 3216d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor originally owned by '%s' (now" 3226d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " owned by '%s') on object of type '%s' on thread '%s'", 323ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes found_owner_string.c_str(), 324ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 325ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 326ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3276d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } else { 3286d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("unlock of monitor owned by '%s' on object of type '%s'" 3296d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers " on thread '%s", 330ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes current_owner_string.c_str(), 331ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes PrettyTypeOf(o).c_str(), 332ffb465f23d9549dd591e6aa62e9250523cb00233Elliott Hughes expected_owner_string.c_str()); 3336d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3346d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3356d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers } 3365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 33800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersbool Monitor::Unlock(Thread* self, bool for_wait) { 3395f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 3406d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers Thread* owner = owner_; 3416d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers if (owner == self) { 3425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We own the monitor, so nobody else can be in here. 3435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (lock_count_ == 0) { 3445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = NULL; 34533dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_ = NULL; 3460399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = 0; 34781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Unlock(self); 3485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes --lock_count_; 3505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 35100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else if (for_wait) { 35200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Wait should have already cleared the fields. 35300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK_EQ(lock_count_, 0); 35400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK(owner == NULL); 35500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK(locking_method_ == NULL); 35600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK_EQ(locking_dex_pc_, 0u); 35781d425b0b232962441616f8b14f73620bffef5e5Ian Rogers monitor_lock_.Unlock(self); 3585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 3595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // We don't own this, so we're not allowed to unlock it. 3605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The JNI spec says that we should throw IllegalMonitorStateException 3615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // in this case. 3626d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers FailedUnlock(obj_, self, owner, this); 3635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 3645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 3655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return true; 3665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 3675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 3695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Wait on a monitor until timeout, interrupt, or notification. Used for 3705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() and (somewhat indirectly) Thread.sleep() and Thread.join(). 3715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * If another thread calls Thread.interrupt(), we throw InterruptedException 3735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and return immediately if one of the following are true: 3745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in wait(), wait(long), or wait(long, int) methods of Object 3755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in join(), join(long), or join(long, int) methods of Thread 3765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * - blocked in sleep(long), or sleep(long, int) methods of Thread 3775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Otherwise, we set the "interrupted" flag. 3785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Checks to make sure that "ns" is in the range 0-999999 3805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * (i.e. fractions of a millisecond) and throws the appropriate 3815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * exception if it isn't. 3825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The spec allows "spurious wakeups", and recommends that all code using 3845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait() do so in a loop. This appears to derive from concerns 3855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * about pthread_cond_wait() on multiprocessor systems. Some commentary 3865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * on the web casts doubt on whether these can/should occur. 3875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 3885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Since we're allowed to wake up "early", we clamp extremely long durations 3895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * to return at the end of the 32-bit time epoch. 3905f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 3914cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::Wait(Thread* self, int64_t ms, int32_t ns, 3924cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 3935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 3944cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes DCHECK(why == kTimedWaiting || why == kWaiting || why == kSleeping); 3955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 3965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 3975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 3986d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 3995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 4005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 40150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers monitor_lock_.AssertHeld(self); 4024cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes 403df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes // We need to turn a zero-length timed wait into a regular wait because 404df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes // Object.wait(0, 0) is defined as Object.wait(0), which is defined as Object.wait(). 405df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes if (why == kTimedWaiting && (ms == 0 && ns == 0)) { 406df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes why = kWaiting; 407df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes } 408df42c4815c30b9df15aacb88070c1e94f41d0226Elliott Hughes 4094cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes WaitWithLock(self, ms, ns, interruptShouldThrow, why); 41000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 4115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4124cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughesvoid Monitor::WaitWithLock(Thread* self, int64_t ms, int32_t ns, 4134cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 4145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Enforce the timeout range. 4155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (ms < 0 || ns < 0 || ns > 999999) { 41662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = self->GetCurrentLocationForThrow(); 41762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers self->ThrowNewExceptionF(throw_location, "Ljava/lang/IllegalArgumentException;", 41862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers "timeout arguments out of range: ms=%lld ns=%d", ms, ns); 4195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 4205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Add ourselves to the set of threads waiting on this monitor, and 4245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * release our hold. We need to let it go even if we're a few levels 4255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * deep in a recursive lock, and we need to restore that later. 4265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 4275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We append to the wait set ahead of clearing the count and owner 4285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * fields so the subroutine can check that the calling thread owns 4295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * the monitor. Aside from that, the order of member updates is 4305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * not order sensitive as we hold the pthread mutex. 4315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes AppendToWaitSet(self); 4330399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers int prev_lock_count = lock_count_; 4345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes lock_count_ = 0; 4355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = NULL; 436ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom const mirror::ArtMethod* saved_method = locking_method_; 43733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao locking_method_ = NULL; 4380399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers uintptr_t saved_dex_pc = locking_dex_pc_; 4390399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = 0; 4405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4424cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes * Update thread state. If the GC wakes up, it'll ignore us, knowing 4435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * that we won't touch any references in this state, and we'll check 4445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * our suspend mode before we transition out. 4455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 4464cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes self->TransitionFromRunnableToSuspended(why); 4475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 448b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes bool was_interrupted = false; 44900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 45000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Pseudo-atomically wait on self's wait_cond_ and release the monitor lock. 45150b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(self, *self->wait_mutex_); 4525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 45300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Set wait_monitor_ to the monitor object we will be waiting on. When wait_monitor_ is 45400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // non-NULL a notifying or interrupting thread must signal the thread's wait_cond_ to wake it 45500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // up. 45600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers DCHECK(self->wait_monitor_ == NULL); 45700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->wait_monitor_ = this; 4585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 45900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Release the monitor lock. 46000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Unlock(self, true); 46100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 462b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // Handle the case where the thread was interrupted before we called wait(). 46300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (self->interrupted_) { 464b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes was_interrupted = true; 46500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else { 46600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Wait for a notification or a timeout to occur. 4674cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes if (why == kWaiting) { 468c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers self->wait_cond_->Wait(self); 46900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } else { 4704cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes DCHECK(why == kTimedWaiting || why == kSleeping) << why; 471c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers self->wait_cond_->TimedWait(self, ms, ns); 47200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 47300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (self->interrupted_) { 474b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes was_interrupted = true; 47500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 47600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->interrupted_ = false; 47700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 4785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 4795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 48000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Set self->status back to kRunnable, and self-suspend if needed. 48100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromSuspendedToRunnable(); 4825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 483b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes { 484b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // We reset the thread's wait_monitor_ field after transitioning back to runnable so 485b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // that a thread in a waiting/sleeping state has a non-null wait_monitor_ for debugging 486b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // and diagnostic purposes. (If you reset this earlier, stack dumps will claim that threads 487b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes // are waiting on "null".) 488b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes MutexLock mu(self, *self->wait_mutex_); 489b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes DCHECK(self->wait_monitor_ != NULL); 490b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes self->wait_monitor_ = NULL; 491b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } 492b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes 49300f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers // Re-acquire the monitor lock. 49400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Lock(self); 4955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 49681d425b0b232962441616f8b14f73620bffef5e5Ian Rogers self->wait_mutex_->AssertNotHeld(self); 4975f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 4985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 4995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We remove our thread from wait set after restoring the count 5005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * and owner fields so the subroutine can check that the calling 5015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * thread owns the monitor. Aside from that, the order of member 5025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * updates is not order sensitive as we hold the pthread mutex. 5035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 5045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes owner_ = self; 5050399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers lock_count_ = prev_lock_count; 5060399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_method_ = saved_method; 5070399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers locking_dex_pc_ = saved_dex_pc; 5085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes RemoveFromWaitSet(self); 5095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 510b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (was_interrupted) { 5115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 5125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We were interrupted while waiting, or somebody interrupted an 5135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * un-interruptible thread earlier and we're bailing out immediately. 5145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * 5155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The doc sayeth: "The interrupted status of the current thread is 5165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * cleared when this exception is thrown." 5175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 51800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 51950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(self, *self->wait_mutex_); 52000f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->interrupted_ = false; 52100f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers } 5225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (interruptShouldThrow) { 52362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = self->GetCurrentLocationForThrow(); 52462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers self->ThrowNewException(throw_location, "Ljava/lang/InterruptedException;", NULL); 5255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::Notify(Thread* self) { 5305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 5325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 5336d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 5345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 53650b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers monitor_lock_.AssertHeld(self); 53750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers NotifyWithLock(self); 53800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 53900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 54050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogersvoid Monitor::NotifyWithLock(Thread* self) { 5415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal the first waiting thread in the wait set. 5425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (wait_set_ != NULL) { 5435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 5445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 5455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 5465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Check to see if the thread is still waiting. 54850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(self, *thread->wait_mutex_); 5495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (thread->wait_monitor_ != NULL) { 550c604d731730b43231f63040c8db1d58304da0cf3Ian Rogers thread->wait_cond_->Signal(self); 5515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughesvoid Monitor::NotifyAll(Thread* self) { 5575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that we hold the lock. 5595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (owner_ != self) { 5606d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); 5615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 5625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 56350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers monitor_lock_.AssertHeld(self); 56400f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers NotifyAllWithLock(); 56500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers} 56600f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers 56700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogersvoid Monitor::NotifyAllWithLock() { 5685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Signal all threads in the wait set. 5695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes while (wait_set_ != NULL) { 5705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* thread = wait_set_; 5715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes wait_set_ = thread->wait_next_; 5725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->wait_next_ = NULL; 5735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thread->Notify(); 5745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 5755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 5785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Changes the shape of a monitor from thin to fat, preserving the 5795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * internal lock state. The calling thread must own the lock. 5805f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 5812dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::Inflate(Thread* self, mirror::Object* obj) { 5825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 5835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj != NULL); 5845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK_EQ(LW_SHAPE(*obj->GetRawLockWordAddress()), LW_SHAPE_THIN); 585f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes DCHECK_EQ(LW_LOCK_OWNER(*obj->GetRawLockWordAddress()), static_cast<int32_t>(self->GetThinLockId())); 5865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Allocate and acquire a new monitor. 58800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Monitor* m = new Monitor(self, obj); 5894dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << "monitor: thread " << self->GetThinLockId() 5904dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes << " created monitor " << m << " for object " << obj; 591c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes Runtime::Current()->GetMonitorList()->Add(m); 5925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 5935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 5942dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::MonitorEnter(Thread* self, mirror::Object* obj) { 5955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 596398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t sleepDelayNs; 597398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t minSleepDelayNs = 1000000; /* 1 millisecond */ 598398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes uint32_t maxSleepDelayNs = 1000000000; /* 1 second */ 599f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes uint32_t thin, newThin; 6005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 6014681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes DCHECK(self != NULL); 6024681c809ba35d50fab92c592ce8d2c7f8b2731f7Elliott Hughes DCHECK(obj != NULL); 603f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes uint32_t threadId = self->GetThinLockId(); 604a21039c3ae2b20e44ceb2735251c04d0aac89afdElliott Hughes retry: 6055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin = *thinp; 6065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 6075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is a thin lock. The owner field is used to 6095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * determine the acquire method, ordered by cost. 6105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_OWNER(thin) == threadId) { 6125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The calling thread owns the lock. Increment the 6145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * value of the recursion count field. 6155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *thinp += 1 << LW_LOCK_COUNT_SHIFT; 6175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_COUNT(*thinp) == LW_LOCK_COUNT_MASK) { 6185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 6195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The reacquisition limit has been reached. Inflate 6205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * the lock so the next acquire will not overflow the 6215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * recursion count field. 6225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 6235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 6245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else if (LW_LOCK_OWNER(thin) == 0) { 62681ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The lock is unowned. Install the thread id of the calling thread into the owner field. 62781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // This is the common case: compiled code will have tried this before calling back into 62881ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // the runtime. 6295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); 6305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (android_atomic_acquire_cas(thin, newThin, thinp) != 0) { 6315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The acquire failed. Try again. 6325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes goto retry; 6335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6354dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p (a %s) owned by %d", 63681ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes threadId, thinp, PrettyTypeOf(obj).c_str(), LW_LOCK_OWNER(thin)); 63781ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The lock is owned by another thread. Notify the runtime that we are about to wait. 6388e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = obj; 63900f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromRunnableToSuspended(kBlocked); 6405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Spin until the thin lock is released or inflated. 6415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = 0; 6425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes for (;;) { 6435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin = *thinp; 6445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Check the shape of the lock word. Another thread 6455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // may have inflated the lock while we were waiting. 6465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 6475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_OWNER(thin) == 0) { 6485f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock has been released. Install the thread id of the 6495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // calling thread into the owner field. 6505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes newThin = thin | (threadId << LW_LOCK_OWNER_SHIFT); 6515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (android_atomic_acquire_cas(thin, newThin, thinp) == 0) { 6525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The acquire succeed. Break out of the loop and proceed to inflate the lock. 6535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes break; 6545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock has not been released. Yield so the owning thread can run. 6575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (sleepDelayNs == 0) { 6585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sched_yield(); 6595f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = minSleepDelayNs; 6605f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 66156edc432fa914f7ccfa87ce443e64f5ef475666dIan Rogers NanoSleep(sleepDelayNs); 6625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Prepare the next delay value. Wrap to avoid once a second polls for eternity. 6635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (sleepDelayNs < maxSleepDelayNs / 2) { 6645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs *= 2; 6655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes sleepDelayNs = minSleepDelayNs; 6675f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6685f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 67181ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // The thin lock was inflated by another thread. Let the runtime know we are no longer 6725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting and try again. 673398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d found lock %p surprise-fattened by another thread", threadId, thinp); 6748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = NULL; 67500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromSuspendedToRunnable(); 6765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes goto retry; 6775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6785f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6794dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d spin on lock %p done", threadId, thinp); 68081ff3184e7eb8de4605c7646674ea4f9fa29b5f3Elliott Hughes // We have acquired the thin lock. Let the runtime know that we are no longer waiting. 6818e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes self->monitor_enter_object_ = NULL; 68200f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers self->TransitionFromSuspendedToRunnable(); 6835f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Fatten the lock. 6845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 6854dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p", threadId, thinp); 6865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 6885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // The lock is a fat lock. 6894dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d locking fat lock %p (%p) %p on a %s", 690398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes threadId, thinp, LW_MONITOR(*thinp), 691398f64b5805246765b699839b439e18c0dfbf2eeElliott Hughes reinterpret_cast<void*>(*thinp), PrettyTypeOf(obj).c_str()); 6925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(LW_MONITOR(*thinp) != NULL); 6935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(*thinp)->Lock(self); 6945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 6955f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 6965f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 6972dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersbool Monitor::MonitorExit(Thread* self, mirror::Object* obj) { 6985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 6995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(self != NULL); 7017934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom // DCHECK_EQ(self->GetState(), kRunnable); 7025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(obj != NULL); 7035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7045f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Cache the lock word as its value can change while we are 7065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * examining its state. 7075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *thinp; 7095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7115f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is thin. We must ensure that the lock is owned 7125f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * by the given thread before unlocking it. 7135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 714f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) == self->GetThinLockId()) { 7155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7165f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We are the lock owner. It is safe to update the lock 7175f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * without CAS as lock ownership guards the lock itself. 7185f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_LOCK_COUNT(thin) == 0) { 7205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock was not recursively acquired, the common 7225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * case. Unlock by clearing all bits except for the 7235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * hash state. 7245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7255f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes thin &= (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT); 7265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes android_atomic_release_store(thin, thinp); 7275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The object was recursively acquired. Decrement the 7305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * lock recursion count field. 7315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes *thinp -= 1 << LW_LOCK_COUNT_SHIFT; 7335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7345f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7355f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7365f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * We do not own the lock. The JVM spec requires that we 7375f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * throw an exception in this case. 7385f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7396d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers FailedUnlock(obj, self, NULL, NULL); 7405f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 7415f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7425f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7435f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* 7445f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * The lock is fat. We must check to see if Unlock has 7455f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * raised any exceptions before continuing. 7465f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7475f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes DCHECK(LW_MONITOR(*thinp) != NULL); 74800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers if (!LW_MONITOR(*thinp)->Unlock(self, false)) { 7495f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // An exception has been raised. Do not fall through. 7505f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return false; 7515f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7525f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7535f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return true; 7545f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7555f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7565f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes/* 7575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * Object.wait(). Also called for class init. 7585f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7592dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::Wait(Thread* self, mirror::Object *obj, int64_t ms, int32_t ns, 7604cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes bool interruptShouldThrow, ThreadState why) { 7615f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes volatile int32_t* thinp = obj->GetRawLockWordAddress(); 7625f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7635f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, we need to fatten it. 7645f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *thinp; 7655f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7665f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 767f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 7686d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before wait()"); 7695f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 7705f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7715f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7725f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes /* This thread holds the lock. We need to fatten the lock 7735f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * so 'self' can block on it. Don't update the object lock 7745f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * field yet, because 'self' needs to acquire the lock before 7755f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes * any other thread gets a chance. 7765f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes */ 7775f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Inflate(self, obj); 7784dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << StringPrintf("monitor: thread %d fattened lock %p by wait()", self->GetThinLockId(), thinp); 7795f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7804cd121ef0cb35fced70c7d9de378277be7a727d9Elliott Hughes LW_MONITOR(*thinp)->Wait(self, ms, ns, interruptShouldThrow, why); 7815f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 7825f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7832dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::Notify(Thread* self, mirror::Object *obj) { 7845f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *obj->GetRawLockWordAddress(); 7855f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 7865f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, there aren't any waiters; 7875f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting on an object forces lock fattening. 7885f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 7895f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 790f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 7916d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notify()"); 7925f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 7935f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 7945f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // no-op; there are no waiters to notify. 7957dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers // We inflate here in case the Notify is in a tight loop. Without inflation here the waiter 7967dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers // will struggle to get in. Bug 6961405. 79700f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Inflate(self, obj); 7985f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 7995f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // It's a fat lock. 8005f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(thin)->Notify(self); 8015f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8025f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8035f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 8042dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::NotifyAll(Thread* self, mirror::Object *obj) { 8055f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes uint32_t thin = *obj->GetRawLockWordAddress(); 8065f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 8075f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // If the lock is still thin, there aren't any waiters; 8085f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // waiting on an object forces lock fattening. 8095f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(thin) == LW_SHAPE_THIN) { 8105f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // Make sure that 'self' holds the lock. 811f8e012785ee61a3d2f43f74a249d66e1381bdb83Elliott Hughes if (LW_LOCK_OWNER(thin) != self->GetThinLockId()) { 8126d0b13e931fb73d5e964d14b0a7ecd1f3f5db547Ian Rogers ThrowIllegalMonitorStateExceptionF("object not locked by thread before notifyAll()"); 8135f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return; 8145f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8155f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // no-op; there are no waiters to notify. 8167dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers // We inflate here in case the NotifyAll is in a tight loop. Without inflation here the waiter 8177dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers // will struggle to get in. Bug 6961405. 81800f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers Inflate(self, obj); 8195f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 8205f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes // It's a fat lock. 8215f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes LW_MONITOR(thin)->NotifyAll(self); 8225f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8235f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8245f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 82524a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstromuint32_t Monitor::GetThinLockId(uint32_t raw_lock_word) { 8265f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes if (LW_SHAPE(raw_lock_word) == LW_SHAPE_THIN) { 8275f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return LW_LOCK_OWNER(raw_lock_word); 8285f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } else { 8295f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes Thread* owner = LW_MONITOR(raw_lock_word)->owner_; 8305f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes return owner ? owner->GetThinLockId() : 0; 8315f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes } 8325f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} 8335f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes 8348e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughesvoid Monitor::DescribeWait(std::ostream& os, const Thread* thread) { 835f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes ThreadState state = thread->GetState(); 8368e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* object = NULL; 8388e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes uint32_t lock_owner = ThreadList::kInvalidId; 839b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (state == kWaiting || state == kTimedWaiting || state == kSleeping) { 840b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (state == kSleeping) { 841b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes os << " - sleeping on "; 842b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } else { 843b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes os << " - waiting on "; 844b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } 84500f7d0eaa6bd93d33bf0c1429bf4ba0b3f28abacIan Rogers { 846f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes Thread* self = Thread::Current(); 847f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes MutexLock mu(self, *thread->wait_mutex_); 848b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes Monitor* monitor = thread->wait_monitor_; 849b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes if (monitor != NULL) { 850b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes object = monitor->obj_; 851b4e94fd380a84c755264e8668a16052442c7ec32Elliott Hughes } 8528e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 85334e069606d6f1698cd3c33b39e72b79ae27e1c7bElliott Hughes } else if (state == kBlocked) { 8548e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " - waiting to lock "; 8558e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes object = thread->monitor_enter_object_; 8568e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (object != NULL) { 85724a3c2e9924e8765c4a9b4d383cb8f3b922f9c9fBrian Carlstrom lock_owner = object->GetThinLockId(); 8588e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8598e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } else { 8608e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // We're not waiting on anything. 8618e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes return; 8628e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8638e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8648e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes // - waiting on <0x6008c468> (a java.lang.Class<java.lang.ref.ReferenceQueue>) 86508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes os << "<" << object << "> (a " << PrettyTypeOf(object) << ")"; 8668e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 867c5dc2ff2d1beb3ff84f93d2c7ebe07c46b76956aElliott Hughes // - waiting to lock <0x613f83d8> (a java.lang.Object) held by thread 5 8688e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes if (lock_owner != ThreadList::kInvalidId) { 8698e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << " held by thread " << lock_owner; 8708e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes } 8718e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8728e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes os << "\n"; 8738e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes} 8748e4aac52962d54cb4be2078b9cd95685e067133aElliott Hughes 8752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Object* Monitor::GetContendedMonitor(Thread* thread) { 876f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes // This is used to implement JDWP's ThreadReference.CurrentContendedMonitor, and has a bizarre 877f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes // definition of contended that includes a monitor a thread is trying to enter... 8782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* result = thread->monitor_enter_object_; 879f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes if (result != NULL) { 880f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes return result; 881f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes } 882f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes // ...but also a monitor that the thread is waiting on. 883f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes { 884f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes MutexLock mu(Thread::Current(), *thread->wait_mutex_); 885f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes Monitor* monitor = thread->wait_monitor_; 886f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes if (monitor != NULL) { 887f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes return monitor->obj_; 888f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes } 889f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes } 890f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes return NULL; 891f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes} 892f9501700f51586cb6ba7cc0ffcb5a920bd64adf1Elliott Hughes 8932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid Monitor::VisitLocks(StackVisitor* stack_visitor, void (*callback)(mirror::Object*, void*), 8942dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers void* callback_context) { 895ea46f950e7a51585db293cd7f047de190a482414Brian Carlstrom mirror::ArtMethod* m = stack_visitor->GetMethod(); 89608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes CHECK(m != NULL); 89708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 89808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Native methods are an easy special case. 89908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // TODO: use the JNI implementation's table of explicit MonitorEnter calls and dump those too. 90008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (m->IsNative()) { 90108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (m->IsSynchronized()) { 9022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* jni_this = stack_visitor->GetCurrentSirt()->GetReference(0); 9034993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes callback(jni_this, callback_context); 90408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 90508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes return; 90608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 90708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 90861f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao // Proxy methods should not be synchronized. 90961f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao if (m->IsProxyMethod()) { 91061f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao CHECK(!m->IsSynchronized()); 91161f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao return; 91261f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao } 91361f916cc4757610ce308bfdea9a00cf29afd2b02jeffhao 91408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // <clinit> is another special case. The runtime holds the class lock while calling <clinit>. 91508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes MethodHelper mh(m); 91608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (mh.IsClassInitializer()) { 9174993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes callback(m->GetDeclaringClass(), callback_context); 91808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Fall through because there might be synchronization in the user code too. 91908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 92008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 92108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes // Is there any reason to believe there's any synchronization in this method? 92208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes const DexFile::CodeItem* code_item = mh.GetCodeItem(); 923caf7654a0e6c76c7489970b1a246fccf220f9982Elliott Hughes CHECK(code_item != NULL) << PrettyMethod(m); 92408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes if (code_item->tries_size_ == 0) { 9257934ac288acfb2552bb0b06ec1f61e5820d924a4Brian Carlstrom return; // No "tries" implies no synchronization, so no held locks to report. 92608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 92708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 92880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // Ask the verifier for the dex pcs of all the monitor-enter instructions corresponding to 92980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // the locks held in this stack frame. 93080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes std::vector<uint32_t> monitor_enter_dex_pcs; 93180537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes verifier::MethodVerifier::FindLocksAtDexPc(m, stack_visitor->GetDexPc(), monitor_enter_dex_pcs); 93280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes if (monitor_enter_dex_pcs.empty()) { 93380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes return; 93480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes } 93508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 93680537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes for (size_t i = 0; i < monitor_enter_dex_pcs.size(); ++i) { 93780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // The verifier works in terms of the dex pcs of the monitor-enter instructions. 93880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // We want the registers used by those instructions (so we can read the values out of them). 93980537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes uint32_t dex_pc = monitor_enter_dex_pcs[i]; 94080537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes uint16_t monitor_enter_instruction = code_item->insns_[dex_pc]; 94108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 94280537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes // Quick sanity check. 94380537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes if ((monitor_enter_instruction & 0xff) != Instruction::MONITOR_ENTER) { 94480537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes LOG(FATAL) << "expected monitor-enter @" << dex_pc << "; was " 94580537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes << reinterpret_cast<void*>(monitor_enter_instruction); 94608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 94780537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes 94880537bb742dff4ccdf6d04b1c0bb7d2179acc8cbElliott Hughes uint16_t monitor_register = ((monitor_enter_instruction >> 8) & 0xff); 9492dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* o = reinterpret_cast<mirror::Object*>(stack_visitor->GetVReg(m, monitor_register, 9502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers kReferenceVReg)); 9514993bbc8eda377804e585efd918f8ab9d9eab7d4Elliott Hughes callback(o, callback_context); 95208fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes } 95308fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes} 95408fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes 9557dfb28c066159e6cde8181720f0c451a700ef966Ian Rogersbool Monitor::IsValidLockWord(int32_t lock_word) { 9567dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers if (lock_word == 0) { 9577dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers return true; 9587dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } else if (LW_SHAPE(lock_word) == LW_SHAPE_FAT) { 9597dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers Monitor* mon = LW_MONITOR(lock_word); 9607dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers MonitorList* list = Runtime::Current()->GetMonitorList(); 9617dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers MutexLock mu(Thread::Current(), list->monitor_list_lock_); 9627dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers bool found = false; 9637dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers for (Monitor* list_mon : list->list_) { 9647dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers if (mon == list_mon) { 9657dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers found = true; 9667dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers break; 9677dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } 9687dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } 9697dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers return found; 9707dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } else { 9717dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers // TODO: thin lock validity checking. 9727dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers return LW_SHAPE(lock_word) == LW_SHAPE_THIN; 9737dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers } 9747dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers} 9757dfb28c066159e6cde8181720f0c451a700ef966Ian Rogers 976ea46f950e7a51585db293cd7f047de190a482414Brian Carlstromvoid Monitor::TranslateLocation(const mirror::ArtMethod* method, uint32_t dex_pc, 97733dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao const char*& source_file, uint32_t& line_number) const { 97833dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao // If method is null, location is unknown 97933dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao if (method == NULL) { 98012c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes source_file = ""; 98133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao line_number = 0; 98233dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao return; 98333dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao } 9846d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers MethodHelper mh(method); 9856d4d9fcb4f01e287ee29e81cd1c941ee5d11d379Ian Rogers source_file = mh.GetDeclaringClassSourceFile(); 98612c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes if (source_file == NULL) { 98712c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes source_file = ""; 98812c51e3e5a8b5655351539618f1cf2331f52d1abElliott Hughes } 9890399dde18753aa9bd2bd0d7cf60beef154d164a4Ian Rogers line_number = mh.GetLineNumFromDexPC(dex_pc); 99033dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao} 99133dc7717cd16592bcc825350bea6305be9eb2ea1jeffhao 992c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu ChartierMonitorList::MonitorList() 993c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier : allow_new_monitors_(true), monitor_list_lock_("MonitorList lock"), 994c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier monitor_add_condition_("MonitorList disallow condition", monitor_list_lock_) { 995c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 996c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 997c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott HughesMonitorList::~MonitorList() { 99850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), monitor_list_lock_); 999c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes STLDeleteElements(&list_); 1000c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 1001c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 1002c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::DisallowNewMonitors() { 100350b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), monitor_list_lock_); 1004c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier allow_new_monitors_ = false; 1005c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier} 1006c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier 1007c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::AllowNewMonitors() { 1008c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier Thread* self = Thread::Current(); 1009c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier MutexLock mu(self, monitor_list_lock_); 1010c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier allow_new_monitors_ = true; 1011c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier monitor_add_condition_.Broadcast(self); 1012c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier} 1013c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier 1014c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartiervoid MonitorList::Add(Monitor* m) { 1015c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier Thread* self = Thread::Current(); 1016c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier MutexLock mu(self, monitor_list_lock_); 1017c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier while (UNLIKELY(!allow_new_monitors_)) { 1018c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier monitor_add_condition_.WaitHoldingLocks(self); 1019c11d9b8870de5f860b13c84003ade7b3f3125a52Mathieu Chartier } 1020c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes list_.push_front(m); 1021c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 1022c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 10232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersvoid MonitorList::SweepMonitorList(IsMarkedTester is_marked, void* arg) { 102450b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers MutexLock mu(Thread::Current(), monitor_list_lock_); 102502e25119b15a6f619f17db99f5d05124a5807ff3Mathieu Chartier for (auto it = list_.begin(); it != list_.end(); ) { 1026c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes Monitor* m = *it; 1027c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes if (!is_marked(m->GetObject(), arg)) { 10284dd9b4d95eec9db5338fb9bf132f9bb8facf6cf4Elliott Hughes VLOG(monitor) << "freeing monitor " << m << " belonging to unmarked object " << m->GetObject(); 1029c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes delete m; 1030c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes it = list_.erase(it); 1031c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } else { 1032c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes ++it; 1033c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 1034c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes } 1035c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes} 1036c33a32bccc4c66ed82ce3a580b16636399385cb4Elliott Hughes 10372dd0e2cea360bc9206eb88ecc40d259e796c239dIan RogersMonitorInfo::MonitorInfo(mirror::Object* o) : owner(NULL), entry_count(0) { 1038f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes uint32_t lock_word = *o->GetRawLockWordAddress(); 1039f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes if (LW_SHAPE(lock_word) == LW_SHAPE_THIN) { 1040f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes uint32_t owner_thin_lock_id = LW_LOCK_OWNER(lock_word); 1041f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes if (owner_thin_lock_id != 0) { 1042f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes owner = Runtime::Current()->GetThreadList()->FindThreadByThinLockId(owner_thin_lock_id); 1043f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes entry_count = 1 + LW_LOCK_COUNT(lock_word); 1044f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 1045f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes // Thin locks have no waiters. 1046f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } else { 1047f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes CHECK_EQ(LW_SHAPE(lock_word), LW_SHAPE_FAT); 1048f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes Monitor* monitor = LW_MONITOR(lock_word); 1049f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes owner = monitor->owner_; 1050f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes entry_count = 1 + monitor->lock_count_; 1051f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes for (Thread* waiter = monitor->wait_set_; waiter != NULL; waiter = waiter->wait_next_) { 1052f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes waiters.push_back(waiter); 1053f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 1054f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes } 1055f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes} 1056f327e07b37e349b1ec5eaad6dc294a9b7a081d20Elliott Hughes 10575f79133a435ebcb20000370d56046fe01201dd80Elliott Hughes} // namespace art 1058