src/share/vm/prims/jvmtiRawMonitor.cpp

Tue, 08 Aug 2017 15:57:29 +0800

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

mercurial