rt_task_unblock() POSIX alternative

Richard Weinberger richard.weinberger at gmail.com
Thu Apr 9 20:48:31 CEST 2020


On Thu, Apr 9, 2020 at 8:10 PM Petr ńĆervenka <grugh at centrum.cz> wrote:
>
> > Don't use signal().
> > Use sigaction() and *don't* set SA_RESTART. Maybe read() is always restarted...
>
>
>
> Thank you for trying to help me.
>
> But with the sigaction (without SA_RESTART) the behavior is completely the same:
>
> Only Ctrl+C or kill(0, SIGINT) are able to invoke the signalHandler.
> The read() is not interrupted (maybe syscall restart in some inner cycle).
>
> Example of the signal handler setup:
>
>     // Setup signal handler
>     struct sigaction act;
>     memset(&act, 0, sizeof (act));
>     act.sa_handler = signalHandler;
>     sigaction(SIGINT, &act, NULL);
>     sigaction(SIGTERM, &act, NULL);

I gave your program a try.
With this changes it works (at least how I understand your test case).

Task started
Set period: 5s
Task livind: 9.9e-05s
Waiting for join
Signal handler: 41
Signal handler end
Task livind: 0.999s
Task ended

diff --git a/read_intr.cpp b/read_intr.cpp
index 722f69c..1c5ef53 100644
--- a/read_intr.cpp
+++ b/read_intr.cpp
@@ -21,14 +21,10 @@ static pthread_t task;
 static int timer_fd;
 static bool end = false;

-void signalHandler(int signal) {
+void signalHandler(int signal, siginfo_t *info, void *ctx) {
     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");
 }

@@ -50,9 +46,6 @@ static void *taskHandler(void *cookie) {

     printf("Task started\n");

-    signal(SIGINT, signalHandler);
-    signal(SIGTERM, signalHandler);
-
     // Get current time
     struct timespec start;
     clock_gettime(CLOCK_MONOTONIC, &start);
@@ -97,13 +90,15 @@ static void *taskHandler(void *cookie) {
 int main(int argc, char** argv) {
     unsigned long overruns;
     int err;
+    struct sigaction sa = {0};

     mlockall(MCL_CURRENT | MCL_FUTURE);

     // Setup signal handler
-    signal(SIGINT, signalHandler);
-    signal(SIGTERM, signalHandler);
-
+    sa.sa_flags = 0;
+    sigemptyset(&sa.sa_mask);
+    sa.sa_sigaction = signalHandler;
+    sigaction(SIGRTMIN + 7, &sa, NULL);
     // Get current process ID
     mainTask_pid = getpid();

@@ -140,11 +135,11 @@ int main(int argc, char** argv) {
     usleep(SLEEP / 1000);

     // Call of signal handler
-    err = kill(0, SIGINT);  // THE ONLY ONE WHICH WORKS !!!
+    //err = kill(0, SIGINT);  // THE ONLY ONE WHICH WORKS !!!
     //err = kill(getpid(), SIGINT);
     //err = kill(mainTask_pid, SIGINT);
     //err = pthread_kill(pthread_self(), SIGINT);
-    //err = pthread_kill(task, SIGINT);
+    err = pthread_kill(task, SIGRTMIN + 7);
     if (err != 0) {
         printf("pthread_kill failed: %d (%s)\n", err, strerror(-err));
     }


-- 
Thanks,
//richard



More information about the Xenomai mailing list