]> sourceware.org Git - glibc.git/blob - linuxthreads/barrier.c
Update.
[glibc.git] / linuxthreads / barrier.c
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.
5
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.
10
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.
15
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. */
20
21 #include <errno.h>
22 #include "pthread.h"
23 #include "internals.h"
24 #include "spinlock.h"
25 #include "queue.h"
26 #include "restart.h"
27
28 int
29 pthread_barrier_wait(pthread_barrier_t *barrier)
30 {
31 pthread_descr self = thread_self();
32 pthread_descr temp_wake_queue, th;
33 int result = 0;
34
35 __pthread_lock(&barrier->__ba_lock, self);
36
37 /* If the required number of threads have achieved rendezvous... */
38 if (barrier->__ba_present >= barrier->__ba_required - 1)
39 {
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;
46 }
47 else
48 {
49 result = 0;
50 barrier->__ba_present++;
51 enqueue(&barrier->__ba_waiting, self);
52 }
53
54 __pthread_unlock(&barrier->__ba_lock);
55
56 if (result == 0)
57 {
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. */
63 }
64 else
65 {
66 /* Serial thread wakes up all others. */
67 while ((th = dequeue(&temp_wake_queue)) != NULL)
68 restart(th);
69 }
70
71 return result;
72 }
73
74 int
75 pthread_barrier_init(pthread_barrier_t *barrier,
76 const pthread_barrierattr_t *attr,
77 unsigned int count)
78 {
79 if (count == 0)
80 return EINVAL;
81
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;
87 }
88
89 int
90 pthread_barrier_destroy(pthread_barrier_t *barrier)
91 {
92 if (barrier->__ba_waiting != NULL) return EBUSY;
93 return 0;
94 }
95
96 int
97 pthread_barrierattr_init(pthread_barrierattr_t *attr)
98 {
99 attr->__pshared = PTHREAD_PROCESS_PRIVATE;
100 return 0;
101 }
102
103 int
104 pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
105 {
106 return 0;
107 }
108
109 int
110 __pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
111 int *pshared)
112 {
113 *pshared = PTHREAD_PROCESS_PRIVATE;
114 return 0;
115 }
116
117 int
118 pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
119 {
120 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
121 return EINVAL;
122
123 /* For now it is not possible to shared a conditional variable. */
124 if (pshared != PTHREAD_PROCESS_PRIVATE)
125 return ENOSYS;
126
127 return 0;
128 }
This page took 0.041144 seconds and 5 git commands to generate.