OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libpthread / nptl / sysdeps / pthread / pthread_barrier_init.c
1 /* Copyright (C) 2002, 2007 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include "pthreadP.h"
21 #include <lowlevellock.h>
22 #include <bits/kernel-features.h>
23
24
25 static const struct pthread_barrierattr default_attr =
26   {
27     .pshared = PTHREAD_PROCESS_PRIVATE
28   };
29
30
31 int
32 pthread_barrier_init (
33      pthread_barrier_t *barrier,
34      const pthread_barrierattr_t *attr,
35      unsigned int count)
36 {
37   struct pthread_barrier *ibarrier;
38
39   if (__builtin_expect (count == 0, 0))
40     return EINVAL;
41
42   const struct pthread_barrierattr *iattr
43     = (attr != NULL
44        ? iattr = (struct pthread_barrierattr *) attr
45        : &default_attr);
46
47   if (iattr->pshared != PTHREAD_PROCESS_PRIVATE
48       && __builtin_expect (iattr->pshared != PTHREAD_PROCESS_SHARED, 0))
49     /* Invalid attribute.  */
50     return EINVAL;
51
52   ibarrier = (struct pthread_barrier *) barrier;
53
54   /* Initialize the individual fields.  */
55   ibarrier->lock = LLL_LOCK_INITIALIZER;
56   ibarrier->left = count;
57   ibarrier->init_count = count;
58   ibarrier->curr_event = 0;
59
60 #ifdef __ASSUME_PRIVATE_FUTEX
61   ibarrier->private = (iattr->pshared != PTHREAD_PROCESS_PRIVATE
62                        ? 0 : FUTEX_PRIVATE_FLAG);
63 #else
64   ibarrier->private = (iattr->pshared != PTHREAD_PROCESS_PRIVATE
65                        ? 0 : THREAD_GETMEM (THREAD_SELF,
66                                             header.private_futex));
67 #endif
68
69   return 0;
70 }