Back to index

glibc  2.9
pthread_barrier_wait.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004, 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 
00026 /* Wait on barrier.  */
00027 int
00028 pthread_barrier_wait (barrier)
00029      pthread_barrier_t *barrier;
00030 {
00031   struct pthread_barrier *ibarrier = (struct pthread_barrier *) barrier;
00032   int result = 0;
00033 
00034   /* Make sure we are alone.  */
00035   lll_lock (ibarrier->lock, ibarrier->private ^ FUTEX_PRIVATE_FLAG);
00036 
00037   /* One more arrival.  */
00038   --ibarrier->left;
00039 
00040   /* Are these all?  */
00041   if (ibarrier->left == 0)
00042     {
00043       /* Yes. Increment the event counter to avoid invalid wake-ups and
00044         tell the current waiters that it is their turn.  */
00045       ++ibarrier->curr_event;
00046 
00047       /* Wake up everybody.  */
00048       lll_futex_wake (&ibarrier->curr_event, INT_MAX,
00049                     ibarrier->private ^ FUTEX_PRIVATE_FLAG);
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->curr_event;
00059 
00060       /* Before suspending, make the barrier available to others.  */
00061       lll_unlock (ibarrier->lock, ibarrier->private ^ FUTEX_PRIVATE_FLAG);
00062 
00063       /* Wait for the event counter of the barrier to change.  */
00064       do
00065        lll_futex_wait (&ibarrier->curr_event, event,
00066                      ibarrier->private ^ FUTEX_PRIVATE_FLAG);
00067       while (event == ibarrier->curr_event);
00068     }
00069 
00070   /* Make sure the init_count is stored locally or in a register.  */
00071   unsigned int init_count = ibarrier->init_count;
00072 
00073   /* If this was the last woken thread, unlock.  */
00074   if (atomic_increment_val (&ibarrier->left) == init_count)
00075     /* We are done.  */
00076     lll_unlock (ibarrier->lock, ibarrier->private ^ FUTEX_PRIVATE_FLAG);
00077 
00078   return result;
00079 }