aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "prims/jvmtiRawMonitor.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/thread.hpp" aoqi@0: aoqi@0: GrowableArray *JvmtiPendingMonitors::_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(1,true); aoqi@0: aoqi@0: void JvmtiPendingMonitors::transition_raw_monitors() { aoqi@0: assert((Threads::number_of_threads()==1), aoqi@0: "Java thread has not created yet or more than one java thread \ aoqi@0: is running. Raw monitor transition will not work"); aoqi@0: JavaThread *current_java_thread = JavaThread::current(); aoqi@0: assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm"); aoqi@0: { aoqi@0: ThreadBlockInVM __tbivm(current_java_thread); aoqi@0: for(int i=0; i< count(); i++) { aoqi@0: JvmtiRawMonitor *rmonitor = monitors()->at(i); aoqi@0: int r = rmonitor->raw_enter(current_java_thread); aoqi@0: assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked"); aoqi@0: } aoqi@0: } aoqi@0: // pending monitors are converted to real monitor so delete them all. aoqi@0: dispose(); aoqi@0: } aoqi@0: aoqi@0: // aoqi@0: // class JvmtiRawMonitor aoqi@0: // aoqi@0: aoqi@0: JvmtiRawMonitor::JvmtiRawMonitor(const char *name) { aoqi@0: #ifdef ASSERT aoqi@0: _name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtInternal), name); aoqi@0: #else aoqi@0: _name = NULL; aoqi@0: #endif aoqi@0: _magic = JVMTI_RM_MAGIC; aoqi@0: } aoqi@0: aoqi@0: JvmtiRawMonitor::~JvmtiRawMonitor() { aoqi@0: #ifdef ASSERT aoqi@0: FreeHeap(_name); aoqi@0: #endif aoqi@0: _magic = 0; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool aoqi@0: JvmtiRawMonitor::is_valid() { aoqi@0: int value = 0; aoqi@0: aoqi@0: // This object might not be a JvmtiRawMonitor so we can't assume aoqi@0: // the _magic field is properly aligned. Get the value in a safe aoqi@0: // way and then check against JVMTI_RM_MAGIC. aoqi@0: aoqi@0: switch (sizeof(_magic)) { aoqi@0: case 2: aoqi@0: value = Bytes::get_native_u2((address)&_magic); aoqi@0: break; aoqi@0: aoqi@0: case 4: aoqi@0: value = Bytes::get_native_u4((address)&_magic); aoqi@0: break; aoqi@0: aoqi@0: case 8: aoqi@0: value = Bytes::get_native_u8((address)&_magic); aoqi@0: break; aoqi@0: aoqi@0: default: aoqi@0: guarantee(false, "_magic field is an unexpected size"); aoqi@0: } aoqi@0: aoqi@0: return value == JVMTI_RM_MAGIC; aoqi@0: } aoqi@0: aoqi@0: // ------------------------------------------------------------------------- aoqi@0: // The raw monitor subsystem is entirely distinct from normal aoqi@0: // java-synchronization or jni-synchronization. raw monitors are not aoqi@0: // associated with objects. They can be implemented in any manner aoqi@0: // that makes sense. The original implementors decided to piggy-back aoqi@0: // the raw-monitor implementation on the existing Java objectMonitor mechanism. aoqi@0: // This flaw needs to fixed. We should reimplement raw monitors as sui-generis. aoqi@0: // Specifically, we should not implement raw monitors via java monitors. aoqi@0: // Time permitting, we should disentangle and deconvolve the two implementations aoqi@0: // and move the resulting raw monitor implementation over to the JVMTI directories. aoqi@0: // Ideally, the raw monitor implementation would be built on top of aoqi@0: // park-unpark and nothing else. aoqi@0: // aoqi@0: // raw monitors are used mainly by JVMTI aoqi@0: // The raw monitor implementation borrows the ObjectMonitor structure, aoqi@0: // but the operators are degenerate and extremely simple. aoqi@0: // aoqi@0: // Mixed use of a single objectMonitor instance -- as both a raw monitor aoqi@0: // and a normal java monitor -- is not permissible. aoqi@0: // aoqi@0: // Note that we use the single RawMonitor_lock to protect queue operations for aoqi@0: // _all_ raw monitors. This is a scalability impediment, but since raw monitor usage aoqi@0: // is deprecated and rare, this is not of concern. The RawMonitor_lock can not aoqi@0: // be held indefinitely. The critical sections must be short and bounded. aoqi@0: // aoqi@0: // ------------------------------------------------------------------------- aoqi@0: aoqi@0: int JvmtiRawMonitor::SimpleEnter (Thread * Self) { aoqi@0: for (;;) { aoqi@0: if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) { aoqi@0: return OS_OK ; aoqi@0: } aoqi@0: aoqi@0: ObjectWaiter Node (Self) ; aoqi@0: Self->_ParkEvent->reset() ; // strictly optional aoqi@0: Node.TState = ObjectWaiter::TS_ENTER ; aoqi@0: aoqi@0: RawMonitor_lock->lock_without_safepoint_check() ; aoqi@0: Node._next = _EntryList ; aoqi@0: _EntryList = &Node ; aoqi@0: OrderAccess::fence() ; aoqi@0: if (_owner == NULL && Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) { aoqi@0: _EntryList = Node._next ; aoqi@0: RawMonitor_lock->unlock() ; aoqi@0: return OS_OK ; aoqi@0: } aoqi@0: RawMonitor_lock->unlock() ; aoqi@0: while (Node.TState == ObjectWaiter::TS_ENTER) { aoqi@0: Self->_ParkEvent->park() ; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: int JvmtiRawMonitor::SimpleExit (Thread * Self) { aoqi@0: guarantee (_owner == Self, "invariant") ; aoqi@0: OrderAccess::release_store_ptr (&_owner, NULL) ; aoqi@0: OrderAccess::fence() ; aoqi@0: if (_EntryList == NULL) return OS_OK ; aoqi@0: ObjectWaiter * w ; aoqi@0: aoqi@0: RawMonitor_lock->lock_without_safepoint_check() ; aoqi@0: w = _EntryList ; aoqi@0: if (w != NULL) { aoqi@0: _EntryList = w->_next ; aoqi@0: } aoqi@0: RawMonitor_lock->unlock() ; aoqi@0: if (w != NULL) { aoqi@0: guarantee (w ->TState == ObjectWaiter::TS_ENTER, "invariant") ; aoqi@0: ParkEvent * ev = w->_event ; aoqi@0: w->TState = ObjectWaiter::TS_RUN ; aoqi@0: OrderAccess::fence() ; aoqi@0: ev->unpark() ; aoqi@0: } aoqi@0: return OS_OK ; aoqi@0: } aoqi@0: aoqi@0: int JvmtiRawMonitor::SimpleWait (Thread * Self, jlong millis) { aoqi@0: guarantee (_owner == Self , "invariant") ; aoqi@0: guarantee (_recursions == 0, "invariant") ; aoqi@0: aoqi@0: ObjectWaiter Node (Self) ; aoqi@0: Node._notified = 0 ; aoqi@0: Node.TState = ObjectWaiter::TS_WAIT ; aoqi@0: aoqi@0: RawMonitor_lock->lock_without_safepoint_check() ; aoqi@0: Node._next = _WaitSet ; aoqi@0: _WaitSet = &Node ; aoqi@0: RawMonitor_lock->unlock() ; aoqi@0: aoqi@0: SimpleExit (Self) ; aoqi@0: guarantee (_owner != Self, "invariant") ; aoqi@0: aoqi@0: int ret = OS_OK ; aoqi@0: if (millis <= 0) { aoqi@0: Self->_ParkEvent->park(); aoqi@0: } else { aoqi@0: ret = Self->_ParkEvent->park(millis); aoqi@0: } aoqi@0: aoqi@0: // If thread still resides on the waitset then unlink it. aoqi@0: // Double-checked locking -- the usage is safe in this context aoqi@0: // as we TState is volatile and the lock-unlock operators are aoqi@0: // serializing (barrier-equivalent). aoqi@0: aoqi@0: if (Node.TState == ObjectWaiter::TS_WAIT) { aoqi@0: RawMonitor_lock->lock_without_safepoint_check() ; aoqi@0: if (Node.TState == ObjectWaiter::TS_WAIT) { aoqi@0: // Simple O(n) unlink, but performance isn't critical here. aoqi@0: ObjectWaiter * p ; aoqi@0: ObjectWaiter * q = NULL ; aoqi@0: for (p = _WaitSet ; p != &Node; p = p->_next) { aoqi@0: q = p ; aoqi@0: } aoqi@0: guarantee (p == &Node, "invariant") ; aoqi@0: if (q == NULL) { aoqi@0: guarantee (p == _WaitSet, "invariant") ; aoqi@0: _WaitSet = p->_next ; aoqi@0: } else { aoqi@0: guarantee (p == q->_next, "invariant") ; aoqi@0: q->_next = p->_next ; aoqi@0: } aoqi@0: Node.TState = ObjectWaiter::TS_RUN ; aoqi@0: } aoqi@0: RawMonitor_lock->unlock() ; aoqi@0: } aoqi@0: aoqi@0: guarantee (Node.TState == ObjectWaiter::TS_RUN, "invariant") ; aoqi@0: SimpleEnter (Self) ; aoqi@0: aoqi@0: guarantee (_owner == Self, "invariant") ; aoqi@0: guarantee (_recursions == 0, "invariant") ; aoqi@0: return ret ; aoqi@0: } aoqi@0: aoqi@0: int JvmtiRawMonitor::SimpleNotify (Thread * Self, bool All) { aoqi@0: guarantee (_owner == Self, "invariant") ; aoqi@0: if (_WaitSet == NULL) return OS_OK ; aoqi@0: aoqi@0: // We have two options: aoqi@0: // A. Transfer the threads from the WaitSet to the EntryList aoqi@0: // B. Remove the thread from the WaitSet and unpark() it. aoqi@0: // aoqi@0: // We use (B), which is crude and results in lots of futile aoqi@0: // context switching. In particular (B) induces lots of contention. aoqi@0: aoqi@0: ParkEvent * ev = NULL ; // consider using a small auto array ... aoqi@0: RawMonitor_lock->lock_without_safepoint_check() ; aoqi@0: for (;;) { aoqi@0: ObjectWaiter * w = _WaitSet ; aoqi@0: if (w == NULL) break ; aoqi@0: _WaitSet = w->_next ; aoqi@0: if (ev != NULL) { ev->unpark(); ev = NULL; } aoqi@0: ev = w->_event ; aoqi@0: OrderAccess::loadstore() ; aoqi@0: w->TState = ObjectWaiter::TS_RUN ; aoqi@0: OrderAccess::storeload(); aoqi@0: if (!All) break ; aoqi@0: } aoqi@0: RawMonitor_lock->unlock() ; aoqi@0: if (ev != NULL) ev->unpark(); aoqi@0: return OS_OK ; aoqi@0: } aoqi@0: aoqi@0: // Any JavaThread will enter here with state _thread_blocked aoqi@0: int JvmtiRawMonitor::raw_enter(TRAPS) { aoqi@0: TEVENT (raw_enter) ; aoqi@0: void * Contended ; aoqi@0: aoqi@0: // don't enter raw monitor if thread is being externally suspended, it will aoqi@0: // surprise the suspender if a "suspended" thread can still enter monitor aoqi@0: JavaThread * jt = (JavaThread *)THREAD; aoqi@0: if (THREAD->is_Java_thread()) { aoqi@0: jt->SR_lock()->lock_without_safepoint_check(); aoqi@0: while (jt->is_external_suspend()) { aoqi@0: jt->SR_lock()->unlock(); aoqi@0: jt->java_suspend_self(); aoqi@0: jt->SR_lock()->lock_without_safepoint_check(); aoqi@0: } aoqi@0: // guarded by SR_lock to avoid racing with new external suspend requests. aoqi@0: Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ; aoqi@0: jt->SR_lock()->unlock(); aoqi@0: } else { aoqi@0: Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ; aoqi@0: } aoqi@0: aoqi@0: if (Contended == THREAD) { aoqi@0: _recursions ++ ; aoqi@0: return OM_OK ; aoqi@0: } aoqi@0: aoqi@0: if (Contended == NULL) { aoqi@0: guarantee (_owner == THREAD, "invariant") ; aoqi@0: guarantee (_recursions == 0, "invariant") ; aoqi@0: return OM_OK ; aoqi@0: } aoqi@0: aoqi@0: THREAD->set_current_pending_monitor(this); aoqi@0: aoqi@0: if (!THREAD->is_Java_thread()) { aoqi@0: // No other non-Java threads besides VM thread would acquire aoqi@0: // a raw monitor. aoqi@0: assert(THREAD->is_VM_thread(), "must be VM thread"); aoqi@0: SimpleEnter (THREAD) ; aoqi@0: } else { aoqi@0: guarantee (jt->thread_state() == _thread_blocked, "invariant") ; aoqi@0: for (;;) { aoqi@0: jt->set_suspend_equivalent(); aoqi@0: // cleared by handle_special_suspend_equivalent_condition() or aoqi@0: // java_suspend_self() aoqi@0: SimpleEnter (THREAD) ; aoqi@0: aoqi@0: // were we externally suspended while we were waiting? aoqi@0: if (!jt->handle_special_suspend_equivalent_condition()) break ; aoqi@0: aoqi@0: // This thread was externally suspended aoqi@0: // aoqi@0: // This logic isn't needed for JVMTI raw monitors, aoqi@0: // but doesn't hurt just in case the suspend rules change. This aoqi@0: // logic is needed for the JvmtiRawMonitor.wait() reentry phase. aoqi@0: // We have reentered the contended monitor, but while we were aoqi@0: // waiting another thread suspended us. We don't want to reenter aoqi@0: // the monitor while suspended because that would surprise the aoqi@0: // thread that suspended us. aoqi@0: // aoqi@0: // Drop the lock - aoqi@0: SimpleExit (THREAD) ; aoqi@0: aoqi@0: jt->java_suspend_self(); aoqi@0: } aoqi@0: aoqi@0: assert(_owner == THREAD, "Fatal error with monitor owner!"); aoqi@0: assert(_recursions == 0, "Fatal error with monitor recursions!"); aoqi@0: } aoqi@0: aoqi@0: THREAD->set_current_pending_monitor(NULL); aoqi@0: guarantee (_recursions == 0, "invariant") ; aoqi@0: return OM_OK; aoqi@0: } aoqi@0: aoqi@0: // Used mainly for JVMTI raw monitor implementation aoqi@0: // Also used for JvmtiRawMonitor::wait(). aoqi@0: int JvmtiRawMonitor::raw_exit(TRAPS) { aoqi@0: TEVENT (raw_exit) ; aoqi@0: if (THREAD != _owner) { aoqi@0: return OM_ILLEGAL_MONITOR_STATE; aoqi@0: } aoqi@0: if (_recursions > 0) { aoqi@0: --_recursions ; aoqi@0: return OM_OK ; aoqi@0: } aoqi@0: aoqi@0: void * List = _EntryList ; aoqi@0: SimpleExit (THREAD) ; aoqi@0: aoqi@0: return OM_OK; aoqi@0: } aoqi@0: aoqi@0: // Used for JVMTI raw monitor implementation. aoqi@0: // All JavaThreads will enter here with state _thread_blocked aoqi@0: aoqi@0: int JvmtiRawMonitor::raw_wait(jlong millis, bool interruptible, TRAPS) { aoqi@0: TEVENT (raw_wait) ; aoqi@0: if (THREAD != _owner) { aoqi@0: return OM_ILLEGAL_MONITOR_STATE; aoqi@0: } aoqi@0: aoqi@0: // To avoid spurious wakeups we reset the parkevent -- This is strictly optional. aoqi@0: // The caller must be able to tolerate spurious returns from raw_wait(). aoqi@0: THREAD->_ParkEvent->reset() ; aoqi@0: OrderAccess::fence() ; aoqi@0: aoqi@0: // check interrupt event aoqi@0: if (interruptible && Thread::is_interrupted(THREAD, true)) { aoqi@0: return OM_INTERRUPTED; aoqi@0: } aoqi@0: aoqi@0: intptr_t save = _recursions ; aoqi@0: _recursions = 0 ; aoqi@0: _waiters ++ ; aoqi@0: if (THREAD->is_Java_thread()) { aoqi@0: guarantee (((JavaThread *) THREAD)->thread_state() == _thread_blocked, "invariant") ; aoqi@0: ((JavaThread *)THREAD)->set_suspend_equivalent(); aoqi@0: } aoqi@0: int rv = SimpleWait (THREAD, millis) ; aoqi@0: _recursions = save ; aoqi@0: _waiters -- ; aoqi@0: aoqi@0: guarantee (THREAD == _owner, "invariant") ; aoqi@0: if (THREAD->is_Java_thread()) { aoqi@0: JavaThread * jSelf = (JavaThread *) THREAD ; aoqi@0: for (;;) { aoqi@0: if (!jSelf->handle_special_suspend_equivalent_condition()) break ; aoqi@0: SimpleExit (THREAD) ; aoqi@0: jSelf->java_suspend_self(); aoqi@0: SimpleEnter (THREAD) ; aoqi@0: jSelf->set_suspend_equivalent() ; aoqi@0: } aoqi@0: } aoqi@0: guarantee (THREAD == _owner, "invariant") ; aoqi@0: aoqi@0: if (interruptible && Thread::is_interrupted(THREAD, true)) { aoqi@0: return OM_INTERRUPTED; aoqi@0: } aoqi@0: return OM_OK ; aoqi@0: } aoqi@0: aoqi@0: int JvmtiRawMonitor::raw_notify(TRAPS) { aoqi@0: TEVENT (raw_notify) ; aoqi@0: if (THREAD != _owner) { aoqi@0: return OM_ILLEGAL_MONITOR_STATE; aoqi@0: } aoqi@0: SimpleNotify (THREAD, false) ; aoqi@0: return OM_OK; aoqi@0: } aoqi@0: aoqi@0: int JvmtiRawMonitor::raw_notifyAll(TRAPS) { aoqi@0: TEVENT (raw_notifyAll) ; aoqi@0: if (THREAD != _owner) { aoqi@0: return OM_ILLEGAL_MONITOR_STATE; aoqi@0: } aoqi@0: SimpleNotify (THREAD, true) ; aoqi@0: return OM_OK; aoqi@0: } aoqi@0: