duke@435: /* duke@435: * Copyright 1998-2007 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: class BasicLock VALUE_OBJ_CLASS_SPEC { duke@435: friend class VMStructs; duke@435: private: duke@435: volatile markOop _displaced_header; duke@435: public: duke@435: markOop displaced_header() const { return _displaced_header; } duke@435: void set_displaced_header(markOop header) { _displaced_header = header; } duke@435: duke@435: void print_on(outputStream* st) const; duke@435: duke@435: // move a basic lock (used during deoptimization duke@435: void move_to(oop obj, BasicLock* dest); duke@435: duke@435: static int displaced_header_offset_in_bytes() { return offset_of(BasicLock, _displaced_header); } duke@435: }; duke@435: duke@435: // A BasicObjectLock associates a specific Java object with a BasicLock. duke@435: // It is currently embedded in an interpreter frame. duke@435: duke@435: // Because some machines have alignment restrictions on the control stack, duke@435: // the actual space allocated by the interpreter may include padding words duke@435: // after the end of the BasicObjectLock. Also, in order to guarantee duke@435: // alignment of the embedded BasicLock objects on such machines, we duke@435: // put the embedded BasicLock at the beginning of the struct. duke@435: duke@435: class BasicObjectLock VALUE_OBJ_CLASS_SPEC { duke@435: friend class VMStructs; duke@435: private: duke@435: BasicLock _lock; // the lock, must be double word aligned duke@435: oop _obj; // object holds the lock; duke@435: duke@435: public: duke@435: // Manipulation duke@435: oop obj() const { return _obj; } duke@435: void set_obj(oop obj) { _obj = obj; } duke@435: BasicLock* lock() { return &_lock; } duke@435: duke@435: // Note: Use frame::interpreter_frame_monitor_size() for the size of BasicObjectLocks duke@435: // in interpreter activation frames since it includes machine-specific padding. duke@435: static int size() { return sizeof(BasicObjectLock)/wordSize; } duke@435: duke@435: // GC support duke@435: void oops_do(OopClosure* f) { f->do_oop(&_obj); } duke@435: duke@435: static int obj_offset_in_bytes() { return offset_of(BasicObjectLock, _obj); } duke@435: static int lock_offset_in_bytes() { return offset_of(BasicObjectLock, _lock); } duke@435: }; 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 duke@435: static ObjectMonitor * omAlloc (Thread * Self) ; duke@435: static void omRelease (Thread * Self, ObjectMonitor * m) ; 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@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 trace_locking(Handle obj, bool is_compiled, bool is_method, bool is_locking) PRODUCT_RETURN; duke@435: static void verify() PRODUCT_RETURN; duke@435: static int verify_objmon_isinpool(ObjectMonitor *addr) PRODUCT_RETURN0; duke@435: duke@435: private: duke@435: enum { _BLOCKSIZE = 128 }; duke@435: static ObjectMonitor* gBlockList; duke@435: static ObjectMonitor * volatile gFreeList; duke@435: duke@435: public: duke@435: static void Initialize () ; duke@435: static PerfCounter * _sync_ContendedLockAttempts ; duke@435: static PerfCounter * _sync_FutileWakeups ; duke@435: static PerfCounter * _sync_Parks ; duke@435: static PerfCounter * _sync_EmptyNotifications ; duke@435: static PerfCounter * _sync_Notifications ; duke@435: static PerfCounter * _sync_SlowEnter ; duke@435: static PerfCounter * _sync_SlowExit ; duke@435: static PerfCounter * _sync_SlowNotify ; duke@435: static PerfCounter * _sync_SlowNotifyAll ; duke@435: static PerfCounter * _sync_FailedSpins ; duke@435: static PerfCounter * _sync_SuccessfulSpins ; duke@435: static PerfCounter * _sync_PrivateA ; duke@435: static PerfCounter * _sync_PrivateB ; duke@435: static PerfCounter * _sync_MonInCirculation ; duke@435: static PerfCounter * _sync_MonScavenged ; duke@435: static PerfCounter * _sync_Inflations ; duke@435: static PerfCounter * _sync_Deflations ; duke@435: static PerfLongVariable * _sync_MonExtant ; duke@435: duke@435: public: duke@435: static void RegisterSpinCallback (int (*)(intptr_t, int), intptr_t) ; 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: };