src/share/vm/runtime/mutex.hpp

Wed, 02 Jul 2008 12:55:16 -0700

author
xdono
date
Wed, 02 Jul 2008 12:55:16 -0700
changeset 631
d1605aabd0a1
parent 490
2a8eb116ebbe
child 1040
98cb887364d3
permissions
-rw-r--r--

6719955: Update copyright year
Summary: Update copyright year for files that have been modified in 2008
Reviewed-by: ohair, tbell

duke@435 1 /*
xdono@631 2 * Copyright 1998-2008 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 // The SplitWord construct allows us to colocate the contention queue
duke@435 26 // (cxq) with the lock-byte. The queue elements are ParkEvents, which are
duke@435 27 // always aligned on 256-byte addresses - the least significant byte of
duke@435 28 // a ParkEvent is always 0. Colocating the lock-byte with the queue
duke@435 29 // allows us to easily avoid what would otherwise be a race in lock()
duke@435 30 // if we were to use two completely separate fields for the contention queue
duke@435 31 // and the lock indicator. Specifically, colocation renders us immune
duke@435 32 // from the race where a thread might enqueue itself in the lock() slow-path
duke@435 33 // immediately after the lock holder drops the outer lock in the unlock()
duke@435 34 // fast-path.
duke@435 35 //
duke@435 36 // Colocation allows us to use a fast-path unlock() form that uses
duke@435 37 // A MEMBAR instead of a CAS. MEMBAR has lower local latency than CAS
duke@435 38 // on many platforms.
duke@435 39 //
duke@435 40 // See:
duke@435 41 // + http://blogs.sun.com/dave/entry/biased_locking_in_hotspot
duke@435 42 // + http://blogs.sun.com/dave/resource/synchronization-public2.pdf
duke@435 43 //
duke@435 44 // Note that we're *not* using word-tearing the classic sense.
duke@435 45 // The lock() fast-path will CAS the lockword and the unlock()
duke@435 46 // fast-path will store into the lock-byte colocated within the lockword.
duke@435 47 // We depend on the fact that all our reference platforms have
duke@435 48 // coherent and atomic byte accesses. More precisely, byte stores
duke@435 49 // interoperate in a safe, sane, and expected manner with respect to
duke@435 50 // CAS, ST and LDs to the full-word containing the byte.
duke@435 51 // If you're porting HotSpot to a platform where that isn't the case
duke@435 52 // then you'll want change the unlock() fast path from:
duke@435 53 // STB;MEMBAR #storeload; LDN
duke@435 54 // to a full-word CAS of the lockword.
duke@435 55
duke@435 56
duke@435 57 union SplitWord { // full-word with separately addressable LSB
duke@435 58 volatile intptr_t FullWord ;
duke@435 59 volatile void * Address ;
duke@435 60 volatile jbyte Bytes [sizeof(intptr_t)] ;
duke@435 61 } ;
duke@435 62
duke@435 63 // Endian-ness ... index of least-significant byte in SplitWord.Bytes[]
duke@435 64 #ifdef AMD64 // little
duke@435 65 #define _LSBINDEX 0
duke@435 66 #else
duke@435 67 #if IA32 // little
duke@435 68 #define _LSBINDEX 0
duke@435 69 #else
duke@435 70 #ifdef SPARC // big
duke@435 71 #define _LSBINDEX (sizeof(intptr_t)-1)
duke@435 72 #else
duke@435 73 #error "unknown architecture"
duke@435 74 #endif
duke@435 75 #endif
duke@435 76 #endif
duke@435 77
duke@435 78 class ParkEvent ;
duke@435 79
duke@435 80 // See orderAccess.hpp. We assume throughout the VM that mutex lock and
duke@435 81 // try_lock do fence-lock-acquire, and that unlock does a release-unlock,
duke@435 82 // *in that order*. If their implementations change such that these
duke@435 83 // assumptions are violated, a whole lot of code will break.
duke@435 84
xlu@490 85 // The default length of monitor name is choosen to be 64 to avoid false sharing.
xlu@490 86 static const int MONITOR_NAME_LEN = 64;
xlu@490 87
duke@435 88 class Monitor : public CHeapObj {
duke@435 89
duke@435 90 public:
duke@435 91 // A special lock: Is a lock where you are guaranteed not to block while you are
duke@435 92 // holding it, i.e., no vm operation can happen, taking other locks, etc.
duke@435 93 // NOTE: It is critical that the rank 'special' be the lowest (earliest)
duke@435 94 // (except for "event"?) for the deadlock dection to work correctly.
duke@435 95 // The rank native is only for use in Mutex's created by JVM_RawMonitorCreate,
duke@435 96 // which being external to the VM are not subject to deadlock detection.
duke@435 97 // The rank safepoint is used only for synchronization in reaching a
duke@435 98 // safepoint and leaving a safepoint. It is only used for the Safepoint_lock
duke@435 99 // currently. While at a safepoint no mutexes of rank safepoint are held
duke@435 100 // by any thread.
duke@435 101 // The rank named "leaf" is probably historical (and should
duke@435 102 // be changed) -- mutexes of this rank aren't really leaf mutexes
duke@435 103 // at all.
duke@435 104 enum lock_types {
duke@435 105 event,
duke@435 106 special,
duke@435 107 suspend_resume,
duke@435 108 leaf = suspend_resume + 2,
duke@435 109 safepoint = leaf + 10,
duke@435 110 barrier = safepoint + 1,
duke@435 111 nonleaf = barrier + 1,
duke@435 112 max_nonleaf = nonleaf + 900,
duke@435 113 native = max_nonleaf + 1
duke@435 114 };
duke@435 115
duke@435 116 // The WaitSet and EntryList linked lists are composed of ParkEvents.
duke@435 117 // I use ParkEvent instead of threads as ParkEvents are immortal and
duke@435 118 // type-stable, meaning we can safely unpark() a possibly stale
duke@435 119 // list element in the unlock()-path.
duke@435 120
duke@435 121 protected: // Monitor-Mutex metadata
duke@435 122 SplitWord _LockWord ; // Contention queue (cxq) colocated with Lock-byte
duke@435 123 enum LockWordBits { _LBIT=1 } ;
duke@435 124 Thread * volatile _owner; // The owner of the lock
duke@435 125 // Consider sequestering _owner on its own $line
duke@435 126 // to aid future synchronization mechanisms.
duke@435 127 ParkEvent * volatile _EntryList ; // List of threads waiting for entry
duke@435 128 ParkEvent * volatile _OnDeck ; // heir-presumptive
duke@435 129 volatile intptr_t _WaitLock [1] ; // Protects _WaitSet
duke@435 130 ParkEvent * volatile _WaitSet ; // LL of ParkEvents
duke@435 131 volatile bool _snuck; // Used for sneaky locking (evil).
duke@435 132 int NotifyCount ; // diagnostic assist
xlu@490 133 char _name[MONITOR_NAME_LEN]; // Name of mutex
duke@435 134
duke@435 135 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
duke@435 136 #ifndef PRODUCT
duke@435 137 bool _allow_vm_block;
duke@435 138 debug_only(int _rank;) // rank (to avoid/detect potential deadlocks)
duke@435 139 debug_only(Monitor * _next;) // Used by a Thread to link up owned locks
duke@435 140 debug_only(Thread* _last_owner;) // the last thread to own the lock
duke@435 141 debug_only(static bool contains(Monitor * locks, Monitor * lock);)
duke@435 142 debug_only(static Monitor * get_least_ranked_lock(Monitor * locks);)
duke@435 143 debug_only(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);)
duke@435 144 #endif
duke@435 145
duke@435 146 void set_owner_implementation(Thread* owner) PRODUCT_RETURN;
duke@435 147 void check_prelock_state (Thread* thread) PRODUCT_RETURN;
duke@435 148 void check_block_state (Thread* thread) PRODUCT_RETURN;
duke@435 149
duke@435 150 // platform-dependent support code can go here (in os_<os_family>.cpp)
duke@435 151 public:
duke@435 152 enum {
duke@435 153 _no_safepoint_check_flag = true,
duke@435 154 _allow_vm_block_flag = true,
duke@435 155 _as_suspend_equivalent_flag = true
duke@435 156 };
duke@435 157
duke@435 158 enum WaitResults {
duke@435 159 CONDVAR_EVENT, // Wait returned because of condition variable notification
duke@435 160 INTERRUPT_EVENT, // Wait returned because waiting thread was interrupted
duke@435 161 NUMBER_WAIT_RESULTS
duke@435 162 };
duke@435 163
duke@435 164 private:
duke@435 165 int TrySpin (Thread * Self) ;
duke@435 166 int TryLock () ;
duke@435 167 int TryFast () ;
duke@435 168 int AcquireOrPush (ParkEvent * ev) ;
duke@435 169 void IUnlock (bool RelaxAssert) ;
duke@435 170 void ILock (Thread * Self) ;
duke@435 171 int IWait (Thread * Self, jlong timo);
duke@435 172 int ILocked () ;
duke@435 173
duke@435 174 protected:
xlu@490 175 static void ClearMonitor (Monitor * m, const char* name = NULL) ;
duke@435 176 Monitor() ;
duke@435 177
duke@435 178 public:
duke@435 179 Monitor(int rank, const char *name, bool allow_vm_block=false);
duke@435 180 ~Monitor();
duke@435 181
duke@435 182 // Wait until monitor is notified (or times out).
duke@435 183 // Defaults are to make safepoint checks, wait time is forever (i.e.,
duke@435 184 // zero), and not a suspend-equivalent condition. Returns true if wait
duke@435 185 // times out; otherwise returns false.
duke@435 186 bool wait(bool no_safepoint_check = !_no_safepoint_check_flag,
duke@435 187 long timeout = 0,
duke@435 188 bool as_suspend_equivalent = !_as_suspend_equivalent_flag);
duke@435 189 bool notify();
duke@435 190 bool notify_all();
duke@435 191
duke@435 192
duke@435 193 void lock(); // prints out warning if VM thread blocks
duke@435 194 void lock(Thread *thread); // overloaded with current thread
duke@435 195 void unlock();
duke@435 196 bool is_locked() const { return _owner != NULL; }
duke@435 197
duke@435 198 bool try_lock(); // Like lock(), but unblocking. It returns false instead
duke@435 199
duke@435 200 // Lock without safepoint check. Should ONLY be used by safepoint code and other code
duke@435 201 // that is guaranteed not to block while running inside the VM.
duke@435 202 void lock_without_safepoint_check();
duke@435 203 void lock_without_safepoint_check (Thread * Self) ;
duke@435 204
duke@435 205 // Current owner - not not MT-safe. Can only be used to guarantee that
duke@435 206 // the current running thread owns the lock
duke@435 207 Thread* owner() const { return _owner; }
duke@435 208 bool owned_by_self() const;
duke@435 209
duke@435 210 // Support for JVM_RawMonitorEnter & JVM_RawMonitorExit. These can be called by
duke@435 211 // non-Java thread. (We should really have a RawMonitor abstraction)
duke@435 212 void jvm_raw_lock();
duke@435 213 void jvm_raw_unlock();
duke@435 214 const char *name() const { return _name; }
duke@435 215
duke@435 216 void print_on_error(outputStream* st) const;
duke@435 217
duke@435 218 #ifndef PRODUCT
duke@435 219 void print_on(outputStream* st) const;
duke@435 220 void print() const { print_on(tty); }
duke@435 221 debug_only(int rank() const { return _rank; })
duke@435 222 bool allow_vm_block() { return _allow_vm_block; }
duke@435 223
duke@435 224 debug_only(Monitor *next() const { return _next; })
duke@435 225 debug_only(void set_next(Monitor *next) { _next = next; })
duke@435 226 #endif
duke@435 227
duke@435 228 void set_owner(Thread* owner) {
duke@435 229 #ifndef PRODUCT
duke@435 230 set_owner_implementation(owner);
duke@435 231 debug_only(void verify_Monitor(Thread* thr));
duke@435 232 #else
duke@435 233 _owner = owner;
duke@435 234 #endif
duke@435 235 }
duke@435 236
duke@435 237 };
duke@435 238
duke@435 239 // Normally we'd expect Monitor to extend Mutex in the sense that a monitor
duke@435 240 // constructed from pthreads primitives might extend a mutex by adding
duke@435 241 // a condvar and some extra metadata. In fact this was the case until J2SE7.
duke@435 242 //
duke@435 243 // Currently, however, the base object is a monitor. Monitor contains all the
duke@435 244 // logic for wait(), notify(), etc. Mutex extends monitor and restricts the
duke@435 245 // visiblity of wait(), notify(), and notify_all().
duke@435 246 //
duke@435 247 // Another viable alternative would have been to have Monitor extend Mutex and
duke@435 248 // implement all the normal mutex and wait()-notify() logic in Mutex base class.
duke@435 249 // The wait()-notify() facility would be exposed via special protected member functions
duke@435 250 // (e.g., _Wait() and _Notify()) in Mutex. Monitor would extend Mutex and expose wait()
duke@435 251 // as a call to _Wait(). That is, the public wait() would be a wrapper for the protected
duke@435 252 // _Wait().
duke@435 253 //
duke@435 254 // An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead.
duke@435 255 // After all, monitors are sufficient for Java-level synchronization. At one point in time
duke@435 256 // there may have been some benefit to having distinct mutexes and monitors, but that time
duke@435 257 // has past.
duke@435 258 //
duke@435 259 // The Mutex/Monitor design parallels that of Java-monitors, being based on
duke@435 260 // thread-specific park-unpark platform-specific primitives.
duke@435 261
duke@435 262
duke@435 263 class Mutex : public Monitor { // degenerate Monitor
duke@435 264 public:
duke@435 265 Mutex (int rank, const char *name, bool allow_vm_block=false);
duke@435 266 ~Mutex () ;
duke@435 267 private:
duke@435 268 bool notify () { ShouldNotReachHere(); return false; }
duke@435 269 bool notify_all() { ShouldNotReachHere(); return false; }
duke@435 270 bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) {
duke@435 271 ShouldNotReachHere() ;
duke@435 272 return false ;
duke@435 273 }
duke@435 274 };
duke@435 275
duke@435 276 /*
duke@435 277 * Per-thread blocking support for JSR166. See the Java-level
duke@435 278 * Documentation for rationale. Basically, park acts like wait, unpark
duke@435 279 * like notify.
duke@435 280 *
duke@435 281 * 6271289 --
duke@435 282 * To avoid errors where an os thread expires but the JavaThread still
duke@435 283 * exists, Parkers are immortal (type-stable) and are recycled across
duke@435 284 * new threads. This parallels the ParkEvent implementation.
duke@435 285 * Because park-unpark allow spurious wakeups it is harmless if an
duke@435 286 * unpark call unparks a new thread using the old Parker reference.
duke@435 287 *
duke@435 288 * In the future we'll want to think about eliminating Parker and using
duke@435 289 * ParkEvent instead. There's considerable duplication between the two
duke@435 290 * services.
duke@435 291 *
duke@435 292 */
duke@435 293
duke@435 294 class Parker : public os::PlatformParker {
duke@435 295 private:
duke@435 296 volatile int _counter ;
duke@435 297 Parker * FreeNext ;
duke@435 298 JavaThread * AssociatedWith ; // Current association
duke@435 299
duke@435 300 public:
duke@435 301 Parker() : PlatformParker() {
duke@435 302 _counter = 0 ;
duke@435 303 FreeNext = NULL ;
duke@435 304 AssociatedWith = NULL ;
duke@435 305 }
duke@435 306 protected:
duke@435 307 ~Parker() { ShouldNotReachHere(); }
duke@435 308 public:
duke@435 309 // For simplicity of interface with Java, all forms of park (indefinite,
duke@435 310 // relative, and absolute) are multiplexed into one call.
duke@435 311 void park(bool isAbsolute, jlong time);
duke@435 312 void unpark();
duke@435 313
duke@435 314 // Lifecycle operators
duke@435 315 static Parker * Allocate (JavaThread * t) ;
duke@435 316 static void Release (Parker * e) ;
duke@435 317 private:
duke@435 318 static Parker * volatile FreeList ;
duke@435 319 static volatile int ListLock ;
duke@435 320 };

mercurial