]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - thread_pthread.c
added debug prints (enabled if you define THREADDEBUG) for debugging
[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(const char *filename, int fileline)
21 {
22         pthread_mutex_t *mutexp = (pthread_mutex_t *) Z_Malloc(sizeof(pthread_mutex_t));
23 #ifdef THREADDEBUG
24         printf("%p create %s:%i\n" , mutexp, filename, fileline);
25 #endif
26         pthread_mutex_init(mutexp, NULL);
27         return mutexp;
28 }
29
30 void _Thread_DestroyMutex(void *mutex, const char *filename, int fileline)
31 {
32         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
33 #ifdef THREADDEBUG
34         printf("%p destroy %s:%i\n", mutex, filename, fileline);
35 #endif
36         pthread_mutex_destroy(mutexp);
37         Z_Free(mutexp);
38 }
39
40 int _Thread_LockMutex(void *mutex, const char *filename, int fileline)
41 {
42         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
43 #ifdef THREADDEBUG
44         printf("%p lock %s:%i\n"   , mutex, filename, fileline);
45 #endif
46         return pthread_mutex_lock(mutexp);
47 }
48
49 int _Thread_UnlockMutex(void *mutex, const char *filename, int fileline)
50 {
51         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
52 #ifdef THREADDEBUG
53         printf("%p unlock %s:%i\n" , mutex, filename, fileline);
54 #endif
55         return pthread_mutex_unlock(mutexp);
56 }
57
58 void *Thread_CreateCond(void)
59 {
60         pthread_cond_t *condp = (pthread_cond_t *) Z_Malloc(sizeof(pthread_cond_t));
61         pthread_cond_init(condp, NULL);
62         return condp;
63 }
64
65 void Thread_DestroyCond(void *cond)
66 {
67         pthread_cond_t *condp = (pthread_cond_t *) cond;
68         pthread_cond_destroy(condp);
69         Z_Free(condp);
70 }
71
72 int Thread_CondSignal(void *cond)
73 {
74         pthread_cond_t *condp = (pthread_cond_t *) cond;
75         return pthread_cond_signal(condp);
76 }
77
78 int Thread_CondBroadcast(void *cond)
79 {
80         pthread_cond_t *condp = (pthread_cond_t *) cond;
81         return pthread_cond_broadcast(condp);
82 }
83
84 int Thread_CondWait(void *cond, void *mutex)
85 {
86         pthread_cond_t *condp = (pthread_cond_t *) cond;
87         pthread_mutex_t *mutexp = (pthread_mutex_t *) mutex;
88         return pthread_cond_wait(condp, mutexp);
89 }
90
91 void *Thread_CreateThread(int (*fn)(void *), void *data)
92 {
93         pthread_t *threadp = (pthread_t *) Z_Malloc(sizeof(pthread_t));
94         int r = pthread_create(threadp, NULL, (void * (*) (void *)) fn, data);
95         if(r)
96         {
97                 Z_Free(threadp);
98                 return NULL;
99         }
100         return threadp;
101 }
102
103 int Thread_WaitThread(void *thread, int retval)
104 {
105         pthread_t *threadp = (pthread_t *) thread;
106         void *status = (void *) (intptr_t) retval;
107         pthread_join(*threadp, &status);
108         Z_Free(threadp);
109         return (int) (intptr_t) status;
110 }
111
112