/project/jni/sdl-1.3/src/thread/windows/SDL_syssem.c

https://github.com/aichunyu/FFPlayer · C · 176 lines · 125 code · 19 blank · 32 comment · 23 complexity · 559459122376f662091fb41f28e335a4 MD5 · raw file

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 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_config.h"
  19. #if SDL_THREAD_WINDOWS
  20. /* Semaphore functions using the Win32 API */
  21. #include "../../core/windows/SDL_windows.h"
  22. #include "SDL_thread.h"
  23. #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
  24. #include "win_ce_semaphore.h"
  25. #endif
  26. struct SDL_semaphore
  27. {
  28. #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
  29. SYNCHHANDLE id;
  30. #else
  31. HANDLE id;
  32. #endif
  33. LONG count;
  34. };
  35. /* Create a semaphore */
  36. SDL_sem *
  37. SDL_CreateSemaphore(Uint32 initial_value)
  38. {
  39. SDL_sem *sem;
  40. /* Allocate sem memory */
  41. sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
  42. if (sem) {
  43. /* Create the semaphore, with max value 32K */
  44. #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
  45. sem->id = CreateSemaphoreCE(NULL, initial_value, 32 * 1024, NULL);
  46. #else
  47. sem->id = CreateSemaphore(NULL, initial_value, 32 * 1024, NULL);
  48. #endif
  49. sem->count = initial_value;
  50. if (!sem->id) {
  51. SDL_SetError("Couldn't create semaphore");
  52. SDL_free(sem);
  53. sem = NULL;
  54. }
  55. } else {
  56. SDL_OutOfMemory();
  57. }
  58. return (sem);
  59. }
  60. /* Free the semaphore */
  61. void
  62. SDL_DestroySemaphore(SDL_sem * sem)
  63. {
  64. if (sem) {
  65. if (sem->id) {
  66. #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
  67. CloseSynchHandle(sem->id);
  68. #else
  69. CloseHandle(sem->id);
  70. #endif
  71. sem->id = 0;
  72. }
  73. SDL_free(sem);
  74. }
  75. }
  76. int
  77. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  78. {
  79. int retval;
  80. DWORD dwMilliseconds;
  81. if (!sem) {
  82. SDL_SetError("Passed a NULL sem");
  83. return -1;
  84. }
  85. if (timeout == SDL_MUTEX_MAXWAIT) {
  86. dwMilliseconds = INFINITE;
  87. } else {
  88. dwMilliseconds = (DWORD) timeout;
  89. }
  90. #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
  91. switch (WaitForSemaphoreCE(sem->id, dwMilliseconds)) {
  92. #else
  93. switch (WaitForSingleObject(sem->id, dwMilliseconds)) {
  94. #endif
  95. case WAIT_OBJECT_0:
  96. InterlockedDecrement(&sem->count);
  97. retval = 0;
  98. break;
  99. case WAIT_TIMEOUT:
  100. retval = SDL_MUTEX_TIMEDOUT;
  101. break;
  102. default:
  103. SDL_SetError("WaitForSingleObject() failed");
  104. retval = -1;
  105. break;
  106. }
  107. return retval;
  108. }
  109. int
  110. SDL_SemTryWait(SDL_sem * sem)
  111. {
  112. return SDL_SemWaitTimeout(sem, 0);
  113. }
  114. int
  115. SDL_SemWait(SDL_sem * sem)
  116. {
  117. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  118. }
  119. /* Returns the current count of the semaphore */
  120. Uint32
  121. SDL_SemValue(SDL_sem * sem)
  122. {
  123. if (!sem) {
  124. SDL_SetError("Passed a NULL sem");
  125. return 0;
  126. }
  127. return (Uint32)sem->count;
  128. }
  129. int
  130. SDL_SemPost(SDL_sem * sem)
  131. {
  132. if (!sem) {
  133. SDL_SetError("Passed a NULL sem");
  134. return -1;
  135. }
  136. /* Increase the counter in the first place, because
  137. * after a successful release the semaphore may
  138. * immediately get destroyed by another thread which
  139. * is waiting for this semaphore.
  140. */
  141. InterlockedIncrement(&sem->count);
  142. #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
  143. if (ReleaseSemaphoreCE(sem->id, 1, NULL) == FALSE) {
  144. #else
  145. if (ReleaseSemaphore(sem->id, 1, NULL) == FALSE) {
  146. #endif
  147. InterlockedDecrement(&sem->count); /* restore */
  148. SDL_SetError("ReleaseSemaphore() failed");
  149. return -1;
  150. }
  151. return 0;
  152. }
  153. #endif /* SDL_THREAD_WINDOWS */
  154. /* vi: set ts=4 sw=4 expandtab: */