SDL_sysmutex.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_sysmutex_c.h"
  22. SDL_Mutex *SDL_CreateMutex(void)
  23. {
  24. SDL_Mutex *mutex;
  25. pthread_mutexattr_t attr;
  26. // Allocate the structure
  27. mutex = (SDL_Mutex *)SDL_calloc(1, sizeof(*mutex));
  28. if (mutex) {
  29. pthread_mutexattr_init(&attr);
  30. #ifdef SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
  31. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  32. #elif defined(SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP)
  33. pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
  34. #else
  35. // No extra attributes necessary
  36. #endif
  37. if (pthread_mutex_init(&mutex->id, &attr) != 0) {
  38. SDL_SetError("pthread_mutex_init() failed");
  39. SDL_free(mutex);
  40. mutex = NULL;
  41. }
  42. }
  43. return mutex;
  44. }
  45. void SDL_DestroyMutex(SDL_Mutex *mutex)
  46. {
  47. if (mutex) {
  48. pthread_mutex_destroy(&mutex->id);
  49. SDL_free(mutex);
  50. }
  51. }
  52. void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  53. {
  54. if (mutex != NULL) {
  55. #ifdef FAKE_RECURSIVE_MUTEX
  56. pthread_t this_thread = pthread_self();
  57. if (mutex->owner == this_thread) {
  58. ++mutex->recursive;
  59. } else {
  60. /* The order of operations is important.
  61. We set the locking thread id after we obtain the lock
  62. so unlocks from other threads will fail.
  63. */
  64. const int rc = pthread_mutex_lock(&mutex->id);
  65. SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
  66. mutex->owner = this_thread;
  67. mutex->recursive = 0;
  68. }
  69. #else
  70. const int rc = pthread_mutex_lock(&mutex->id);
  71. SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
  72. #endif
  73. }
  74. }
  75. int SDL_TryLockMutex(SDL_Mutex *mutex)
  76. {
  77. int retval = 0;
  78. if (mutex) {
  79. #ifdef FAKE_RECURSIVE_MUTEX
  80. pthread_t this_thread = pthread_self();
  81. if (mutex->owner == this_thread) {
  82. ++mutex->recursive;
  83. } else {
  84. /* The order of operations is important.
  85. We set the locking thread id after we obtain the lock
  86. so unlocks from other threads will fail.
  87. */
  88. const int result = pthread_mutex_trylock(&mutex->id);
  89. if (result == 0) {
  90. mutex->owner = this_thread;
  91. mutex->recursive = 0;
  92. } else if (result == EBUSY) {
  93. retval = SDL_MUTEX_TIMEDOUT;
  94. } else {
  95. SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.
  96. retval = SDL_MUTEX_TIMEDOUT;
  97. }
  98. }
  99. #else
  100. const int result = pthread_mutex_trylock(&mutex->id);
  101. if (result != 0) {
  102. if (result == EBUSY) {
  103. retval = SDL_MUTEX_TIMEDOUT;
  104. } else {
  105. SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.
  106. retval = SDL_MUTEX_TIMEDOUT;
  107. }
  108. }
  109. #endif
  110. }
  111. return retval;
  112. }
  113. void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  114. {
  115. if (mutex != NULL) {
  116. #ifdef FAKE_RECURSIVE_MUTEX
  117. // We can only unlock the mutex if we own it
  118. if (pthread_self() == mutex->owner) {
  119. if (mutex->recursive) {
  120. --mutex->recursive;
  121. } else {
  122. /* The order of operations is important.
  123. First reset the owner so another thread doesn't lock
  124. the mutex and set the ownership before we reset it,
  125. then release the lock semaphore.
  126. */
  127. mutex->owner = 0;
  128. pthread_mutex_unlock(&mutex->id);
  129. }
  130. } else {
  131. return SDL_SetError("mutex not owned by this thread");
  132. }
  133. #else
  134. const int rc = pthread_mutex_unlock(&mutex->id);
  135. SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
  136. #endif // FAKE_RECURSIVE_MUTEX
  137. }
  138. }