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