]> git.saurik.com Git - apple/libc.git/blob - pthreads/pthread_cond.c
Libc-391.4.1.tar.gz
[apple/libc.git] / pthreads / pthread_cond.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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 */
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>
54
55 extern void _pthread_mutex_remove(pthread_mutex_t *, pthread_t);
56 extern int __unix_conforming;
57
58 #ifndef BUILDING_VARIANT /* [ */
59
60 /*
61 * Destroy a condition variable.
62 */
63 int
64 pthread_cond_destroy(pthread_cond_t *cond)
65 {
66 int ret;
67 int sig = cond->sig;
68
69 /* to provide backwards compat for apps using united condtn vars */
70 if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
71 return(EINVAL);
72
73 LOCK(cond->lock);
74 if (cond->sig == _PTHREAD_COND_SIG)
75 {
76 if (cond->busy == (pthread_mutex_t *)NULL)
77 {
78 cond->sig = _PTHREAD_NO_SIG;
79 ret = ESUCCESS;
80 } else
81 ret = EBUSY;
82 } else
83 ret = EINVAL; /* Not an initialized condition variable structure */
84 UNLOCK(cond->lock);
85 return (ret);
86 }
87
88 /*
89 * Initialize a condition variable. Note: 'attr' is ignored.
90 */
91 static int
92 _pthread_cond_init(pthread_cond_t *cond,
93 const pthread_condattr_t *attr)
94 {
95 cond->next = (pthread_cond_t *)NULL;
96 cond->prev = (pthread_cond_t *)NULL;
97 cond->busy = (pthread_mutex_t *)NULL;
98 cond->waiters = 0;
99 cond->sigspending = 0;
100 cond->sem = SEMAPHORE_NULL;
101 cond->sig = _PTHREAD_COND_SIG;
102 return (ESUCCESS);
103 }
104
105 /*
106 * Initialize a condition variable. This is the public interface.
107 * We can't trust the lock, so initialize it first before taking
108 * it.
109 */
110 int
111 pthread_cond_init(pthread_cond_t *cond,
112 const pthread_condattr_t *attr)
113 {
114 LOCK_INIT(cond->lock);
115 return (_pthread_cond_init(cond, attr));
116 }
117
118 /*
119 * Signal a condition variable, waking up all threads waiting for it.
120 */
121 int
122 pthread_cond_broadcast(pthread_cond_t *cond)
123 {
124 kern_return_t kern_res;
125 semaphore_t sem;
126 int sig = cond->sig;
127
128 /* to provide backwards compat for apps using united condtn vars */
129 if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
130 return(EINVAL);
131
132 LOCK(cond->lock);
133 if (cond->sig != _PTHREAD_COND_SIG)
134 {
135 int res;
136
137 if (cond->sig == _PTHREAD_COND_SIG_init)
138 {
139 _pthread_cond_init(cond, NULL);
140 res = ESUCCESS;
141 } else
142 res = EINVAL; /* Not a condition variable */
143 UNLOCK(cond->lock);
144 return (res);
145 }
146 else if ((sem = cond->sem) == SEMAPHORE_NULL)
147 {
148 /* Avoid kernel call since there are no waiters... */
149 UNLOCK(cond->lock);
150 return (ESUCCESS);
151 }
152 cond->sigspending++;
153 UNLOCK(cond->lock);
154
155 PTHREAD_MACH_CALL(semaphore_signal_all(sem), kern_res);
156
157 LOCK(cond->lock);
158 cond->sigspending--;
159 if (cond->waiters == 0 && cond->sigspending == 0)
160 {
161 cond->sem = SEMAPHORE_NULL;
162 restore_sem_to_pool(sem);
163 }
164 UNLOCK(cond->lock);
165 if (kern_res != KERN_SUCCESS)
166 return (EINVAL);
167 return (ESUCCESS);
168 }
169
170 /*
171 * Signal a condition variable, waking a specified thread.
172 */
173 int
174 pthread_cond_signal_thread_np(pthread_cond_t *cond, pthread_t thread)
175 {
176 kern_return_t kern_res;
177 semaphore_t sem;
178 int sig = cond->sig;
179
180 /* to provide backwards compat for apps using united condtn vars */
181 if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
182 return(EINVAL);
183
184 LOCK(cond->lock);
185 if (cond->sig != _PTHREAD_COND_SIG)
186 {
187 int ret;
188
189 if (cond->sig == _PTHREAD_COND_SIG_init)
190 {
191 _pthread_cond_init(cond, NULL);
192 ret = ESUCCESS;
193 }
194 else
195 ret = EINVAL; /* Not a condition variable */
196 UNLOCK(cond->lock);
197 return (ret);
198 }
199 else if ((sem = cond->sem) == SEMAPHORE_NULL)
200 {
201 /* Avoid kernel call since there are not enough waiters... */
202 UNLOCK(cond->lock);
203 return (ESUCCESS);
204 }
205 cond->sigspending++;
206 UNLOCK(cond->lock);
207
208 if (thread == (pthread_t)NULL)
209 {
210 kern_res = semaphore_signal_thread(sem, THREAD_NULL);
211 if (kern_res == KERN_NOT_WAITING)
212 kern_res = KERN_SUCCESS;
213 }
214 else if (thread->sig == _PTHREAD_SIG)
215 {
216 PTHREAD_MACH_CALL(semaphore_signal_thread(
217 sem, pthread_mach_thread_np(thread)), kern_res);
218 }
219 else
220 kern_res = KERN_FAILURE;
221
222 LOCK(cond->lock);
223 cond->sigspending--;
224 if (cond->waiters == 0 && cond->sigspending == 0)
225 {
226 cond->sem = SEMAPHORE_NULL;
227 restore_sem_to_pool(sem);
228 }
229 UNLOCK(cond->lock);
230 if (kern_res != KERN_SUCCESS)
231 return (EINVAL);
232 return (ESUCCESS);
233 }
234
235 /*
236 * Signal a condition variable, waking only one thread.
237 */
238 int
239 pthread_cond_signal(pthread_cond_t *cond)
240 {
241 return pthread_cond_signal_thread_np(cond, NULL);
242 }
243
244 /*
245 * Manage a list of condition variables associated with a mutex
246 */
247
248 static void
249 _pthread_cond_add(pthread_cond_t *cond, pthread_mutex_t *mutex)
250 {
251 pthread_cond_t *c;
252 LOCK(mutex->lock);
253 if ((c = mutex->busy) != (pthread_cond_t *)NULL)
254 {
255 c->prev = cond;
256 }
257 cond->next = c;
258 cond->prev = (pthread_cond_t *)NULL;
259 mutex->busy = cond;
260 UNLOCK(mutex->lock);
261 if (cond->sem == SEMAPHORE_NULL)
262 cond->sem = new_sem_from_pool();
263 }
264
265 static void
266 _pthread_cond_remove(pthread_cond_t *cond, pthread_mutex_t *mutex)
267 {
268 pthread_cond_t *n, *p;
269
270 LOCK(mutex->lock);
271 if ((n = cond->next) != (pthread_cond_t *)NULL)
272 {
273 n->prev = cond->prev;
274 }
275 if ((p = cond->prev) != (pthread_cond_t *)NULL)
276 {
277 p->next = cond->next;
278 }
279 else
280 { /* This is the first in the list */
281 mutex->busy = n;
282 }
283 UNLOCK(mutex->lock);
284 if (cond->sigspending == 0)
285 {
286 restore_sem_to_pool(cond->sem);
287 cond->sem = SEMAPHORE_NULL;
288 }
289 }
290
291 static void cond_cleanup(void *arg)
292 {
293 pthread_cond_t *cond = (pthread_cond_t *)arg;
294 pthread_mutex_t *mutex;
295 LOCK(cond->lock);
296 mutex = cond->busy;
297 cond->waiters--;
298 if (cond->waiters == 0) {
299 _pthread_cond_remove(cond, mutex);
300 cond->busy = (pthread_mutex_t *)NULL;
301 }
302 UNLOCK(cond->lock);
303 /*
304 ** Can't do anything if this fails -- we're on the way out
305 */
306 (void)pthread_mutex_lock(mutex);
307 }
308
309 /*
310 * Suspend waiting for a condition variable.
311 * Note: we have to keep a list of condition variables which are using
312 * this same mutex variable so we can detect invalid 'destroy' sequences.
313 */
314 __private_extern__ int
315 _pthread_cond_wait(pthread_cond_t *cond,
316 pthread_mutex_t *mutex,
317 const struct timespec *abstime,
318 int isRelative,
319 int isconforming)
320 {
321 int res;
322 kern_return_t kern_res;
323 int wait_res;
324 pthread_mutex_t *busy;
325 mach_timespec_t then;
326 struct timespec cthen = {0,0};
327 int sig = cond->sig;
328
329 /* to provide backwards compat for apps using united condtn vars */
330 if((sig != _PTHREAD_COND_SIG) && (sig !=_PTHREAD_COND_SIG_init))
331 return(EINVAL);
332 LOCK(cond->lock);
333 if (cond->sig != _PTHREAD_COND_SIG)
334 {
335 if (cond->sig != _PTHREAD_COND_SIG_init)
336 {
337 UNLOCK(cond->lock);
338 return (EINVAL); /* Not a condition variable */
339 }
340 _pthread_cond_init(cond, NULL);
341 }
342
343 if (abstime) {
344 if (!isconforming)
345 {
346 if (isRelative == 0) {
347 struct timespec now;
348 struct timeval tv;
349 gettimeofday(&tv, NULL);
350 TIMEVAL_TO_TIMESPEC(&tv, &now);
351
352 /* Compute relative time to sleep */
353 then.tv_nsec = abstime->tv_nsec - now.tv_nsec;
354 then.tv_sec = abstime->tv_sec - now.tv_sec;
355 if (then.tv_nsec < 0)
356 {
357 then.tv_nsec += NSEC_PER_SEC;
358 then.tv_sec--;
359 }
360 if (((int)then.tv_sec < 0) ||
361 ((then.tv_sec == 0) && (then.tv_nsec == 0)))
362 {
363 UNLOCK(cond->lock);
364 return ETIMEDOUT;
365 }
366 } else {
367 then.tv_sec = abstime->tv_sec;
368 then.tv_nsec = abstime->tv_nsec;
369 }
370 if (then.tv_nsec >= NSEC_PER_SEC) {
371 UNLOCK(cond->lock);
372 return EINVAL;
373 }
374 } else {
375 cthen.tv_sec = abstime->tv_sec;
376 cthen.tv_nsec = abstime->tv_nsec;
377 if ((cthen.tv_sec < 0) || (cthen.tv_nsec < 0)) {
378 UNLOCK(cond->lock);
379 return EINVAL;
380 }
381 if (cthen.tv_nsec >= NSEC_PER_SEC) {
382 UNLOCK(cond->lock);
383 return EINVAL;
384 }
385 }
386 }
387
388 if (++cond->waiters == 1)
389 {
390 _pthread_cond_add(cond, mutex);
391 cond->busy = mutex;
392 }
393 else if ((busy = cond->busy) != mutex)
394 {
395 /* Must always specify the same mutex! */
396 cond->waiters--;
397 UNLOCK(cond->lock);
398 return (EINVAL);
399 }
400 UNLOCK(cond->lock);
401
402 #if defined(DEBUG)
403 _pthread_mutex_remove(mutex, pthread_self());
404 #endif
405 LOCK(mutex->lock);
406 if (--mutex->lock_count == 0)
407 {
408 if (mutex->sem == SEMAPHORE_NULL)
409 mutex->sem = new_sem_from_pool();
410 mutex->owner = _PTHREAD_MUTEX_OWNER_SWITCHING;
411 UNLOCK(mutex->lock);
412
413 if (!isconforming) {
414 if (abstime) {
415 kern_res = semaphore_timedwait_signal(cond->sem, mutex->sem, then);
416 } else {
417 PTHREAD_MACH_CALL(semaphore_wait_signal(cond->sem, mutex->sem), kern_res);
418 }
419 } else {
420 pthread_cleanup_push(cond_cleanup, (void *)cond);
421 wait_res = __semwait_signal(cond->sem, mutex->sem, abstime != NULL, isRelative,
422 cthen.tv_sec, cthen.tv_nsec);
423 pthread_cleanup_pop(0);
424 }
425 } else {
426 UNLOCK(mutex->lock);
427 if (!isconforming) {
428 if (abstime) {
429 kern_res = semaphore_timedwait(cond->sem, then);
430 } else {
431 PTHREAD_MACH_CALL(semaphore_wait(cond->sem), kern_res);
432 }
433 } else {
434 pthread_cleanup_push(cond_cleanup, (void *)cond);
435 wait_res = __semwait_signal(cond->sem, NULL, abstime != NULL, isRelative,
436 cthen.tv_sec, cthen.tv_nsec);
437 pthread_cleanup_pop(0);
438 }
439
440 }
441
442 LOCK(cond->lock);
443 cond->waiters--;
444 if (cond->waiters == 0)
445 {
446 _pthread_cond_remove(cond, mutex);
447 cond->busy = (pthread_mutex_t *)NULL;
448 }
449 UNLOCK(cond->lock);
450 if ((res = pthread_mutex_lock(mutex)) != ESUCCESS)
451 return (res);
452
453 if (!isconforming) {
454 /* KERN_ABORTED can be treated as a spurious wakeup */
455 if ((kern_res == KERN_SUCCESS) || (kern_res == KERN_ABORTED))
456 return (ESUCCESS);
457 else if (kern_res == KERN_OPERATION_TIMED_OUT)
458 return (ETIMEDOUT);
459 return (EINVAL);
460 } else {
461 if (wait_res < 0) {
462 if (errno == ETIMEDOUT) {
463 return ETIMEDOUT;
464 } else if (errno == EINTR) {
465 /*
466 ** EINTR can be treated as a spurious wakeup unless we were canceled.
467 */
468 return 0;
469 }
470 return EINVAL;
471 }
472 return 0;
473 }
474 }
475
476
477 int
478 pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
479 pthread_mutex_t *mutex,
480 const struct timespec *abstime)
481 {
482 return (_pthread_cond_wait(cond, mutex, abstime, 1, 0));
483 }
484
485 int
486 pthread_condattr_init(pthread_condattr_t *attr)
487 {
488 attr->sig = _PTHREAD_COND_ATTR_SIG;
489 return (ESUCCESS);
490 }
491
492 int
493 pthread_condattr_destroy(pthread_condattr_t *attr)
494 {
495 attr->sig = _PTHREAD_NO_SIG; /* Uninitialized */
496 return (ESUCCESS);
497 }
498
499 int
500 pthread_condattr_getpshared(const pthread_condattr_t *attr,
501 int *pshared)
502 {
503 if (attr->sig == _PTHREAD_COND_ATTR_SIG)
504 {
505 *pshared = (int)PTHREAD_PROCESS_PRIVATE;
506 return (ESUCCESS);
507 } else
508 {
509 return (EINVAL); /* Not an initialized 'attribute' structure */
510 }
511 }
512
513
514 int
515 pthread_condattr_setpshared(pthread_condattr_t * attr, int pshared)
516 {
517 if (attr->sig == _PTHREAD_COND_ATTR_SIG)
518 {
519 if ( pshared == PTHREAD_PROCESS_PRIVATE)
520 {
521 /* attr->pshared = pshared */
522 return (ESUCCESS);
523 } else
524 {
525 return (EINVAL); /* Invalid parameter */
526 }
527 } else
528 {
529 return (EINVAL); /* Not an initialized 'attribute' structure */
530 }
531
532 }
533
534 #else /* !BUILDING_VARIANT */
535 extern int _pthread_cond_wait(pthread_cond_t *cond,
536 pthread_mutex_t *mutex,
537 const struct timespec *abstime,
538 int isRelative,
539 int isconforming);
540
541 #endif /* !BUILDING_VARIANT ] */
542
543 int
544 pthread_cond_wait(pthread_cond_t *cond,
545 pthread_mutex_t *mutex)
546 {
547 int conforming;
548 #if __DARWIN_UNIX03
549
550 if (__unix_conforming == 0)
551 __unix_conforming = 1;
552
553 conforming = 1;
554 #else /* __DARWIN_UNIX03 */
555 conforming = 0;
556 #endif /* __DARWIN_UNIX03 */
557 return (_pthread_cond_wait(cond, mutex, (struct timespec *)NULL, 0, conforming));
558 }
559
560 int
561 pthread_cond_timedwait(pthread_cond_t *cond,
562 pthread_mutex_t *mutex,
563 const struct timespec *abstime)
564 {
565 int conforming;
566 #if __DARWIN_UNIX03
567 if (__unix_conforming == 0)
568 __unix_conforming = 1;
569
570 conforming = 1;
571 #else /* __DARWIN_UNIX03 */
572 conforming = 0;
573 #endif /* __DARWIN_UNIX03 */
574
575 return (_pthread_cond_wait(cond, mutex, abstime, 0, conforming));
576 }
577