comparison src/thread/pthread/SDL_syscond.c @ 1361:19418e4422cb

New configure-based build system. Still work in progress, but much improved
author Sam Lantinga <slouken@libsdl.org>
date Thu, 16 Feb 2006 10:11:48 +0000
parents
children d910939febfa
comparison
equal deleted inserted replaced
1360:70a9cfb4cf1b 1361:19418e4422cb
1 /*
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
21 */
22
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <pthread.h>
27
28 #include "SDL_thread.h"
29 #include "SDL_sysmutex_c.h"
30
31 struct SDL_cond
32 {
33 pthread_cond_t cond;
34 };
35
36 /* Create a condition variable */
37 SDL_cond * SDL_CreateCond(void)
38 {
39 SDL_cond *cond;
40
41 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
42 if ( cond ) {
43 if ( pthread_cond_init(&cond->cond, NULL) < 0 ) {
44 SDL_SetError("pthread_cond_init() failed");
45 SDL_free(cond);
46 cond = NULL;
47 }
48 }
49 return(cond);
50 }
51
52 /* Destroy a condition variable */
53 void SDL_DestroyCond(SDL_cond *cond)
54 {
55 if ( cond ) {
56 pthread_cond_destroy(&cond->cond);
57 SDL_free(cond);
58 }
59 }
60
61 /* Restart one of the threads that are waiting on the condition variable */
62 int SDL_CondSignal(SDL_cond *cond)
63 {
64 int retval;
65
66 if ( ! cond ) {
67 SDL_SetError("Passed a NULL condition variable");
68 return -1;
69 }
70
71 retval = 0;
72 if ( pthread_cond_signal(&cond->cond) != 0 ) {
73 SDL_SetError("pthread_cond_signal() failed");
74 retval = -1;
75 }
76 return retval;
77 }
78
79 /* Restart all threads that are waiting on the condition variable */
80 int SDL_CondBroadcast(SDL_cond *cond)
81 {
82 int retval;
83
84 if ( ! cond ) {
85 SDL_SetError("Passed a NULL condition variable");
86 return -1;
87 }
88
89 retval = 0;
90 if ( pthread_cond_broadcast(&cond->cond) != 0 ) {
91 SDL_SetError("pthread_cond_broadcast() failed");
92 retval = -1;
93 }
94 return retval;
95 }
96
97 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
98 {
99 int retval;
100 struct timeval delta;
101 struct timespec abstime;
102
103 if ( ! cond ) {
104 SDL_SetError("Passed a NULL condition variable");
105 return -1;
106 }
107
108 gettimeofday(&delta, NULL);
109
110 abstime.tv_sec = delta.tv_sec + (ms/1000);
111 abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000;
112 if ( abstime.tv_nsec > 1000000000 ) {
113 abstime.tv_sec += 1;
114 abstime.tv_nsec -= 1000000000;
115 }
116
117 tryagain:
118 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
119 switch (retval) {
120 case EINTR:
121 goto tryagain;
122 break;
123 case ETIMEDOUT:
124 retval = SDL_MUTEX_TIMEDOUT;
125 break;
126 case 0:
127 break;
128 default:
129 SDL_SetError("pthread_cond_timedwait() failed");
130 retval = -1;
131 break;
132 }
133 return retval;
134 }
135
136 /* Wait on the condition variable, unlocking the provided mutex.
137 The mutex must be locked before entering this function!
138 */
139 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
140 {
141 int retval;
142
143 if ( ! cond ) {
144 SDL_SetError("Passed a NULL condition variable");
145 return -1;
146 }
147
148 retval = 0;
149 if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) {
150 SDL_SetError("pthread_cond_wait() failed");
151 retval = -1;
152 }
153 return retval;
154 }