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