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