From: Tomas Mudrunka Date: Wed, 30 Jun 2021 14:36:43 +0000 (+0200) Subject: Thread registry is now protected by mutex X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=238a70320639c49bc44c74588fd94cd435dfa4c3;p=mirrors%2FPrograms.git Thread registry is now protected by mutex --- diff --git a/c/pthread_extra/pthread_extra.h b/c/pthread_extra/pthread_extra.h index ca5514d..efba75e 100644 --- a/c/pthread_extra/pthread_extra.h +++ b/c/pthread_extra/pthread_extra.h @@ -45,6 +45,7 @@ int pthread_extra_create(pthread_t *restrict thread, void pthread_unpause_handler(); void pthread_pause_handler(); void pthread_pause_enable(); +void pthread_pause_disable(); int pthread_pause(pthread_t thread); int pthread_unpause(pthread_t thread); int pthread_pause_reschedule(pthread_t thread); diff --git a/c/pthread_extra/pthread_pause.c b/c/pthread_extra/pthread_pause.c index 409d570..c6b96f6 100644 --- a/c/pthread_extra/pthread_pause.c +++ b/c/pthread_extra/pthread_pause.c @@ -37,7 +37,7 @@ void pthread_pause_handler(const int signal, siginfo_t *info, void *ptr) { 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: @@ -66,7 +66,7 @@ void pthread_pause_enable() { 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; diff --git a/c/pthread_extra/pthread_user_data.c b/c/pthread_extra/pthread_user_data.c index 26204a8..e0ba979 100644 --- a/c/pthread_extra/pthread_user_data.c +++ b/c/pthread_extra/pthread_user_data.c @@ -7,24 +7,43 @@ //Static array with user data for all thread handles //TODO: perhaps use something more sophisticated like linked list? pthread_user_data_internal_t pthread_user_data[PTHREAD_XTHREADS_MAX+1] = {{.tid=PTHREAD_XNULL}}; +pthread_mutex_t pthread_user_data_mutex = PTHREAD_MUTEX_INITIALIZER; + +int pthread_user_data_lock() { + pthread_pause_disable(); + return pthread_mutex_lock(&pthread_user_data_mutex); +} + +int pthread_user_data_unlock() { + pthread_mutex_unlock(&pthread_user_data_mutex); + pthread_pause_enable(); + return 0; +} //Get pointer to internal record tied to specified thread pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) { //Return NULL if requested thread handle is NULL if(pthread_equal(thread, PTHREAD_XNULL)) return NULL; + //Lock + pthread_user_data_lock(); //TODO: maybe use R/W locking scheme? + //Find if the thread is already registered, add it if not - //FIXME: recycle slots of destroyed threads!!! + //FIXME: detect overflow of array and cause assert fail! + //FIXME: recycle slots of destroyed threads!!! signaled using pthread_user_data_remove(); pthread_t i; for(i = 0; i