From: Tomas Mudrunka Date: Wed, 30 Jun 2021 14:09:32 +0000 (+0200) Subject: Nepracujem s dynamickym polem z handleru signalu X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=dec91d377d95ff0388a1d2297bb9c88ea076bd78;p=mirrors%2FPrograms.git Nepracujem s dynamickym polem z handleru signalu --- diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h index 58a98e3..ca5514d 100644 --- a/c/pthread_extra/pthread_extra.h +++ b/c/pthread_extra/pthread_extra.h @@ -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); diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index e11a053..409d570 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -6,12 +6,17 @@ #include #include #include +//#include //#include #include #include //#include -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; }