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