X-Git-Url: https://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Fpthread_user_data.c;h=e0ba979be0c90751aac8e92429e31f162d6290d3;hb=238a70320639c49bc44c74588fd94cd435dfa4c3;hp=26204a870aa03c55d1d7499266ccc827f10cc4df;hpb=dec91d377d95ff0388a1d2297bb9c88ea076bd78;p=mirrors%2FPrograms.git 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