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