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