SDL_syssem.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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 *
  33. SDL_CreateSemaphore(Uint32 initial_value)
  34. {
  35. ULONG ulRC;
  36. SDL_sem *pSDLSem = SDL_malloc(sizeof(SDL_sem));
  37. if (pSDLSem == NULL) {
  38. SDL_OutOfMemory();
  39. return NULL;
  40. }
  41. ulRC = DosCreateEventSem(NULL, &pSDLSem->hEv, DCE_AUTORESET, FALSE);
  42. if (ulRC != NO_ERROR) {
  43. debug_os2("DosCreateEventSem(), rc = %u", ulRC);
  44. SDL_free(pSDLSem);
  45. return NULL;
  46. }
  47. ulRC = DosCreateMutexSem(NULL, &pSDLSem->hMtx, 0, FALSE);
  48. if (ulRC != NO_ERROR) {
  49. debug_os2("DosCreateMutexSem(), rc = %u", ulRC);
  50. DosCloseEventSem(pSDLSem->hEv);
  51. SDL_free(pSDLSem);
  52. return NULL;
  53. }
  54. pSDLSem->cPost = initial_value;
  55. return pSDLSem;
  56. }
  57. void
  58. SDL_DestroySemaphore(SDL_sem * sem)
  59. {
  60. if (!sem) return;
  61. DosCloseMutexSem(sem->hMtx);
  62. DosCloseEventSem(sem->hEv);
  63. SDL_free(sem);
  64. }
  65. int
  66. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  67. {
  68. ULONG ulRC;
  69. ULONG ulStartTime, ulCurTime;
  70. ULONG ulTimeout;
  71. ULONG cPost;
  72. if (sem == NULL)
  73. return SDL_SetError("Passed a NULL sem");
  74. if (timeout != SEM_INDEFINITE_WAIT)
  75. DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulStartTime, sizeof(ULONG));
  76. while (TRUE) {
  77. ulRC = DosRequestMutexSem(sem->hMtx, SEM_INDEFINITE_WAIT);
  78. if (ulRC != NO_ERROR)
  79. return SDL_SetError("DosRequestMutexSem() failed, rc = %u", ulRC);
  80. cPost = sem->cPost;
  81. if (sem->cPost != 0)
  82. sem->cPost--;
  83. DosReleaseMutexSem(sem->hMtx);
  84. if (cPost != 0)
  85. break;
  86. if (timeout == SEM_INDEFINITE_WAIT)
  87. ulTimeout = SEM_INDEFINITE_WAIT;
  88. else {
  89. DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulCurTime, sizeof(ULONG));
  90. ulTimeout = ulCurTime - ulStartTime;
  91. if (timeout < ulTimeout)
  92. return SDL_MUTEX_TIMEDOUT;
  93. ulTimeout = timeout - ulTimeout;
  94. }
  95. ulRC = DosWaitEventSem(sem->hEv, ulTimeout);
  96. if (ulRC == ERROR_TIMEOUT)
  97. return SDL_MUTEX_TIMEDOUT;
  98. if (ulRC != NO_ERROR)
  99. return SDL_SetError("DosWaitEventSem() failed, rc = %u", ulRC);
  100. }
  101. return 0;
  102. }
  103. int
  104. SDL_SemTryWait(SDL_sem * sem)
  105. {
  106. return SDL_SemWaitTimeout(sem, 0);
  107. }
  108. int
  109. SDL_SemWait(SDL_sem * sem)
  110. {
  111. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  112. }
  113. Uint32
  114. SDL_SemValue(SDL_sem * sem)
  115. {
  116. ULONG ulRC;
  117. if (sem == NULL) {
  118. SDL_SetError("Passed a NULL sem");
  119. return 0;
  120. }
  121. ulRC = DosRequestMutexSem(sem->hMtx, SEM_INDEFINITE_WAIT);
  122. if (ulRC != NO_ERROR)
  123. return SDL_SetError("DosRequestMutexSem() failed, rc = %u", ulRC);
  124. ulRC = sem->cPost;
  125. DosReleaseMutexSem(sem->hMtx);
  126. return ulRC;
  127. }
  128. int
  129. SDL_SemPost(SDL_sem * sem)
  130. {
  131. ULONG ulRC;
  132. if (sem == NULL)
  133. return SDL_SetError("Passed a NULL sem");
  134. ulRC = DosRequestMutexSem(sem->hMtx, SEM_INDEFINITE_WAIT);
  135. if (ulRC != NO_ERROR)
  136. return SDL_SetError("DosRequestMutexSem() failed, rc = %u", ulRC);
  137. sem->cPost++;
  138. ulRC = DosPostEventSem(sem->hEv);
  139. if (ulRC != NO_ERROR && ulRC != ERROR_ALREADY_POSTED) {
  140. debug_os2("DosPostEventSem() failed, rc = %u", ulRC);
  141. }
  142. DosReleaseMutexSem(sem->hMtx);
  143. return 0;
  144. }
  145. #endif /* SDL_THREAD_OS2 */
  146. /* vi: set ts=4 sw=4 expandtab: */