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

https://github.com/aichunyu/FFPlayer · C · 155 lines · 108 code · 21 blank · 26 comment · 15 complexity · 248efb7536dca3ece7fc3c84ab905c25 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. #ifdef SDL_THREAD_BEOS
  20. /* Semaphores in the BeOS environment */
  21. #include <be/kernel/OS.h>
  22. #include "SDL_thread.h"
  23. struct SDL_semaphore
  24. {
  25. sem_id id;
  26. };
  27. /* Create a counting semaphore */
  28. SDL_sem *
  29. SDL_CreateSemaphore(Uint32 initial_value)
  30. {
  31. SDL_sem *sem;
  32. sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
  33. if (sem) {
  34. sem->id = create_sem(initial_value, "SDL semaphore");
  35. if (sem->id < B_NO_ERROR) {
  36. SDL_SetError("create_sem() failed");
  37. SDL_free(sem);
  38. sem = NULL;
  39. }
  40. } else {
  41. SDL_OutOfMemory();
  42. }
  43. return (sem);
  44. }
  45. /* Free the semaphore */
  46. void
  47. SDL_DestroySemaphore(SDL_sem * sem)
  48. {
  49. if (sem) {
  50. if (sem->id >= B_NO_ERROR) {
  51. delete_sem(sem->id);
  52. }
  53. SDL_free(sem);
  54. }
  55. }
  56. int
  57. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  58. {
  59. int32 val;
  60. int retval;
  61. if (!sem) {
  62. SDL_SetError("Passed a NULL semaphore");
  63. return -1;
  64. }
  65. tryagain:
  66. if (timeout == SDL_MUTEX_MAXWAIT) {
  67. val = acquire_sem(sem->id);
  68. } else {
  69. timeout *= 1000; /* BeOS uses a timeout in microseconds */
  70. val = acquire_sem_etc(sem->id, 1, B_RELATIVE_TIMEOUT, timeout);
  71. }
  72. switch (val) {
  73. case B_INTERRUPTED:
  74. goto tryagain;
  75. case B_NO_ERROR:
  76. retval = 0;
  77. break;
  78. case B_TIMED_OUT:
  79. retval = SDL_MUTEX_TIMEDOUT;
  80. break;
  81. case B_WOULD_BLOCK:
  82. retval = SDL_MUTEX_TIMEDOUT;
  83. break;
  84. default:
  85. SDL_SetError("acquire_sem() failed");
  86. retval = -1;
  87. break;
  88. }
  89. return retval;
  90. }
  91. int
  92. SDL_SemTryWait(SDL_sem * sem)
  93. {
  94. return SDL_SemWaitTimeout(sem, 0);
  95. }
  96. int
  97. SDL_SemWait(SDL_sem * sem)
  98. {
  99. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  100. }
  101. /* Returns the current count of the semaphore */
  102. Uint32
  103. SDL_SemValue(SDL_sem * sem)
  104. {
  105. int32 count;
  106. Uint32 value;
  107. value = 0;
  108. if (sem) {
  109. get_sem_count(sem->id, &count);
  110. if (count > 0) {
  111. value = (Uint32) count;
  112. }
  113. }
  114. return value;
  115. }
  116. /* Atomically increases the semaphore's count (not blocking) */
  117. int
  118. SDL_SemPost(SDL_sem * sem)
  119. {
  120. if (!sem) {
  121. SDL_SetError("Passed a NULL semaphore");
  122. return -1;
  123. }
  124. if (release_sem(sem->id) != B_NO_ERROR) {
  125. SDL_SetError("release_sem() failed");
  126. return -1;
  127. }
  128. return 0;
  129. }
  130. #endif /* SDL_THREAD_BEOS */
  131. /* vi: set ts=4 sw=4 expandtab: */