rt_task_unblock() POSIX alternative

Richard Weinberger richard.weinberger at gmail.com
Thu Apr 9 18:13:37 CEST 2020


On Thu, Apr 9, 2020 at 5:00 PM Petr Červenka <grugh at centrum.cz> wrote:
>
> > BTW:
> > Why do you think nothing can interrupt a read() from a timerfd?
> > If read() blocks on a timerfd the calling thread will be put into
> > interruptable sleep.
>
> "I" was not able to send a signal to the task and interrupt it.
> Here is an example. Please let me know, how to improve it to interrupt the read() before its timeout:
>
> #include <math.h>
> #include <pthread.h>
> #include <stdint.h>
> #include <signal.h>
> #include <stdio.h>
> #include <string.h>
> #include <sys/mman.h>
> #include <sys/timerfd.h>
> #include <unistd.h>
>
> #define AUTO_KILL
>
> #define SECOND  1000000000l     // ns
> #define PERIOD  (5 * SECOND)    // ns
> #define SLEEP   (1 * SECOND)    // ns
>
> using namespace std;
>
> static pid_t mainTask_pid;
> static pthread_t task;
> static int timer_fd;
> static bool end = false;
>
> void signalHandler(int signal) {
>     int err;
>     printf("Signal handler: %d\n", signal);
>     end = true;
>     err = pthread_kill(task, SIGINT);
>     if (err != 0) {
>         printf("pthread_kill failed: %d (%s)\n", err, strerror(-err));
>     }
>     printf("Signal handler end\n");
> }
>
> void timediff (struct timespec *result, const struct timespec *t1, const struct timespec *t2) {
>     result->tv_sec = t2->tv_sec - t1->tv_sec;
>     result->tv_nsec = t2->tv_nsec - t1->tv_nsec;
>     if (result->tv_nsec >= SECOND) {
>         result->tv_nsec -= SECOND;
>         result->tv_sec++;
>     }
>     if (result->tv_nsec < 0) {
>         result->tv_nsec += SECOND;
>         result->tv_sec--;
>     }
> }
>
> static void *taskHandler(void *cookie) {
>     int err;
>
>     printf("Task started\n");
>
>     signal(SIGINT, signalHandler);
>     signal(SIGTERM, signalHandler);

Don't use signal().
Use sigaction() and *don't* set SA_RESTART. Maybe read() is always restarted...

-- 
Thanks,
//richard



More information about the Xenomai mailing list