@@ -539,9 +539,13 @@ JL_DLLEXPORT int jl_profile_start_timer(void)
539
539
540
540
JL_DLLEXPORT void jl_profile_stop_timer (void )
541
541
{
542
- if (running )
542
+ if (running ) {
543
543
timer_delete (timerprof );
544
- running = 0 ;
544
+ // Because SIGUSR1 is multipurpose, care must be taken for running = 0 to be set after the timer has fully stopped.
545
+ // There may be a pending signal emitted from the timer so wait a few timer cycles
546
+ sleep_ms ((nsecprof / GIGA ) * 1000 * 3 );
547
+ running = 0 ;
548
+ }
545
549
}
546
550
547
551
#elif defined(HAVE_ITIMER )
@@ -556,18 +560,24 @@ JL_DLLEXPORT int jl_profile_start_timer(void)
556
560
timerprof .it_interval .tv_usec = 0 ;
557
561
timerprof .it_value .tv_sec = nsecprof / GIGA ;
558
562
timerprof .it_value .tv_usec = ((nsecprof % GIGA ) + 999 ) / 1000 ;
559
- if (setitimer (ITIMER_PROF , & timerprof , NULL ) == -1 )
560
- return -3 ;
563
+ // Because SIGUSR1 is multipurpose, set `running` before so that we know that the first SIGUSR1 came from the timer
561
564
running = 1 ;
565
+ if (setitimer (ITIMER_PROF , & timerprof , NULL ) == -1 ) {
566
+ running = 0 ;
567
+ return -3 ;
568
+ }
562
569
return 0 ;
563
570
}
564
571
565
572
JL_DLLEXPORT void jl_profile_stop_timer (void )
566
573
{
567
574
if (running ) {
568
- running = 0 ;
569
575
memset (& timerprof , 0 , sizeof (timerprof ));
570
576
setitimer (ITIMER_PROF , & timerprof , NULL );
577
+ // Because SIGUSR1 is multipurpose, care must be taken for running = 0 to be set after the timer has fully stopped.
578
+ // There may be a pending signal emitted from the timer so wait a few timer cycles
579
+ sleep_ms ((nsecprof / GIGA ) * 1000 * 3 );
580
+ running = 0 ;
571
581
}
572
582
}
573
583
0 commit comments