SDL_syscond.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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_N3DS
  20. // An implementation of condition variables using libctru's CondVar
  21. #include "SDL_sysmutex_c.h"
  22. struct SDL_Condition
  23. {
  24. CondVar cond_variable;
  25. };
  26. // Create a condition variable
  27. SDL_Condition *SDL_CreateCondition(void)
  28. {
  29. SDL_Condition *cond = (SDL_Condition *)SDL_malloc(sizeof(SDL_Condition));
  30. if (cond) {
  31. CondVar_Init(&cond->cond_variable);
  32. }
  33. return cond;
  34. }
  35. // Destroy a condition variable
  36. void SDL_DestroyCondition(SDL_Condition *cond)
  37. {
  38. if (cond) {
  39. SDL_free(cond);
  40. }
  41. }
  42. // Restart one of the threads that are waiting on the condition variable
  43. void SDL_SignalCondition(SDL_Condition *cond)
  44. {
  45. if (!cond) {
  46. return;
  47. }
  48. CondVar_Signal(&cond->cond_variable);
  49. }
  50. // Restart all threads that are waiting on the condition variable
  51. void SDL_BroadcastCondition(SDL_Condition *cond)
  52. {
  53. if (!cond) {
  54. return;
  55. }
  56. CondVar_Broadcast(&cond->cond_variable);
  57. }
  58. /* Wait on the condition variable for at most 'timeoutNS' nanoseconds.
  59. The mutex must be locked before entering this function!
  60. The mutex is unlocked during the wait, and locked again after the wait.
  61. Typical use:
  62. Thread A:
  63. SDL_LockMutex(lock);
  64. while ( ! condition ) {
  65. SDL_WaitCondition(cond, lock);
  66. }
  67. SDL_UnlockMutex(lock);
  68. Thread B:
  69. SDL_LockMutex(lock);
  70. ...
  71. condition = true;
  72. ...
  73. SDL_SignalCondition(cond);
  74. SDL_UnlockMutex(lock);
  75. */
  76. bool SDL_WaitConditionTimeoutNS(SDL_Condition *cond, SDL_Mutex *mutex, Sint64 timeoutNS)
  77. {
  78. Result res;
  79. if (!cond || !mutex) {
  80. return true;
  81. }
  82. res = 0;
  83. if (timeoutNS < 0) {
  84. CondVar_Wait(&cond->cond_variable, &mutex->lock.lock);
  85. } else {
  86. res = CondVar_WaitTimeout(&cond->cond_variable, &mutex->lock.lock, timeoutNS);
  87. }
  88. return R_SUCCEEDED(res);
  89. }
  90. #endif // SDL_THREAD_N3DS