3434#include " jfr/utilities/jfrTime.hpp"
3535#include " jfrfiles/jfrEventClasses.hpp"
3636#include " logging/log.hpp"
37+ #include " runtime/atomic.hpp"
3738#include " runtime/frame.inline.hpp"
3839#include " runtime/os.hpp"
3940#include " runtime/semaphore.hpp"
@@ -350,8 +351,8 @@ class JfrThreadSampler : public NonJavaThread {
350351 void run ();
351352 static Monitor* transition_block () { return JfrThreadSampler_lock; }
352353 static void on_javathread_suspend (JavaThread* thread);
353- int64_t get_java_period () const { return _java_period_millis; };
354- int64_t get_native_period () const { return _native_period_millis; };
354+ int64_t get_java_period () const { return Atomic::load (& _java_period_millis) ; };
355+ int64_t get_native_period () const { return Atomic::load (& _native_period_millis) ; };
355356};
356357
357358static void clear_transition_block (JavaThread* jt) {
@@ -412,12 +413,12 @@ JfrThreadSampler::~JfrThreadSampler() {
412413
413414void JfrThreadSampler::set_java_period (int64_t period_millis) {
414415 assert (period_millis >= 0 , " invariant" );
415- _java_period_millis = period_millis;
416+ Atomic::store (& _java_period_millis, period_millis) ;
416417}
417418
418419void JfrThreadSampler::set_native_period (int64_t period_millis) {
419420 assert (period_millis >= 0 , " invariant" );
420- _native_period_millis = period_millis;
421+ Atomic::store (& _native_period_millis, period_millis) ;
421422}
422423
423424static inline bool is_released (JavaThread* jt) {
@@ -496,8 +497,17 @@ void JfrThreadSampler::run() {
496497 last_native_ms = last_java_ms;
497498 }
498499 _sample.signal ();
499- const int64_t java_period_millis = _java_period_millis == 0 ? max_jlong : MAX2<int64_t >(_java_period_millis, 1 );
500- const int64_t native_period_millis = _native_period_millis == 0 ? max_jlong : MAX2<int64_t >(_native_period_millis, 1 );
500+
501+ int64_t java_period_millis = get_java_period ();
502+ java_period_millis = java_period_millis == 0 ? max_jlong : MAX2<int64_t >(java_period_millis, 1 );
503+ int64_t native_period_millis = get_native_period ();
504+ native_period_millis = native_period_millis == 0 ? max_jlong : MAX2<int64_t >(native_period_millis, 1 );
505+
506+ // If both periods are max_jlong, it implies the sampler is in the process of
507+ // disenrolling. Loop back for graceful disenroll by means of the semaphore.
508+ if (java_period_millis == max_jlong && native_period_millis == max_jlong) {
509+ continue ;
510+ }
501511
502512 const int64_t now_ms = get_monotonic_ms ();
503513
@@ -516,7 +526,7 @@ void JfrThreadSampler::run() {
516526 const int64_t sleep_to_next = MIN2<int64_t >(next_j, next_n);
517527
518528 if (sleep_to_next > 0 ) {
519- os::naked_short_sleep (sleep_to_next);
529+ os::naked_sleep (sleep_to_next);
520530 }
521531
522532 if ((next_j - sleep_to_next) <= 0 ) {
0 commit comments