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"
24 /* RISC OS implementations uses pthreads based on linux code */
26 #if SDL_THREADS_DISABLED
27 #include "../generic/SDL_syscond.c"
33 #include "SDL_thread.h"
34 #include "SDL_sysmutex_c.h"
41 /* Create a condition variable */
42 SDL_cond * SDL_CreateCond(void)
46 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
48 if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
49 SDL_SetError("pthread_cond_init() failed");
57 /* Destroy a condition variable */
58 void SDL_DestroyCond(SDL_cond *cond)
61 pthread_cond_destroy(&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 ( pthread_cond_signal(&cond->cond) != 0 ) {
78 SDL_SetError("pthread_cond_signal() 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 ( pthread_cond_broadcast(&cond->cond) != 0 ) {
96 SDL_SetError("pthread_cond_broadcast() failed");
102 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
105 struct timeval delta;
106 struct timespec abstime;
109 SDL_SetError("Passed a NULL condition variable");
113 gettimeofday(&delta, NULL);
115 abstime.tv_sec = delta.tv_sec + (ms/1000);
116 abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
117 if ( abstime.tv_nsec > 1000000000 ) {
119 abstime.tv_nsec -= 1000000000;
123 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
129 retval = SDL_MUTEX_TIMEDOUT;
134 SDL_SetError("pthread_cond_timedwait() failed");
141 /* Wait on the condition variable, unlocking the provided mutex.
142 The mutex must be locked before entering this function!
144 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
149 SDL_SetError("Passed a NULL condition variable");
154 if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
155 SDL_SetError("pthread_cond_wait() failed");