SDL_syssem.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_OS2
  20. /* An implementation of semaphores for OS/2 */
  21. #include "SDL_thread.h"
  22. #include "../../core/os2/SDL_os2.h"
  23. #define INCL_DOSSEMAPHORES
  24. #define INCL_DOSERRORS
  25. #define INCL_DOSMISC
  26. #include <os2.h>
  27. struct SDL_semaphore {
  28. HEV hEv;
  29. HMTX hMtx;
  30. ULONG cPost;
  31. };
  32. SDL_sem *SDL_CreateSemaphore(Uint32 initial_value)
  33. {
  34. ULONG ulRC;
  35. SDL_sem *pSDLSem = SDL_malloc(sizeof(SDL_sem));
  36. if (pSDLSem == NULL) {
  37. SDL_OutOfMemory();
  38. return NULL;
  39. }
  40. ulRC = DosCreateEventSem(NULL, &pSDLSem->hEv, DCE_AUTORESET, FALSE);
  41. if (ulRC != NO_ERROR) {
  42. debug_os2("DosCreateEventSem(), rc = %u", ulRC);
  43. SDL_free(pSDLSem);
  44. return NULL;
  45. }
  46. ulRC = DosCreateMutexSem(NULL, &pSDLSem->hMtx, 0, FALSE);
  47. if (ulRC != NO_ERROR) {
  48. debug_os2("DosCreateMutexSem(), rc = %u", ulRC);
  49. DosCloseEventSem(pSDLSem->hEv);
  50. SDL_free(pSDLSem);
  51. return NULL;
  52. }
  53. pSDLSem->cPost = initial_value;
  54. return pSDLSem;
  55. }
  56. void SDL_DestroySemaphore(SDL_sem * sem)
  57. {
  58. if (!sem) return;
  59. DosCloseMutexSem(sem->hMtx);
  60. DosCloseEventSem(sem->hEv);
  61. SDL_free(sem);
  62. }
  63. int SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  64. {
  65. ULONG ulRC;
  66. ULONG ulStartTime, ulCurTime;
  67. ULONG ulTimeout;
  68. ULONG cPost;
  69. if (sem == NULL)
  70. return SDL_InvalidParamError("sem");
  71. if (timeout != SEM_INDEFINITE_WAIT)
  72. DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulStartTime, sizeof(ULONG));
  73. while (TRUE) {
  74. ulRC = DosRequestMutexSem(sem->hMtx, SEM_INDEFINITE_WAIT);
  75. if (ulRC != NO_ERROR)
  76. return SDL_SetError("DosRequestMutexSem() failed, rc = %u", ulRC);
  77. cPost = sem->cPost;
  78. if (sem->cPost != 0)
  79. sem->cPost--;
  80. DosReleaseMutexSem(sem->hMtx);
  81. if (cPost != 0)
  82. break;
  83. if (timeout == SEM_INDEFINITE_WAIT)
  84. ulTimeout = SEM_INDEFINITE_WAIT;
  85. else {
  86. DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulCurTime, sizeof(ULONG));
  87. ulTimeout = ulCurTime - ulStartTime;
  88. if (timeout < ulTimeout)
  89. return SDL_MUTEX_TIMEDOUT;
  90. ulTimeout = timeout - ulTimeout;
  91. }
  92. ulRC = DosWaitEventSem(sem->hEv, ulTimeout);
  93. if (ulRC == ERROR_TIMEOUT)
  94. return SDL_MUTEX_TIMEDOUT;
  95. if (ulRC != NO_ERROR)
  96. return SDL_SetError("DosWaitEventSem() failed, rc = %u", ulRC);
  97. }
  98. return 0;
  99. }
  100. int SDL_SemTryWait(SDL_sem * sem)
  101. {
  102. return SDL_SemWaitTimeout(sem, 0);
  103. }
  104. int SDL_SemWait(SDL_sem * sem)
  105. {
  106. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  107. }
  108. Uint32 SDL_SemValue(SDL_sem * sem)
  109. {
  110. ULONG ulRC;
  111. if (sem == NULL) {
  112. SDL_InvalidParamError("sem");
  113. return 0;
  114. }
  115. ulRC = DosRequestMutexSem(sem->hMtx, SEM_INDEFINITE_WAIT);
  116. if (ulRC != NO_ERROR)
  117. return SDL_SetError("DosRequestMutexSem() failed, rc = %u", ulRC);
  118. ulRC = sem->cPost;
  119. DosReleaseMutexSem(sem->hMtx);
  120. return ulRC;
  121. }
  122. int SDL_SemPost(SDL_sem * sem)
  123. {
  124. ULONG ulRC;
  125. if (sem == NULL)
  126. return SDL_InvalidParamError("sem");
  127. ulRC = DosRequestMutexSem(sem->hMtx, SEM_INDEFINITE_WAIT);
  128. if (ulRC != NO_ERROR)
  129. return SDL_SetError("DosRequestMutexSem() failed, rc = %u", ulRC);
  130. sem->cPost++;
  131. ulRC = DosPostEventSem(sem->hEv);
  132. if (ulRC != NO_ERROR && ulRC != ERROR_ALREADY_POSTED) {
  133. debug_os2("DosPostEventSem() failed, rc = %u", ulRC);
  134. }
  135. DosReleaseMutexSem(sem->hMtx);
  136. return 0;
  137. }
  138. #endif /* SDL_THREAD_OS2 */
  139. /* vi: set ts=4 sw=4 expandtab: */