SDL_sysmutex.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #include <errno.h>
  20. #include <pthread.h>
  21. #include "SDL_thread.h"
  22. #if !(defined(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX) || \
  23. defined(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP))
  24. #define FAKE_RECURSIVE_MUTEX
  25. #endif
  26. struct SDL_mutex
  27. {
  28. pthread_mutex_t id;
  29. #ifdef FAKE_RECURSIVE_MUTEX
  30. int recursive;
  31. pthread_t owner;
  32. #endif
  33. };
  34. SDL_mutex *SDL_CreateMutex(void)
  35. {
  36. SDL_mutex *mutex;
  37. pthread_mutexattr_t attr;
  38. /* Allocate the structure */
  39. mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
  40. if (mutex) {
  41. pthread_mutexattr_init(&attr);
  42. #ifdef SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
  43. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  44. #elif defined(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP)
  45. pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
  46. #else
  47. /* No extra attributes necessary */
  48. #endif
  49. if (pthread_mutex_init(&mutex->id, &attr) != 0) {
  50. SDL_SetError("pthread_mutex_init() failed");
  51. SDL_free(mutex);
  52. mutex = NULL;
  53. }
  54. } else {
  55. SDL_OutOfMemory();
  56. }
  57. return mutex;
  58. }
  59. void SDL_DestroyMutex(SDL_mutex *mutex)
  60. {
  61. if (mutex) {
  62. pthread_mutex_destroy(&mutex->id);
  63. SDL_free(mutex);
  64. }
  65. }
  66. /* Lock the mutex */
  67. int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  68. {
  69. #ifdef FAKE_RECURSIVE_MUTEX
  70. pthread_t this_thread;
  71. #endif
  72. if (mutex == NULL) {
  73. return 0;
  74. }
  75. #ifdef FAKE_RECURSIVE_MUTEX
  76. this_thread = pthread_self();
  77. if (mutex->owner == this_thread) {
  78. ++mutex->recursive;
  79. } else {
  80. /* The order of operations is important.
  81. We set the locking thread id after we obtain the lock
  82. so unlocks from other threads will fail.
  83. */
  84. if (pthread_mutex_lock(&mutex->id) == 0) {
  85. mutex->owner = this_thread;
  86. mutex->recursive = 0;
  87. } else {
  88. return SDL_SetError("pthread_mutex_lock() failed");
  89. }
  90. }
  91. #else
  92. if (pthread_mutex_lock(&mutex->id) != 0) {
  93. return SDL_SetError("pthread_mutex_lock() failed");
  94. }
  95. #endif
  96. return 0;
  97. }
  98. int SDL_TryLockMutex(SDL_mutex *mutex)
  99. {
  100. int retval;
  101. int result;
  102. #ifdef FAKE_RECURSIVE_MUTEX
  103. pthread_t this_thread;
  104. #endif
  105. if (!mutex) {
  106. return 0;
  107. }
  108. retval = 0;
  109. #ifdef FAKE_RECURSIVE_MUTEX
  110. this_thread = pthread_self();
  111. if (mutex->owner == this_thread) {
  112. ++mutex->recursive;
  113. } else {
  114. /* The order of operations is important.
  115. We set the locking thread id after we obtain the lock
  116. so unlocks from other threads will fail.
  117. */
  118. result = pthread_mutex_trylock(&mutex->id);
  119. if (result == 0) {
  120. mutex->owner = this_thread;
  121. mutex->recursive = 0;
  122. } else if (result == EBUSY) {
  123. retval = SDL_MUTEX_TIMEDOUT;
  124. } else {
  125. retval = SDL_SetError("pthread_mutex_trylock() failed");
  126. }
  127. }
  128. #else
  129. result = pthread_mutex_trylock(&mutex->id);
  130. if (result != 0) {
  131. if (result == EBUSY) {
  132. retval = SDL_MUTEX_TIMEDOUT;
  133. } else {
  134. retval = SDL_SetError("pthread_mutex_trylock() failed");
  135. }
  136. }
  137. #endif
  138. return retval;
  139. }
  140. int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  141. {
  142. if (mutex == NULL) {
  143. return 0;
  144. }
  145. #ifdef FAKE_RECURSIVE_MUTEX
  146. /* We can only unlock the mutex if we own it */
  147. if (pthread_self() == mutex->owner) {
  148. if (mutex->recursive) {
  149. --mutex->recursive;
  150. } else {
  151. /* The order of operations is important.
  152. First reset the owner so another thread doesn't lock
  153. the mutex and set the ownership before we reset it,
  154. then release the lock semaphore.
  155. */
  156. mutex->owner = 0;
  157. pthread_mutex_unlock(&mutex->id);
  158. }
  159. } else {
  160. return SDL_SetError("mutex not owned by this thread");
  161. }
  162. #else
  163. if (pthread_mutex_unlock(&mutex->id) != 0) {
  164. return SDL_SetError("pthread_mutex_unlock() failed");
  165. }
  166. #endif /* FAKE_RECURSIVE_MUTEX */
  167. return 0;
  168. }
  169. /* vi: set ts=4 sw=4 expandtab: */