1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4
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.
8
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:
12
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.
20 */
21 #include "../../SDL_internal.h"
22
23 extern "C" {
24 #include "SDL_thread.h"
25 }
26
27 #include <chrono>
28 #include <condition_variable>
29 #include <ratio>
30 #include <system_error>
31
32 #include "SDL_sysmutex_c.h"
33
34 struct SDL_cond
35 {
36 std::condition_variable_any cpp_cond;
37 };
38
39 /* Create a condition variable */
40 extern "C"
41 SDL_cond *
SDL_CreateCond(void)42 SDL_CreateCond(void)
43 {
44 /* Allocate and initialize the condition variable */
45 try {
46 SDL_cond * cond = new SDL_cond;
47 return cond;
48 } catch (std::system_error & ex) {
49 SDL_SetError("unable to create a C++ condition variable: code=%d; %s", ex.code(), ex.what());
50 return NULL;
51 } catch (std::bad_alloc &) {
52 SDL_OutOfMemory();
53 return NULL;
54 }
55 }
56
57 /* Destroy a condition variable */
58 extern "C"
59 void
SDL_DestroyCond(SDL_cond * cond)60 SDL_DestroyCond(SDL_cond * cond)
61 {
62 if (cond) {
63 delete cond;
64 }
65 }
66
67 /* Restart one of the threads that are waiting on the condition variable */
68 extern "C"
69 int
SDL_CondSignal(SDL_cond * cond)70 SDL_CondSignal(SDL_cond * cond)
71 {
72 if (!cond) {
73 SDL_SetError("Passed a NULL condition variable");
74 return -1;
75 }
76
77 cond->cpp_cond.notify_one();
78 return 0;
79 }
80
81 /* Restart all threads that are waiting on the condition variable */
82 extern "C"
83 int
SDL_CondBroadcast(SDL_cond * cond)84 SDL_CondBroadcast(SDL_cond * cond)
85 {
86 if (!cond) {
87 SDL_SetError("Passed a NULL condition variable");
88 return -1;
89 }
90
91 cond->cpp_cond.notify_all();
92 return 0;
93 }
94
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.
98
99 Typical use:
100
101 Thread A:
102 SDL_LockMutex(lock);
103 while ( ! condition ) {
104 SDL_CondWait(cond, lock);
105 }
106 SDL_UnlockMutex(lock);
107
108 Thread B:
109 SDL_LockMutex(lock);
110 ...
111 condition = true;
112 ...
113 SDL_CondSignal(cond);
114 SDL_UnlockMutex(lock);
115 */
116 extern "C"
117 int
SDL_CondWaitTimeout(SDL_cond * cond,SDL_mutex * mutex,Uint32 ms)118 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
119 {
120 if (!cond) {
121 SDL_SetError("Passed a NULL condition variable");
122 return -1;
123 }
124
125 if (!mutex) {
126 SDL_SetError("Passed a NULL mutex variable");
127 return -1;
128 }
129
130 try {
131 std::unique_lock<std::recursive_mutex> cpp_lock(mutex->cpp_mutex, std::adopt_lock_t());
132 if (ms == SDL_MUTEX_MAXWAIT) {
133 cond->cpp_cond.wait(
134 cpp_lock
135 );
136 cpp_lock.release();
137 return 0;
138 } else {
139 auto wait_result = cond->cpp_cond.wait_for(
140 cpp_lock,
141 std::chrono::duration<Uint32, std::milli>(ms)
142 );
143 cpp_lock.release();
144 if (wait_result == std::cv_status::timeout) {
145 return SDL_MUTEX_TIMEDOUT;
146 } else {
147 return 0;
148 }
149 }
150 } catch (std::system_error & ex) {
151 SDL_SetError("unable to wait on a C++ condition variable: code=%d; %s", ex.code(), ex.what());
152 return -1;
153 }
154 }
155
156 /* Wait on the condition variable forever */
157 extern "C"
158 int
SDL_CondWait(SDL_cond * cond,SDL_mutex * mutex)159 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
160 {
161 return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
162 }
163
164 /* vi: set ts=4 sw=4 expandtab: */
165