X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Fpthread_pause.c;h=4107b38004fac9c2cb1686b91ff546d157ca7518;hb=70bd1c94dee46dac0885831ccce3d1840388656e;hp=d168121f20e6fb986440380865beb5bbe2cb2867;hpb=8df6da88c80f10cb96ed53233ce6c4d414368059;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index d168121..4107b38 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -6,12 +6,21 @@ #include #include #include +//#include //#include #include #include //#include -void pthread_pause_handler() { +///When this variable is nonzero, only referenced thread is allowed to run +///Access has to be protected by pthread_user_data_lock() +pthread_t pthread_pause_holder = 0; + +void pthread_pause_handler(const int signal, siginfo_t *info, void *ptr) { + (void)signal; (void)info; (void)ptr; + int run = info->si_value.sival_int; + //(void)td; + //Do nothing when there are more signals pending (to cleanup the queue) sigset_t pending; sigpending(&pending); @@ -21,14 +30,18 @@ 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(!run) { sigsuspend(&sigset); } } void pthread_pause_enable() { //Add thread to internal registry - pthread_user_data_internal(pthread_self()); + //pthread_user_data_internal(pthread_self()); //Nesting signals too deep is not good for stack //You can get runtime stats using following command: @@ -36,19 +49,28 @@ 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); } void pthread_pause_disable() { //Add thread to internal registry - pthread_user_data_internal(pthread_self()); + //pthread_user_data_internal(pthread_self()); //Block signal sigset_t sigset; @@ -57,9 +79,35 @@ void pthread_pause_disable() { pthread_sigmask(SIG_BLOCK, &sigset, NULL); } +/* int pthread_pause_reschedule(pthread_t thread) { //Send signal to initiate pause handler - while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + //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=pthread_user_data_internal(thread)} + ) == EAGAIN) usleep(1000); + return 0; +} +*/ + +int pthread_pause_reschedule(pthread_t thread) { + //Decide if the thread should run + + pthread_user_data_lock(); + //Check if thread has running flag + int run = (pthread_user_data_internal(thread)->running); + //Check if privileged (single thread) mode is active + if((pthread_pause_holder != 0) && !pthread_equal(pthread_pause_holder, thread)) { + run = 0; + } + pthread_user_data_unlock(); + + //Send signal to initiate pause handler (keep trying while SigQueue is full) + //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP, + (const union sigval){.sival_int=run} + ) == EAGAIN) usleep(1000); return 0; } @@ -71,18 +119,39 @@ int pthread_extra_yield() { int pthread_pause(pthread_t thread) { //Set thread as paused and notify it via signal (wait when queue full) + pthread_user_data_lock(); pthread_user_data_internal(thread)->running = 0; + pthread_user_data_unlock(); pthread_pause_reschedule(thread); return 0; } int pthread_unpause(pthread_t thread) { //Set thread as running and notify it via signal (wait when queue full) + pthread_user_data_lock(); pthread_user_data_internal(thread)->running = 1; + pthread_user_data_unlock(); pthread_pause_reschedule(thread); return 0; } +int pthread_pause_all() { + pthread_user_data_lock(); + if(pthread_pause_holder!=0) assert(pthread_equal(pthread_pause_holder, pthread_self())); + pthread_pause_holder = pthread_self(); + pthread_user_data_unlock(); + //todo reschedule all + return 0; +} + +int pthread_unpause_all() { + pthread_user_data_lock(); + if(pthread_pause_holder!=0) assert(pthread_equal(pthread_pause_holder, pthread_self())); + pthread_pause_holder = 0; + pthread_user_data_unlock(); + //todo reschedule + return 0; +} // Wrappers /////////////////////////////////////////////////////////// @@ -104,8 +173,10 @@ void *pthread_extra_thread_wrapper(void *arg) { pthread_cleanup_push(pthread_user_data_cleanup, (void *)pthread_self()); //Check if we should be running according to pthread_pause sub-scheduler - pthread_pause_reschedule(pthread_self()); + //pthread_pause_reschedule(pthread_self()); + pthread_extra_yield(); + //Run actual task return task.start_routine(task.arg); pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro