SDL_sysmutex.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  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. /* An implementation of mutexes using semaphores */
  20. #include "SDL_systhread_c.h"
  21. struct SDL_mutex
  22. {
  23. int recursive;
  24. SDL_threadID owner;
  25. SDL_sem *sem;
  26. };
  27. /* Create a mutex */
  28. SDL_mutex *
  29. SDL_CreateMutex(void)
  30. {
  31. SDL_mutex *mutex;
  32. /* Allocate mutex memory */
  33. mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
  34. #ifndef SDL_THREADS_DISABLED
  35. if (mutex) {
  36. /* Create the mutex semaphore, with initial value 1 */
  37. mutex->sem = SDL_CreateSemaphore(1);
  38. mutex->recursive = 0;
  39. mutex->owner = 0;
  40. if (!mutex->sem) {
  41. SDL_free(mutex);
  42. mutex = NULL;
  43. }
  44. } else {
  45. SDL_OutOfMemory();
  46. }
  47. #endif /* !SDL_THREADS_DISABLED */
  48. return mutex;
  49. }
  50. /* Free the mutex */
  51. void SDL_DestroyMutex(SDL_mutex *mutex)
  52. {
  53. if (mutex) {
  54. if (mutex->sem) {
  55. SDL_DestroySemaphore(mutex->sem);
  56. }
  57. SDL_free(mutex);
  58. }
  59. }
  60. /* Lock the mutex */
  61. int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  62. {
  63. #ifdef SDL_THREADS_DISABLED
  64. return 0;
  65. #else
  66. SDL_threadID this_thread;
  67. if (mutex == NULL) {
  68. return 0;
  69. }
  70. this_thread = SDL_ThreadID();
  71. if (mutex->owner == this_thread) {
  72. ++mutex->recursive;
  73. } else {
  74. /* The order of operations is important.
  75. We set the locking thread id after we obtain the lock
  76. so unlocks from other threads will fail.
  77. */
  78. SDL_SemWait(mutex->sem);
  79. mutex->owner = this_thread;
  80. mutex->recursive = 0;
  81. }
  82. return 0;
  83. #endif /* SDL_THREADS_DISABLED */
  84. }
  85. /* try Lock the mutex */
  86. int SDL_TryLockMutex(SDL_mutex *mutex)
  87. {
  88. #ifdef SDL_THREADS_DISABLED
  89. return 0;
  90. #else
  91. int retval = 0;
  92. SDL_threadID this_thread;
  93. if (mutex == NULL) {
  94. return 0;
  95. }
  96. this_thread = SDL_ThreadID();
  97. if (mutex->owner == this_thread) {
  98. ++mutex->recursive;
  99. } else {
  100. /* The order of operations is important.
  101. We set the locking thread id after we obtain the lock
  102. so unlocks from other threads will fail.
  103. */
  104. retval = SDL_SemWait(mutex->sem);
  105. if (retval == 0) {
  106. mutex->owner = this_thread;
  107. mutex->recursive = 0;
  108. }
  109. }
  110. return retval;
  111. #endif /* SDL_THREADS_DISABLED */
  112. }
  113. /* Unlock the mutex */
  114. int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  115. {
  116. #ifdef SDL_THREADS_DISABLED
  117. return 0;
  118. #else
  119. if (mutex == NULL) {
  120. return 0;
  121. }
  122. /* If we don't own the mutex, we can't unlock it */
  123. if (SDL_ThreadID() != mutex->owner) {
  124. return SDL_SetError("mutex not owned by this thread");
  125. }
  126. if (mutex->recursive) {
  127. --mutex->recursive;
  128. } else {
  129. /* The order of operations is important.
  130. First reset the owner so another thread doesn't lock
  131. the mutex and set the ownership before we reset it,
  132. then release the lock semaphore.
  133. */
  134. mutex->owner = 0;
  135. SDL_SemPost(mutex->sem);
  136. }
  137. return 0;
  138. #endif /* SDL_THREADS_DISABLED */
  139. }