2
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
25
#include "precompiled.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "prims/jvmtiRawMonitor.hpp"
28
#include "runtime/atomic.hpp"
29
#include "runtime/interfaceSupport.inline.hpp"
30
#include "runtime/javaThread.hpp"
31
#include "runtime/orderAccess.hpp"
32
#include "runtime/threads.hpp"
34
JvmtiRawMonitor::QNode::QNode(Thread* thread) : _next(nullptr), _prev(nullptr),
35
_event(thread->_ParkEvent),
36
_notified(0), _t_state(TS_RUN) {
39
GrowableArray<JvmtiRawMonitor*>* JvmtiPendingMonitors::_monitors =
40
new (mtServiceability) GrowableArray<JvmtiRawMonitor*>(1, mtServiceability);
42
void JvmtiPendingMonitors::transition_raw_monitors() {
43
assert((Threads::number_of_threads()==1),
44
"Java thread has not been created yet or more than one java thread "
45
"is running. Raw monitor transition will not work");
46
JavaThread* current_java_thread = JavaThread::current();
48
ThreadToNativeFromVM ttnfvm(current_java_thread);
49
for (int i = 0; i < count(); i++) {
50
JvmtiRawMonitor* rmonitor = monitors()->at(i);
51
rmonitor->raw_enter(current_java_thread);
54
// pending monitors are converted to real monitor so delete them all.
59
// class JvmtiRawMonitor
62
JvmtiRawMonitor::JvmtiRawMonitor(const char* name) : _owner(nullptr),
66
_magic(JVMTI_RM_MAGIC),
69
_name = strcpy(NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtInternal), name);
73
JvmtiRawMonitor::~JvmtiRawMonitor() {
82
JvmtiRawMonitor::is_valid() {
85
// This object might not be a JvmtiRawMonitor so we can't assume
86
// the _magic field is properly aligned. Get the value in a safe
87
// way and then check against JVMTI_RM_MAGIC.
89
switch (sizeof(_magic)) {
91
value = Bytes::get_native_u2((address)&_magic);
95
value = Bytes::get_native_u4((address)&_magic);
99
value = Bytes::get_native_u8((address)&_magic);
103
guarantee(false, "_magic field is an unexpected size");
106
return value == JVMTI_RM_MAGIC;
109
// -------------------------------------------------------------------------
110
// The JVMTI raw monitor subsystem is entirely distinct from normal
111
// java-synchronization or jni-synchronization. JVMTI raw monitors are not
112
// associated with objects. They can be implemented in any manner
113
// that makes sense. The original implementors decided to piggy-back
114
// the raw-monitor implementation on the existing Java ObjectMonitor mechanism.
115
// Now we just use a simplified form of that ObjectMonitor code.
117
// Note that we use the single RawMonitor_lock to protect queue operations for
118
// _all_ raw monitors. This is a scalability impediment, but since raw monitor usage
119
// is fairly rare, this is not of concern. The RawMonitor_lock can not
120
// be held indefinitely. The critical sections must be short and bounded.
122
// -------------------------------------------------------------------------
124
void JvmtiRawMonitor::simple_enter(Thread* self) {
126
if (Atomic::replace_if_null(&_owner, self)) {
127
if (self->is_Java_thread()) {
128
Continuation::pin(JavaThread::cast(self));
134
self->_ParkEvent->reset(); // strictly optional
135
node._t_state = QNode::TS_ENTER;
137
RawMonitor_lock->lock_without_safepoint_check();
138
node._next = _entry_list;
140
OrderAccess::fence();
141
if (_owner == nullptr && Atomic::replace_if_null(&_owner, self)) {
142
_entry_list = node._next;
143
RawMonitor_lock->unlock();
144
if (self->is_Java_thread()) {
145
Continuation::pin(JavaThread::cast(self));
149
RawMonitor_lock->unlock();
150
while (node._t_state == QNode::TS_ENTER) {
151
self->_ParkEvent->park();
156
void JvmtiRawMonitor::simple_exit(Thread* self) {
157
guarantee(_owner == self, "invariant");
158
Atomic::release_store(&_owner, (Thread*)nullptr);
159
OrderAccess::fence();
160
if (self->is_Java_thread()) {
161
Continuation::unpin(JavaThread::cast(self));
163
if (_entry_list == nullptr) {
167
RawMonitor_lock->lock_without_safepoint_check();
168
QNode* w = _entry_list;
170
_entry_list = w->_next;
172
RawMonitor_lock->unlock();
174
guarantee(w ->_t_state == QNode::TS_ENTER, "invariant");
175
// Once we set _t_state to TS_RUN the waiting thread can complete
176
// simple_enter and 'w' is pointing into random stack space. So we have
177
// to ensure we extract the ParkEvent (which is in type-stable memory)
178
// before we set the state, and then don't access 'w'.
179
ParkEvent* ev = w->_event;
180
OrderAccess::loadstore();
181
w->_t_state = QNode::TS_RUN;
182
OrderAccess::fence();
188
inline void JvmtiRawMonitor::enqueue_waiter(QNode& node) {
190
node._t_state = QNode::TS_WAIT;
191
RawMonitor_lock->lock_without_safepoint_check();
192
node._next = _wait_set;
194
RawMonitor_lock->unlock();
197
inline void JvmtiRawMonitor::dequeue_waiter(QNode& node) {
198
// If thread still resides on the waitset then unlink it.
199
// Double-checked locking -- the usage is safe in this context
200
// as _t_state is volatile and the lock-unlock operators are
201
// serializing (barrier-equivalent).
203
if (node._t_state == QNode::TS_WAIT) {
204
RawMonitor_lock->lock_without_safepoint_check();
205
if (node._t_state == QNode::TS_WAIT) {
206
// Simple O(n) unlink, but performance isn't critical here.
209
for (p = _wait_set; p != &node; p = p->_next) {
212
guarantee(p == &node, "invariant");
214
guarantee (p == _wait_set, "invariant");
215
_wait_set = p->_next;
217
guarantee(p == q->_next, "invariant");
220
node._t_state = QNode::TS_RUN;
222
RawMonitor_lock->unlock();
225
guarantee(node._t_state == QNode::TS_RUN, "invariant");
228
// simple_wait is not quite so simple as we have to deal with the interaction
229
// with the Thread interrupt state, which resides in the java.lang.Thread object.
230
// That state must only be accessed while _thread_in_vm and requires proper thread-state
232
// Returns M_OK usually, but M_INTERRUPTED if the thread is a JavaThread and was
235
// - simple_wait never reenters the monitor.
236
// - A JavaThread must be in native.
237
int JvmtiRawMonitor::simple_wait(Thread* self, jlong millis) {
238
guarantee(_owner == self , "invariant");
239
guarantee(_recursions == 0, "invariant");
242
enqueue_waiter(node);
245
guarantee(_owner != self, "invariant");
248
if (self->is_Java_thread()) {
249
JavaThread* jt = JavaThread::cast(self);
250
guarantee(jt->thread_state() == _thread_in_native, "invariant");
252
// This transition must be after we exited the monitor.
253
ThreadInVMfromNative tivmfn(jt);
254
if (jt->get_and_clear_interrupted()) {
257
ThreadBlockInVM tbivm(jt);
259
self->_ParkEvent->park();
261
self->_ParkEvent->park(millis);
263
// Return to VM before post-check of interrupt state
265
if (jt->get_and_clear_interrupted()) {
271
self->_ParkEvent->park();
273
self->_ParkEvent->park(millis);
277
dequeue_waiter(node);
282
void JvmtiRawMonitor::simple_notify(Thread* self, bool all) {
283
guarantee(_owner == self, "invariant");
284
if (_wait_set == nullptr) {
288
// We have two options:
289
// A. Transfer the threads from the _wait_set to the _entry_list
290
// B. Remove the thread from the _wait_set and unpark() it.
292
// We use (B), which is crude and results in lots of futile
293
// context switching. In particular (B) induces lots of contention.
295
ParkEvent* ev = nullptr; // consider using a small auto array ...
296
RawMonitor_lock->lock_without_safepoint_check();
298
QNode* w = _wait_set;
299
if (w == nullptr) break;
300
_wait_set = w->_next;
306
OrderAccess::loadstore();
307
w->_t_state = QNode::TS_RUN;
308
OrderAccess::storeload();
313
RawMonitor_lock->unlock();
320
void JvmtiRawMonitor::ExitOnSuspend::operator()(JavaThread* current) {
321
// We must exit the monitor in case of a safepoint.
322
_rm->simple_exit(current);
326
// JavaThreads will enter here with state _thread_in_native.
327
void JvmtiRawMonitor::raw_enter(Thread* self) {
328
// TODO Atomic::load on _owner field
329
if (_owner == self) {
334
self->set_current_pending_raw_monitor(this);
336
if (!self->is_Java_thread()) {
339
JavaThread* jt = JavaThread::cast(self);
340
guarantee(jt->thread_state() == _thread_in_native, "invariant");
341
ThreadInVMfromNative tivmfn(jt);
343
ExitOnSuspend eos(this);
345
ThreadBlockInVMPreprocess<ExitOnSuspend> tbivmp(jt, eos, true /* allow_suspend */);
348
if (!eos.monitor_exited()) {
354
self->set_current_pending_raw_monitor(nullptr);
356
guarantee(_owner == self, "invariant");
357
guarantee(_recursions == 0, "invariant");
360
int JvmtiRawMonitor::raw_exit(Thread* self) {
361
if (self != _owner) {
362
return M_ILLEGAL_MONITOR_STATE;
364
if (_recursions > 0) {
373
int JvmtiRawMonitor::raw_wait(jlong millis, Thread* self) {
374
if (self != _owner) {
375
return M_ILLEGAL_MONITOR_STATE;
380
// To avoid spurious wakeups we reset the parkevent. This is strictly optional.
381
// The caller must be able to tolerate spurious returns from raw_wait().
382
self->_ParkEvent->reset();
383
OrderAccess::fence();
385
int save = _recursions;
387
ret = simple_wait(self, millis);
389
// Now we need to re-enter the monitor. For JavaThreads
390
// we need to manage suspend requests.
391
if (self->is_Java_thread()) { // JavaThread re-enter
392
JavaThread* jt = JavaThread::cast(self);
393
ThreadInVMfromNative tivmfn(jt);
395
ExitOnSuspend eos(this);
397
ThreadBlockInVMPreprocess<ExitOnSuspend> tbivmp(jt, eos, true /* allow_suspend */);
400
if (!eos.monitor_exited()) {
404
if (jt->get_and_clear_interrupted()) {
407
} else { // Non-JavaThread re-enter
408
assert(ret != M_INTERRUPTED, "Only JavaThreads can be interrupted");
414
guarantee(self == _owner, "invariant");
418
int JvmtiRawMonitor::raw_notify(Thread* self) {
419
if (self != _owner) {
420
return M_ILLEGAL_MONITOR_STATE;
422
simple_notify(self, false);
426
int JvmtiRawMonitor::raw_notifyAll(Thread* self) {
427
if (self != _owner) {
428
return M_ILLEGAL_MONITOR_STATE;
430
simple_notify(self, true);