X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Fpthread_pause.c;h=e11a0531e3d645496da4822a9a828c8a046f096f;hb=86166123aef3c81e6e05cd6dcdc73ea93b5775a1;hp=6df2e3a83c5f51bedecc93b2cde41ed615d3bc08;hpb=24286ba9a7ae3a8c6f5eefe9455e18bd758e742b;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index 6df2e3a..e11a053 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -7,6 +7,8 @@ #include #include //#include +#include +#include //#include void pthread_pause_handler() { @@ -25,6 +27,9 @@ void pthread_pause_handler() { } void pthread_pause_enable() { + //Add thread to internal registry + pthread_user_data_internal(pthread_self()); + //Nesting signals too deep is not good for stack //You can get runtime stats using following command: //grep -i sig /proc/$(pgrep binary)/status @@ -42,6 +47,9 @@ void pthread_pause_enable() { } void pthread_pause_disable() { + //Add thread to internal registry + pthread_user_data_internal(pthread_self()); + //Block signal sigset_t sigset; sigemptyset(&sigset); @@ -49,16 +57,69 @@ 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); + while(pthread_sigqueue(thread, PTHREAD_XSIG_STOP, (const union sigval){.sival_ptr=NULL}) == EAGAIN) usleep(1000); + return 0; +} + +int pthread_extra_yield() { + //Yield to both schedulers + pthread_pause_reschedule(pthread_self()); + return pthread_yield(); +} + int pthread_pause(pthread_t thread) { //Set thread as paused and notify it via signal (wait when queue full) pthread_user_data_internal(thread)->running = 0; - while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + 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_internal(thread)->running = 1; - while(pthread_kill(thread, PTHREAD_XSIG_STOP) == EAGAIN) usleep(1000); + pthread_pause_reschedule(thread); return 0; } + + + +// Wrappers /////////////////////////////////////////////////////////// + + +typedef struct pthread_extra_wrapper_t { + void *(*start_routine)(void *); + void *restrict arg; +} pthread_extra_wrapper_t; + +void *pthread_extra_thread_wrapper(void *arg) { + pthread_extra_wrapper_t task = *((pthread_extra_wrapper_t*)arg); + free(arg); + + //Register new thread to user data structure + pthread_user_data_internal(pthread_self()); + + //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()); + + return task.start_routine(task.arg); + + pthread_cleanup_pop(1); //Needed by pthread_cleanup_push() macro +} + +int pthread_extra_create(pthread_t *restrict thread, + const pthread_attr_t *restrict attr, + void *(*start_routine)(void *), + void *restrict arg) { + + pthread_extra_wrapper_t *task = malloc(sizeof(pthread_extra_wrapper_t)); + assert(task != NULL); + task->start_routine=start_routine; + task->arg=arg; + return pthread_create(thread, attr, pthread_extra_thread_wrapper, task); +}