11/*
2- SDL - Simple DirectMedia Layer
3- Copyright (C) 1997-2006 Sam Lantinga
4-
5- This library is free software; you can redistribute it and/or
6- modify it under the terms of the GNU Lesser General Public
7- License as published by the Free Software Foundation; either
8- version 2.1 of the License, or (at your option) any later version.
9-
10- This library is distributed in the hope that it will be useful,
11- but WITHOUT ANY WARRANTY; without even the implied warranty of
12- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13- Lesser General Public License for more details.
14-
15- You should have received a copy of the GNU Lesser General Public
16- License along with this library; if not, write to the Free Software
17- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18-
19- Sam Lantinga
20- slouken@libsdl.org
2+ Simple DirectMedia Layer
3+ Copyright (C) 1997-2023 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.
2120*/
22- #include "SDL_config .h"
21+ #include "../../SDL_internal .h"
2322
24- /* An implementation of condition variables using semaphores and mutexes */
25- /*
26- This implementation borrows heavily from the BeOS condition variable
27- implementation, written by Christopher Tate and Owen Smith. Thanks!
28- */
23+ #include <errno.h>
24+ #include <ogc/cond.h>
25+ #include <ogc/timesupp.h>
2926
3027#include "SDL_thread.h"
3128#include "SDL_sysmutex_c.h"
3229
33- #include <ogcsys.h>
34- #include <ogc/cond.h>
35-
3630struct SDL_cond
3731{
3832 cond_t cond ;
3933};
4034
4135/* Create a condition variable */
42- SDL_cond * SDL_CreateCond (void )
36+ SDL_cond * SDL_CreateCond (void )
4337{
4438 SDL_cond * cond ;
4539
46- cond = (SDL_cond * ) SDL_malloc (sizeof (SDL_cond ));
47- if (cond ) {
48- if (LWP_CondInit (& (cond -> cond )) < 0 ) {
49- SDL_DestroyCond (cond );
40+ cond = (SDL_cond * )SDL_malloc (sizeof (SDL_cond ));
41+ if (cond != NULL ) {
42+ if (LWP_CondInit (& cond -> cond ) != 0 ) {
43+ SDL_SetError ("LWP_CondInit() failed" );
44+ SDL_free (cond );
5045 cond = NULL ;
5146 }
5247 } else {
5348 SDL_OutOfMemory ();
5449 }
55- return ( cond ) ;
50+ return cond ;
5651}
5752
5853/* Destroy a condition variable */
5954void SDL_DestroyCond (SDL_cond * cond )
6055{
61- if (cond ) {
56+ if (cond != NULL ) {
6257 LWP_CondDestroy (cond -> cond );
6358 SDL_free (cond );
6459 }
@@ -67,65 +62,92 @@ void SDL_DestroyCond(SDL_cond *cond)
6762/* Restart one of the threads that are waiting on the condition variable */
6863int SDL_CondSignal (SDL_cond * cond )
6964{
70- if (!cond ) {
71- SDL_SetError ("Passed a NULL condition variable" );
72- return -1 ;
65+ if (cond == NULL ) {
66+ return SDL_InvalidParamError ("cond" );
7367 }
7468
75- return LWP_CondSignal (cond -> cond ) == 0 ? 0 : -1 ;
76-
69+ if (LWP_CondSignal (cond -> cond ) != 0 ) {
70+ return SDL_SetError ("LWP_CondSignal() failed" );
71+ }
72+ return 0 ;
7773}
7874
7975/* Restart all threads that are waiting on the condition variable */
8076int SDL_CondBroadcast (SDL_cond * cond )
8177{
82- if (!cond ) {
83- SDL_SetError ("Passed a NULL condition variable" );
84- return -1 ;
78+ if (cond == NULL ) {
79+ return SDL_InvalidParamError ("cond" );
8580 }
8681
87- return LWP_CondBroadcast (cond -> cond ) == 0 ? 0 : -1 ;
82+ if (LWP_CondBroadcast (cond -> cond ) != 0 ) {
83+ return SDL_SetError ("LWP_CondBroadcast() failed" );
84+ }
85+ return 0 ;
8886}
8987
9088/* Wait on the condition variable for at most 'ms' milliseconds.
91- The mutex must be locked before entering this function!
92- The mutex is unlocked during the wait, and locked again after the wait.
93-
94- Typical use:
95-
96- Thread A:
97- SDL_LockMutex(lock);
98- while ( ! condition ) {
99- SDL_CondWait(cond);
100- }
101- SDL_UnlockMutex(lock);
102-
103- Thread B:
104- SDL_LockMutex(lock);
105- ...
106- condition = true;
107- ...
108- SDL_UnlockMutex(lock);
109- */
89+ The mutex must be locked before entering this function!
90+ The mutex is unlocked during the wait, and locked again after the wait.
11091
92+ Typical use:
11193
94+ Thread A:
95+ SDL_LockMutex(lock);
96+ while ( ! condition ) {
97+ SDL_CondWait(cond, lock);
98+ }
99+ SDL_UnlockMutex(lock);
100+
101+ Thread B:
102+ SDL_LockMutex(lock);
103+ ...
104+ condition = true;
105+ ...
106+ SDL_CondSignal(cond);
107+ SDL_UnlockMutex(lock);
108+ */
112109int SDL_CondWaitTimeout (SDL_cond * cond , SDL_mutex * mutex , Uint32 ms )
113110{
114- struct timespec time ;
111+ int retval ;
112+ struct timespec tv ;
115113
116- if (!cond ) {
117- SDL_SetError ("Passed a NULL condition variable" );
118- return -1 ;
114+ if (cond == NULL ) {
115+ return SDL_InvalidParamError ("cond" );
116+ }
117+ if (mutex == NULL ) {
118+ return SDL_InvalidParamError ("mutex" );
119119 }
120- //LWP_CondTimedWait expects relative timeout
121- time .tv_sec = (ms / 1000 );
122- time .tv_nsec = (ms % 1000 ) * 1000000 ;
123120
124- return LWP_CondTimedWait (cond -> cond , mutex -> id , & time );
121+ tv .tv_sec = ms / TB_MSPERSEC ;
122+ tv .tv_nsec = (ms % TB_MSPERSEC ) * TB_NSPERMS ;
123+
124+ retval = LWP_CondTimedWait (cond -> cond , mutex -> id , & tv );
125+ if (retval != 0 ) {
126+ if (retval == ETIMEDOUT ) {
127+ retval = SDL_MUTEX_TIMEDOUT ;
128+ } else {
129+ retval = SDL_SetError ("LWP_CondTimedWait() failed" );
130+ }
131+ }
132+ return retval ;
125133}
126134
127- /* Wait on the condition variable forever */
135+ /* Wait on the condition variable, unlocking the provided mutex.
136+ The mutex must be locked before entering this function!
137+ */
128138int SDL_CondWait (SDL_cond * cond , SDL_mutex * mutex )
129139{
130- return LWP_CondWait (cond -> cond , mutex -> id );
140+ if (cond == NULL ) {
141+ return SDL_InvalidParamError ("cond" );
142+ }
143+ if (mutex == NULL ) {
144+ return SDL_InvalidParamError ("mutex" );
145+ }
146+
147+ if (LWP_CondWait (cond -> cond , mutex -> id ) != 0 ) {
148+ return SDL_SetError ("LWP_CondWait() failed" );
149+ }
150+ return 0 ;
131151}
152+
153+ /* vi: set ts=4 sw=4 expandtab: */
0 commit comments