]>
Commit | Line | Data |
---|---|---|
9385eb3d | 1 | /* |
224c7076 | 2 | * Copyright (c) 2000-2003, 2007 Apple Inc. All rights reserved. |
9385eb3d A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
9385eb3d A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
e9ce8d39 A |
23 | /* |
24 | * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991 | |
25 | * All Rights Reserved | |
26 | * | |
27 | * Permission to use, copy, modify, and distribute this software and | |
28 | * its documentation for any purpose and without fee is hereby granted, | |
29 | * provided that the above copyright notice appears in all copies and | |
30 | * that both the copyright notice and this permission notice appear in | |
31 | * supporting documentation. | |
32 | * | |
33 | * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE | |
34 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
35 | * FOR A PARTICULAR PURPOSE. | |
36 | * | |
37 | * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR | |
38 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
39 | * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, | |
40 | * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | |
41 | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
42 | */ | |
43 | /* | |
44 | * MkLinux | |
45 | */ | |
46 | ||
47 | /* | |
48 | * POSIX Pthread Library | |
49 | */ | |
50 | ||
51 | #include "pthread_internals.h" | |
52 | #include <sys/time.h> /* For struct timespec and getclock(). */ | |
53 | #include <stdio.h> | |
224c7076 A |
54 | |
55 | #include "plockstat.h" | |
56 | ||
9385eb3d | 57 | extern void _pthread_mutex_remove(pthread_mutex_t *, pthread_t); |
3d9156a7 A |
58 | extern int __unix_conforming; |
59 | ||
60 | #ifndef BUILDING_VARIANT /* [ */ | |
9385eb3d A |
61 | |
62 | /* | |
e9ce8d39 A |
63 | * Destroy a condition variable. |
64 | */ | |
65 | int | |
66 | pthread_cond_destroy(pthread_cond_t *cond) | |
67 | { | |
9385eb3d A |
68 | int ret; |
69 | int sig = cond->sig; | |
70 | ||
71 | /* to provide backwards compat for apps using united condtn vars */ | |
224c7076 | 72 | if((sig != _PTHREAD_COND_SIG) && (sig != _PTHREAD_COND_SIG_init) && (sig != _PTHREAD_KERN_COND_SIG)) |
9385eb3d A |
73 | return(EINVAL); |
74 | ||
75 | LOCK(cond->lock); | |
e9ce8d39 A |
76 | if (cond->sig == _PTHREAD_COND_SIG) |
77 | { | |
9385eb3d | 78 | if (cond->busy == (pthread_mutex_t *)NULL) |
e9ce8d39 A |
79 | { |
80 | cond->sig = _PTHREAD_NO_SIG; | |
224c7076 | 81 | ret = 0; |
9385eb3d A |
82 | } else |
83 | ret = EBUSY; | |
224c7076 A |
84 | } else if (cond->sig == _PTHREAD_KERN_COND_SIG) { |
85 | int condid = cond->_pthread_cond_kernid; | |
86 | UNLOCK(cond->lock); | |
87 | if (__pthread_cond_destroy(condid) == -1) | |
88 | return(errno); | |
89 | cond->sig = _PTHREAD_NO_SIG; | |
90 | return(0); | |
e9ce8d39 | 91 | } else |
9385eb3d A |
92 | ret = EINVAL; /* Not an initialized condition variable structure */ |
93 | UNLOCK(cond->lock); | |
94 | return (ret); | |
e9ce8d39 A |
95 | } |
96 | ||
e9ce8d39 A |
97 | |
98 | /* | |
99 | * Signal a condition variable, waking up all threads waiting for it. | |
100 | */ | |
101 | int | |
102 | pthread_cond_broadcast(pthread_cond_t *cond) | |
103 | { | |
9385eb3d A |
104 | kern_return_t kern_res; |
105 | semaphore_t sem; | |
106 | int sig = cond->sig; | |
107 | ||
108 | /* to provide backwards compat for apps using united condtn vars */ | |
224c7076 | 109 | if((sig != _PTHREAD_COND_SIG) && (sig != _PTHREAD_COND_SIG_init) && (sig != _PTHREAD_KERN_COND_SIG)) |
9385eb3d A |
110 | return(EINVAL); |
111 | ||
112 | LOCK(cond->lock); | |
113 | if (cond->sig != _PTHREAD_COND_SIG) | |
114 | { | |
115 | int res; | |
116 | ||
117 | if (cond->sig == _PTHREAD_COND_SIG_init) | |
118 | { | |
224c7076 A |
119 | _pthread_cond_init(cond, NULL, 0); |
120 | res = 0; | |
121 | } else if (cond->sig == _PTHREAD_KERN_COND_SIG) { | |
122 | int condid = cond->_pthread_cond_kernid; | |
123 | UNLOCK(cond->lock); | |
124 | if (__pthread_cond_broadcast(condid) == -1) | |
125 | return(errno); | |
126 | return(0); | |
9385eb3d A |
127 | } else |
128 | res = EINVAL; /* Not a condition variable */ | |
129 | UNLOCK(cond->lock); | |
130 | return (res); | |
131 | } | |
132 | else if ((sem = cond->sem) == SEMAPHORE_NULL) | |
133 | { | |
134 | /* Avoid kernel call since there are no waiters... */ | |
135 | UNLOCK(cond->lock); | |
224c7076 | 136 | return (0); |
9385eb3d A |
137 | } |
138 | cond->sigspending++; | |
139 | UNLOCK(cond->lock); | |
140 | ||
141 | PTHREAD_MACH_CALL(semaphore_signal_all(sem), kern_res); | |
142 | ||
143 | LOCK(cond->lock); | |
144 | cond->sigspending--; | |
145 | if (cond->waiters == 0 && cond->sigspending == 0) | |
146 | { | |
147 | cond->sem = SEMAPHORE_NULL; | |
148 | restore_sem_to_pool(sem); | |
149 | } | |
150 | UNLOCK(cond->lock); | |
151 | if (kern_res != KERN_SUCCESS) | |
152 | return (EINVAL); | |
224c7076 | 153 | return (0); |
e9ce8d39 A |
154 | } |
155 | ||
156 | /* | |
157 | * Signal a condition variable, waking a specified thread. | |
158 | */ | |
159 | int | |
160 | pthread_cond_signal_thread_np(pthread_cond_t *cond, pthread_t thread) | |
161 | { | |
9385eb3d A |
162 | kern_return_t kern_res; |
163 | semaphore_t sem; | |
164 | int sig = cond->sig; | |
165 | ||
166 | /* to provide backwards compat for apps using united condtn vars */ | |
9385eb3d | 167 | |
224c7076 A |
168 | if((sig != _PTHREAD_COND_SIG) && (sig != _PTHREAD_COND_SIG_init) && (sig != _PTHREAD_KERN_COND_SIG)) |
169 | return(EINVAL); | |
9385eb3d A |
170 | LOCK(cond->lock); |
171 | if (cond->sig != _PTHREAD_COND_SIG) | |
172 | { | |
173 | int ret; | |
174 | ||
175 | if (cond->sig == _PTHREAD_COND_SIG_init) | |
176 | { | |
224c7076 A |
177 | _pthread_cond_init(cond, NULL, 0); |
178 | ret = 0; | |
179 | } else if (cond->sig == _PTHREAD_KERN_COND_SIG) { | |
180 | int condid = cond->_pthread_cond_kernid; | |
181 | UNLOCK(cond->lock); | |
182 | if (__pthread_cond_signal(condid) == -1) | |
183 | return(errno); | |
184 | return(0); | |
185 | } else | |
9385eb3d A |
186 | ret = EINVAL; /* Not a condition variable */ |
187 | UNLOCK(cond->lock); | |
188 | return (ret); | |
189 | } | |
190 | else if ((sem = cond->sem) == SEMAPHORE_NULL) | |
191 | { | |
192 | /* Avoid kernel call since there are not enough waiters... */ | |
193 | UNLOCK(cond->lock); | |
224c7076 | 194 | return (0); |
e9ce8d39 | 195 | } |
9385eb3d A |
196 | cond->sigspending++; |
197 | UNLOCK(cond->lock); | |
198 | ||
199 | if (thread == (pthread_t)NULL) | |
200 | { | |
201 | kern_res = semaphore_signal_thread(sem, THREAD_NULL); | |
202 | if (kern_res == KERN_NOT_WAITING) | |
203 | kern_res = KERN_SUCCESS; | |
204 | } | |
205 | else if (thread->sig == _PTHREAD_SIG) | |
206 | { | |
207 | PTHREAD_MACH_CALL(semaphore_signal_thread( | |
208 | sem, pthread_mach_thread_np(thread)), kern_res); | |
209 | } | |
210 | else | |
211 | kern_res = KERN_FAILURE; | |
212 | ||
213 | LOCK(cond->lock); | |
214 | cond->sigspending--; | |
215 | if (cond->waiters == 0 && cond->sigspending == 0) | |
216 | { | |
217 | cond->sem = SEMAPHORE_NULL; | |
218 | restore_sem_to_pool(sem); | |
219 | } | |
220 | UNLOCK(cond->lock); | |
221 | if (kern_res != KERN_SUCCESS) | |
222 | return (EINVAL); | |
224c7076 | 223 | return (0); |
e9ce8d39 A |
224 | } |
225 | ||
226 | /* | |
227 | * Signal a condition variable, waking only one thread. | |
228 | */ | |
229 | int | |
230 | pthread_cond_signal(pthread_cond_t *cond) | |
231 | { | |
9385eb3d | 232 | return pthread_cond_signal_thread_np(cond, NULL); |
e9ce8d39 A |
233 | } |
234 | ||
235 | /* | |
236 | * Manage a list of condition variables associated with a mutex | |
237 | */ | |
238 | ||
239 | static void | |
240 | _pthread_cond_add(pthread_cond_t *cond, pthread_mutex_t *mutex) | |
241 | { | |
242 | pthread_cond_t *c; | |
243 | LOCK(mutex->lock); | |
244 | if ((c = mutex->busy) != (pthread_cond_t *)NULL) | |
245 | { | |
246 | c->prev = cond; | |
247 | } | |
248 | cond->next = c; | |
249 | cond->prev = (pthread_cond_t *)NULL; | |
250 | mutex->busy = cond; | |
251 | UNLOCK(mutex->lock); | |
9385eb3d | 252 | if (cond->sem == SEMAPHORE_NULL) |
e9ce8d39 | 253 | cond->sem = new_sem_from_pool(); |
e9ce8d39 A |
254 | } |
255 | ||
256 | static void | |
257 | _pthread_cond_remove(pthread_cond_t *cond, pthread_mutex_t *mutex) | |
258 | { | |
259 | pthread_cond_t *n, *p; | |
9385eb3d | 260 | |
e9ce8d39 A |
261 | LOCK(mutex->lock); |
262 | if ((n = cond->next) != (pthread_cond_t *)NULL) | |
263 | { | |
264 | n->prev = cond->prev; | |
265 | } | |
266 | if ((p = cond->prev) != (pthread_cond_t *)NULL) | |
267 | { | |
268 | p->next = cond->next; | |
9385eb3d A |
269 | } |
270 | else | |
e9ce8d39 A |
271 | { /* This is the first in the list */ |
272 | mutex->busy = n; | |
273 | } | |
274 | UNLOCK(mutex->lock); | |
9385eb3d A |
275 | if (cond->sigspending == 0) |
276 | { | |
277 | restore_sem_to_pool(cond->sem); | |
278 | cond->sem = SEMAPHORE_NULL; | |
e9ce8d39 A |
279 | } |
280 | } | |
281 | ||
3d9156a7 A |
282 | static void cond_cleanup(void *arg) |
283 | { | |
284 | pthread_cond_t *cond = (pthread_cond_t *)arg; | |
285 | pthread_mutex_t *mutex; | |
224c7076 A |
286 | // 4597450: begin |
287 | pthread_t thread = pthread_self(); | |
288 | int thcanceled = 0; | |
289 | ||
290 | LOCK(thread->lock); | |
291 | thcanceled = (thread->detached & _PTHREAD_WASCANCEL); | |
292 | UNLOCK(thread->lock); | |
293 | ||
294 | if (thcanceled == 0) | |
295 | return; | |
296 | ||
297 | // 4597450: end | |
3d9156a7 A |
298 | LOCK(cond->lock); |
299 | mutex = cond->busy; | |
300 | cond->waiters--; | |
301 | if (cond->waiters == 0) { | |
302 | _pthread_cond_remove(cond, mutex); | |
303 | cond->busy = (pthread_mutex_t *)NULL; | |
304 | } | |
305 | UNLOCK(cond->lock); | |
224c7076 | 306 | |
3d9156a7 A |
307 | /* |
308 | ** Can't do anything if this fails -- we're on the way out | |
309 | */ | |
310 | (void)pthread_mutex_lock(mutex); | |
311 | } | |
312 | ||
e9ce8d39 A |
313 | /* |
314 | * Suspend waiting for a condition variable. | |
315 | * Note: we have to keep a list of condition variables which are using | |
316 | * this same mutex variable so we can detect invalid 'destroy' sequences. | |
224c7076 A |
317 | * If isconforming < 0, we skip the _pthread_testcancel(), but keep the |
318 | * remaining conforming behavior.. | |
e9ce8d39 | 319 | */ |
3d9156a7 | 320 | __private_extern__ int |
e9ce8d39 A |
321 | _pthread_cond_wait(pthread_cond_t *cond, |
322 | pthread_mutex_t *mutex, | |
323 | const struct timespec *abstime, | |
3d9156a7 A |
324 | int isRelative, |
325 | int isconforming) | |
e9ce8d39 | 326 | { |
224c7076 | 327 | int res, saved_error; |
9385eb3d | 328 | kern_return_t kern_res; |
3d9156a7 | 329 | int wait_res; |
9385eb3d A |
330 | pthread_mutex_t *busy; |
331 | mach_timespec_t then; | |
3d9156a7 | 332 | struct timespec cthen = {0,0}; |
9385eb3d | 333 | int sig = cond->sig; |
224c7076 A |
334 | int msig = mutex->sig; |
335 | extern void _pthread_testcancel(pthread_t thread, int isconforming); | |
9385eb3d A |
336 | |
337 | /* to provide backwards compat for apps using united condtn vars */ | |
224c7076 | 338 | if((sig != _PTHREAD_COND_SIG) && (sig != _PTHREAD_COND_SIG_init) && (sig != _PTHREAD_KERN_COND_SIG)) |
9385eb3d | 339 | return(EINVAL); |
224c7076 A |
340 | |
341 | if (isconforming) { | |
342 | if((msig != _PTHREAD_MUTEX_SIG) && (msig != _PTHREAD_MUTEX_SIG_init) && (msig != _PTHREAD_KERN_MUTEX_SIG)) | |
343 | return(EINVAL); | |
344 | if (isconforming > 0) | |
345 | _pthread_testcancel(pthread_self(), 1); | |
346 | } | |
9385eb3d A |
347 | LOCK(cond->lock); |
348 | if (cond->sig != _PTHREAD_COND_SIG) | |
349 | { | |
350 | if (cond->sig != _PTHREAD_COND_SIG_init) | |
351 | { | |
224c7076 A |
352 | if ((cond->sig == _PTHREAD_KERN_COND_SIG) && (mutex->sig == _PTHREAD_KERN_MUTEX_SIG)) { |
353 | int condid = cond->_pthread_cond_kernid; | |
354 | int mutexid = mutex->_pthread_mutex_kernid; | |
355 | UNLOCK(cond->lock); | |
356 | ||
357 | if (abstime) { | |
358 | struct timespec now; | |
359 | struct timeval tv; | |
360 | gettimeofday(&tv, NULL); | |
361 | TIMEVAL_TO_TIMESPEC(&tv, &now); | |
362 | ||
363 | /* Compute relative time to sleep */ | |
364 | then.tv_nsec = abstime->tv_nsec - now.tv_nsec; | |
365 | then.tv_sec = abstime->tv_sec - now.tv_sec; | |
366 | if (then.tv_nsec < 0) | |
367 | { | |
368 | then.tv_nsec += NSEC_PER_SEC; | |
369 | then.tv_sec--; | |
370 | } | |
371 | if (((int)then.tv_sec < 0) || | |
372 | ((then.tv_sec == 0) && (then.tv_nsec == 0))) | |
373 | { | |
374 | UNLOCK(cond->lock); | |
375 | return ETIMEDOUT; | |
376 | } | |
377 | if ((res = pthread_mutex_unlock(mutex)) != 0) | |
378 | return (res); | |
379 | ||
380 | if ((__pthread_cond_timedwait(condid, mutexid, &then)) == -1) | |
381 | saved_error = errno; | |
382 | else | |
383 | saved_error = 0; | |
384 | } else { | |
385 | if ((res = pthread_mutex_unlock(mutex)) != 0) | |
386 | return (res); | |
387 | if(( __pthread_cond_wait(condid, mutexid)) == -1) | |
388 | saved_error = errno; | |
389 | else | |
390 | saved_error = 0; | |
391 | } | |
392 | if ((res = pthread_mutex_lock(mutex)) != 0) | |
393 | return (res); | |
394 | return(saved_error); | |
395 | } else { | |
396 | UNLOCK(cond->lock); | |
397 | return (EINVAL); /* Not a condition variable */ | |
398 | } | |
9385eb3d | 399 | } |
224c7076 | 400 | _pthread_cond_init(cond, NULL, 0); |
9385eb3d A |
401 | } |
402 | ||
3d9156a7 A |
403 | if (abstime) { |
404 | if (!isconforming) | |
9385eb3d | 405 | { |
3d9156a7 A |
406 | if (isRelative == 0) { |
407 | struct timespec now; | |
408 | struct timeval tv; | |
409 | gettimeofday(&tv, NULL); | |
410 | TIMEVAL_TO_TIMESPEC(&tv, &now); | |
411 | ||
412 | /* Compute relative time to sleep */ | |
413 | then.tv_nsec = abstime->tv_nsec - now.tv_nsec; | |
414 | then.tv_sec = abstime->tv_sec - now.tv_sec; | |
415 | if (then.tv_nsec < 0) | |
416 | { | |
417 | then.tv_nsec += NSEC_PER_SEC; | |
418 | then.tv_sec--; | |
419 | } | |
420 | if (((int)then.tv_sec < 0) || | |
421 | ((then.tv_sec == 0) && (then.tv_nsec == 0))) | |
422 | { | |
423 | UNLOCK(cond->lock); | |
424 | return ETIMEDOUT; | |
425 | } | |
426 | } else { | |
427 | then.tv_sec = abstime->tv_sec; | |
428 | then.tv_nsec = abstime->tv_nsec; | |
9385eb3d | 429 | } |
3d9156a7 | 430 | if (then.tv_nsec >= NSEC_PER_SEC) { |
9385eb3d | 431 | UNLOCK(cond->lock); |
3d9156a7 | 432 | return EINVAL; |
9385eb3d | 433 | } |
3d9156a7 | 434 | } else { |
224c7076 A |
435 | if (isRelative == 0) { |
436 | /* preflight the checks for failures */ | |
437 | struct timespec now; | |
438 | struct timeval tv; | |
439 | gettimeofday(&tv, NULL); | |
440 | TIMEVAL_TO_TIMESPEC(&tv, &now); | |
441 | ||
442 | /* Compute relative time to sleep */ | |
443 | then.tv_nsec = abstime->tv_nsec - now.tv_nsec; | |
444 | then.tv_sec = abstime->tv_sec - now.tv_sec; | |
445 | if (then.tv_nsec < 0) | |
446 | { | |
447 | then.tv_nsec += NSEC_PER_SEC; | |
448 | then.tv_sec--; | |
449 | } | |
450 | if (((int)then.tv_sec < 0) || | |
451 | ((then.tv_sec == 0) && (then.tv_nsec == 0))) | |
452 | { | |
453 | UNLOCK(cond->lock); | |
454 | return ETIMEDOUT; | |
455 | } | |
456 | if (then.tv_nsec >= NSEC_PER_SEC) { | |
457 | UNLOCK(cond->lock); | |
458 | return EINVAL; | |
459 | } | |
460 | } | |
461 | /* we can cleanup this code and pass the calculated time | |
462 | * to the kernel. But kernel is going to do the same. TILL | |
463 | * we change the kernel do this anyway | |
464 | */ | |
3d9156a7 A |
465 | cthen.tv_sec = abstime->tv_sec; |
466 | cthen.tv_nsec = abstime->tv_nsec; | |
467 | if ((cthen.tv_sec < 0) || (cthen.tv_nsec < 0)) { | |
468 | UNLOCK(cond->lock); | |
469 | return EINVAL; | |
470 | } | |
471 | if (cthen.tv_nsec >= NSEC_PER_SEC) { | |
472 | UNLOCK(cond->lock); | |
473 | return EINVAL; | |
474 | } | |
475 | } | |
9385eb3d A |
476 | } |
477 | ||
478 | if (++cond->waiters == 1) | |
479 | { | |
480 | _pthread_cond_add(cond, mutex); | |
481 | cond->busy = mutex; | |
482 | } | |
483 | else if ((busy = cond->busy) != mutex) | |
484 | { | |
485 | /* Must always specify the same mutex! */ | |
486 | cond->waiters--; | |
487 | UNLOCK(cond->lock); | |
488 | return (EINVAL); | |
489 | } | |
490 | UNLOCK(cond->lock); | |
491 | ||
492 | #if defined(DEBUG) | |
493 | _pthread_mutex_remove(mutex, pthread_self()); | |
494 | #endif | |
495 | LOCK(mutex->lock); | |
496 | if (--mutex->lock_count == 0) | |
497 | { | |
224c7076 A |
498 | PLOCKSTAT_MUTEX_RELEASE(mutex, (mutex->type == PTHREAD_MUTEX_RECURSIVE)? 1:0); |
499 | ||
9385eb3d A |
500 | if (mutex->sem == SEMAPHORE_NULL) |
501 | mutex->sem = new_sem_from_pool(); | |
502 | mutex->owner = _PTHREAD_MUTEX_OWNER_SWITCHING; | |
503 | UNLOCK(mutex->lock); | |
504 | ||
3d9156a7 A |
505 | if (!isconforming) { |
506 | if (abstime) { | |
507 | kern_res = semaphore_timedwait_signal(cond->sem, mutex->sem, then); | |
508 | } else { | |
509 | PTHREAD_MACH_CALL(semaphore_wait_signal(cond->sem, mutex->sem), kern_res); | |
510 | } | |
9385eb3d | 511 | } else { |
3d9156a7 A |
512 | pthread_cleanup_push(cond_cleanup, (void *)cond); |
513 | wait_res = __semwait_signal(cond->sem, mutex->sem, abstime != NULL, isRelative, | |
514 | cthen.tv_sec, cthen.tv_nsec); | |
515 | pthread_cleanup_pop(0); | |
9385eb3d | 516 | } |
3d9156a7 | 517 | } else { |
224c7076 | 518 | PLOCKSTAT_MUTEX_RELEASE(mutex, (mutex->type == PTHREAD_MUTEX_RECURSIVE)? 1:0); |
9385eb3d | 519 | UNLOCK(mutex->lock); |
3d9156a7 A |
520 | if (!isconforming) { |
521 | if (abstime) { | |
522 | kern_res = semaphore_timedwait(cond->sem, then); | |
523 | } else { | |
524 | PTHREAD_MACH_CALL(semaphore_wait(cond->sem), kern_res); | |
525 | } | |
526 | } else { | |
527 | pthread_cleanup_push(cond_cleanup, (void *)cond); | |
528 | wait_res = __semwait_signal(cond->sem, NULL, abstime != NULL, isRelative, | |
529 | cthen.tv_sec, cthen.tv_nsec); | |
530 | pthread_cleanup_pop(0); | |
9385eb3d | 531 | } |
3d9156a7 | 532 | |
9385eb3d A |
533 | } |
534 | ||
535 | LOCK(cond->lock); | |
536 | cond->waiters--; | |
537 | if (cond->waiters == 0) | |
538 | { | |
539 | _pthread_cond_remove(cond, mutex); | |
540 | cond->busy = (pthread_mutex_t *)NULL; | |
541 | } | |
542 | UNLOCK(cond->lock); | |
224c7076 | 543 | if ((res = pthread_mutex_lock(mutex)) != 0) |
9385eb3d A |
544 | return (res); |
545 | ||
3d9156a7 A |
546 | if (!isconforming) { |
547 | /* KERN_ABORTED can be treated as a spurious wakeup */ | |
548 | if ((kern_res == KERN_SUCCESS) || (kern_res == KERN_ABORTED)) | |
224c7076 | 549 | return (0); |
3d9156a7 A |
550 | else if (kern_res == KERN_OPERATION_TIMED_OUT) |
551 | return (ETIMEDOUT); | |
552 | return (EINVAL); | |
553 | } else { | |
554 | if (wait_res < 0) { | |
555 | if (errno == ETIMEDOUT) { | |
556 | return ETIMEDOUT; | |
557 | } else if (errno == EINTR) { | |
558 | /* | |
559 | ** EINTR can be treated as a spurious wakeup unless we were canceled. | |
560 | */ | |
561 | return 0; | |
562 | } | |
563 | return EINVAL; | |
564 | } | |
565 | return 0; | |
566 | } | |
e9ce8d39 A |
567 | } |
568 | ||
e9ce8d39 A |
569 | |
570 | int | |
571 | pthread_cond_timedwait_relative_np(pthread_cond_t *cond, | |
572 | pthread_mutex_t *mutex, | |
573 | const struct timespec *abstime) | |
574 | { | |
3d9156a7 | 575 | return (_pthread_cond_wait(cond, mutex, abstime, 1, 0)); |
e9ce8d39 A |
576 | } |
577 | ||
5b2abdfb A |
578 | int |
579 | pthread_condattr_init(pthread_condattr_t *attr) | |
580 | { | |
581 | attr->sig = _PTHREAD_COND_ATTR_SIG; | |
224c7076 A |
582 | attr->pshared = _PTHREAD_DEFAULT_PSHARED; |
583 | return (0); | |
5b2abdfb A |
584 | } |
585 | ||
586 | int | |
587 | pthread_condattr_destroy(pthread_condattr_t *attr) | |
588 | { | |
589 | attr->sig = _PTHREAD_NO_SIG; /* Uninitialized */ | |
224c7076 | 590 | return (0); |
5b2abdfb A |
591 | } |
592 | ||
593 | int | |
594 | pthread_condattr_getpshared(const pthread_condattr_t *attr, | |
595 | int *pshared) | |
596 | { | |
597 | if (attr->sig == _PTHREAD_COND_ATTR_SIG) | |
598 | { | |
224c7076 A |
599 | *pshared = (int)attr->pshared; |
600 | return (0); | |
5b2abdfb A |
601 | } else |
602 | { | |
603 | return (EINVAL); /* Not an initialized 'attribute' structure */ | |
604 | } | |
605 | } | |
606 | ||
224c7076 A |
607 | #ifdef PR_5243343 |
608 | /* 5243343 - temporary hack to detect if we are running the conformance test */ | |
609 | extern int PR_5243343_flag; | |
610 | #endif /* PR_5243343 */ | |
5b2abdfb | 611 | |
224c7076 A |
612 | __private_extern__ int |
613 | _pthread_cond_init(pthread_cond_t *cond, | |
614 | const pthread_condattr_t *attr, | |
615 | int conforming) | |
616 | { | |
617 | cond->next = (pthread_cond_t *)NULL; | |
618 | cond->prev = (pthread_cond_t *)NULL; | |
619 | cond->busy = (pthread_mutex_t *)NULL; | |
620 | cond->waiters = 0; | |
621 | cond->sigspending = 0; | |
622 | if (conforming) { | |
623 | if (attr) { | |
624 | cond->pshared = attr->pshared; | |
625 | if (cond->pshared == PTHREAD_PROCESS_SHARED) { | |
626 | cond->sem = SEMAPHORE_NULL; | |
627 | cond->sig = 0; | |
628 | if( __pthread_cond_init(cond, attr) == -1) | |
629 | return(errno); | |
630 | cond->sig = _PTHREAD_KERN_COND_SIG; | |
631 | return(0); | |
632 | } | |
633 | } | |
634 | else | |
635 | cond->pshared = _PTHREAD_DEFAULT_PSHARED; | |
636 | } else | |
637 | cond->pshared = _PTHREAD_DEFAULT_PSHARED; | |
638 | cond->sem = SEMAPHORE_NULL; | |
639 | cond->sig = _PTHREAD_COND_SIG; | |
640 | return (0); | |
641 | } | |
642 | ||
643 | ||
644 | /* temp home till pshared is fixed correctly */ | |
5b2abdfb A |
645 | int |
646 | pthread_condattr_setpshared(pthread_condattr_t * attr, int pshared) | |
647 | { | |
224c7076 | 648 | |
5b2abdfb A |
649 | if (attr->sig == _PTHREAD_COND_ATTR_SIG) |
650 | { | |
224c7076 A |
651 | #if __DARWIN_UNIX03 |
652 | #ifdef PR_5243343 | |
653 | if (( pshared == PTHREAD_PROCESS_PRIVATE) || (pshared == PTHREAD_PROCESS_SHARED && PR_5243343_flag)) | |
654 | #else /* !PR_5243343 */ | |
655 | if (( pshared == PTHREAD_PROCESS_PRIVATE) || (pshared == PTHREAD_PROCESS_SHARED)) | |
656 | #endif /* PR_5243343 */ | |
657 | #else /* __DARWIN_UNIX03 */ | |
5b2abdfb | 658 | if ( pshared == PTHREAD_PROCESS_PRIVATE) |
224c7076 | 659 | #endif /* __DARWIN_UNIX03 */ |
5b2abdfb | 660 | { |
224c7076 A |
661 | attr->pshared = pshared; |
662 | return (0); | |
5b2abdfb A |
663 | } else |
664 | { | |
665 | return (EINVAL); /* Invalid parameter */ | |
666 | } | |
667 | } else | |
668 | { | |
669 | return (EINVAL); /* Not an initialized 'attribute' structure */ | |
670 | } | |
671 | ||
672 | } | |
673 | ||
224c7076 | 674 | |
3d9156a7 A |
675 | #else /* !BUILDING_VARIANT */ |
676 | extern int _pthread_cond_wait(pthread_cond_t *cond, | |
677 | pthread_mutex_t *mutex, | |
678 | const struct timespec *abstime, | |
679 | int isRelative, | |
680 | int isconforming); | |
681 | ||
224c7076 A |
682 | extern int |
683 | _pthread_cond_init(pthread_cond_t *cond, | |
684 | const pthread_condattr_t *attr, | |
685 | int conforming); | |
686 | ||
3d9156a7 | 687 | #endif /* !BUILDING_VARIANT ] */ |
224c7076 A |
688 | /* |
689 | * Initialize a condition variable. Note: 'attr' is ignored. | |
690 | */ | |
3d9156a7 | 691 | |
224c7076 A |
692 | /* |
693 | * Initialize a condition variable. This is the public interface. | |
694 | * We can't trust the lock, so initialize it first before taking | |
695 | * it. | |
696 | */ | |
3d9156a7 | 697 | int |
224c7076 A |
698 | pthread_cond_init(pthread_cond_t *cond, |
699 | const pthread_condattr_t *attr) | |
3d9156a7 A |
700 | { |
701 | int conforming; | |
3d9156a7 | 702 | |
224c7076 A |
703 | #if __DARWIN_UNIX03 |
704 | conforming = 1; | |
3d9156a7 | 705 | #else /* __DARWIN_UNIX03 */ |
224c7076 | 706 | conforming = 0; |
3d9156a7 | 707 | #endif /* __DARWIN_UNIX03 */ |
224c7076 A |
708 | |
709 | ||
710 | LOCK_INIT(cond->lock); | |
711 | return (_pthread_cond_init(cond, attr, conforming)); | |
3d9156a7 A |
712 | } |
713 | ||
224c7076 A |
714 | /* |
715 | int | |
716 | pthread_cond_wait(pthread_cond_t *cond, | |
717 | pthread_mutex_t *mutex) | |
718 | ||
3d9156a7 A |
719 | int |
720 | pthread_cond_timedwait(pthread_cond_t *cond, | |
721 | pthread_mutex_t *mutex, | |
722 | const struct timespec *abstime) | |
3d9156a7 | 723 | |
224c7076 | 724 | moved to pthread_cancelable.c */ |