3535#include " jfr/utilities/jfrTime.hpp"
3636#include " jfrfiles/jfrEventClasses.hpp"
3737#include " logging/log.hpp"
38+ #include " runtime/atomic.hpp"
3839#include " runtime/frame.inline.hpp"
3940#include " runtime/os.hpp"
4041#include " runtime/semaphore.hpp"
@@ -353,8 +354,8 @@ class JfrThreadSampler : public NonJavaThread {
353354 void run ();
354355 static Monitor* transition_block () { return JfrThreadSampler_lock; }
355356 static void on_javathread_suspend (JavaThread* thread);
356- int64_t get_java_period () const { return _java_period_millis; };
357- int64_t get_native_period () const { return _native_period_millis; };
357+ int64_t get_java_period () const { return Atomic::load (& _java_period_millis) ; };
358+ int64_t get_native_period () const { return Atomic::load (& _native_period_millis) ; };
358359};
359360
360361static void clear_transition_block (JavaThread* jt) {
@@ -417,12 +418,12 @@ JfrThreadSampler::~JfrThreadSampler() {
417418
418419void JfrThreadSampler::set_java_period (int64_t period_millis) {
419420 assert (period_millis >= 0 , " invariant" );
420- _java_period_millis = period_millis;
421+ Atomic::store (& _java_period_millis, period_millis) ;
421422}
422423
423424void JfrThreadSampler::set_native_period (int64_t period_millis) {
424425 assert (period_millis >= 0 , " invariant" );
425- _native_period_millis = period_millis;
426+ Atomic::store (& _native_period_millis, period_millis) ;
426427}
427428
428429static inline bool is_released (JavaThread* jt) {
@@ -501,8 +502,17 @@ void JfrThreadSampler::run() {
501502 last_native_ms = last_java_ms;
502503 }
503504 _sample.signal ();
504- const int64_t java_period_millis = _java_period_millis == 0 ? max_jlong : MAX2<int64_t >(_java_period_millis, 1 );
505- const int64_t native_period_millis = _native_period_millis == 0 ? max_jlong : MAX2<int64_t >(_native_period_millis, 1 );
505+
506+ int64_t java_period_millis = get_java_period ();
507+ java_period_millis = java_period_millis == 0 ? max_jlong : MAX2<int64_t >(java_period_millis, 1 );
508+ int64_t native_period_millis = get_native_period ();
509+ native_period_millis = native_period_millis == 0 ? max_jlong : MAX2<int64_t >(native_period_millis, 1 );
510+
511+ // If both periods are max_jlong, it implies the sampler is in the process of
512+ // disenrolling. Loop back for graceful disenroll by means of the semaphore.
513+ if (java_period_millis == max_jlong && native_period_millis == max_jlong) {
514+ continue ;
515+ }
506516
507517 const int64_t now_ms = get_monotonic_ms ();
508518
@@ -521,7 +531,7 @@ void JfrThreadSampler::run() {
521531 const int64_t sleep_to_next = MIN2<int64_t >(next_j, next_n);
522532
523533 if (sleep_to_next > 0 ) {
524- os::naked_short_sleep (sleep_to_next);
534+ os::naked_sleep (sleep_to_next);
525535 }
526536
527537 if ((next_j - sleep_to_next) <= 0 ) {
0 commit comments