duke@435: /* mikael@6198: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_RUNTIME_SYNCHRONIZER_HPP stefank@2314: #define SHARE_VM_RUNTIME_SYNCHRONIZER_HPP stefank@2314: stefank@2314: #include "oops/markOop.hpp" stefank@2314: #include "runtime/basicLock.hpp" stefank@2314: #include "runtime/handles.hpp" stefank@2314: #include "runtime/perfData.hpp" stefank@2314: #include "utilities/top.hpp" stefank@2314: duke@435: duke@435: class ObjectMonitor; duke@435: duke@435: class ObjectSynchronizer : AllStatic { duke@435: friend class VMStructs; duke@435: public: duke@435: typedef enum { duke@435: owner_self, duke@435: owner_none, duke@435: owner_other duke@435: } LockOwnership; duke@435: // exit must be implemented non-blocking, since the compiler cannot easily handle duke@435: // deoptimization at monitor exit. Hence, it does not take a Handle argument. duke@435: duke@435: // This is full version of monitor enter and exit. I choose not duke@435: // to use enter() and exit() in order to make sure user be ware duke@435: // of the performance and semantics difference. They are normally duke@435: // used by ObjectLocker etc. The interpreter and compiler use duke@435: // assembly copies of these routines. Please keep them synchornized. duke@435: // duke@435: // attempt_rebias flag is used by UseBiasedLocking implementation duke@435: static void fast_enter (Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS); duke@435: static void fast_exit (oop obj, BasicLock* lock, Thread* THREAD); duke@435: duke@435: // WARNING: They are ONLY used to handle the slow cases. They should duke@435: // only be used when the fast cases failed. Use of these functions duke@435: // without previous fast case check may cause fatal error. duke@435: static void slow_enter (Handle obj, BasicLock* lock, TRAPS); duke@435: static void slow_exit (oop obj, BasicLock* lock, Thread* THREAD); duke@435: duke@435: // Used only to handle jni locks or other unmatched monitor enter/exit duke@435: // Internally they will use heavy weight monitor. duke@435: static void jni_enter (Handle obj, TRAPS); duke@435: static bool jni_try_enter(Handle obj, Thread* THREAD); // Implements Unsafe.tryMonitorEnter duke@435: static void jni_exit (oop obj, Thread* THREAD); duke@435: duke@435: // Handle all interpreter, compiler and jni cases duke@435: static void wait (Handle obj, jlong millis, TRAPS); duke@435: static void notify (Handle obj, TRAPS); duke@435: static void notifyall (Handle obj, TRAPS); duke@435: duke@435: // Special internal-use-only method for use by JVM infrastructure duke@435: // that needs to wait() on a java-level object but that can't risk duke@435: // throwing unexpected InterruptedExecutionExceptions. duke@435: static void waitUninterruptibly (Handle obj, jlong Millis, Thread * THREAD) ; duke@435: duke@435: // used by classloading to free classloader object lock, duke@435: // wait on an internal lock, and reclaim original lock duke@435: // with original recursion count duke@435: static intptr_t complete_exit (Handle obj, TRAPS); duke@435: static void reenter (Handle obj, intptr_t recursion, TRAPS); duke@435: duke@435: // thread-specific and global objectMonitor free list accessors acorn@1995: // static void verifyInUse (Thread * Self) ; too slow for general assert/debug duke@435: static ObjectMonitor * omAlloc (Thread * Self) ; acorn@1995: static void omRelease (Thread * Self, ObjectMonitor * m, bool FromPerThreadAlloc) ; duke@435: static void omFlush (Thread * Self) ; duke@435: duke@435: // Inflate light weight monitor to heavy weight monitor duke@435: static ObjectMonitor* inflate(Thread * Self, oop obj); duke@435: // This version is only for internal use duke@435: static ObjectMonitor* inflate_helper(oop obj); duke@435: duke@435: // Returns the identity hash value for an oop duke@435: // NOTE: It may cause monitor inflation duke@435: static intptr_t identity_hash_value_for(Handle obj); duke@435: static intptr_t FastHashCode (Thread * Self, oop obj) ; duke@435: duke@435: // java.lang.Thread support duke@435: static bool current_thread_holds_lock(JavaThread* thread, Handle h_obj); duke@435: static LockOwnership query_lock_ownership(JavaThread * self, Handle h_obj); duke@435: duke@435: static JavaThread* get_lock_owner(Handle h_obj, bool doLock); duke@435: duke@435: // JNI detach support duke@435: static void release_monitors_owned_by_thread(TRAPS); duke@435: static void monitors_iterate(MonitorClosure* m); duke@435: duke@435: // GC: we current use aggressive monitor deflation policy duke@435: // Basically we deflate all monitors that are not busy. duke@435: // An adaptive profile-based deflation policy could be used if needed duke@435: static void deflate_idle_monitors(); acorn@1995: static int walk_monitor_list(ObjectMonitor** listheadp, acorn@1995: ObjectMonitor** FreeHeadp, acorn@1995: ObjectMonitor** FreeTailp); acorn@1942: static bool deflate_monitor(ObjectMonitor* mid, oop obj, ObjectMonitor** FreeHeadp, acorn@1942: ObjectMonitor** FreeTailp); duke@435: static void oops_do(OopClosure* f); duke@435: duke@435: // debugging duke@435: static void verify() PRODUCT_RETURN; duke@435: static int verify_objmon_isinpool(ObjectMonitor *addr) PRODUCT_RETURN0; duke@435: acorn@2233: static void RegisterSpinCallback (int (*)(intptr_t, int), intptr_t) ; acorn@2233: duke@435: private: duke@435: enum { _BLOCKSIZE = 128 }; duke@435: static ObjectMonitor* gBlockList; duke@435: static ObjectMonitor * volatile gFreeList; acorn@1995: static ObjectMonitor * volatile gOmInUseList; // for moribund thread, so monitors they inflated still get scanned acorn@1995: static int gOmInUseCount; duke@435: duke@435: }; duke@435: duke@435: // ObjectLocker enforced balanced locking and can never thrown an duke@435: // IllegalMonitorStateException. However, a pending exception may duke@435: // have to pass through, and we must also be able to deal with duke@435: // asynchronous exceptions. The caller is responsible for checking duke@435: // the threads pending exception if needed. duke@435: // doLock was added to support classloading with UnsyncloadClass which duke@435: // requires flag based choice of locking the classloader lock. duke@435: class ObjectLocker : public StackObj { duke@435: private: duke@435: Thread* _thread; duke@435: Handle _obj; duke@435: BasicLock _lock; duke@435: bool _dolock; // default true duke@435: public: duke@435: ObjectLocker(Handle obj, Thread* thread, bool doLock = true); duke@435: ~ObjectLocker(); duke@435: duke@435: // Monitor behavior duke@435: void wait (TRAPS) { ObjectSynchronizer::wait (_obj, 0, CHECK); } // wait forever duke@435: void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); } duke@435: void waitUninterruptibly (TRAPS) { ObjectSynchronizer::waitUninterruptibly (_obj, 0, CHECK);} duke@435: // complete_exit gives up lock completely, returning recursion count duke@435: // reenter reclaims lock with original recursion count duke@435: intptr_t complete_exit(TRAPS) { return ObjectSynchronizer::complete_exit(_obj, CHECK_0); } duke@435: void reenter(intptr_t recursion, TRAPS) { ObjectSynchronizer::reenter(_obj, recursion, CHECK); } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_RUNTIME_SYNCHRONIZER_HPP