Back to index

glibc  2.9
barrier.c
Go to the documentation of this file.
00001 /* POSIX barrier implementation for LinuxThreads.
00002    Copyright (C) 2000, 2003 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Kaz Kylheku <kaz@ashi.footprints.net>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public License as
00008    published by the Free Software Foundation; either version 2.1 of the
00009    License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00018    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.  */
00020 
00021 #include <errno.h>
00022 #include "pthread.h"
00023 #include "internals.h"
00024 #include "spinlock.h"
00025 #include "queue.h"
00026 #include "restart.h"
00027 
00028 int
00029 pthread_barrier_wait(pthread_barrier_t *barrier)
00030 {
00031   pthread_descr self = thread_self();
00032   pthread_descr temp_wake_queue, th;
00033   int result = 0;
00034 
00035   __pthread_lock(&barrier->__ba_lock, self);
00036 
00037   /* If the required number of threads have achieved rendezvous... */
00038   if (barrier->__ba_present >= barrier->__ba_required - 1)
00039     {
00040       /* ... then this last caller shall be the serial thread */
00041       result = PTHREAD_BARRIER_SERIAL_THREAD;
00042       /* Copy and clear wait queue and reset barrier. */
00043       temp_wake_queue = barrier->__ba_waiting;
00044       barrier->__ba_waiting = NULL;
00045       barrier->__ba_present = 0;
00046     }
00047   else
00048     {
00049       result = 0;
00050       barrier->__ba_present++;
00051       enqueue(&barrier->__ba_waiting, self);
00052     }
00053 
00054   __pthread_unlock(&barrier->__ba_lock);
00055 
00056   if (result == 0)
00057     {
00058       /* Non-serial threads have to suspend */
00059       suspend(self);
00060       /* We don't bother dealing with cancellation because the POSIX
00061          spec for barriers doesn't mention that pthread_barrier_wait
00062          is a cancellation point. */
00063     }
00064   else
00065     {
00066       /* Serial thread wakes up all others. */
00067       while ((th = dequeue(&temp_wake_queue)) != NULL)
00068        restart(th);
00069     }
00070 
00071   return result;
00072 }
00073 
00074 int
00075 pthread_barrier_init(pthread_barrier_t *barrier,
00076                             const pthread_barrierattr_t *attr,
00077                             unsigned int count)
00078 {
00079   if (count == 0)
00080      return EINVAL;
00081 
00082   __pthread_init_lock(&barrier->__ba_lock);
00083   barrier->__ba_required = count;
00084   barrier->__ba_present = 0;
00085   barrier->__ba_waiting = NULL;
00086   return 0;
00087 }
00088 
00089 int
00090 pthread_barrier_destroy(pthread_barrier_t *barrier)
00091 {
00092   if (barrier->__ba_waiting != NULL) return EBUSY;
00093   return 0;
00094 }
00095 
00096 int
00097 pthread_barrierattr_init(pthread_barrierattr_t *attr)
00098 {
00099   attr->__pshared = PTHREAD_PROCESS_PRIVATE;
00100   return 0;
00101 }
00102 
00103 int
00104 pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
00105 {
00106   return 0;
00107 }
00108 
00109 int
00110 __pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
00111                              int *pshared)
00112 {
00113   *pshared = PTHREAD_PROCESS_PRIVATE;
00114   return 0;
00115 }
00116 
00117 int
00118 pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
00119 {
00120   if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
00121     return EINVAL;
00122 
00123   /* For now it is not possible to shared a conditional variable.  */
00124   if (pshared != PTHREAD_PROCESS_PRIVATE)
00125     return ENOSYS;
00126 
00127   return 0;
00128 }