From: Tomas Mudrunka Date: Tue, 29 Jun 2021 14:23:39 +0000 (+0200) Subject: Cleanup scaffolds X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=7e0f3dba1713edfea98cdfe7909c08f68fa5f7b7;p=mirrors%2FPrograms.git Cleanup scaffolds --- diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h index faffb3d..15e4d8b 100644 --- a/c/pthread_extra/pthread_extra.h +++ b/c/pthread_extra/pthread_extra.h @@ -28,6 +28,7 @@ pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread); void** pthread_user_data_ptr(pthread_t thread); void* pthread_user_data_get(pthread_t thread); void pthread_user_data_set(pthread_t thread, void *usr); +void pthread_user_data_cleanup(void * arg); // Pausing @@ -36,11 +37,16 @@ void pthread_user_data_set(pthread_t thread, void *usr); #define PTHREAD_XSIG_CONT (SIGRTMIN+1) #define PTHREAD_XSIGRTMIN (SIGRTMIN+2) //First unused RT signal +int pthread_extra_create(pthread_t *restrict thread, + const pthread_attr_t *restrict attr, + void *(*start_routine)(void *), + void *restrict arg); void pthread_unpause_handler(); void pthread_pause_handler(); void pthread_pause_enable(); int pthread_pause(pthread_t thread); int pthread_unpause(pthread_t thread); +int pthread_pause_reschedule(pthread_t thread); // Message queues diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index 1eacdf4..c0d4350 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() { @@ -55,16 +57,62 @@ 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); + return 0; +} + 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); +} diff --git a/c/pthread_extra/pthread_user_data.c b/c/pthread_extra/pthread_user_data.c index b86c4a3..26204a8 100644 --- a/c/pthread_extra/pthread_user_data.c +++ b/c/pthread_extra/pthread_user_data.c @@ -1,5 +1,6 @@ #define __PTHREAD_EXTRA_INTERNAL +#include #include #include @@ -17,8 +18,9 @@ pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) { pthread_t i; for(i = 0; i