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