SDL_sysmutex.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. pthread_mutexattr_destroy(&attr);
  43. }
  44. return mutex;
  45. }
  46. void SDL_DestroyMutex(SDL_Mutex *mutex)
  47. {
  48. if (mutex) {
  49. pthread_mutex_destroy(&mutex->id);
  50. SDL_free(mutex);
  51. }
  52. }
  53. void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  54. {
  55. if (mutex) {
  56. #ifdef FAKE_RECURSIVE_MUTEX
  57. pthread_t this_thread = pthread_self();
  58. if (mutex->owner == this_thread) {
  59. ++mutex->recursive;
  60. } else {
  61. /* The order of operations is important.
  62. We set the locking thread id after we obtain the lock
  63. so unlocks from other threads will fail.
  64. */
  65. const int rc = pthread_mutex_lock(&mutex->id);
  66. SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
  67. mutex->owner = this_thread;
  68. mutex->recursive = 0;
  69. }
  70. #else
  71. const int rc = pthread_mutex_lock(&mutex->id);
  72. SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
  73. #endif
  74. }
  75. }
  76. bool SDL_TryLockMutex(SDL_Mutex *mutex)
  77. {
  78. bool result = true;
  79. if (mutex) {
  80. #ifdef FAKE_RECURSIVE_MUTEX
  81. pthread_t this_thread = pthread_self();
  82. if (mutex->owner == this_thread) {
  83. ++mutex->recursive;
  84. } else {
  85. /* The order of operations is important.
  86. We set the locking thread id after we obtain the lock
  87. so unlocks from other threads will fail.
  88. */
  89. const int rc = pthread_mutex_trylock(&mutex->id);
  90. if (rc == 0) {
  91. mutex->owner = this_thread;
  92. mutex->recursive = 0;
  93. } else if (rc == EBUSY) {
  94. result = false;
  95. } else {
  96. SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.
  97. result = false;
  98. }
  99. }
  100. #else
  101. const int rc = pthread_mutex_trylock(&mutex->id);
  102. if (rc != 0) {
  103. if (rc == EBUSY) {
  104. result = false;
  105. } else {
  106. SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.
  107. result = false;
  108. }
  109. }
  110. #endif
  111. }
  112. return result;
  113. }
  114. void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  115. {
  116. if (mutex) {
  117. #ifdef FAKE_RECURSIVE_MUTEX
  118. // We can only unlock the mutex if we own it
  119. if (pthread_self() == mutex->owner) {
  120. if (mutex->recursive) {
  121. --mutex->recursive;
  122. } else {
  123. /* The order of operations is important.
  124. First reset the owner so another thread doesn't lock
  125. the mutex and set the ownership before we reset it,
  126. then release the lock semaphore.
  127. */
  128. mutex->owner = 0;
  129. pthread_mutex_unlock(&mutex->id);
  130. }
  131. } else {
  132. SDL_SetError("mutex not owned by this thread");
  133. return;
  134. }
  135. #else
  136. const int rc = pthread_mutex_unlock(&mutex->id);
  137. SDL_assert(rc == 0); // assume we're in a lot of trouble if this assert fails.
  138. #endif // FAKE_RECURSIVE_MUTEX
  139. }
  140. }