X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Fpthread_pause.c;h=950d6fa26954ffdbf5d42ad9fcec3a27fd3e860b;hb=46a7758b58eeff9b507e6e173b03b7fd909e22c3;hp=c6b96f6d8a0518907c477971c7f63da366cabaa7;hpb=238a70320639c49bc44c74588fd94cd435dfa4c3;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index c6b96f6..950d6fa 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -12,38 +13,47 @@ #include //#include +//Mutex that ensures proper serialization of (un)pause calls +//pthread_mutex_t pthread_pause_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; + +//Semaphore that ensures proper serialization of (un)pause signals +sem_t pthread_pause_sem; +//Once control to init the semaphore (and possibly other stuff) +pthread_once_t pthread_pause_once_ctrl = PTHREAD_ONCE_INIT; +void pthread_pause_once(void) { + sem_init(&pthread_pause_sem, 0, 1); +} +void pthread_pause_init() { pthread_once(&pthread_pause_once_ctrl, &pthread_pause_once); } + +///When this variable is nonzero, only referenced thread is allowed to run +///Access has to be protected by pthread_user_data_lock() and pthread_pause_sem; +pthread_t pthread_pause_holder = PTHREAD_XNULL; + 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; + 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); - if(sigismember(&pending, PTHREAD_XSIG_STOP)) return; + //Post semaphore to confirm that signal is handled + sem_post(&pthread_pause_sem); //Keep waiting for signals until we are supposed to be running - sigset_t sigset; - sigfillset(&sigset); - sigdelset(&sigset, PTHREAD_XSIG_STOP); - - //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) { + if(!run) { + sigset_t sigset; + sigfillset(&sigset); + sigdelset(&sigset, PTHREAD_XSIG_STOP); sigsuspend(&sigset); } } void pthread_pause_enable() { - //Add thread to internal registry - //pthread_user_data_internal(pthread_self()); + pthread_pause_init(); //Make sure semaphore is init'd //Nesting signals too deep is not good for stack //You can get runtime stats using following command: //grep -i sig /proc/$(pgrep binary)/status - struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32}; - setrlimit(RLIMIT_SIGPENDING, &sigq); + //struct rlimit sigq = {.rlim_cur = 32, .rlim_max=32}; + //setrlimit(RLIMIT_SIGPENDING, &sigq); //Prepare signal mask sigset_t sigset; @@ -55,26 +65,41 @@ void pthread_pause_enable() { const struct sigaction pause_sa = { .sa_sigaction = pthread_pause_handler, .sa_mask = sigset, - .sa_flags = SA_SIGINFO, + .sa_flags = SA_SIGINFO | SA_RESTART, .sa_restorer = NULL }; + sigaction(PTHREAD_XSIG_STOP, &pause_sa, NULL); //Unblock signal pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); + + //Add thread to internal registry + pthread_user_data_internal(pthread_self()); //Only now, when signals are unblocked! } void pthread_pause_disable() { + //pthread_user_data_lock(); + pthread_pause_init(); //Make sure semaphore is init'd + //Add thread to internal registry - //pthread_user_data_internal(pthread_self()); + //pthread_user_data_internal(pthread_self()); //DEADLOCKS! //Block signal sigset_t sigset; sigemptyset(&sigset); sigaddset(&sigset, PTHREAD_XSIG_STOP); + + //Make sure all signals are dispatched before we block them + //Maybe not a good idea, causes DEADLOCKS! + //sem_wait(&pthread_pause_sem); pthread_sigmask(SIG_BLOCK, &sigset, NULL); + //sem_post(&pthread_pause_sem); + + //pthread_user_data_unlock(); } +/* int pthread_pause_reschedule(pthread_t thread) { //Send signal to initiate pause handler //printf("SND: %p\n", (void *)pthread_user_data_internal(thread)); @@ -84,6 +109,40 @@ int pthread_pause_reschedule(pthread_t thread) { ) == EAGAIN) usleep(1000); return 0; } +*/ + +int pthread_pause_reschedule(pthread_t thread) { + //Decide if the thread should run and signal it + pthread_user_data_lock(); + + //Wait for semaphore which means signal queue is empty + pthread_pause_init(); //Make sure semaphore is init'd + sem_wait(&pthread_pause_sem); + + //Only call this if you already acquired pthread_pause_sem semaphore!!!! + //Otherwise call pthread_pause_reschedule() + + //Check if thread has running flag + int run = (pthread_user_data_internal(thread)->running); + //Check if privileged (single thread) mode is active + if(!pthread_equal(pthread_pause_holder, PTHREAD_XNULL) && !pthread_equal(pthread_pause_holder, thread)) { + run = 0; + } + + //Send signal to initiate pause handler (keep trying while SigQueue is full) + //while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + //printf("Sched %lu = %d (self: %lu, lck: %lu)\n", thread, run, pthread_self(), pthread_pause_holder); + while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP, + (const union sigval){.sival_int=run} + ) == EAGAIN) usleep(1000); + + //Wait for signal to be delivered + sem_wait(&pthread_pause_sem); + sem_post(&pthread_pause_sem); + pthread_user_data_unlock(); + + return 0; +} int pthread_extra_yield() { //Yield to both schedulers @@ -91,20 +150,61 @@ int pthread_extra_yield() { return pthread_yield(); } +///Sanity check to be sure there are no race conditions +inline void pthread_pause_assert_owner() { + if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) + assert(pthread_equal(pthread_pause_holder, pthread_self())); +} + +///Pause specified thread (block until it is paused) int pthread_pause(pthread_t thread) { + pthread_user_data_lock(); + pthread_pause_assert_owner(); + //if(!pthread_equal(pthread_pause_holder,PTHREAD_XNULL)) assert(pthread_equal(pthread_pause_holder, pthread_self())); //Set thread as paused and notify it via signal (wait when queue full) pthread_user_data_internal(thread)->running = 0; pthread_pause_reschedule(thread); + pthread_user_data_unlock(); return 0; } +///UnPause specified thread (block until it is unpaused) int pthread_unpause(pthread_t thread) { + pthread_user_data_lock(); + pthread_pause_assert_owner(); //Set thread as running and notify it via signal (wait when queue full) pthread_user_data_internal(thread)->running = 1; pthread_pause_reschedule(thread); + pthread_user_data_unlock(); return 0; } +///Enter exclusive mode by pausing everyone else +int pthread_pause_all() { + //printf("Pause ALL\n"); + pthread_user_data_lock(); + //printf("Pause ALL+\n"); + //printf("Pause %p == %p\n", (void *)pthread_pause_holder, (void *)pthread_self()); + pthread_pause_assert_owner(); + pthread_pause_holder = pthread_self(); + pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); + //printf("Pause ALL!\n"); + pthread_user_data_unlock(); + return 0; +} + +///Leave exclusive mode by unpausing everyone else +int pthread_unpause_all() { + //printf("UnPause ALL\n"); + pthread_user_data_lock(); + //printf("UnPause ALL+\n"); + pthread_pause_assert_owner(); + pthread_pause_holder = PTHREAD_XNULL; + pthread_user_data_internal_iterate(&pthread_pause_reschedule, NULL); + //printf("UnPause ALL!\n"); + pthread_user_data_unlock(); + return 0; +} // Wrappers /////////////////////////////////////////////////////////// @@ -119,15 +219,19 @@ void *pthread_extra_thread_wrapper(void *arg) { pthread_extra_wrapper_t task = *((pthread_extra_wrapper_t*)arg); free(arg); + pthread_pause_enable(); + //Register new thread to user data structure - pthread_user_data_internal(pthread_self()); + //pthread_user_data_internal(pthread_self()); //Perhaps already done in pthread_extra_yield() and pthread_pause_enable()?? //TODO: user_data should do this automaticaly? 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