Commit | Line | Data |
---|---|---|
88342701 TM |
1 | #define __PTHREAD_EXTRA_INTERNAL |
2 | ||
7e0f3dba | 3 | #include <assert.h> |
88342701 TM |
4 | #include <pthread.h> |
5 | #include <pthread_extra.h> | |
6 | ||
7 | //Static array with user data for all thread handles | |
8 | //TODO: perhaps use something more sophisticated like linked list? | |
9 | pthread_user_data_internal_t pthread_user_data[PTHREAD_XTHREADS_MAX+1] = {{.tid=PTHREAD_XNULL}}; | |
10 | ||
11 | //Get pointer to internal record tied to specified thread | |
12 | pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) { | |
13 | //Return NULL if requested thread handle is NULL | |
14 | if(pthread_equal(thread, PTHREAD_XNULL)) return NULL; | |
15 | ||
16 | //Find if the thread is already registered, add it if not | |
17 | //FIXME: recycle slots of destroyed threads!!! | |
18 | pthread_t i; | |
19 | for(i = 0; i<PTHREAD_XTHREADS_MAX; i++) { | |
20 | if(pthread_equal(pthread_user_data[i].tid, PTHREAD_XNULL)) { | |
7e0f3dba | 21 | //FIXME: Should be locking for addition to the array!!!! |
88342701 | 22 | pthread_user_data[i+1].tid = PTHREAD_XNULL; |
7e0f3dba | 23 | pthread_user_data[i].tid = thread; |
88342701 TM |
24 | break; |
25 | } | |
26 | if(pthread_equal(pthread_user_data[i].tid, thread)) break; | |
27 | } | |
28 | //Return pointer | |
29 | return &pthread_user_data[i]; | |
30 | } | |
31 | ||
32 | //Get pointer to user specified pointer of that thread | |
33 | void** pthread_user_data_ptr(pthread_t thread) { | |
34 | return &pthread_user_data_internal(thread)->usr; | |
35 | } | |
36 | ||
37 | //Set user specified pointer for thread | |
38 | void pthread_user_data_set(pthread_t thread, void *usr) { | |
39 | *(pthread_user_data_ptr(thread)) = usr; | |
40 | } | |
41 | ||
42 | //Get user specified pointer for thread | |
43 | void* pthread_user_data_get(pthread_t thread) { | |
44 | return *(pthread_user_data_ptr(thread)); | |
45 | } | |
7e0f3dba TM |
46 | |
47 | //Remove canceled thread from user data registry | |
48 | int pthread_user_data_remove(pthread_t thread) { | |
49 | //FIXME: not implemented yet! | |
50 | (void) thread; | |
51 | return 0; | |
52 | } | |
53 | ||
54 | //User data cleanup handler to be registered with pthread_cleanup_push() | |
55 | void pthread_user_data_cleanup( void * arg ) { | |
56 | pthread_t t = (pthread_t)arg; | |
57 | assert(pthread_equal(t, pthread_self()) && "Pthread_self() is not working in cleanup context!"); | |
58 | pthread_user_data_remove(t); | |
59 | } |