XShm and pthreads support for vid_glx dpsoftrast
[xonotic/darkplaces.git] / thread_pthread.c
1 #include "quakedef.h"
2 #include "thread.h"
3 #include <pthread.h>
4 #include <stdint.h>
5
6 int Thread_Init(void)
7 {
8         return 0;
9 }
10
11 void Thread_Shutdown(void)
12 {
13 }
14
15 qboolean Thread_HasThreads(void)
16 {
17         return true;
18 }
19
20 void *Thread_CreateMutex(void)
21 {
22         pthread_mutex_t *mutexp = (pthread_mutex_t *) Z_Malloc(sizeof(pthread_mutex_t));
23         pthread_mutex_init(mutexp, NULL);
24         return mutexp;
25 }
26
27 void Thread_DestroyMutex(void *mutex)
28 {
29         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
30         pthread_mutex_destroy(mutexp);
31         Z_Free(mutexp);
32 }
33
34 int Thread_LockMutex(void *mutex)
35 {
36         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
37         return pthread_mutex_lock(mutexp);
38 }
39
40 int Thread_UnlockMutex(void *mutex)
41 {
42         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
43         return pthread_mutex_unlock(mutexp);
44 }
45
46 void *Thread_CreateCond(void)
47 {
48         pthread_cond_t *condp = (pthread_cond_t *) Z_Malloc(sizeof(pthread_cond_t));
49         pthread_cond_init(condp, NULL);
50         return condp;
51 }
52
53 void Thread_DestroyCond(void *cond)
54 {
55         pthread_cond_t *condp = (pthread_cond_t *) cond;
56         pthread_cond_destroy(condp);
57         Z_Free(condp);
58 }
59
60 int Thread_CondSignal(void *cond)
61 {
62         pthread_cond_t *condp = (pthread_cond_t *) cond;
63         return pthread_cond_signal(condp);
64 }
65
66 int Thread_CondBroadcast(void *cond)
67 {
68         pthread_cond_t *condp = (pthread_cond_t *) cond;
69         return pthread_cond_broadcast(condp);
70 }
71
72 int Thread_CondWait(void *cond, void *mutex)
73 {
74         pthread_cond_t *condp = (pthread_cond_t *) cond;
75         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
76         return pthread_cond_wait(condp, mutexp);
77 }
78
79 void *Thread_CreateThread(int (*fn)(void *), void *data)
80 {
81         pthread_t *threadp = (pthread_t *) Z_Malloc(sizeof(pthread_t));
82         int r = pthread_create(threadp, NULL, (void * (*) (void *)) fn, data);
83         if(r)
84         {
85                 Z_Free(threadp);
86                 return NULL;
87         }
88         return threadp;
89 }
90
91 int Thread_WaitThread(void *thread, int retval)
92 {
93         pthread_t *threadp = (pthread_t *) thread;
94         void *status = (void *) (intptr_t) retval;
95         pthread_join(*threadp, &status);
96         Z_Free(threadp);
97         return (int) (intptr_t) status;
98 }
99
100