Path: blob/master/src/hotspot/share/prims/jvmtiRawMonitor.cpp
41145 views
/*1* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "memory/allocation.inline.hpp"26#include "prims/jvmtiRawMonitor.hpp"27#include "runtime/atomic.hpp"28#include "runtime/interfaceSupport.inline.hpp"29#include "runtime/orderAccess.hpp"30#include "runtime/thread.inline.hpp"3132JvmtiRawMonitor::QNode::QNode(Thread* thread) : _next(NULL), _prev(NULL),33_event(thread->_ParkEvent),34_notified(0), _t_state(TS_RUN) {35}3637GrowableArray<JvmtiRawMonitor*>* JvmtiPendingMonitors::_monitors =38new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JvmtiRawMonitor*>(1, mtServiceability);3940void JvmtiPendingMonitors::transition_raw_monitors() {41assert((Threads::number_of_threads()==1),42"Java thread has not been created yet or more than one java thread "43"is running. Raw monitor transition will not work");44JavaThread* current_java_thread = JavaThread::current();45{46ThreadToNativeFromVM ttnfvm(current_java_thread);47for (int i = 0; i < count(); i++) {48JvmtiRawMonitor* rmonitor = monitors()->at(i);49rmonitor->raw_enter(current_java_thread);50}51}52// pending monitors are converted to real monitor so delete them all.53dispose();54}5556//57// class JvmtiRawMonitor58//5960JvmtiRawMonitor::JvmtiRawMonitor(const char* name) : _owner(NULL),61_recursions(0),62_entry_list(NULL),63_wait_set(NULL),64_magic(JVMTI_RM_MAGIC),65_name(NULL) {66#ifdef ASSERT67_name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtInternal), name);68#endif69}7071JvmtiRawMonitor::~JvmtiRawMonitor() {72#ifdef ASSERT73FreeHeap(_name);74#endif75_magic = 0;76}777879bool80JvmtiRawMonitor::is_valid() {81int value = 0;8283// This object might not be a JvmtiRawMonitor so we can't assume84// the _magic field is properly aligned. Get the value in a safe85// way and then check against JVMTI_RM_MAGIC.8687switch (sizeof(_magic)) {88case 2:89value = Bytes::get_native_u2((address)&_magic);90break;9192case 4:93value = Bytes::get_native_u4((address)&_magic);94break;9596case 8:97value = Bytes::get_native_u8((address)&_magic);98break;99100default:101guarantee(false, "_magic field is an unexpected size");102}103104return value == JVMTI_RM_MAGIC;105}106107// -------------------------------------------------------------------------108// The JVMTI raw monitor subsystem is entirely distinct from normal109// java-synchronization or jni-synchronization. JVMTI raw monitors are not110// associated with objects. They can be implemented in any manner111// that makes sense. The original implementors decided to piggy-back112// the raw-monitor implementation on the existing Java ObjectMonitor mechanism.113// Now we just use a simplified form of that ObjectMonitor code.114//115// Note that we use the single RawMonitor_lock to protect queue operations for116// _all_ raw monitors. This is a scalability impediment, but since raw monitor usage117// is fairly rare, this is not of concern. The RawMonitor_lock can not118// be held indefinitely. The critical sections must be short and bounded.119//120// -------------------------------------------------------------------------121122void JvmtiRawMonitor::simple_enter(Thread* self) {123for (;;) {124if (Atomic::replace_if_null(&_owner, self)) {125return;126}127128QNode node(self);129self->_ParkEvent->reset(); // strictly optional130node._t_state = QNode::TS_ENTER;131132RawMonitor_lock->lock_without_safepoint_check();133node._next = _entry_list;134_entry_list = &node;135OrderAccess::fence();136if (_owner == NULL && Atomic::replace_if_null(&_owner, self)) {137_entry_list = node._next;138RawMonitor_lock->unlock();139return;140}141RawMonitor_lock->unlock();142while (node._t_state == QNode::TS_ENTER) {143self->_ParkEvent->park();144}145}146}147148void JvmtiRawMonitor::simple_exit(Thread* self) {149guarantee(_owner == self, "invariant");150Atomic::release_store(&_owner, (Thread*)NULL);151OrderAccess::fence();152if (_entry_list == NULL) {153return;154}155156RawMonitor_lock->lock_without_safepoint_check();157QNode* w = _entry_list;158if (w != NULL) {159_entry_list = w->_next;160}161RawMonitor_lock->unlock();162if (w != NULL) {163guarantee(w ->_t_state == QNode::TS_ENTER, "invariant");164// Once we set _t_state to TS_RUN the waiting thread can complete165// simple_enter and 'w' is pointing into random stack space. So we have166// to ensure we extract the ParkEvent (which is in type-stable memory)167// before we set the state, and then don't access 'w'.168ParkEvent* ev = w->_event;169OrderAccess::loadstore();170w->_t_state = QNode::TS_RUN;171OrderAccess::fence();172ev->unpark();173}174return;175}176177inline void JvmtiRawMonitor::enqueue_waiter(QNode& node) {178node._notified = 0;179node._t_state = QNode::TS_WAIT;180RawMonitor_lock->lock_without_safepoint_check();181node._next = _wait_set;182_wait_set = &node;183RawMonitor_lock->unlock();184}185186inline void JvmtiRawMonitor::dequeue_waiter(QNode& node) {187// If thread still resides on the waitset then unlink it.188// Double-checked locking -- the usage is safe in this context189// as _t_state is volatile and the lock-unlock operators are190// serializing (barrier-equivalent).191192if (node._t_state == QNode::TS_WAIT) {193RawMonitor_lock->lock_without_safepoint_check();194if (node._t_state == QNode::TS_WAIT) {195// Simple O(n) unlink, but performance isn't critical here.196QNode* p;197QNode* q = NULL;198for (p = _wait_set; p != &node; p = p->_next) {199q = p;200}201guarantee(p == &node, "invariant");202if (q == NULL) {203guarantee (p == _wait_set, "invariant");204_wait_set = p->_next;205} else {206guarantee(p == q->_next, "invariant");207q->_next = p->_next;208}209node._t_state = QNode::TS_RUN;210}211RawMonitor_lock->unlock();212}213214guarantee(node._t_state == QNode::TS_RUN, "invariant");215}216217// simple_wait is not quite so simple as we have to deal with the interaction218// with the Thread interrupt state, which resides in the java.lang.Thread object.219// That state must only be accessed while _thread_in_vm and requires proper thread-state220// transitions.221// Returns M_OK usually, but M_INTERRUPTED if the thread is a JavaThread and was222// interrupted.223// Note:224// - simple_wait never reenters the monitor.225// - A JavaThread must be in native.226int JvmtiRawMonitor::simple_wait(Thread* self, jlong millis) {227guarantee(_owner == self , "invariant");228guarantee(_recursions == 0, "invariant");229230QNode node(self);231enqueue_waiter(node);232233simple_exit(self);234guarantee(_owner != self, "invariant");235236int ret = M_OK;237if (self->is_Java_thread()) {238JavaThread* jt = self->as_Java_thread();239guarantee(jt->thread_state() == _thread_in_native, "invariant");240{241// This transition must be after we exited the monitor.242ThreadInVMfromNative tivmfn(jt);243if (jt->is_interrupted(true)) {244ret = M_INTERRUPTED;245} else {246ThreadBlockInVM tbivm(jt);247if (millis <= 0) {248self->_ParkEvent->park();249} else {250self->_ParkEvent->park(millis);251}252// Return to VM before post-check of interrupt state253}254if (jt->is_interrupted(true)) {255ret = M_INTERRUPTED;256}257}258} else {259if (millis <= 0) {260self->_ParkEvent->park();261} else {262self->_ParkEvent->park(millis);263}264}265266dequeue_waiter(node);267268return ret;269}270271void JvmtiRawMonitor::simple_notify(Thread* self, bool all) {272guarantee(_owner == self, "invariant");273if (_wait_set == NULL) {274return;275}276277// We have two options:278// A. Transfer the threads from the _wait_set to the _entry_list279// B. Remove the thread from the _wait_set and unpark() it.280//281// We use (B), which is crude and results in lots of futile282// context switching. In particular (B) induces lots of contention.283284ParkEvent* ev = NULL; // consider using a small auto array ...285RawMonitor_lock->lock_without_safepoint_check();286for (;;) {287QNode* w = _wait_set;288if (w == NULL) break;289_wait_set = w->_next;290if (ev != NULL) {291ev->unpark();292ev = NULL;293}294ev = w->_event;295OrderAccess::loadstore();296w->_t_state = QNode::TS_RUN;297OrderAccess::storeload();298if (!all) {299break;300}301}302RawMonitor_lock->unlock();303if (ev != NULL) {304ev->unpark();305}306return;307}308309void JvmtiRawMonitor::ExitOnSuspend::operator()(JavaThread* current) {310// We must exit the monitor in case of a safepoint.311_rm->simple_exit(current);312_rm_exited = true;313}314315// JavaThreads will enter here with state _thread_in_native.316void JvmtiRawMonitor::raw_enter(Thread* self) {317// TODO Atomic::load on _owner field318if (_owner == self) {319_recursions++;320return;321}322323self->set_current_pending_raw_monitor(this);324325if (!self->is_Java_thread()) {326simple_enter(self);327} else {328JavaThread* jt = self->as_Java_thread();329guarantee(jt->thread_state() == _thread_in_native, "invariant");330ThreadInVMfromNative tivmfn(jt);331for (;;) {332ExitOnSuspend eos(this);333{334ThreadBlockInVMPreprocess<ExitOnSuspend> tbivmp(jt, eos);335simple_enter(jt);336}337if (!eos.monitor_exited()) {338break;339}340}341}342343self->set_current_pending_raw_monitor(NULL);344345guarantee(_owner == self, "invariant");346guarantee(_recursions == 0, "invariant");347}348349int JvmtiRawMonitor::raw_exit(Thread* self) {350if (self != _owner) {351return M_ILLEGAL_MONITOR_STATE;352}353if (_recursions > 0) {354_recursions--;355} else {356simple_exit(self);357}358359return M_OK;360}361362int JvmtiRawMonitor::raw_wait(jlong millis, Thread* self) {363if (self != _owner) {364return M_ILLEGAL_MONITOR_STATE;365}366367int ret = M_OK;368369// To avoid spurious wakeups we reset the parkevent. This is strictly optional.370// The caller must be able to tolerate spurious returns from raw_wait().371self->_ParkEvent->reset();372OrderAccess::fence();373374intptr_t save = _recursions;375_recursions = 0;376ret = simple_wait(self, millis);377378// Now we need to re-enter the monitor. For JavaThreads379// we need to manage suspend requests.380if (self->is_Java_thread()) { // JavaThread re-enter381JavaThread* jt = self->as_Java_thread();382ThreadInVMfromNative tivmfn(jt);383for (;;) {384ExitOnSuspend eos(this);385{386ThreadBlockInVMPreprocess<ExitOnSuspend> tbivmp(jt, eos);387simple_enter(jt);388}389if (!eos.monitor_exited()) {390break;391}392}393if (jt->is_interrupted(true)) {394ret = M_INTERRUPTED;395}396} else { // Non-JavaThread re-enter397assert(ret != M_INTERRUPTED, "Only JavaThreads can be interrupted");398simple_enter(self);399}400401_recursions = save;402403guarantee(self == _owner, "invariant");404return ret;405}406407int JvmtiRawMonitor::raw_notify(Thread* self) {408if (self != _owner) {409return M_ILLEGAL_MONITOR_STATE;410}411simple_notify(self, false);412return M_OK;413}414415int JvmtiRawMonitor::raw_notifyAll(Thread* self) {416if (self != _owner) {417return M_ILLEGAL_MONITOR_STATE;418}419simple_notify(self, true);420return M_OK;421}422423424