X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;ds=inline;f=c%2Fpthread_extra%2Fpthread_user_data.c;h=87055127dcd178921e68599a0c970b6f02aa2967;hb=0b8d006aa72c56eff1368c6c69b09547c455a5ed;hp=26204a870aa03c55d1d7499266ccc827f10cc4df;hpb=7e0f3dba1713edfea98cdfe7909c08f68fa5f7b7;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/pthread_user_data.c b/c/pthread_extra/pthread_user_data.c index 26204a8..8705512 100644 --- a/c/pthread_extra/pthread_user_data.c +++ b/c/pthread_extra/pthread_user_data.c @@ -1,5 +1,10 @@ #define __PTHREAD_EXTRA_INTERNAL +#ifndef _GNU_SOURCE +#define _GNU_SOURCE //PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP +#endif //_GNU_SOURCE + +//#include #include #include #include @@ -7,28 +12,63 @@ //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_RECURSIVE_MUTEX_INITIALIZER_NP; + +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; iusr;