]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/common/mutex.c
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / tools / quake3 / common / mutex.c
1 /*\r
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.\r
4 \r
5 This file is part of GtkRadiant.\r
6 \r
7 GtkRadiant is free software; you can redistribute it and/or modify\r
8 it under the terms of the GNU General Public License as published by\r
9 the Free Software Foundation; either version 2 of the License, or\r
10 (at your option) any later version.\r
11 \r
12 GtkRadiant is distributed in the hope that it will be useful,\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 GNU General Public License for more details.\r
16 \r
17 You should have received a copy of the GNU General Public License\r
18 along with GtkRadiant; if not, write to the Free Software\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\r
20 */\r
21 \r
22 \r
23 #include "cmdlib.h"\r
24 #include "qthreads.h"\r
25 #include "mutex.h"\r
26 \r
27 /*\r
28 ===================================================================\r
29 \r
30 WIN32\r
31 \r
32 ===================================================================\r
33 */\r
34 #ifdef _WIN32\r
35 \r
36 #define USED\r
37 \r
38 #include <windows.h>\r
39 \r
40 void MutexLock (mutex_t *m)\r
41 {\r
42         CRITICAL_SECTION *crit;\r
43 \r
44         if (!m)\r
45                 return;\r
46         crit = (CRITICAL_SECTION *) m;\r
47         EnterCriticalSection (crit);\r
48 }\r
49 \r
50 void MutexUnlock (mutex_t *m)\r
51 {\r
52         CRITICAL_SECTION *crit;\r
53 \r
54         if (!m)\r
55                 return;\r
56         crit = (CRITICAL_SECTION *) m;\r
57         LeaveCriticalSection (crit);\r
58 }\r
59 \r
60 mutex_t *MutexAlloc(void)\r
61 {\r
62         CRITICAL_SECTION *crit;\r
63 \r
64         if (numthreads == 1)\r
65                 return NULL;\r
66         crit = (CRITICAL_SECTION *) safe_malloc(sizeof(CRITICAL_SECTION));\r
67         InitializeCriticalSection (crit);\r
68         return (void *) crit;\r
69 }\r
70 \r
71 #endif\r
72 \r
73 /*\r
74 ===================================================================\r
75 \r
76 OSF1\r
77 \r
78 ===================================================================\r
79 */\r
80 \r
81 #ifdef __osf__\r
82 #define USED\r
83 \r
84 #include <pthread.h>\r
85 \r
86 void MutexLock (mutex_t *m)\r
87 {\r
88         pthread_mutex_t *my_mutex;\r
89 \r
90         if (!m)\r
91                 return;\r
92         my_mutex = (pthread_mutex_t *) m;\r
93         pthread_mutex_lock (my_mutex);\r
94 }\r
95 \r
96 void MutexUnlock (mutex_t *m)\r
97 {\r
98         pthread_mutex_t *my_mutex;\r
99 \r
100         if (!m)\r
101                 return;\r
102         my_mutex = (pthread_mutex_t *) m;\r
103         pthread_mutex_unlock (my_mutex);\r
104 }\r
105 \r
106 mutex_t *MutexAlloc(void)\r
107 {\r
108         pthread_mutex_t *my_mutex;\r
109         pthread_mutexattr_t     mattrib;\r
110 \r
111         if (numthreads == 1)\r
112                 return NULL;\r
113         my_mutex = safe_malloc (sizeof(*my_mutex));\r
114         if (pthread_mutexattr_create (&mattrib) == -1)\r
115                 Error ("pthread_mutex_attr_create failed");\r
116         if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)\r
117                 Error ("pthread_mutexattr_setkind_np failed");\r
118         if (pthread_mutex_init (my_mutex, mattrib) == -1)\r
119                 Error ("pthread_mutex_init failed");\r
120         return (void *) my_mutex;\r
121 }\r
122 \r
123 #endif\r
124 \r
125 /*\r
126 ===================================================================\r
127 \r
128 IRIX\r
129 \r
130 ===================================================================\r
131 */\r
132 \r
133 #ifdef _MIPS_ISA \r
134 #define USED\r
135 \r
136 #include <task.h>\r
137 #include <abi_mutex.h>\r
138 #include <sys/types.h>\r
139 #include <sys/prctl.h>\r
140 \r
141 void MutexLock (mutex_t *m)\r
142 {\r
143         abilock_t *lck;\r
144 \r
145         if (!m)\r
146                 return;\r
147         lck = (abilock_t *) m;\r
148         spin_lock (lck);\r
149 }\r
150 \r
151 void MutexUnlock (mutex_t *m)\r
152 {\r
153         abilock_t *lck;\r
154 \r
155         if (!m)\r
156                 return;\r
157         lck = (abilock_t *) m;\r
158         release_lock (lck);\r
159 }\r
160 \r
161 mutex_t *MutexAlloc(void)\r
162 {\r
163         abilock_t *lck;\r
164 \r
165         if (numthreads == 1)\r
166                 return NULL;\r
167         lck = (abilock_t *) safe_malloc(sizeof(abilock_t));\r
168         init_lock (lck);\r
169         return (void *) lck;\r
170 }\r
171 \r
172 #endif\r
173 \r
174 /*\r
175 =======================================================================\r
176 \r
177   SINGLE THREAD\r
178 \r
179 =======================================================================\r
180 */\r
181 \r
182 #ifndef USED\r
183 \r
184 void MutexLock (mutex_t *m)\r
185 {\r
186 }\r
187 \r
188 void MutexUnlock (mutex_t *m)\r
189 {\r
190 }\r
191 \r
192 mutex_t *MutexAlloc(void)\r
193 {\r
194         return NULL;\r
195 }\r
196 \r
197 #endif\r