1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 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
21*/
22#include "SDL_config.h"
23
24/* RISC OS implementations uses pthreads based on linux code */
25
26#if SDL_THREADS_DISABLED
27#include "../generic/SDL_syscond.c"
28#else
29#include <sys/time.h>
30#include <unistd.h>
31#include <pthread.h>
32
33#include "SDL_thread.h"
34#include "SDL_sysmutex_c.h"
35
36struct SDL_cond
37{
38	pthread_cond_t cond;
39};
40
41/* Create a condition variable */
42SDL_cond * SDL_CreateCond(void)
43{
44	SDL_cond *cond;
45
46	cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
47	if ( cond ) {
48		if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
49			SDL_SetError("pthread_cond_init() failed");
50			SDL_free(cond);
51			cond = NULL;
52		}
53	}
54	return(cond);
55}
56
57/* Destroy a condition variable */
58void SDL_DestroyCond(SDL_cond *cond)
59{
60	if ( cond ) {
61		pthread_cond_destroy(&cond->cond);
62		SDL_free(cond);
63	}
64}
65
66/* Restart one of the threads that are waiting on the condition variable */
67int SDL_CondSignal(SDL_cond *cond)
68{
69	int retval;
70
71	if ( ! cond ) {
72		SDL_SetError("Passed a NULL condition variable");
73		return -1;
74	}
75
76	retval = 0;
77	if ( pthread_cond_signal(&cond->cond) != 0 ) {
78		SDL_SetError("pthread_cond_signal() failed");
79		retval = -1;
80	}
81	return retval;
82}
83
84/* Restart all threads that are waiting on the condition variable */
85int SDL_CondBroadcast(SDL_cond *cond)
86{
87	int retval;
88
89	if ( ! cond ) {
90		SDL_SetError("Passed a NULL condition variable");
91		return -1;
92	}
93
94	retval = 0;
95	if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
96		SDL_SetError("pthread_cond_broadcast() failed");
97		retval = -1;
98	}
99	return retval;
100}
101
102int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
103{
104	int retval;
105	struct timeval delta;
106	struct timespec abstime;
107
108	if ( ! cond ) {
109		SDL_SetError("Passed a NULL condition variable");
110		return -1;
111	}
112
113	gettimeofday(&delta, NULL);
114
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 ) {
118          abstime.tv_sec += 1;
119          abstime.tv_nsec -= 1000000000;
120        }
121
122  tryagain:
123	retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
124	switch (retval) {
125	    case EINTR:
126		goto tryagain;
127		break;
128	    case ETIMEDOUT:
129		retval = SDL_MUTEX_TIMEDOUT;
130		break;
131	    case 0:
132		break;
133	    default:
134		SDL_SetError("pthread_cond_timedwait() failed");
135		retval = -1;
136		break;
137	}
138	return retval;
139}
140
141/* Wait on the condition variable, unlocking the provided mutex.
142   The mutex must be locked before entering this function!
143 */
144int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
145{
146	int retval;
147
148	if ( ! cond ) {
149		SDL_SetError("Passed a NULL condition variable");
150		return -1;
151	}
152
153	retval = 0;
154	if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
155		SDL_SetError("pthread_cond_wait() failed");
156		retval = -1;
157	}
158	return retval;
159}
160#endif
161