diff --git a/src/hotspot/share/runtime/mutex.cpp b/src/hotspot/share/runtime/mutex.cpp index c455b5f36a2..8e0c1d10e5c 100644 --- a/src/hotspot/share/runtime/mutex.cpp +++ b/src/hotspot/share/runtime/mutex.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -293,7 +293,8 @@ Mutex::Mutex(Rank rank, const char * name, bool allow_vm_block) : _owner(nullptr _rank = rank; _skip_rank_check = false; - assert(_rank >= static_cast(0) && _rank <= safepoint, "Bad lock rank %s: %s", rank_name(), name); + assert(_rank >= static_cast(0) && _rank <= safepoint, "Bad lock rank %d outside [0, %d]: %s", + static_cast(rank), static_cast(safepoint), name); // The allow_vm_block also includes allowing other non-Java threads to block or // allowing Java threads to block in native. @@ -324,25 +325,33 @@ static const char* _rank_names[] = { "event", "service", "stackwatermark", "tty" static const int _num_ranks = 7; -static const char* rank_name_internal(Mutex::Rank r) { +static void print_rank_name_internal(outputStream* st, Mutex::Rank r) { // Find closest rank and print out the name - stringStream st; for (int i = 0; i < _num_ranks; i++) { if (r == _ranks[i]) { - return _rank_names[i]; + st->print("%s", _rank_names[i]); } else if (r > _ranks[i] && (i < _num_ranks-1 && r < _ranks[i+1])) { int delta = static_cast(_ranks[i+1]) - static_cast(r); - st.print("%s-%d", _rank_names[i+1], delta); - return st.as_string(); + st->print("%s-%d", _rank_names[i+1], delta); } } - return "fail"; +} + +// Requires caller to have ResourceMark. +static const char* rank_name_internal(Mutex::Rank r) { + stringStream st; + print_rank_name_internal(&st, r); + return st.as_string(); } const char* Mutex::rank_name() const { return rank_name_internal(_rank); } +// Does not require caller to have ResourceMark. +void Mutex::print_rank_name(outputStream* st) const { + print_rank_name_internal(st, _rank); +} void Mutex::assert_no_overlap(Rank orig, Rank adjusted, int adjust) { int i = 0; @@ -364,7 +373,8 @@ void Mutex::print_on(outputStream* st) const { if (_allow_vm_block) { st->print("%s", " allow_vm_block"); } - DEBUG_ONLY(st->print(" %s", rank_name())); + st->print(" "); + DEBUG_ONLY(print_rank_name(st)); st->cr(); } diff --git a/src/hotspot/share/runtime/mutex.hpp b/src/hotspot/share/runtime/mutex.hpp index cf2b222d2da..4d30a320cbf 100644 --- a/src/hotspot/share/runtime/mutex.hpp +++ b/src/hotspot/share/runtime/mutex.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -130,9 +130,11 @@ class Mutex : public CHeapObj { return _skip_rank_check; } + const char* rank_name() const; + void print_rank_name(outputStream* st) const; + public: Rank rank() const { return _rank; } - const char* rank_name() const; Mutex* next() const { return _next; } #endif // ASSERT diff --git a/test/hotspot/gtest/runtime/test_mutex.cpp b/test/hotspot/gtest/runtime/test_mutex.cpp index 2ed7b1b3a34..64b7a5207d7 100644 --- a/test/hotspot/gtest/runtime/test_mutex.cpp +++ b/test/hotspot/gtest/runtime/test_mutex.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020, 2026, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -323,11 +323,12 @@ TEST_VM_ASSERT_MSG(MutexSafepoint, possible_safepoint_lock, ".* Possible safepoint reached by thread that does not allow it") { JavaThread* thread = JavaThread::current(); ThreadInVMfromNative in_native(thread); - MutexLocker ml(new Mutex(Mutex::nosafepoint, "SpecialTest_lock"), + MutexLocker ml(new Mutex(Mutex::nosafepoint-2, "SpecialTest_lock"), Mutex::_no_safepoint_check_flag); thread->print_thread_state_on(tty); // If the lock above succeeds, try to safepoint to test the NSV implied with this nosafepoint lock. ThreadBlockInVM tbivm(thread); thread->print_thread_state_on(tty); } + #endif // ASSERT