Re: [PATCH v6 1/2] posix-timers: Prefer delivery of signals to the current thread

From: Oleg Nesterov
Date: Thu Apr 04 2024 - 10:56:12 EST


On 04/04, Thomas Gleixner wrote:
>
> IOW, we cannot test this reliably at all with the current approach.

Agreed!

So how about a REALLY SIMPLE test-case below?

Lacks error checking, should be updated to match tools/testing/selftests.

Without commit bcb7ee79029dca assert(sig_cnt > SIG_CNT) fails, the very
1st tick wakes the leader up.

With that commit it doesn't fail.

Oleg.

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <time.h>
#include <assert.h>

#define SIG_CNT 100
static volatile int sig_cnt;

static void alarm_func(int sig)
{
++sig_cnt;
}

static void *thread_func(void *arg)
{
// one second before the 1st tick to ensure the leader sleeps
struct itimerspec val = {
.it_value.tv_sec = 1,
.it_value.tv_nsec = 0,
.it_interval.tv_sec = 0,
.it_interval.tv_nsec = 1000 * 1000,
};
timer_t id;

timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id);
timer_settime(id, 0, &val, NULL);

while (sig_cnt < SIG_CNT)
;

// wake up the leader
kill(getpid(), SIGALRM);

return NULL;
}

int main(void)
{
pthread_t thread;

signal(SIGALRM, alarm_func);

pthread_create(&thread, NULL, thread_func, NULL);

pause();

assert(sig_cnt > SIG_CNT); // likely SIG_CNT + 1

return 0;
}