src/share/vm/runtime/synchronizer.hpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 9507
7e72702243a4
parent 8856
ac27a9c85bea
child 9852
70aa912cebe5
permissions
-rw-r--r--

Merge

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

mercurial