Debian Codegraph @

tools/quake3/common/mutex.c

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