src/share/vm/runtime/mutex.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_RUNTIME_MUTEX_HPP
aoqi@0 26 #define SHARE_VM_RUNTIME_MUTEX_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/os.hpp"
aoqi@0 30 #include "utilities/histogram.hpp"
aoqi@0 31
aoqi@0 32 // The SplitWord construct allows us to colocate the contention queue
aoqi@0 33 // (cxq) with the lock-byte. The queue elements are ParkEvents, which are
aoqi@0 34 // always aligned on 256-byte addresses - the least significant byte of
aoqi@0 35 // a ParkEvent is always 0. Colocating the lock-byte with the queue
aoqi@0 36 // allows us to easily avoid what would otherwise be a race in lock()
aoqi@0 37 // if we were to use two completely separate fields for the contention queue
aoqi@0 38 // and the lock indicator. Specifically, colocation renders us immune
aoqi@0 39 // from the race where a thread might enqueue itself in the lock() slow-path
aoqi@0 40 // immediately after the lock holder drops the outer lock in the unlock()
aoqi@0 41 // fast-path.
aoqi@0 42 //
aoqi@0 43 // Colocation allows us to use a fast-path unlock() form that uses
aoqi@0 44 // A MEMBAR instead of a CAS. MEMBAR has lower local latency than CAS
aoqi@0 45 // on many platforms.
aoqi@0 46 //
aoqi@0 47 // See:
aoqi@0 48 // + http://blogs.sun.com/dave/entry/biased_locking_in_hotspot
aoqi@0 49 // + http://blogs.sun.com/dave/resource/synchronization-public2.pdf
aoqi@0 50 //
aoqi@0 51 // Note that we're *not* using word-tearing the classic sense.
aoqi@0 52 // The lock() fast-path will CAS the lockword and the unlock()
aoqi@0 53 // fast-path will store into the lock-byte colocated within the lockword.
aoqi@0 54 // We depend on the fact that all our reference platforms have
aoqi@0 55 // coherent and atomic byte accesses. More precisely, byte stores
aoqi@0 56 // interoperate in a safe, sane, and expected manner with respect to
aoqi@0 57 // CAS, ST and LDs to the full-word containing the byte.
aoqi@0 58 // If you're porting HotSpot to a platform where that isn't the case
aoqi@0 59 // then you'll want change the unlock() fast path from:
aoqi@0 60 // STB;MEMBAR #storeload; LDN
aoqi@0 61 // to a full-word CAS of the lockword.
aoqi@0 62
aoqi@0 63
aoqi@0 64 union SplitWord { // full-word with separately addressable LSB
aoqi@0 65 volatile intptr_t FullWord ;
aoqi@0 66 volatile void * Address ;
aoqi@0 67 volatile jbyte Bytes [sizeof(intptr_t)] ;
aoqi@0 68 } ;
aoqi@0 69
aoqi@0 70 // Endian-ness ... index of least-significant byte in SplitWord.Bytes[]
aoqi@0 71 #ifdef VM_LITTLE_ENDIAN
aoqi@0 72 #define _LSBINDEX 0
aoqi@0 73 #else
aoqi@0 74 #define _LSBINDEX (sizeof(intptr_t)-1)
aoqi@0 75 #endif
aoqi@0 76
aoqi@0 77 class ParkEvent ;
aoqi@0 78
aoqi@0 79 // See orderAccess.hpp. We assume throughout the VM that mutex lock and
aoqi@0 80 // try_lock do fence-lock-acquire, and that unlock does a release-unlock,
aoqi@0 81 // *in that order*. If their implementations change such that these
aoqi@0 82 // assumptions are violated, a whole lot of code will break.
aoqi@0 83
aoqi@0 84 // The default length of monitor name is chosen to be 64 to avoid false sharing.
aoqi@0 85 static const int MONITOR_NAME_LEN = 64;
aoqi@0 86
aoqi@0 87 class Monitor : public CHeapObj<mtInternal> {
aoqi@0 88
aoqi@0 89 public:
aoqi@0 90 // A special lock: Is a lock where you are guaranteed not to block while you are
aoqi@0 91 // holding it, i.e., no vm operation can happen, taking other locks, etc.
aoqi@0 92 // NOTE: It is critical that the rank 'special' be the lowest (earliest)
aoqi@0 93 // (except for "event"?) for the deadlock dection to work correctly.
aoqi@0 94 // The rank native is only for use in Mutex's created by JVM_RawMonitorCreate,
aoqi@0 95 // which being external to the VM are not subject to deadlock detection.
aoqi@0 96 // The rank safepoint is used only for synchronization in reaching a
aoqi@0 97 // safepoint and leaving a safepoint. It is only used for the Safepoint_lock
aoqi@0 98 // currently. While at a safepoint no mutexes of rank safepoint are held
aoqi@0 99 // by any thread.
aoqi@0 100 // The rank named "leaf" is probably historical (and should
aoqi@0 101 // be changed) -- mutexes of this rank aren't really leaf mutexes
aoqi@0 102 // at all.
aoqi@0 103 enum lock_types {
aoqi@0 104 event,
aoqi@0 105 special,
aoqi@0 106 suspend_resume,
aoqi@0 107 leaf = suspend_resume + 2,
aoqi@0 108 safepoint = leaf + 10,
aoqi@0 109 barrier = safepoint + 1,
aoqi@0 110 nonleaf = barrier + 1,
aoqi@0 111 max_nonleaf = nonleaf + 900,
aoqi@0 112 native = max_nonleaf + 1
aoqi@0 113 };
aoqi@0 114
aoqi@0 115 // The WaitSet and EntryList linked lists are composed of ParkEvents.
aoqi@0 116 // I use ParkEvent instead of threads as ParkEvents are immortal and
aoqi@0 117 // type-stable, meaning we can safely unpark() a possibly stale
aoqi@0 118 // list element in the unlock()-path.
aoqi@0 119
aoqi@0 120 protected: // Monitor-Mutex metadata
aoqi@0 121 SplitWord _LockWord ; // Contention queue (cxq) colocated with Lock-byte
aoqi@0 122 enum LockWordBits { _LBIT=1 } ;
aoqi@0 123 Thread * volatile _owner; // The owner of the lock
aoqi@0 124 // Consider sequestering _owner on its own $line
aoqi@0 125 // to aid future synchronization mechanisms.
aoqi@0 126 ParkEvent * volatile _EntryList ; // List of threads waiting for entry
aoqi@0 127 ParkEvent * volatile _OnDeck ; // heir-presumptive
aoqi@0 128 volatile intptr_t _WaitLock [1] ; // Protects _WaitSet
aoqi@0 129 ParkEvent * volatile _WaitSet ; // LL of ParkEvents
aoqi@0 130 volatile bool _snuck; // Used for sneaky locking (evil).
aoqi@0 131 int NotifyCount ; // diagnostic assist
aoqi@0 132 char _name[MONITOR_NAME_LEN]; // Name of mutex
aoqi@0 133
aoqi@0 134 // Debugging fields for naming, deadlock detection, etc. (some only used in debug mode)
aoqi@0 135 #ifndef PRODUCT
aoqi@0 136 bool _allow_vm_block;
aoqi@0 137 debug_only(int _rank;) // rank (to avoid/detect potential deadlocks)
aoqi@0 138 debug_only(Monitor * _next;) // Used by a Thread to link up owned locks
aoqi@0 139 debug_only(Thread* _last_owner;) // the last thread to own the lock
aoqi@0 140 debug_only(static bool contains(Monitor * locks, Monitor * lock);)
aoqi@0 141 debug_only(static Monitor * get_least_ranked_lock(Monitor * locks);)
aoqi@0 142 debug_only(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);)
aoqi@0 143 #endif
aoqi@0 144
aoqi@0 145 void set_owner_implementation(Thread* owner) PRODUCT_RETURN;
aoqi@0 146 void check_prelock_state (Thread* thread) PRODUCT_RETURN;
aoqi@0 147 void check_block_state (Thread* thread) PRODUCT_RETURN;
aoqi@0 148
aoqi@0 149 // platform-dependent support code can go here (in os_<os_family>.cpp)
aoqi@0 150 public:
aoqi@0 151 enum {
aoqi@0 152 _no_safepoint_check_flag = true,
aoqi@0 153 _allow_vm_block_flag = true,
aoqi@0 154 _as_suspend_equivalent_flag = true
aoqi@0 155 };
aoqi@0 156
aoqi@0 157 enum WaitResults {
aoqi@0 158 CONDVAR_EVENT, // Wait returned because of condition variable notification
aoqi@0 159 INTERRUPT_EVENT, // Wait returned because waiting thread was interrupted
aoqi@0 160 NUMBER_WAIT_RESULTS
aoqi@0 161 };
aoqi@0 162
aoqi@0 163 private:
aoqi@0 164 int TrySpin (Thread * Self) ;
aoqi@0 165 int TryLock () ;
aoqi@0 166 int TryFast () ;
aoqi@0 167 int AcquireOrPush (ParkEvent * ev) ;
aoqi@0 168 void IUnlock (bool RelaxAssert) ;
aoqi@0 169 void ILock (Thread * Self) ;
aoqi@0 170 int IWait (Thread * Self, jlong timo);
aoqi@0 171 int ILocked () ;
aoqi@0 172
aoqi@0 173 protected:
aoqi@0 174 static void ClearMonitor (Monitor * m, const char* name = NULL) ;
aoqi@0 175 Monitor() ;
aoqi@0 176
aoqi@0 177 public:
aoqi@0 178 Monitor(int rank, const char *name, bool allow_vm_block=false);
aoqi@0 179 ~Monitor();
aoqi@0 180
aoqi@0 181 // Wait until monitor is notified (or times out).
aoqi@0 182 // Defaults are to make safepoint checks, wait time is forever (i.e.,
aoqi@0 183 // zero), and not a suspend-equivalent condition. Returns true if wait
aoqi@0 184 // times out; otherwise returns false.
aoqi@0 185 bool wait(bool no_safepoint_check = !_no_safepoint_check_flag,
aoqi@0 186 long timeout = 0,
aoqi@0 187 bool as_suspend_equivalent = !_as_suspend_equivalent_flag);
aoqi@0 188 bool notify();
aoqi@0 189 bool notify_all();
aoqi@0 190
aoqi@0 191
aoqi@0 192 void lock(); // prints out warning if VM thread blocks
aoqi@0 193 void lock(Thread *thread); // overloaded with current thread
aoqi@0 194 void unlock();
aoqi@0 195 bool is_locked() const { return _owner != NULL; }
aoqi@0 196
aoqi@0 197 bool try_lock(); // Like lock(), but unblocking. It returns false instead
aoqi@0 198
aoqi@0 199 // Lock without safepoint check. Should ONLY be used by safepoint code and other code
aoqi@0 200 // that is guaranteed not to block while running inside the VM.
aoqi@0 201 void lock_without_safepoint_check();
aoqi@0 202 void lock_without_safepoint_check (Thread * Self) ;
aoqi@0 203
aoqi@0 204 // Current owner - not not MT-safe. Can only be used to guarantee that
aoqi@0 205 // the current running thread owns the lock
aoqi@0 206 Thread* owner() const { return _owner; }
aoqi@0 207 bool owned_by_self() const;
aoqi@0 208
aoqi@0 209 // Support for JVM_RawMonitorEnter & JVM_RawMonitorExit. These can be called by
aoqi@0 210 // non-Java thread. (We should really have a RawMonitor abstraction)
aoqi@0 211 void jvm_raw_lock();
aoqi@0 212 void jvm_raw_unlock();
aoqi@0 213 const char *name() const { return _name; }
aoqi@0 214
aoqi@0 215 void print_on_error(outputStream* st) const;
aoqi@0 216
aoqi@0 217 #ifndef PRODUCT
aoqi@0 218 void print_on(outputStream* st) const;
aoqi@0 219 void print() const { print_on(tty); }
aoqi@0 220 debug_only(int rank() const { return _rank; })
aoqi@0 221 bool allow_vm_block() { return _allow_vm_block; }
aoqi@0 222
aoqi@0 223 debug_only(Monitor *next() const { return _next; })
aoqi@0 224 debug_only(void set_next(Monitor *next) { _next = next; })
aoqi@0 225 #endif
aoqi@0 226
aoqi@0 227 void set_owner(Thread* owner) {
aoqi@0 228 #ifndef PRODUCT
aoqi@0 229 set_owner_implementation(owner);
aoqi@0 230 debug_only(void verify_Monitor(Thread* thr));
aoqi@0 231 #else
aoqi@0 232 _owner = owner;
aoqi@0 233 #endif
aoqi@0 234 }
aoqi@0 235
aoqi@0 236 };
aoqi@0 237
aoqi@0 238 // Normally we'd expect Monitor to extend Mutex in the sense that a monitor
aoqi@0 239 // constructed from pthreads primitives might extend a mutex by adding
aoqi@0 240 // a condvar and some extra metadata. In fact this was the case until J2SE7.
aoqi@0 241 //
aoqi@0 242 // Currently, however, the base object is a monitor. Monitor contains all the
aoqi@0 243 // logic for wait(), notify(), etc. Mutex extends monitor and restricts the
aoqi@0 244 // visiblity of wait(), notify(), and notify_all().
aoqi@0 245 //
aoqi@0 246 // Another viable alternative would have been to have Monitor extend Mutex and
aoqi@0 247 // implement all the normal mutex and wait()-notify() logic in Mutex base class.
aoqi@0 248 // The wait()-notify() facility would be exposed via special protected member functions
aoqi@0 249 // (e.g., _Wait() and _Notify()) in Mutex. Monitor would extend Mutex and expose wait()
aoqi@0 250 // as a call to _Wait(). That is, the public wait() would be a wrapper for the protected
aoqi@0 251 // _Wait().
aoqi@0 252 //
aoqi@0 253 // An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead.
aoqi@0 254 // After all, monitors are sufficient for Java-level synchronization. At one point in time
aoqi@0 255 // there may have been some benefit to having distinct mutexes and monitors, but that time
aoqi@0 256 // has past.
aoqi@0 257 //
aoqi@0 258 // The Mutex/Monitor design parallels that of Java-monitors, being based on
aoqi@0 259 // thread-specific park-unpark platform-specific primitives.
aoqi@0 260
aoqi@0 261
aoqi@0 262 class Mutex : public Monitor { // degenerate Monitor
aoqi@0 263 public:
aoqi@0 264 Mutex (int rank, const char *name, bool allow_vm_block=false);
aoqi@0 265 ~Mutex () ;
aoqi@0 266 private:
aoqi@0 267 bool notify () { ShouldNotReachHere(); return false; }
aoqi@0 268 bool notify_all() { ShouldNotReachHere(); return false; }
aoqi@0 269 bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) {
aoqi@0 270 ShouldNotReachHere() ;
aoqi@0 271 return false ;
aoqi@0 272 }
aoqi@0 273 };
aoqi@0 274
aoqi@0 275
aoqi@0 276 #endif // SHARE_VM_RUNTIME_MUTEX_HPP

mercurial