]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - thread_pthread.c
thread_pthread now uses recursive mutex (THREADRECURSIVE)
[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