comparison src/thread/generic/SDL_syssem.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents aee7ea396f59
children 99210400e8b9
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
28 #include "SDL_systhread_c.h" 28 #include "SDL_systhread_c.h"
29 29
30 30
31 #if SDL_THREADS_DISABLED 31 #if SDL_THREADS_DISABLED
32 32
33 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 33 SDL_sem *
34 { 34 SDL_CreateSemaphore(Uint32 initial_value)
35 SDL_SetError("SDL not configured with thread support"); 35 {
36 return (SDL_sem *)0; 36 SDL_SetError("SDL not configured with thread support");
37 } 37 return (SDL_sem *) 0;
38 38 }
39 void SDL_DestroySemaphore(SDL_sem *sem) 39
40 { 40 void
41 return; 41 SDL_DestroySemaphore(SDL_sem * sem)
42 } 42 {
43 43 return;
44 int SDL_SemTryWait(SDL_sem *sem) 44 }
45 { 45
46 SDL_SetError("SDL not configured with thread support"); 46 int
47 return -1; 47 SDL_SemTryWait(SDL_sem * sem)
48 } 48 {
49 49 SDL_SetError("SDL not configured with thread support");
50 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 50 return -1;
51 { 51 }
52 SDL_SetError("SDL not configured with thread support"); 52
53 return -1; 53 int
54 } 54 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
55 55 {
56 int SDL_SemWait(SDL_sem *sem) 56 SDL_SetError("SDL not configured with thread support");
57 { 57 return -1;
58 SDL_SetError("SDL not configured with thread support"); 58 }
59 return -1; 59
60 } 60 int
61 61 SDL_SemWait(SDL_sem * sem)
62 Uint32 SDL_SemValue(SDL_sem *sem) 62 {
63 { 63 SDL_SetError("SDL not configured with thread support");
64 return 0; 64 return -1;
65 } 65 }
66 66
67 int SDL_SemPost(SDL_sem *sem) 67 Uint32
68 { 68 SDL_SemValue(SDL_sem * sem)
69 SDL_SetError("SDL not configured with thread support"); 69 {
70 return -1; 70 return 0;
71 }
72
73 int
74 SDL_SemPost(SDL_sem * sem)
75 {
76 SDL_SetError("SDL not configured with thread support");
77 return -1;
71 } 78 }
72 79
73 #else 80 #else
74 81
75 struct SDL_semaphore 82 struct SDL_semaphore
76 { 83 {
77 Uint32 count; 84 Uint32 count;
78 Uint32 waiters_count; 85 Uint32 waiters_count;
79 SDL_mutex *count_lock; 86 SDL_mutex *count_lock;
80 SDL_cond *count_nonzero; 87 SDL_cond *count_nonzero;
81 }; 88 };
82 89
83 SDL_sem *SDL_CreateSemaphore(Uint32 initial_value) 90 SDL_sem *
84 { 91 SDL_CreateSemaphore(Uint32 initial_value)
85 SDL_sem *sem; 92 {
86 93 SDL_sem *sem;
87 sem = (SDL_sem *)SDL_malloc(sizeof(*sem)); 94
88 if ( ! sem ) { 95 sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
89 SDL_OutOfMemory(); 96 if (!sem) {
90 return NULL; 97 SDL_OutOfMemory();
91 } 98 return NULL;
92 sem->count = initial_value; 99 }
93 sem->waiters_count = 0; 100 sem->count = initial_value;
94 101 sem->waiters_count = 0;
95 sem->count_lock = SDL_CreateMutex(); 102
96 sem->count_nonzero = SDL_CreateCond(); 103 sem->count_lock = SDL_CreateMutex();
97 if ( ! sem->count_lock || ! sem->count_nonzero ) { 104 sem->count_nonzero = SDL_CreateCond();
98 SDL_DestroySemaphore(sem); 105 if (!sem->count_lock || !sem->count_nonzero) {
99 return NULL; 106 SDL_DestroySemaphore(sem);
100 } 107 return NULL;
101 108 }
102 return sem; 109
110 return sem;
103 } 111 }
104 112
105 /* WARNING: 113 /* WARNING:
106 You cannot call this function when another thread is using the semaphore. 114 You cannot call this function when another thread is using the semaphore.
107 */ 115 */
108 void SDL_DestroySemaphore(SDL_sem *sem) 116 void
109 { 117 SDL_DestroySemaphore(SDL_sem * sem)
110 if ( sem ) { 118 {
111 sem->count = 0xFFFFFFFF; 119 if (sem) {
112 while ( sem->waiters_count > 0) { 120 sem->count = 0xFFFFFFFF;
113 SDL_CondSignal(sem->count_nonzero); 121 while (sem->waiters_count > 0) {
114 SDL_Delay(10); 122 SDL_CondSignal(sem->count_nonzero);
115 } 123 SDL_Delay(10);
116 SDL_DestroyCond(sem->count_nonzero); 124 }
117 if ( sem->count_lock ) { 125 SDL_DestroyCond(sem->count_nonzero);
118 SDL_mutexP(sem->count_lock); 126 if (sem->count_lock) {
119 SDL_mutexV(sem->count_lock); 127 SDL_mutexP(sem->count_lock);
120 SDL_DestroyMutex(sem->count_lock); 128 SDL_mutexV(sem->count_lock);
121 } 129 SDL_DestroyMutex(sem->count_lock);
122 SDL_free(sem); 130 }
123 } 131 SDL_free(sem);
124 } 132 }
125 133 }
126 int SDL_SemTryWait(SDL_sem *sem) 134
127 { 135 int
128 int retval; 136 SDL_SemTryWait(SDL_sem * sem)
129 137 {
130 if ( ! sem ) { 138 int retval;
131 SDL_SetError("Passed a NULL semaphore"); 139
132 return -1; 140 if (!sem) {
133 } 141 SDL_SetError("Passed a NULL semaphore");
134 142 return -1;
135 retval = SDL_MUTEX_TIMEDOUT; 143 }
136 SDL_LockMutex(sem->count_lock); 144
137 if ( sem->count > 0 ) { 145 retval = SDL_MUTEX_TIMEDOUT;
138 --sem->count; 146 SDL_LockMutex(sem->count_lock);
139 retval = 0; 147 if (sem->count > 0) {
140 } 148 --sem->count;
141 SDL_UnlockMutex(sem->count_lock); 149 retval = 0;
142 150 }
143 return retval; 151 SDL_UnlockMutex(sem->count_lock);
144 } 152
145 153 return retval;
146 int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout) 154 }
147 { 155
148 int retval; 156 int
149 157 SDL_SemWaitTimeout(SDL_sem * sem, Uint32 timeout)
150 if ( ! sem ) { 158 {
151 SDL_SetError("Passed a NULL semaphore"); 159 int retval;
152 return -1; 160
153 } 161 if (!sem) {
154 162 SDL_SetError("Passed a NULL semaphore");
155 /* A timeout of 0 is an easy case */ 163 return -1;
156 if ( timeout == 0 ) { 164 }
157 return SDL_SemTryWait(sem); 165
158 } 166 /* A timeout of 0 is an easy case */
159 167 if (timeout == 0) {
160 SDL_LockMutex(sem->count_lock); 168 return SDL_SemTryWait(sem);
161 ++sem->waiters_count; 169 }
162 retval = 0; 170
163 while ( (sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT) ) { 171 SDL_LockMutex(sem->count_lock);
164 retval = SDL_CondWaitTimeout(sem->count_nonzero, 172 ++sem->waiters_count;
165 sem->count_lock, timeout); 173 retval = 0;
166 } 174 while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
167 --sem->waiters_count; 175 retval = SDL_CondWaitTimeout(sem->count_nonzero,
168 --sem->count; 176 sem->count_lock, timeout);
169 SDL_UnlockMutex(sem->count_lock); 177 }
170 178 --sem->waiters_count;
171 return retval; 179 --sem->count;
172 } 180 SDL_UnlockMutex(sem->count_lock);
173 181
174 int SDL_SemWait(SDL_sem *sem) 182 return retval;
175 { 183 }
176 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT); 184
177 } 185 int
178 186 SDL_SemWait(SDL_sem * sem)
179 Uint32 SDL_SemValue(SDL_sem *sem) 187 {
180 { 188 return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
181 Uint32 value; 189 }
182 190
183 value = 0; 191 Uint32
184 if ( sem ) { 192 SDL_SemValue(SDL_sem * sem)
185 SDL_LockMutex(sem->count_lock); 193 {
186 value = sem->count; 194 Uint32 value;
187 SDL_UnlockMutex(sem->count_lock); 195
188 } 196 value = 0;
189 return value; 197 if (sem) {
190 } 198 SDL_LockMutex(sem->count_lock);
191 199 value = sem->count;
192 int SDL_SemPost(SDL_sem *sem) 200 SDL_UnlockMutex(sem->count_lock);
193 { 201 }
194 if ( ! sem ) { 202 return value;
195 SDL_SetError("Passed a NULL semaphore"); 203 }
196 return -1; 204
197 } 205 int
198 206 SDL_SemPost(SDL_sem * sem)
199 SDL_LockMutex(sem->count_lock); 207 {
200 if ( sem->waiters_count > 0 ) { 208 if (!sem) {
201 SDL_CondSignal(sem->count_nonzero); 209 SDL_SetError("Passed a NULL semaphore");
202 } 210 return -1;
203 ++sem->count; 211 }
204 SDL_UnlockMutex(sem->count_lock); 212
205 213 SDL_LockMutex(sem->count_lock);
206 return 0; 214 if (sem->waiters_count > 0) {
215 SDL_CondSignal(sem->count_nonzero);
216 }
217 ++sem->count;
218 SDL_UnlockMutex(sem->count_lock);
219
220 return 0;
207 } 221 }
208 222
209 #endif /* SDL_THREADS_DISABLED */ 223 #endif /* SDL_THREADS_DISABLED */
224 /* vi: set ts=4 sw=4 expandtab: */