From: Tomas Mudrunka Date: Fri, 2 Jul 2021 09:25:37 +0000 (+0200) Subject: pthread_pause now more robust with sleep restart, SA_RESTART and semaphores X-Git-Url: http://git.harvie.cz/?p=mirrors%2FPrograms.git;a=commitdiff_plain;h=75e2456441dd2360b17b5e08022e91f8bb9331df pthread_pause now more robust with sleep restart, SA_RESTART and semaphores --- diff --git a/c/pthread_pause.c b/c/pthread_pause.c index 90282a2..e5f46b1 100644 --- a/c/pthread_pause.c +++ b/c/pthread_pause.c @@ -6,29 +6,67 @@ //I've wrote this code as excercise to solve following stack overflow question: // https://stackoverflow.com/questions/9397068/how-to-pause-a-pthread-any-time-i-want/68119116#68119116 +#define _GNU_SOURCE //pthread_yield() needs this #include #include //#include +#include #include #include #include #include #include #include +#include #define PTHREAD_XSIG_STOP (SIGRTMIN+0) #define PTHREAD_XSIG_CONT (SIGRTMIN+1) #define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal pthread_t main_thread; +sem_t pthread_pause_sem; + +void pthread_nanosleep(struct timespec t) { + //Sleep calls on Linux get interrupted by signals, causing premature wake + //Pthread (un)pause is built using signals + //Therefore we need self-restarting sleep implementation + //IO timeouts are restarted by SA_RESTART, but sleeps do need explicit restart + while(nanosleep(&t, &t)) if(errno!=EINTR) break; + return; +} + +void pthread_nsleep(time_t s, long ns) { + struct timespec t; + t.tv_sec = s; + t.tv_nsec = ns; + pthread_nanosleep(t); +} + +void pthread_sleep(time_t s) { + pthread_nsleep(s, 0); +} + +void pthread_pause_yield() { + //Call this to give other threads chance to run + sem_wait(&pthread_pause_sem); + sem_post(&pthread_pause_sem); + //usleep(0); + //nanosleep(&((const struct timespec){.tv_sec=0,.tv_nsec=1}), NULL); + pthread_nsleep(0,1); //pthread_yield() is not enough, so we use sleep + pthread_yield(); +} void pthread_pause_handler(int signal) { //Do nothing when there are more signals pending (to cleanup the queue) + //This is no longer needed, since we use semaphore to limit pending signals + /* sigset_t pending; sigpending(&pending); if(sigismember(&pending, PTHREAD_XSIG_STOP)) return; if(sigismember(&pending, PTHREAD_XSIG_CONT)) return; + */ + sem_post(&pthread_pause_sem); if(signal == PTHREAD_XSIG_STOP) { sigset_t sigset; sigfillset(&sigset); @@ -46,15 +84,26 @@ void pthread_pause_enable() { struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32}; setrlimit(RLIMIT_SIGPENDING, &sigq); - //Register signal handlers - signal(PTHREAD_XSIG_STOP, pthread_pause_handler); - signal(PTHREAD_XSIG_CONT, pthread_pause_handler); - - //UnBlock signals + //Prepare sigset sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, PTHREAD_XSIG_STOP); sigaddset(&sigset, PTHREAD_XSIG_CONT); + + //Register signal handlers + //signal(PTHREAD_XSIG_STOP, pthread_pause_handler); + //signal(PTHREAD_XSIG_CONT, pthread_pause_handler); + //We now use sigaction() instead of signal(), because it supports SA_RESTART + const struct sigaction pause_sa = { + .sa_handler = pthread_pause_handler, + .sa_mask = sigset, + .sa_flags = SA_RESTART, + .sa_restorer = NULL + }; + sigaction(PTHREAD_XSIG_STOP, &pause_sa, NULL); + sigaction(PTHREAD_XSIG_CONT, &pause_sa, NULL); + + //UnBlock signals pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); } @@ -63,24 +112,33 @@ void pthread_pause_disable() { //Eg.: locking mutex, calling printf() which has internal mutex, etc... //After unlocking mutex, you can enable pause again. + //Make sure all signals are dispatched before we block them + sem_wait(&pthread_pause_sem); + //Block signals sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, PTHREAD_XSIG_STOP); sigaddset(&sigset, PTHREAD_XSIG_CONT); pthread_sigmask(SIG_BLOCK, &sigset, NULL); + + sem_post(&pthread_pause_sem); } int pthread_pause(pthread_t thread) { + sem_wait(&pthread_pause_sem); //If signal queue is full, we keep retrying while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + pthread_pause_yield(); return 0; } int pthread_unpause(pthread_t thread) { + sem_wait(&pthread_pause_sem); //If signal queue is full, we keep retrying while(pthread_kill(thread, PTHREAD_XSIG_CONT) == EAGAIN) usleep(1000); + pthread_pause_yield(); return 0; } @@ -88,7 +146,6 @@ void *thread_test() { //Whole process dies if you kill thread immediately before it is pausable //pthread_pause_enable(); while(1) { - usleep(1000*300); //Printf() is not async signal safe (because it holds internal mutex), //you should call it only with pause disabled! //Will throw helgrind warnings anyway, not sure why... @@ -96,27 +153,44 @@ void *thread_test() { pthread_pause_disable(); printf("Running!\n"); pthread_pause_enable(); - //pthread_pause(main_thread); - //pthread_unpause(main_thread); + + //Pausing main thread should not cause deadlock + //We pause main thread here just to test it is OK + pthread_pause(main_thread); + pthread_nsleep(0, 1000*1000); + pthread_unpause(main_thread); + + //Wait for a while + pthread_nsleep(0, 1000*1000*100); + pthread_unpause(main_thread); } } int main() { pthread_t t; + sem_init(&pthread_pause_sem, 0, 1); //TODO: hide somewhere using pthred_once() main_thread = pthread_self(); pthread_pause_enable(); //Will get inherited by all threads from now on //you need to call pthread_pause_enable (or disable) before creating threads, - //otherwise first signal will kill whole process + //otherwise first (un)pause signal will kill whole process pthread_create(&t, NULL, thread_test, NULL); while(1) { pthread_pause(t); printf("PAUSED\n"); - sleep(3); + pthread_sleep(3); printf("UNPAUSED\n"); pthread_unpause(t); - sleep(1); + pthread_sleep(1); + + /* + pthread_pause_disable(); + printf("RUNNING!\n"); + pthread_pause_enable(); + */ + pthread_pause(t); + pthread_unpause(t); } pthread_join(t, NULL);