On linux, I have a setup where I spawn off a profiler thread that blasts its parent thread with SIGPROF signals for a sampling profiler. I send the signal explicitly rather than relying on setitimer for better control. I may need to reconsider that, but for now assume I'm sending the signals myself.
My problem is that this sends signals all the time, even when the profiled thread is sleeping in a syscall. I get underruns on my sample ring buffer this way.
Is there some way to detect the run state of the profiled thread, even if it might change immediately after I check it? Or better yet, send a signal conditional on the target being in a running state?
I could sort of fake it by atomically incrementing a counter in the profiled thread's signal handler and checking whether it's changed from the profiler thread:
volatile int samples; while(1) { int old_samples = samples; pthread_kill(profiled_thread, SIGPROF); while (samples == old_samples) usleep(appropriate amount); }
or something, but I'm wondering if there's a better way. (The above code simplifies the delay calculation, since really the whole point is to have unbiased samples, but ignore that for now.)