diff --git a/src/hotspot/share/jfr/recorder/service/jfrPostBox.cpp b/src/hotspot/share/jfr/recorder/service/jfrPostBox.cpp index 00ebd710d30..971377043c3 100644 --- a/src/hotspot/share/jfr/recorder/service/jfrPostBox.cpp +++ b/src/hotspot/share/jfr/recorder/service/jfrPostBox.cpp @@ -103,7 +103,7 @@ void JfrPostBox::deposit(int new_messages) { void JfrPostBox::asynchronous_post(int msg) { assert(!is_synchronous(msg), "invariant"); deposit(msg); - JfrMonitorTryLock try_msg_lock(JfrMsg_lock); + JfrMutexTryLock try_msg_lock(JfrMsg_lock); if (try_msg_lock.acquired()) { JfrMsg_lock->notify_all(); } diff --git a/src/hotspot/share/jfr/recorder/storage/jfrStorage.cpp b/src/hotspot/share/jfr/recorder/storage/jfrStorage.cpp index 019793d0456..420232743a0 100644 --- a/src/hotspot/share/jfr/recorder/storage/jfrStorage.cpp +++ b/src/hotspot/share/jfr/recorder/storage/jfrStorage.cpp @@ -38,6 +38,7 @@ #include "jfr/utilities/jfrIterator.hpp" #include "jfr/utilities/jfrLinkedList.inline.hpp" #include "jfr/utilities/jfrTime.hpp" +#include "jfr/utilities/jfrTryLock.hpp" #include "jfr/writers/jfrNativeEventWriter.hpp" #include "logging/log.hpp" #include "runtime/javaThread.hpp" @@ -316,7 +317,8 @@ static void log_discard(size_t pre_full_count, size_t post_full_count, size_t am } void JfrStorage::discard_oldest(Thread* thread) { - if (JfrBuffer_lock->try_lock()) { + JfrMutexTryLock mutex(JfrBuffer_lock); + if (mutex.acquired()) { if (!control().should_discard()) { // another thread handled it return; @@ -338,7 +340,6 @@ void JfrStorage::discard_oldest(Thread* thread) { oldest->release(); // publish break; } - JfrBuffer_lock->unlock(); log_discard(num_full_pre_discard, control().full_count(), discarded_size); } } diff --git a/src/hotspot/share/jfr/utilities/jfrTryLock.hpp b/src/hotspot/share/jfr/utilities/jfrTryLock.hpp index 10b3c93f5dc..5c76fa66321 100644 --- a/src/hotspot/share/jfr/utilities/jfrTryLock.hpp +++ b/src/hotspot/share/jfr/utilities/jfrTryLock.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2023, 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 @@ -50,25 +50,23 @@ class JfrTryLock { } }; -class JfrMonitorTryLock : public StackObj { +class JfrMutexTryLock : public StackObj { private: - Monitor* _lock; + Mutex* _mutex; bool _acquired; public: - JfrMonitorTryLock(Monitor* lock) : _lock(lock), _acquired(lock->try_lock()) {} - - ~JfrMonitorTryLock() { + JfrMutexTryLock(Mutex* mutex) : _mutex(mutex), _acquired(mutex->try_lock()) {} + ~JfrMutexTryLock() { if (_acquired) { - assert(_lock->owned_by_self(), "invariant"); - _lock->unlock(); + assert(_mutex->owned_by_self(), "invariant"); + _mutex->unlock(); } } bool acquired() const { return _acquired; } - }; #endif // SHARE_JFR_UTILITIES_JFRTRYLOCK_HPP