synchronizer.hpp revision 1798:fa83ab460c54
1193323Sed/* 2193323Sed * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved. 3193323Sed * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4193323Sed * 5193323Sed * This code is free software; you can redistribute it and/or modify it 6193323Sed * under the terms of the GNU General Public License version 2 only, as 7193323Sed * published by the Free Software Foundation. 8193323Sed * 9193323Sed * This code is distributed in the hope that it will be useful, but WITHOUT 10193323Sed * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11193323Sed * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12193323Sed * version 2 for more details (a copy is included in the LICENSE file that 13193323Sed * accompanied this code). 14193323Sed * 15193323Sed * You should have received a copy of the GNU General Public License version 16234982Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17193323Sed * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18234353Sdim * 19193323Sed * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20193323Sed * or visit www.oracle.com if you need additional information or have any 21193323Sed * questions. 22193323Sed * 23193323Sed */ 24193323Sed 25193323Sed 26193323Sedclass ObjectMonitor; 27193323Sed 28193323Sedclass ObjectSynchronizer : AllStatic { 29193323Sed friend class VMStructs; 30193323Sed public: 31193323Sed typedef enum { 32249423Sdim owner_self, 33193323Sed owner_none, 34193323Sed owner_other 35193323Sed } LockOwnership; 36193323Sed // exit must be implemented non-blocking, since the compiler cannot easily handle 37193323Sed // deoptimization at monitor exit. Hence, it does not take a Handle argument. 38193323Sed 39193323Sed // This is full version of monitor enter and exit. I choose not 40193323Sed // to use enter() and exit() in order to make sure user be ware 41193323Sed // of the performance and semantics difference. They are normally 42193323Sed // used by ObjectLocker etc. The interpreter and compiler use 43193323Sed // assembly copies of these routines. Please keep them synchornized. 44193323Sed // 45193323Sed // attempt_rebias flag is used by UseBiasedLocking implementation 46193323Sed static void fast_enter (Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS); 47193323Sed static void fast_exit (oop obj, BasicLock* lock, Thread* THREAD); 48193323Sed 49193323Sed // WARNING: They are ONLY used to handle the slow cases. They should 50193323Sed // only be used when the fast cases failed. Use of these functions 51193323Sed // without previous fast case check may cause fatal error. 52193323Sed static void slow_enter (Handle obj, BasicLock* lock, TRAPS); 53221345Sdim static void slow_exit (oop obj, BasicLock* lock, Thread* THREAD); 54221345Sdim 55221345Sdim // Used only to handle jni locks or other unmatched monitor enter/exit 56221345Sdim // Internally they will use heavy weight monitor. 57221345Sdim static void jni_enter (Handle obj, TRAPS); 58221345Sdim static bool jni_try_enter(Handle obj, Thread* THREAD); // Implements Unsafe.tryMonitorEnter 59221345Sdim static void jni_exit (oop obj, Thread* THREAD); 60221345Sdim 61193323Sed // Handle all interpreter, compiler and jni cases 62193323Sed static void wait (Handle obj, jlong millis, TRAPS); 63193323Sed static void notify (Handle obj, TRAPS); 64193323Sed static void notifyall (Handle obj, TRAPS); 65193323Sed 66193323Sed // Special internal-use-only method for use by JVM infrastructure 67193323Sed // that needs to wait() on a java-level object but that can't risk 68193323Sed // throwing unexpected InterruptedExecutionExceptions. 69193323Sed static void waitUninterruptibly (Handle obj, jlong Millis, Thread * THREAD) ; 70193323Sed 71193323Sed // used by classloading to free classloader object lock, 72193323Sed // wait on an internal lock, and reclaim original lock 73193323Sed // with original recursion count 74193323Sed static intptr_t complete_exit (Handle obj, TRAPS); 75193323Sed static void reenter (Handle obj, intptr_t recursion, TRAPS); 76193323Sed 77193323Sed // thread-specific and global objectMonitor free list accessors 78193323Sed// static void verifyInUse (Thread * Self) ; too slow for general assert/debug 79193323Sed static ObjectMonitor * omAlloc (Thread * Self) ; 80193323Sed static void omRelease (Thread * Self, ObjectMonitor * m, bool FromPerThreadAlloc) ; 81193323Sed static void omFlush (Thread * Self) ; 82193323Sed 83193323Sed // Inflate light weight monitor to heavy weight monitor 84193323Sed static ObjectMonitor* inflate(Thread * Self, oop obj); 85193323Sed // This version is only for internal use 86193323Sed static ObjectMonitor* inflate_helper(oop obj); 87193323Sed 88193323Sed // Returns the identity hash value for an oop 89193323Sed // NOTE: It may cause monitor inflation 90193323Sed static intptr_t identity_hash_value_for(Handle obj); 91193323Sed static intptr_t FastHashCode (Thread * Self, oop obj) ; 92193323Sed 93193323Sed // java.lang.Thread support 94193323Sed static bool current_thread_holds_lock(JavaThread* thread, Handle h_obj); 95193323Sed static LockOwnership query_lock_ownership(JavaThread * self, Handle h_obj); 96193323Sed 97193323Sed static JavaThread* get_lock_owner(Handle h_obj, bool doLock); 98193323Sed 99193323Sed // JNI detach support 100193323Sed static void release_monitors_owned_by_thread(TRAPS); 101193323Sed static void monitors_iterate(MonitorClosure* m); 102234982Sdim 103193323Sed // GC: we current use aggressive monitor deflation policy 104193323Sed // Basically we deflate all monitors that are not busy. 105193323Sed // An adaptive profile-based deflation policy could be used if needed 106193323Sed static void deflate_idle_monitors(); 107193323Sed static int walk_monitor_list(ObjectMonitor** listheadp, 108193323Sed ObjectMonitor** FreeHeadp, 109193323Sed ObjectMonitor** FreeTailp); 110193323Sed static bool deflate_monitor(ObjectMonitor* mid, oop obj, ObjectMonitor** FreeHeadp, 111193323Sed ObjectMonitor** FreeTailp); 112193323Sed static void oops_do(OopClosure* f); 113193323Sed 114193323Sed // debugging 115193323Sed static void trace_locking(Handle obj, bool is_compiled, bool is_method, bool is_locking) PRODUCT_RETURN; 116193323Sed static void verify() PRODUCT_RETURN; 117193323Sed static int verify_objmon_isinpool(ObjectMonitor *addr) PRODUCT_RETURN0; 118193323Sed 119193323Sed static void RegisterSpinCallback (int (*)(intptr_t, int), intptr_t) ; 120193323Sed 121193323Sed private: 122193323Sed enum { _BLOCKSIZE = 128 }; 123193323Sed static ObjectMonitor* gBlockList; 124193323Sed static ObjectMonitor * volatile gFreeList; 125193323Sed static ObjectMonitor * volatile gOmInUseList; // for moribund thread, so monitors they inflated still get scanned 126193323Sed static int gOmInUseCount; 127193323Sed 128193323Sed}; 129193323Sed 130221345Sdim// ObjectLocker enforced balanced locking and can never thrown an 131193323Sed// IllegalMonitorStateException. However, a pending exception may 132193323Sed// have to pass through, and we must also be able to deal with 133193323Sed// asynchronous exceptions. The caller is responsible for checking 134193323Sed// the threads pending exception if needed. 135193323Sed// doLock was added to support classloading with UnsyncloadClass which 136193323Sed// requires flag based choice of locking the classloader lock. 137193323Sedclass ObjectLocker : public StackObj { 138249423Sdim private: 139193323Sed Thread* _thread; 140193323Sed Handle _obj; 141193323Sed BasicLock _lock; 142193323Sed bool _dolock; // default true 143193323Sed public: 144193323Sed ObjectLocker(Handle obj, Thread* thread, bool doLock = true); 145193323Sed ~ObjectLocker(); 146193323Sed 147193323Sed // Monitor behavior 148193323Sed void wait (TRAPS) { ObjectSynchronizer::wait (_obj, 0, CHECK); } // wait forever 149193323Sed void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); } 150193323Sed void waitUninterruptibly (TRAPS) { ObjectSynchronizer::waitUninterruptibly (_obj, 0, CHECK);} 151193323Sed // complete_exit gives up lock completely, returning recursion count 152193323Sed // reenter reclaims lock with original recursion count 153193323Sed intptr_t complete_exit(TRAPS) { return ObjectSynchronizer::complete_exit(_obj, CHECK_0); } 154193323Sed void reenter(intptr_t recursion, TRAPS) { ObjectSynchronizer::reenter(_obj, recursion, CHECK); } 155193323Sed}; 156193323Sed