src/share/vm/prims/jvmtiRawMonitor.cpp

Mon, 03 Jun 2019 16:14:54 +0100

author
xliu
date
Mon, 03 Jun 2019 16:14:54 +0100
changeset 9689
89dcef434423
parent 6911
ce8f6bb717c9
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

8059575: JEP-JDK-8043304: Test task: Tiered Compilation level transition tests
Summary: Includes compile_id addition from JDK-8054492
Reviewed-by: andrew

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

mercurial