mutex.c 3.6 KB

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