SDL_sysmutex.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_thread.h"
  21. #include "SDL_systhread_c.h"
  22. struct SDL_mutex
  23. {
  24. int recursive;
  25. SDL_threadID owner;
  26. SDL_sem *sem;
  27. };
  28. /* Create a mutex */
  29. SDL_mutex *
  30. SDL_CreateMutex(void)
  31. {
  32. SDL_mutex *mutex;
  33. /* Allocate mutex memory */
  34. mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
  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. return mutex;
  48. }
  49. /* Free the mutex */
  50. void
  51. 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
  62. SDL_LockMutex(SDL_mutex * mutex)
  63. {
  64. #if SDL_THREADS_DISABLED
  65. return 0;
  66. #else
  67. SDL_threadID this_thread;
  68. if (mutex == NULL) {
  69. return SDL_InvalidParamError("mutex");
  70. }
  71. this_thread = SDL_ThreadID();
  72. if (mutex->owner == this_thread) {
  73. ++mutex->recursive;
  74. } else {
  75. /* The order of operations is important.
  76. We set the locking thread id after we obtain the lock
  77. so unlocks from other threads will fail.
  78. */
  79. SDL_SemWait(mutex->sem);
  80. mutex->owner = this_thread;
  81. mutex->recursive = 0;
  82. }
  83. return 0;
  84. #endif /* SDL_THREADS_DISABLED */
  85. }
  86. /* try Lock the mutex */
  87. int
  88. SDL_TryLockMutex(SDL_mutex * mutex)
  89. {
  90. #if SDL_THREADS_DISABLED
  91. return 0;
  92. #else
  93. int retval = 0;
  94. SDL_threadID this_thread;
  95. if (mutex == NULL) {
  96. return SDL_InvalidParamError("mutex");
  97. }
  98. this_thread = SDL_ThreadID();
  99. if (mutex->owner == this_thread) {
  100. ++mutex->recursive;
  101. } else {
  102. /* The order of operations is important.
  103. We set the locking thread id after we obtain the lock
  104. so unlocks from other threads will fail.
  105. */
  106. retval = SDL_SemWait(mutex->sem);
  107. if (retval == 0) {
  108. mutex->owner = this_thread;
  109. mutex->recursive = 0;
  110. }
  111. }
  112. return retval;
  113. #endif /* SDL_THREADS_DISABLED */
  114. }
  115. /* Unlock the mutex */
  116. int
  117. SDL_mutexV(SDL_mutex * mutex)
  118. {
  119. #if SDL_THREADS_DISABLED
  120. return 0;
  121. #else
  122. if (mutex == NULL) {
  123. return SDL_InvalidParamError("mutex");
  124. }
  125. /* If we don't own the mutex, we can't unlock it */
  126. if (SDL_ThreadID() != mutex->owner) {
  127. return SDL_SetError("mutex not owned by this thread");
  128. }
  129. if (mutex->recursive) {
  130. --mutex->recursive;
  131. } else {
  132. /* The order of operations is important.
  133. First reset the owner so another thread doesn't lock
  134. the mutex and set the ownership before we reset it,
  135. then release the lock semaphore.
  136. */
  137. mutex->owner = 0;
  138. SDL_SemPost(mutex->sem);
  139. }
  140. return 0;
  141. #endif /* SDL_THREADS_DISABLED */
  142. }
  143. /* vi: set ts=4 sw=4 expandtab: */