SDL_syssem.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #ifdef SDL_THREAD_PS2
  20. /* Semaphore functions for the PS2. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <kernel_util.h>
  24. #include <kernel.h>
  25. struct SDL_Semaphore
  26. {
  27. s32 semid;
  28. };
  29. /* Create a semaphore */
  30. SDL_Semaphore *SDL_CreateSemaphore(Uint32 initial_value)
  31. {
  32. SDL_Semaphore *sem;
  33. ee_sema_t sema;
  34. sem = (SDL_Semaphore *)SDL_malloc(sizeof(*sem));
  35. if (sem) {
  36. /* TODO: Figure out the limit on the maximum value. */
  37. sema.init_count = initial_value;
  38. sema.max_count = 255;
  39. sema.option = 0;
  40. sem->semid = CreateSema(&sema);
  41. if (sem->semid < 0) {
  42. SDL_SetError("Couldn't create semaphore");
  43. SDL_free(sem);
  44. sem = NULL;
  45. }
  46. }
  47. return sem;
  48. }
  49. /* Free the semaphore */
  50. void SDL_DestroySemaphore(SDL_Semaphore *sem)
  51. {
  52. if (sem) {
  53. if (sem->semid > 0) {
  54. DeleteSema(sem->semid);
  55. sem->semid = 0;
  56. }
  57. SDL_free(sem);
  58. }
  59. }
  60. int SDL_WaitSemaphoreTimeoutNS(SDL_Semaphore *sem, Sint64 timeoutNS)
  61. {
  62. int ret;
  63. u64 timeout_usec;
  64. u64 *timeout_ptr;
  65. if (!sem) {
  66. return SDL_InvalidParamError("sem");
  67. }
  68. if (timeoutNS == 0) {
  69. if (PollSema(sem->semid) < 0) {
  70. return SDL_MUTEX_TIMEDOUT;
  71. }
  72. return 0;
  73. }
  74. timeout_ptr = NULL;
  75. if (timeoutNS != -1) { // -1 == wait indefinitely.
  76. timeout_usec = SDL_NS_TO_US(timeoutNS);
  77. timeout_ptr = &timeout_usec;
  78. }
  79. ret = WaitSemaEx(sem->semid, 1, timeout_ptr);
  80. if (ret < 0) {
  81. return SDL_MUTEX_TIMEDOUT;
  82. }
  83. return 0; // Wait condition satisfied.
  84. }
  85. /* Returns the current count of the semaphore */
  86. Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
  87. {
  88. ee_sema_t info;
  89. if (!sem) {
  90. SDL_InvalidParamError("sem");
  91. return 0;
  92. }
  93. if (ReferSemaStatus(sem->semid, &info) >= 0) {
  94. return info.count;
  95. }
  96. return 0;
  97. }
  98. int SDL_PostSemaphore(SDL_Semaphore *sem)
  99. {
  100. int res;
  101. if (!sem) {
  102. return SDL_InvalidParamError("sem");
  103. }
  104. res = SignalSema(sem->semid);
  105. if (res < 0) {
  106. return SDL_SetError("sceKernelSignalSema() failed");
  107. }
  108. return 0;
  109. }
  110. #endif /* SDL_THREAD_PS2 */