Skip to content

Commit da18495

Browse files
committed
8230003: Make Monitor inherit from Mutex
Reverse inheritance that makes more sense. Reviewed-by: dholmes, rehn, pchilanomate
1 parent a405118 commit da18495

File tree

10 files changed

+140
-166
lines changed

10 files changed

+140
-166
lines changed

src/hotspot/share/compiler/compileBroker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2674,8 +2674,8 @@ void CompileBroker::print_heapinfo(outputStream* out, const char* function, size
26742674
// for the entire duration of aggregation and printing. That makes sure
26752675
// we see a consistent picture and do not run into issues caused by
26762676
// the CodeHeap being altered concurrently.
2677-
Monitor* global_lock = allFun ? CodeCache_lock : NULL;
2678-
Monitor* function_lock = allFun ? NULL : CodeCache_lock;
2677+
Mutex* global_lock = allFun ? CodeCache_lock : NULL;
2678+
Mutex* function_lock = allFun ? NULL : CodeCache_lock;
26792679
ts_global.update(); // record starting point
26802680
MutexLocker mu2(global_lock, Mutex::_no_safepoint_check_flag);
26812681
if (global_lock != NULL) {

src/hotspot/share/jfr/recorder/repository/jfrEmergencyDump.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,9 @@ static void prepare_for_emergency_dump(Thread* thread) {
356356
}
357357

358358
#ifdef ASSERT
359-
Monitor* owned_lock = thread->owned_locks();
359+
Mutex* owned_lock = thread->owned_locks();
360360
while (owned_lock != NULL) {
361-
Monitor* next = owned_lock->next();
361+
Mutex* next = owned_lock->next();
362362
owned_lock->unlock();
363363
owned_lock = next;
364364
}

src/hotspot/share/logging/logTag.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
LOG_TAG(mirror) \
172172
LOG_TAG(verification) \
173173
LOG_TAG(verify) \
174-
LOG_TAG(vmmonitor) \
174+
LOG_TAG(vmmutex) \
175175
LOG_TAG(vmoperation) \
176176
LOG_TAG(vmthread) \
177177
LOG_TAG(vtables) \

src/hotspot/share/runtime/interfaceSupport.inline.hpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -254,30 +254,30 @@ class ThreadBlockInVM : public ThreadStateTransition {
254254
};
255255

256256
// Unlike ThreadBlockInVM, this class is designed to avoid certain deadlock scenarios while making
257-
// transitions inside class Monitor in cases where we need to block for a safepoint or handshake. It
258-
// receives an extra argument compared to ThreadBlockInVM, the address of a pointer to the monitor we
259-
// are trying to acquire. This will be used to access and release the monitor if needed to avoid
257+
// transitions inside class Mutex in cases where we need to block for a safepoint or handshake. It
258+
// receives an extra argument compared to ThreadBlockInVM, the address of a pointer to the mutex we
259+
// are trying to acquire. This will be used to access and release the mutex if needed to avoid
260260
// said deadlocks.
261261
// It works like ThreadBlockInVM but differs from it in two ways:
262262
// - When transitioning in (constructor), it checks for safepoints without blocking, i.e., calls
263263
// back if needed to allow a pending safepoint to continue but does not block in it.
264264
// - When transitioning back (destructor), if there is a pending safepoint or handshake it releases
265-
// the monitor that is only partially acquired.
265+
// the mutex that is only partially acquired.
266266
class ThreadBlockInVMWithDeadlockCheck : public ThreadStateTransition {
267267
private:
268-
Monitor** _in_flight_monitor_adr;
269-
270-
void release_monitor() {
271-
assert(_in_flight_monitor_adr != NULL, "_in_flight_monitor_adr should have been set on constructor");
272-
Monitor* in_flight_monitor = *_in_flight_monitor_adr;
273-
if (in_flight_monitor != NULL) {
274-
in_flight_monitor->release_for_safepoint();
275-
*_in_flight_monitor_adr = NULL;
268+
Mutex** _in_flight_mutex_addr;
269+
270+
void release_mutex() {
271+
assert(_in_flight_mutex_addr != NULL, "_in_flight_mutex_addr should have been set on constructor");
272+
Mutex* in_flight_mutex = *_in_flight_mutex_addr;
273+
if (in_flight_mutex != NULL) {
274+
in_flight_mutex->release_for_safepoint();
275+
*_in_flight_mutex_addr = NULL;
276276
}
277277
}
278278
public:
279-
ThreadBlockInVMWithDeadlockCheck(JavaThread* thread, Monitor** in_flight_monitor_adr)
280-
: ThreadStateTransition(thread), _in_flight_monitor_adr(in_flight_monitor_adr) {
279+
ThreadBlockInVMWithDeadlockCheck(JavaThread* thread, Mutex** in_flight_mutex_addr)
280+
: ThreadStateTransition(thread), _in_flight_mutex_addr(in_flight_mutex_addr) {
281281
// Once we are blocked vm expects stack to be walkable
282282
thread->frame_anchor()->make_walkable(thread);
283283

@@ -293,7 +293,7 @@ class ThreadBlockInVMWithDeadlockCheck : public ThreadStateTransition {
293293
_thread->set_thread_state_fence((JavaThreadState)(_thread_blocked_trans));
294294

295295
if (SafepointMechanism::should_block(_thread)) {
296-
release_monitor();
296+
release_mutex();
297297
SafepointMechanism::block_if_requested(_thread);
298298
}
299299

src/hotspot/share/runtime/mutex.cpp

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@
3333
#include "utilities/macros.hpp"
3434

3535
#ifdef ASSERT
36-
void Monitor::check_safepoint_state(Thread* thread, bool do_safepoint_check) {
36+
void Mutex::check_safepoint_state(Thread* thread, bool do_safepoint_check) {
3737
// If the JavaThread checks for safepoint, verify that the lock wasn't created with safepoint_check_never.
38-
SafepointCheckRequired not_allowed = do_safepoint_check ? Monitor::_safepoint_check_never :
39-
Monitor::_safepoint_check_always;
38+
SafepointCheckRequired not_allowed = do_safepoint_check ? Mutex::_safepoint_check_never :
39+
Mutex::_safepoint_check_always;
4040
assert(!thread->is_active_Java_thread() || _safepoint_check_required != not_allowed,
4141
"This lock should %s have a safepoint check for Java threads: %s",
4242
_safepoint_check_required ? "always" : "never", name());
4343

4444
// If defined with safepoint_check_never, a NonJavaThread should never ask to safepoint check either.
45-
assert(thread->is_Java_thread() || !do_safepoint_check || _safepoint_check_required != Monitor::_safepoint_check_never,
45+
assert(thread->is_Java_thread() || !do_safepoint_check || _safepoint_check_required != Mutex::_safepoint_check_never,
4646
"NonJavaThread should not check for safepoint");
4747
}
4848
#endif // ASSERT
4949

50-
void Monitor::lock(Thread * self) {
50+
void Mutex::lock(Thread * self) {
5151
check_safepoint_state(self, true);
5252

5353
DEBUG_ONLY(check_prelock_state(self, true));
5454
assert(_owner != self, "invariant");
5555

56-
Monitor* in_flight_monitor = NULL;
56+
Mutex* in_flight_mutex = NULL;
5757
DEBUG_ONLY(int retry_cnt = 0;)
5858
bool is_active_Java_thread = self->is_active_Java_thread();
5959
while (!_lock.try_lock()) {
@@ -62,18 +62,18 @@ void Monitor::lock(Thread * self) {
6262
#ifdef ASSERT
6363
check_block_state(self);
6464
if (retry_cnt++ > 3) {
65-
log_trace(vmmonitor)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmonitor %s", p2i(self), retry_cnt, _name);
65+
log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
6666
}
6767
#endif // ASSERT
6868

6969
// Is it a JavaThread participating in the safepoint protocol.
7070
if (is_active_Java_thread) {
7171
assert(rank() > Mutex::special, "Potential deadlock with special or lesser rank mutex");
72-
{ ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_monitor);
73-
in_flight_monitor = this; // save for ~ThreadBlockInVMWithDeadlockCheck
72+
{ ThreadBlockInVMWithDeadlockCheck tbivmdc((JavaThread *) self, &in_flight_mutex);
73+
in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck
7474
_lock.lock();
7575
}
76-
if (in_flight_monitor != NULL) {
76+
if (in_flight_mutex != NULL) {
7777
// Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
7878
break;
7979
}
@@ -87,7 +87,7 @@ void Monitor::lock(Thread * self) {
8787
set_owner(self);
8888
}
8989

90-
void Monitor::lock() {
90+
void Mutex::lock() {
9191
this->lock(Thread::current());
9292
}
9393

@@ -97,22 +97,22 @@ void Monitor::lock() {
9797
// safepoint-safe and so will prevent a safepoint from being reached. If used
9898
// in the wrong way this can lead to a deadlock with the safepoint code.
9999

100-
void Monitor::lock_without_safepoint_check(Thread * self) {
100+
void Mutex::lock_without_safepoint_check(Thread * self) {
101101
check_safepoint_state(self, false);
102102
assert(_owner != self, "invariant");
103103
_lock.lock();
104104
assert_owner(NULL);
105105
set_owner(self);
106106
}
107107

108-
void Monitor::lock_without_safepoint_check() {
108+
void Mutex::lock_without_safepoint_check() {
109109
lock_without_safepoint_check(Thread::current());
110110
}
111111

112112

113113
// Returns true if thread succeeds in grabbing the lock, otherwise false.
114114

115-
bool Monitor::try_lock() {
115+
bool Mutex::try_lock() {
116116
Thread * const self = Thread::current();
117117
DEBUG_ONLY(check_prelock_state(self, false);)
118118

@@ -124,12 +124,12 @@ bool Monitor::try_lock() {
124124
return false;
125125
}
126126

127-
void Monitor::release_for_safepoint() {
127+
void Mutex::release_for_safepoint() {
128128
assert_owner(NULL);
129129
_lock.unlock();
130130
}
131131

132-
void Monitor::unlock() {
132+
void Mutex::unlock() {
133133
assert_owner(Thread::current());
134134
set_owner(NULL);
135135
_lock.unlock();
@@ -147,7 +147,7 @@ void Monitor::notify_all() {
147147

148148
#ifdef ASSERT
149149
void Monitor::assert_wait_lock_state(Thread* self) {
150-
Monitor* least = get_least_ranked_lock_besides_this(self->owned_locks());
150+
Mutex* least = get_least_ranked_lock_besides_this(self->owned_locks());
151151
assert(least != this, "Specification of get_least_... call above");
152152
if (least != NULL && least->rank() <= special) {
153153
::tty->print("Attempting to wait on monitor %s/%d while holding"
@@ -194,10 +194,10 @@ bool Monitor::wait(long timeout, bool as_suspend_equivalent) {
194194
// abdicating the lock in wait
195195
set_owner(NULL);
196196
JavaThread *jt = (JavaThread *)self;
197-
Monitor* in_flight_monitor = NULL;
197+
Mutex* in_flight_mutex = NULL;
198198

199199
{
200-
ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_monitor);
200+
ThreadBlockInVMWithDeadlockCheck tbivmdc(jt, &in_flight_mutex);
201201
OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */);
202202
if (as_suspend_equivalent) {
203203
jt->set_suspend_equivalent();
@@ -206,7 +206,7 @@ bool Monitor::wait(long timeout, bool as_suspend_equivalent) {
206206
}
207207

208208
wait_status = _lock.wait(timeout);
209-
in_flight_monitor = this; // save for ~ThreadBlockInVMWithDeadlockCheck
209+
in_flight_mutex = this; // save for ~ThreadBlockInVMWithDeadlockCheck
210210

211211
// were we externally suspended while we were waiting?
212212
if (as_suspend_equivalent && jt->handle_special_suspend_equivalent_condition()) {
@@ -220,7 +220,7 @@ bool Monitor::wait(long timeout, bool as_suspend_equivalent) {
220220
}
221221
}
222222

223-
if (in_flight_monitor != NULL) {
223+
if (in_flight_mutex != NULL) {
224224
// Not unlocked by ~ThreadBlockInVMWithDeadlockCheck
225225
assert_owner(NULL);
226226
// Conceptually reestablish ownership of the lock.
@@ -232,7 +232,7 @@ bool Monitor::wait(long timeout, bool as_suspend_equivalent) {
232232
return wait_status != 0; // return true IFF timeout
233233
}
234234

235-
Monitor::~Monitor() {
235+
Mutex::~Mutex() {
236236
assert_owner(NULL);
237237
}
238238

@@ -241,34 +241,34 @@ bool is_sometimes_ok(const char* name) {
241241
return (strcmp(name, "Threads_lock") == 0 || strcmp(name, "Heap_lock") == 0 || strcmp(name, "SR_lock") == 0);
242242
}
243243

244-
Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
245-
SafepointCheckRequired safepoint_check_required) : _owner(NULL) {
244+
Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
245+
SafepointCheckRequired safepoint_check_required) : _owner(NULL) {
246246
assert(os::mutex_init_done(), "Too early!");
247247
if (name == NULL) {
248248
strcpy(_name, "UNKNOWN");
249249
} else {
250-
strncpy(_name, name, MONITOR_NAME_LEN - 1);
251-
_name[MONITOR_NAME_LEN - 1] = '\0';
250+
strncpy(_name, name, MUTEX_NAME_LEN - 1);
251+
_name[MUTEX_NAME_LEN - 1] = '\0';
252252
}
253253
#ifdef ASSERT
254254
_allow_vm_block = allow_vm_block;
255255
_rank = Rank;
256256
_safepoint_check_required = safepoint_check_required;
257257

258-
assert(_safepoint_check_required != Monitor::_safepoint_check_sometimes || is_sometimes_ok(name),
258+
assert(_safepoint_check_required != Mutex::_safepoint_check_sometimes || is_sometimes_ok(name),
259259
"Lock has _safepoint_check_sometimes %s", name);
260260
#endif
261261
}
262262

263-
Mutex::Mutex(int Rank, const char * name, bool allow_vm_block,
263+
Monitor::Monitor(int Rank, const char * name, bool allow_vm_block,
264264
SafepointCheckRequired safepoint_check_required) :
265-
Monitor(Rank, name, allow_vm_block, safepoint_check_required) {}
265+
Mutex(Rank, name, allow_vm_block, safepoint_check_required) {}
266266

267-
bool Monitor::owned_by_self() const {
267+
bool Mutex::owned_by_self() const {
268268
return _owner == Thread::current();
269269
}
270270

271-
void Monitor::print_on_error(outputStream* st) const {
271+
void Mutex::print_on_error(outputStream* st) const {
272272
st->print("[" PTR_FORMAT, p2i(this));
273273
st->print("] %s", _name);
274274
st->print(" - owner thread: " PTR_FORMAT, p2i(_owner));
@@ -278,7 +278,7 @@ void Monitor::print_on_error(outputStream* st) const {
278278
// Non-product code
279279

280280
#ifndef PRODUCT
281-
void Monitor::print_on(outputStream* st) const {
281+
void Mutex::print_on(outputStream* st) const {
282282
st->print_cr("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT,
283283
p2i(this), _name, p2i(_owner));
284284
}
@@ -287,7 +287,7 @@ void Monitor::print_on(outputStream* st) const {
287287
#ifndef PRODUCT
288288
#ifdef ASSERT
289289

290-
void Monitor::assert_owner(Thread * expected) {
290+
void Mutex::assert_owner(Thread * expected) {
291291
const char* msg = "invalid owner";
292292
if (expected == NULL) {
293293
msg = "should be un-owned";
@@ -300,8 +300,8 @@ void Monitor::assert_owner(Thread * expected) {
300300
msg, p2i(_owner), p2i(expected));
301301
}
302302

303-
Monitor * Monitor::get_least_ranked_lock(Monitor * locks) {
304-
Monitor *res, *tmp;
303+
Mutex* Mutex::get_least_ranked_lock(Mutex* locks) {
304+
Mutex *res, *tmp;
305305
for (res = tmp = locks; tmp != NULL; tmp = tmp->next()) {
306306
if (tmp->rank() < res->rank()) {
307307
res = tmp;
@@ -320,8 +320,8 @@ Monitor * Monitor::get_least_ranked_lock(Monitor * locks) {
320320
return res;
321321
}
322322

323-
Monitor* Monitor::get_least_ranked_lock_besides_this(Monitor* locks) {
324-
Monitor *res, *tmp;
323+
Mutex* Mutex::get_least_ranked_lock_besides_this(Mutex* locks) {
324+
Mutex *res, *tmp;
325325
for (res = NULL, tmp = locks; tmp != NULL; tmp = tmp->next()) {
326326
if (tmp != this && (res == NULL || tmp->rank() < res->rank())) {
327327
res = tmp;
@@ -341,7 +341,7 @@ Monitor* Monitor::get_least_ranked_lock_besides_this(Monitor* locks) {
341341
}
342342

343343

344-
bool Monitor::contains(Monitor* locks, Monitor * lock) {
344+
bool Mutex::contains(Mutex* locks, Mutex* lock) {
345345
for (; locks != NULL; locks = locks->next()) {
346346
if (locks == lock) {
347347
return true;
@@ -356,7 +356,7 @@ bool Monitor::contains(Monitor* locks, Monitor * lock) {
356356
// might indicate exposure to deadlock.
357357
// Rather like an EventListener for _owner (:>).
358358

359-
void Monitor::set_owner_implementation(Thread *new_owner) {
359+
void Mutex::set_owner_implementation(Thread *new_owner) {
360360
// This function is solely responsible for maintaining
361361
// and checking the invariant that threads and locks
362362
// are in a 1/N relation, with some some locks unowned.
@@ -377,7 +377,7 @@ void Monitor::set_owner_implementation(Thread *new_owner) {
377377
// link "this" into the owned locks list
378378

379379
#ifdef ASSERT // Thread::_owned_locks is under the same ifdef
380-
Monitor* locks = get_least_ranked_lock(new_owner->owned_locks());
380+
Mutex* locks = get_least_ranked_lock(new_owner->owned_locks());
381381
// Mutex::set_owner_implementation is a friend of Thread
382382

383383
assert(this->rank() >= 0, "bad lock rank");
@@ -415,11 +415,11 @@ void Monitor::set_owner_implementation(Thread *new_owner) {
415415
_owner = NULL; // set the owner
416416

417417
#ifdef ASSERT
418-
Monitor *locks = old_owner->owned_locks();
418+
Mutex* locks = old_owner->owned_locks();
419419

420420
// remove "this" from the owned locks list
421421

422-
Monitor *prev = NULL;
422+
Mutex* prev = NULL;
423423
bool found = false;
424424
for (; locks != NULL; prev = locks, locks = locks->next()) {
425425
if (locks == this) {
@@ -440,7 +440,7 @@ void Monitor::set_owner_implementation(Thread *new_owner) {
440440

441441

442442
// Factored out common sanity checks for locking mutex'es. Used by lock() and try_lock()
443-
void Monitor::check_prelock_state(Thread *thread, bool safepoint_check) {
443+
void Mutex::check_prelock_state(Thread *thread, bool safepoint_check) {
444444
if (safepoint_check) {
445445
assert((!thread->is_active_Java_thread() || ((JavaThread *)thread)->thread_state() == _thread_in_vm)
446446
|| rank() == Mutex::special, "wrong thread state for using locks");
@@ -454,13 +454,13 @@ void Monitor::check_prelock_state(Thread *thread, bool safepoint_check) {
454454
"locking not allowed when crash protection is set");
455455
}
456456

457-
void Monitor::check_block_state(Thread *thread) {
457+
void Mutex::check_block_state(Thread *thread) {
458458
if (!_allow_vm_block && thread->is_VM_thread()) {
459459
warning("VM thread blocked on lock");
460460
print();
461461
BREAKPOINT;
462462
}
463-
assert(_owner != thread, "deadlock: blocking on monitor owned by current thread");
463+
assert(_owner != thread, "deadlock: blocking on mutex owned by current thread");
464464
}
465465

466466
#endif // PRODUCT

0 commit comments

Comments
 (0)