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