Mercurial > sdl-ios-xcode
annotate src/thread/linux/SDL_syscond.c @ 1314:2b3ebc327017
Fixed dynamic X11 for now, I think.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 01 Feb 2006 08:03:04 +0000 |
parents | c9b51268668f |
children | 3692456e7b0f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
244
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef linux | |
24 /* Look to see if glibc is available, and if so, what version */ | |
25 #include <features.h> | |
26 | |
27 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) | |
28 #warning Working around a bug in glibc 2.0 pthreads | |
29 #undef SDL_USE_PTHREADS | |
30 /* The bug is actually a problem where threads are suspended, but don't | |
31 wake up when the thread manager sends them a signal. This is a problem | |
32 with thread creation too, but it happens less often. :-/ | |
33 We avoid this by using System V IPC for mutexes. | |
34 */ | |
35 #endif /* glibc 2.0 */ | |
36 #endif /* linux */ | |
37 | |
38 #ifdef SDL_USE_PTHREADS | |
39 | |
40 #include <sys/time.h> | |
41 #include <unistd.h> | |
42 #include <errno.h> | |
43 #include <stdlib.h> | |
44 #include <pthread.h> | |
45 | |
46 #include "SDL_error.h" | |
47 #include "SDL_thread.h" | |
48 #include "SDL_sysmutex_c.h" | |
49 | |
50 | |
51 #if defined(PTHREAD_NO_RECURSIVE_MUTEX) && !defined(__bsdi__) | |
52 #error You need to use the generic condition variable implementation | |
53 #endif | |
54 | |
55 struct SDL_cond | |
56 { | |
57 pthread_cond_t cond; | |
58 }; | |
59 | |
60 /* Create a condition variable */ | |
61 SDL_cond * SDL_CreateCond(void) | |
62 { | |
63 SDL_cond *cond; | |
64 | |
65 cond = (SDL_cond *) malloc(sizeof(SDL_cond)); | |
66 if ( cond ) { | |
67 if ( pthread_cond_init(&cond->cond, NULL) < 0 ) { | |
68 SDL_SetError("pthread_cond_init() failed"); | |
69 free(cond); | |
70 cond = NULL; | |
71 } | |
72 } | |
73 return(cond); | |
74 } | |
75 | |
76 /* Destroy a condition variable */ | |
77 void SDL_DestroyCond(SDL_cond *cond) | |
78 { | |
79 if ( cond ) { | |
80 pthread_cond_destroy(&cond->cond); | |
81 free(cond); | |
82 } | |
83 } | |
84 | |
85 /* Restart one of the threads that are waiting on the condition variable */ | |
86 int SDL_CondSignal(SDL_cond *cond) | |
87 { | |
88 int retval; | |
89 | |
90 if ( ! cond ) { | |
91 SDL_SetError("Passed a NULL condition variable"); | |
92 return -1; | |
93 } | |
94 | |
95 retval = 0; | |
96 if ( pthread_cond_signal(&cond->cond) != 0 ) { | |
97 SDL_SetError("pthread_cond_signal() failed"); | |
98 retval = -1; | |
99 } | |
100 return retval; | |
101 } | |
102 | |
103 /* Restart all threads that are waiting on the condition variable */ | |
104 int SDL_CondBroadcast(SDL_cond *cond) | |
105 { | |
106 int retval; | |
107 | |
108 if ( ! cond ) { | |
109 SDL_SetError("Passed a NULL condition variable"); | |
110 return -1; | |
111 } | |
112 | |
113 retval = 0; | |
114 if ( pthread_cond_broadcast(&cond->cond) != 0 ) { | |
115 SDL_SetError("pthread_cond_broadcast() failed"); | |
116 retval = -1; | |
117 } | |
118 return retval; | |
119 } | |
120 | |
121 int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms) | |
122 { | |
123 int retval; | |
124 struct timeval delta; | |
125 struct timespec abstime; | |
126 | |
127 if ( ! cond ) { | |
128 SDL_SetError("Passed a NULL condition variable"); | |
129 return -1; | |
130 } | |
131 | |
132 gettimeofday(&delta, NULL); | |
133 | |
134 abstime.tv_sec = delta.tv_sec + (ms/1000); | |
244
dc660aee7d7d
Fixed timeout in Linux condition variable implementation
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
135 abstime.tv_nsec = (delta.tv_usec + (ms%1000) * 1000) * 1000; |
dc660aee7d7d
Fixed timeout in Linux condition variable implementation
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
136 if ( abstime.tv_nsec > 1000000000 ) { |
0 | 137 abstime.tv_sec += 1; |
244
dc660aee7d7d
Fixed timeout in Linux condition variable implementation
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
138 abstime.tv_nsec -= 1000000000; |
0 | 139 } |
140 | |
141 tryagain: | |
142 retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime); | |
143 switch (retval) { | |
144 case EINTR: | |
145 goto tryagain; | |
146 break; | |
147 case ETIMEDOUT: | |
148 retval = SDL_MUTEX_TIMEDOUT; | |
149 break; | |
150 case 0: | |
151 break; | |
152 default: | |
153 SDL_SetError("pthread_cond_timedwait() failed"); | |
154 retval = -1; | |
155 break; | |
156 } | |
157 return retval; | |
158 } | |
159 | |
160 /* Wait on the condition variable, unlocking the provided mutex. | |
161 The mutex must be locked before entering this function! | |
162 */ | |
163 int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex) | |
164 { | |
165 int retval; | |
166 | |
167 if ( ! cond ) { | |
168 SDL_SetError("Passed a NULL condition variable"); | |
169 return -1; | |
170 } | |
171 | |
172 retval = 0; | |
173 if ( pthread_cond_wait(&cond->cond, &mutex->id) != 0 ) { | |
174 SDL_SetError("pthread_cond_wait() failed"); | |
175 retval = -1; | |
176 } | |
177 return retval; | |
178 } | |
179 | |
180 #else /* Use semaphore implementation */ | |
181 | |
182 #include "generic/SDL_syscond.c" | |
183 | |
184 #endif /* SDL_USE_PTHREADS */ |