2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
25 * GNU pth conditions variables
32 #include "SDL_thread.h"
33 #include "SDL_sysmutex_c.h"
40 /* Create a condition variable */
41 SDL_cond * SDL_CreateCond(void)
45 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
47 if ( pth_cond_init(&(cond->condpth_p)) < 0 ) {
48 SDL_SetError("pthread_cond_init() failed");
58 /* Destroy a condition variable */
59 void SDL_DestroyCond(SDL_cond *cond)
66 /* Restart one of the threads that are waiting on the condition variable */
67 int SDL_CondSignal(SDL_cond *cond)
72 SDL_SetError("Passed a NULL condition variable");
77 if ( pth_cond_notify(&(cond->condpth_p), FALSE) != 0 ) {
78 SDL_SetError("pth_cond_notify() failed");
84 /* Restart all threads that are waiting on the condition variable */
85 int SDL_CondBroadcast(SDL_cond *cond)
90 SDL_SetError("Passed a NULL condition variable");
95 if ( pth_cond_notify(&(cond->condpth_p), TRUE) != 0 ) {
96 SDL_SetError("pth_cond_notify() failed");
102 /* Wait on the condition variable for at most 'ms' milliseconds.
103 The mutex must be locked before entering this function!
104 The mutex is unlocked during the wait, and locked again after the wait.
110 while ( ! condition ) {
113 SDL_UnlockMutex(lock);
120 SDL_UnlockMutex(lock);
122 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
129 SDL_SetError("Passed a NULL condition variable");
136 ev = pth_event(PTH_EVENT_TIME, pth_timeout(sec,(ms-sec*1000)*1000));
138 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), ev) != 0 ) {
139 SDL_SetError("pth_cond_await() failed");
143 pth_event_free(ev, PTH_FREE_ALL);
148 /* Wait on the condition variable forever */
149 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
154 SDL_SetError("Passed a NULL condition variable");
159 if ( pth_cond_await(&(cond->condpth_p), &(mutex->mutexpth_p), NULL) != 0 ) {
160 SDL_SetError("pth_cond_await() failed");