SDL_syssem.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #ifdef SDL_THREAD_N3DS
  20. /* An implementation of semaphores using libctru's LightSemaphore */
  21. #include <3ds.h>
  22. #include "SDL_thread.h"
  23. #include "SDL_timer.h"
  24. int WaitOnSemaphoreFor(SDL_sem *sem, Uint32 timeout);
  25. struct SDL_semaphore
  26. {
  27. LightSemaphore semaphore;
  28. };
  29. SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
  30. {
  31. SDL_sem *sem;
  32. if (initial_value > SDL_MAX_SINT16) {
  33. SDL_SetError("Initial semaphore value too high for this platform");
  34. return NULL;
  35. }
  36. sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
  37. if (sem == NULL) {
  38. SDL_OutOfMemory();
  39. return NULL;
  40. }
  41. LightSemaphore_Init(&sem->semaphore, initial_value, SDL_MAX_SINT16);
  42. return sem;
  43. }
  44. /* WARNING:
  45. You cannot call this function when another thread is using the semaphore.
  46. */
  47. void SDL_DestroySemaphore(SDL_sem *sem)
  48. {
  49. SDL_free(sem);
  50. }
  51. int SDL_SemTryWait(SDL_sem *sem)
  52. {
  53. if (sem == NULL) {
  54. return SDL_InvalidParamError("sem");
  55. }
  56. if (LightSemaphore_TryAcquire(&sem->semaphore, 1) != 0) {
  57. /* If we failed, yield to avoid starvation on busy waits */
  58. svcSleepThread(1);
  59. return SDL_MUTEX_TIMEDOUT;
  60. }
  61. return 0;
  62. }
  63. int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
  64. {
  65. if (sem == NULL) {
  66. return SDL_InvalidParamError("sem");
  67. }
  68. if (timeout == SDL_MUTEX_MAXWAIT) {
  69. LightSemaphore_Acquire(&sem->semaphore, 1);
  70. return 0;
  71. }
  72. if (LightSemaphore_TryAcquire(&sem->semaphore, 1) != 0) {
  73. return WaitOnSemaphoreFor(sem, timeout);
  74. }
  75. return 0;
  76. }
  77. int WaitOnSemaphoreFor(SDL_sem *sem, Uint32 timeout)
  78. {
  79. Uint64 stop_time = SDL_GetTicks64() + timeout;
  80. Uint64 current_time = SDL_GetTicks64();
  81. while (current_time < stop_time) {
  82. if (LightSemaphore_TryAcquire(&sem->semaphore, 1) == 0) {
  83. return 0;
  84. }
  85. /* 100 microseconds seems to be the sweet spot */
  86. svcSleepThread(100000LL);
  87. current_time = SDL_GetTicks64();
  88. }
  89. /* If we failed, yield to avoid starvation on busy waits */
  90. svcSleepThread(1);
  91. return SDL_MUTEX_TIMEDOUT;
  92. }
  93. int SDL_SemWait(SDL_sem *sem)
  94. {
  95. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  96. }
  97. Uint32 SDL_SemValue(SDL_sem *sem)
  98. {
  99. if (sem == NULL) {
  100. SDL_InvalidParamError("sem");
  101. return 0;
  102. }
  103. return sem->semaphore.current_count;
  104. }
  105. int SDL_SemPost(SDL_sem *sem)
  106. {
  107. if (sem == NULL) {
  108. return SDL_InvalidParamError("sem");
  109. }
  110. LightSemaphore_Release(&sem->semaphore, 1);
  111. return 0;
  112. }
  113. #endif /* SDL_THREAD_N3DS */
  114. /* vi: set sts=4 ts=4 sw=4 expandtab: */