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}}; | |
70bd1c94 | 10 | pthread_mutex_t pthread_user_data_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; |
238a7032 TM |
11 | |
12 | int pthread_user_data_lock() { | |
13 | pthread_pause_disable(); | |
14 | return pthread_mutex_lock(&pthread_user_data_mutex); | |
15 | } | |
16 | ||
17 | int pthread_user_data_unlock() { | |
18 | pthread_mutex_unlock(&pthread_user_data_mutex); | |
19 | pthread_pause_enable(); | |
20 | return 0; | |
21 | } | |
88342701 TM |
22 | |
23 | //Get pointer to internal record tied to specified thread | |
24 | pthread_user_data_internal_t* pthread_user_data_internal(pthread_t thread) { | |
25 | //Return NULL if requested thread handle is NULL | |
26 | if(pthread_equal(thread, PTHREAD_XNULL)) return NULL; | |
27 | ||
238a7032 TM |
28 | //Lock |
29 | pthread_user_data_lock(); //TODO: maybe use R/W locking scheme? | |
30 | ||
88342701 | 31 | //Find if the thread is already registered, add it if not |
238a7032 TM |
32 | //FIXME: detect overflow of array and cause assert fail! |
33 | //FIXME: recycle slots of destroyed threads!!! signaled using pthread_user_data_remove(); | |
88342701 TM |
34 | pthread_t i; |
35 | for(i = 0; i<PTHREAD_XTHREADS_MAX; i++) { | |
36 | if(pthread_equal(pthread_user_data[i].tid, PTHREAD_XNULL)) { | |
88342701 | 37 | pthread_user_data[i+1].tid = PTHREAD_XNULL; |
7e0f3dba | 38 | pthread_user_data[i].tid = thread; |
88342701 TM |
39 | break; |
40 | } | |
41 | if(pthread_equal(pthread_user_data[i].tid, thread)) break; | |
42 | } | |
238a7032 TM |
43 | |
44 | //UnLock | |
45 | pthread_user_data_unlock(); | |
46 | ||
88342701 TM |
47 | //Return pointer |
48 | return &pthread_user_data[i]; | |
49 | } | |
50 | ||
9b4be8e8 TM |
51 | //Iterate specified callback over all registered threads |
52 | int pthread_user_data_internal_iterate(int (*routine)(pthread_t), void *arg) { | |
53 | (void) arg; | |
54 | int i; | |
55 | pthread_user_data_lock(); | |
56 | for(i = 0; i<PTHREAD_XTHREADS_MAX; i++) { | |
57 | if(pthread_equal(pthread_user_data[i].tid, PTHREAD_XNULL)) break; | |
58 | routine(pthread_user_data[i].tid); | |
59 | } | |
60 | pthread_user_data_unlock(); | |
61 | return i; | |
62 | } | |
63 | ||
88342701 TM |
64 | //Get pointer to user specified pointer of that thread |
65 | void** pthread_user_data_ptr(pthread_t thread) { | |
66 | return &pthread_user_data_internal(thread)->usr; | |
67 | } | |
68 | ||
69 | //Set user specified pointer for thread | |
70 | void pthread_user_data_set(pthread_t thread, void *usr) { | |
71 | *(pthread_user_data_ptr(thread)) = usr; | |
72 | } | |
73 | ||
74 | //Get user specified pointer for thread | |
75 | void* pthread_user_data_get(pthread_t thread) { | |
76 | return *(pthread_user_data_ptr(thread)); | |
77 | } | |
7e0f3dba TM |
78 | |
79 | //Remove canceled thread from user data registry | |
80 | int pthread_user_data_remove(pthread_t thread) { | |
81 | //FIXME: not implemented yet! | |
82 | (void) thread; | |
83 | return 0; | |
84 | } | |
85 | ||
86 | //User data cleanup handler to be registered with pthread_cleanup_push() | |
87 | void pthread_user_data_cleanup( void * arg ) { | |
88 | pthread_t t = (pthread_t)arg; | |
89 | assert(pthread_equal(t, pthread_self()) && "Pthread_self() is not working in cleanup context!"); | |
90 | pthread_user_data_remove(t); | |
91 | } |