SDL_syscond.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. extern "C" {
  20. }
  21. #include <chrono>
  22. #include <condition_variable>
  23. #include <ratio>
  24. #include <system_error>
  25. #include "SDL_sysmutex_c.h"
  26. struct SDL_Condition
  27. {
  28. std::condition_variable_any cpp_cond;
  29. };
  30. /* Create a condition variable */
  31. extern "C" SDL_Condition *
  32. SDL_CreateCondition(void)
  33. {
  34. /* Allocate and initialize the condition variable */
  35. try {
  36. SDL_Condition *cond = new SDL_Condition;
  37. return cond;
  38. } catch (std::system_error &ex) {
  39. SDL_SetError("unable to create a C++ condition variable: code=%d; %s", ex.code(), ex.what());
  40. return NULL;
  41. } catch (std::bad_alloc &) {
  42. SDL_OutOfMemory();
  43. return NULL;
  44. }
  45. }
  46. /* Destroy a condition variable */
  47. extern "C" void
  48. SDL_DestroyCondition(SDL_Condition *cond)
  49. {
  50. if (cond) {
  51. delete cond;
  52. }
  53. }
  54. /* Restart one of the threads that are waiting on the condition variable */
  55. extern "C" int
  56. SDL_SignalCondition(SDL_Condition *cond)
  57. {
  58. if (!cond) {
  59. return SDL_InvalidParamError("cond");
  60. }
  61. cond->cpp_cond.notify_one();
  62. return 0;
  63. }
  64. /* Restart all threads that are waiting on the condition variable */
  65. extern "C" int
  66. SDL_BroadcastCondition(SDL_Condition *cond)
  67. {
  68. if (!cond) {
  69. return SDL_InvalidParamError("cond");
  70. }
  71. cond->cpp_cond.notify_all();
  72. return 0;
  73. }
  74. /* Wait on the condition variable for at most 'timeoutNS' nanoseconds.
  75. The mutex must be locked before entering this function!
  76. The mutex is unlocked during the wait, and locked again after the wait.
  77. Typical use:
  78. Thread A:
  79. SDL_LockMutex(lock);
  80. while ( ! condition ) {
  81. SDL_WaitCondition(cond, lock);
  82. }
  83. SDL_UnlockMutex(lock);
  84. Thread B:
  85. SDL_LockMutex(lock);
  86. ...
  87. condition = true;
  88. ...
  89. SDL_SignalCondition(cond);
  90. SDL_UnlockMutex(lock);
  91. */
  92. extern "C" int
  93. SDL_WaitConditionTimeoutNS(SDL_Condition *cond, SDL_Mutex *mutex, Sint64 timeoutNS)
  94. {
  95. if (cond == NULL) {
  96. return SDL_InvalidParamError("cond");
  97. }
  98. if (mutex == NULL) {
  99. return SDL_InvalidParamError("mutex");
  100. }
  101. try {
  102. std::unique_lock<std::recursive_mutex> cpp_lock(mutex->cpp_mutex, std::adopt_lock_t());
  103. if (timeoutNS < 0) {
  104. cond->cpp_cond.wait(
  105. cpp_lock);
  106. cpp_lock.release();
  107. return 0;
  108. } else {
  109. auto wait_result = cond->cpp_cond.wait_for(
  110. cpp_lock,
  111. std::chrono::duration<Sint64, std::nano>(timeoutNS));
  112. cpp_lock.release();
  113. if (wait_result == std::cv_status::timeout) {
  114. return SDL_MUTEX_TIMEDOUT;
  115. } else {
  116. return 0;
  117. }
  118. }
  119. } catch (std::system_error &ex) {
  120. return SDL_SetError("Unable to wait on a C++ condition variable: code=%d; %s", ex.code(), ex.what());
  121. }
  122. }