src/share/vm/prims/jvmtiRawMonitor.cpp

Fri, 22 Oct 2010 15:59:34 -0400

author
acorn
date
Fri, 22 Oct 2010 15:59:34 -0400
changeset 2233
fa83ab460c54
child 2314
f95d63e2154a
permissions
-rw-r--r--

6988353: refactor contended sync subsystem
Summary: reduce complexity by factoring synchronizer.cpp
Reviewed-by: dholmes, never, coleenp

acorn@2233 1 /*
acorn@2233 2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
acorn@2233 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
acorn@2233 4 *
acorn@2233 5 * This code is free software; you can redistribute it and/or modify it
acorn@2233 6 * under the terms of the GNU General Public License version 2 only, as
acorn@2233 7 * published by the Free Software Foundation.
acorn@2233 8 *
acorn@2233 9 * This code is distributed in the hope that it will be useful, but WITHOUT
acorn@2233 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
acorn@2233 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
acorn@2233 12 * version 2 for more details (a copy is included in the LICENSE file that
acorn@2233 13 * accompanied this code).
acorn@2233 14 *
acorn@2233 15 * You should have received a copy of the GNU General Public License version
acorn@2233 16 * 2 along with this work; if not, write to the Free Software Foundation,
acorn@2233 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
acorn@2233 18 *
acorn@2233 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
acorn@2233 20 * or visit www.oracle.com if you need additional information or have any
acorn@2233 21 * questions.
acorn@2233 22 *
acorn@2233 23 */
acorn@2233 24
acorn@2233 25 # include "incls/_precompiled.incl"
acorn@2233 26 # include "incls/_jvmtiRawMonitor.cpp.incl"
acorn@2233 27
acorn@2233 28 GrowableArray<JvmtiRawMonitor*> *JvmtiPendingMonitors::_monitors = new (ResourceObj::C_HEAP) GrowableArray<JvmtiRawMonitor*>(1,true);
acorn@2233 29
acorn@2233 30 void JvmtiPendingMonitors::transition_raw_monitors() {
acorn@2233 31 assert((Threads::number_of_threads()==1),
acorn@2233 32 "Java thread has not created yet or more than one java thread \
acorn@2233 33 is running. Raw monitor transition will not work");
acorn@2233 34 JavaThread *current_java_thread = JavaThread::current();
acorn@2233 35 assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm");
acorn@2233 36 {
acorn@2233 37 ThreadBlockInVM __tbivm(current_java_thread);
acorn@2233 38 for(int i=0; i< count(); i++) {
acorn@2233 39 JvmtiRawMonitor *rmonitor = monitors()->at(i);
acorn@2233 40 int r = rmonitor->raw_enter(current_java_thread);
acorn@2233 41 assert(r == ObjectMonitor::OM_OK, "raw_enter should have worked");
acorn@2233 42 }
acorn@2233 43 }
acorn@2233 44 // pending monitors are converted to real monitor so delete them all.
acorn@2233 45 dispose();
acorn@2233 46 }
acorn@2233 47
acorn@2233 48 //
acorn@2233 49 // class JvmtiRawMonitor
acorn@2233 50 //
acorn@2233 51
acorn@2233 52 JvmtiRawMonitor::JvmtiRawMonitor(const char *name) {
acorn@2233 53 #ifdef ASSERT
acorn@2233 54 _name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1), name);
acorn@2233 55 #else
acorn@2233 56 _name = NULL;
acorn@2233 57 #endif
acorn@2233 58 _magic = JVMTI_RM_MAGIC;
acorn@2233 59 }
acorn@2233 60
acorn@2233 61 JvmtiRawMonitor::~JvmtiRawMonitor() {
acorn@2233 62 #ifdef ASSERT
acorn@2233 63 FreeHeap(_name);
acorn@2233 64 #endif
acorn@2233 65 _magic = 0;
acorn@2233 66 }
acorn@2233 67
acorn@2233 68
acorn@2233 69 bool
acorn@2233 70 JvmtiRawMonitor::is_valid() {
acorn@2233 71 int value = 0;
acorn@2233 72
acorn@2233 73 // This object might not be a JvmtiRawMonitor so we can't assume
acorn@2233 74 // the _magic field is properly aligned. Get the value in a safe
acorn@2233 75 // way and then check against JVMTI_RM_MAGIC.
acorn@2233 76
acorn@2233 77 switch (sizeof(_magic)) {
acorn@2233 78 case 2:
acorn@2233 79 value = Bytes::get_native_u2((address)&_magic);
acorn@2233 80 break;
acorn@2233 81
acorn@2233 82 case 4:
acorn@2233 83 value = Bytes::get_native_u4((address)&_magic);
acorn@2233 84 break;
acorn@2233 85
acorn@2233 86 case 8:
acorn@2233 87 value = Bytes::get_native_u8((address)&_magic);
acorn@2233 88 break;
acorn@2233 89
acorn@2233 90 default:
acorn@2233 91 guarantee(false, "_magic field is an unexpected size");
acorn@2233 92 }
acorn@2233 93
acorn@2233 94 return value == JVMTI_RM_MAGIC;
acorn@2233 95 }
acorn@2233 96
acorn@2233 97 // -------------------------------------------------------------------------
acorn@2233 98 // The raw monitor subsystem is entirely distinct from normal
acorn@2233 99 // java-synchronization or jni-synchronization. raw monitors are not
acorn@2233 100 // associated with objects. They can be implemented in any manner
acorn@2233 101 // that makes sense. The original implementors decided to piggy-back
acorn@2233 102 // the raw-monitor implementation on the existing Java objectMonitor mechanism.
acorn@2233 103 // This flaw needs to fixed. We should reimplement raw monitors as sui-generis.
acorn@2233 104 // Specifically, we should not implement raw monitors via java monitors.
acorn@2233 105 // Time permitting, we should disentangle and deconvolve the two implementations
acorn@2233 106 // and move the resulting raw monitor implementation over to the JVMTI directories.
acorn@2233 107 // Ideally, the raw monitor implementation would be built on top of
acorn@2233 108 // park-unpark and nothing else.
acorn@2233 109 //
acorn@2233 110 // raw monitors are used mainly by JVMTI
acorn@2233 111 // The raw monitor implementation borrows the ObjectMonitor structure,
acorn@2233 112 // but the operators are degenerate and extremely simple.
acorn@2233 113 //
acorn@2233 114 // Mixed use of a single objectMonitor instance -- as both a raw monitor
acorn@2233 115 // and a normal java monitor -- is not permissible.
acorn@2233 116 //
acorn@2233 117 // Note that we use the single RawMonitor_lock to protect queue operations for
acorn@2233 118 // _all_ raw monitors. This is a scalability impediment, but since raw monitor usage
acorn@2233 119 // is deprecated and rare, this is not of concern. The RawMonitor_lock can not
acorn@2233 120 // be held indefinitely. The critical sections must be short and bounded.
acorn@2233 121 //
acorn@2233 122 // -------------------------------------------------------------------------
acorn@2233 123
acorn@2233 124 int JvmtiRawMonitor::SimpleEnter (Thread * Self) {
acorn@2233 125 for (;;) {
acorn@2233 126 if (Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
acorn@2233 127 return OS_OK ;
acorn@2233 128 }
acorn@2233 129
acorn@2233 130 ObjectWaiter Node (Self) ;
acorn@2233 131 Self->_ParkEvent->reset() ; // strictly optional
acorn@2233 132 Node.TState = ObjectWaiter::TS_ENTER ;
acorn@2233 133
acorn@2233 134 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 135 Node._next = _EntryList ;
acorn@2233 136 _EntryList = &Node ;
acorn@2233 137 OrderAccess::fence() ;
acorn@2233 138 if (_owner == NULL && Atomic::cmpxchg_ptr (Self, &_owner, NULL) == NULL) {
acorn@2233 139 _EntryList = Node._next ;
acorn@2233 140 RawMonitor_lock->unlock() ;
acorn@2233 141 return OS_OK ;
acorn@2233 142 }
acorn@2233 143 RawMonitor_lock->unlock() ;
acorn@2233 144 while (Node.TState == ObjectWaiter::TS_ENTER) {
acorn@2233 145 Self->_ParkEvent->park() ;
acorn@2233 146 }
acorn@2233 147 }
acorn@2233 148 }
acorn@2233 149
acorn@2233 150 int JvmtiRawMonitor::SimpleExit (Thread * Self) {
acorn@2233 151 guarantee (_owner == Self, "invariant") ;
acorn@2233 152 OrderAccess::release_store_ptr (&_owner, NULL) ;
acorn@2233 153 OrderAccess::fence() ;
acorn@2233 154 if (_EntryList == NULL) return OS_OK ;
acorn@2233 155 ObjectWaiter * w ;
acorn@2233 156
acorn@2233 157 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 158 w = _EntryList ;
acorn@2233 159 if (w != NULL) {
acorn@2233 160 _EntryList = w->_next ;
acorn@2233 161 }
acorn@2233 162 RawMonitor_lock->unlock() ;
acorn@2233 163 if (w != NULL) {
acorn@2233 164 guarantee (w ->TState == ObjectWaiter::TS_ENTER, "invariant") ;
acorn@2233 165 ParkEvent * ev = w->_event ;
acorn@2233 166 w->TState = ObjectWaiter::TS_RUN ;
acorn@2233 167 OrderAccess::fence() ;
acorn@2233 168 ev->unpark() ;
acorn@2233 169 }
acorn@2233 170 return OS_OK ;
acorn@2233 171 }
acorn@2233 172
acorn@2233 173 int JvmtiRawMonitor::SimpleWait (Thread * Self, jlong millis) {
acorn@2233 174 guarantee (_owner == Self , "invariant") ;
acorn@2233 175 guarantee (_recursions == 0, "invariant") ;
acorn@2233 176
acorn@2233 177 ObjectWaiter Node (Self) ;
acorn@2233 178 Node._notified = 0 ;
acorn@2233 179 Node.TState = ObjectWaiter::TS_WAIT ;
acorn@2233 180
acorn@2233 181 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 182 Node._next = _WaitSet ;
acorn@2233 183 _WaitSet = &Node ;
acorn@2233 184 RawMonitor_lock->unlock() ;
acorn@2233 185
acorn@2233 186 SimpleExit (Self) ;
acorn@2233 187 guarantee (_owner != Self, "invariant") ;
acorn@2233 188
acorn@2233 189 int ret = OS_OK ;
acorn@2233 190 if (millis <= 0) {
acorn@2233 191 Self->_ParkEvent->park();
acorn@2233 192 } else {
acorn@2233 193 ret = Self->_ParkEvent->park(millis);
acorn@2233 194 }
acorn@2233 195
acorn@2233 196 // If thread still resides on the waitset then unlink it.
acorn@2233 197 // Double-checked locking -- the usage is safe in this context
acorn@2233 198 // as we TState is volatile and the lock-unlock operators are
acorn@2233 199 // serializing (barrier-equivalent).
acorn@2233 200
acorn@2233 201 if (Node.TState == ObjectWaiter::TS_WAIT) {
acorn@2233 202 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 203 if (Node.TState == ObjectWaiter::TS_WAIT) {
acorn@2233 204 // Simple O(n) unlink, but performance isn't critical here.
acorn@2233 205 ObjectWaiter * p ;
acorn@2233 206 ObjectWaiter * q = NULL ;
acorn@2233 207 for (p = _WaitSet ; p != &Node; p = p->_next) {
acorn@2233 208 q = p ;
acorn@2233 209 }
acorn@2233 210 guarantee (p == &Node, "invariant") ;
acorn@2233 211 if (q == NULL) {
acorn@2233 212 guarantee (p == _WaitSet, "invariant") ;
acorn@2233 213 _WaitSet = p->_next ;
acorn@2233 214 } else {
acorn@2233 215 guarantee (p == q->_next, "invariant") ;
acorn@2233 216 q->_next = p->_next ;
acorn@2233 217 }
acorn@2233 218 Node.TState = ObjectWaiter::TS_RUN ;
acorn@2233 219 }
acorn@2233 220 RawMonitor_lock->unlock() ;
acorn@2233 221 }
acorn@2233 222
acorn@2233 223 guarantee (Node.TState == ObjectWaiter::TS_RUN, "invariant") ;
acorn@2233 224 SimpleEnter (Self) ;
acorn@2233 225
acorn@2233 226 guarantee (_owner == Self, "invariant") ;
acorn@2233 227 guarantee (_recursions == 0, "invariant") ;
acorn@2233 228 return ret ;
acorn@2233 229 }
acorn@2233 230
acorn@2233 231 int JvmtiRawMonitor::SimpleNotify (Thread * Self, bool All) {
acorn@2233 232 guarantee (_owner == Self, "invariant") ;
acorn@2233 233 if (_WaitSet == NULL) return OS_OK ;
acorn@2233 234
acorn@2233 235 // We have two options:
acorn@2233 236 // A. Transfer the threads from the WaitSet to the EntryList
acorn@2233 237 // B. Remove the thread from the WaitSet and unpark() it.
acorn@2233 238 //
acorn@2233 239 // We use (B), which is crude and results in lots of futile
acorn@2233 240 // context switching. In particular (B) induces lots of contention.
acorn@2233 241
acorn@2233 242 ParkEvent * ev = NULL ; // consider using a small auto array ...
acorn@2233 243 RawMonitor_lock->lock_without_safepoint_check() ;
acorn@2233 244 for (;;) {
acorn@2233 245 ObjectWaiter * w = _WaitSet ;
acorn@2233 246 if (w == NULL) break ;
acorn@2233 247 _WaitSet = w->_next ;
acorn@2233 248 if (ev != NULL) { ev->unpark(); ev = NULL; }
acorn@2233 249 ev = w->_event ;
acorn@2233 250 OrderAccess::loadstore() ;
acorn@2233 251 w->TState = ObjectWaiter::TS_RUN ;
acorn@2233 252 OrderAccess::storeload();
acorn@2233 253 if (!All) break ;
acorn@2233 254 }
acorn@2233 255 RawMonitor_lock->unlock() ;
acorn@2233 256 if (ev != NULL) ev->unpark();
acorn@2233 257 return OS_OK ;
acorn@2233 258 }
acorn@2233 259
acorn@2233 260 // Any JavaThread will enter here with state _thread_blocked
acorn@2233 261 int JvmtiRawMonitor::raw_enter(TRAPS) {
acorn@2233 262 TEVENT (raw_enter) ;
acorn@2233 263 void * Contended ;
acorn@2233 264
acorn@2233 265 // don't enter raw monitor if thread is being externally suspended, it will
acorn@2233 266 // surprise the suspender if a "suspended" thread can still enter monitor
acorn@2233 267 JavaThread * jt = (JavaThread *)THREAD;
acorn@2233 268 if (THREAD->is_Java_thread()) {
acorn@2233 269 jt->SR_lock()->lock_without_safepoint_check();
acorn@2233 270 while (jt->is_external_suspend()) {
acorn@2233 271 jt->SR_lock()->unlock();
acorn@2233 272 jt->java_suspend_self();
acorn@2233 273 jt->SR_lock()->lock_without_safepoint_check();
acorn@2233 274 }
acorn@2233 275 // guarded by SR_lock to avoid racing with new external suspend requests.
acorn@2233 276 Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
acorn@2233 277 jt->SR_lock()->unlock();
acorn@2233 278 } else {
acorn@2233 279 Contended = Atomic::cmpxchg_ptr (THREAD, &_owner, NULL) ;
acorn@2233 280 }
acorn@2233 281
acorn@2233 282 if (Contended == THREAD) {
acorn@2233 283 _recursions ++ ;
acorn@2233 284 return OM_OK ;
acorn@2233 285 }
acorn@2233 286
acorn@2233 287 if (Contended == NULL) {
acorn@2233 288 guarantee (_owner == THREAD, "invariant") ;
acorn@2233 289 guarantee (_recursions == 0, "invariant") ;
acorn@2233 290 return OM_OK ;
acorn@2233 291 }
acorn@2233 292
acorn@2233 293 THREAD->set_current_pending_monitor(this);
acorn@2233 294
acorn@2233 295 if (!THREAD->is_Java_thread()) {
acorn@2233 296 // No other non-Java threads besides VM thread would acquire
acorn@2233 297 // a raw monitor.
acorn@2233 298 assert(THREAD->is_VM_thread(), "must be VM thread");
acorn@2233 299 SimpleEnter (THREAD) ;
acorn@2233 300 } else {
acorn@2233 301 guarantee (jt->thread_state() == _thread_blocked, "invariant") ;
acorn@2233 302 for (;;) {
acorn@2233 303 jt->set_suspend_equivalent();
acorn@2233 304 // cleared by handle_special_suspend_equivalent_condition() or
acorn@2233 305 // java_suspend_self()
acorn@2233 306 SimpleEnter (THREAD) ;
acorn@2233 307
acorn@2233 308 // were we externally suspended while we were waiting?
acorn@2233 309 if (!jt->handle_special_suspend_equivalent_condition()) break ;
acorn@2233 310
acorn@2233 311 // This thread was externally suspended
acorn@2233 312 //
acorn@2233 313 // This logic isn't needed for JVMTI raw monitors,
acorn@2233 314 // but doesn't hurt just in case the suspend rules change. This
acorn@2233 315 // logic is needed for the JvmtiRawMonitor.wait() reentry phase.
acorn@2233 316 // We have reentered the contended monitor, but while we were
acorn@2233 317 // waiting another thread suspended us. We don't want to reenter
acorn@2233 318 // the monitor while suspended because that would surprise the
acorn@2233 319 // thread that suspended us.
acorn@2233 320 //
acorn@2233 321 // Drop the lock -
acorn@2233 322 SimpleExit (THREAD) ;
acorn@2233 323
acorn@2233 324 jt->java_suspend_self();
acorn@2233 325 }
acorn@2233 326
acorn@2233 327 assert(_owner == THREAD, "Fatal error with monitor owner!");
acorn@2233 328 assert(_recursions == 0, "Fatal error with monitor recursions!");
acorn@2233 329 }
acorn@2233 330
acorn@2233 331 THREAD->set_current_pending_monitor(NULL);
acorn@2233 332 guarantee (_recursions == 0, "invariant") ;
acorn@2233 333 return OM_OK;
acorn@2233 334 }
acorn@2233 335
acorn@2233 336 // Used mainly for JVMTI raw monitor implementation
acorn@2233 337 // Also used for JvmtiRawMonitor::wait().
acorn@2233 338 int JvmtiRawMonitor::raw_exit(TRAPS) {
acorn@2233 339 TEVENT (raw_exit) ;
acorn@2233 340 if (THREAD != _owner) {
acorn@2233 341 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 342 }
acorn@2233 343 if (_recursions > 0) {
acorn@2233 344 --_recursions ;
acorn@2233 345 return OM_OK ;
acorn@2233 346 }
acorn@2233 347
acorn@2233 348 void * List = _EntryList ;
acorn@2233 349 SimpleExit (THREAD) ;
acorn@2233 350
acorn@2233 351 return OM_OK;
acorn@2233 352 }
acorn@2233 353
acorn@2233 354 // Used for JVMTI raw monitor implementation.
acorn@2233 355 // All JavaThreads will enter here with state _thread_blocked
acorn@2233 356
acorn@2233 357 int JvmtiRawMonitor::raw_wait(jlong millis, bool interruptible, TRAPS) {
acorn@2233 358 TEVENT (raw_wait) ;
acorn@2233 359 if (THREAD != _owner) {
acorn@2233 360 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 361 }
acorn@2233 362
acorn@2233 363 // To avoid spurious wakeups we reset the parkevent -- This is strictly optional.
acorn@2233 364 // The caller must be able to tolerate spurious returns from raw_wait().
acorn@2233 365 THREAD->_ParkEvent->reset() ;
acorn@2233 366 OrderAccess::fence() ;
acorn@2233 367
acorn@2233 368 // check interrupt event
acorn@2233 369 if (interruptible && Thread::is_interrupted(THREAD, true)) {
acorn@2233 370 return OM_INTERRUPTED;
acorn@2233 371 }
acorn@2233 372
acorn@2233 373 intptr_t save = _recursions ;
acorn@2233 374 _recursions = 0 ;
acorn@2233 375 _waiters ++ ;
acorn@2233 376 if (THREAD->is_Java_thread()) {
acorn@2233 377 guarantee (((JavaThread *) THREAD)->thread_state() == _thread_blocked, "invariant") ;
acorn@2233 378 ((JavaThread *)THREAD)->set_suspend_equivalent();
acorn@2233 379 }
acorn@2233 380 int rv = SimpleWait (THREAD, millis) ;
acorn@2233 381 _recursions = save ;
acorn@2233 382 _waiters -- ;
acorn@2233 383
acorn@2233 384 guarantee (THREAD == _owner, "invariant") ;
acorn@2233 385 if (THREAD->is_Java_thread()) {
acorn@2233 386 JavaThread * jSelf = (JavaThread *) THREAD ;
acorn@2233 387 for (;;) {
acorn@2233 388 if (!jSelf->handle_special_suspend_equivalent_condition()) break ;
acorn@2233 389 SimpleExit (THREAD) ;
acorn@2233 390 jSelf->java_suspend_self();
acorn@2233 391 SimpleEnter (THREAD) ;
acorn@2233 392 jSelf->set_suspend_equivalent() ;
acorn@2233 393 }
acorn@2233 394 }
acorn@2233 395 guarantee (THREAD == _owner, "invariant") ;
acorn@2233 396
acorn@2233 397 if (interruptible && Thread::is_interrupted(THREAD, true)) {
acorn@2233 398 return OM_INTERRUPTED;
acorn@2233 399 }
acorn@2233 400 return OM_OK ;
acorn@2233 401 }
acorn@2233 402
acorn@2233 403 int JvmtiRawMonitor::raw_notify(TRAPS) {
acorn@2233 404 TEVENT (raw_notify) ;
acorn@2233 405 if (THREAD != _owner) {
acorn@2233 406 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 407 }
acorn@2233 408 SimpleNotify (THREAD, false) ;
acorn@2233 409 return OM_OK;
acorn@2233 410 }
acorn@2233 411
acorn@2233 412 int JvmtiRawMonitor::raw_notifyAll(TRAPS) {
acorn@2233 413 TEVENT (raw_notifyAll) ;
acorn@2233 414 if (THREAD != _owner) {
acorn@2233 415 return OM_ILLEGAL_MONITOR_STATE;
acorn@2233 416 }
acorn@2233 417 SimpleNotify (THREAD, true) ;
acorn@2233 418 return OM_OK;
acorn@2233 419 }
acorn@2233 420

mercurial