1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2016 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 #if SDL_THREAD_PSP
24 
25 /* An implementation of condition variables using semaphores and mutexes */
26 /*
27    This implementation borrows heavily from the BeOS condition variable
28    implementation, written by Christopher Tate and Owen Smith.  Thanks!
29  */
30 
31 #include "SDL_thread.h"
32 
33 struct SDL_cond
34 {
35     SDL_mutex *lock;
36     int waiting;
37     int signals;
38     SDL_sem *wait_sem;
39     SDL_sem *wait_done;
40 };
41 
42 /* Create a condition variable */
43 SDL_cond *
SDL_CreateCond(void)44 SDL_CreateCond(void)
45 {
46     SDL_cond *cond;
47 
48     cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
49     if (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);
56             cond = NULL;
57         }
58     } else {
59         SDL_OutOfMemory();
60     }
61     return (cond);
62 }
63 
64 /* Destroy a condition variable */
65 void
SDL_DestroyCond(SDL_cond * cond)66 SDL_DestroyCond(SDL_cond * cond)
67 {
68     if (cond) {
69         if (cond->wait_sem) {
70             SDL_DestroySemaphore(cond->wait_sem);
71         }
72         if (cond->wait_done) {
73             SDL_DestroySemaphore(cond->wait_done);
74         }
75         if (cond->lock) {
76             SDL_DestroyMutex(cond->lock);
77         }
78         SDL_free(cond);
79     }
80 }
81 
82 /* Restart one of the threads that are waiting on the condition variable */
83 int
SDL_CondSignal(SDL_cond * cond)84 SDL_CondSignal(SDL_cond * cond)
85 {
86     if (!cond) {
87         return SDL_SetError("Passed a NULL condition variable");
88     }
89 
90     /* If there are waiting threads not already signalled, then
91        signal the condition and wait for the thread to respond.
92      */
93     SDL_LockMutex(cond->lock);
94     if (cond->waiting > cond->signals) {
95         ++cond->signals;
96         SDL_SemPost(cond->wait_sem);
97         SDL_UnlockMutex(cond->lock);
98         SDL_SemWait(cond->wait_done);
99     } else {
100         SDL_UnlockMutex(cond->lock);
101     }
102 
103     return 0;
104 }
105 
106 /* Restart all threads that are waiting on the condition variable */
107 int
SDL_CondBroadcast(SDL_cond * cond)108 SDL_CondBroadcast(SDL_cond * cond)
109 {
110     if (!cond) {
111         return SDL_SetError("Passed a NULL condition variable");
112     }
113 
114     /* If there are waiting threads not already signalled, then
115        signal the condition and wait for the thread to respond.
116      */
117     SDL_LockMutex(cond->lock);
118     if (cond->waiting > cond->signals) {
119         int i, num_waiting;
120 
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);
125         }
126         /* Now all released threads are blocked here, waiting for us.
127            Collect them all (and win fabulous prizes!) :-)
128          */
129         SDL_UnlockMutex(cond->lock);
130         for (i = 0; i < num_waiting; ++i) {
131             SDL_SemWait(cond->wait_done);
132         }
133     } else {
134         SDL_UnlockMutex(cond->lock);
135     }
136 
137     return 0;
138 }
139 
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.
143 
144 Typical use:
145 
146 Thread A:
147     SDL_LockMutex(lock);
148     while ( ! condition ) {
149         SDL_CondWait(cond, lock);
150     }
151     SDL_UnlockMutex(lock);
152 
153 Thread B:
154     SDL_LockMutex(lock);
155     ...
156     condition = true;
157     ...
158     SDL_CondSignal(cond);
159     SDL_UnlockMutex(lock);
160  */
161 int
SDL_CondWaitTimeout(SDL_cond * cond,SDL_mutex * mutex,Uint32 ms)162 SDL_CondWaitTimeout(SDL_cond * cond, SDL_mutex * mutex, Uint32 ms)
163 {
164     int retval;
165 
166     if (!cond) {
167         return SDL_SetError("Passed a NULL condition variable");
168     }
169 
170     /* Obtain the protection mutex, and increment the number of waiters.
171        This allows the signal mechanism to only perform a signal if there
172        are waiting threads.
173      */
174     SDL_LockMutex(cond->lock);
175     ++cond->waiting;
176     SDL_UnlockMutex(cond->lock);
177 
178     /* Unlock the mutex, as is required by condition variable semantics */
179     SDL_UnlockMutex(mutex);
180 
181     /* Wait for a signal */
182     if (ms == SDL_MUTEX_MAXWAIT) {
183         retval = SDL_SemWait(cond->wait_sem);
184     } else {
185         retval = SDL_SemWaitTimeout(cond->wait_sem, ms);
186     }
187 
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
193      */
194     SDL_LockMutex(cond->lock);
195     if (cond->signals > 0) {
196         /* If we timed out, we need to eat a condition signal */
197         if (retval > 0) {
198             SDL_SemWait(cond->wait_sem);
199         }
200         /* We always notify the signal thread that we are done */
201         SDL_SemPost(cond->wait_done);
202 
203         /* Signal handshake complete */
204         --cond->signals;
205     }
206     --cond->waiting;
207     SDL_UnlockMutex(cond->lock);
208 
209     /* Lock the mutex, as is required by condition variable semantics */
210     SDL_LockMutex(mutex);
211 
212     return retval;
213 }
214 
215 /* Wait on the condition variable forever */
216 int
SDL_CondWait(SDL_cond * cond,SDL_mutex * mutex)217 SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex)
218 {
219     return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT);
220 }
221 
222 #endif /* SDL_THREAD_PSP */
223 
224 /* vi: set ts=4 sw=4 expandtab: */
225