SDL_sysmutex.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. #ifdef SDL_THREAD_PSP
  20. // An implementation of mutexes using semaphores
  21. #include "SDL_systhread_c.h"
  22. #include <pspthreadman.h>
  23. #include <pspkerror.h>
  24. #define SCE_KERNEL_MUTEX_ATTR_RECURSIVE 0x0200U
  25. struct SDL_Mutex
  26. {
  27. SceLwMutexWorkarea lock;
  28. };
  29. SDL_Mutex *SDL_CreateMutex(void)
  30. {
  31. SDL_Mutex *mutex = (SDL_Mutex *)SDL_malloc(sizeof(*mutex));
  32. if (mutex) {
  33. const SceInt32 res = sceKernelCreateLwMutex(
  34. &mutex->lock,
  35. "SDL mutex",
  36. SCE_KERNEL_MUTEX_ATTR_RECURSIVE,
  37. 0,
  38. NULL);
  39. if (res < 0) {
  40. SDL_free(mutex);
  41. mutex = NULL;
  42. SDL_SetError("Error trying to create mutex: %lx", res);
  43. }
  44. } else {
  45. SDL_OutOfMemory();
  46. }
  47. return mutex;
  48. }
  49. void SDL_DestroyMutex(SDL_Mutex *mutex)
  50. {
  51. if (mutex) {
  52. sceKernelDeleteLwMutex(&mutex->lock);
  53. SDL_free(mutex);
  54. }
  55. }
  56. void SDL_LockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  57. {
  58. #ifndef SDL_THREADS_DISABLED
  59. if (mutex != NULL) {
  60. const SceInt32 res = sceKernelLockLwMutex(&mutex->lock, 1, NULL);
  61. SDL_assert(res == SCE_KERNEL_ERROR_OK); // assume we're in a lot of trouble if this assert fails.
  62. }
  63. #endif // SDL_THREADS_DISABLED
  64. }
  65. int SDL_TryLockMutex(SDL_Mutex *mutex)
  66. {
  67. int retval = 0;
  68. #ifndef SDL_THREADS_DISABLED
  69. if (mutex != NULL) {
  70. const SceInt32 res = sceKernelTryLockLwMutex(&mutex->lock, 1);
  71. if (res == SCE_KERNEL_ERROR_OK) {
  72. retval = 0;
  73. } else if (res == SCE_KERNEL_ERROR_WAIT_TIMEOUT) {
  74. retval = SDL_MUTEX_TIMEDOUT;
  75. } else {
  76. SDL_assert(!"Error trying to lock mutex"); // assume we're in a lot of trouble if this assert fails.
  77. retval = SDL_MUTEX_TIMEDOUT;
  78. }
  79. }
  80. #endif // SDL_THREADS_DISABLED
  81. return retval;
  82. }
  83. void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_NO_THREAD_SAFETY_ANALYSIS // clang doesn't know about NULL mutexes
  84. {
  85. #ifndef SDL_THREADS_DISABLED
  86. if (mutex != NULL) {
  87. const SceInt32 res = sceKernelUnlockLwMutex(&mutex->lock, 1);
  88. SDL_assert(res == 0); // assume we're in a lot of trouble if this assert fails.
  89. }
  90. #endif // SDL_THREADS_DISABLED
  91. }
  92. #endif // SDL_THREAD_PSP