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"
29 #include "SDL_thread.h"
30 #include "SDL_sysmutex_c.h"
37 /* Create a condition variable */
38 SDL_cond * SDL_CreateCond(void)
42 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
44 if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
45 SDL_SetError("pthread_cond_init() failed");
53 /* Destroy a condition variable */
54 void SDL_DestroyCond(SDL_cond *cond)
57 pthread_cond_destroy(&cond->cond);
62 /* Restart one of the threads that are waiting on the condition variable */
63 int SDL_CondSignal(SDL_cond *cond)
68 SDL_SetError("Passed a NULL condition variable");
73 if ( pthread_cond_signal(&cond->cond) != 0 ) {
74 SDL_SetError("pthread_cond_signal() failed");
80 /* Restart all threads that are waiting on the condition variable */
81 int SDL_CondBroadcast(SDL_cond *cond)
86 SDL_SetError("Passed a NULL condition variable");
91 if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
92 SDL_SetError("pthread_cond_broadcast() failed");
98 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
101 struct timeval delta;
102 struct timespec abstime;
105 SDL_SetError("Passed a NULL condition variable");
109 gettimeofday(&delta, NULL);
111 abstime.tv_sec = delta.tv_sec + (ms/1000);
112 abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
113 if ( abstime.tv_nsec > 1000000000 ) {
115 abstime.tv_nsec -= 1000000000;
119 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
125 retval = SDL_MUTEX_TIMEDOUT;
130 SDL_SetError("pthread_cond_timedwait() failed");
137 /* Wait on the condition variable, unlocking the provided mutex.
138 The mutex must be locked before entering this function!
140 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
145 SDL_SetError("Passed a NULL condition variable");
150 if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
151 SDL_SetError("pthread_cond_wait() failed");