src/share/vm/runtime/synchronizer.hpp

Tue, 11 May 2010 14:35:43 -0700

author
prr
date
Tue, 11 May 2010 14:35:43 -0700
changeset 1840
fb57d4cf76c2
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
child 1942
b96a3e44582f
permissions
-rw-r--r--

6931180: Migration to recent versions of MS Platform SDK
6951582: Build problems on win64
Summary: Changes to enable building JDK7 with Microsoft Visual Studio 2010
Reviewed-by: ohair, art, ccheung, dcubed

duke@435 1 /*
duke@435 2 * Copyright 1998-2007 Sun Microsystems, Inc. All Rights Reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 class BasicLock VALUE_OBJ_CLASS_SPEC {
duke@435 26 friend class VMStructs;
duke@435 27 private:
duke@435 28 volatile markOop _displaced_header;
duke@435 29 public:
duke@435 30 markOop displaced_header() const { return _displaced_header; }
duke@435 31 void set_displaced_header(markOop header) { _displaced_header = header; }
duke@435 32
duke@435 33 void print_on(outputStream* st) const;
duke@435 34
duke@435 35 // move a basic lock (used during deoptimization
duke@435 36 void move_to(oop obj, BasicLock* dest);
duke@435 37
duke@435 38 static int displaced_header_offset_in_bytes() { return offset_of(BasicLock, _displaced_header); }
duke@435 39 };
duke@435 40
duke@435 41 // A BasicObjectLock associates a specific Java object with a BasicLock.
duke@435 42 // It is currently embedded in an interpreter frame.
duke@435 43
duke@435 44 // Because some machines have alignment restrictions on the control stack,
duke@435 45 // the actual space allocated by the interpreter may include padding words
duke@435 46 // after the end of the BasicObjectLock. Also, in order to guarantee
duke@435 47 // alignment of the embedded BasicLock objects on such machines, we
duke@435 48 // put the embedded BasicLock at the beginning of the struct.
duke@435 49
duke@435 50 class BasicObjectLock VALUE_OBJ_CLASS_SPEC {
duke@435 51 friend class VMStructs;
duke@435 52 private:
duke@435 53 BasicLock _lock; // the lock, must be double word aligned
duke@435 54 oop _obj; // object holds the lock;
duke@435 55
duke@435 56 public:
duke@435 57 // Manipulation
duke@435 58 oop obj() const { return _obj; }
duke@435 59 void set_obj(oop obj) { _obj = obj; }
duke@435 60 BasicLock* lock() { return &_lock; }
duke@435 61
duke@435 62 // Note: Use frame::interpreter_frame_monitor_size() for the size of BasicObjectLocks
duke@435 63 // in interpreter activation frames since it includes machine-specific padding.
duke@435 64 static int size() { return sizeof(BasicObjectLock)/wordSize; }
duke@435 65
duke@435 66 // GC support
duke@435 67 void oops_do(OopClosure* f) { f->do_oop(&_obj); }
duke@435 68
duke@435 69 static int obj_offset_in_bytes() { return offset_of(BasicObjectLock, _obj); }
duke@435 70 static int lock_offset_in_bytes() { return offset_of(BasicObjectLock, _lock); }
duke@435 71 };
duke@435 72
duke@435 73 class ObjectMonitor;
duke@435 74
duke@435 75 class ObjectSynchronizer : AllStatic {
duke@435 76 friend class VMStructs;
duke@435 77 public:
duke@435 78 typedef enum {
duke@435 79 owner_self,
duke@435 80 owner_none,
duke@435 81 owner_other
duke@435 82 } LockOwnership;
duke@435 83 // exit must be implemented non-blocking, since the compiler cannot easily handle
duke@435 84 // deoptimization at monitor exit. Hence, it does not take a Handle argument.
duke@435 85
duke@435 86 // This is full version of monitor enter and exit. I choose not
duke@435 87 // to use enter() and exit() in order to make sure user be ware
duke@435 88 // of the performance and semantics difference. They are normally
duke@435 89 // used by ObjectLocker etc. The interpreter and compiler use
duke@435 90 // assembly copies of these routines. Please keep them synchornized.
duke@435 91 //
duke@435 92 // attempt_rebias flag is used by UseBiasedLocking implementation
duke@435 93 static void fast_enter (Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS);
duke@435 94 static void fast_exit (oop obj, BasicLock* lock, Thread* THREAD);
duke@435 95
duke@435 96 // WARNING: They are ONLY used to handle the slow cases. They should
duke@435 97 // only be used when the fast cases failed. Use of these functions
duke@435 98 // without previous fast case check may cause fatal error.
duke@435 99 static void slow_enter (Handle obj, BasicLock* lock, TRAPS);
duke@435 100 static void slow_exit (oop obj, BasicLock* lock, Thread* THREAD);
duke@435 101
duke@435 102 // Used only to handle jni locks or other unmatched monitor enter/exit
duke@435 103 // Internally they will use heavy weight monitor.
duke@435 104 static void jni_enter (Handle obj, TRAPS);
duke@435 105 static bool jni_try_enter(Handle obj, Thread* THREAD); // Implements Unsafe.tryMonitorEnter
duke@435 106 static void jni_exit (oop obj, Thread* THREAD);
duke@435 107
duke@435 108 // Handle all interpreter, compiler and jni cases
duke@435 109 static void wait (Handle obj, jlong millis, TRAPS);
duke@435 110 static void notify (Handle obj, TRAPS);
duke@435 111 static void notifyall (Handle obj, TRAPS);
duke@435 112
duke@435 113 // Special internal-use-only method for use by JVM infrastructure
duke@435 114 // that needs to wait() on a java-level object but that can't risk
duke@435 115 // throwing unexpected InterruptedExecutionExceptions.
duke@435 116 static void waitUninterruptibly (Handle obj, jlong Millis, Thread * THREAD) ;
duke@435 117
duke@435 118 // used by classloading to free classloader object lock,
duke@435 119 // wait on an internal lock, and reclaim original lock
duke@435 120 // with original recursion count
duke@435 121 static intptr_t complete_exit (Handle obj, TRAPS);
duke@435 122 static void reenter (Handle obj, intptr_t recursion, TRAPS);
duke@435 123
duke@435 124 // thread-specific and global objectMonitor free list accessors
duke@435 125 static ObjectMonitor * omAlloc (Thread * Self) ;
duke@435 126 static void omRelease (Thread * Self, ObjectMonitor * m) ;
duke@435 127 static void omFlush (Thread * Self) ;
duke@435 128
duke@435 129 // Inflate light weight monitor to heavy weight monitor
duke@435 130 static ObjectMonitor* inflate(Thread * Self, oop obj);
duke@435 131 // This version is only for internal use
duke@435 132 static ObjectMonitor* inflate_helper(oop obj);
duke@435 133
duke@435 134 // Returns the identity hash value for an oop
duke@435 135 // NOTE: It may cause monitor inflation
duke@435 136 static intptr_t identity_hash_value_for(Handle obj);
duke@435 137 static intptr_t FastHashCode (Thread * Self, oop obj) ;
duke@435 138
duke@435 139 // java.lang.Thread support
duke@435 140 static bool current_thread_holds_lock(JavaThread* thread, Handle h_obj);
duke@435 141 static LockOwnership query_lock_ownership(JavaThread * self, Handle h_obj);
duke@435 142
duke@435 143 static JavaThread* get_lock_owner(Handle h_obj, bool doLock);
duke@435 144
duke@435 145 // JNI detach support
duke@435 146 static void release_monitors_owned_by_thread(TRAPS);
duke@435 147 static void monitors_iterate(MonitorClosure* m);
duke@435 148
duke@435 149 // GC: we current use aggressive monitor deflation policy
duke@435 150 // Basically we deflate all monitors that are not busy.
duke@435 151 // An adaptive profile-based deflation policy could be used if needed
duke@435 152 static void deflate_idle_monitors();
duke@435 153 static void oops_do(OopClosure* f);
duke@435 154
duke@435 155 // debugging
duke@435 156 static void trace_locking(Handle obj, bool is_compiled, bool is_method, bool is_locking) PRODUCT_RETURN;
duke@435 157 static void verify() PRODUCT_RETURN;
duke@435 158 static int verify_objmon_isinpool(ObjectMonitor *addr) PRODUCT_RETURN0;
duke@435 159
duke@435 160 private:
duke@435 161 enum { _BLOCKSIZE = 128 };
duke@435 162 static ObjectMonitor* gBlockList;
duke@435 163 static ObjectMonitor * volatile gFreeList;
duke@435 164
duke@435 165 public:
duke@435 166 static void Initialize () ;
duke@435 167 static PerfCounter * _sync_ContendedLockAttempts ;
duke@435 168 static PerfCounter * _sync_FutileWakeups ;
duke@435 169 static PerfCounter * _sync_Parks ;
duke@435 170 static PerfCounter * _sync_EmptyNotifications ;
duke@435 171 static PerfCounter * _sync_Notifications ;
duke@435 172 static PerfCounter * _sync_SlowEnter ;
duke@435 173 static PerfCounter * _sync_SlowExit ;
duke@435 174 static PerfCounter * _sync_SlowNotify ;
duke@435 175 static PerfCounter * _sync_SlowNotifyAll ;
duke@435 176 static PerfCounter * _sync_FailedSpins ;
duke@435 177 static PerfCounter * _sync_SuccessfulSpins ;
duke@435 178 static PerfCounter * _sync_PrivateA ;
duke@435 179 static PerfCounter * _sync_PrivateB ;
duke@435 180 static PerfCounter * _sync_MonInCirculation ;
duke@435 181 static PerfCounter * _sync_MonScavenged ;
duke@435 182 static PerfCounter * _sync_Inflations ;
duke@435 183 static PerfCounter * _sync_Deflations ;
duke@435 184 static PerfLongVariable * _sync_MonExtant ;
duke@435 185
duke@435 186 public:
duke@435 187 static void RegisterSpinCallback (int (*)(intptr_t, int), intptr_t) ;
duke@435 188
duke@435 189 };
duke@435 190
duke@435 191 // ObjectLocker enforced balanced locking and can never thrown an
duke@435 192 // IllegalMonitorStateException. However, a pending exception may
duke@435 193 // have to pass through, and we must also be able to deal with
duke@435 194 // asynchronous exceptions. The caller is responsible for checking
duke@435 195 // the threads pending exception if needed.
duke@435 196 // doLock was added to support classloading with UnsyncloadClass which
duke@435 197 // requires flag based choice of locking the classloader lock.
duke@435 198 class ObjectLocker : public StackObj {
duke@435 199 private:
duke@435 200 Thread* _thread;
duke@435 201 Handle _obj;
duke@435 202 BasicLock _lock;
duke@435 203 bool _dolock; // default true
duke@435 204 public:
duke@435 205 ObjectLocker(Handle obj, Thread* thread, bool doLock = true);
duke@435 206 ~ObjectLocker();
duke@435 207
duke@435 208 // Monitor behavior
duke@435 209 void wait (TRAPS) { ObjectSynchronizer::wait (_obj, 0, CHECK); } // wait forever
duke@435 210 void notify_all(TRAPS) { ObjectSynchronizer::notifyall(_obj, CHECK); }
duke@435 211 void waitUninterruptibly (TRAPS) { ObjectSynchronizer::waitUninterruptibly (_obj, 0, CHECK);}
duke@435 212 // complete_exit gives up lock completely, returning recursion count
duke@435 213 // reenter reclaims lock with original recursion count
duke@435 214 intptr_t complete_exit(TRAPS) { return ObjectSynchronizer::complete_exit(_obj, CHECK_0); }
duke@435 215 void reenter(intptr_t recursion, TRAPS) { ObjectSynchronizer::reenter(_obj, recursion, CHECK); }
duke@435 216 };

mercurial