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"
25 /* An implementation of condition variables using semaphores and mutexes */
27 This implementation borrows heavily from the BeOS condition variable
28 implementation, written by Christopher Tate and Owen Smith. Thanks!
31 #include "SDL_thread.h"
42 /* Create a condition variable */
48 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
50 cond->lock = SDL_CreateMutex();
51 cond->wait_sem = SDL_CreateSemaphore(0);
52 cond->wait_done = SDL_CreateSemaphore(0);
53 cond->waiting = cond->signals = 0;
54 if (!cond->lock || !cond->wait_sem || !cond->wait_done) {
55 SDL_DestroyCond(cond);
64 /* Destroy a condition variable */
66 SDL_DestroyCond(SDL_cond * cond)
70 SDL_DestroySemaphore(cond->wait_sem);
72 if (cond->wait_done) {
73 SDL_DestroySemaphore(cond->wait_done);
76 SDL_DestroyMutex(cond->lock);
82 /* Restart one of the threads that are waiting on the condition variable */
84 SDL_CondSignal(SDL_cond * cond)
87 return SDL_SetError("Passed a NULL condition variable");
90 /* If there are waiting threads not already signalled, then
91 signal the condition and wait for the thread to respond.
93 SDL_LockMutex(cond->lock);
94 if (cond->waiting > cond->signals) {
96 SDL_SemPost(cond->wait_sem);
97 SDL_UnlockMutex(cond->lock);
98 SDL_SemWait(cond->wait_done);
100 SDL_UnlockMutex(cond->lock);
106 /* Restart all threads that are waiting on the condition variable */
108 SDL_CondBroadcast(SDL_cond * cond)
111 return SDL_SetError("Passed a NULL condition variable");
114 /* If there are waiting threads not already signalled, then
115 signal the condition and wait for the thread to respond.
117 SDL_LockMutex(cond->lock);
118 if (cond->waiting > cond->signals) {
121 num_waiting = (cond->waiting - cond->signals);
122 cond->signals = cond->waiting;
123 for (i = 0; i < num_waiting; ++i) {
124 SDL_SemPost(cond->wait_sem);
126 /* Now all released threads are blocked here, waiting for us.
127 Collect them all (and win fabulous prizes!) :-)
129 SDL_UnlockMutex(cond->lock);
130 for (i = 0; i < num_waiting; ++i) {
131 SDL_SemWait(cond->wait_done);
134 SDL_UnlockMutex(cond->lock);
140 /* Wait on the condition variable for at most 'ms' milliseconds.
141 The mutex must be locked before entering this function!
142 The mutex is unlocked during the wait, and locked again after the wait.
148 while ( ! condition ) {
149 SDL_CondWait(cond, lock);
151 SDL_UnlockMutex(lock);
158 SDL_CondSignal(cond);
159 SDL_UnlockMutex(lock);
162 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
167 return SDL_SetError("Passed a NULL condition variable");
170 /* Obtain the protection mutex, and increment the number of waiters.
171 This allows the signal mechanism to only perform a signal if there
174 SDL_LockMutex(cond->lock);
176 SDL_UnlockMutex(cond->lock);
178 /* Unlock the mutex, as is required by condition variable semantics */
179 SDL_UnlockMutex(mutex);
181 /* Wait for a signal */
182 if (ms == SDL_MUTEX_MAXWAIT) {
183 retval = SDL_SemWait(cond->wait_sem);
185 retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
188 /* Let the signaler know we have completed the wait, otherwise
189 the signaler can race ahead and get the condition semaphore
190 if we are stopped between the mutex unlock and semaphore wait,
191 giving a deadlock. See the following URL for details:
192 http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
194 SDL_LockMutex(cond->lock);
195 if (cond->signals > 0) {
196 /* If we timed out, we need to eat a condition signal */
198 SDL_SemWait(cond->wait_sem);
200 /* We always notify the signal thread that we are done */
201 SDL_SemPost(cond->wait_done);
203 /* Signal handshake complete */
207 SDL_UnlockMutex(cond->lock);
209 /* Lock the mutex, as is required by condition variable semantics */
210 SDL_LockMutex(mutex);
215 /* Wait on the condition variable forever */
217 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
219 return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
222 #endif /* SDL_THREAD_PSP */
224 /* vi: set ts=4 sw=4 expandtab: */