Mercurial > hg > truffle
annotate src/share/vm/runtime/mutex.cpp @ 18454:5af19da6fae5
DelayedFrameMapBuilder: remove TrackedVirtualStackSlot.
author | Josef Eisl <josef.eisl@jku.at> |
---|---|
date | Mon, 17 Nov 2014 19:34:08 +0100 |
parents | 52b4284cb496 |
children | 7848fc12602b |
rev | line source |
---|---|
0 | 1 |
2 /* | |
17937
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
14223
diff
changeset
|
3 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. |
0 | 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
5 * | |
6 * This code is free software; you can redistribute it and/or modify it | |
7 * under the terms of the GNU General Public License version 2 only, as | |
8 * published by the Free Software Foundation. | |
9 * | |
10 * This code is distributed in the hope that it will be useful, but WITHOUT | |
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 * version 2 for more details (a copy is included in the LICENSE file that | |
14 * accompanied this code). | |
15 * | |
16 * You should have received a copy of the GNU General Public License version | |
17 * 2 along with this work; if not, write to the Free Software Foundation, | |
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
19 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1490
diff
changeset
|
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1490
diff
changeset
|
21 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1490
diff
changeset
|
22 * questions. |
0 | 23 * |
24 */ | |
25 | |
1972 | 26 #include "precompiled.hpp" |
27 #include "runtime/mutex.hpp" | |
28 #include "runtime/osThread.hpp" | |
7180
f34d701e952e
8003935: Simplify the needed includes for using Thread::current()
stefank
parents:
4872
diff
changeset
|
29 #include "runtime/thread.inline.hpp" |
1972 | 30 #include "utilities/events.hpp" |
31 #ifdef TARGET_OS_FAMILY_linux | |
32 # include "mutex_linux.inline.hpp" | |
33 #endif | |
34 #ifdef TARGET_OS_FAMILY_solaris | |
35 # include "mutex_solaris.inline.hpp" | |
36 #endif | |
37 #ifdef TARGET_OS_FAMILY_windows | |
38 # include "mutex_windows.inline.hpp" | |
39 #endif | |
3960 | 40 #ifdef TARGET_OS_FAMILY_bsd |
41 # include "mutex_bsd.inline.hpp" | |
42 #endif | |
0 | 43 |
17937
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
14223
diff
changeset
|
44 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
78bbf4d43a14
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
14223
diff
changeset
|
45 |
0 | 46 // o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o |
47 // | |
48 // Native Monitor-Mutex locking - theory of operations | |
49 // | |
50 // * Native Monitors are completely unrelated to Java-level monitors, | |
51 // although the "back-end" slow-path implementations share a common lineage. | |
52 // See objectMonitor:: in synchronizer.cpp. | |
53 // Native Monitors do *not* support nesting or recursion but otherwise | |
54 // they're basically Hoare-flavor monitors. | |
55 // | |
56 // * A thread acquires ownership of a Monitor/Mutex by CASing the LockByte | |
57 // in the _LockWord from zero to non-zero. Note that the _Owner field | |
58 // is advisory and is used only to verify that the thread calling unlock() | |
59 // is indeed the last thread to have acquired the lock. | |
60 // | |
61 // * Contending threads "push" themselves onto the front of the contention | |
62 // queue -- called the cxq -- with CAS and then spin/park. | |
63 // The _LockWord contains the LockByte as well as the pointer to the head | |
64 // of the cxq. Colocating the LockByte with the cxq precludes certain races. | |
65 // | |
66 // * Using a separately addressable LockByte allows for CAS:MEMBAR or CAS:0 | |
67 // idioms. We currently use MEMBAR in the uncontended unlock() path, as | |
68 // MEMBAR often has less latency than CAS. If warranted, we could switch to | |
69 // a CAS:0 mode, using timers to close the resultant race, as is done | |
70 // with Java Monitors in synchronizer.cpp. | |
71 // | |
72 // See the following for a discussion of the relative cost of atomics (CAS) | |
73 // MEMBAR, and ways to eliminate such instructions from the common-case paths: | |
74 // -- http://blogs.sun.com/dave/entry/biased_locking_in_hotspot | |
75 // -- http://blogs.sun.com/dave/resource/MustangSync.pdf | |
76 // -- http://blogs.sun.com/dave/resource/synchronization-public2.pdf | |
77 // -- synchronizer.cpp | |
78 // | |
79 // * Overall goals - desiderata | |
80 // 1. Minimize context switching | |
81 // 2. Minimize lock migration | |
82 // 3. Minimize CPI -- affinity and locality | |
83 // 4. Minimize the execution of high-latency instructions such as CAS or MEMBAR | |
84 // 5. Minimize outer lock hold times | |
85 // 6. Behave gracefully on a loaded system | |
86 // | |
87 // * Thread flow and list residency: | |
88 // | |
89 // Contention queue --> EntryList --> OnDeck --> Owner --> !Owner | |
90 // [..resident on monitor list..] | |
91 // [...........contending..................] | |
92 // | |
93 // -- The contention queue (cxq) contains recently-arrived threads (RATs). | |
94 // Threads on the cxq eventually drain into the EntryList. | |
95 // -- Invariant: a thread appears on at most one list -- cxq, EntryList | |
96 // or WaitSet -- at any one time. | |
97 // -- For a given monitor there can be at most one "OnDeck" thread at any | |
98 // given time but if needbe this particular invariant could be relaxed. | |
99 // | |
100 // * The WaitSet and EntryList linked lists are composed of ParkEvents. | |
101 // I use ParkEvent instead of threads as ParkEvents are immortal and | |
102 // type-stable, meaning we can safely unpark() a possibly stale | |
103 // list element in the unlock()-path. (That's benign). | |
104 // | |
105 // * Succession policy - providing for progress: | |
106 // | |
107 // As necessary, the unlock()ing thread identifies, unlinks, and unparks | |
108 // an "heir presumptive" tentative successor thread from the EntryList. | |
109 // This becomes the so-called "OnDeck" thread, of which there can be only | |
110 // one at any given time for a given monitor. The wakee will recontend | |
111 // for ownership of monitor. | |
112 // | |
113 // Succession is provided for by a policy of competitive handoff. | |
114 // The exiting thread does _not_ grant or pass ownership to the | |
115 // successor thread. (This is also referred to as "handoff" succession"). | |
116 // Instead the exiting thread releases ownership and possibly wakes | |
117 // a successor, so the successor can (re)compete for ownership of the lock. | |
118 // | |
119 // Competitive handoff provides excellent overall throughput at the expense | |
120 // of short-term fairness. If fairness is a concern then one remedy might | |
121 // be to add an AcquireCounter field to the monitor. After a thread acquires | |
122 // the lock it will decrement the AcquireCounter field. When the count | |
123 // reaches 0 the thread would reset the AcquireCounter variable, abdicate | |
124 // the lock directly to some thread on the EntryList, and then move itself to the | |
125 // tail of the EntryList. | |
126 // | |
127 // But in practice most threads engage or otherwise participate in resource | |
128 // bounded producer-consumer relationships, so lock domination is not usually | |
129 // a practical concern. Recall too, that in general it's easier to construct | |
130 // a fair lock from a fast lock, but not vice-versa. | |
131 // | |
132 // * The cxq can have multiple concurrent "pushers" but only one concurrent | |
133 // detaching thread. This mechanism is immune from the ABA corruption. | |
134 // More precisely, the CAS-based "push" onto cxq is ABA-oblivious. | |
135 // We use OnDeck as a pseudo-lock to enforce the at-most-one detaching | |
136 // thread constraint. | |
137 // | |
138 // * Taken together, the cxq and the EntryList constitute or form a | |
139 // single logical queue of threads stalled trying to acquire the lock. | |
140 // We use two distinct lists to reduce heat on the list ends. | |
141 // Threads in lock() enqueue onto cxq while threads in unlock() will | |
142 // dequeue from the EntryList. (c.f. Michael Scott's "2Q" algorithm). | |
143 // A key desideratum is to minimize queue & monitor metadata manipulation | |
144 // that occurs while holding the "outer" monitor lock -- that is, we want to | |
145 // minimize monitor lock holds times. | |
146 // | |
147 // The EntryList is ordered by the prevailing queue discipline and | |
148 // can be organized in any convenient fashion, such as a doubly-linked list or | |
149 // a circular doubly-linked list. If we need a priority queue then something akin | |
150 // to Solaris' sleepq would work nicely. Viz., | |
151 // -- http://agg.eng/ws/on10_nightly/source/usr/src/uts/common/os/sleepq.c. | |
152 // -- http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/os/sleepq.c | |
153 // Queue discipline is enforced at ::unlock() time, when the unlocking thread | |
154 // drains the cxq into the EntryList, and orders or reorders the threads on the | |
155 // EntryList accordingly. | |
156 // | |
157 // Barring "lock barging", this mechanism provides fair cyclic ordering, | |
158 // somewhat similar to an elevator-scan. | |
159 // | |
160 // * OnDeck | |
161 // -- For a given monitor there can be at most one OnDeck thread at any given | |
162 // instant. The OnDeck thread is contending for the lock, but has been | |
163 // unlinked from the EntryList and cxq by some previous unlock() operations. | |
164 // Once a thread has been designated the OnDeck thread it will remain so | |
165 // until it manages to acquire the lock -- being OnDeck is a stable property. | |
166 // -- Threads on the EntryList or cxq are _not allowed to attempt lock acquisition. | |
167 // -- OnDeck also serves as an "inner lock" as follows. Threads in unlock() will, after | |
168 // having cleared the LockByte and dropped the outer lock, attempt to "trylock" | |
169 // OnDeck by CASing the field from null to non-null. If successful, that thread | |
170 // is then responsible for progress and succession and can use CAS to detach and | |
171 // drain the cxq into the EntryList. By convention, only this thread, the holder of | |
172 // the OnDeck inner lock, can manipulate the EntryList or detach and drain the | |
173 // RATs on the cxq into the EntryList. This avoids ABA corruption on the cxq as | |
174 // we allow multiple concurrent "push" operations but restrict detach concurrency | |
175 // to at most one thread. Having selected and detached a successor, the thread then | |
176 // changes the OnDeck to refer to that successor, and then unparks the successor. | |
177 // That successor will eventually acquire the lock and clear OnDeck. Beware | |
178 // that the OnDeck usage as a lock is asymmetric. A thread in unlock() transiently | |
179 // "acquires" OnDeck, performs queue manipulations, passes OnDeck to some successor, | |
180 // and then the successor eventually "drops" OnDeck. Note that there's never | |
181 // any sense of contention on the inner lock, however. Threads never contend | |
182 // or wait for the inner lock. | |
183 // -- OnDeck provides for futile wakeup throttling a described in section 3.3 of | |
184 // See http://www.usenix.org/events/jvm01/full_papers/dice/dice.pdf | |
185 // In a sense, OnDeck subsumes the ObjectMonitor _Succ and ObjectWaiter | |
186 // TState fields found in Java-level objectMonitors. (See synchronizer.cpp). | |
187 // | |
188 // * Waiting threads reside on the WaitSet list -- wait() puts | |
189 // the caller onto the WaitSet. Notify() or notifyAll() simply | |
190 // transfers threads from the WaitSet to either the EntryList or cxq. | |
191 // Subsequent unlock() operations will eventually unpark the notifyee. | |
192 // Unparking a notifee in notify() proper is inefficient - if we were to do so | |
193 // it's likely the notifyee would simply impale itself on the lock held | |
194 // by the notifier. | |
195 // | |
196 // * The mechanism is obstruction-free in that if the holder of the transient | |
197 // OnDeck lock in unlock() is preempted or otherwise stalls, other threads | |
198 // can still acquire and release the outer lock and continue to make progress. | |
199 // At worst, waking of already blocked contending threads may be delayed, | |
200 // but nothing worse. (We only use "trylock" operations on the inner OnDeck | |
201 // lock). | |
202 // | |
203 // * Note that thread-local storage must be initialized before a thread | |
204 // uses Native monitors or mutexes. The native monitor-mutex subsystem | |
205 // depends on Thread::current(). | |
206 // | |
207 // * The monitor synchronization subsystem avoids the use of native | |
208 // synchronization primitives except for the narrow platform-specific | |
209 // park-unpark abstraction. See the comments in os_solaris.cpp regarding | |
210 // the semantics of park-unpark. Put another way, this monitor implementation | |
211 // depends only on atomic operations and park-unpark. The monitor subsystem | |
212 // manages all RUNNING->BLOCKED and BLOCKED->READY transitions while the | |
213 // underlying OS manages the READY<->RUN transitions. | |
214 // | |
215 // * The memory consistency model provide by lock()-unlock() is at least as | |
216 // strong or stronger than the Java Memory model defined by JSR-133. | |
217 // That is, we guarantee at least entry consistency, if not stronger. | |
218 // See http://g.oswego.edu/dl/jmm/cookbook.html. | |
219 // | |
220 // * Thread:: currently contains a set of purpose-specific ParkEvents: | |
221 // _MutexEvent, _ParkEvent, etc. A better approach might be to do away with | |
222 // the purpose-specific ParkEvents and instead implement a general per-thread | |
223 // stack of available ParkEvents which we could provision on-demand. The | |
224 // stack acts as a local cache to avoid excessive calls to ParkEvent::Allocate() | |
225 // and ::Release(). A thread would simply pop an element from the local stack before it | |
226 // enqueued or park()ed. When the contention was over the thread would | |
227 // push the no-longer-needed ParkEvent back onto its stack. | |
228 // | |
229 // * A slightly reduced form of ILock() and IUnlock() have been partially | |
230 // model-checked (Murphi) for safety and progress at T=1,2,3 and 4. | |
231 // It'd be interesting to see if TLA/TLC could be useful as well. | |
232 // | |
233 // * Mutex-Monitor is a low-level "leaf" subsystem. That is, the monitor | |
234 // code should never call other code in the JVM that might itself need to | |
235 // acquire monitors or mutexes. That's true *except* in the case of the | |
236 // ThreadBlockInVM state transition wrappers. The ThreadBlockInVM DTOR handles | |
237 // mutator reentry (ingress) by checking for a pending safepoint in which case it will | |
238 // call SafepointSynchronize::block(), which in turn may call Safepoint_lock->lock(), etc. | |
239 // In that particular case a call to lock() for a given Monitor can end up recursively | |
240 // calling lock() on another monitor. While distasteful, this is largely benign | |
241 // as the calls come from jacket that wraps lock(), and not from deep within lock() itself. | |
242 // | |
243 // It's unfortunate that native mutexes and thread state transitions were convolved. | |
244 // They're really separate concerns and should have remained that way. Melding | |
245 // them together was facile -- a bit too facile. The current implementation badly | |
246 // conflates the two concerns. | |
247 // | |
248 // * TODO-FIXME: | |
249 // | |
250 // -- Add DTRACE probes for contended acquire, contended acquired, contended unlock | |
251 // We should also add DTRACE probes in the ParkEvent subsystem for | |
252 // Park-entry, Park-exit, and Unpark. | |
253 // | |
254 // -- We have an excess of mutex-like constructs in the JVM, namely: | |
255 // 1. objectMonitors for Java-level synchronization (synchronizer.cpp) | |
256 // 2. low-level muxAcquire and muxRelease | |
257 // 3. low-level spinAcquire and spinRelease | |
258 // 4. native Mutex:: and Monitor:: | |
259 // 5. jvm_raw_lock() and _unlock() | |
260 // 6. JVMTI raw monitors -- distinct from (5) despite having a confusingly | |
261 // similar name. | |
262 // | |
263 // o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o | |
264 | |
265 | |
266 // CASPTR() uses the canonical argument order that dominates in the literature. | |
267 // Our internal cmpxchg_ptr() uses a bastardized ordering to accommodate Sun .il templates. | |
268 | |
269 #define CASPTR(a,c,s) intptr_t(Atomic::cmpxchg_ptr ((void *)(s),(void *)(a),(void *)(c))) | |
270 #define UNS(x) (uintptr_t(x)) | |
271 #define TRACE(m) { static volatile int ctr = 0 ; int x = ++ctr ; if ((x & (x-1))==0) { ::printf ("%d:%s\n", x, #m); ::fflush(stdout); }} | |
272 | |
273 // Simplistic low-quality Marsaglia SHIFT-XOR RNG. | |
274 // Bijective except for the trailing mask operation. | |
275 // Useful for spin loops as the compiler can't optimize it away. | |
276 | |
277 static inline jint MarsagliaXORV (jint x) { | |
278 if (x == 0) x = 1|os::random() ; | |
279 x ^= x << 6; | |
280 x ^= ((unsigned)x) >> 21; | |
281 x ^= x << 7 ; | |
282 return x & 0x7FFFFFFF ; | |
283 } | |
284 | |
285 static int Stall (int its) { | |
286 static volatile jint rv = 1 ; | |
287 volatile int OnFrame = 0 ; | |
288 jint v = rv ^ UNS(OnFrame) ; | |
289 while (--its >= 0) { | |
290 v = MarsagliaXORV (v) ; | |
291 } | |
292 // Make this impossible for the compiler to optimize away, | |
293 // but (mostly) avoid W coherency sharing on MP systems. | |
294 if (v == 0x12345) rv = v ; | |
295 return v ; | |
296 } | |
297 | |
298 int Monitor::TryLock () { | |
299 intptr_t v = _LockWord.FullWord ; | |
300 for (;;) { | |
301 if ((v & _LBIT) != 0) return 0 ; | |
302 const intptr_t u = CASPTR (&_LockWord, v, v|_LBIT) ; | |
303 if (v == u) return 1 ; | |
304 v = u ; | |
305 } | |
306 } | |
307 | |
308 int Monitor::TryFast () { | |
309 // Optimistic fast-path form ... | |
310 // Fast-path attempt for the common uncontended case. | |
311 // Avoid RTS->RTO $ coherence upgrade on typical SMP systems. | |
312 intptr_t v = CASPTR (&_LockWord, 0, _LBIT) ; // agro ... | |
313 if (v == 0) return 1 ; | |
314 | |
315 for (;;) { | |
316 if ((v & _LBIT) != 0) return 0 ; | |
317 const intptr_t u = CASPTR (&_LockWord, v, v|_LBIT) ; | |
318 if (v == u) return 1 ; | |
319 v = u ; | |
320 } | |
321 } | |
322 | |
323 int Monitor::ILocked () { | |
324 const intptr_t w = _LockWord.FullWord & 0xFF ; | |
325 assert (w == 0 || w == _LBIT, "invariant") ; | |
326 return w == _LBIT ; | |
327 } | |
328 | |
329 // Polite TATAS spinlock with exponential backoff - bounded spin. | |
330 // Ideally we'd use processor cycles, time or vtime to control | |
331 // the loop, but we currently use iterations. | |
332 // All the constants within were derived empirically but work over | |
333 // over the spectrum of J2SE reference platforms. | |
334 // On Niagara-class systems the back-off is unnecessary but | |
335 // is relatively harmless. (At worst it'll slightly retard | |
336 // acquisition times). The back-off is critical for older SMP systems | |
337 // where constant fetching of the LockWord would otherwise impair | |
338 // scalability. | |
339 // | |
340 // Clamp spinning at approximately 1/2 of a context-switch round-trip. | |
341 // See synchronizer.cpp for details and rationale. | |
342 | |
343 int Monitor::TrySpin (Thread * const Self) { | |
344 if (TryLock()) return 1 ; | |
345 if (!os::is_MP()) return 0 ; | |
346 | |
347 int Probes = 0 ; | |
348 int Delay = 0 ; | |
349 int Steps = 0 ; | |
350 int SpinMax = NativeMonitorSpinLimit ; | |
351 int flgs = NativeMonitorFlags ; | |
352 for (;;) { | |
353 intptr_t v = _LockWord.FullWord; | |
354 if ((v & _LBIT) == 0) { | |
355 if (CASPTR (&_LockWord, v, v|_LBIT) == v) { | |
356 return 1 ; | |
357 } | |
358 continue ; | |
359 } | |
360 | |
361 if ((flgs & 8) == 0) { | |
362 SpinPause () ; | |
363 } | |
364 | |
365 // Periodically increase Delay -- variable Delay form | |
366 // conceptually: delay *= 1 + 1/Exponent | |
367 ++ Probes; | |
368 if (Probes > SpinMax) return 0 ; | |
369 | |
370 if ((Probes & 0x7) == 0) { | |
371 Delay = ((Delay << 1)|1) & 0x7FF ; | |
372 // CONSIDER: Delay += 1 + (Delay/4); Delay &= 0x7FF ; | |
373 } | |
374 | |
375 if (flgs & 2) continue ; | |
376 | |
377 // Consider checking _owner's schedctl state, if OFFPROC abort spin. | |
378 // If the owner is OFFPROC then it's unlike that the lock will be dropped | |
379 // in a timely fashion, which suggests that spinning would not be fruitful | |
380 // or profitable. | |
381 | |
382 // Stall for "Delay" time units - iterations in the current implementation. | |
383 // Avoid generating coherency traffic while stalled. | |
384 // Possible ways to delay: | |
385 // PAUSE, SLEEP, MEMBAR #sync, MEMBAR #halt, | |
386 // wr %g0,%asi, gethrtime, rdstick, rdtick, rdtsc, etc. ... | |
387 // Note that on Niagara-class systems we want to minimize STs in the | |
388 // spin loop. N1 and brethren write-around the L1$ over the xbar into the L2$. | |
389 // Furthermore, they don't have a W$ like traditional SPARC processors. | |
390 // We currently use a Marsaglia Shift-Xor RNG loop. | |
391 Steps += Delay ; | |
392 if (Self != NULL) { | |
393 jint rv = Self->rng[0] ; | |
394 for (int k = Delay ; --k >= 0; ) { | |
395 rv = MarsagliaXORV (rv) ; | |
396 if ((flgs & 4) == 0 && SafepointSynchronize::do_call_back()) return 0 ; | |
397 } | |
398 Self->rng[0] = rv ; | |
399 } else { | |
400 Stall (Delay) ; | |
401 } | |
402 } | |
403 } | |
404 | |
405 static int ParkCommon (ParkEvent * ev, jlong timo) { | |
406 // Diagnostic support - periodically unwedge blocked threads | |
407 intx nmt = NativeMonitorTimeout ; | |
408 if (nmt > 0 && (nmt < timo || timo <= 0)) { | |
409 timo = nmt ; | |
410 } | |
411 int err = OS_OK ; | |
412 if (0 == timo) { | |
413 ev->park() ; | |
414 } else { | |
415 err = ev->park(timo) ; | |
416 } | |
417 return err ; | |
418 } | |
419 | |
420 inline int Monitor::AcquireOrPush (ParkEvent * ESelf) { | |
421 intptr_t v = _LockWord.FullWord ; | |
422 for (;;) { | |
423 if ((v & _LBIT) == 0) { | |
424 const intptr_t u = CASPTR (&_LockWord, v, v|_LBIT) ; | |
425 if (u == v) return 1 ; // indicate acquired | |
426 v = u ; | |
427 } else { | |
428 // Anticipate success ... | |
429 ESelf->ListNext = (ParkEvent *) (v & ~_LBIT) ; | |
430 const intptr_t u = CASPTR (&_LockWord, v, intptr_t(ESelf)|_LBIT) ; | |
431 if (u == v) return 0 ; // indicate pushed onto cxq | |
432 v = u ; | |
433 } | |
434 // Interference - LockWord change - just retry | |
435 } | |
436 } | |
437 | |
438 // ILock and IWait are the lowest level primitive internal blocking | |
439 // synchronization functions. The callers of IWait and ILock must have | |
440 // performed any needed state transitions beforehand. | |
441 // IWait and ILock may directly call park() without any concern for thread state. | |
442 // Note that ILock and IWait do *not* access _owner. | |
443 // _owner is a higher-level logical concept. | |
444 | |
445 void Monitor::ILock (Thread * Self) { | |
446 assert (_OnDeck != Self->_MutexEvent, "invariant") ; | |
447 | |
448 if (TryFast()) { | |
449 Exeunt: | |
450 assert (ILocked(), "invariant") ; | |
451 return ; | |
452 } | |
453 | |
454 ParkEvent * const ESelf = Self->_MutexEvent ; | |
455 assert (_OnDeck != ESelf, "invariant") ; | |
456 | |
457 // As an optimization, spinners could conditionally try to set ONDECK to _LBIT | |
458 // Synchronizer.cpp uses a similar optimization. | |
459 if (TrySpin (Self)) goto Exeunt ; | |
460 | |
461 // Slow-path - the lock is contended. | |
462 // Either Enqueue Self on cxq or acquire the outer lock. | |
463 // LockWord encoding = (cxq,LOCKBYTE) | |
464 ESelf->reset() ; | |
465 OrderAccess::fence() ; | |
466 | |
467 // Optional optimization ... try barging on the inner lock | |
468 if ((NativeMonitorFlags & 32) && CASPTR (&_OnDeck, NULL, UNS(Self)) == 0) { | |
469 goto OnDeck_LOOP ; | |
470 } | |
471 | |
472 if (AcquireOrPush (ESelf)) goto Exeunt ; | |
473 | |
474 // At any given time there is at most one ondeck thread. | |
475 // ondeck implies not resident on cxq and not resident on EntryList | |
476 // Only the OnDeck thread can try to acquire -- contended for -- the lock. | |
477 // CONSIDER: use Self->OnDeck instead of m->OnDeck. | |
478 // Deschedule Self so that others may run. | |
479 while (_OnDeck != ESelf) { | |
480 ParkCommon (ESelf, 0) ; | |
481 } | |
482 | |
483 // Self is now in the ONDECK position and will remain so until it | |
484 // manages to acquire the lock. | |
485 OnDeck_LOOP: | |
486 for (;;) { | |
487 assert (_OnDeck == ESelf, "invariant") ; | |
488 if (TrySpin (Self)) break ; | |
489 // CONSIDER: if ESelf->TryPark() && TryLock() break ... | |
490 // It's probably wise to spin only if we *actually* blocked | |
491 // CONSIDER: check the lockbyte, if it remains set then | |
492 // preemptively drain the cxq into the EntryList. | |
493 // The best place and time to perform queue operations -- lock metadata -- | |
494 // is _before having acquired the outer lock, while waiting for the lock to drop. | |
495 ParkCommon (ESelf, 0) ; | |
496 } | |
497 | |
498 assert (_OnDeck == ESelf, "invariant") ; | |
499 _OnDeck = NULL ; | |
500 | |
501 // Note that we current drop the inner lock (clear OnDeck) in the slow-path | |
502 // epilog immediately after having acquired the outer lock. | |
503 // But instead we could consider the following optimizations: | |
504 // A. Shift or defer dropping the inner lock until the subsequent IUnlock() operation. | |
505 // This might avoid potential reacquisition of the inner lock in IUlock(). | |
506 // B. While still holding the inner lock, attempt to opportunistically select | |
507 // and unlink the next ONDECK thread from the EntryList. | |
508 // If successful, set ONDECK to refer to that thread, otherwise clear ONDECK. | |
509 // It's critical that the select-and-unlink operation run in constant-time as | |
510 // it executes when holding the outer lock and may artificially increase the | |
511 // effective length of the critical section. | |
512 // Note that (A) and (B) are tantamount to succession by direct handoff for | |
513 // the inner lock. | |
514 goto Exeunt ; | |
515 } | |
516 | |
517 void Monitor::IUnlock (bool RelaxAssert) { | |
518 assert (ILocked(), "invariant") ; | |
4740
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
519 // Conceptually we need a MEMBAR #storestore|#loadstore barrier or fence immediately |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
520 // before the store that releases the lock. Crucially, all the stores and loads in the |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
521 // critical section must be globally visible before the store of 0 into the lock-word |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
522 // that releases the lock becomes globally visible. That is, memory accesses in the |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
523 // critical section should not be allowed to bypass or overtake the following ST that |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
524 // releases the lock. As such, to prevent accesses within the critical section |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
525 // from "leaking" out, we need a release fence between the critical section and the |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
526 // store that releases the lock. In practice that release barrier is elided on |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
527 // platforms with strong memory models such as TSO. |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
528 // |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
529 // Note that the OrderAccess::storeload() fence that appears after unlock store |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
530 // provides for progress conditions and succession and is _not related to exclusion |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
531 // safety or lock release consistency. |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
532 OrderAccess::release_store(&_LockWord.Bytes[_LSBINDEX], 0); // drop outer lock |
eccc4b1f8945
7050298: ARM: SIGSEGV in JNIHandleBlock::allocate_handle
vladidan
parents:
3960
diff
changeset
|
533 |
0 | 534 OrderAccess::storeload (); |
535 ParkEvent * const w = _OnDeck ; | |
536 assert (RelaxAssert || w != Thread::current()->_MutexEvent, "invariant") ; | |
537 if (w != NULL) { | |
538 // Either we have a valid ondeck thread or ondeck is transiently "locked" | |
539 // by some exiting thread as it arranges for succession. The LSBit of | |
540 // OnDeck allows us to discriminate two cases. If the latter, the | |
541 // responsibility for progress and succession lies with that other thread. | |
542 // For good performance, we also depend on the fact that redundant unpark() | |
543 // operations are cheap. That is, repeated Unpark()ing of the ONDECK thread | |
544 // is inexpensive. This approach provides implicit futile wakeup throttling. | |
545 // Note that the referent "w" might be stale with respect to the lock. | |
546 // In that case the following unpark() is harmless and the worst that'll happen | |
547 // is a spurious return from a park() operation. Critically, if "w" _is stale, | |
548 // then progress is known to have occurred as that means the thread associated | |
549 // with "w" acquired the lock. In that case this thread need take no further | |
550 // action to guarantee progress. | |
551 if ((UNS(w) & _LBIT) == 0) w->unpark() ; | |
552 return ; | |
553 } | |
554 | |
555 intptr_t cxq = _LockWord.FullWord ; | |
556 if (((cxq & ~_LBIT)|UNS(_EntryList)) == 0) { | |
557 return ; // normal fast-path exit - cxq and EntryList both empty | |
558 } | |
559 if (cxq & _LBIT) { | |
560 // Optional optimization ... | |
561 // Some other thread acquired the lock in the window since this | |
562 // thread released it. Succession is now that thread's responsibility. | |
563 return ; | |
564 } | |
565 | |
566 Succession: | |
567 // Slow-path exit - this thread must ensure succession and progress. | |
568 // OnDeck serves as lock to protect cxq and EntryList. | |
569 // Only the holder of OnDeck can manipulate EntryList or detach the RATs from cxq. | |
570 // Avoid ABA - allow multiple concurrent producers (enqueue via push-CAS) | |
571 // but only one concurrent consumer (detacher of RATs). | |
572 // Consider protecting this critical section with schedctl on Solaris. | |
573 // Unlike a normal lock, however, the exiting thread "locks" OnDeck, | |
574 // picks a successor and marks that thread as OnDeck. That successor | |
575 // thread will then clear OnDeck once it eventually acquires the outer lock. | |
576 if (CASPTR (&_OnDeck, NULL, _LBIT) != UNS(NULL)) { | |
577 return ; | |
578 } | |
579 | |
580 ParkEvent * List = _EntryList ; | |
581 if (List != NULL) { | |
582 // Transfer the head of the EntryList to the OnDeck position. | |
583 // Once OnDeck, a thread stays OnDeck until it acquires the lock. | |
584 // For a given lock there is at most OnDeck thread at any one instant. | |
585 WakeOne: | |
586 assert (List == _EntryList, "invariant") ; | |
587 ParkEvent * const w = List ; | |
588 assert (RelaxAssert || w != Thread::current()->_MutexEvent, "invariant") ; | |
589 _EntryList = w->ListNext ; | |
590 // as a diagnostic measure consider setting w->_ListNext = BAD | |
591 assert (UNS(_OnDeck) == _LBIT, "invariant") ; | |
592 _OnDeck = w ; // pass OnDeck to w. | |
593 // w will clear OnDeck once it acquires the outer lock | |
594 | |
595 // Another optional optimization ... | |
596 // For heavily contended locks it's not uncommon that some other | |
597 // thread acquired the lock while this thread was arranging succession. | |
598 // Try to defer the unpark() operation - Delegate the responsibility | |
599 // for unpark()ing the OnDeck thread to the current or subsequent owners | |
600 // That is, the new owner is responsible for unparking the OnDeck thread. | |
601 OrderAccess::storeload() ; | |
602 cxq = _LockWord.FullWord ; | |
603 if (cxq & _LBIT) return ; | |
604 | |
605 w->unpark() ; | |
606 return ; | |
607 } | |
608 | |
609 cxq = _LockWord.FullWord ; | |
610 if ((cxq & ~_LBIT) != 0) { | |
611 // The EntryList is empty but the cxq is populated. | |
612 // drain RATs from cxq into EntryList | |
613 // Detach RATs segment with CAS and then merge into EntryList | |
614 for (;;) { | |
615 // optional optimization - if locked, the owner is responsible for succession | |
616 if (cxq & _LBIT) goto Punt ; | |
617 const intptr_t vfy = CASPTR (&_LockWord, cxq, cxq & _LBIT) ; | |
618 if (vfy == cxq) break ; | |
619 cxq = vfy ; | |
620 // Interference - LockWord changed - Just retry | |
621 // We can see concurrent interference from contending threads | |
622 // pushing themselves onto the cxq or from lock-unlock operations. | |
623 // From the perspective of this thread, EntryList is stable and | |
624 // the cxq is prepend-only -- the head is volatile but the interior | |
625 // of the cxq is stable. In theory if we encounter interference from threads | |
626 // pushing onto cxq we could simply break off the original cxq suffix and | |
627 // move that segment to the EntryList, avoiding a 2nd or multiple CAS attempts | |
628 // on the high-traffic LockWord variable. For instance lets say the cxq is "ABCD" | |
629 // when we first fetch cxq above. Between the fetch -- where we observed "A" | |
630 // -- and CAS -- where we attempt to CAS null over A -- "PQR" arrive, | |
631 // yielding cxq = "PQRABCD". In this case we could simply set A.ListNext | |
632 // null, leaving cxq = "PQRA" and transfer the "BCD" segment to the EntryList. | |
633 // Note too, that it's safe for this thread to traverse the cxq | |
634 // without taking any special concurrency precautions. | |
635 } | |
636 | |
637 // We don't currently reorder the cxq segment as we move it onto | |
638 // the EntryList, but it might make sense to reverse the order | |
639 // or perhaps sort by thread priority. See the comments in | |
640 // synchronizer.cpp objectMonitor::exit(). | |
641 assert (_EntryList == NULL, "invariant") ; | |
642 _EntryList = List = (ParkEvent *)(cxq & ~_LBIT) ; | |
643 assert (List != NULL, "invariant") ; | |
644 goto WakeOne ; | |
645 } | |
646 | |
647 // cxq|EntryList is empty. | |
648 // w == NULL implies that cxq|EntryList == NULL in the past. | |
649 // Possible race - rare inopportune interleaving. | |
650 // A thread could have added itself to cxq since this thread previously checked. | |
651 // Detect and recover by refetching cxq. | |
652 Punt: | |
653 assert (UNS(_OnDeck) == _LBIT, "invariant") ; | |
654 _OnDeck = NULL ; // Release inner lock. | |
655 OrderAccess::storeload(); // Dekker duality - pivot point | |
656 | |
657 // Resample LockWord/cxq to recover from possible race. | |
658 // For instance, while this thread T1 held OnDeck, some other thread T2 might | |
659 // acquire the outer lock. Another thread T3 might try to acquire the outer | |
660 // lock, but encounter contention and enqueue itself on cxq. T2 then drops the | |
661 // outer lock, but skips succession as this thread T1 still holds OnDeck. | |
662 // T1 is and remains responsible for ensuring succession of T3. | |
663 // | |
664 // Note that we don't need to recheck EntryList, just cxq. | |
665 // If threads moved onto EntryList since we dropped OnDeck | |
666 // that implies some other thread forced succession. | |
667 cxq = _LockWord.FullWord ; | |
668 if ((cxq & ~_LBIT) != 0 && (cxq & _LBIT) == 0) { | |
669 goto Succession ; // potential race -- re-run succession | |
670 } | |
671 return ; | |
672 } | |
673 | |
674 bool Monitor::notify() { | |
675 assert (_owner == Thread::current(), "invariant") ; | |
676 assert (ILocked(), "invariant") ; | |
677 if (_WaitSet == NULL) return true ; | |
678 NotifyCount ++ ; | |
679 | |
680 // Transfer one thread from the WaitSet to the EntryList or cxq. | |
681 // Currently we just unlink the head of the WaitSet and prepend to the cxq. | |
682 // And of course we could just unlink it and unpark it, too, but | |
683 // in that case it'd likely impale itself on the reentry. | |
684 Thread::muxAcquire (_WaitLock, "notify:WaitLock") ; | |
685 ParkEvent * nfy = _WaitSet ; | |
686 if (nfy != NULL) { // DCL idiom | |
687 _WaitSet = nfy->ListNext ; | |
688 assert (nfy->Notified == 0, "invariant") ; | |
689 // push nfy onto the cxq | |
690 for (;;) { | |
691 const intptr_t v = _LockWord.FullWord ; | |
692 assert ((v & 0xFF) == _LBIT, "invariant") ; | |
693 nfy->ListNext = (ParkEvent *)(v & ~_LBIT); | |
694 if (CASPTR (&_LockWord, v, UNS(nfy)|_LBIT) == v) break; | |
695 // interference - _LockWord changed -- just retry | |
696 } | |
697 // Note that setting Notified before pushing nfy onto the cxq is | |
698 // also legal and safe, but the safety properties are much more | |
699 // subtle, so for the sake of code stewardship ... | |
700 OrderAccess::fence() ; | |
701 nfy->Notified = 1; | |
702 } | |
703 Thread::muxRelease (_WaitLock) ; | |
704 if (nfy != NULL && (NativeMonitorFlags & 16)) { | |
705 // Experimental code ... light up the wakee in the hope that this thread (the owner) | |
706 // will drop the lock just about the time the wakee comes ONPROC. | |
707 nfy->unpark() ; | |
708 } | |
709 assert (ILocked(), "invariant") ; | |
710 return true ; | |
711 } | |
712 | |
713 // Currently notifyAll() transfers the waiters one-at-a-time from the waitset | |
714 // to the cxq. This could be done more efficiently with a single bulk en-mass transfer, | |
715 // but in practice notifyAll() for large #s of threads is rare and not time-critical. | |
716 // Beware too, that we invert the order of the waiters. Lets say that the | |
717 // waitset is "ABCD" and the cxq is "XYZ". After a notifyAll() the waitset | |
718 // will be empty and the cxq will be "DCBAXYZ". This is benign, of course. | |
719 | |
720 bool Monitor::notify_all() { | |
721 assert (_owner == Thread::current(), "invariant") ; | |
722 assert (ILocked(), "invariant") ; | |
723 while (_WaitSet != NULL) notify() ; | |
724 return true ; | |
725 } | |
726 | |
727 int Monitor::IWait (Thread * Self, jlong timo) { | |
728 assert (ILocked(), "invariant") ; | |
729 | |
730 // Phases: | |
731 // 1. Enqueue Self on WaitSet - currently prepend | |
732 // 2. unlock - drop the outer lock | |
733 // 3. wait for either notification or timeout | |
734 // 4. lock - reentry - reacquire the outer lock | |
735 | |
736 ParkEvent * const ESelf = Self->_MutexEvent ; | |
737 ESelf->Notified = 0 ; | |
738 ESelf->reset() ; | |
739 OrderAccess::fence() ; | |
740 | |
741 // Add Self to WaitSet | |
742 // Ideally only the holder of the outer lock would manipulate the WaitSet - | |
743 // That is, the outer lock would implicitly protect the WaitSet. | |
744 // But if a thread in wait() encounters a timeout it will need to dequeue itself | |
745 // from the WaitSet _before it becomes the owner of the lock. We need to dequeue | |
746 // as the ParkEvent -- which serves as a proxy for the thread -- can't reside | |
747 // on both the WaitSet and the EntryList|cxq at the same time.. That is, a thread | |
748 // on the WaitSet can't be allowed to compete for the lock until it has managed to | |
749 // unlink its ParkEvent from WaitSet. Thus the need for WaitLock. | |
750 // Contention on the WaitLock is minimal. | |
751 // | |
752 // Another viable approach would be add another ParkEvent, "WaitEvent" to the | |
753 // thread class. The WaitSet would be composed of WaitEvents. Only the | |
754 // owner of the outer lock would manipulate the WaitSet. A thread in wait() | |
755 // could then compete for the outer lock, and then, if necessary, unlink itself | |
756 // from the WaitSet only after having acquired the outer lock. More precisely, | |
757 // there would be no WaitLock. A thread in in wait() would enqueue its WaitEvent | |
758 // on the WaitSet; release the outer lock; wait for either notification or timeout; | |
759 // reacquire the inner lock; and then, if needed, unlink itself from the WaitSet. | |
760 // | |
761 // Alternatively, a 2nd set of list link fields in the ParkEvent might suffice. | |
762 // One set would be for the WaitSet and one for the EntryList. | |
763 // We could also deconstruct the ParkEvent into a "pure" event and add a | |
764 // new immortal/TSM "ListElement" class that referred to ParkEvents. | |
765 // In that case we could have one ListElement on the WaitSet and another | |
766 // on the EntryList, with both referring to the same pure Event. | |
767 | |
768 Thread::muxAcquire (_WaitLock, "wait:WaitLock:Add") ; | |
769 ESelf->ListNext = _WaitSet ; | |
770 _WaitSet = ESelf ; | |
771 Thread::muxRelease (_WaitLock) ; | |
772 | |
773 // Release the outer lock | |
774 // We call IUnlock (RelaxAssert=true) as a thread T1 might | |
775 // enqueue itself on the WaitSet, call IUnlock(), drop the lock, | |
776 // and then stall before it can attempt to wake a successor. | |
777 // Some other thread T2 acquires the lock, and calls notify(), moving | |
778 // T1 from the WaitSet to the cxq. T2 then drops the lock. T1 resumes, | |
779 // and then finds *itself* on the cxq. During the course of a normal | |
780 // IUnlock() call a thread should _never find itself on the EntryList | |
781 // or cxq, but in the case of wait() it's possible. | |
782 // See synchronizer.cpp objectMonitor::wait(). | |
783 IUnlock (true) ; | |
784 | |
785 // Wait for either notification or timeout | |
786 // Beware that in some circumstances we might propagate | |
787 // spurious wakeups back to the caller. | |
788 | |
789 for (;;) { | |
790 if (ESelf->Notified) break ; | |
791 int err = ParkCommon (ESelf, timo) ; | |
792 if (err == OS_TIMEOUT || (NativeMonitorFlags & 1)) break ; | |
793 } | |
794 | |
795 // Prepare for reentry - if necessary, remove ESelf from WaitSet | |
796 // ESelf can be: | |
797 // 1. Still on the WaitSet. This can happen if we exited the loop by timeout. | |
798 // 2. On the cxq or EntryList | |
799 // 3. Not resident on cxq, EntryList or WaitSet, but in the OnDeck position. | |
800 | |
801 OrderAccess::fence() ; | |
802 int WasOnWaitSet = 0 ; | |
803 if (ESelf->Notified == 0) { | |
804 Thread::muxAcquire (_WaitLock, "wait:WaitLock:remove") ; | |
805 if (ESelf->Notified == 0) { // DCL idiom | |
806 assert (_OnDeck != ESelf, "invariant") ; // can't be both OnDeck and on WaitSet | |
807 // ESelf is resident on the WaitSet -- unlink it. | |
808 // A doubly-linked list would be better here so we can unlink in constant-time. | |
809 // We have to unlink before we potentially recontend as ESelf might otherwise | |
810 // end up on the cxq|EntryList -- it can't be on two lists at once. | |
811 ParkEvent * p = _WaitSet ; | |
812 ParkEvent * q = NULL ; // classic q chases p | |
813 while (p != NULL && p != ESelf) { | |
814 q = p ; | |
815 p = p->ListNext ; | |
816 } | |
817 assert (p == ESelf, "invariant") ; | |
818 if (p == _WaitSet) { // found at head | |
819 assert (q == NULL, "invariant") ; | |
820 _WaitSet = p->ListNext ; | |
821 } else { // found in interior | |
822 assert (q->ListNext == p, "invariant") ; | |
823 q->ListNext = p->ListNext ; | |
824 } | |
825 WasOnWaitSet = 1 ; // We were *not* notified but instead encountered timeout | |
826 } | |
827 Thread::muxRelease (_WaitLock) ; | |
828 } | |
829 | |
830 // Reentry phase - reacquire the lock | |
831 if (WasOnWaitSet) { | |
832 // ESelf was previously on the WaitSet but we just unlinked it above | |
833 // because of a timeout. ESelf is not resident on any list and is not OnDeck | |
834 assert (_OnDeck != ESelf, "invariant") ; | |
835 ILock (Self) ; | |
836 } else { | |
837 // A prior notify() operation moved ESelf from the WaitSet to the cxq. | |
838 // ESelf is now on the cxq, EntryList or at the OnDeck position. | |
839 // The following fragment is extracted from Monitor::ILock() | |
840 for (;;) { | |
841 if (_OnDeck == ESelf && TrySpin(Self)) break ; | |
842 ParkCommon (ESelf, 0) ; | |
843 } | |
844 assert (_OnDeck == ESelf, "invariant") ; | |
845 _OnDeck = NULL ; | |
846 } | |
847 | |
848 assert (ILocked(), "invariant") ; | |
849 return WasOnWaitSet != 0 ; // return true IFF timeout | |
850 } | |
851 | |
852 | |
853 // ON THE VMTHREAD SNEAKING PAST HELD LOCKS: | |
854 // In particular, there are certain types of global lock that may be held | |
855 // by a Java thread while it is blocked at a safepoint but before it has | |
856 // written the _owner field. These locks may be sneakily acquired by the | |
857 // VM thread during a safepoint to avoid deadlocks. Alternatively, one should | |
858 // identify all such locks, and ensure that Java threads never block at | |
859 // safepoints while holding them (_no_safepoint_check_flag). While it | |
860 // seems as though this could increase the time to reach a safepoint | |
861 // (or at least increase the mean, if not the variance), the latter | |
862 // approach might make for a cleaner, more maintainable JVM design. | |
863 // | |
864 // Sneaking is vile and reprehensible and should be excised at the 1st | |
865 // opportunity. It's possible that the need for sneaking could be obviated | |
866 // as follows. Currently, a thread might (a) while TBIVM, call pthread_mutex_lock | |
867 // or ILock() thus acquiring the "physical" lock underlying Monitor/Mutex. | |
868 // (b) stall at the TBIVM exit point as a safepoint is in effect. Critically, | |
869 // it'll stall at the TBIVM reentry state transition after having acquired the | |
870 // underlying lock, but before having set _owner and having entered the actual | |
871 // critical section. The lock-sneaking facility leverages that fact and allowed the | |
872 // VM thread to logically acquire locks that had already be physically locked by mutators | |
873 // but where mutators were known blocked by the reentry thread state transition. | |
874 // | |
875 // If we were to modify the Monitor-Mutex so that TBIVM state transitions tightly | |
876 // wrapped calls to park(), then we could likely do away with sneaking. We'd | |
877 // decouple lock acquisition and parking. The critical invariant to eliminating | |
878 // sneaking is to ensure that we never "physically" acquire the lock while TBIVM. | |
879 // An easy way to accomplish this is to wrap the park calls in a narrow TBIVM jacket. | |
880 // One difficulty with this approach is that the TBIVM wrapper could recurse and | |
881 // call lock() deep from within a lock() call, while the MutexEvent was already enqueued. | |
882 // Using a stack (N=2 at minimum) of ParkEvents would take care of that problem. | |
883 // | |
884 // But of course the proper ultimate approach is to avoid schemes that require explicit | |
885 // sneaking or dependence on any any clever invariants or subtle implementation properties | |
886 // of Mutex-Monitor and instead directly address the underlying design flaw. | |
887 | |
888 void Monitor::lock (Thread * Self) { | |
889 #ifdef CHECK_UNHANDLED_OOPS | |
890 // Clear unhandled oops so we get a crash right away. Only clear for non-vm | |
891 // or GC threads. | |
892 if (Self->is_Java_thread()) { | |
893 Self->clear_unhandled_oops(); | |
894 } | |
895 #endif // CHECK_UNHANDLED_OOPS | |
896 | |
897 debug_only(check_prelock_state(Self)); | |
898 assert (_owner != Self , "invariant") ; | |
899 assert (_OnDeck != Self->_MutexEvent, "invariant") ; | |
900 | |
901 if (TryFast()) { | |
902 Exeunt: | |
903 assert (ILocked(), "invariant") ; | |
904 assert (owner() == NULL, "invariant"); | |
905 set_owner (Self); | |
906 return ; | |
907 } | |
908 | |
909 // The lock is contended ... | |
910 | |
911 bool can_sneak = Self->is_VM_thread() && SafepointSynchronize::is_at_safepoint(); | |
912 if (can_sneak && _owner == NULL) { | |
913 // a java thread has locked the lock but has not entered the | |
914 // critical region -- let's just pretend we've locked the lock | |
915 // and go on. we note this with _snuck so we can also | |
916 // pretend to unlock when the time comes. | |
917 _snuck = true; | |
918 goto Exeunt ; | |
919 } | |
920 | |
921 // Try a brief spin to avoid passing thru thread state transition ... | |
922 if (TrySpin (Self)) goto Exeunt ; | |
923 | |
924 check_block_state(Self); | |
925 if (Self->is_Java_thread()) { | |
926 // Horribile dictu - we suffer through a state transition | |
927 assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex"); | |
928 ThreadBlockInVM tbivm ((JavaThread *) Self) ; | |
929 ILock (Self) ; | |
930 } else { | |
931 // Mirabile dictu | |
932 ILock (Self) ; | |
933 } | |
934 goto Exeunt ; | |
935 } | |
936 | |
937 void Monitor::lock() { | |
938 this->lock(Thread::current()); | |
939 } | |
940 | |
941 // Lock without safepoint check - a degenerate variant of lock(). | |
942 // Should ONLY be used by safepoint code and other code | |
943 // that is guaranteed not to block while running inside the VM. If this is called with | |
944 // thread state set to be in VM, the safepoint synchronization code will deadlock! | |
945 | |
946 void Monitor::lock_without_safepoint_check (Thread * Self) { | |
947 assert (_owner != Self, "invariant") ; | |
948 ILock (Self) ; | |
949 assert (_owner == NULL, "invariant"); | |
950 set_owner (Self); | |
951 } | |
952 | |
953 void Monitor::lock_without_safepoint_check () { | |
954 lock_without_safepoint_check (Thread::current()) ; | |
955 } | |
956 | |
957 | |
958 // Returns true if thread succeceed [sic] in grabbing the lock, otherwise false. | |
959 | |
960 bool Monitor::try_lock() { | |
961 Thread * const Self = Thread::current(); | |
962 debug_only(check_prelock_state(Self)); | |
963 // assert(!thread->is_inside_signal_handler(), "don't lock inside signal handler"); | |
964 | |
965 // Special case, where all Java threads are stopped. | |
966 // The lock may have been acquired but _owner is not yet set. | |
967 // In that case the VM thread can safely grab the lock. | |
968 // It strikes me this should appear _after the TryLock() fails, below. | |
969 bool can_sneak = Self->is_VM_thread() && SafepointSynchronize::is_at_safepoint(); | |
970 if (can_sneak && _owner == NULL) { | |
971 set_owner(Self); // Do not need to be atomic, since we are at a safepoint | |
972 _snuck = true; | |
973 return true; | |
974 } | |
975 | |
976 if (TryLock()) { | |
977 // We got the lock | |
978 assert (_owner == NULL, "invariant"); | |
979 set_owner (Self); | |
980 return true; | |
981 } | |
982 return false; | |
983 } | |
984 | |
985 void Monitor::unlock() { | |
986 assert (_owner == Thread::current(), "invariant") ; | |
987 assert (_OnDeck != Thread::current()->_MutexEvent , "invariant") ; | |
988 set_owner (NULL) ; | |
989 if (_snuck) { | |
990 assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "sneak"); | |
991 _snuck = false; | |
992 return ; | |
993 } | |
994 IUnlock (false) ; | |
995 } | |
996 | |
997 // Yet another degenerate version of Monitor::lock() or lock_without_safepoint_check() | |
998 // jvm_raw_lock() and _unlock() can be called by non-Java threads via JVM_RawMonitorEnter. | |
999 // | |
1000 // There's no expectation that JVM_RawMonitors will interoperate properly with the native | |
1001 // Mutex-Monitor constructs. We happen to implement JVM_RawMonitors in terms of | |
1002 // native Mutex-Monitors simply as a matter of convenience. A simple abstraction layer | |
1003 // over a pthread_mutex_t would work equally as well, but require more platform-specific | |
1004 // code -- a "PlatformMutex". Alternatively, a simply layer over muxAcquire-muxRelease | |
1005 // would work too. | |
1006 // | |
1007 // Since the caller might be a foreign thread, we don't necessarily have a Thread.MutexEvent | |
1008 // instance available. Instead, we transiently allocate a ParkEvent on-demand if | |
1009 // we encounter contention. That ParkEvent remains associated with the thread | |
1010 // until it manages to acquire the lock, at which time we return the ParkEvent | |
1011 // to the global ParkEvent free list. This is correct and suffices for our purposes. | |
1012 // | |
1013 // Beware that the original jvm_raw_unlock() had a "_snuck" test but that | |
1014 // jvm_raw_lock() didn't have the corresponding test. I suspect that's an | |
1015 // oversight, but I've replicated the original suspect logic in the new code ... | |
1016 | |
1017 void Monitor::jvm_raw_lock() { | |
1018 assert(rank() == native, "invariant"); | |
1019 | |
1020 if (TryLock()) { | |
1021 Exeunt: | |
1022 assert (ILocked(), "invariant") ; | |
1023 assert (_owner == NULL, "invariant"); | |
1024 // This can potentially be called by non-java Threads. Thus, the ThreadLocalStorage | |
1025 // might return NULL. Don't call set_owner since it will break on an NULL owner | |
1026 // Consider installing a non-null "ANON" distinguished value instead of just NULL. | |
1027 _owner = ThreadLocalStorage::thread(); | |
1028 return ; | |
1029 } | |
1030 | |
1031 if (TrySpin(NULL)) goto Exeunt ; | |
1032 | |
1033 // slow-path - apparent contention | |
1034 // Allocate a ParkEvent for transient use. | |
1035 // The ParkEvent remains associated with this thread until | |
1036 // the time the thread manages to acquire the lock. | |
1037 ParkEvent * const ESelf = ParkEvent::Allocate(NULL) ; | |
1038 ESelf->reset() ; | |
1039 OrderAccess::storeload() ; | |
1040 | |
1041 // Either Enqueue Self on cxq or acquire the outer lock. | |
1042 if (AcquireOrPush (ESelf)) { | |
1043 ParkEvent::Release (ESelf) ; // surrender the ParkEvent | |
1044 goto Exeunt ; | |
1045 } | |
1046 | |
1047 // At any given time there is at most one ondeck thread. | |
1048 // ondeck implies not resident on cxq and not resident on EntryList | |
1049 // Only the OnDeck thread can try to acquire -- contended for -- the lock. | |
1050 // CONSIDER: use Self->OnDeck instead of m->OnDeck. | |
1051 for (;;) { | |
1052 if (_OnDeck == ESelf && TrySpin(NULL)) break ; | |
1053 ParkCommon (ESelf, 0) ; | |
1054 } | |
1055 | |
1056 assert (_OnDeck == ESelf, "invariant") ; | |
1057 _OnDeck = NULL ; | |
1058 ParkEvent::Release (ESelf) ; // surrender the ParkEvent | |
1059 goto Exeunt ; | |
1060 } | |
1061 | |
1062 void Monitor::jvm_raw_unlock() { | |
1063 // Nearly the same as Monitor::unlock() ... | |
1064 // directly set _owner instead of using set_owner(null) | |
1065 _owner = NULL ; | |
1066 if (_snuck) { // ??? | |
1067 assert(SafepointSynchronize::is_at_safepoint() && Thread::current()->is_VM_thread(), "sneak"); | |
1068 _snuck = false; | |
1069 return ; | |
1070 } | |
1071 IUnlock(false) ; | |
1072 } | |
1073 | |
1074 bool Monitor::wait(bool no_safepoint_check, long timeout, bool as_suspend_equivalent) { | |
1075 Thread * const Self = Thread::current() ; | |
1076 assert (_owner == Self, "invariant") ; | |
1077 assert (ILocked(), "invariant") ; | |
1078 | |
1079 // as_suspend_equivalent logically implies !no_safepoint_check | |
1080 guarantee (!as_suspend_equivalent || !no_safepoint_check, "invariant") ; | |
1081 // !no_safepoint_check logically implies java_thread | |
1082 guarantee (no_safepoint_check || Self->is_Java_thread(), "invariant") ; | |
1083 | |
1084 #ifdef ASSERT | |
1085 Monitor * least = get_least_ranked_lock_besides_this(Self->owned_locks()); | |
1086 assert(least != this, "Specification of get_least_... call above"); | |
1087 if (least != NULL && least->rank() <= special) { | |
1088 tty->print("Attempting to wait on monitor %s/%d while holding" | |
1089 " lock %s/%d -- possible deadlock", | |
1090 name(), rank(), least->name(), least->rank()); | |
1091 assert(false, "Shouldn't block(wait) while holding a lock of rank special"); | |
1092 } | |
1093 #endif // ASSERT | |
1094 | |
1095 int wait_status ; | |
1096 // conceptually set the owner to NULL in anticipation of | |
1097 // abdicating the lock in wait | |
1098 set_owner(NULL); | |
1099 if (no_safepoint_check) { | |
1100 wait_status = IWait (Self, timeout) ; | |
1101 } else { | |
1102 assert (Self->is_Java_thread(), "invariant") ; | |
1103 JavaThread *jt = (JavaThread *)Self; | |
1104 | |
1105 // Enter safepoint region - ornate and Rococo ... | |
1106 ThreadBlockInVM tbivm(jt); | |
1107 OSThreadWaitState osts(Self->osthread(), false /* not Object.wait() */); | |
1108 | |
1109 if (as_suspend_equivalent) { | |
1110 jt->set_suspend_equivalent(); | |
1111 // cleared by handle_special_suspend_equivalent_condition() or | |
1112 // java_suspend_self() | |
1113 } | |
1114 | |
1115 wait_status = IWait (Self, timeout) ; | |
1116 | |
1117 // were we externally suspended while we were waiting? | |
1118 if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) { | |
1119 // Our event wait has finished and we own the lock, but | |
1120 // while we were waiting another thread suspended us. We don't | |
1121 // want to hold the lock while suspended because that | |
1122 // would surprise the thread that suspended us. | |
1123 assert (ILocked(), "invariant") ; | |
1124 IUnlock (true) ; | |
1125 jt->java_suspend_self(); | |
1126 ILock (Self) ; | |
1127 assert (ILocked(), "invariant") ; | |
1128 } | |
1129 } | |
1130 | |
1131 // Conceptually reestablish ownership of the lock. | |
1132 // The "real" lock -- the LockByte -- was reacquired by IWait(). | |
1133 assert (ILocked(), "invariant") ; | |
1134 assert (_owner == NULL, "invariant") ; | |
1135 set_owner (Self) ; | |
1136 return wait_status != 0 ; // return true IFF timeout | |
1137 } | |
1138 | |
1139 Monitor::~Monitor() { | |
1140 assert ((UNS(_owner)|UNS(_LockWord.FullWord)|UNS(_EntryList)|UNS(_WaitSet)|UNS(_OnDeck)) == 0, "") ; | |
1141 } | |
1142 | |
55
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1143 void Monitor::ClearMonitor (Monitor * m, const char *name) { |
0 | 1144 m->_owner = NULL ; |
1145 m->_snuck = false ; | |
55
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1146 if (name == NULL) { |
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1147 strcpy(m->_name, "UNKNOWN") ; |
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1148 } else { |
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1149 strncpy(m->_name, name, MONITOR_NAME_LEN - 1); |
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1150 m->_name[MONITOR_NAME_LEN - 1] = '\0'; |
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1151 } |
0 | 1152 m->_LockWord.FullWord = 0 ; |
1153 m->_EntryList = NULL ; | |
1154 m->_OnDeck = NULL ; | |
1155 m->_WaitSet = NULL ; | |
1156 m->_WaitLock[0] = 0 ; | |
1157 } | |
1158 | |
1159 Monitor::Monitor() { ClearMonitor(this); } | |
1160 | |
1161 Monitor::Monitor (int Rank, const char * name, bool allow_vm_block) { | |
55
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1162 ClearMonitor (this, name) ; |
0 | 1163 #ifdef ASSERT |
1164 _allow_vm_block = allow_vm_block; | |
1165 _rank = Rank ; | |
1166 #endif | |
1167 } | |
1168 | |
1169 Mutex::~Mutex() { | |
1170 assert ((UNS(_owner)|UNS(_LockWord.FullWord)|UNS(_EntryList)|UNS(_WaitSet)|UNS(_OnDeck)) == 0, "") ; | |
1171 } | |
1172 | |
1173 Mutex::Mutex (int Rank, const char * name, bool allow_vm_block) { | |
55
2a8eb116ebbe
6610420: Debug VM crashes during monitor lock rank checking
xlu
parents:
0
diff
changeset
|
1174 ClearMonitor ((Monitor *) this, name) ; |
0 | 1175 #ifdef ASSERT |
1176 _allow_vm_block = allow_vm_block; | |
1177 _rank = Rank ; | |
1178 #endif | |
1179 } | |
1180 | |
1181 bool Monitor::owned_by_self() const { | |
1182 bool ret = _owner == Thread::current(); | |
1183 assert (!ret || _LockWord.Bytes[_LSBINDEX] != 0, "invariant") ; | |
1184 return ret; | |
1185 } | |
1186 | |
1187 void Monitor::print_on_error(outputStream* st) const { | |
1188 st->print("[" PTR_FORMAT, this); | |
1189 st->print("] %s", _name); | |
1190 st->print(" - owner thread: " PTR_FORMAT, _owner); | |
1191 } | |
1192 | |
1193 | |
1194 | |
1195 | |
1196 // ---------------------------------------------------------------------------------- | |
1197 // Non-product code | |
1198 | |
1199 #ifndef PRODUCT | |
1200 void Monitor::print_on(outputStream* st) const { | |
1201 st->print_cr("Mutex: [0x%lx/0x%lx] %s - owner: 0x%lx", this, _LockWord.FullWord, _name, _owner); | |
1202 } | |
1203 #endif | |
1204 | |
1205 #ifndef PRODUCT | |
1206 #ifdef ASSERT | |
1207 Monitor * Monitor::get_least_ranked_lock(Monitor * locks) { | |
1208 Monitor *res, *tmp; | |
1209 for (res = tmp = locks; tmp != NULL; tmp = tmp->next()) { | |
1210 if (tmp->rank() < res->rank()) { | |
1211 res = tmp; | |
1212 } | |
1213 } | |
1214 if (!SafepointSynchronize::is_at_safepoint()) { | |
1215 // In this case, we expect the held locks to be | |
1216 // in increasing rank order (modulo any native ranks) | |
1217 for (tmp = locks; tmp != NULL; tmp = tmp->next()) { | |
1218 if (tmp->next() != NULL) { | |
1219 assert(tmp->rank() == Mutex::native || | |
1220 tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?"); | |
1221 } | |
1222 } | |
1223 } | |
1224 return res; | |
1225 } | |
1226 | |
1227 Monitor* Monitor::get_least_ranked_lock_besides_this(Monitor* locks) { | |
1228 Monitor *res, *tmp; | |
1229 for (res = NULL, tmp = locks; tmp != NULL; tmp = tmp->next()) { | |
1230 if (tmp != this && (res == NULL || tmp->rank() < res->rank())) { | |
1231 res = tmp; | |
1232 } | |
1233 } | |
1234 if (!SafepointSynchronize::is_at_safepoint()) { | |
1235 // In this case, we expect the held locks to be | |
1236 // in increasing rank order (modulo any native ranks) | |
1237 for (tmp = locks; tmp != NULL; tmp = tmp->next()) { | |
1238 if (tmp->next() != NULL) { | |
1239 assert(tmp->rank() == Mutex::native || | |
1240 tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?"); | |
1241 } | |
1242 } | |
1243 } | |
1244 return res; | |
1245 } | |
1246 | |
1247 | |
1248 bool Monitor::contains(Monitor* locks, Monitor * lock) { | |
1249 for (; locks != NULL; locks = locks->next()) { | |
1250 if (locks == lock) | |
1251 return true; | |
1252 } | |
1253 return false; | |
1254 } | |
1255 #endif | |
1256 | |
1257 // Called immediately after lock acquisition or release as a diagnostic | |
1258 // to track the lock-set of the thread and test for rank violations that | |
1259 // might indicate exposure to deadlock. | |
1260 // Rather like an EventListener for _owner (:>). | |
1261 | |
1262 void Monitor::set_owner_implementation(Thread *new_owner) { | |
1263 // This function is solely responsible for maintaining | |
1264 // and checking the invariant that threads and locks | |
1265 // are in a 1/N relation, with some some locks unowned. | |
1266 // It uses the Mutex::_owner, Mutex::_next, and | |
1267 // Thread::_owned_locks fields, and no other function | |
1268 // changes those fields. | |
1269 // It is illegal to set the mutex from one non-NULL | |
1270 // owner to another--it must be owned by NULL as an | |
1271 // intermediate state. | |
1272 | |
1273 if (new_owner != NULL) { | |
1274 // the thread is acquiring this lock | |
1275 | |
1276 assert(new_owner == Thread::current(), "Should I be doing this?"); | |
1277 assert(_owner == NULL, "setting the owner thread of an already owned mutex"); | |
1278 _owner = new_owner; // set the owner | |
1279 | |
1280 // link "this" into the owned locks list | |
1281 | |
1282 #ifdef ASSERT // Thread::_owned_locks is under the same ifdef | |
1283 Monitor* locks = get_least_ranked_lock(new_owner->owned_locks()); | |
1284 // Mutex::set_owner_implementation is a friend of Thread | |
1285 | |
1286 assert(this->rank() >= 0, "bad lock rank"); | |
1287 | |
1288 // Deadlock avoidance rules require us to acquire Mutexes only in | |
1289 // a global total order. For example m1 is the lowest ranked mutex | |
1290 // that the thread holds and m2 is the mutex the thread is trying | |
1291 // to acquire, then deadlock avoidance rules require that the rank | |
1292 // of m2 be less than the rank of m1. | |
1293 // The rank Mutex::native is an exception in that it is not subject | |
1294 // to the verification rules. | |
1295 // Here are some further notes relating to mutex acquisition anomalies: | |
1296 // . under Solaris, the interrupt lock gets acquired when doing | |
1297 // profiling, so any lock could be held. | |
1298 // . it is also ok to acquire Safepoint_lock at the very end while we | |
1299 // already hold Terminator_lock - may happen because of periodic safepoints | |
1300 if (this->rank() != Mutex::native && | |
1301 this->rank() != Mutex::suspend_resume && | |
1302 locks != NULL && locks->rank() <= this->rank() && | |
1303 !SafepointSynchronize::is_at_safepoint() && | |
1304 this != Interrupt_lock && this != ProfileVM_lock && | |
1305 !(this == Safepoint_lock && contains(locks, Terminator_lock) && | |
1306 SafepointSynchronize::is_synchronizing())) { | |
1307 new_owner->print_owned_locks(); | |
1490
f03d0a26bf83
6888954: argument formatting for assert() and friends
jcoomes
parents:
196
diff
changeset
|
1308 fatal(err_msg("acquiring lock %s/%d out of order with lock %s/%d -- " |
f03d0a26bf83
6888954: argument formatting for assert() and friends
jcoomes
parents:
196
diff
changeset
|
1309 "possible deadlock", this->name(), this->rank(), |
f03d0a26bf83
6888954: argument formatting for assert() and friends
jcoomes
parents:
196
diff
changeset
|
1310 locks->name(), locks->rank())); |
0 | 1311 } |
1312 | |
1313 this->_next = new_owner->_owned_locks; | |
1314 new_owner->_owned_locks = this; | |
1315 #endif | |
1316 | |
1317 } else { | |
1318 // the thread is releasing this lock | |
1319 | |
1320 Thread* old_owner = _owner; | |
1321 debug_only(_last_owner = old_owner); | |
1322 | |
1323 assert(old_owner != NULL, "removing the owner thread of an unowned mutex"); | |
1324 assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex"); | |
1325 | |
1326 _owner = NULL; // set the owner | |
1327 | |
1328 #ifdef ASSERT | |
1329 Monitor *locks = old_owner->owned_locks(); | |
1330 | |
1331 // remove "this" from the owned locks list | |
1332 | |
1333 Monitor *prev = NULL; | |
1334 bool found = false; | |
1335 for (; locks != NULL; prev = locks, locks = locks->next()) { | |
1336 if (locks == this) { | |
1337 found = true; | |
1338 break; | |
1339 } | |
1340 } | |
1341 assert(found, "Removing a lock not owned"); | |
1342 if (prev == NULL) { | |
1343 old_owner->_owned_locks = _next; | |
1344 } else { | |
1345 prev->_next = _next; | |
1346 } | |
1347 _next = NULL; | |
1348 #endif | |
1349 } | |
1350 } | |
1351 | |
1352 | |
1353 // Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock() | |
1354 void Monitor::check_prelock_state(Thread *thread) { | |
1355 assert((!thread->is_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm) | |
1356 || rank() == Mutex::special, "wrong thread state for using locks"); | |
1357 if (StrictSafepointChecks) { | |
1358 if (thread->is_VM_thread() && !allow_vm_block()) { | |
1490
f03d0a26bf83
6888954: argument formatting for assert() and friends
jcoomes
parents:
196
diff
changeset
|
1359 fatal(err_msg("VM thread using lock %s (not allowed to block on)", |
f03d0a26bf83
6888954: argument formatting for assert() and friends
jcoomes
parents:
196
diff
changeset
|
1360 name())); |
0 | 1361 } |
1362 debug_only(if (rank() != Mutex::special) \ | |
1363 thread->check_for_valid_safepoint_state(false);) | |
1364 } | |
11151 | 1365 if (thread->is_Watcher_thread()) { |
1366 assert(!WatcherThread::watcher_thread()->has_crash_protection(), | |
1367 "locking not allowed when crash protection is set"); | |
1368 } | |
0 | 1369 } |
1370 | |
1371 void Monitor::check_block_state(Thread *thread) { | |
1372 if (!_allow_vm_block && thread->is_VM_thread()) { | |
1373 warning("VM thread blocked on lock"); | |
1374 print(); | |
1375 BREAKPOINT; | |
1376 } | |
1377 assert(_owner != thread, "deadlock: blocking on monitor owned by current thread"); | |
1378 } | |
1379 | |
1380 #endif // PRODUCT |