SDL_syssem.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #include "SDL_error.h"
  22. #include "SDL_thread.h"
  23. #define SDL_MUTEX_TIMEOUT -2
  24. struct SDL_semaphore
  25. {
  26. TInt handle;
  27. TInt count;
  28. };
  29. struct TInfo
  30. {
  31. TInfo(TInt aTime, TInt aHandle) :
  32. iTime(aTime), iHandle(aHandle), iVal(0) {}
  33. TInt iTime;
  34. TInt iHandle;
  35. TInt iVal;
  36. };
  37. extern TInt CreateUnique(TInt (*aFunc)(const TDesC& aName, TAny*, TAny*), TAny*, TAny*);
  38. static TBool RunThread(TAny* aInfo)
  39. {
  40. TInfo* info = STATIC_CAST(TInfo*, aInfo);
  41. User::After(info->iTime);
  42. RSemaphore sema;
  43. sema.SetHandle(info->iHandle);
  44. sema.Signal();
  45. info->iVal = SDL_MUTEX_TIMEOUT;
  46. return 0;
  47. }
  48. static TInt
  49. NewThread(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
  50. {
  51. return ((RThread*)(aPtr1))->Create
  52. (aName,
  53. RunThread,
  54. KDefaultStackSize,
  55. NULL,
  56. aPtr2);
  57. }
  58. static TInt NewSema(const TDesC& aName, TAny* aPtr1, TAny* aPtr2)
  59. {
  60. TInt value = *((TInt*) aPtr2);
  61. return ((RSemaphore*)aPtr1)->CreateGlobal(aName, value);
  62. }
  63. static void WaitAll(SDL_sem *sem)
  64. {
  65. RSemaphore sema;
  66. sema.SetHandle(sem->handle);
  67. sema.Wait();
  68. while(sem->count < 0)
  69. {
  70. sema.Wait();
  71. }
  72. }
  73. SDL_sem *
  74. SDL_CreateSemaphore(Uint32 initial_value)
  75. {
  76. RSemaphore s;
  77. TInt status = CreateUnique(NewSema, &s, &initial_value);
  78. if(status != KErrNone)
  79. {
  80. SDL_SetError("Couldn't create semaphore");
  81. }
  82. SDL_semaphore* sem = new /*(ELeave)*/ SDL_semaphore;
  83. sem->handle = s.Handle();
  84. sem->count = initial_value;
  85. return(sem);
  86. }
  87. void
  88. SDL_DestroySemaphore(SDL_sem * sem)
  89. {
  90. if (sem)
  91. {
  92. RSemaphore sema;
  93. sema.SetHandle(sem->handle);
  94. sema.Signal(sema.Count());
  95. sema.Close();
  96. delete sem;
  97. sem = NULL;
  98. }
  99. }
  100. int
  101. SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
  102. {
  103. if (! sem)
  104. {
  105. SDL_SetError("Passed a NULL sem");
  106. return -1;
  107. }
  108. if (timeout == SDL_MUTEX_MAXWAIT)
  109. {
  110. WaitAll(sem);
  111. return SDL_MUTEX_MAXWAIT;
  112. }
  113. RThread thread;
  114. TInfo* info = new (ELeave)TInfo(timeout, sem->handle);
  115. TInt status = CreateUnique(NewThread, &thread, info);
  116. if(status != KErrNone)
  117. {
  118. return status;
  119. }
  120. thread.Resume();
  121. WaitAll(sem);
  122. if(thread.ExitType() == EExitPending)
  123. {
  124. thread.Kill(SDL_MUTEX_TIMEOUT);
  125. }
  126. thread.Close();
  127. return info->iVal;
  128. }
  129. int
  130. SDL_SemTryWait(SDL_sem *sem)
  131. {
  132. if(sem->count > 0)
  133. {
  134. sem->count--;
  135. }
  136. return SDL_MUTEX_TIMEOUT;
  137. }
  138. int
  139. SDL_SemWait(SDL_sem * sem)
  140. {
  141. return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
  142. }
  143. Uint32
  144. SDL_SemValue(SDL_sem * sem)
  145. {
  146. if (! sem)
  147. {
  148. SDL_SetError("Passed a NULL sem.");
  149. return 0;
  150. }
  151. return sem->count;
  152. }
  153. int
  154. SDL_SemPost(SDL_sem * sem)
  155. {
  156. if (! sem)
  157. {
  158. SDL_SetError("Passed a NULL sem.");
  159. return -1;
  160. }
  161. sem->count++;
  162. RSemaphore sema;
  163. sema.SetHandle(sem->handle);
  164. sema.Signal();
  165. return 0;
  166. }
  167. /* vi: set ts=4 sw=4 expandtab: */