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) {
288 assert(os::mutex_init_done(), "Too early!");
289 assert(name != nullptr, "Mutex requires a name");
290 _name = os::strdup(name, mtInternal);
291 #ifdef ASSERT
292 _allow_vm_block = allow_vm_block;
293 _rank = rank;
294 _skip_rank_check = false;
295
296 assert(_rank >= static_cast<Rank>(0) && _rank <= safepoint, "Bad lock rank %s: %s", rank_name(), name);
297
298 // The allow_vm_block also includes allowing other non-Java threads to block or
299 // allowing Java threads to block in native.
300 assert(_rank > nosafepoint || _allow_vm_block,
301 "Locks that don't check for safepoint should always allow the vm to block: %s", name);
302 #endif
303 }
304
305 bool Mutex::owned_by_self() const {
306 return owner() == Thread::current();
307 }
308
309 void Mutex::print_on_error(outputStream* st) const {
310 st->print("[" PTR_FORMAT, p2i(this));
311 st->print("] %s", _name);
312 st->print(" - owner thread: " PTR_FORMAT, p2i(owner()));
313 }
314
315 // ----------------------------------------------------------------------------------
316 // Non-product code
317 //
318 #ifdef ASSERT
319 static Mutex::Rank _ranks[] = { Mutex::event, Mutex::service, Mutex::stackwatermark, Mutex::tty, Mutex::oopstorage,
320 Mutex::nosafepoint, Mutex::safepoint };
321
322 static const char* _rank_names[] = { "event", "service", "stackwatermark", "tty", "oopstorage",
323 "nosafepoint", "safepoint" };
324
325 static const int _num_ranks = 7;
326
327 static const char* rank_name_internal(Mutex::Rank r) {
328 // Find closest rank and print out the name
329 stringStream st;
330 for (int i = 0; i < _num_ranks; i++) {
331 if (r == _ranks[i]) {
332 return _rank_names[i];
333 } else if (r > _ranks[i] && (i < _num_ranks-1 && r < _ranks[i+1])) {
334 int delta = static_cast<int>(_ranks[i+1]) - static_cast<int>(r);
335 st.print("%s-%d", _rank_names[i+1], delta);
336 return st.as_string();
337 }
338 }
339 return "fail";
340 }
341
342 const char* Mutex::rank_name() const {
343 return rank_name_internal(_rank);
344 }
345
346
347 void Mutex::assert_no_overlap(Rank orig, Rank adjusted, int adjust) {
348 int i = 0;
349 while (_ranks[i] < orig) i++;
350 // underflow is caught in constructor
351 if (i != 0 && adjusted > event && adjusted <= _ranks[i-1]) {
352 ResourceMark rm;
353 assert(adjusted > _ranks[i-1],
354 "Rank %s-%d overlaps with %s",
355 rank_name_internal(orig), adjust, rank_name_internal(adjusted));
356 }
357 }
358 #endif // ASSERT
359
360 #ifndef PRODUCT
361 void Mutex::print_on(outputStream* st) const {
362 st->print("Mutex: [" PTR_FORMAT "] %s - owner: " PTR_FORMAT,
363 p2i(this), _name, p2i(owner()));
364 if (_allow_vm_block) {
365 st->print("%s", " allow_vm_block");
366 }
367 DEBUG_ONLY(st->print(" %s", rank_name()));
368 st->cr();
369 }
370
371 void Mutex::print() const {
372 print_on(::tty);
373 }
374 #endif // PRODUCT
375
376 #ifdef ASSERT
377 void Mutex::assert_owner(Thread * expected) {
378 const char* msg = "invalid owner";
379 if (expected == nullptr) {
380 msg = "should be un-owned";
381 }
382 else if (expected == Thread::current()) {
383 msg = "should be owned by current thread";
384 }
385 assert(owner() == expected,
386 "%s: owner=" INTPTR_FORMAT ", should be=" INTPTR_FORMAT,
387 msg, p2i(owner()), p2i(expected));
388 }
389
390 Mutex* Mutex::get_least_ranked_lock(Mutex* locks) {
391 Mutex *res, *tmp;
392 for (res = tmp = locks; tmp != nullptr; tmp = tmp->next()) {
393 if (tmp->rank() < res->rank()) {
394 res = tmp;
395 }
396 }
397 return res;
398 }
399
400 Mutex* Mutex::get_least_ranked_lock_besides_this(Mutex* locks) {
401 Mutex *res, *tmp;
402 for (res = nullptr, tmp = locks; tmp != nullptr; tmp = tmp->next()) {
403 if (tmp != this && (res == nullptr || tmp->rank() < res->rank())) {
404 res = tmp;
405 }
406 }
407 assert(res != this, "invariant");
408 return res;
409 }
410
411 // Tests for rank violations that might indicate exposure to deadlock.
412 void Mutex::check_rank(Thread* thread) {
413 Mutex* locks_owned = thread->owned_locks();
414
415 // We expect the locks already acquired to be in increasing rank order,
416 // modulo locks acquired in try_lock_without_rank_check()
417 for (Mutex* tmp = locks_owned; tmp != nullptr; tmp = tmp->next()) {
418 if (tmp->next() != nullptr) {
419 assert(tmp->rank() < tmp->next()->rank()
420 || tmp->skip_rank_check(), "mutex rank anomaly?");
421 }
422 }
423
424 if (owned_by_self()) {
425 // wait() case
426 Mutex* least = get_least_ranked_lock_besides_this(locks_owned);
427 // For JavaThreads, we enforce not holding locks of rank nosafepoint or lower while waiting
428 // because the held lock has a NoSafepointVerifier so waiting on a lower ranked lock will not be
429 // able to check for safepoints first with a TBIVM.
430 // For all threads, we enforce not holding the tty lock or below, since this could block progress also.
431 // Also "this" should be the monitor with lowest rank owned by this thread.
432 if (least != nullptr && ((least->rank() <= Mutex::nosafepoint && thread->is_Java_thread()) ||
433 least->rank() <= Mutex::tty ||
434 least->rank() <= this->rank())) {
435 ResourceMark rm(thread);
436 assert(false, "Attempting to wait on monitor %s/%s while holding lock %s/%s -- "
437 "possible deadlock. %s", name(), rank_name(), least->name(), least->rank_name(),
438 least->rank() <= this->rank() ?
439 "Should wait on the least ranked monitor from all owned locks." :
440 thread->is_Java_thread() ?
441 "Should not block(wait) while holding a lock of rank nosafepoint or below." :
442 "Should not block(wait) while holding a lock of rank tty or below.");
443 }
444 } else {
445 // lock()/lock_without_safepoint_check()/try_lock() case
446 Mutex* least = get_least_ranked_lock(locks_owned);
447 // Deadlock prevention rules require us to acquire Mutexes only in
448 // a global total order. For example, if m1 is the lowest ranked mutex
449 // that the thread holds and m2 is the mutex the thread is trying
450 // to acquire, then deadlock prevention rules require that the rank
451 // of m2 be less than the rank of m1. This prevents circular waits.
452 if (least != nullptr && least->rank() <= this->rank()) {
453 ResourceMark rm(thread);
454 if (least->rank() > Mutex::tty) {
455 // Printing owned locks acquires tty lock. If the least rank was below or equal
456 // tty, then deadlock detection code would circle back here, until we run
457 // out of stack and crash hard. Print locks only when it is safe.
458 thread->print_owned_locks();
459 }
460 assert(false, "Attempting to acquire lock %s/%s out of order with lock %s/%s -- "
461 "possible deadlock", this->name(), this->rank_name(), least->name(), least->rank_name());
462 }
463 }
464 }
465
466 // Called immediately after lock acquisition or release as a diagnostic
467 // to track the lock-set of the thread.
468 // Rather like an EventListener for _owner (:>).
469
470 void Mutex::set_owner_implementation(Thread *new_owner) {
471 // This function is solely responsible for maintaining
472 // and checking the invariant that threads and locks
473 // are in a 1/N relation, with some some locks unowned.
474 // It uses the Mutex::_owner, Mutex::_next, and
475 // Thread::_owned_locks fields, and no other function
476 // changes those fields.
477 // It is illegal to set the mutex from one non-null
478 // owner to another--it must be owned by null as an
479 // intermediate state.
480
481 if (new_owner != nullptr) {
482 // the thread is acquiring this lock
483
484 assert(new_owner == Thread::current(), "Should I be doing this?");
485 assert(owner() == nullptr, "setting the owner thread of an already owned mutex");
486 raw_set_owner(new_owner); // set the owner
487
488 // link "this" into the owned locks list
489 this->_next = new_owner->_owned_locks;
490 new_owner->_owned_locks = this;
491
492 // NSV implied with locking allow_vm_block flag.
493 // The tty_lock is special because it is released for the safepoint by
494 // the safepoint mechanism.
495 if (new_owner->is_Java_thread() && _allow_vm_block && this != tty_lock) {
496 JavaThread::cast(new_owner)->inc_no_safepoint_count();
497 }
498
499 } else {
500 // the thread is releasing this lock
501
502 Thread* old_owner = owner();
503 _last_owner = old_owner;
504 _skip_rank_check = false;
505
506 assert(old_owner != nullptr, "removing the owner thread of an unowned mutex");
507 assert(old_owner == Thread::current(), "removing the owner thread of an unowned mutex");
508
509 raw_set_owner(nullptr); // set the owner
510
511 Mutex* locks = old_owner->owned_locks();
512
513 // remove "this" from the owned locks list
514
515 Mutex* prev = nullptr;
516 bool found = false;
517 for (; locks != nullptr; prev = locks, locks = locks->next()) {
518 if (locks == this) {
519 found = true;
520 break;
521 }
522 }
523 assert(found, "Removing a lock not owned");
524 if (prev == nullptr) {
525 old_owner->_owned_locks = _next;
526 } else {
527 prev->_next = _next;
528 }
529 _next = nullptr;
530
531 // ~NSV implied with locking allow_vm_block flag.
532 if (old_owner->is_Java_thread() && _allow_vm_block && this != tty_lock) {
533 JavaThread::cast(old_owner)->dec_no_safepoint_count();
534 }
535 }
536 }
537 #endif // ASSERT
538
539 // Print all mutexes/monitors that are currently owned by a thread; called
540 // by fatal error handler.
541 void Mutex::print_owned_locks_on_error(outputStream* st) {
542 st->print("VM Mutex/Monitor currently owned by a thread: ");
543 bool none = true;
544 for (int i = 0; i < _num_mutex; i++) {
545 // see if it has an owner
546 if (_mutex_array[i]->owner() != nullptr) {
547 if (none) {
548 // print format used by Mutex::print_on_error()
549 st->print_cr(" ([mutex/lock_event])");
550 none = false;
551 }
552 _mutex_array[i]->print_on_error(st);
553 st->cr();
554 }
555 }
556 if (none) st->print_cr("None");
557 }
558
559 void Mutex::print_lock_ranks(outputStream* st) {
560 st->print_cr("VM Mutex/Monitor ranks: ");
561
562 #ifdef ASSERT
563 // Be extra defensive and figure out the bounds on
564 // ranks right here. This also saves a bit of time
565 // in the #ranks*#mutexes loop below.
566 int min_rank = INT_MAX;
567 int max_rank = INT_MIN;
568 for (int i = 0; i < _num_mutex; i++) {
569 Mutex* m = _mutex_array[i];
570 int r = (int) m->rank();
571 if (min_rank > r) min_rank = r;
572 if (max_rank < r) max_rank = r;
573 }
574
575 // Print the listings rank by rank
576 for (int r = min_rank; r <= max_rank; r++) {
577 bool first = true;
578 for (int i = 0; i < _num_mutex; i++) {
579 Mutex* m = _mutex_array[i];
580 if (r != (int) m->rank()) continue;
581
582 if (first) {
583 st->cr();
584 st->print_cr("Rank \"%s\":", m->rank_name());
585 first = false;
586 }
587 st->print_cr(" %s", m->name());
588 }
589 }
590 #else
591 st->print_cr(" Only known in debug builds.");
592 #endif // ASSERT
593 }
594
595 RecursiveMutex::RecursiveMutex() : _sem(1), _owner(nullptr), _recursions(0) {}
596
597 void RecursiveMutex::lock(Thread* current) {
598 assert(current == Thread::current(), "must be current thread");
599 if (current == _owner) {
600 _recursions++;
601 } else {
602 // can be called by jvmti by VMThread.
603 if (current->is_Java_thread()) {
604 _sem.wait_with_safepoint_check(JavaThread::cast(current));
605 } else {
606 _sem.wait();
607 }
608 _recursions++;
609 assert(_recursions == 1, "should be");
610 _owner = current;
611 }
612 }
613
614 void RecursiveMutex::unlock(Thread* current) {
615 assert(current == Thread::current(), "must be current thread");
616 assert(current == _owner, "must be owner");
617 _recursions--;
618 if (_recursions == 0) {
619 _owner = nullptr;
620 _sem.signal();
621 }
622 }