author | dholmes |
Mon, 07 Oct 2019 18:44:53 -0400 | |
changeset 58488 | 165b193b30dd |
parent 58196 | cea6839598e8 |
child 58509 | 7b41c88f8432 |
permissions | -rw-r--r-- |
6975 | 1 |
/* |
54469
8592226f5cd3
8221584: SIGSEGV in os::PlatformEvent::unpark() in JvmtiRawMonitor::raw_exit while posting method exit event
dholmes
parents:
51702
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved. |
6975 | 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 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
49360 | 26 |
#include "memory/allocation.inline.hpp" |
7397 | 27 |
#include "prims/jvmtiRawMonitor.hpp" |
40655
9f644073d3a0
8157907: Incorrect inclusion of atomic.hpp instead of atomic.inline.hpp
dholmes
parents:
25351
diff
changeset
|
28 |
#include "runtime/atomic.hpp" |
49449
ef5d5d343e2a
8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents:
49360
diff
changeset
|
29 |
#include "runtime/interfaceSupport.inline.hpp" |
50429
83aec1d357d4
8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents:
49449
diff
changeset
|
30 |
#include "runtime/orderAccess.hpp" |
24351
61b33cc6d3cf
8042195: Introduce umbrella header orderAccess.inline.hpp.
goetz
parents:
13963
diff
changeset
|
31 |
#include "runtime/thread.inline.hpp" |
6975 | 32 |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
33 |
JvmtiRawMonitor::QNode::QNode(Thread* thread) : _next(NULL), _prev(NULL), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
34 |
_event(thread->_ParkEvent), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
35 |
_notified(0), TState(TS_RUN) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
36 |
} |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
37 |
|
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
38 |
GrowableArray<JvmtiRawMonitor*> *JvmtiPendingMonitors::_monitors = |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
39 |
new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiRawMonitor*>(1, true); |
6975 | 40 |
|
41 |
void JvmtiPendingMonitors::transition_raw_monitors() { |
|
42 |
assert((Threads::number_of_threads()==1), |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
43 |
"Java thread has not been created yet or more than one java thread \ |
6975 | 44 |
is running. Raw monitor transition will not work"); |
45 |
JavaThread *current_java_thread = JavaThread::current(); |
|
46 |
assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm"); |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
47 |
for(int i=0; i< count(); i++) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
48 |
JvmtiRawMonitor *rmonitor = monitors()->at(i); |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
49 |
rmonitor->raw_enter(current_java_thread); |
6975 | 50 |
} |
51 |
// pending monitors are converted to real monitor so delete them all. |
|
52 |
dispose(); |
|
53 |
} |
|
54 |
||
55 |
// |
|
56 |
// class JvmtiRawMonitor |
|
57 |
// |
|
58 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
59 |
JvmtiRawMonitor::JvmtiRawMonitor(const char *name) : _owner(NULL), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
60 |
_recursions(0), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
61 |
_EntryList(NULL), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
62 |
_WaitSet(NULL), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
63 |
_waiters(0), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
64 |
_magic(JVMTI_RM_MAGIC), |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
65 |
_name(NULL) { |
6975 | 66 |
#ifdef ASSERT |
13195 | 67 |
_name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtInternal), name); |
6975 | 68 |
#endif |
69 |
} |
|
70 |
||
71 |
JvmtiRawMonitor::~JvmtiRawMonitor() { |
|
72 |
#ifdef ASSERT |
|
73 |
FreeHeap(_name); |
|
74 |
#endif |
|
75 |
_magic = 0; |
|
76 |
} |
|
77 |
||
78 |
||
79 |
bool |
|
80 |
JvmtiRawMonitor::is_valid() { |
|
81 |
int value = 0; |
|
82 |
||
83 |
// This object might not be a JvmtiRawMonitor so we can't assume |
|
84 |
// the _magic field is properly aligned. Get the value in a safe |
|
85 |
// way and then check against JVMTI_RM_MAGIC. |
|
86 |
||
87 |
switch (sizeof(_magic)) { |
|
88 |
case 2: |
|
89 |
value = Bytes::get_native_u2((address)&_magic); |
|
90 |
break; |
|
91 |
||
92 |
case 4: |
|
93 |
value = Bytes::get_native_u4((address)&_magic); |
|
94 |
break; |
|
95 |
||
96 |
case 8: |
|
97 |
value = Bytes::get_native_u8((address)&_magic); |
|
98 |
break; |
|
99 |
||
100 |
default: |
|
101 |
guarantee(false, "_magic field is an unexpected size"); |
|
102 |
} |
|
103 |
||
104 |
return value == JVMTI_RM_MAGIC; |
|
105 |
} |
|
106 |
||
107 |
// ------------------------------------------------------------------------- |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
108 |
// The JVMTI raw monitor subsystem is entirely distinct from normal |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
109 |
// java-synchronization or jni-synchronization. JVMTI raw monitors are not |
6975 | 110 |
// associated with objects. They can be implemented in any manner |
111 |
// that makes sense. The original implementors decided to piggy-back |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
112 |
// the raw-monitor implementation on the existing Java ObjectMonitor mechanism. |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
113 |
// Now we just use a simplified form of that ObjectMonitor code. |
6975 | 114 |
// |
115 |
// Note that we use the single RawMonitor_lock to protect queue operations for |
|
116 |
// _all_ raw monitors. This is a scalability impediment, but since raw monitor usage |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
117 |
// is fairly rare, this is not of concern. The RawMonitor_lock can not |
6975 | 118 |
// be held indefinitely. The critical sections must be short and bounded. |
119 |
// |
|
120 |
// ------------------------------------------------------------------------- |
|
121 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
122 |
void JvmtiRawMonitor::SimpleEnter (Thread * Self) { |
6975 | 123 |
for (;;) { |
48488 | 124 |
if (Atomic::replace_if_null(Self, &_owner)) { |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
125 |
return ; |
6975 | 126 |
} |
127 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
128 |
QNode Node (Self) ; |
6975 | 129 |
Self->_ParkEvent->reset() ; // strictly optional |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
130 |
Node.TState = QNode::TS_ENTER ; |
6975 | 131 |
|
132 |
RawMonitor_lock->lock_without_safepoint_check() ; |
|
133 |
Node._next = _EntryList ; |
|
134 |
_EntryList = &Node ; |
|
135 |
OrderAccess::fence() ; |
|
48488 | 136 |
if (_owner == NULL && Atomic::replace_if_null(Self, &_owner)) { |
6975 | 137 |
_EntryList = Node._next ; |
138 |
RawMonitor_lock->unlock() ; |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
139 |
return ; |
6975 | 140 |
} |
141 |
RawMonitor_lock->unlock() ; |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
142 |
while (Node.TState == QNode::TS_ENTER) { |
6975 | 143 |
Self->_ParkEvent->park() ; |
144 |
} |
|
145 |
} |
|
146 |
} |
|
147 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
148 |
void JvmtiRawMonitor::SimpleExit (Thread * Self) { |
6975 | 149 |
guarantee (_owner == Self, "invariant") ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
150 |
OrderAccess::release_store(&_owner, (Thread*)NULL) ; |
6975 | 151 |
OrderAccess::fence() ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
152 |
if (_EntryList == NULL) return ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
153 |
QNode * w ; |
6975 | 154 |
|
155 |
RawMonitor_lock->lock_without_safepoint_check() ; |
|
156 |
w = _EntryList ; |
|
157 |
if (w != NULL) { |
|
158 |
_EntryList = w->_next ; |
|
159 |
} |
|
160 |
RawMonitor_lock->unlock() ; |
|
161 |
if (w != NULL) { |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
162 |
guarantee (w ->TState == QNode::TS_ENTER, "invariant") ; |
54469
8592226f5cd3
8221584: SIGSEGV in os::PlatformEvent::unpark() in JvmtiRawMonitor::raw_exit while posting method exit event
dholmes
parents:
51702
diff
changeset
|
163 |
// Once we set TState to TS_RUN the waiting thread can complete |
8592226f5cd3
8221584: SIGSEGV in os::PlatformEvent::unpark() in JvmtiRawMonitor::raw_exit while posting method exit event
dholmes
parents:
51702
diff
changeset
|
164 |
// SimpleEnter and 'w' is pointing into random stack space. So we have |
8592226f5cd3
8221584: SIGSEGV in os::PlatformEvent::unpark() in JvmtiRawMonitor::raw_exit while posting method exit event
dholmes
parents:
51702
diff
changeset
|
165 |
// to ensure we extract the ParkEvent (which is in type-stable memory) |
8592226f5cd3
8221584: SIGSEGV in os::PlatformEvent::unpark() in JvmtiRawMonitor::raw_exit while posting method exit event
dholmes
parents:
51702
diff
changeset
|
166 |
// before we set the state, and then don't access 'w'. |
6975 | 167 |
ParkEvent * ev = w->_event ; |
54469
8592226f5cd3
8221584: SIGSEGV in os::PlatformEvent::unpark() in JvmtiRawMonitor::raw_exit while posting method exit event
dholmes
parents:
51702
diff
changeset
|
168 |
OrderAccess::loadstore(); |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
169 |
w->TState = QNode::TS_RUN ; |
6975 | 170 |
OrderAccess::fence() ; |
171 |
ev->unpark() ; |
|
172 |
} |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
173 |
return ; |
6975 | 174 |
} |
175 |
||
176 |
int JvmtiRawMonitor::SimpleWait (Thread * Self, jlong millis) { |
|
177 |
guarantee (_owner == Self , "invariant") ; |
|
178 |
guarantee (_recursions == 0, "invariant") ; |
|
179 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
180 |
QNode Node (Self) ; |
6975 | 181 |
Node._notified = 0 ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
182 |
Node.TState = QNode::TS_WAIT ; |
6975 | 183 |
|
184 |
RawMonitor_lock->lock_without_safepoint_check() ; |
|
185 |
Node._next = _WaitSet ; |
|
186 |
_WaitSet = &Node ; |
|
187 |
RawMonitor_lock->unlock() ; |
|
188 |
||
189 |
SimpleExit (Self) ; |
|
190 |
guarantee (_owner != Self, "invariant") ; |
|
191 |
||
192 |
int ret = OS_OK ; |
|
193 |
if (millis <= 0) { |
|
194 |
Self->_ParkEvent->park(); |
|
195 |
} else { |
|
196 |
ret = Self->_ParkEvent->park(millis); |
|
197 |
} |
|
198 |
||
199 |
// If thread still resides on the waitset then unlink it. |
|
200 |
// Double-checked locking -- the usage is safe in this context |
|
54469
8592226f5cd3
8221584: SIGSEGV in os::PlatformEvent::unpark() in JvmtiRawMonitor::raw_exit while posting method exit event
dholmes
parents:
51702
diff
changeset
|
201 |
// as TState is volatile and the lock-unlock operators are |
6975 | 202 |
// serializing (barrier-equivalent). |
203 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
204 |
if (Node.TState == QNode::TS_WAIT) { |
6975 | 205 |
RawMonitor_lock->lock_without_safepoint_check() ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
206 |
if (Node.TState == QNode::TS_WAIT) { |
6975 | 207 |
// Simple O(n) unlink, but performance isn't critical here. |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
208 |
QNode * p ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
209 |
QNode * q = NULL ; |
6975 | 210 |
for (p = _WaitSet ; p != &Node; p = p->_next) { |
211 |
q = p ; |
|
212 |
} |
|
213 |
guarantee (p == &Node, "invariant") ; |
|
214 |
if (q == NULL) { |
|
215 |
guarantee (p == _WaitSet, "invariant") ; |
|
216 |
_WaitSet = p->_next ; |
|
217 |
} else { |
|
218 |
guarantee (p == q->_next, "invariant") ; |
|
219 |
q->_next = p->_next ; |
|
220 |
} |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
221 |
Node.TState = QNode::TS_RUN ; |
6975 | 222 |
} |
223 |
RawMonitor_lock->unlock() ; |
|
224 |
} |
|
225 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
226 |
guarantee (Node.TState == QNode::TS_RUN, "invariant") ; |
6975 | 227 |
SimpleEnter (Self) ; |
228 |
||
229 |
guarantee (_owner == Self, "invariant") ; |
|
230 |
guarantee (_recursions == 0, "invariant") ; |
|
231 |
return ret ; |
|
232 |
} |
|
233 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
234 |
void JvmtiRawMonitor::SimpleNotify (Thread * Self, bool All) { |
6975 | 235 |
guarantee (_owner == Self, "invariant") ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
236 |
if (_WaitSet == NULL) return ; |
6975 | 237 |
|
238 |
// We have two options: |
|
239 |
// A. Transfer the threads from the WaitSet to the EntryList |
|
240 |
// B. Remove the thread from the WaitSet and unpark() it. |
|
241 |
// |
|
242 |
// We use (B), which is crude and results in lots of futile |
|
243 |
// context switching. In particular (B) induces lots of contention. |
|
244 |
||
245 |
ParkEvent * ev = NULL ; // consider using a small auto array ... |
|
246 |
RawMonitor_lock->lock_without_safepoint_check() ; |
|
247 |
for (;;) { |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
248 |
QNode * w = _WaitSet ; |
6975 | 249 |
if (w == NULL) break ; |
250 |
_WaitSet = w->_next ; |
|
251 |
if (ev != NULL) { ev->unpark(); ev = NULL; } |
|
252 |
ev = w->_event ; |
|
253 |
OrderAccess::loadstore() ; |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
254 |
w->TState = QNode::TS_RUN ; |
6975 | 255 |
OrderAccess::storeload(); |
256 |
if (!All) break ; |
|
257 |
} |
|
258 |
RawMonitor_lock->unlock() ; |
|
259 |
if (ev != NULL) ev->unpark(); |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
260 |
return ; |
6975 | 261 |
} |
262 |
||
263 |
// Any JavaThread will enter here with state _thread_blocked |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
264 |
void JvmtiRawMonitor::raw_enter(Thread * Self) { |
6975 | 265 |
void * Contended ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
266 |
JavaThread * jt = NULL; |
6975 | 267 |
// don't enter raw monitor if thread is being externally suspended, it will |
268 |
// surprise the suspender if a "suspended" thread can still enter monitor |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
269 |
if (Self->is_Java_thread()) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
270 |
jt = (JavaThread*) Self; |
6975 | 271 |
jt->SR_lock()->lock_without_safepoint_check(); |
272 |
while (jt->is_external_suspend()) { |
|
273 |
jt->SR_lock()->unlock(); |
|
274 |
jt->java_suspend_self(); |
|
275 |
jt->SR_lock()->lock_without_safepoint_check(); |
|
276 |
} |
|
277 |
// guarded by SR_lock to avoid racing with new external suspend requests. |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
278 |
Contended = Atomic::cmpxchg(jt, &_owner, (Thread*)NULL); |
6975 | 279 |
jt->SR_lock()->unlock(); |
280 |
} else { |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
281 |
Contended = Atomic::cmpxchg(Self, &_owner, (Thread*)NULL); |
6975 | 282 |
} |
283 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
284 |
if (Contended == Self) { |
6975 | 285 |
_recursions ++ ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
286 |
return ; |
6975 | 287 |
} |
288 |
||
289 |
if (Contended == NULL) { |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
290 |
guarantee (_owner == Self, "invariant") ; |
6975 | 291 |
guarantee (_recursions == 0, "invariant") ; |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
292 |
return ; |
6975 | 293 |
} |
294 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
295 |
Self->set_current_pending_raw_monitor(this); |
6975 | 296 |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
297 |
if (!Self->is_Java_thread()) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
298 |
SimpleEnter (Self) ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
299 |
} else { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
300 |
guarantee (jt->thread_state() == _thread_blocked, "invariant") ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
301 |
for (;;) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
302 |
jt->set_suspend_equivalent(); |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
303 |
// cleared by handle_special_suspend_equivalent_condition() or |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
304 |
// java_suspend_self() |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
305 |
SimpleEnter (jt) ; |
6975 | 306 |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
307 |
// were we externally suspended while we were waiting? |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
308 |
if (!jt->handle_special_suspend_equivalent_condition()) break ; |
6975 | 309 |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
310 |
// This thread was externally suspended |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
311 |
// We have reentered the contended monitor, but while we were |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
312 |
// waiting another thread suspended us. We don't want to reenter |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
313 |
// the monitor while suspended because that would surprise the |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
314 |
// thread that suspended us. |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
315 |
// |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
316 |
// Drop the lock |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
317 |
SimpleExit (jt) ; |
6975 | 318 |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
319 |
jt->java_suspend_self(); |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
320 |
} |
6975 | 321 |
} |
322 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
323 |
Self->set_current_pending_raw_monitor(NULL); |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
324 |
|
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
325 |
guarantee (_owner == Self, "invariant") ; |
6975 | 326 |
guarantee (_recursions == 0, "invariant") ; |
327 |
} |
|
328 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
329 |
int JvmtiRawMonitor::raw_exit(Thread * Self) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
330 |
if (Self != _owner) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
331 |
return M_ILLEGAL_MONITOR_STATE; |
6975 | 332 |
} |
333 |
if (_recursions > 0) { |
|
334 |
--_recursions ; |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
335 |
} else { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
336 |
SimpleExit (Self) ; |
6975 | 337 |
} |
338 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
339 |
return M_OK; |
6975 | 340 |
} |
341 |
||
342 |
// All JavaThreads will enter here with state _thread_blocked |
|
343 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
344 |
int JvmtiRawMonitor::raw_wait(jlong millis, bool interruptible, Thread * Self) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
345 |
if (Self != _owner) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
346 |
return M_ILLEGAL_MONITOR_STATE; |
6975 | 347 |
} |
348 |
||
349 |
// To avoid spurious wakeups we reset the parkevent -- This is strictly optional. |
|
350 |
// The caller must be able to tolerate spurious returns from raw_wait(). |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
351 |
Self->_ParkEvent->reset() ; |
6975 | 352 |
OrderAccess::fence() ; |
353 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
354 |
JavaThread * jt = NULL; |
6975 | 355 |
// check interrupt event |
58196
cea6839598e8
8230424: Use platform independent code for Thread.interrupt support
dholmes
parents:
54469
diff
changeset
|
356 |
if (interruptible) { |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
357 |
assert(Self->is_Java_thread(), "Only JavaThreads can be interruptible"); |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
358 |
jt = (JavaThread*) Self; |
58196
cea6839598e8
8230424: Use platform independent code for Thread.interrupt support
dholmes
parents:
54469
diff
changeset
|
359 |
if (jt->is_interrupted(true)) { |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
360 |
return M_INTERRUPTED; |
58196
cea6839598e8
8230424: Use platform independent code for Thread.interrupt support
dholmes
parents:
54469
diff
changeset
|
361 |
} |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
362 |
} else { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
363 |
assert(!Self->is_Java_thread(), "JavaThreads must be interuptible"); |
6975 | 364 |
} |
365 |
||
366 |
intptr_t save = _recursions ; |
|
367 |
_recursions = 0 ; |
|
368 |
_waiters ++ ; |
|
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
369 |
if (Self->is_Java_thread()) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
370 |
guarantee (jt->thread_state() == _thread_blocked, "invariant") ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
371 |
jt->set_suspend_equivalent(); |
6975 | 372 |
} |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
373 |
int rv = SimpleWait (Self, millis) ; |
6975 | 374 |
_recursions = save ; |
375 |
_waiters -- ; |
|
376 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
377 |
guarantee (Self == _owner, "invariant") ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
378 |
if (Self->is_Java_thread()) { |
6975 | 379 |
for (;;) { |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
380 |
if (!jt->handle_special_suspend_equivalent_condition()) break ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
381 |
SimpleExit (jt) ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
382 |
jt->java_suspend_self(); |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
383 |
SimpleEnter (jt) ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
384 |
jt->set_suspend_equivalent() ; |
6975 | 385 |
} |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
386 |
guarantee (jt == _owner, "invariant") ; |
6975 | 387 |
} |
388 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
389 |
if (interruptible && jt->is_interrupted(true)) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
390 |
return M_INTERRUPTED; |
6975 | 391 |
} |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
392 |
|
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
393 |
return M_OK ; |
6975 | 394 |
} |
395 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
396 |
int JvmtiRawMonitor::raw_notify(Thread * Self) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
397 |
if (Self != _owner) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
398 |
return M_ILLEGAL_MONITOR_STATE; |
6975 | 399 |
} |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
400 |
SimpleNotify (Self, false) ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
401 |
return M_OK; |
6975 | 402 |
} |
403 |
||
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
404 |
int JvmtiRawMonitor::raw_notifyAll(Thread * Self) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
405 |
if (Self != _owner) { |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
406 |
return M_ILLEGAL_MONITOR_STATE; |
6975 | 407 |
} |
58488
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
408 |
SimpleNotify (Self, true) ; |
165b193b30dd
8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents:
58196
diff
changeset
|
409 |
return M_OK; |
6975 | 410 |
} |