SDL_syscond.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. SDL_SetError("Passed a NULL condition variable");
  64. return -1;
  65. }
  66. cond->cpp_cond.notify_one();
  67. return 0;
  68. }
  69. /* Restart all threads that are waiting on the condition variable */
  70. extern "C"
  71. int
  72. SDL_CondBroadcast(SDL_cond * cond)
  73. {
  74. if (!cond) {
  75. SDL_SetError("Passed a NULL condition variable");
  76. return -1;
  77. }
  78. cond->cpp_cond.notify_all();
  79. return 0;
  80. }
  81. /* Wait on the condition variable for at most 'ms' milliseconds.
  82. The mutex must be locked before entering this function!
  83. The mutex is unlocked during the wait, and locked again after the wait.
  84. Typical use:
  85. Thread A:
  86. SDL_LockMutex(lock);
  87. while ( ! condition ) {
  88. SDL_CondWait(cond, lock);
  89. }
  90. SDL_UnlockMutex(lock);
  91. Thread B:
  92. SDL_LockMutex(lock);
  93. ...
  94. condition = true;
  95. ...
  96. SDL_CondSignal(cond);
  97. SDL_UnlockMutex(lock);
  98. */
  99. extern "C"
  100. int
  101. SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
  102. {
  103. if (!cond) {
  104. SDL_SetError("Passed a NULL condition variable");
  105. return -1;
  106. }
  107. if (!mutex) {
  108. SDL_SetError("Passed a NULL mutex variable");
  109. return -1;
  110. }
  111. try {
  112. std::unique_lock<std::recursive_mutex> cpp_lock(mutex->cpp_mutex, std::defer_lock_t());
  113. if (ms == SDL_MUTEX_MAXWAIT) {
  114. cond->cpp_cond.wait(
  115. cpp_lock
  116. );
  117. cpp_lock.release();
  118. return 0;
  119. } else {
  120. auto wait_result = cond->cpp_cond.wait_for(
  121. cpp_lock,
  122. std::chrono::duration<Uint32, std::milli>(ms)
  123. );
  124. cpp_lock.release();
  125. if (wait_result == std::cv_status::timeout) {
  126. return SDL_MUTEX_TIMEDOUT;
  127. } else {
  128. return 0;
  129. }
  130. }
  131. } catch (std::system_error & ex) {
  132. SDL_SetError("unable to wait on a C++ condition variable: code=%d; %s", ex.code(), ex.what());
  133. return -1;
  134. }
  135. }
  136. /* Wait on the condition variable forever */
  137. extern "C"
  138. int
  139. SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
  140. {
  141. return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
  142. }
  143. /* vi: set ts=4 sw=4 expandtab: */