SDL_syssem.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #if SDL_THREAD_PS2
  20. /* Semaphore functions for the PS2. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <kernel_util.h>
  24. #include "SDL_error.h"
  25. #include "SDL_thread.h"
  26. #include <kernel.h>
  27. struct SDL_semaphore
  28. {
  29. s32 semid;
  30. };
  31. /* Create a semaphore */
  32. SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
  33. {
  34. SDL_sem *sem;
  35. ee_sema_t sema;
  36. sem = (SDL_sem *)SDL_malloc(sizeof(*sem));
  37. if (sem != NULL) {
  38. /* TODO: Figure out the limit on the maximum value. */
  39. sema.init_count = initial_value;
  40. sema.max_count = 255;
  41. sema.option = 0;
  42. sem->semid = CreateSema(&sema);
  43. if (sem->semid < 0) {
  44. SDL_SetError("Couldn't create semaphore");
  45. SDL_free(sem);
  46. sem = NULL;
  47. }
  48. } else {
  49. SDL_OutOfMemory();
  50. }
  51. return sem;
  52. }
  53. /* Free the semaphore */
  54. void SDL_DestroySemaphore(SDL_sem *sem)
  55. {
  56. if (sem != NULL) {
  57. if (sem->semid > 0) {
  58. DeleteSema(sem->semid);
  59. sem->semid = 0;
  60. }
  61. SDL_free(sem);
  62. }
  63. }
  64. int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
  65. {
  66. int ret;
  67. u64 timeout_usec;
  68. u64 *timeout_ptr;
  69. if (sem == NULL) {
  70. return SDL_InvalidParamError("sem");
  71. }
  72. if (timeout == 0) {
  73. if (PollSema(sem->semid) < 0) {
  74. return SDL_MUTEX_TIMEDOUT;
  75. }
  76. return 0;
  77. }
  78. timeout_ptr = NULL;
  79. if (timeout != SDL_MUTEX_MAXWAIT) {
  80. timeout_usec = timeout * 1000;
  81. timeout_ptr = &timeout_usec;
  82. }
  83. ret = WaitSemaEx(sem->semid, 1, timeout_ptr);
  84. if (ret < 0) {
  85. return SDL_MUTEX_TIMEDOUT;
  86. }
  87. return 0; // Wait condition satisfied.
  88. }
  89. int SDL_SemTryWait(SDL_sem *sem)
  90. {
  91. return SDL_SemWaitTimeout(sem, 0);
  92. }
  93. int SDL_SemWait(SDL_sem *sem)
  94. {
  95. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  96. }
  97. /* Returns the current count of the semaphore */
  98. Uint32 SDL_SemValue(SDL_sem *sem)
  99. {
  100. ee_sema_t info;
  101. if (sem == NULL) {
  102. SDL_InvalidParamError("sem");
  103. return 0;
  104. }
  105. if (ReferSemaStatus(sem->semid, &info) >= 0) {
  106. return info.count;
  107. }
  108. return 0;
  109. }
  110. int SDL_SemPost(SDL_sem *sem)
  111. {
  112. int res;
  113. if (sem == NULL) {
  114. return SDL_InvalidParamError("sem");
  115. }
  116. res = SignalSema(sem->semid);
  117. if (res < 0) {
  118. return SDL_SetError("sceKernelSignalSema() failed");
  119. }
  120. return 0;
  121. }
  122. #endif /* SDL_THREAD_PS2 */
  123. /* vim: ts=4 sw=4
  124. */