Mercurial > sdl-ios-xcode
comparison src/thread/nds/SDL_syssem.c @ 2671:c3e7c0698cbb gsoc2008_nds
some changes to the dummy driver for debug purposes that should be reverted.
most importantly, commenting out a check for an env. var.
author | Darren Alton <dalton@stevens.edu> |
---|---|
date | Thu, 12 Jun 2008 02:38:49 +0000 |
parents | 6e4669f4db49 |
children |
comparison
equal
deleted
inserted
replaced
2670:6e4669f4db49 | 2671:c3e7c0698cbb |
---|---|
20 slouken@devolution.com | 20 slouken@devolution.com |
21 */ | 21 */ |
22 | 22 |
23 #ifdef SAVE_RCSID | 23 #ifdef SAVE_RCSID |
24 static char rcsid = | 24 static char rcsid = |
25 "@(#) $Id: SDL_syssem.c,v 1.2 2001/04/26 16:50:18 hercules Exp $"; | 25 "@(#) $Id: SDL_syssem.c,v 1.2 2001/04/26 16:50:18 hercules Exp $"; |
26 #endif | 26 #endif |
27 | 27 |
28 /* An implementation of semaphores using mutexes and condition variables */ | 28 /* An implementation of semaphores using mutexes and condition variables */ |
29 | 29 |
30 #include <stdlib.h> | 30 #include <stdlib.h> |
35 #include "SDL_systhread_c.h" | 35 #include "SDL_systhread_c.h" |
36 | 36 |
37 | 37 |
38 #ifdef DISABLE_THREADS | 38 #ifdef DISABLE_THREADS |
39 | 39 |
40 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | 40 SDL_sem * |
41 { | 41 SDL_CreateSemaphore(Uint32 initial_value) |
42 SDL_SetError("SDL not configured with thread support"); | 42 { |
43 return (SDL_sem *)0; | 43 SDL_SetError("SDL not configured with thread support"); |
44 } | 44 return (SDL_sem *) 0; |
45 | 45 } |
46 void SDL_DestroySemaphore(SDL_sem *sem) | 46 |
47 { | 47 void |
48 return; | 48 SDL_DestroySemaphore(SDL_sem * sem) |
49 } | 49 { |
50 | 50 return; |
51 int SDL_SemTryWait(SDL_sem *sem) | 51 } |
52 { | 52 |
53 SDL_SetError("SDL not configured with thread support"); | 53 int |
54 return -1; | 54 SDL_SemTryWait(SDL_sem * sem) |
55 } | 55 { |
56 | 56 SDL_SetError("SDL not configured with thread support"); |
57 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | 57 return -1; |
58 { | 58 } |
59 SDL_SetError("SDL not configured with thread support"); | 59 |
60 return -1; | 60 int |
61 } | 61 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout) |
62 | 62 { |
63 int SDL_SemWait(SDL_sem *sem) | 63 SDL_SetError("SDL not configured with thread support"); |
64 { | 64 return -1; |
65 SDL_SetError("SDL not configured with thread support"); | 65 } |
66 return -1; | 66 |
67 } | 67 int |
68 | 68 SDL_SemWait(SDL_sem * sem) |
69 Uint32 SDL_SemValue(SDL_sem *sem) | 69 { |
70 { | 70 SDL_SetError("SDL not configured with thread support"); |
71 return 0; | 71 return -1; |
72 } | 72 } |
73 | 73 |
74 int SDL_SemPost(SDL_sem *sem) | 74 Uint32 |
75 { | 75 SDL_SemValue(SDL_sem * sem) |
76 SDL_SetError("SDL not configured with thread support"); | 76 { |
77 return -1; | 77 return 0; |
78 } | |
79 | |
80 int | |
81 SDL_SemPost(SDL_sem * sem) | |
82 { | |
83 SDL_SetError("SDL not configured with thread support"); | |
84 return -1; | |
78 } | 85 } |
79 | 86 |
80 #else | 87 #else |
81 | 88 |
82 struct SDL_semaphore | 89 struct SDL_semaphore |
83 { | 90 { |
84 Uint32 count; | 91 Uint32 count; |
85 Uint32 waiters_count; | 92 Uint32 waiters_count; |
86 SDL_mutex *count_lock; | 93 SDL_mutex *count_lock; |
87 SDL_cond *count_nonzero; | 94 SDL_cond *count_nonzero; |
88 }; | 95 }; |
89 | 96 |
90 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | 97 SDL_sem * |
91 { | 98 SDL_CreateSemaphore(Uint32 initial_value) |
92 SDL_sem *sem; | 99 { |
93 | 100 SDL_sem *sem; |
94 sem = (SDL_sem *)malloc(sizeof(*sem)); | 101 |
95 if ( ! sem ) { | 102 sem = (SDL_sem *) malloc(sizeof(*sem)); |
96 SDL_OutOfMemory(); | 103 if (!sem) { |
97 return(0); | 104 SDL_OutOfMemory(); |
98 } | 105 return (0); |
99 sem->count = initial_value; | 106 } |
100 sem->waiters_count = 0; | 107 sem->count = initial_value; |
101 | 108 sem->waiters_count = 0; |
102 sem->count_lock = SDL_CreateMutex(); | 109 |
103 sem->count_nonzero = SDL_CreateCond(); | 110 sem->count_lock = SDL_CreateMutex(); |
104 if ( ! sem->count_lock || ! sem->count_nonzero ) { | 111 sem->count_nonzero = SDL_CreateCond(); |
105 SDL_DestroySemaphore(sem); | 112 if (!sem->count_lock || !sem->count_nonzero) { |
106 return(0); | 113 SDL_DestroySemaphore(sem); |
107 } | 114 return (0); |
108 | 115 } |
109 return(sem); | 116 |
117 return (sem); | |
110 } | 118 } |
111 | 119 |
112 /* WARNING: | 120 /* WARNING: |
113 You cannot call this function when another thread is using the semaphore. | 121 You cannot call this function when another thread is using the semaphore. |
114 */ | 122 */ |
115 void SDL_DestroySemaphore(SDL_sem *sem) | 123 void |
116 { | 124 SDL_DestroySemaphore(SDL_sem * sem) |
117 if ( sem ) { | 125 { |
118 sem->count = 0xFFFFFFFF; | 126 if (sem) { |
119 while ( sem->waiters_count > 0) { | 127 sem->count = 0xFFFFFFFF; |
120 SDL_CondSignal(sem->count_nonzero); | 128 while (sem->waiters_count > 0) { |
121 SDL_Delay(10); | 129 SDL_CondSignal(sem->count_nonzero); |
122 } | 130 SDL_Delay(10); |
123 SDL_DestroyCond(sem->count_nonzero); | 131 } |
124 SDL_mutexP(sem->count_lock); | 132 SDL_DestroyCond(sem->count_nonzero); |
125 SDL_mutexV(sem->count_lock); | 133 SDL_mutexP(sem->count_lock); |
126 SDL_DestroyMutex(sem->count_lock); | 134 SDL_mutexV(sem->count_lock); |
127 free(sem); | 135 SDL_DestroyMutex(sem->count_lock); |
128 } | 136 free(sem); |
129 } | 137 } |
130 | 138 } |
131 int SDL_SemTryWait(SDL_sem *sem) | 139 |
132 { | 140 int |
133 int retval; | 141 SDL_SemTryWait(SDL_sem * sem) |
134 | 142 { |
135 if ( ! sem ) { | 143 int retval; |
136 SDL_SetError("Passed a NULL semaphore"); | 144 |
137 return -1; | 145 if (!sem) { |
138 } | 146 SDL_SetError("Passed a NULL semaphore"); |
139 | 147 return -1; |
140 retval = SDL_MUTEX_TIMEDOUT; | 148 } |
141 SDL_LockMutex(sem->count_lock); | 149 |
142 if ( sem->count > 0 ) { | 150 retval = SDL_MUTEX_TIMEDOUT; |
143 --sem->count; | 151 SDL_LockMutex(sem->count_lock); |
144 retval = 0; | 152 if (sem->count > 0) { |
145 } | 153 --sem->count; |
146 SDL_UnlockMutex(sem->count_lock); | 154 retval = 0; |
147 | 155 } |
148 return retval; | 156 SDL_UnlockMutex(sem->count_lock); |
149 } | 157 |
150 | 158 return retval; |
151 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | 159 } |
152 { | 160 |
153 int retval; | 161 int |
154 | 162 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout) |
155 if ( ! sem ) { | 163 { |
156 SDL_SetError("Passed a NULL semaphore"); | 164 int retval; |
157 return -1; | 165 |
158 } | 166 if (!sem) { |
159 | 167 SDL_SetError("Passed a NULL semaphore"); |
160 /* A timeout of 0 is an easy case */ | 168 return -1; |
161 if ( timeout == 0 ) { | 169 } |
162 return SDL_SemTryWait(sem); | 170 |
163 } | 171 /* A timeout of 0 is an easy case */ |
164 | 172 if (timeout == 0) { |
165 SDL_LockMutex(sem->count_lock); | 173 return SDL_SemTryWait(sem); |
166 ++sem->waiters_count; | 174 } |
167 retval = 0; | 175 |
168 while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) { | 176 SDL_LockMutex(sem->count_lock); |
169 retval = SDL_CondWaitTimeout(sem->count_nonzero, | 177 ++sem->waiters_count; |
170 sem->count_lock, timeout); | 178 retval = 0; |
171 } | 179 while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) { |
172 --sem->waiters_count; | 180 retval = SDL_CondWaitTimeout(sem->count_nonzero, |
173 --sem->count; | 181 sem->count_lock, timeout); |
174 SDL_UnlockMutex(sem->count_lock); | 182 } |
175 | 183 --sem->waiters_count; |
176 return retval; | 184 --sem->count; |
177 } | 185 SDL_UnlockMutex(sem->count_lock); |
178 | 186 |
179 int SDL_SemWait(SDL_sem *sem) | 187 return retval; |
180 { | 188 } |
181 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | 189 |
182 } | 190 int |
183 | 191 SDL_SemWait(SDL_sem * sem) |
184 Uint32 SDL_SemValue(SDL_sem *sem) | 192 { |
185 { | 193 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); |
186 Uint32 value; | 194 } |
187 | 195 |
188 value = 0; | 196 Uint32 |
189 if ( sem ) { | 197 SDL_SemValue(SDL_sem * sem) |
190 SDL_LockMutex(sem->count_lock); | 198 { |
191 value = sem->count; | 199 Uint32 value; |
192 SDL_UnlockMutex(sem->count_lock); | 200 |
193 } | 201 value = 0; |
194 return value; | 202 if (sem) { |
195 } | 203 SDL_LockMutex(sem->count_lock); |
196 | 204 value = sem->count; |
197 int SDL_SemPost(SDL_sem *sem) | 205 SDL_UnlockMutex(sem->count_lock); |
198 { | 206 } |
199 if ( ! sem ) { | 207 return value; |
200 SDL_SetError("Passed a NULL semaphore"); | 208 } |
201 return -1; | 209 |
202 } | 210 int |
203 | 211 SDL_SemPost(SDL_sem * sem) |
204 SDL_LockMutex(sem->count_lock); | 212 { |
205 if ( sem->waiters_count > 0 ) { | 213 if (!sem) { |
206 SDL_CondSignal(sem->count_nonzero); | 214 SDL_SetError("Passed a NULL semaphore"); |
207 } | 215 return -1; |
208 ++sem->count; | 216 } |
209 SDL_UnlockMutex(sem->count_lock); | 217 |
210 | 218 SDL_LockMutex(sem->count_lock); |
211 return 0; | 219 if (sem->waiters_count > 0) { |
220 SDL_CondSignal(sem->count_nonzero); | |
221 } | |
222 ++sem->count; | |
223 SDL_UnlockMutex(sem->count_lock); | |
224 | |
225 return 0; | |
212 } | 226 } |
213 | 227 |
214 #endif /* DISABLE_THREADS */ | 228 #endif /* DISABLE_THREADS */ |