Quake-III-Arena

Quake III Arena GPL Source Release
Log | Files | Refs

mutex.c (3707B)


      1 /*
      2 ===========================================================================
      3 Copyright (C) 1999-2005 Id Software, Inc.
      4 
      5 This file is part of Quake III Arena source code.
      6 
      7 Quake III Arena source code is free software; you can redistribute it
      8 and/or modify it under the terms of the GNU General Public License as
      9 published by the Free Software Foundation; either version 2 of the License,
     10 or (at your option) any later version.
     11 
     12 Quake III Arena source code is distributed in the hope that it will be
     13 useful, 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 Foobar; if not, write to the Free Software
     19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20 ===========================================================================
     21 */
     22 
     23 #include "cmdlib.h"
     24 #include "threads.h"
     25 #include "mutex.h"
     26 
     27 /*
     28 ===================================================================
     29 
     30 WIN32
     31 
     32 ===================================================================
     33 */
     34 #ifdef WIN32
     35 
     36 #define	USED
     37 
     38 #include <windows.h>
     39 
     40 void MutexLock (mutex_t *m)
     41 {
     42 	CRITICAL_SECTION *crit;
     43 
     44 	if (!m)
     45 		return;
     46 	crit = (CRITICAL_SECTION *) m;
     47 	EnterCriticalSection (crit);
     48 }
     49 
     50 void MutexUnlock (mutex_t *m)
     51 {
     52 	CRITICAL_SECTION *crit;
     53 
     54 	if (!m)
     55 		return;
     56 	crit = (CRITICAL_SECTION *) m;
     57 	LeaveCriticalSection (crit);
     58 }
     59 
     60 mutex_t *MutexAlloc(void)
     61 {
     62 	CRITICAL_SECTION *crit;
     63 
     64 	if (numthreads == 1)
     65 		return NULL;
     66 	crit = (CRITICAL_SECTION *) 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 {
     88 	pthread_mutex_t	*my_mutex;
     89 
     90 	if (!m)
     91 		return;
     92 	my_mutex = (pthread_mutex_t *) m;
     93 	pthread_mutex_lock (my_mutex);
     94 }
     95 
     96 void MutexUnlock (mutex_t *m)
     97 {
     98 	pthread_mutex_t	*my_mutex;
     99 
    100 	if (!m)
    101 		return;
    102 	my_mutex = (pthread_mutex_t *) m;
    103 	pthread_mutex_unlock (my_mutex);
    104 }
    105 
    106 mutex_t *MutexAlloc(void)
    107 {
    108 	pthread_mutex_t	*my_mutex;
    109 	pthread_mutexattr_t	mattrib;
    110 
    111 	if (numthreads == 1)
    112 		return NULL;
    113 	my_mutex = malloc (sizeof(*my_mutex));
    114 	if (pthread_mutexattr_create (&mattrib) == -1)
    115 		Error ("pthread_mutex_attr_create failed");
    116 	if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
    117 		Error ("pthread_mutexattr_setkind_np failed");
    118 	if (pthread_mutex_init (my_mutex, mattrib) == -1)
    119 		Error ("pthread_mutex_init failed");
    120 	return (void *) my_mutex;
    121 }
    122 
    123 #endif
    124 
    125 /*
    126 ===================================================================
    127 
    128 IRIX
    129 
    130 ===================================================================
    131 */
    132 
    133 #ifdef _MIPS_ISA 
    134 #define	USED
    135 
    136 #include <task.h>
    137 #include <abi_mutex.h>
    138 #include <sys/types.h>
    139 #include <sys/prctl.h>
    140 
    141 void MutexLock (mutex_t *m)
    142 {
    143 	abilock_t *lck;
    144 
    145 	if (!m)
    146 		return;
    147 	lck = (abilock_t *) m;
    148 	spin_lock (lck);
    149 }
    150 
    151 void MutexUnlock (mutex_t *m)
    152 {
    153 	abilock_t *lck;
    154 
    155 	if (!m)
    156 		return;
    157 	lck = (abilock_t *) m;
    158 	release_lock (lck);
    159 }
    160 
    161 mutex_t *MutexAlloc(void)
    162 {
    163 	abilock_t *lck;
    164 
    165 	if (numthreads == 1)
    166 		return NULL;
    167 	lck = (abilock_t *) malloc(sizeof(abilock_t));
    168 	init_lock (lck);
    169 	return (void *) lck;
    170 }
    171 
    172 #endif
    173 
    174 /*
    175 =======================================================================
    176 
    177   SINGLE THREAD
    178 
    179 =======================================================================
    180 */
    181 
    182 #ifndef USED
    183 
    184 void MutexLock (mutex_t *m)
    185 {
    186 }
    187 
    188 void MutexUnlock (mutex_t *m)
    189 {
    190 }
    191 
    192 mutex_t *MutexAlloc(void)
    193 {
    194 	return NULL;
    195 }
    196 
    197 #endif