(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / barrier.c
blob37d997cfc163a11cdb56aec11b184c02f3fb0a0f
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. */
21 #include <errno.h>
22 #include "pthread.h"
23 #include "internals.h"
24 #include "spinlock.h"
25 #include "queue.h"
26 #include "restart.h"
28 int
29 pthread_barrier_wait(pthread_barrier_t *barrier)
31 pthread_descr self = thread_self();
32 pthread_descr temp_wake_queue, th;
33 int result = 0;
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;
47 else
49 result = 0;
50 barrier->__ba_present++;
51 enqueue(&barrier->__ba_waiting, self);
54 __pthread_unlock(&barrier->__ba_lock);
56 if (result == 0)
58 /* Non-serial threads have to suspend */
59 suspend(self);
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. */
64 else
66 /* Serial thread wakes up all others. */
67 while ((th = dequeue(&temp_wake_queue)) != NULL)
68 restart(th);
71 return result;
74 int
75 pthread_barrier_init(pthread_barrier_t *barrier,
76 const pthread_barrierattr_t *attr,
77 unsigned int count)
79 if (count == 0)
80 return EINVAL;
82 __pthread_init_lock(&barrier->__ba_lock);
83 barrier->__ba_required = count;
84 barrier->__ba_present = 0;
85 barrier->__ba_waiting = NULL;
86 return 0;
89 int
90 pthread_barrier_destroy(pthread_barrier_t *barrier)
92 if (barrier->__ba_waiting != NULL) return EBUSY;
93 return 0;
96 int
97 pthread_barrierattr_init(pthread_barrierattr_t *attr)
99 attr->__pshared = PTHREAD_PROCESS_PRIVATE;
100 return 0;
104 pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
106 return 0;
110 __pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
111 int *pshared)
113 *pshared = PTHREAD_PROCESS_PRIVATE;
114 return 0;
118 pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
120 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
121 return EINVAL;
123 /* For now it is not possible to shared a conditional variable. */
124 if (pshared != PTHREAD_PROCESS_PRIVATE)
125 return ENOSYS;
127 return 0;