src/share/vm/prims/jvmtiRawMonitor.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 7535
7ae4e26cb1e0
permissions
-rw-r--r--

Merge

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

mercurial