SDL_syssem.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. /* An implementation of semaphores using the Symbian API. */
  20. #include <e32std.h>
  21. #define SDL_MUTEX_TIMEOUT -2
  22. struct SDL_semaphore
  23. {
  24. TInt handle;
  25. TInt count;
  26. };
  27. struct TInfo
  28. {
  29. TInfo(TInt aTime, TInt aHandle) : iTime(aTime), iHandle(aHandle), iVal(0) {}
  30. TInt iTime;
  31. TInt iHandle;
  32. TInt iVal;
  33. };
  34. extern TInt CreateUnique(TInt (*aFunc)(const TDesC &aName, TAny *, TAny *), TAny *, TAny *);
  35. static TBool RunThread(TAny *aInfo)
  36. {
  37. TInfo *info = STATIC_CAST(TInfo *, aInfo);
  38. User::After(info->iTime);
  39. RSemaphore sema;
  40. sema.SetHandle(info->iHandle);
  41. sema.Signal();
  42. info->iVal = SDL_MUTEX_TIMEOUT;
  43. return 0;
  44. }
  45. static TInt NewThread(const TDesC &aName, TAny *aPtr1, TAny *aPtr2)
  46. {
  47. return ((RThread *)(aPtr1))->Create(aName, RunThread, KDefaultStackSize, NULL, aPtr2);
  48. }
  49. static TInt NewSema(const TDesC &aName, TAny *aPtr1, TAny *aPtr2)
  50. {
  51. TInt value = *((TInt *)aPtr2);
  52. return ((RSemaphore *)aPtr1)->CreateGlobal(aName, value);
  53. }
  54. static void WaitAll(SDL_sem *sem)
  55. {
  56. RSemaphore sema;
  57. sema.SetHandle(sem->handle);
  58. sema.Wait();
  59. while (sem->count < 0) {
  60. sema.Wait();
  61. }
  62. }
  63. SDL_sem *
  64. SDL_CreateSemaphore(Uint32 initial_value)
  65. {
  66. RSemaphore s;
  67. TInt status = CreateUnique(NewSema, &s, &initial_value);
  68. if (status != KErrNone) {
  69. SDL_SetError("Couldn't create semaphore");
  70. }
  71. SDL_semaphore *sem = new /*(ELeave)*/ SDL_semaphore;
  72. sem->handle = s.Handle();
  73. sem->count = initial_value;
  74. return sem;
  75. }
  76. void SDL_DestroySemaphore(SDL_sem *sem)
  77. {
  78. if (sem != NULL) {
  79. RSemaphore sema;
  80. sema.SetHandle(sem->handle);
  81. sema.Signal(sema.Count());
  82. sema.Close();
  83. delete sem;
  84. sem = NULL;
  85. }
  86. }
  87. int SDL_SemWaitTimeoutNS(SDL_sem *sem, Sint64 timeoutNS)
  88. {
  89. if (sem == NULL) {
  90. return SDL_InvalidParamError("sem");
  91. }
  92. if (timeoutNS == 0) {
  93. if (sem->count > 0) {
  94. --sem->count;
  95. return 0;
  96. }
  97. return SDL_MUTEX_TIMEOUT;
  98. }
  99. if (timeoutNS == SDL_MUTEX_MAXWAIT) {
  100. WaitAll(sem);
  101. return 0;
  102. }
  103. RThread thread;
  104. TInfo *info = new (ELeave) TInfo((TInt)SDL_NS_TO_MS(timeoutNS), sem->handle);
  105. TInt status = CreateUnique(NewThread, &thread, info);
  106. if (status != KErrNone) {
  107. return status;
  108. }
  109. thread.Resume();
  110. WaitAll(sem);
  111. if (thread.ExitType() == EExitPending) {
  112. thread.Kill(SDL_MUTEX_TIMEOUT);
  113. }
  114. thread.Close();
  115. return info->iVal;
  116. }
  117. Uint32
  118. SDL_SemValue(SDL_sem *sem)
  119. {
  120. if (sem == NULL) {
  121. SDL_InvalidParamError("sem");
  122. return 0;
  123. }
  124. return sem->count;
  125. }
  126. int SDL_SemPost(SDL_sem *sem)
  127. {
  128. if (sem == NULL) {
  129. return SDL_InvalidParamError("sem");
  130. }
  131. sem->count++;
  132. RSemaphore sema;
  133. sema.SetHandle(sem->handle);
  134. sema.Signal();
  135. return 0;
  136. }
  137. /* vi: set ts=4 sw=4 expandtab: */