Mercurial > sdl-ios-xcode
annotate src/thread/generic/SDL_syssem.c @ 1312:c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 06:32:25 +0000 |
parents | b8d311d90021 |
children | 450721ad5436 |
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:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* An implementation of semaphores using mutexes and condition variables */ | |
24 | |
25 #include <stdlib.h> | |
26 | |
27 #include "SDL_error.h" | |
28 #include "SDL_timer.h" | |
29 #include "SDL_thread.h" | |
30 #include "SDL_systhread_c.h" | |
31 | |
32 | |
33 #ifdef DISABLE_THREADS | |
34 | |
35 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
36 { | |
37 SDL_SetError("SDL not configured with thread support"); | |
38 return (SDL_sem *)0; | |
39 } | |
40 | |
41 void SDL_DestroySemaphore(SDL_sem *sem) | |
42 { | |
43 return; | |
44 } | |
45 | |
46 int SDL_SemTryWait(SDL_sem *sem) | |
47 { | |
48 SDL_SetError("SDL not configured with thread support"); | |
49 return -1; | |
50 } | |
51 | |
52 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
53 { | |
54 SDL_SetError("SDL not configured with thread support"); | |
55 return -1; | |
56 } | |
57 | |
58 int SDL_SemWait(SDL_sem *sem) | |
59 { | |
60 SDL_SetError("SDL not configured with thread support"); | |
61 return -1; | |
62 } | |
63 | |
64 Uint32 SDL_SemValue(SDL_sem *sem) | |
65 { | |
66 return 0; | |
67 } | |
68 | |
69 int SDL_SemPost(SDL_sem *sem) | |
70 { | |
71 SDL_SetError("SDL not configured with thread support"); | |
72 return -1; | |
73 } | |
74 | |
75 #else | |
76 | |
77 struct SDL_semaphore | |
78 { | |
79 Uint32 count; | |
80 Uint32 waiters_count; | |
81 SDL_mutex *count_lock; | |
82 SDL_cond *count_nonzero; | |
83 }; | |
84 | |
85 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) | |
86 { | |
87 SDL_sem *sem; | |
88 | |
89 sem = (SDL_sem *)malloc(sizeof(*sem)); | |
90 if ( ! sem ) { | |
91 SDL_OutOfMemory(); | |
92 return(0); | |
93 } | |
94 sem->count = initial_value; | |
95 sem->waiters_count = 0; | |
96 | |
97 sem->count_lock = SDL_CreateMutex(); | |
98 sem->count_nonzero = SDL_CreateCond(); | |
99 if ( ! sem->count_lock || ! sem->count_nonzero ) { | |
100 SDL_DestroySemaphore(sem); | |
101 return(0); | |
102 } | |
103 | |
104 return(sem); | |
105 } | |
106 | |
107 /* WARNING: | |
108 You cannot call this function when another thread is using the semaphore. | |
109 */ | |
110 void SDL_DestroySemaphore(SDL_sem *sem) | |
111 { | |
112 if ( sem ) { | |
113 sem->count = 0xFFFFFFFF; | |
114 while ( sem->waiters_count > 0) { | |
115 SDL_CondSignal(sem->count_nonzero); | |
116 SDL_Delay(10); | |
117 } | |
118 SDL_DestroyCond(sem->count_nonzero); | |
119 SDL_mutexP(sem->count_lock); | |
120 SDL_mutexV(sem->count_lock); | |
121 SDL_DestroyMutex(sem->count_lock); | |
122 free(sem); | |
123 } | |
124 } | |
125 | |
126 int SDL_SemTryWait(SDL_sem *sem) | |
127 { | |
128 int retval; | |
129 | |
130 if ( ! sem ) { | |
131 SDL_SetError("Passed a NULL semaphore"); | |
132 return -1; | |
133 } | |
134 | |
135 retval = SDL_MUTEX_TIMEDOUT; | |
136 SDL_LockMutex(sem->count_lock); | |
137 if ( sem->count > 0 ) { | |
138 --sem->count; | |
139 retval = 0; | |
140 } | |
141 SDL_UnlockMutex(sem->count_lock); | |
142 | |
143 return retval; | |
144 } | |
145 | |
146 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) | |
147 { | |
148 int retval; | |
149 | |
150 if ( ! sem ) { | |
151 SDL_SetError("Passed a NULL semaphore"); | |
152 return -1; | |
153 } | |
154 | |
155 /* A timeout of 0 is an easy case */ | |
156 if ( timeout == 0 ) { | |
157 return SDL_SemTryWait(sem); | |
158 } | |
159 | |
160 SDL_LockMutex(sem->count_lock); | |
161 ++sem->waiters_count; | |
162 retval = 0; | |
163 while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) { | |
164 retval = SDL_CondWaitTimeout(sem->count_nonzero, | |
165 sem->count_lock, timeout); | |
166 } | |
167 --sem->waiters_count; | |
168 --sem->count; | |
169 SDL_UnlockMutex(sem->count_lock); | |
170 | |
171 return retval; | |
172 } | |
173 | |
174 int SDL_SemWait(SDL_sem *sem) | |
175 { | |
176 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); | |
177 } | |
178 | |
179 Uint32 SDL_SemValue(SDL_sem *sem) | |
180 { | |
181 Uint32 value; | |
182 | |
183 value = 0; | |
184 if ( sem ) { | |
185 SDL_LockMutex(sem->count_lock); | |
186 value = sem->count; | |
187 SDL_UnlockMutex(sem->count_lock); | |
188 } | |
189 return value; | |
190 } | |
191 | |
192 int SDL_SemPost(SDL_sem *sem) | |
193 { | |
194 if ( ! sem ) { | |
195 SDL_SetError("Passed a NULL semaphore"); | |
196 return -1; | |
197 } | |
198 | |
199 SDL_LockMutex(sem->count_lock); | |
200 if ( sem->waiters_count > 0 ) { | |
201 SDL_CondSignal(sem->count_nonzero); | |
202 } | |
203 ++sem->count; | |
204 SDL_UnlockMutex(sem->count_lock); | |
205 | |
206 return 0; | |
207 } | |
208 | |
209 #endif /* DISABLE_THREADS */ |