2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
24 #include "SDL_thread.h"
28 #include <condition_variable>
30 #include <system_error>
32 #include "SDL_sysmutex_c.h"
36 std::condition_variable_any cpp_cond;
39 /* Create a condition variable */
44 /* Allocate and initialize the condition variable */
46 SDL_cond * cond = new SDL_cond;
48 } catch (std::system_error & ex) {
49 SDL_SetError("unable to create a C++ condition variable: code=%d; %s", ex.code(), ex.what());
51 } catch (std::bad_alloc &) {
57 /* Destroy a condition variable */
60 SDL_DestroyCond(SDL_cond * cond)
67 /* Restart one of the threads that are waiting on the condition variable */
70 SDL_CondSignal(SDL_cond * cond)
73 SDL_SetError("Passed a NULL condition variable");
77 cond->cpp_cond.notify_one();
81 /* Restart all threads that are waiting on the condition variable */
84 SDL_CondBroadcast(SDL_cond * cond)
87 SDL_SetError("Passed a NULL condition variable");
91 cond->cpp_cond.notify_all();
95 /* Wait on the condition variable for at most 'ms' milliseconds.
96 The mutex must be locked before entering this function!
97 The mutex is unlocked during the wait, and locked again after the wait.
103 while ( ! condition ) {
104 SDL_CondWait(cond, lock);
106 SDL_UnlockMutex(lock);
113 SDL_CondSignal(cond);
114 SDL_UnlockMutex(lock);
118 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
121 SDL_SetError("Passed a NULL condition variable");
126 SDL_SetError("Passed a NULL mutex variable");
131 std::unique_lock<std::recursive_mutex> cpp_lock(mutex->cpp_mutex, std::adopt_lock_t());
132 if (ms == SDL_MUTEX_MAXWAIT) {
139 auto wait_result = cond->cpp_cond.wait_for(
141 std::chrono::duration<Uint32, std::milli>(ms)
144 if (wait_result == std::cv_status::timeout) {
145 return SDL_MUTEX_TIMEDOUT;
150 } catch (std::system_error & ex) {
151 SDL_SetError("unable to wait on a C++ condition variable: code=%d; %s", ex.code(), ex.what());
156 /* Wait on the condition variable forever */
159 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
161 return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
164 /* vi: set ts=4 sw=4 expandtab: */