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