author | chegar |
Thu, 17 Oct 2019 20:53:35 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58678 | 9cf78a70fa4f |
parent 54469 | 8592226f5cd3 |
child 58679 | 9c3209ff7550 |
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 |
|
58678 | 33 |
JvmtiRawMonitor::QNode::QNode(Thread* thread) : _next(NULL), _prev(NULL), |
34 |
_event(thread->_ParkEvent), |
|
35 |
_notified(0), _t_state(TS_RUN) { |
|
36 |
} |
|
37 |
||
38 |
GrowableArray<JvmtiRawMonitor*>* JvmtiPendingMonitors::_monitors = |
|
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), |
|
58678 | 43 |
"Java thread has not been created yet or more than one java thread " |
44 |
"is running. Raw monitor transition will not work"); |
|
45 |
JavaThread* current_java_thread = JavaThread::current(); |
|
6975 | 46 |
assert(current_java_thread->thread_state() == _thread_in_vm, "Must be in vm"); |
58678 | 47 |
for (int i = 0; i < count(); i++) { |
48 |
JvmtiRawMonitor* rmonitor = monitors()->at(i); |
|
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 |
||
58678 | 59 |
JvmtiRawMonitor::JvmtiRawMonitor(const char* name) : _owner(NULL), |
60 |
_recursions(0), |
|
61 |
_entry_list(NULL), |
|
62 |
_wait_set(NULL), |
|
63 |
_waiters(0), |
|
64 |
_magic(JVMTI_RM_MAGIC), |
|
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 |
// ------------------------------------------------------------------------- |
|
58678 | 108 |
// The JVMTI raw monitor subsystem is entirely distinct from normal |
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 |
|
58678 | 112 |
// the raw-monitor implementation on the existing Java ObjectMonitor mechanism. |
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 |
|
58678 | 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 |
||
58678 | 122 |
void JvmtiRawMonitor::simple_enter(Thread* self) { |
6975 | 123 |
for (;;) { |
58678 | 124 |
if (Atomic::replace_if_null(self, &_owner)) { |
125 |
return; |
|
6975 | 126 |
} |
127 |
||
58678 | 128 |
QNode node(self); |
129 |
self->_ParkEvent->reset(); // strictly optional |
|
130 |
node._t_state = QNode::TS_ENTER; |
|
6975 | 131 |
|
58678 | 132 |
RawMonitor_lock->lock_without_safepoint_check(); |
133 |
node._next = _entry_list; |
|
134 |
_entry_list = &node; |
|
135 |
OrderAccess::fence(); |
|
136 |
if (_owner == NULL && Atomic::replace_if_null(self, &_owner)) { |
|
137 |
_entry_list = node._next; |
|
138 |
RawMonitor_lock->unlock(); |
|
139 |
return; |
|
6975 | 140 |
} |
58678 | 141 |
RawMonitor_lock->unlock(); |
142 |
while (node._t_state == QNode::TS_ENTER) { |
|
143 |
self->_ParkEvent->park(); |
|
6975 | 144 |
} |
145 |
} |
|
146 |
} |
|
147 |
||
58678 | 148 |
void JvmtiRawMonitor::simple_exit(Thread* self) { |
149 |
guarantee(_owner == self, "invariant"); |
|
150 |
OrderAccess::release_store(&_owner, (Thread*)NULL); |
|
151 |
OrderAccess::fence(); |
|
152 |
if (_entry_list == NULL) { |
|
153 |
return; |
|
154 |
} |
|
6975 | 155 |
|
58678 | 156 |
RawMonitor_lock->lock_without_safepoint_check(); |
157 |
QNode* w = _entry_list; |
|
6975 | 158 |
if (w != NULL) { |
58678 | 159 |
_entry_list = w->_next; |
6975 | 160 |
} |
58678 | 161 |
RawMonitor_lock->unlock(); |
6975 | 162 |
if (w != NULL) { |
58678 | 163 |
guarantee(w ->_t_state == QNode::TS_ENTER, "invariant"); |
164 |
// Once we set _t_state to TS_RUN the waiting thread can complete |
|
165 |
// simple_enter and 'w' is pointing into random stack space. So we have |
|
166 |
// to ensure we extract the ParkEvent (which is in type-stable memory) |
|
167 |
// before we set the state, and then don't access 'w'. |
|
168 |
ParkEvent* ev = w->_event; |
|
169 |
OrderAccess::loadstore(); |
|
170 |
w->_t_state = QNode::TS_RUN; |
|
171 |
OrderAccess::fence(); |
|
172 |
ev->unpark(); |
|
6975 | 173 |
} |
58678 | 174 |
return; |
6975 | 175 |
} |
176 |
||
58678 | 177 |
int JvmtiRawMonitor::simple_wait(Thread* self, jlong millis) { |
178 |
guarantee(_owner == self , "invariant"); |
|
179 |
guarantee(_recursions == 0, "invariant"); |
|
6975 | 180 |
|
58678 | 181 |
QNode node(self); |
182 |
node._notified = 0; |
|
183 |
node._t_state = QNode::TS_WAIT; |
|
6975 | 184 |
|
58678 | 185 |
RawMonitor_lock->lock_without_safepoint_check(); |
186 |
node._next = _wait_set; |
|
187 |
_wait_set = &node; |
|
188 |
RawMonitor_lock->unlock(); |
|
6975 | 189 |
|
58678 | 190 |
simple_exit(self); |
191 |
guarantee(_owner != self, "invariant"); |
|
6975 | 192 |
|
58678 | 193 |
int ret = OS_OK; |
6975 | 194 |
if (millis <= 0) { |
58678 | 195 |
self->_ParkEvent->park(); |
6975 | 196 |
} else { |
58678 | 197 |
ret = self->_ParkEvent->park(millis); |
6975 | 198 |
} |
199 |
||
200 |
// If thread still resides on the waitset then unlink it. |
|
201 |
// Double-checked locking -- the usage is safe in this context |
|
58678 | 202 |
// as _t_state is volatile and the lock-unlock operators are |
6975 | 203 |
// serializing (barrier-equivalent). |
204 |
||
58678 | 205 |
if (node._t_state == QNode::TS_WAIT) { |
206 |
RawMonitor_lock->lock_without_safepoint_check(); |
|
207 |
if (node._t_state == QNode::TS_WAIT) { |
|
6975 | 208 |
// Simple O(n) unlink, but performance isn't critical here. |
58678 | 209 |
QNode* p; |
210 |
QNode* q = NULL; |
|
211 |
for (p = _wait_set; p != &node; p = p->_next) { |
|
212 |
q = p; |
|
6975 | 213 |
} |
58678 | 214 |
guarantee(p == &node, "invariant"); |
6975 | 215 |
if (q == NULL) { |
58678 | 216 |
guarantee (p == _wait_set, "invariant"); |
217 |
_wait_set = p->_next; |
|
6975 | 218 |
} else { |
58678 | 219 |
guarantee(p == q->_next, "invariant"); |
220 |
q->_next = p->_next; |
|
6975 | 221 |
} |
58678 | 222 |
node._t_state = QNode::TS_RUN; |
6975 | 223 |
} |
58678 | 224 |
RawMonitor_lock->unlock(); |
6975 | 225 |
} |
226 |
||
58678 | 227 |
guarantee(node._t_state == QNode::TS_RUN, "invariant"); |
228 |
simple_enter(self); |
|
6975 | 229 |
|
58678 | 230 |
guarantee(_owner == self, "invariant"); |
231 |
guarantee(_recursions == 0, "invariant"); |
|
232 |
return ret; |
|
6975 | 233 |
} |
234 |
||
58678 | 235 |
void JvmtiRawMonitor::simple_notify(Thread* self, bool all) { |
236 |
guarantee(_owner == self, "invariant"); |
|
237 |
if (_wait_set == NULL) { |
|
238 |
return; |
|
239 |
} |
|
6975 | 240 |
|
241 |
// We have two options: |
|
58678 | 242 |
// A. Transfer the threads from the _wait_set to the _entry_list |
243 |
// B. Remove the thread from the _wait_set and unpark() it. |
|
6975 | 244 |
// |
245 |
// We use (B), which is crude and results in lots of futile |
|
246 |
// context switching. In particular (B) induces lots of contention. |
|
247 |
||
58678 | 248 |
ParkEvent* ev = NULL; // consider using a small auto array ... |
249 |
RawMonitor_lock->lock_without_safepoint_check(); |
|
6975 | 250 |
for (;;) { |
58678 | 251 |
QNode* w = _wait_set; |
252 |
if (w == NULL) break; |
|
253 |
_wait_set = w->_next; |
|
254 |
if (ev != NULL) { |
|
255 |
ev->unpark(); |
|
256 |
ev = NULL; |
|
257 |
} |
|
258 |
ev = w->_event; |
|
259 |
OrderAccess::loadstore(); |
|
260 |
w->_t_state = QNode::TS_RUN; |
|
261 |
OrderAccess::storeload(); |
|
262 |
if (!all) { |
|
263 |
break; |
|
264 |
} |
|
6975 | 265 |
} |
58678 | 266 |
RawMonitor_lock->unlock(); |
267 |
if (ev != NULL) { |
|
268 |
ev->unpark(); |
|
269 |
} |
|
270 |
return; |
|
6975 | 271 |
} |
272 |
||
273 |
// Any JavaThread will enter here with state _thread_blocked |
|
58678 | 274 |
void JvmtiRawMonitor::raw_enter(Thread* self) { |
275 |
void* contended; |
|
276 |
JavaThread* jt = NULL; |
|
6975 | 277 |
// don't enter raw monitor if thread is being externally suspended, it will |
278 |
// surprise the suspender if a "suspended" thread can still enter monitor |
|
58678 | 279 |
if (self->is_Java_thread()) { |
280 |
jt = (JavaThread*)self; |
|
6975 | 281 |
jt->SR_lock()->lock_without_safepoint_check(); |
282 |
while (jt->is_external_suspend()) { |
|
283 |
jt->SR_lock()->unlock(); |
|
284 |
jt->java_suspend_self(); |
|
285 |
jt->SR_lock()->lock_without_safepoint_check(); |
|
286 |
} |
|
287 |
// guarded by SR_lock to avoid racing with new external suspend requests. |
|
58678 | 288 |
contended = Atomic::cmpxchg(jt, &_owner, (Thread*)NULL); |
6975 | 289 |
jt->SR_lock()->unlock(); |
290 |
} else { |
|
58678 | 291 |
contended = Atomic::cmpxchg(self, &_owner, (Thread*)NULL); |
6975 | 292 |
} |
293 |
||
58678 | 294 |
if (contended == self) { |
295 |
_recursions++; |
|
296 |
return; |
|
6975 | 297 |
} |
298 |
||
58678 | 299 |
if (contended == NULL) { |
300 |
guarantee(_owner == self, "invariant"); |
|
301 |
guarantee(_recursions == 0, "invariant"); |
|
302 |
return; |
|
6975 | 303 |
} |
304 |
||
58678 | 305 |
self->set_current_pending_raw_monitor(this); |
6975 | 306 |
|
58678 | 307 |
if (!self->is_Java_thread()) { |
308 |
simple_enter(self); |
|
309 |
} else { |
|
310 |
guarantee(jt->thread_state() == _thread_blocked, "invariant"); |
|
311 |
for (;;) { |
|
312 |
jt->set_suspend_equivalent(); |
|
313 |
// cleared by handle_special_suspend_equivalent_condition() or |
|
314 |
// java_suspend_self() |
|
315 |
simple_enter(jt); |
|
6975 | 316 |
|
58678 | 317 |
// were we externally suspended while we were waiting? |
318 |
if (!jt->handle_special_suspend_equivalent_condition()) { |
|
319 |
break; |
|
320 |
} |
|
6975 | 321 |
|
58678 | 322 |
// This thread was externally suspended |
323 |
// We have reentered the contended monitor, but while we were |
|
324 |
// waiting another thread suspended us. We don't want to reenter |
|
325 |
// the monitor while suspended because that would surprise the |
|
326 |
// thread that suspended us. |
|
327 |
// |
|
328 |
// Drop the lock |
|
329 |
simple_exit(jt); |
|
6975 | 330 |
|
58678 | 331 |
jt->java_suspend_self(); |
332 |
} |
|
6975 | 333 |
} |
334 |
||
58678 | 335 |
self->set_current_pending_raw_monitor(NULL); |
336 |
||
337 |
guarantee(_owner == self, "invariant"); |
|
338 |
guarantee(_recursions == 0, "invariant"); |
|
6975 | 339 |
} |
340 |
||
58678 | 341 |
int JvmtiRawMonitor::raw_exit(Thread* self) { |
342 |
if (self != _owner) { |
|
343 |
return M_ILLEGAL_MONITOR_STATE; |
|
6975 | 344 |
} |
345 |
if (_recursions > 0) { |
|
58678 | 346 |
_recursions--; |
347 |
} else { |
|
348 |
simple_exit(self); |
|
6975 | 349 |
} |
350 |
||
58678 | 351 |
return M_OK; |
6975 | 352 |
} |
353 |
||
354 |
// All JavaThreads will enter here with state _thread_blocked |
|
355 |
||
58678 | 356 |
int JvmtiRawMonitor::raw_wait(jlong millis, bool interruptible, Thread* self) { |
357 |
if (self != _owner) { |
|
358 |
return M_ILLEGAL_MONITOR_STATE; |
|
6975 | 359 |
} |
360 |
||
58678 | 361 |
// To avoid spurious wakeups we reset the parkevent. This is strictly optional. |
6975 | 362 |
// The caller must be able to tolerate spurious returns from raw_wait(). |
58678 | 363 |
self->_ParkEvent->reset(); |
364 |
OrderAccess::fence(); |
|
6975 | 365 |
|
58678 | 366 |
JavaThread* jt = NULL; |
6975 | 367 |
// check interrupt event |
58678 | 368 |
if (interruptible) { |
369 |
assert(self->is_Java_thread(), "Only JavaThreads can be interruptible"); |
|
370 |
jt = (JavaThread*)self; |
|
371 |
if (jt->is_interrupted(true)) { |
|
372 |
return M_INTERRUPTED; |
|
373 |
} |
|
374 |
} else { |
|
375 |
assert(!self->is_Java_thread(), "JavaThreads must be interuptible"); |
|
6975 | 376 |
} |
377 |
||
58678 | 378 |
intptr_t save = _recursions; |
379 |
_recursions = 0; |
|
380 |
_waiters++; |
|
381 |
if (self->is_Java_thread()) { |
|
382 |
guarantee(jt->thread_state() == _thread_blocked, "invariant"); |
|
383 |
jt->set_suspend_equivalent(); |
|
6975 | 384 |
} |
58678 | 385 |
int rv = simple_wait(self, millis); |
386 |
_recursions = save; |
|
387 |
_waiters--; |
|
6975 | 388 |
|
58678 | 389 |
guarantee(self == _owner, "invariant"); |
390 |
if (self->is_Java_thread()) { |
|
391 |
for (;;) { |
|
392 |
if (!jt->handle_special_suspend_equivalent_condition()) { |
|
393 |
break; |
|
394 |
} |
|
395 |
simple_exit(jt); |
|
396 |
jt->java_suspend_self(); |
|
397 |
simple_enter(jt); |
|
398 |
jt->set_suspend_equivalent(); |
|
399 |
} |
|
400 |
guarantee(jt == _owner, "invariant"); |
|
6975 | 401 |
} |
402 |
||
58678 | 403 |
if (interruptible && jt->is_interrupted(true)) { |
404 |
return M_INTERRUPTED; |
|
6975 | 405 |
} |
58678 | 406 |
|
407 |
return M_OK; |
|
6975 | 408 |
} |
409 |
||
58678 | 410 |
int JvmtiRawMonitor::raw_notify(Thread* self) { |
411 |
if (self != _owner) { |
|
412 |
return M_ILLEGAL_MONITOR_STATE; |
|
6975 | 413 |
} |
58678 | 414 |
simple_notify(self, false); |
415 |
return M_OK; |
|
6975 | 416 |
} |
417 |
||
58678 | 418 |
int JvmtiRawMonitor::raw_notifyAll(Thread* self) { |
419 |
if (self != _owner) { |
|
420 |
return M_ILLEGAL_MONITOR_STATE; |
|
6975 | 421 |
} |
58678 | 422 |
simple_notify(self, true); |
423 |
return M_OK; |
|
6975 | 424 |
} |