Nepracujem s dynamickym polem z handleru signalu
authorTomas Mudrunka <tomas@mudrunka.cz>
Wed, 30 Jun 2021 14:09:32 +0000 (16:09 +0200)
committerTomas Mudrunka <tomas@mudrunka.cz>
Wed, 30 Jun 2021 14:09:32 +0000 (16:09 +0200)
c/pthread_extra/pthread_extra.h
c/pthread_extra/pthread_pause.c

index 58a98e368750d356664e09395ae89c2b2dc64d19..ca5514d26f6bf04992b88f6b11c54b36e33d032f 100644 (file)
@@ -18,8 +18,9 @@
 #ifdef __PTHREAD_EXTRA_INTERNAL
 typedef struct pthread_user_data_internal_t {
        pthread_t tid; //Thread ID
-       sig_atomic_t running; //Internaly used by pthread_pause
        void *usr; //User pointer
+       //Internaly used members:
+       sig_atomic_t running; //pthread_pause
 } pthread_user_data_internal_t;
 
 pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread);
index e11a0531e3d645496da4822a9a828c8a046f096f..409d57038ab8d6b69c7233e6ddffedbce879c079 100644 (file)
@@ -6,12 +6,17 @@
 #include <errno.h>
 #include <unistd.h>
 #include <sys/resource.h>
+//#include <sys/siginfo.h>
 //#include <stdio.h>
 #include <stdlib.h>
 #include <assert.h>
 //#include <sys/time.h>
 
-void pthread_pause_handler() {
+void pthread_pause_handler(const int signal, siginfo_t *info, void *ptr) {
+       (void)signal; (void)info; (void)ptr;
+       pthread_user_data_internal_t *td = (pthread_user_data_internal_t *)(info->si_value.sival_ptr);
+       (void)td;
+
        //Do nothing when there are more signals pending (to cleanup the queue)
        sigset_t pending;
        sigpending(&pending);
@@ -21,7 +26,11 @@ void pthread_pause_handler() {
        sigset_t sigset;
        sigfillset(&sigset);
        sigdelset(&sigset, PTHREAD_XSIG_STOP);
-       if(!pthread_user_data_internal(pthread_self())->running) {
+
+       //printf("RCV: %p = %p\n", (void *)pthread_user_data_internal(pthread_self()), (void *)td);
+
+       //if(!pthread_user_data_internal(pthread_self())->running) {
+       if(!td->running) {
                sigsuspend(&sigset);
        }
 }
@@ -36,13 +45,22 @@ void pthread_pause_enable() {
        struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32};
        setrlimit(RLIMIT_SIGPENDING, &sigq);
 
-       //Setup signal handler
-       signal(PTHREAD_XSIG_STOP, pthread_pause_handler);
-
-       //Unblock signal
+       //Prepare signal mask
        sigset_t sigset;
        sigemptyset(&sigset);
        sigaddset(&sigset, PTHREAD_XSIG_STOP);
+
+       //Setup signal handler
+       //signal(PTHREAD_XSIG_STOP, pthread_pause_handler);
+       const struct sigaction pause_sa = {
+               .sa_sigaction = pthread_pause_handler,
+               .sa_mask = sigset,
+               .sa_flags = SA_SIGINFO,
+               .sa_restorer = NULL
+       };
+       sigaction(PTHREAD_XSIG_STOP, &pause_sa, NULL);
+
+       //Unblock signal
        pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
 }
 
@@ -59,8 +77,11 @@ void pthread_pause_disable() {
 
 int pthread_pause_reschedule(pthread_t thread) {
        //Send signal to initiate pause handler
+       //printf("SND: %p\n", (void *)pthread_user_data_internal(thread));
        //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000);
-       while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP, (const union sigval){.sival_ptr=NULL}) == EAGAIN) usleep(1000);
+       while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP,
+               (const union sigval){.sival_ptr=pthread_user_data_internal(thread)}
+               ) == EAGAIN) usleep(1000);
        return 0;
 }
 
This page took 0.197465 seconds and 4 git commands to generate.