1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
/* POSIX barrier implementation for LinuxThreads.
Copyright (C) 2000, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Kaz Kylheku <kaz@ashi.footprints.net>, 2000.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; see the file COPYING.LIB. If not,
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <errno.h>
#include "pthread.h"
#include "internals.h"
#include "spinlock.h"
#include "queue.h"
#include "restart.h"
int
pthread_barrier_wait(pthread_barrier_t *barrier)
{
pthread_descr self = thread_self();
pthread_descr temp_wake_queue, th;
int result = 0;
__pthread_lock(&barrier->__ba_lock, self);
/* If the required number of threads have achieved rendezvous... */
if (barrier->__ba_present >= barrier->__ba_required - 1)
{
/* ... then this last caller shall be the serial thread */
result = PTHREAD_BARRIER_SERIAL_THREAD;
/* Copy and clear wait queue and reset barrier. */
temp_wake_queue = barrier->__ba_waiting;
barrier->__ba_waiting = NULL;
barrier->__ba_present = 0;
}
else
{
result = 0;
barrier->__ba_present++;
enqueue(&barrier->__ba_waiting, self);
}
__pthread_unlock(&barrier->__ba_lock);
if (result == 0)
{
/* Non-serial threads have to suspend */
suspend(self);
/* We don't bother dealing with cancellation because the POSIX
spec for barriers doesn't mention that pthread_barrier_wait
is a cancellation point. */
}
else
{
/* Serial thread wakes up all others. */
while ((th = dequeue(&temp_wake_queue)) != NULL)
restart(th);
}
return result;
}
int
pthread_barrier_init(pthread_barrier_t *barrier,
const pthread_barrierattr_t *attr,
unsigned int count)
{
if (count == 0)
return EINVAL;
__pthread_init_lock(&barrier->__ba_lock);
barrier->__ba_required = count;
barrier->__ba_present = 0;
barrier->__ba_waiting = NULL;
return 0;
}
int
pthread_barrier_destroy(pthread_barrier_t *barrier)
{
if (barrier->__ba_waiting != NULL) return EBUSY;
return 0;
}
int
pthread_barrierattr_init(pthread_barrierattr_t *attr)
{
attr->__pshared = PTHREAD_PROCESS_PRIVATE;
return 0;
}
int
pthread_barrierattr_destroy(pthread_barrierattr_t *attr)
{
return 0;
}
int
__pthread_barrierattr_getpshared(const pthread_barrierattr_t *attr,
int *pshared)
{
*pshared = PTHREAD_PROCESS_PRIVATE;
return 0;
}
int
pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared)
{
if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
return EINVAL;
/* For now it is not possible to shared a conditional variable. */
if (pshared != PTHREAD_PROCESS_PRIVATE)
return ENOSYS;
return 0;
}
|