SDL_sysmutex.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #if SDL_THREAD_VITA
  20. #include "SDL_thread.h"
  21. #include "SDL_systhread_c.h"
  22. #include <psp2/kernel/threadmgr.h>
  23. #include <psp2/kernel/error.h>
  24. struct SDL_mutex
  25. {
  26. SceKernelLwMutexWork lock;
  27. };
  28. /* Create a mutex */
  29. SDL_mutex *
  30. SDL_CreateMutex(void)
  31. {
  32. SDL_mutex *mutex = NULL;
  33. SceInt32 res = 0;
  34. /* Allocate mutex memory */
  35. mutex = (SDL_mutex *) SDL_malloc(sizeof(*mutex));
  36. if (mutex) {
  37. res = sceKernelCreateLwMutex(
  38. &mutex->lock,
  39. "SDL mutex",
  40. SCE_KERNEL_MUTEX_ATTR_RECURSIVE,
  41. 0,
  42. NULL
  43. );
  44. if (res < 0) {
  45. SDL_SetError("Error trying to create mutex: %x", res);
  46. }
  47. } else {
  48. SDL_OutOfMemory();
  49. }
  50. return mutex;
  51. }
  52. /* Free the mutex */
  53. void
  54. SDL_DestroyMutex(SDL_mutex * mutex)
  55. {
  56. if (mutex) {
  57. sceKernelDeleteLwMutex(&mutex->lock);
  58. SDL_free(mutex);
  59. }
  60. }
  61. /* Try to lock the mutex */
  62. int
  63. SDL_TryLockMutex(SDL_mutex * mutex)
  64. {
  65. #if SDL_THREADS_DISABLED
  66. return 0;
  67. #else
  68. SceInt32 res = 0;
  69. if (mutex == NULL) {
  70. return SDL_InvalidParamError("mutex");
  71. }
  72. res = sceKernelTryLockLwMutex(&mutex->lock, 1);
  73. switch (res) {
  74. case SCE_KERNEL_OK:
  75. return 0;
  76. break;
  77. case SCE_KERNEL_ERROR_MUTEX_FAILED_TO_OWN:
  78. return SDL_MUTEX_TIMEDOUT;
  79. break;
  80. default:
  81. return SDL_SetError("Error trying to lock mutex: %x", res);
  82. break;
  83. }
  84. return -1;
  85. #endif /* SDL_THREADS_DISABLED */
  86. }
  87. /* Lock the mutex */
  88. int
  89. SDL_mutexP(SDL_mutex * mutex)
  90. {
  91. #if SDL_THREADS_DISABLED
  92. return 0;
  93. #else
  94. SceInt32 res = 0;
  95. if (mutex == NULL) {
  96. return SDL_InvalidParamError("mutex");
  97. }
  98. res = sceKernelLockLwMutex(&mutex->lock, 1, NULL);
  99. if (res != SCE_KERNEL_OK) {
  100. return SDL_SetError("Error trying to lock mutex: %x", res);
  101. }
  102. return 0;
  103. #endif /* SDL_THREADS_DISABLED */
  104. }
  105. /* Unlock the mutex */
  106. int
  107. SDL_mutexV(SDL_mutex * mutex)
  108. {
  109. #if SDL_THREADS_DISABLED
  110. return 0;
  111. #else
  112. SceInt32 res = 0;
  113. if (mutex == NULL) {
  114. return SDL_InvalidParamError("mutex");
  115. }
  116. res = sceKernelUnlockLwMutex(&mutex->lock, 1);
  117. if (res != 0) {
  118. return SDL_SetError("Error trying to unlock mutex: %x", res);
  119. }
  120. return 0;
  121. #endif /* SDL_THREADS_DISABLED */
  122. }
  123. #endif /* SDL_THREAD_VITA */
  124. /* vi: set ts=4 sw=4 expandtab: */