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