1 /* POSIX barrier implementation for LinuxThreads.
2 Copyright (C) 2000, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Kaz Kylheku <kaz@ashi.footprints.net>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, see <http://www.gnu.org/licenses/>. */
22 #include "internals.h"
28 pthread_barrier_wait(pthread_barrier_t *barrier)
30 pthread_descr self = thread_self();
31 pthread_descr temp_wake_queue, th;
34 __pthread_lock(&barrier->__ba_lock, self);
36 /* If the required number of threads have achieved rendezvous... */
37 if (barrier->__ba_present >= barrier->__ba_required - 1)
39 /* ... then this last caller shall be the serial thread */
40 result = PTHREAD_BARRIER_SERIAL_THREAD;
41 /* Copy and clear wait queue and reset barrier. */
42 temp_wake_queue = barrier->__ba_waiting;
43 barrier->__ba_waiting = NULL;
44 barrier->__ba_present = 0;
49 barrier->__ba_present++;
50 enqueue(&barrier->__ba_waiting, self);
53 __pthread_unlock(&barrier->__ba_lock);
57 /* Non-serial threads have to suspend */
59 /* We don't bother dealing with cancellation because the POSIX
60 spec for barriers doesn't mention that pthread_barrier_wait
61 is a cancellation point. */
65 /* Serial thread wakes up all others. */
66 while ((th = dequeue(&temp_wake_queue)) != NULL)
74 pthread_barrier_init(pthread_barrier_t *barrier,
75 const pthread_barrierattr_t *attr,
81 __pthread_init_lock(&barrier->__ba_lock);
82 barrier->__ba_required = count;
83 barrier->__ba_present = 0;
84 barrier->__ba_waiting = NULL;
89 pthread_barrier_destroy(pthread_barrier_t *barrier)
91 if (barrier->__ba_waiting != NULL) return EBUSY;
96 pthread_barrierattr_init(pthread_barrierattr_t *attr)
98 attr->__pshared = PTHREAD_PROCESS_PRIVATE;
103 pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
109 __pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
112 *pshared = PTHREAD_PROCESS_PRIVATE;
117 pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
119 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
122 /* For now it is not possible to shared a conditional variable. */
123 if (pshared != PTHREAD_PROCESS_PRIVATE)