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 not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include "internals.h"
29 pthread_barrier_wait(pthread_barrier_t *barrier)
31 pthread_descr self = thread_self();
32 pthread_descr temp_wake_queue, th;
35 __pthread_lock(&barrier->__ba_lock, self);
37 /* If the required number of threads have achieved rendezvous... */
38 if (barrier->__ba_present >= barrier->__ba_required - 1)
40 /* ... then this last caller shall be the serial thread */
41 result = PTHREAD_BARRIER_SERIAL_THREAD;
42 /* Copy and clear wait queue and reset barrier. */
43 temp_wake_queue = barrier->__ba_waiting;
44 barrier->__ba_waiting = NULL;
45 barrier->__ba_present = 0;
50 barrier->__ba_present++;
51 enqueue(&barrier->__ba_waiting, self);
54 __pthread_unlock(&barrier->__ba_lock);
58 /* Non-serial threads have to suspend */
60 /* We don't bother dealing with cancellation because the POSIX
61 spec for barriers doesn't mention that pthread_barrier_wait
62 is a cancellation point. */
66 /* Serial thread wakes up all others. */
67 while ((th = dequeue(&temp_wake_queue)) != NULL)
75 pthread_barrier_init(pthread_barrier_t *barrier,
76 const pthread_barrierattr_t *attr,
82 __pthread_init_lock(&barrier->__ba_lock);
83 barrier->__ba_required = count;
84 barrier->__ba_present = 0;
85 barrier->__ba_waiting = NULL;
90 pthread_barrier_destroy(pthread_barrier_t *barrier)
92 if (barrier->__ba_waiting != NULL) return EBUSY;
97 pthread_barrierattr_init(pthread_barrierattr_t *attr)
99 attr->__pshared = PTHREAD_PROCESS_PRIVATE;
104 pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
110 __pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
113 *pshared = PTHREAD_PROCESS_PRIVATE;
118 pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
120 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
123 /* For now it is not possible to shared a conditional variable. */
124 if (pshared != PTHREAD_PROCESS_PRIVATE)