1 /*
   2  * Copyright (c) 1998, 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 #ifndef SHARE_VM_RUNTIME_SYNCHRONIZER_HPP
  26 #define SHARE_VM_RUNTIME_SYNCHRONIZER_HPP
  27 
  28 #include "oops/markOop.hpp"
  29 #include "runtime/basicLock.hpp"
  30 #include "runtime/handles.hpp"
  31 #include "runtime/perfData.hpp"
  32 #include "utilities/top.hpp"
  33 
  34 
  35 class ObjectMonitor;
  36 
  37 class ObjectSynchronizer : AllStatic {
  38   friend class VMStructs;
  39 #if INCLUDE_ALL_GCS
  40   friend class ShenandoahSynchronizerIterator;
  41 #endif
  42 
  43 public:
  44   typedef enum {
  45     owner_self,
  46     owner_none,
  47     owner_other
  48   } LockOwnership;
  49   // exit must be implemented non-blocking, since the compiler cannot easily handle
  50   // deoptimization at monitor exit. Hence, it does not take a Handle argument.
  51 
  52   // This is full version of monitor enter and exit. I choose not
  53   // to use enter() and exit() in order to make sure user be ware
  54   // of the performance and semantics difference. They are normally
  55   // used by ObjectLocker etc. The interpreter and compiler use
  56   // assembly copies of these routines. Please keep them synchornized.
  57   //
  58   // attempt_rebias flag is used by UseBiasedLocking implementation
  59   static void fast_enter  (Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS);
  60   static void fast_exit   (oop obj,    BasicLock* lock, Thread* THREAD);
  61 
  62   // WARNING: They are ONLY used to handle the slow cases. They should
  63   // only be used when the fast cases failed. Use of these functions
  64   // without previous fast case check may cause fatal error.
  65   static void slow_enter  (Handle obj, BasicLock* lock, TRAPS);
  66   static void slow_exit   (oop obj,    BasicLock* lock, Thread* THREAD);
  67 
  68   // Used only to handle jni locks or other unmatched monitor enter/exit
  69   // Internally they will use heavy weight monitor.
  70   static void jni_enter   (Handle obj, TRAPS);
  71   static bool jni_try_enter(Handle obj, Thread* THREAD); // Implements Unsafe.tryMonitorEnter
  72   static void jni_exit    (oop obj,    Thread* THREAD);
  73 
  74   // Handle all interpreter, compiler and jni cases
  75   static void wait               (Handle obj, jlong millis, TRAPS);
  76   static void notify             (Handle obj,               TRAPS);
  77   static void notifyall          (Handle obj,               TRAPS);
  78 
  79   // Special internal-use-only method for use by JVM infrastructure
  80   // that needs to wait() on a java-level object but that can't risk
  81   // throwing unexpected InterruptedExecutionExceptions.
  82   static void waitUninterruptibly (Handle obj, jlong Millis, Thread * THREAD) ;
  83 
  84   // used by classloading to free classloader object lock,
  85   // wait on an internal lock, and reclaim original lock
  86   // with original recursion count
  87   static intptr_t complete_exit  (Handle obj,                TRAPS);
  88   static void reenter            (Handle obj, intptr_t recursion, TRAPS);
  89 
  90   // thread-specific and global objectMonitor free list accessors
  91 //  static void verifyInUse (Thread * Self) ; too slow for general assert/debug
  92   static ObjectMonitor * omAlloc (Thread * Self) ;
  93   static void omRelease (Thread * Self, ObjectMonitor * m, bool FromPerThreadAlloc) ;
  94   static void omFlush   (Thread * Self) ;
  95 
  96   // Inflate light weight monitor to heavy weight monitor
  97   static ObjectMonitor* inflate(Thread * Self, oop obj);
  98   // This version is only for internal use
  99   static ObjectMonitor* inflate_helper(oop obj);
 100 
 101   // Returns the identity hash value for an oop
 102   // NOTE: It may cause monitor inflation
 103   static intptr_t identity_hash_value_for(Handle obj);
 104   static intptr_t FastHashCode (Thread * Self, oop obj) ;
 105 
 106   // java.lang.Thread support
 107   static bool current_thread_holds_lock(JavaThread* thread, Handle h_obj);
 108   static LockOwnership query_lock_ownership(JavaThread * self, Handle h_obj);
 109 
 110   static JavaThread* get_lock_owner(Handle h_obj, bool doLock);
 111 
 112   // JNI detach support
 113   static void release_monitors_owned_by_thread(TRAPS);
 114   static void monitors_iterate(MonitorClosure* m);
 115 
 116   // GC: we current use aggressive monitor deflation policy
 117   // Basically we deflate all monitors that are not busy.
 118   // An adaptive profile-based deflation policy could be used if needed
 119   static void deflate_idle_monitors();
 120   static int walk_monitor_list(ObjectMonitor** listheadp,
 121                                ObjectMonitor** FreeHeadp,
 122                                ObjectMonitor** FreeTailp);
 123   static bool deflate_monitor(ObjectMonitor* mid, oop obj, ObjectMonitor** FreeHeadp,
 124                               ObjectMonitor** FreeTailp);
 125   static void oops_do(OopClosure* f);
 126 
 127   // debugging
 128   static void sanity_checks(const bool verbose,
 129                             const unsigned int cache_line_size,
 130                             int *error_cnt_ptr, int *warning_cnt_ptr);
 131   static void verify() PRODUCT_RETURN;
 132   static int  verify_objmon_isinpool(ObjectMonitor *addr) PRODUCT_RETURN0;
 133 
 134   static void RegisterSpinCallback (int (*)(intptr_t, int), intptr_t) ;
 135 
 136  private:
 137   enum { _BLOCKSIZE = 128 };
 138   static ObjectMonitor * volatile gBlockList;
 139   static ObjectMonitor * volatile gFreeList;
 140   static ObjectMonitor * volatile gOmInUseList; // for moribund thread, so monitors they inflated still get scanned
 141   static int gOmInUseCount;
 142 
 143 };
 144 
 145 // ObjectLocker enforced balanced locking and can never thrown an
 146 // IllegalMonitorStateException. However, a pending exception may
 147 // have to pass through, and we must also be able to deal with
 148 // asynchronous exceptions. The caller is responsible for checking
 149 // the threads pending exception if needed.
 150 // doLock was added to support classloading with UnsyncloadClass which
 151 // requires flag based choice of locking the classloader lock.
 152 class ObjectLocker : public StackObj {
 153  private:
 154   Thread*   _thread;
 155   Handle    _obj;
 156   BasicLock _lock;
 157   bool      _dolock;   // default true
 158  public:
 159   ObjectLocker(Handle obj, Thread* thread, bool doLock = true);
 160   ~ObjectLocker();
 161 
 162   // Monitor behavior
 163   void wait      (TRAPS)      { ObjectSynchronizer::wait     (_obj, 0, CHECK); } // wait forever
 164   void notify_all(TRAPS)      { ObjectSynchronizer::notifyall(_obj,    CHECK); }
 165   void waitUninterruptibly (TRAPS) { ObjectSynchronizer::waitUninterruptibly (_obj, 0, CHECK);}
 166   // complete_exit gives up lock completely, returning recursion count
 167   // reenter reclaims lock with original recursion count
 168   intptr_t complete_exit(TRAPS) { return  ObjectSynchronizer::complete_exit(_obj, THREAD); }
 169   void reenter(intptr_t recursion, TRAPS) { ObjectSynchronizer::reenter(_obj, recursion, CHECK); }
 170 };
 171 
 172 #endif // SHARE_VM_RUNTIME_SYNCHRONIZER_HPP