SDL_sysmutex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. // 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_Semaphore *sem;
  26. };
  27. SDL_Mutex *SDL_CreateMutex(void)
  28. {
  29. SDL_Mutex *mutex = (SDL_Mutex *)SDL_calloc(1, sizeof(*mutex));
  30. #ifndef SDL_THREADS_DISABLED
  31. if (mutex) {
  32. /* Create the mutex semaphore, with initial value 1 */
  33. mutex->sem = SDL_CreateSemaphore(1);
  34. mutex->recursive = 0;
  35. mutex->owner = 0;
  36. if (!mutex->sem) {
  37. SDL_free(mutex);
  38. mutex = NULL;
  39. }
  40. }
  41. #endif // !SDL_THREADS_DISABLED
  42. return mutex;
  43. }
  44. void SDL_DestroyMutex(SDL_Mutex *mutex)
  45. {
  46. if (mutex) {
  47. if (mutex->sem) {
  48. SDL_DestroySemaphore(mutex->sem);
  49. }
  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. #ifndef SDL_THREADS_DISABLED
  56. if (mutex != NULL) {
  57. SDL_ThreadID this_thread = SDL_GetCurrentThreadID();
  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. SDL_WaitSemaphore(mutex->sem);
  66. mutex->owner = this_thread;
  67. mutex->recursive = 0;
  68. }
  69. }
  70. #endif /* SDL_THREADS_DISABLED */
  71. }
  72. int SDL_TryLockMutex(SDL_Mutex *mutex)
  73. {
  74. int retval = 0;
  75. #ifndef SDL_THREADS_DISABLED
  76. if (mutex) {
  77. SDL_ThreadID this_thread = SDL_GetCurrentThreadID();
  78. if (mutex->owner == this_thread) {
  79. ++mutex->recursive;
  80. } else {
  81. /* The order of operations is important.
  82. We set the locking thread id after we obtain the lock
  83. so unlocks from other threads will fail.
  84. */
  85. retval = SDL_TryWaitSemaphore(mutex->sem);
  86. if (retval == 0) {
  87. mutex->owner = this_thread;
  88. mutex->recursive = 0;
  89. }
  90. }
  91. }
  92. #endif // SDL_THREADS_DISABLED
  93. return retval;
  94. }
  95. void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  96. {
  97. #ifndef SDL_THREADS_DISABLED
  98. if (mutex != NULL) {
  99. // If we don't own the mutex, we can't unlock it
  100. if (SDL_GetCurrentThreadID() != mutex->owner) {
  101. SDL_assert(!"Tried to unlock a mutex we don't own!");
  102. return; // (undefined behavior!) SDL_SetError("mutex not owned by this thread");
  103. }
  104. if (mutex->recursive) {
  105. --mutex->recursive;
  106. } else {
  107. /* The order of operations is important.
  108. First reset the owner so another thread doesn't lock
  109. the mutex and set the ownership before we reset it,
  110. then release the lock semaphore.
  111. */
  112. mutex->owner = 0;
  113. SDL_PostSemaphore(mutex->sem);
  114. }
  115. }
  116. #endif // SDL_THREADS_DISABLED
  117. }