]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - thread_pthread.c
actually fix the barrier typecasts... these really need to be volatile due to the...
[xonotic/darkplaces.git] / thread_pthread.c
1 #include "quakedef.h"
2 #include "thread.h"
3 #ifdef THREADRECURSIVE
4 #define __USE_UNIX98
5 #include <pthread.h>
6 #endif
7 #include <stdint.h>
8
9
10 int Thread_Init(void)
11 {
12         return 0;
13 }
14
15 void Thread_Shutdown(void)
16 {
17 }
18
19 qboolean Thread_HasThreads(void)
20 {
21         return true;
22 }
23
24 void *_Thread_CreateMutex(const char *filename, int fileline)
25 {
26 #ifdef THREADRECURSIVE
27         pthread_mutexattr_t    attr;
28 #endif
29         pthread_mutex_t *mutexp = (pthread_mutex_t *) Z_Malloc(sizeof(pthread_mutex_t));
30 #ifdef THREADDEBUG
31         Sys_PrintfToTerminal("%p mutex create %s:%i\n" , mutexp, filename, fileline);
32 #endif
33 #ifdef THREADRECURSIVE
34         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
35         pthread_mutex_init(mutexp, &attr);
36 #else
37         pthread_mutex_init(mutexp, NULL);
38 #endif
39         return mutexp;
40 }
41
42 void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline)
43 {
44         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
45 #ifdef THREADDEBUG
46         Sys_PrintfToTerminal("%p mutex destroy %s:%i\n", mutex, filename, fileline);
47 #endif
48         pthread_mutex_destroy(mutexp);
49         Z_Free(mutexp);
50 }
51
52 int _Thread_LockMutex(void *mutex, const char *filename, int fileline)
53 {
54         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
55 #ifdef THREADDEBUG
56         Sys_PrintfToTerminal("%p mutex lock %s:%i\n"   , mutex, filename, fileline);
57 #endif
58         return pthread_mutex_lock(mutexp);
59 }
60
61 int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline)
62 {
63         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
64 #ifdef THREADDEBUG
65         Sys_PrintfToTerminal("%p mutex unlock %s:%i\n" , mutex, filename, fileline);
66 #endif
67         return pthread_mutex_unlock(mutexp);
68 }
69
70 void *_Thread_CreateCond(const char *filename, int fileline)
71 {
72         pthread_cond_t *condp = (pthread_cond_t *) Z_Malloc(sizeof(pthread_cond_t));
73         pthread_cond_init(condp, NULL);
74 #ifdef THREADDEBUG
75         Sys_PrintfToTerminal("%p cond create %s:%i\n"   , condp, filename, fileline);
76 #endif
77         return condp;
78 }
79
80 void _Thread_DestroyCond(void *cond, const char *filename, int fileline)
81 {
82         pthread_cond_t *condp = (pthread_cond_t *) cond;
83 #ifdef THREADDEBUG
84         Sys_PrintfToTerminal("%p cond destroy %s:%i\n"   , cond, filename, fileline);
85 #endif
86         pthread_cond_destroy(condp);
87         Z_Free(condp);
88 }
89
90 int _Thread_CondSignal(void *cond, const char *filename, int fileline)
91 {
92         pthread_cond_t *condp = (pthread_cond_t *) cond;
93 #ifdef THREADDEBUG
94         Sys_PrintfToTerminal("%p cond signal %s:%i\n"   , cond, filename, fileline);
95 #endif
96         return pthread_cond_signal(condp);
97 }
98
99 int _Thread_CondBroadcast(void *cond, const char *filename, int fileline)
100 {
101         pthread_cond_t *condp = (pthread_cond_t *) cond;
102 #ifdef THREADDEBUG
103         Sys_PrintfToTerminal("%p cond broadcast %s:%i\n"   , cond, filename, fileline);
104 #endif
105         return pthread_cond_broadcast(condp);
106 }
107
108 int _Thread_CondWait(void *cond, void *mutex, const char *filename, int fileline)
109 {
110         pthread_cond_t *condp = (pthread_cond_t *) cond;
111         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
112 #ifdef THREADDEBUG
113         Sys_PrintfToTerminal("%p cond wait %s:%i\n"   , cond, filename, fileline);
114 #endif
115         return pthread_cond_wait(condp, mutexp);
116 }
117
118 void *_Thread_CreateThread(int (*fn)(void *), void *data, const char *filename, int fileline)
119 {
120         pthread_t *threadp = (pthread_t *) Z_Malloc(sizeof(pthread_t));
121 #ifdef THREADDEBUG
122         Sys_PrintfToTerminal("%p thread create %s:%i\n"   , threadp, filename, fileline);
123 #endif
124         int r = pthread_create(threadp, NULL, (void * (*) (void *)) fn, data);
125         if(r)
126         {
127                 Z_Free(threadp);
128                 return NULL;
129         }
130         return threadp;
131 }
132
133 int _Thread_WaitThread(void *thread, int retval, const char *filename, int fileline)
134 {
135         pthread_t *threadp = (pthread_t *) thread;
136         void *status = (void *) (intptr_t) retval;
137 #ifdef THREADDEBUG
138         Sys_PrintfToTerminal("%p thread wait %s:%i\n"   , thread, filename, fileline);
139 #endif
140         pthread_join(*threadp, &status);
141         Z_Free(threadp);
142         return (int) (intptr_t) status;
143 }
144
145 #ifdef PTHREAD_BARRIER_SERIAL_THREAD
146 void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline)
147 {
148         pthread_barrier_t *b = (pthread_barrier_t *) Z_Malloc(sizeof(pthread_barrier_t));
149 #ifdef THREADDEBUG
150         Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline);
151 #endif
152         pthread_barrier_init(b, NULL, count);
153         return (void *) b;
154 }
155
156 void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline)
157 {
158         pthread_barrier_t *b = (pthread_barrier_t *) barrier;
159 #ifdef THREADDEBUG
160         Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline);
161 #endif
162         pthread_barrier_destroy(b);
163 }
164
165 void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline)
166 {
167         pthread_barrier_t *b = (pthread_barrier_t *) barrier;
168 #ifdef THREADDEBUG
169         Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline);
170 #endif
171         pthread_barrier_wait(b);
172 }
173 #else
174 // standard barrier implementation using conds and mutexes
175 // see: http://www.howforge.com/implementing-barrier-in-pthreads
176 typedef struct {
177         unsigned int needed;
178         unsigned int called;
179         void *mutex;
180         void *cond;
181 } barrier_t;
182
183 void *_Thread_CreateBarrier(unsigned int count, const char *filename, int fileline)
184 {
185         volatile barrier_t *b = (volatile barrier_t *) Z_Malloc(sizeof(barrier_t));
186 #ifdef THREADDEBUG
187         Sys_PrintfToTerminal("%p barrier create(%d) %s:%i\n", b, count, filename, fileline);
188 #endif
189         b->needed = count;
190         b->called = 0;
191         b->mutex = Thread_CreateMutex();
192         b->cond = Thread_CreateCond();
193         return (void *) b;
194 }
195
196 void _Thread_DestroyBarrier(void *barrier, const char *filename, int fileline)
197 {
198         volatile barrier_t *b = (volatile barrier_t *) barrier;
199 #ifdef THREADDEBUG
200         Sys_PrintfToTerminal("%p barrier destroy %s:%i\n", b, filename, fileline);
201 #endif
202         Thread_DestroyMutex(b->mutex);
203         Thread_DestroyCond(b->cond);
204 }
205
206 void _Thread_WaitBarrier(void *barrier, const char *filename, int fileline)
207 {
208         volatile barrier_t *b = (volatile barrier_t *) barrier;
209 #ifdef THREADDEBUG
210         Sys_PrintfToTerminal("%p barrier wait %s:%i\n", b, filename, fileline);
211 #endif
212         Thread_LockMutex(b->mutex);
213         b->called++;
214         if (b->called == b->needed) {
215                 b->called = 0;
216                 Thread_CondBroadcast(b->cond);
217         } else {
218                 do {
219                         Thread_CondWait(b->cond, b->mutex);
220                 } while(b->called);
221         }
222         Thread_UnlockMutex(b->mutex);
223 }
224 #endif