22
22
#define FAKE_FRAME_MARK INT2FIX(1)
23
23
#define FAKE_FRAME_SWEEP INT2FIX(2)
24
24
25
+ /*
26
+ * As of Ruby 3.0, it should be safe to read stack frames at any time
27
+ * See https://github.com/ruby/ruby/commit/0e276dc458f94d9d79a0f7c7669bde84abe80f21
28
+ */
29
+ #if RUBY_API_VERSION_MAJOR < 3
30
+ #define USE_POSTPONED_JOB
31
+ #endif
32
+
25
33
static const char * fake_frame_cstrs [] = {
26
34
"(garbage collection)" ,
27
35
"(marking)" ,
@@ -596,31 +604,30 @@ stackprof_record_gc_samples()
596
604
static void
597
605
stackprof_gc_job_handler (void * data )
598
606
{
599
- static int in_signal_handler = 0 ;
600
- if (in_signal_handler ) return ;
601
607
if (!_stackprof .running ) return ;
602
608
603
- in_signal_handler ++ ;
604
609
stackprof_record_gc_samples ();
605
- in_signal_handler -- ;
606
610
}
607
611
608
612
static void
609
613
stackprof_job_handler (void * data )
610
614
{
611
- static int in_signal_handler = 0 ;
612
- if (in_signal_handler ) return ;
613
615
if (!_stackprof .running ) return ;
614
616
615
- in_signal_handler ++ ;
616
617
stackprof_record_sample ();
617
- in_signal_handler -- ;
618
618
}
619
619
620
620
static void
621
621
stackprof_signal_handler (int sig , siginfo_t * sinfo , void * ucontext )
622
622
{
623
+ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER ;
624
+
623
625
_stackprof .overall_signals ++ ;
626
+
627
+ if (!_stackprof .running ) return ;
628
+ if (!ruby_native_thread_p ()) return ;
629
+ if (pthread_mutex_trylock (& lock )) return ;
630
+
624
631
if (!_stackprof .ignore_gc && rb_during_gc ()) {
625
632
VALUE mode = rb_gc_latest_gc_info (sym_state );
626
633
if (mode == sym_marking ) {
@@ -631,8 +638,13 @@ stackprof_signal_handler(int sig, siginfo_t *sinfo, void *ucontext)
631
638
_stackprof .unrecorded_gc_samples ++ ;
632
639
rb_postponed_job_register_one (0 , stackprof_gc_job_handler , (void * )0 );
633
640
} else {
641
+ #ifdef USE_POSTPONED_JOB
634
642
rb_postponed_job_register_one (0 , stackprof_job_handler , (void * )0 );
643
+ #else
644
+ stackprof_job_handler (0 );
645
+ #endif
635
646
}
647
+ pthread_mutex_unlock (& lock );
636
648
}
637
649
638
650
static void
0 commit comments