RFR: 8366671: Refactor Thread::SpinAcquire and Thread::SpinRelease [v9]

Axel Boldt-Christmas aboldtch at openjdk.org
Mon Dec 1 13:20:49 UTC 2025


On Mon, 1 Dec 2025 10:16:29 GMT, Anton Artemov <aartemov at openjdk.org> wrote:

> Adding a `NoSafepointVerifier` before `spin_acquire()` breaks creation of the main thread. The problem is in `ParkEvent::Allocate(this)`. And there is a comment in park.cpp: "Caveat: Allocate() and Release() may be called from threads other than the thread associated with the Event!". So it breaks the assert in `Thread::current()`. Any other way how to ensure no polling for safepoint?

I see. Yeah, do not think we have a construction that does exactly this right now. But we can easily extend the NSV to only call `Thread::current()` if it is not active. Something like:


diff --git a/src/hotspot/share/runtime/safepointVerifiers.cpp b/src/hotspot/share/runtime/safepointVerifiers.cpp
index 6eb61efe0ca..0c6f2e9add3 100644
--- a/src/hotspot/share/runtime/safepointVerifiers.cpp
+++ b/src/hotspot/share/runtime/safepointVerifiers.cpp
@@ -30,7 +30,9 @@
 
 #ifdef ASSERT
 
-NoSafepointVerifier::NoSafepointVerifier(bool active) : _thread(Thread::current()), _active(active) {
+NoSafepointVerifier::NoSafepointVerifier(bool active)
+  : _thread(active ? Thread::current() : nullptr),
+    _active(active) {
   if (!_active) {
     return;
   }
diff --git a/src/hotspot/share/utilities/spinCriticalSection.hpp b/src/hotspot/share/utilities/spinCriticalSection.hpp
index 487edc01b06..c4ccfd7d295 100644
--- a/src/hotspot/share/utilities/spinCriticalSection.hpp
+++ b/src/hotspot/share/utilities/spinCriticalSection.hpp
@@ -26,6 +26,9 @@
 #define SHARE_UTILITIES_SPINCRITICALSECTION_HPP
 
 #include "runtime/javaThread.hpp"
+#include "runtime/safepointVerifiers.hpp"
+#include "runtime/thread.hpp"
+#include "utilities/macros.hpp"
 
 // Ad-hoc mutual exclusion primitive: spin critical section,
 // which employs a spin lock.
@@ -40,12 +43,15 @@ class SpinCriticalSection {
   // We use int type as 32-bit atomic operation is the most performant
   // compared to  smaller/larger types.
   volatile int* const _lock;
+  DEBUG_ONLY(NoSafepointVerifier _nsv;)
 
   static void spin_acquire(volatile int* Lock);
   static void spin_release(volatile int* Lock);
 public:
   NONCOPYABLE(SpinCriticalSection);
-  SpinCriticalSection(volatile int* lock) : _lock(lock) {
+  SpinCriticalSection(volatile int* lock)
+    : _lock(lock)
+      DEBUG_ONLY(COMMA _nsv(Thread::current_or_null_safe() != nullptr)) {
     spin_acquire(_lock);
   }
   ~SpinCriticalSection() {

-------------

PR Comment: https://git.openjdk.org/jdk/pull/28264#issuecomment-3596509884


More information about the hotspot-dev mailing list