1 /*
2 * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved.
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
25 #include "logging/log.hpp"
26 #include "memory/resourceArea.hpp"
27 #include "runtime/interfaceSupport.inline.hpp"
28 #include "runtime/javaThread.inline.hpp"
29 #include "runtime/mutex.hpp"
30 #include "runtime/os.inline.hpp"
31 #include "runtime/osThread.hpp"
32 #include "runtime/safepointMechanism.inline.hpp"
33 #include "runtime/semaphore.inline.hpp"
34 #include "runtime/threadCrashProtection.hpp"
35 #include "utilities/events.hpp"
36 #include "utilities/macros.hpp"
37
38 class InFlightMutexRelease {
39 private:
40 Mutex* _in_flight_mutex;
41 public:
42 InFlightMutexRelease(Mutex* in_flight_mutex) : _in_flight_mutex(in_flight_mutex) {
43 assert(in_flight_mutex != nullptr, "must be");
44 }
45 void operator()(JavaThread* current) {
46 _in_flight_mutex->release_for_safepoint();
47 _in_flight_mutex = nullptr;
48 }
49 bool not_released() { return _in_flight_mutex != nullptr; }
50 };
51
52 #ifdef ASSERT
53 void Mutex::check_block_state(Thread* thread) {
54 if (!_allow_vm_block && thread->is_VM_thread()) {
55 // JavaThreads are checked to make sure that they do not hold _allow_vm_block locks during operations
56 // that could safepoint. Make sure the vm thread never uses locks with _allow_vm_block == false.
57 fatal("VM thread could block on lock that may be held by a JavaThread during safepoint: %s", name());
58 }
59
60 assert(!ThreadCrashProtection::is_crash_protected(thread),
61 "locking not allowed when crash protection is set");
62 }
63
64 void Mutex::check_safepoint_state(Thread* thread) {
65 check_block_state(thread);
66
67 // If the lock acquisition checks for safepoint, verify that the lock was created with rank that
68 // has safepoint checks. Technically this doesn't affect NonJavaThreads since they won't actually
69 // check for safepoint, but let's make the rule unconditional unless there's a good reason not to.
70 assert(_rank > nosafepoint,
71 "This lock should not be taken with a safepoint check: %s", name());
72
73 if (thread->is_active_Java_thread()) {
74 // Also check NoSafepointVerifier, and thread state is _thread_in_vm
75 JavaThread::cast(thread)->check_for_valid_safepoint_state();
76 }
77 }
78
79 void Mutex::check_no_safepoint_state(Thread* thread) {
80 check_block_state(thread);
81 assert(!thread->is_active_Java_thread() || _rank <= nosafepoint,
82 "This lock should always have a safepoint check for Java threads: %s",
83 name());
84 }
85 #endif // ASSERT
86
87 void Mutex::lock_contended(Thread* self) {
88 DEBUG_ONLY(int retry_cnt = 0;)
89 bool is_active_Java_thread = self->is_active_Java_thread();
90 do {
91 #ifdef ASSERT
92 if (retry_cnt++ > 3) {
93 log_trace(vmmutex)("JavaThread " INTPTR_FORMAT " on %d attempt trying to acquire vmmutex %s", p2i(self), retry_cnt, _name);
94 }
95 #endif // ASSERT
96
97 // Is it a JavaThread participating in the safepoint protocol.
98 if (is_active_Java_thread) {
99 InFlightMutexRelease ifmr(this);
100 assert(rank() > Mutex::nosafepoint, "Potential deadlock with nosafepoint or lesser rank mutex");
101 {
102 ThreadBlockInVMPreprocess<InFlightMutexRelease> tbivmdc(JavaThread::cast(self), ifmr);
103 _lock.lock();
104 }
105 if (ifmr.not_released()) {
106 // Not unlocked by ~ThreadBlockInVMPreprocess
107 break;
108 }
109 } else {
110 _lock.lock();
111 break;
112 }
113 } while (!_lock.try_lock());
114 }
115
116 void Mutex::lock(Thread* self) {
117 assert(owner() != self, "invariant");
118
119 check_safepoint_state(self);
120 check_rank(self);
121
122 OrderAccess::fence();
123 if (!_lock.try_lock()) {
124 // The lock is contended, use contended slow-path function to lock
125 lock_contended(self);
126 }
127
128 assert_owner(nullptr);
129 set_owner(self);
130 }
131
132 void Mutex::lock() {
133 lock(Thread::current());
134 }
135
136 // Lock without safepoint check - a degenerate variant of lock() for use by
137 // JavaThreads when it is known to be safe to not check for a safepoint when
138 // acquiring this lock. If the thread blocks acquiring the lock it is not
139 // safepoint-safe and so will prevent a safepoint from being reached. If used
140 // in the wrong way this can lead to a deadlock with the safepoint code.
141
142 void Mutex::lock_without_safepoint_check(Thread * self) {
143 assert(owner() != self, "invariant");
144
145 check_no_safepoint_state(self);
146 check_rank(self);
147
148 OrderAccess::fence();
149 _lock.lock();
150 assert_owner(nullptr);
151 set_owner(self);
152 }
153
154 void Mutex::lock_without_safepoint_check() {
155 lock_without_safepoint_check(Thread::current());
156 }
157
158
159 // Returns true if thread succeeds in grabbing the lock, otherwise false.
160 bool Mutex::try_lock_inner(bool do_rank_checks) {
161 Thread * const self = Thread::current();
162 // Checking the owner hides the potential difference in recursive locking behaviour
163 // on some platforms.
164 if (owner() == self) {
165 return false;
166 }
167
168 if (do_rank_checks) {
169 check_rank(self);
170 }
171 // Some safepoint checking locks use try_lock, so cannot check
172 // safepoint state, but can check blocking state.
173 check_block_state(self);
174
175 OrderAccess::fence();
176 if (_lock.try_lock()) {
177 assert_owner(nullptr);
178 set_owner(self);
179 return true;
180 }
181 return false;
182 }
183
184 bool Mutex::try_lock() {
185 return try_lock_inner(true /* do_rank_checks */);
186 }
187
188 bool Mutex::try_lock_without_rank_check() {
189 bool res = try_lock_inner(false /* do_rank_checks */);
190 DEBUG_ONLY(if (res) _skip_rank_check = true;)
191 return res;
192 }
193
194 void Mutex::release_for_safepoint() {
195 assert_owner(nullptr);
196 _lock.unlock();
197 }
198
199 void Mutex::unlock() {
200 DEBUG_ONLY(assert_owner(Thread::current()));
201 set_owner(nullptr);
202 _lock.unlock();
203 }
204
205 void Monitor::notify() {
206 DEBUG_ONLY(assert_owner(Thread::current()));
207 _lock.notify();
208 }
209
210 void Monitor::notify_all() {
211 DEBUG_ONLY(assert_owner(Thread::current()));
212 _lock.notify_all();
213 }
214
215 // timeout is in milliseconds - with zero meaning never timeout
216 bool Monitor::wait_without_safepoint_check(uint64_t timeout) {
217 Thread* const self = Thread::current();
218
219 assert_owner(self);
220 check_rank(self);
221
222 // conceptually set the owner to null in anticipation of
223 // abdicating the lock in wait
224 set_owner(nullptr);
225
226 // Check safepoint state after resetting owner and possible NSV.
227 check_no_safepoint_state(self);
228
229 int wait_status = _lock.wait(timeout);
230 set_owner(self);
231 return wait_status != 0; // return true IFF timeout
232 }
233
234 // timeout is in milliseconds - with zero meaning never timeout
235 bool Monitor::wait(uint64_t timeout) {
236 JavaThread* const self = JavaThread::current();
237 // Safepoint checking logically implies an active JavaThread.
238 assert(self->is_active_Java_thread(), "invariant");
239
240 assert_owner(self);
241 check_rank(self);
242
243 // conceptually set the owner to null in anticipation of
244 // abdicating the lock in wait
245 set_owner(nullptr);
246
247 // Check safepoint state after resetting owner and possible NSV.
248 check_safepoint_state(self);
249
250 int wait_status;
251 InFlightMutexRelease ifmr(this);
252
253 {
254 ThreadBlockInVMPreprocess<InFlightMutexRelease> tbivmdc(self, ifmr);
255 OSThreadWaitState osts(self->osthread(), false /* not Object.wait() */);
256
257 wait_status = _lock.wait(timeout);
258 }
259
260 if (ifmr.not_released()) {
261 // Not unlocked by ~ThreadBlockInVMPreprocess
262 assert_owner(nullptr);
263 // Conceptually reestablish ownership of the lock.
264 set_owner(self);
265 } else {
266 lock(self);
267 }
268
269 return wait_status != 0; // return true IFF timeout
270 }
271
272 static const int MAX_NUM_MUTEX = 1204;
273 static Mutex* _internal_mutex_arr[MAX_NUM_MUTEX];
274 Mutex** Mutex::_mutex_array = _internal_mutex_arr;
275 int Mutex::_num_mutex = 0;
276
277 void Mutex::add_mutex(Mutex* var) {
278 assert(Mutex::_num_mutex < MAX_NUM_MUTEX, "increase MAX_NUM_MUTEX");
279 Mutex::_mutex_array[_num_mutex++] = var;
280 }
281
282 Mutex::~Mutex() {
283 assert_owner(nullptr);
284 os::free(const_cast<char*>(_name));
285 }
286
287 Mutex::Mutex(Rank rank, const char * name, bool allow_vm_block) : _owner(nullptr), _id(-1) {
288 assert(os::mutex_init_done(), "Too early!");
289 assert(name != nullptr, "Mutex requires a name");
290 _name = os::strdup(name, mtInternal);
291 _id = MutexLocker::name2id(name);
292 #ifdef ASSERT
293 _allow_vm_block = allow_vm_block;
294 _rank = rank;
295 _skip_rank_check = false;
296
297 assert(_rank >= static_cast<Rank>(0) && _rank <= safepoint, "Bad lock rank %s: %s", rank_name(), name);
298
299 // The allow_vm_block also includes allowing other non-Java threads to block or
300 // allowing Java threads to block in native.
301 assert(_rank > nosafepoint || _allow_vm_block,
302 "Locks that don't check for safepoint should always allow the vm to block: %s", name);
303 #endif
304 }
305
306 bool Mutex::owned_by_self() const {
307 return owner() == Thread::current();
308 }
309
310 void Mutex::print_on_error(outputStream* st) const {
311 st->print("[" PTR_FORMAT, p2i(this));
312 st->print("] %s", _name);
313 st->print(" - owner thread: " PTR_FORMAT, p2i(owner()));
314 }
315
316 // ----------------------------------------------------------------------------------
317 // Non-product code
318 //
319 #ifdef ASSERT
320 static Mutex::Rank _ranks[] = { Mutex::event, Mutex::service, Mutex::stackwatermark, Mutex::tty, Mutex::oopstorage,
321 Mutex::nosafepoint, Mutex::safepoint };
322
323 static const char* _rank_names[] = { "event", "service", "stackwatermark", "tty", "oopstorage",
324 "nosafepoint", "safepoint" };
325
326 static const int _num_ranks = 7;
327
328 static const char* rank_name_internal(Mutex::Rank r) {
329 // Find closest rank and print out the name
330 stringStream st;
331 for (int i = 0; i < _num_ranks; i++) {
332 if (r == _ranks[i]) {
333 return _rank_names[i];
334 } else if (r > _ranks[i] && (i < _num_ranks-1 && r < _ranks[i+1])) {
335 int delta = static_cast<int>(_ranks[i+1]) - static_cast<int>(r);
336 st.print("%s-%d", _rank_names[i+1], delta);
337 return st.as_string();
338 }
339 }
340 return "fail";
341 }
342
343 const char* Mutex::rank_name() const {
344 return rank_name_internal(_rank);
345 }
346
347
348 void Mutex::assert_no_overlap(Rank orig, Rank adjusted, int adjust) {
349 int i = 0;
350 while (_ranks[i] < orig) i++;
351 // underflow is caught in constructor
352 if (i != 0 && adjusted > event && adjusted <= _ranks[i-1]) {
353 ResourceMark rm;
354 assert(adjusted > _ranks[i-1],
355 "Rank %s-%d overlaps with %s",
356 rank_name_internal(orig), adjust, rank_name_internal(adjusted));
357 }
358 }
359 #endif // ASSERT
360
361 #ifndef PRODUCT
362 void Mutex::print_on(outputStream* st) const {
363 st->print("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT,
364 p2i(this), _name, p2i(owner()));
365 if (_allow_vm_block) {
366 st->print("%s", " allow_vm_block");
367 }
368 DEBUG_ONLY(st->print(" %s", rank_name()));
369 st->cr();
370 }
371
372 void Mutex::print() const {
373 print_on(::tty);
374 }
375 #endif // PRODUCT
376
377 #ifdef ASSERT
378 void Mutex::assert_owner(Thread * expected) {
379 const char* msg = "invalid owner";
380 if (expected == nullptr) {
381 msg = "should be un-owned";
382 }
383 else if (expected == Thread::current()) {
384 msg = "should be owned by current thread";
385 }
386 assert(owner() == expected,
387 "%s: owner=" INTPTR_FORMAT ", should be=" INTPTR_FORMAT,
388 msg, p2i(owner()), p2i(expected));
389 }
390
391 Mutex* Mutex::get_least_ranked_lock(Mutex* locks) {
392 Mutex *res, *tmp;
393 for (res = tmp = locks; tmp != nullptr; tmp = tmp->next()) {
394 if (tmp->rank() < res->rank()) {
395 res = tmp;
396 }
397 }
398 return res;
399 }
400
401 Mutex* Mutex::get_least_ranked_lock_besides_this(Mutex* locks) {
402 Mutex *res, *tmp;
403 for (res = nullptr, tmp = locks; tmp != nullptr; tmp = tmp->next()) {
404 if (tmp != this && (res == nullptr || tmp->rank() < res->rank())) {
405 res = tmp;
406 }
407 }
408 assert(res != this, "invariant");
409 return res;
410 }
411
412 // Tests for rank violations that might indicate exposure to deadlock.
413 void Mutex::check_rank(Thread* thread) {
414 Mutex* locks_owned = thread->owned_locks();
415
416 // We expect the locks already acquired to be in increasing rank order,
417 // modulo locks acquired in try_lock_without_rank_check()
418 for (Mutex* tmp = locks_owned; tmp != nullptr; tmp = tmp->next()) {
419 if (tmp->next() != nullptr) {
420 assert(tmp->rank() < tmp->next()->rank()
421 || tmp->skip_rank_check(), "mutex rank anomaly?");
422 }
423 }
424
425 if (owned_by_self()) {
426 // wait() case
427 Mutex* least = get_least_ranked_lock_besides_this(locks_owned);
428 // For JavaThreads, we enforce not holding locks of rank nosafepoint or lower while waiting
429 // because the held lock has a NoSafepointVerifier so waiting on a lower ranked lock will not be
430 // able to check for safepoints first with a TBIVM.
431 // For all threads, we enforce not holding the tty lock or below, since this could block progress also.
432 // Also "this" should be the monitor with lowest rank owned by this thread.
433 if (least != nullptr && ((least->rank() <= Mutex::nosafepoint && thread->is_Java_thread()) ||
434 least->rank() <= Mutex::tty ||
435 least->rank() <= this->rank())) {
436 ResourceMark rm(thread);
437 assert(false, "Attempting to wait on monitor %s/%s while holding lock %s/%s -- "
438 "possible deadlock. %s", name(), rank_name(), least->name(), least->rank_name(),
439 least->rank() <= this->rank() ?
440 "Should wait on the least ranked monitor from all owned locks." :
441 thread->is_Java_thread() ?
442 "Should not block(wait) while holding a lock of rank nosafepoint or below." :
443 "Should not block(wait) while holding a lock of rank tty or below.");
444 }
445 } else {
446 // lock()/lock_without_safepoint_check()/try_lock() case
447 Mutex* least = get_least_ranked_lock(locks_owned);
448 // Deadlock prevention rules require us to acquire Mutexes only in
449 // a global total order. For example, if m1 is the lowest ranked mutex
450 // that the thread holds and m2 is the mutex the thread is trying
451 // to acquire, then deadlock prevention rules require that the rank
452 // of m2 be less than the rank of m1. This prevents circular waits.
453 if (least != nullptr && least->rank() <= this->rank()) {
454 ResourceMark rm(thread);
455 if (least->rank() > Mutex::tty) {
456 // Printing owned locks acquires tty lock. If the least rank was below or equal
457 // tty, then deadlock detection code would circle back here, until we run
458 // out of stack and crash hard. Print locks only when it is safe.
459 thread->print_owned_locks();
460 }
461 assert(false, "Attempting to acquire lock %s/%s out of order with lock %s/%s -- "
462 "possible deadlock", this->name(), this->rank_name(), least->name(), least->rank_name());
463 }
464 }
465 }
466
467 // Called immediately after lock acquisition or release as a diagnostic
468 // to track the lock-set of the thread.
469 // Rather like an EventListener for _owner (:>).
470
471 void Mutex::set_owner_implementation(Thread *new_owner) {
472 // This function is solely responsible for maintaining
473 // and checking the invariant that threads and locks
474 // are in a 1/N relation, with some some locks unowned.
475 // It uses the Mutex::_owner, Mutex::_next, and
476 // Thread::_owned_locks fields, and no other function
477 // changes those fields.
478 // It is illegal to set the mutex from one non-null
479 // owner to another--it must be owned by null as an
480 // intermediate state.
481
482 if (new_owner != nullptr) {
483 // the thread is acquiring this lock
484
485 assert(new_owner == Thread::current(), "Should I be doing this?");
486 assert(owner() == nullptr, "setting the owner thread of an already owned mutex");
487 raw_set_owner(new_owner); // set the owner
488
489 // link "this" into the owned locks list
490 this->_next = new_owner->_owned_locks;
491 new_owner->_owned_locks = this;
492
493 // NSV implied with locking allow_vm_block flag.
494 // The tty_lock is special because it is released for the safepoint by
495 // the safepoint mechanism.
496 if (new_owner->is_Java_thread() && _allow_vm_block && this != tty_lock) {
497 JavaThread::cast(new_owner)->inc_no_safepoint_count();
498 }
499
500 } else {
501 // the thread is releasing this lock
502
503 Thread* old_owner = owner();
504 _last_owner = old_owner;
505 _skip_rank_check = false;
506
507 assert(old_owner != nullptr, "removing the owner thread of an unowned mutex");
508 assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
509
510 raw_set_owner(nullptr); // set the owner
511
512 Mutex* locks = old_owner->owned_locks();
513
514 // remove "this" from the owned locks list
515
516 Mutex* prev = nullptr;
517 bool found = false;
518 for (; locks != nullptr; prev = locks, locks = locks->next()) {
519 if (locks == this) {
520 found = true;
521 break;
522 }
523 }
524 assert(found, "Removing a lock not owned");
525 if (prev == nullptr) {
526 old_owner->_owned_locks = _next;
527 } else {
528 prev->_next = _next;
529 }
530 _next = nullptr;
531
532 // ~NSV implied with locking allow_vm_block flag.
533 if (old_owner->is_Java_thread() && _allow_vm_block && this != tty_lock) {
534 JavaThread::cast(old_owner)->dec_no_safepoint_count();
535 }
536 }
537 }
538 #endif // ASSERT
539
540 // Print all mutexes/monitors that are currently owned by a thread; called
541 // by fatal error handler.
542 void Mutex::print_owned_locks_on_error(outputStream* st) {
543 st->print("VM Mutex/Monitor currently owned by a thread: ");
544 bool none = true;
545 for (int i = 0; i < _num_mutex; i++) {
546 // see if it has an owner
547 if (_mutex_array[i]->owner() != nullptr) {
548 if (none) {
549 // print format used by Mutex::print_on_error()
550 st->print_cr(" ([mutex/lock_event])");
551 none = false;
552 }
553 _mutex_array[i]->print_on_error(st);
554 st->cr();
555 }
556 }
557 if (none) st->print_cr("None");
558 }
559
560 void Mutex::print_lock_ranks(outputStream* st) {
561 st->print_cr("VM Mutex/Monitor ranks: ");
562
563 #ifdef ASSERT
564 // Be extra defensive and figure out the bounds on
565 // ranks right here. This also saves a bit of time
566 // in the #ranks*#mutexes loop below.
567 int min_rank = INT_MAX;
568 int max_rank = INT_MIN;
569 for (int i = 0; i < _num_mutex; i++) {
570 Mutex* m = _mutex_array[i];
571 int r = (int) m->rank();
572 if (min_rank > r) min_rank = r;
573 if (max_rank < r) max_rank = r;
574 }
575
576 // Print the listings rank by rank
577 for (int r = min_rank; r <= max_rank; r++) {
578 bool first = true;
579 for (int i = 0; i < _num_mutex; i++) {
580 Mutex* m = _mutex_array[i];
581 if (r != (int) m->rank()) continue;
582
583 if (first) {
584 st->cr();
585 st->print_cr("Rank \"%s\":", m->rank_name());
586 first = false;
587 }
588 st->print_cr(" %s", m->name());
589 }
590 }
591 #else
592 st->print_cr(" Only known in debug builds.");
593 #endif // ASSERT
594 }
595
596 RecursiveMutex::RecursiveMutex() : _sem(1), _owner(nullptr), _recursions(0) {}
597
598 void RecursiveMutex::lock(Thread* current) {
599 assert(current == Thread::current(), "must be current thread");
600 if (current == _owner) {
601 _recursions++;
602 } else {
603 // can be called by jvmti by VMThread.
604 if (current->is_Java_thread()) {
605 _sem.wait_with_safepoint_check(JavaThread::cast(current));
606 } else {
607 _sem.wait();
608 }
609 _recursions++;
610 assert(_recursions == 1, "should be");
611 _owner = current;
612 }
613 }
614
615 void RecursiveMutex::unlock(Thread* current) {
616 assert(current == Thread::current(), "must be current thread");
617 assert(current == _owner, "must be owner");
618 _recursions--;
619 if (_recursions == 0) {
620 _owner = nullptr;
621 _sem.signal();
622 }
623 }