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