SDL_sysmutex.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 mutexes using the Symbian API. */
  20. #include <e32std.h>
  21. #include "SDL_systhread_c.h"
  22. struct SDL_mutex
  23. {
  24. TInt handle;
  25. };
  26. extern TInt CreateUnique(TInt (*aFunc)(const TDesC &aName, TAny *, TAny *), TAny *, TAny *);
  27. static TInt NewMutex(const TDesC &aName, TAny *aPtr1, TAny *)
  28. {
  29. return ((RMutex *)aPtr1)->CreateGlobal(aName);
  30. }
  31. /* Create a mutex */
  32. SDL_mutex *
  33. SDL_CreateMutex(void)
  34. {
  35. RMutex rmutex;
  36. TInt status = CreateUnique(NewMutex, &rmutex, NULL);
  37. if (status != KErrNone) {
  38. SDL_SetError("Couldn't create mutex.");
  39. return NULL;
  40. }
  41. SDL_mutex *mutex = new /*(ELeave)*/ SDL_mutex;
  42. mutex->handle = rmutex.Handle();
  43. return mutex;
  44. }
  45. /* Free the mutex */
  46. void SDL_DestroyMutex(SDL_mutex *mutex)
  47. {
  48. if (mutex) {
  49. RMutex rmutex;
  50. rmutex.SetHandle(mutex->handle);
  51. rmutex.Signal();
  52. rmutex.Close();
  53. delete (mutex);
  54. mutex = NULL;
  55. }
  56. }
  57. /* Lock the mutex */
  58. int SDL_LockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  59. {
  60. if (mutex == NULL) {
  61. return 0;
  62. }
  63. RMutex rmutex;
  64. rmutex.SetHandle(mutex->handle);
  65. rmutex.Wait();
  66. return 0;
  67. }
  68. /* Try to lock the mutex */
  69. #if 0
  70. int
  71. SDL_TryLockMutex(SDL_mutex *mutex)
  72. {
  73. if (mutex == NULL)
  74. {
  75. return 0;
  76. }
  77. // Not yet implemented.
  78. return 0;
  79. }
  80. #endif
  81. /* Unlock the mutex */
  82. int SDL_UnlockMutex(SDL_mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS /* clang doesn't know about NULL mutexes */
  83. {
  84. if (mutex == NULL) {
  85. return 0;
  86. }
  87. RMutex rmutex;
  88. rmutex.SetHandle(mutex->handle);
  89. rmutex.Signal();
  90. return 0;
  91. }