Exclusive mode seems to work without deadlocks
[mirrors/Programs.git] / c / pthread_extra / pthread_user_data.c
1 #define __PTHREAD_EXTRA_INTERNAL
2
3 //#include <stdio.h>
4 #include <assert.h>
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}};
11 pthread_mutex_t pthread_user_data_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
12
13 int pthread_user_data_lock() {
14 //pthread_pause_disable();
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);
20 //pthread_pause_enable();
21 return 0;
22 }
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
29 //Lock
30 pthread_user_data_lock(); //TODO: maybe use R/W locking scheme?
31
32 //Find if the thread is already registered, add it if not
33 //FIXME: detect overflow of array and cause assert fail!
34 //FIXME: recycle slots of destroyed threads!!! signaled using pthread_user_data_remove();
35 pthread_t i;
36 for(i = 0; i<PTHREAD_XTHREADS_MAX; i++) {
37 if(pthread_equal(pthread_user_data[i].tid, PTHREAD_XNULL)) {
38 pthread_user_data[i+1].tid = PTHREAD_XNULL;
39 pthread_user_data[i].tid = thread;
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);
42 break;
43 }
44 if(pthread_equal(pthread_user_data[i].tid, thread)) break;
45 }
46
47 //UnLock
48 pthread_user_data_unlock();
49
50 //Return pointer
51 return &pthread_user_data[i];
52 }
53
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
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 }
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 }
This page took 0.337991 seconds and 4 git commands to generate.