Back to index

glibc  2.9
pthread_barrier_wait.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <sysdep.h>
00022 #include <lowlevellock.h>
00023 #include <pthreadP.h>
00024 
00025 /* Wait on barrier.  */
00026 int
00027 pthread_barrier_wait (barrier)
00028      pthread_barrier_t *barrier;
00029 {
00030   union sparc_pthread_barrier *ibarrier
00031     = (union sparc_pthread_barrier *) barrier;
00032   int result = 0;
00033   int private = ibarrier->s.pshared ? LLL_SHARED : LLL_PRIVATE;
00034 
00035   /* Make sure we are alone.  */
00036   lll_lock (ibarrier->b.lock, private);
00037 
00038   /* One more arrival.  */
00039   --ibarrier->b.left;
00040 
00041   /* Are these all?  */
00042   if (ibarrier->b.left == 0)
00043     {
00044       /* Yes. Increment the event counter to avoid invalid wake-ups and
00045         tell the current waiters that it is their turn.  */
00046       ++ibarrier->b.curr_event;
00047 
00048       /* Wake up everybody.  */
00049       lll_futex_wake (&ibarrier->b.curr_event, INT_MAX, private);
00050 
00051       /* This is the thread which finished the serialization.  */
00052       result = PTHREAD_BARRIER_SERIAL_THREAD;
00053     }
00054   else
00055     {
00056       /* The number of the event we are waiting for.  The barrier's event
00057         number must be bumped before we continue.  */
00058       unsigned int event = ibarrier->b.curr_event;
00059 
00060       /* Before suspending, make the barrier available to others.  */
00061       lll_unlock (ibarrier->b.lock, private);
00062 
00063       /* Wait for the event counter of the barrier to change.  */
00064       do
00065        lll_futex_wait (&ibarrier->b.curr_event, event, private);
00066       while (event == ibarrier->b.curr_event);
00067     }
00068 
00069   /* Make sure the init_count is stored locally or in a register.  */
00070   unsigned int init_count = ibarrier->b.init_count;
00071 
00072   /* If this was the last woken thread, unlock.  */
00073   if (atomic_increment_val (&ibarrier->b.left) == init_count)
00074     /* We are done.  */
00075     lll_unlock (ibarrier->b.lock, private);
00076 
00077   return result;
00078 }