| 1 | #define __PTHREAD_EXTRA_INTERNAL |
| 2 | |
| 3 | #include <pthread.h> |
| 4 | #include <pthread_extra.h> |
| 5 | |
| 6 | //Static array with user data for all thread handles |
| 7 | //TODO: perhaps use something more sophisticated like linked list? |
| 8 | pthread_user_data_internal_t pthread_user_data[PTHREAD_XTHREADS_MAX+1] = {{.tid=PTHREAD_XNULL}}; |
| 9 | |
| 10 | //Get pointer to internal record tied to specified thread |
| 11 | pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) { |
| 12 | //Return NULL if requested thread handle is NULL |
| 13 | if(pthread_equal(thread, PTHREAD_XNULL)) return NULL; |
| 14 | |
| 15 | //Find if the thread is already registered, add it if not |
| 16 | //FIXME: recycle slots of destroyed threads!!! |
| 17 | pthread_t i; |
| 18 | for(i = 0; i<PTHREAD_XTHREADS_MAX; i++) { |
| 19 | if(pthread_equal(pthread_user_data[i].tid, PTHREAD_XNULL)) { |
| 20 | pthread_user_data[i].tid = thread; |
| 21 | pthread_user_data[i+1].tid = PTHREAD_XNULL; |
| 22 | break; |
| 23 | } |
| 24 | if(pthread_equal(pthread_user_data[i].tid, thread)) break; |
| 25 | } |
| 26 | //Return pointer |
| 27 | return &pthread_user_data[i]; |
| 28 | } |
| 29 | |
| 30 | //Get pointer to user specified pointer of that thread |
| 31 | void** pthread_user_data_ptr(pthread_t thread) { |
| 32 | return &pthread_user_data_internal(thread)->usr; |
| 33 | } |
| 34 | |
| 35 | //Set user specified pointer for thread |
| 36 | void pthread_user_data_set(pthread_t thread, void *usr) { |
| 37 | *(pthread_user_data_ptr(thread)) = usr; |
| 38 | } |
| 39 | |
| 40 | //Get user specified pointer for thread |
| 41 | void* pthread_user_data_get(pthread_t thread) { |
| 42 | return *(pthread_user_data_ptr(thread)); |
| 43 | } |