Mercurial > sdl-ios-xcode
annotate src/thread/os2/SDL_syscond.c @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | c9b51268668f |
children | 604d73db6802 |
rev | line source |
---|---|
1190 | 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:
1190
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
1190 | 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:
1190
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
1190 | 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:
1190
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
1190 | 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:
1190
diff
changeset
|
13 Lesser General Public License for more details. |
1190 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
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:
1190
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:
1190
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
1190 | 18 |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 | |
23 /* An implementation of condition variables using semaphores and mutexes */ | |
24 /* | |
25 This implementation borrows heavily from the BeOS condition variable | |
26 implementation, written by Christopher Tate and Owen Smith. Thanks! | |
27 */ | |
28 | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
31 | |
32 #include "SDL_error.h" | |
33 #include "SDL_thread.h" | |
34 | |
35 struct SDL_cond | |
36 { | |
37 SDL_mutex *lock; | |
38 int waiting; | |
39 int signals; | |
40 SDL_sem *wait_sem; | |
41 SDL_sem *wait_done; | |
42 }; | |
43 | |
44 /* Create a condition variable */ | |
45 DECLSPEC SDL_cond * SDLCALL SDL_CreateCond(void) | |
46 { | |
47 SDL_cond *cond; | |
48 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
49 cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond)); |
1190 | 50 if ( cond ) { |
51 cond->lock = SDL_CreateMutex(); | |
52 cond->wait_sem = SDL_CreateSemaphore(0); | |
53 cond->wait_done = SDL_CreateSemaphore(0); | |
54 cond->waiting = cond->signals = 0; | |
55 if ( ! cond->lock || ! cond->wait_sem || ! cond->wait_done ) { | |
56 SDL_DestroyCond(cond); | |
57 cond = NULL; | |
58 } | |
59 } else { | |
60 SDL_OutOfMemory(); | |
61 } | |
62 return(cond); | |
63 } | |
64 | |
65 /* Destroy a condition variable */ | |
66 DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond) | |
67 { | |
68 if ( cond ) { | |
69 if ( cond->wait_sem ) { | |
70 SDL_DestroySemaphore(cond->wait_sem); | |
71 } | |
72 if ( cond->wait_done ) { | |
73 SDL_DestroySemaphore(cond->wait_done); | |
74 } | |
75 if ( cond->lock ) { | |
76 SDL_DestroyMutex(cond->lock); | |
77 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
78 SDL_free(cond); |
1190 | 79 } |
80 } | |
81 | |
82 /* Restart one of the threads that are waiting on the condition variable */ | |
83 DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond) | |
84 { | |
85 if ( ! cond ) { | |
86 SDL_SetError("Passed a NULL condition variable"); | |
87 return -1; | |
88 } | |
89 | |
90 /* If there are waiting threads not already signalled, then | |
91 signal the condition and wait for the thread to respond. | |
92 */ | |
93 SDL_LockMutex(cond->lock); | |
94 if ( cond->waiting > cond->signals ) { | |
95 ++cond->signals; | |
96 SDL_SemPost(cond->wait_sem); | |
97 SDL_UnlockMutex(cond->lock); | |
98 SDL_SemWait(cond->wait_done); | |
99 } else { | |
100 SDL_UnlockMutex(cond->lock); | |
101 } | |
102 | |
103 return 0; | |
104 } | |
105 | |
106 /* Restart all threads that are waiting on the condition variable */ | |
107 DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond *cond) | |
108 { | |
109 if ( ! cond ) { | |
110 SDL_SetError("Passed a NULL condition variable"); | |
111 return -1; | |
112 } | |
113 | |
114 /* If there are waiting threads not already signalled, then | |
115 signal the condition and wait for the thread to respond. | |
116 */ | |
117 SDL_LockMutex(cond->lock); | |
118 if ( cond->waiting > cond->signals ) { | |
119 int i, num_waiting; | |
120 | |
121 num_waiting = (cond->waiting - cond->signals); | |
122 cond->signals = cond->waiting; | |
123 for ( i=0; i<num_waiting; ++i ) { | |
124 SDL_SemPost(cond->wait_sem); | |
125 } | |
126 /* Now all released threads are blocked here, waiting for us. | |
127 Collect them all (and win fabulous prizes!) :-) | |
128 */ | |
129 SDL_UnlockMutex(cond->lock); | |
130 for ( i=0; i<num_waiting; ++i ) { | |
131 SDL_SemWait(cond->wait_done); | |
132 } | |
133 } else { | |
134 SDL_UnlockMutex(cond->lock); | |
135 } | |
136 | |
137 return 0; | |
138 } | |
139 | |
140 /* Wait on the condition variable for at most 'ms' milliseconds. | |
141 The mutex must be locked before entering this function! | |
142 The mutex is unlocked during the wait, and locked again after the wait. | |
143 | |
144 Typical use: | |
145 | |
146 Thread A: | |
147 SDL_LockMutex(lock); | |
148 while ( ! condition ) { | |
149 SDL_CondWait(cond); | |
150 } | |
151 SDL_UnlockMutex(lock); | |
152 | |
153 Thread B: | |
154 SDL_LockMutex(lock); | |
155 ... | |
156 condition = true; | |
157 ... | |
158 SDL_UnlockMutex(lock); | |
159 */ | |
160 DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms) | |
161 { | |
162 int retval; | |
163 | |
164 if ( ! cond ) { | |
165 SDL_SetError("Passed a NULL condition variable"); | |
166 return -1; | |
167 } | |
168 | |
169 /* Obtain the protection mutex, and increment the number of waiters. | |
170 This allows the signal mechanism to only perform a signal if there | |
171 are waiting threads. | |
172 */ | |
173 SDL_LockMutex(cond->lock); | |
174 ++cond->waiting; | |
175 SDL_UnlockMutex(cond->lock); | |
176 | |
177 /* Unlock the mutex, as is required by condition variable semantics */ | |
178 SDL_UnlockMutex(mutex); | |
179 | |
180 /* Wait for a signal */ | |
181 if ( ms == SDL_MUTEX_MAXWAIT ) { | |
182 retval = SDL_SemWait(cond->wait_sem); | |
183 } else { | |
184 retval = SDL_SemWaitTimeout(cond->wait_sem, ms); | |
185 } | |
186 | |
187 /* Let the signaler know we have completed the wait, otherwise | |
188 the signaler can race ahead and get the condition semaphore | |
189 if we are stopped between the mutex unlock and semaphore wait, | |
190 giving a deadlock. See the following URL for details: | |
191 http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html | |
192 */ | |
193 SDL_LockMutex(cond->lock); | |
194 if ( cond->signals > 0 ) { | |
195 /* If we timed out, we need to eat a condition signal */ | |
196 if ( retval > 0 ) { | |
197 SDL_SemWait(cond->wait_sem); | |
198 } | |
199 /* We always notify the signal thread that we are done */ | |
200 SDL_SemPost(cond->wait_done); | |
201 | |
202 /* Signal handshake complete */ | |
203 --cond->signals; | |
204 } | |
205 --cond->waiting; | |
206 SDL_UnlockMutex(cond->lock); | |
207 | |
208 /* Lock the mutex, as is required by condition variable semantics */ | |
209 SDL_LockMutex(mutex); | |
210 | |
211 return retval; | |
212 } | |
213 | |
214 /* Wait on the condition variable forever */ | |
215 DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex) | |
216 { | |
217 return SDL_CondWaitTimeout(cond, mutex, SDL_MUTEX_MAXWAIT); | |
218 } |