author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 46767 | hotspot/src/share/vm/runtime/mutex.hpp@e2bb2b8ff65a |
child 47634 | 6a0c42c40cd1 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
46685
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
2 |
* Copyright (c) 1998, 2017, 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 |
||
41710
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
84 |
// The default length of monitor name was originally chosen to be 64 to avoid |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
85 |
// false sharing. Now, PaddedMonitor is available for this purpose. |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
86 |
// TODO: Check if _name[MONITOR_NAME_LEN] should better get replaced by const char*. |
228
69939fa91efd
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
1
diff
changeset
|
87 |
static const int MONITOR_NAME_LEN = 64; |
69939fa91efd
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
1
diff
changeset
|
88 |
|
13195 | 89 |
class Monitor : public CHeapObj<mtInternal> { |
1 | 90 |
|
91 |
public: |
|
92 |
// A special lock: Is a lock where you are guaranteed not to block while you are |
|
46685
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
93 |
// holding it, i.e., no vm operation can happen, taking other (blocking) locks, etc. |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
94 |
// The rank 'access' is similar to 'special' and has the same restrictions on usage. |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
95 |
// It is reserved for locks that may be required in order to perform memory accesses |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
96 |
// that require special barriers, e.g. SATB GC barriers, that in turn uses locks. |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
97 |
// Since memory accesses should be able to be performed pretty much anywhere |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
98 |
// in the code, that requires locks required for performing accesses being |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
99 |
// inherently a bit more special than even locks of the 'special' rank. |
1 | 100 |
// NOTE: It is critical that the rank 'special' be the lowest (earliest) |
46685
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
101 |
// (except for "event" and "access") for the deadlock detection to work correctly. |
1 | 102 |
// The rank native is only for use in Mutex's created by JVM_RawMonitorCreate, |
103 |
// which being external to the VM are not subject to deadlock detection. |
|
104 |
// The rank safepoint is used only for synchronization in reaching a |
|
105 |
// safepoint and leaving a safepoint. It is only used for the Safepoint_lock |
|
106 |
// currently. While at a safepoint no mutexes of rank safepoint are held |
|
107 |
// by any thread. |
|
108 |
// The rank named "leaf" is probably historical (and should |
|
109 |
// be changed) -- mutexes of this rank aren't really leaf mutexes |
|
110 |
// at all. |
|
111 |
enum lock_types { |
|
112 |
event, |
|
46685
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
113 |
access = event + 1, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
114 |
special = access + 2, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
115 |
suspend_resume = special + 1, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
116 |
leaf = suspend_resume + 2, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
117 |
safepoint = leaf + 10, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
118 |
barrier = safepoint + 1, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
119 |
nonleaf = barrier + 1, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
120 |
max_nonleaf = nonleaf + 900, |
b218dfc2853a
8182703: Correct G1 barrier queue lock orderings
eosterlund
parents:
41710
diff
changeset
|
121 |
native = max_nonleaf + 1 |
1 | 122 |
}; |
123 |
||
124 |
// The WaitSet and EntryList linked lists are composed of ParkEvents. |
|
125 |
// I use ParkEvent instead of threads as ParkEvents are immortal and |
|
126 |
// type-stable, meaning we can safely unpark() a possibly stale |
|
127 |
// list element in the unlock()-path. |
|
128 |
||
129 |
protected: // Monitor-Mutex metadata |
|
130 |
SplitWord _LockWord ; // Contention queue (cxq) colocated with Lock-byte |
|
131 |
enum LockWordBits { _LBIT=1 } ; |
|
132 |
Thread * volatile _owner; // The owner of the lock |
|
133 |
// Consider sequestering _owner on its own $line |
|
134 |
// to aid future synchronization mechanisms. |
|
135 |
ParkEvent * volatile _EntryList ; // List of threads waiting for entry |
|
136 |
ParkEvent * volatile _OnDeck ; // heir-presumptive |
|
137 |
volatile intptr_t _WaitLock [1] ; // Protects _WaitSet |
|
138 |
ParkEvent * volatile _WaitSet ; // LL of ParkEvents |
|
139 |
volatile bool _snuck; // Used for sneaky locking (evil). |
|
140 |
int NotifyCount ; // diagnostic assist |
|
228
69939fa91efd
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
1
diff
changeset
|
141 |
char _name[MONITOR_NAME_LEN]; // Name of mutex |
1 | 142 |
|
143 |
// Debugging fields for naming, deadlock detection, etc. (some only used in debug mode) |
|
144 |
#ifndef PRODUCT |
|
145 |
bool _allow_vm_block; |
|
146 |
debug_only(int _rank;) // rank (to avoid/detect potential deadlocks) |
|
147 |
debug_only(Monitor * _next;) // Used by a Thread to link up owned locks |
|
148 |
debug_only(Thread* _last_owner;) // the last thread to own the lock |
|
149 |
debug_only(static bool contains(Monitor * locks, Monitor * lock);) |
|
150 |
debug_only(static Monitor * get_least_ranked_lock(Monitor * locks);) |
|
151 |
debug_only(Monitor * get_least_ranked_lock_besides_this(Monitor * locks);) |
|
152 |
#endif |
|
153 |
||
154 |
void set_owner_implementation(Thread* owner) PRODUCT_RETURN; |
|
155 |
void check_prelock_state (Thread* thread) PRODUCT_RETURN; |
|
156 |
void check_block_state (Thread* thread) PRODUCT_RETURN; |
|
157 |
||
158 |
// platform-dependent support code can go here (in os_<os_family>.cpp) |
|
159 |
public: |
|
160 |
enum { |
|
161 |
_no_safepoint_check_flag = true, |
|
162 |
_allow_vm_block_flag = true, |
|
163 |
_as_suspend_equivalent_flag = true |
|
164 |
}; |
|
165 |
||
28163
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
166 |
// Locks can be acquired with or without safepoint check. |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
167 |
// Monitor::lock and Monitor::lock_without_safepoint_check |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
168 |
// checks these flags when acquiring a lock to ensure |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
169 |
// consistent checking for each lock. |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
170 |
// A few existing locks will sometimes have a safepoint check and |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
171 |
// sometimes not, but these locks are set up in such a way to avoid deadlocks. |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
172 |
enum SafepointCheckRequired { |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
173 |
_safepoint_check_never, // Monitors with this value will cause errors |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
174 |
// when acquired with a safepoint check. |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
175 |
_safepoint_check_sometimes, // Certain locks are called sometimes with and |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
176 |
// sometimes without safepoint checks. These |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
177 |
// locks will not produce errors when locked. |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
178 |
_safepoint_check_always // Causes error if locked without a safepoint |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
179 |
// check. |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
180 |
}; |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
181 |
|
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
182 |
NOT_PRODUCT(SafepointCheckRequired _safepoint_check_required;) |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
183 |
|
1 | 184 |
enum WaitResults { |
185 |
CONDVAR_EVENT, // Wait returned because of condition variable notification |
|
186 |
INTERRUPT_EVENT, // Wait returned because waiting thread was interrupted |
|
187 |
NUMBER_WAIT_RESULTS |
|
188 |
}; |
|
189 |
||
190 |
private: |
|
191 |
int TrySpin (Thread * Self) ; |
|
192 |
int TryLock () ; |
|
193 |
int TryFast () ; |
|
194 |
int AcquireOrPush (ParkEvent * ev) ; |
|
195 |
void IUnlock (bool RelaxAssert) ; |
|
196 |
void ILock (Thread * Self) ; |
|
197 |
int IWait (Thread * Self, jlong timo); |
|
198 |
int ILocked () ; |
|
199 |
||
200 |
protected: |
|
228
69939fa91efd
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
1
diff
changeset
|
201 |
static void ClearMonitor (Monitor * m, const char* name = NULL) ; |
1 | 202 |
Monitor() ; |
203 |
||
204 |
public: |
|
28163
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
205 |
Monitor(int rank, const char *name, bool allow_vm_block = false, |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
206 |
SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
1 | 207 |
~Monitor(); |
208 |
||
209 |
// Wait until monitor is notified (or times out). |
|
210 |
// Defaults are to make safepoint checks, wait time is forever (i.e., |
|
211 |
// zero), and not a suspend-equivalent condition. Returns true if wait |
|
212 |
// times out; otherwise returns false. |
|
213 |
bool wait(bool no_safepoint_check = !_no_safepoint_check_flag, |
|
214 |
long timeout = 0, |
|
215 |
bool as_suspend_equivalent = !_as_suspend_equivalent_flag); |
|
216 |
bool notify(); |
|
217 |
bool notify_all(); |
|
218 |
||
219 |
||
220 |
void lock(); // prints out warning if VM thread blocks |
|
221 |
void lock(Thread *thread); // overloaded with current thread |
|
222 |
void unlock(); |
|
223 |
bool is_locked() const { return _owner != NULL; } |
|
224 |
||
225 |
bool try_lock(); // Like lock(), but unblocking. It returns false instead |
|
226 |
||
227 |
// Lock without safepoint check. Should ONLY be used by safepoint code and other code |
|
228 |
// that is guaranteed not to block while running inside the VM. |
|
229 |
void lock_without_safepoint_check(); |
|
230 |
void lock_without_safepoint_check (Thread * Self) ; |
|
231 |
||
232 |
// Current owner - not not MT-safe. Can only be used to guarantee that |
|
233 |
// the current running thread owns the lock |
|
234 |
Thread* owner() const { return _owner; } |
|
235 |
bool owned_by_self() const; |
|
236 |
||
237 |
// Support for JVM_RawMonitorEnter & JVM_RawMonitorExit. These can be called by |
|
238 |
// non-Java thread. (We should really have a RawMonitor abstraction) |
|
239 |
void jvm_raw_lock(); |
|
240 |
void jvm_raw_unlock(); |
|
241 |
const char *name() const { return _name; } |
|
242 |
||
243 |
void print_on_error(outputStream* st) const; |
|
244 |
||
245 |
#ifndef PRODUCT |
|
246 |
void print_on(outputStream* st) const; |
|
247 |
void print() const { print_on(tty); } |
|
248 |
debug_only(int rank() const { return _rank; }) |
|
249 |
bool allow_vm_block() { return _allow_vm_block; } |
|
250 |
||
251 |
debug_only(Monitor *next() const { return _next; }) |
|
252 |
debug_only(void set_next(Monitor *next) { _next = next; }) |
|
253 |
#endif |
|
254 |
||
255 |
void set_owner(Thread* owner) { |
|
256 |
#ifndef PRODUCT |
|
257 |
set_owner_implementation(owner); |
|
258 |
debug_only(void verify_Monitor(Thread* thr)); |
|
259 |
#else |
|
260 |
_owner = owner; |
|
261 |
#endif |
|
262 |
} |
|
263 |
||
264 |
}; |
|
265 |
||
41710
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
266 |
class PaddedMonitor : public Monitor { |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
267 |
enum { |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
268 |
CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Monitor), |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
269 |
PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1 |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
270 |
}; |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
271 |
char _padding[PADDING_LEN]; |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
272 |
public: |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
273 |
PaddedMonitor(int rank, const char *name, bool allow_vm_block = false, |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
274 |
SafepointCheckRequired safepoint_check_required = _safepoint_check_always) : |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
275 |
Monitor(rank, name, allow_vm_block, safepoint_check_required) {}; |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
276 |
}; |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
277 |
|
1 | 278 |
// Normally we'd expect Monitor to extend Mutex in the sense that a monitor |
279 |
// constructed from pthreads primitives might extend a mutex by adding |
|
280 |
// a condvar and some extra metadata. In fact this was the case until J2SE7. |
|
281 |
// |
|
282 |
// Currently, however, the base object is a monitor. Monitor contains all the |
|
283 |
// logic for wait(), notify(), etc. Mutex extends monitor and restricts the |
|
22551 | 284 |
// visibility of wait(), notify(), and notify_all(). |
1 | 285 |
// |
286 |
// Another viable alternative would have been to have Monitor extend Mutex and |
|
287 |
// implement all the normal mutex and wait()-notify() logic in Mutex base class. |
|
288 |
// The wait()-notify() facility would be exposed via special protected member functions |
|
289 |
// (e.g., _Wait() and _Notify()) in Mutex. Monitor would extend Mutex and expose wait() |
|
290 |
// as a call to _Wait(). That is, the public wait() would be a wrapper for the protected |
|
291 |
// _Wait(). |
|
292 |
// |
|
293 |
// An even better alternative is to simply eliminate Mutex:: and use Monitor:: instead. |
|
294 |
// After all, monitors are sufficient for Java-level synchronization. At one point in time |
|
295 |
// there may have been some benefit to having distinct mutexes and monitors, but that time |
|
296 |
// has past. |
|
297 |
// |
|
298 |
// The Mutex/Monitor design parallels that of Java-monitors, being based on |
|
299 |
// thread-specific park-unpark platform-specific primitives. |
|
300 |
||
301 |
||
302 |
class Mutex : public Monitor { // degenerate Monitor |
|
303 |
public: |
|
28163
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
304 |
Mutex(int rank, const char *name, bool allow_vm_block = false, |
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
22551
diff
changeset
|
305 |
SafepointCheckRequired safepoint_check_required = _safepoint_check_always); |
46767 | 306 |
// default destructor |
1 | 307 |
private: |
308 |
bool notify () { ShouldNotReachHere(); return false; } |
|
309 |
bool notify_all() { ShouldNotReachHere(); return false; } |
|
310 |
bool wait (bool no_safepoint_check, long timeout, bool as_suspend_equivalent) { |
|
311 |
ShouldNotReachHere() ; |
|
312 |
return false ; |
|
313 |
} |
|
314 |
}; |
|
315 |
||
41710
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
316 |
class PaddedMutex : public Mutex { |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
317 |
enum { |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
318 |
CACHE_LINE_PADDING = (int)DEFAULT_CACHE_LINE_SIZE - (int)sizeof(Mutex), |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
319 |
PADDING_LEN = CACHE_LINE_PADDING > 0 ? CACHE_LINE_PADDING : 1 |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
320 |
}; |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
321 |
char _padding[PADDING_LEN]; |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
322 |
public: |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
323 |
PaddedMutex(int rank, const char *name, bool allow_vm_block = false, |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
324 |
SafepointCheckRequired safepoint_check_required = _safepoint_check_always) : |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
325 |
Mutex(rank, name, allow_vm_block, safepoint_check_required) {}; |
b830f5141dbb
8166970: Adapt mutex padding according to DEFAULT_CACHE_LINE_SIZE
mdoerr
parents:
28163
diff
changeset
|
326 |
}; |
7397 | 327 |
|
328 |
#endif // SHARE_VM_RUNTIME_MUTEX_HPP |