author | coleenp |
Tue, 24 Sep 2019 10:12:56 -0400 | |
changeset 58291 | a013100f7a35 |
parent 57840 | 4863a802a7c1 |
child 58409 | a595e67d6683 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
2 |
* Copyright (c) 1998, 2019, 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:
5403
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
5403
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:
5403
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
26 |
#include "logging/log.hpp" |
49449
ef5d5d343e2a
8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents:
48488
diff
changeset
|
27 |
#include "runtime/interfaceSupport.inline.hpp" |
7397 | 28 |
#include "runtime/mutex.hpp" |
29 |
#include "runtime/osThread.hpp" |
|
47881
0ce0ac68ace7
8189941: Implementation JEP 312: Thread-local handshake
rehn
parents:
47634
diff
changeset
|
30 |
#include "runtime/safepointMechanism.inline.hpp" |
14583
d70ee55535f4
8003935: Simplify the needed includes for using Thread::current()
stefank
parents:
11636
diff
changeset
|
31 |
#include "runtime/thread.inline.hpp" |
7397 | 32 |
#include "utilities/events.hpp" |
40010 | 33 |
#include "utilities/macros.hpp" |
1 | 34 |
|
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
35 |
#ifdef ASSERT |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
36 |
void Mutex::check_block_state(Thread* thread) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
37 |
if (!_allow_vm_block && thread->is_VM_thread()) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
38 |
// JavaThreads are checked to make sure that they do not hold _allow_vm_block locks during operations |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
39 |
// that could safepoint. Make sure the vm thread never uses locks with _allow_vm_block == false. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
40 |
fatal("VM thread could block on lock that may be held by a JavaThread during safepoint: %s", name()); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
41 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
42 |
|
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
43 |
assert(!os::ThreadCrashProtection::is_crash_protected(thread), |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
44 |
"locking not allowed when crash protection is set"); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
45 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
46 |
|
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
47 |
void Mutex::check_safepoint_state(Thread* thread) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
48 |
check_block_state(thread); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
49 |
|
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
50 |
// If the JavaThread checks for safepoint, verify that the lock wasn't created with safepoint_check_never. |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
51 |
if (thread->is_active_Java_thread()) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
52 |
assert(_safepoint_check_required != _safepoint_check_never, |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
53 |
"This lock should %s have a safepoint check for Java threads: %s", |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
54 |
_safepoint_check_required ? "always" : "never", name()); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
55 |
|
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
56 |
// Also check NoSafepointVerifier, and thread state is _thread_in_vm |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
57 |
thread->check_for_valid_safepoint_state(); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
58 |
} else { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
59 |
// If initialized with safepoint_check_never, a NonJavaThread should never ask to safepoint check either. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
60 |
assert(_safepoint_check_required != _safepoint_check_never, |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
61 |
"NonJavaThread should not check for safepoint"); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
62 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
63 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
64 |
|
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
65 |
void Mutex::check_no_safepoint_state(Thread* thread) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
66 |
check_block_state(thread); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
67 |
assert(!thread->is_active_Java_thread() || _safepoint_check_required != _safepoint_check_always, |
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
68 |
"This lock should %s have a safepoint check for Java threads: %s", |
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
69 |
_safepoint_check_required ? "always" : "never", name()); |
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
70 |
} |
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
71 |
#endif // ASSERT |
1 | 72 |
|
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
73 |
void Mutex::lock(Thread* self) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
74 |
check_safepoint_state(self); |
28163
322d55d167be
8047290: Make Mutex::_no_safepoint_check_flag locks verify that this lock never checks for safepoint
coleenp
parents:
26684
diff
changeset
|
75 |
|
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
76 |
assert(_owner != self, "invariant"); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
77 |
|
57840 | 78 |
Mutex* in_flight_mutex = NULL; |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
79 |
DEBUG_ONLY(int retry_cnt = 0;) |
57699
4aea554692aa
8226228: Make Threads_lock an always safepoint checked lock.
rehn
parents:
57668
diff
changeset
|
80 |
bool is_active_Java_thread = self->is_active_Java_thread(); |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
81 |
while (!_lock.try_lock()) { |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
82 |
// The lock is contended |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
83 |
|
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
84 |
#ifdef ASSERT |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
85 |
if (retry_cnt++ > 3) { |
57840 | 86 |
log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name); |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
87 |
} |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
88 |
#endif // ASSERT |
1 | 89 |
|
57699
4aea554692aa
8226228: Make Threads_lock an always safepoint checked lock.
rehn
parents:
57668
diff
changeset
|
90 |
// Is it a JavaThread participating in the safepoint protocol. |
4aea554692aa
8226228: Make Threads_lock an always safepoint checked lock.
rehn
parents:
57668
diff
changeset
|
91 |
if (is_active_Java_thread) { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
92 |
assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex"); |
57840 | 93 |
{ ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex); |
94 |
in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck |
|
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
95 |
_lock.lock(); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
96 |
} |
57840 | 97 |
if (in_flight_mutex != NULL) { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
98 |
// Not unlocked by ~ThreadBlockInVMWithDeadlockCheck |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
99 |
break; |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
100 |
} |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
101 |
} else { |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
102 |
_lock.lock(); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
103 |
break; |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
104 |
} |
1 | 105 |
} |
106 |
||
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
107 |
assert_owner(NULL); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
108 |
set_owner(self); |
1 | 109 |
} |
110 |
||
57840 | 111 |
void Mutex::lock() { |
1 | 112 |
this->lock(Thread::current()); |
113 |
} |
|
114 |
||
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
115 |
// Lock without safepoint check - a degenerate variant of lock() for use by |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
116 |
// JavaThreads when it is known to be safe to not check for a safepoint when |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
117 |
// acquiring this lock. If the thread blocks acquiring the lock it is not |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
118 |
// safepoint-safe and so will prevent a safepoint from being reached. If used |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
119 |
// in the wrong way this can lead to a deadlock with the safepoint code. |
1 | 120 |
|
57840 | 121 |
void Mutex::lock_without_safepoint_check(Thread * self) { |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
122 |
check_no_safepoint_state(self); |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
123 |
assert(_owner != self, "invariant"); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
124 |
_lock.lock(); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
125 |
assert_owner(NULL); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
126 |
set_owner(self); |
1 | 127 |
} |
128 |
||
57840 | 129 |
void Mutex::lock_without_safepoint_check() { |
25069
c937c5e883c5
8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents:
24424
diff
changeset
|
130 |
lock_without_safepoint_check(Thread::current()); |
1 | 131 |
} |
132 |
||
133 |
||
22551 | 134 |
// Returns true if thread succeeds in grabbing the lock, otherwise false. |
1 | 135 |
|
57840 | 136 |
bool Mutex::try_lock() { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
137 |
Thread * const self = Thread::current(); |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
138 |
// Some safepoint_check_always locks use try_lock, so cannot check |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
139 |
// safepoint state, but can check blocking state. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
140 |
check_block_state(self); |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
141 |
if (_lock.try_lock()) { |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
142 |
assert_owner(NULL); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
143 |
set_owner(self); |
1 | 144 |
return true; |
145 |
} |
|
146 |
return false; |
|
147 |
} |
|
148 |
||
57840 | 149 |
void Mutex::release_for_safepoint() { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
150 |
assert_owner(NULL); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
151 |
_lock.unlock(); |
1 | 152 |
} |
153 |
||
57840 | 154 |
void Mutex::unlock() { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
155 |
assert_owner(Thread::current()); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
156 |
set_owner(NULL); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
157 |
_lock.unlock(); |
1 | 158 |
} |
159 |
||
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
160 |
void Monitor::notify() { |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
161 |
assert_owner(Thread::current()); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
162 |
_lock.notify(); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
163 |
} |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
164 |
|
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
165 |
void Monitor::notify_all() { |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
166 |
assert_owner(Thread::current()); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
167 |
_lock.notify_all(); |
1 | 168 |
} |
169 |
||
54495
941db9c0b5b5
8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents:
53775
diff
changeset
|
170 |
#ifdef ASSERT |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
171 |
void Monitor::assert_wait_lock_state(Thread* self) { |
57840 | 172 |
Mutex* least = get_least_ranked_lock_besides_this(self->owned_locks()); |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
173 |
assert(least != this, "Specification of get_least_... call above"); |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
174 |
if (least != NULL && least->rank() <= special) { |
52913
bf2f2560dd53
8214315: G1: fatal error: acquiring lock SATB_Q_FL_lock/1 out of order with lock tty_lock/0
kbarrett
parents:
52581
diff
changeset
|
175 |
::tty->print("Attempting to wait on monitor %s/%d while holding" |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
176 |
" lock %s/%d -- possible deadlock", |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
177 |
name(), rank(), least->name(), least->rank()); |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
178 |
assert(false, "Shouldn't block(wait) while holding a lock of rank special"); |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
179 |
} |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
180 |
} |
54495
941db9c0b5b5
8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents:
53775
diff
changeset
|
181 |
#endif // ASSERT |
941db9c0b5b5
8222231: Clean up interfaceSupport.inline.hpp duplicated code
coleenp
parents:
53775
diff
changeset
|
182 |
|
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
183 |
bool Monitor::wait_without_safepoint_check(long timeout) { |
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
184 |
Thread* const self = Thread::current(); |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
185 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
186 |
// timeout is in milliseconds - with zero meaning never timeout |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
187 |
assert(timeout >= 0, "negative timeout"); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
188 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
189 |
assert_owner(self); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
190 |
assert_wait_lock_state(self); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
191 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
192 |
// conceptually set the owner to NULL in anticipation of |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
193 |
// abdicating the lock in wait |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
194 |
set_owner(NULL); |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
195 |
// Check safepoint state after resetting owner and possible NSV. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
196 |
check_no_safepoint_state(self); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
197 |
|
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
198 |
int wait_status = _lock.wait(timeout); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
199 |
set_owner(self); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
200 |
return wait_status != 0; // return true IFF timeout |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
201 |
} |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
202 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
203 |
bool Monitor::wait(long timeout, bool as_suspend_equivalent) { |
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
204 |
Thread* const self = Thread::current(); |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
205 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
206 |
// timeout is in milliseconds - with zero meaning never timeout |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
207 |
assert(timeout >= 0, "negative timeout"); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
208 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
209 |
assert_owner(self); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
210 |
|
57699
4aea554692aa
8226228: Make Threads_lock an always safepoint checked lock.
rehn
parents:
57668
diff
changeset
|
211 |
// Safepoint checking logically implies an active JavaThread. |
4aea554692aa
8226228: Make Threads_lock an always safepoint checked lock.
rehn
parents:
57668
diff
changeset
|
212 |
guarantee(self->is_active_Java_thread(), "invariant"); |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
213 |
assert_wait_lock_state(self); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
214 |
|
25069
c937c5e883c5
8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents:
24424
diff
changeset
|
215 |
int wait_status; |
1 | 216 |
// conceptually set the owner to NULL in anticipation of |
217 |
// abdicating the lock in wait |
|
218 |
set_owner(NULL); |
|
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
219 |
// Check safepoint state after resetting owner and possible NSV. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
220 |
check_safepoint_state(self); |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
221 |
JavaThread *jt = (JavaThread *)self; |
57840 | 222 |
Mutex* in_flight_mutex = NULL; |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
223 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
224 |
{ |
57840 | 225 |
ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_mutex); |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
226 |
OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
227 |
if (as_suspend_equivalent) { |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
228 |
jt->set_suspend_equivalent(); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
229 |
// cleared by handle_special_suspend_equivalent_condition() or |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
230 |
// java_suspend_self() |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
231 |
} |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
232 |
|
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
233 |
wait_status = _lock.wait(timeout); |
57840 | 234 |
in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
235 |
|
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
236 |
// were we externally suspended while we were waiting? |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
237 |
if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) { |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
238 |
// Our event wait has finished and we own the lock, but |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
239 |
// while we were waiting another thread suspended us. We don't |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
240 |
// want to hold the lock while suspended because that |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
241 |
// would surprise the thread that suspended us. |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
242 |
_lock.unlock(); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
243 |
jt->java_suspend_self(); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
244 |
_lock.lock(); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
245 |
} |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
246 |
} |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
247 |
|
57840 | 248 |
if (in_flight_mutex != NULL) { |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
249 |
// Not unlocked by ~ThreadBlockInVMWithDeadlockCheck |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
250 |
assert_owner(NULL); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
251 |
// Conceptually reestablish ownership of the lock. |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
252 |
set_owner(self); |
1 | 253 |
} else { |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
254 |
lock(self); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
54495
diff
changeset
|
255 |
} |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
256 |
|
25069
c937c5e883c5
8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents:
24424
diff
changeset
|
257 |
return wait_status != 0; // return true IFF timeout |
1 | 258 |
} |
259 |
||
57840 | 260 |
Mutex::~Mutex() { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
261 |
assert_owner(NULL); |
1 | 262 |
} |
263 |
||
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
264 |
// Only Threads_lock, Heap_lock and SR_lock may be safepoint_check_sometimes. |
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
265 |
bool is_sometimes_ok(const char* name) { |
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
266 |
return (strcmp(name, "Threads_lock") == 0 || strcmp(name, "Heap_lock") == 0 || strcmp(name, "SR_lock") == 0); |
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
267 |
} |
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
268 |
|
57840 | 269 |
Mutex::Mutex(int Rank, const char * name, bool allow_vm_block, |
270 |
SafepointCheckRequired safepoint_check_required) : _owner(NULL) { |
|
52581
d402a406bbc3
8213723: More Monitor/mutex initialization management
dholmes
parents:
52555
diff
changeset
|
271 |
assert(os::mutex_init_done(), "Too early!"); |
57668 | 272 |
if (name == NULL) { |
273 |
strcpy(_name, "UNKNOWN"); |
|
274 |
} else { |
|
57840 | 275 |
strncpy(_name, name, MUTEX_NAME_LEN - 1); |
276 |
_name[MUTEX_NAME_LEN - 1] = '\0'; |
|
57668 | 277 |
} |
1 | 278 |
#ifdef ASSERT |
279 |
_allow_vm_block = allow_vm_block; |
|
25069
c937c5e883c5
8047156: cleanup more non-indent white space issues prior to Contended Locking cleanup bucket
dcubed
parents:
24424
diff
changeset
|
280 |
_rank = Rank; |
57668 | 281 |
_safepoint_check_required = safepoint_check_required; |
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
282 |
|
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
283 |
assert(_safepoint_check_required != _safepoint_check_sometimes || is_sometimes_ok(name), |
54663
f03d5a093093
8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents:
54623
diff
changeset
|
284 |
"Lock has _safepoint_check_sometimes %s", name); |
1 | 285 |
#endif |
286 |
} |
|
287 |
||
57840 | 288 |
Monitor::Monitor(int Rank, const char * name, bool allow_vm_block, |
57668 | 289 |
SafepointCheckRequired safepoint_check_required) : |
57840 | 290 |
Mutex(Rank, name, allow_vm_block, safepoint_check_required) {} |
1 | 291 |
|
57840 | 292 |
bool Mutex::owned_by_self() const { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
293 |
return _owner == Thread::current(); |
1 | 294 |
} |
295 |
||
57840 | 296 |
void Mutex::print_on_error(outputStream* st) const { |
33148
68fa8b6c4340
8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents:
33105
diff
changeset
|
297 |
st->print("[" PTR_FORMAT, p2i(this)); |
1 | 298 |
st->print("] %s", _name); |
33148
68fa8b6c4340
8042893: compiler: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC needs to be removed from source files
david
parents:
33105
diff
changeset
|
299 |
st->print(" - owner thread: " PTR_FORMAT, p2i(_owner)); |
1 | 300 |
} |
301 |
||
302 |
// ---------------------------------------------------------------------------------- |
|
303 |
// Non-product code |
|
304 |
||
305 |
#ifndef PRODUCT |
|
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
306 |
const char* print_safepoint_check(Mutex::SafepointCheckRequired safepoint_check) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
307 |
switch (safepoint_check) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
308 |
case Mutex::_safepoint_check_never: return "safepoint_check_never"; |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
309 |
case Mutex::_safepoint_check_sometimes: return "safepoint_check_sometimes"; |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
310 |
case Mutex::_safepoint_check_always: return "safepoint_check_always"; |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
311 |
default: return ""; |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
312 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
313 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
314 |
|
57840 | 315 |
void Mutex::print_on(outputStream* st) const { |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
316 |
st->print("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT, |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
317 |
p2i(this), _name, p2i(_owner)); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
318 |
if (_allow_vm_block) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
319 |
st->print("%s", " allow_vm_block"); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
320 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
321 |
st->print(" %s", print_safepoint_check(_safepoint_check_required)); |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
322 |
st->cr(); |
1 | 323 |
} |
324 |
#endif |
|
325 |
||
326 |
#ifdef ASSERT |
|
57840 | 327 |
void Mutex::assert_owner(Thread * expected) { |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
328 |
const char* msg = "invalid owner"; |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
329 |
if (expected == NULL) { |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
330 |
msg = "should be un-owned"; |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
331 |
} |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
332 |
else if (expected == Thread::current()) { |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
333 |
msg = "should be owned by current thread"; |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
334 |
} |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
335 |
assert(_owner == expected, |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
336 |
"%s: owner=" INTPTR_FORMAT ", should be=" INTPTR_FORMAT, |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
337 |
msg, p2i(_owner), p2i(expected)); |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
338 |
} |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
339 |
|
57840 | 340 |
Mutex* Mutex::get_least_ranked_lock(Mutex* locks) { |
341 |
Mutex *res, *tmp; |
|
1 | 342 |
for (res = tmp = locks; tmp != NULL; tmp = tmp->next()) { |
343 |
if (tmp->rank() < res->rank()) { |
|
344 |
res = tmp; |
|
345 |
} |
|
346 |
} |
|
347 |
if (!SafepointSynchronize::is_at_safepoint()) { |
|
348 |
// In this case, we expect the held locks to be |
|
349 |
// in increasing rank order (modulo any native ranks) |
|
350 |
for (tmp = locks; tmp != NULL; tmp = tmp->next()) { |
|
351 |
if (tmp->next() != NULL) { |
|
352 |
assert(tmp->rank() == Mutex::native || |
|
353 |
tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?"); |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
return res; |
|
358 |
} |
|
359 |
||
57840 | 360 |
Mutex* Mutex::get_least_ranked_lock_besides_this(Mutex* locks) { |
361 |
Mutex *res, *tmp; |
|
1 | 362 |
for (res = NULL, tmp = locks; tmp != NULL; tmp = tmp->next()) { |
363 |
if (tmp != this && (res == NULL || tmp->rank() < res->rank())) { |
|
364 |
res = tmp; |
|
365 |
} |
|
366 |
} |
|
367 |
if (!SafepointSynchronize::is_at_safepoint()) { |
|
368 |
// In this case, we expect the held locks to be |
|
369 |
// in increasing rank order (modulo any native ranks) |
|
370 |
for (tmp = locks; tmp != NULL; tmp = tmp->next()) { |
|
371 |
if (tmp->next() != NULL) { |
|
372 |
assert(tmp->rank() == Mutex::native || |
|
373 |
tmp->rank() <= tmp->next()->rank(), "mutex rank anomaly?"); |
|
374 |
} |
|
375 |
} |
|
376 |
} |
|
377 |
return res; |
|
378 |
} |
|
379 |
||
57840 | 380 |
bool Mutex::contains(Mutex* locks, Mutex* lock) { |
1 | 381 |
for (; locks != NULL; locks = locks->next()) { |
26684
d1221849ea3d
8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
26683
diff
changeset
|
382 |
if (locks == lock) { |
1 | 383 |
return true; |
26684
d1221849ea3d
8057109: manual cleanup of white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
26683
diff
changeset
|
384 |
} |
1 | 385 |
} |
386 |
return false; |
|
387 |
} |
|
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
388 |
|
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
389 |
// NSV implied with locking allow_vm_block or !safepoint_check locks. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
390 |
void Mutex::no_safepoint_verifier(Thread* thread, bool enable) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
391 |
// Threads_lock is special, since the safepoint synchronization will not start before this is |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
392 |
// acquired. Hence, a JavaThread cannot be holding it at a safepoint. So is VMOperationRequest_lock, |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
393 |
// since it is used to transfer control between JavaThreads and the VMThread |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
394 |
// Do not *exclude* any locks unless you are absolutely sure it is correct. Ask someone else first! |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
395 |
if ((_allow_vm_block && |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
396 |
this != Threads_lock && |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
397 |
this != Compile_lock && // Temporary: should not be necessary when we get separate compilation |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
398 |
this != tty_lock && // The tty_lock is released for the safepoint. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
399 |
this != VMOperationRequest_lock && |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
400 |
this != VMOperationQueue_lock) || |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
401 |
rank() == Mutex::special) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
402 |
if (enable) { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
403 |
thread->_no_safepoint_count++; |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
404 |
} else { |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
405 |
thread->_no_safepoint_count--; |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
406 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
407 |
} |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
408 |
} |
1 | 409 |
|
410 |
// Called immediately after lock acquisition or release as a diagnostic |
|
411 |
// to track the lock-set of the thread and test for rank violations that |
|
412 |
// might indicate exposure to deadlock. |
|
413 |
// Rather like an EventListener for _owner (:>). |
|
414 |
||
57840 | 415 |
void Mutex::set_owner_implementation(Thread *new_owner) { |
1 | 416 |
// This function is solely responsible for maintaining |
417 |
// and checking the invariant that threads and locks |
|
418 |
// are in a 1/N relation, with some some locks unowned. |
|
419 |
// It uses the Mutex::_owner, Mutex::_next, and |
|
420 |
// Thread::_owned_locks fields, and no other function |
|
421 |
// changes those fields. |
|
422 |
// It is illegal to set the mutex from one non-NULL |
|
423 |
// owner to another--it must be owned by NULL as an |
|
424 |
// intermediate state. |
|
425 |
||
426 |
if (new_owner != NULL) { |
|
427 |
// the thread is acquiring this lock |
|
428 |
||
429 |
assert(new_owner == Thread::current(), "Should I be doing this?"); |
|
430 |
assert(_owner == NULL, "setting the owner thread of an already owned mutex"); |
|
431 |
_owner = new_owner; // set the owner |
|
432 |
||
433 |
// link "this" into the owned locks list |
|
434 |
||
57840 | 435 |
Mutex* locks = get_least_ranked_lock(new_owner->owned_locks()); |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
436 |
// Mutex::set_owner_implementation is a friend of Thread |
1 | 437 |
|
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
438 |
assert(this->rank() >= 0, "bad lock rank"); |
1 | 439 |
|
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
440 |
// Deadlock avoidance rules require us to acquire Mutexes only in |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
441 |
// a global total order. For example m1 is the lowest ranked mutex |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
442 |
// that the thread holds and m2 is the mutex the thread is trying |
53646
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
443 |
// to acquire, then deadlock avoidance rules require that the rank |
043ae846819f
8210832: Remove sneaky locking in class Monitor
pchilanomate
parents:
52913
diff
changeset
|
444 |
// of m2 be less than the rank of m1. |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
445 |
// The rank Mutex::native is an exception in that it is not subject |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
446 |
// to the verification rules. |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
447 |
if (this->rank() != Mutex::native && |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
448 |
this->rank() != Mutex::suspend_resume && |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
449 |
locks != NULL && locks->rank() <= this->rank() && |
53775 | 450 |
!SafepointSynchronize::is_at_safepoint()) { |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
451 |
new_owner->print_owned_locks(); |
33105
294e48b4f704
8080775: Better argument formatting for assert() and friends
david
parents:
28163
diff
changeset
|
452 |
fatal("acquiring lock %s/%d out of order with lock %s/%d -- " |
294e48b4f704
8080775: Better argument formatting for assert() and friends
david
parents:
28163
diff
changeset
|
453 |
"possible deadlock", this->name(), this->rank(), |
294e48b4f704
8080775: Better argument formatting for assert() and friends
david
parents:
28163
diff
changeset
|
454 |
locks->name(), locks->rank()); |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
455 |
} |
1 | 456 |
|
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
457 |
this->_next = new_owner->_owned_locks; |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
458 |
new_owner->_owned_locks = this; |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
459 |
|
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
460 |
// NSV implied with locking allow_vm_block flag. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
461 |
no_safepoint_verifier(new_owner, true); |
1 | 462 |
|
463 |
} else { |
|
464 |
// the thread is releasing this lock |
|
465 |
||
466 |
Thread* old_owner = _owner; |
|
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
467 |
_last_owner = old_owner; |
1 | 468 |
|
469 |
assert(old_owner != NULL, "removing the owner thread of an unowned mutex"); |
|
470 |
assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex"); |
|
471 |
||
472 |
_owner = NULL; // set the owner |
|
473 |
||
57840 | 474 |
Mutex* locks = old_owner->owned_locks(); |
1 | 475 |
|
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
476 |
// remove "this" from the owned locks list |
1 | 477 |
|
57840 | 478 |
Mutex* prev = NULL; |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
479 |
bool found = false; |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
480 |
for (; locks != NULL; prev = locks, locks = locks->next()) { |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
481 |
if (locks == this) { |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
482 |
found = true; |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
483 |
break; |
1 | 484 |
} |
26683
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
485 |
} |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
486 |
assert(found, "Removing a lock not owned"); |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
487 |
if (prev == NULL) { |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
488 |
old_owner->_owned_locks = _next; |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
489 |
} else { |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
490 |
prev->_next = _next; |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
491 |
} |
a02753d5a0b2
8057107: cleanup indent white space issues prior to Contended Locking reorder and cache line bucket
dcubed
parents:
25472
diff
changeset
|
492 |
_next = NULL; |
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
493 |
|
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
494 |
// ~NSV implied with locking allow_vm_block flag. |
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
495 |
no_safepoint_verifier(old_owner, false); |
1 | 496 |
} |
497 |
} |
|
58291
a013100f7a35
8213150: Add verification for locking by VMThread
coleenp
parents:
57840
diff
changeset
|
498 |
#endif // ASSERT |