SDL_syscond.cpp 3.9 KB

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