From 697568d1b48a497024bca234483a157cee79c7a2 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Wed, 21 Jun 2000 04:35:29 +0000 Subject: Update. 2000-06-20 Jes Sorensen * sysdeps/unix/sysv/linux/ia64/syscalls.list: New file. 2000-06-20 Jes Sorensen * sysdeps/unix/sysv/linux/ia64/machine-gmon.h: New file. * sysdeps/unix/sysv/linux/ia64/profil-counter.h: New file. 2000-06-20 David Mosberger-Tang * sysdeps/unix/sysv/linux/ia64/getpagesize.c: New file. 2000-06-20 Jes Sorensen * sysdeps/unix/sysv/linux/ia64/sys/ucontext.h: Fix typo. --- linuxthreads/ChangeLog | 13 +++ linuxthreads/Examples/ex9.c | 94 ++++++++++++++++ linuxthreads/Makefile | 5 +- linuxthreads/Versions | 3 + linuxthreads/barrier.c | 125 +++++++++++++++++++++ linuxthreads/sysdeps/pthread/bits/pthreadtypes.h | 14 +++ linuxthreads/sysdeps/pthread/pthread.h | 30 ++++- linuxthreads/sysdeps/pthread/timer_create.c | 16 +-- linuxthreads/sysdeps/pthread/timer_delete.c | 15 +-- .../sysdeps/unix/sysv/linux/bits/posix_opt.h | 3 + .../sysdeps/unix/sysv/linux/i386/bits/posix_opt.h | 3 + 11 files changed, 303 insertions(+), 18 deletions(-) create mode 100644 linuxthreads/Examples/ex9.c create mode 100644 linuxthreads/barrier.c (limited to 'linuxthreads') diff --git a/linuxthreads/ChangeLog b/linuxthreads/ChangeLog index b79ee3f43e..965ac05e31 100644 --- a/linuxthreads/ChangeLog +++ b/linuxthreads/ChangeLog @@ -1,3 +1,16 @@ +2000-06-20 Ulrich Drepper + + * sysdeps/unix/sysv/linux/bits/posix_opt.h: Define _POSIX_BARRIERS. + * sysdeps/unix/sysv/linux/i386/bits/posix_opt.h: Likewise. + + * Makefile (libpthread-routines): Add barrier. + (tests): Add ex9. Add rule to build ex9. + * Versions: Export barrier functions. + * barrier.c: New file. + * Examples/ex9.c: New file. + * sysdeps/pthread/pthread.h: Add barrier data types and declarations. + * sysdeps/pthread/bits/pthreadtypes.h: Likewise. + 2000-06-19 H.J. Lu * spinlock.h (HAS_COMPARE_AND_SWAP): Defined if diff --git a/linuxthreads/Examples/ex9.c b/linuxthreads/Examples/ex9.c new file mode 100644 index 0000000000..0c08741003 --- /dev/null +++ b/linuxthreads/Examples/ex9.c @@ -0,0 +1,94 @@ +/* Tests for pthread_barrier_* functions. + Copyright (C) 2000 Free Software Foundation, Inc. + Contributed by Kaz Kylheku , 2000. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library 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 +#include +#include +#include +#include + +#define NUM_THREADS 10 +#define NUM_ITERS 500 + +static void *thread (void *); +static pthread_barrier_t barrier; + +int +main (void) +{ + pthread_t th; + int i; + + if (pthread_barrier_init (&barrier, NULL, NUM_THREADS + 1) != 0) + error (EXIT_FAILURE, 0, "cannot initialize barrier"); + + for (i = 0; i < NUM_THREADS; i++) + { + if (pthread_create (&th, NULL, thread, NULL) != 0) + error (EXIT_FAILURE, 0, "cannot create thread"); + } + + (void) thread (NULL); + /* notreached */ + return 0; +} + + +static void * +thread (void *arg) +{ + int i; + pthread_t self = pthread_self (); + static pthread_t last_serial_thread; + static int linecount; /* protected by flockfile(stdout) */ + + for (i = 0; i < NUM_ITERS; i++) + { + switch (pthread_barrier_wait (&barrier)) + { + case 0: + flockfile (stdout); + printf ("%04d: non-serial thread %lu\n", ++linecount, + (unsigned long) self); + funlockfile (stdout); + break; + case PTHREAD_BARRIER_SERIAL_THREAD: + flockfile (stdout); + printf ("%04d: serial thread %lu\n", ++linecount, + (unsigned long) self); + funlockfile (stdout); + last_serial_thread = self; + break; + default: + /* Huh? */ + error (EXIT_FAILURE, 0, "unexpected return value from barrier wait"); + } + } + + if (pthread_equal (self, last_serial_thread)) + { + flockfile (stdout); + printf ("%04d: last serial thread %lu terminating process\n", + ++linecount, (unsigned long) self); + funlockfile (stdout); + exit (0); + } + + pthread_exit(NULL); +} diff --git a/linuxthreads/Makefile b/linuxthreads/Makefile index 6e9f8141bd..1b4ecc0012 100644 --- a/linuxthreads/Makefile +++ b/linuxthreads/Makefile @@ -35,10 +35,10 @@ extra-libs-others := $(extra-libs) libpthread-routines := attr cancel condvar join manager mutex ptfork \ ptlongjmp pthread signals specific errno lockfile \ semaphore spinlock wrapsyscall rwlock pt-machine \ - oldsemaphore events getcpuclockid pspinlock + oldsemaphore events getcpuclockid pspinlock barrier vpath %.c Examples -tests = ex1 ex2 ex3 ex4 ex5 ex6 ex7 ex8 joinrace +tests = ex1 ex2 ex3 ex4 ex5 ex6 ex7 ex8 ex9 joinrace include ../Rules @@ -68,4 +68,5 @@ $(objpfx)ex5: $(libpthread) $(objpfx)ex6: $(libpthread) $(objpfx)ex7: $(libpthread) $(objpfx)ex8: $(libpthread) +$(objpfx)ex9: $(libpthread) $(objpfx)joinrace: $(libpthread) diff --git a/linuxthreads/Versions b/linuxthreads/Versions index 327e62ea0c..48f62ae0dd 100644 --- a/linuxthreads/Versions +++ b/linuxthreads/Versions @@ -135,6 +135,9 @@ libpthread { pthread_spin_destroy; pthread_spin_init; pthread_spin_lock; pthread_spin_trylock; pthread_spin_unlock; pthread_getcpuclockid; + pthread_barrier_destroy; pthread_barrier_init; pthread_barrier_wait; + pthread_barrierattr_destroy; pthread_barrierattr_init; + pthread_barrierattr_getpshared; pthread_barrierattr_setpshared; # Extensions. pthread_yield; diff --git a/linuxthreads/barrier.c b/linuxthreads/barrier.c new file mode 100644 index 0000000000..1293bad858 --- /dev/null +++ b/linuxthreads/barrier.c @@ -0,0 +1,125 @@ +/* POSIX barrier implementation for LinuxThreads. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Kaz Kylheku , 2000. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library 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 +#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 = attr->__pshared; + return 0; +} + +int +pthread_barrierattr_setpshared(pthread_barrierattr_t *attr, int pshared) +{ + if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED) + return EINVAL; + + attr->__pshared = pshared; + return 0; +} diff --git a/linuxthreads/sysdeps/pthread/bits/pthreadtypes.h b/linuxthreads/sysdeps/pthread/bits/pthreadtypes.h index 99e56d23ee..a53889f5f0 100644 --- a/linuxthreads/sysdeps/pthread/bits/pthreadtypes.h +++ b/linuxthreads/sysdeps/pthread/bits/pthreadtypes.h @@ -118,6 +118,20 @@ typedef struct #ifdef __USE_XOPEN2K /* POSIX spinlock data type. */ typedef volatile int pthread_spinlock_t; + +/* POSIX barrier. */ +typedef struct { + struct _pthread_fastlock __ba_lock; /* Lock to guarantee mutual exclusion */ + int __ba_required; /* Threads needed for completion */ + int __ba_present; /* Threads waiting */ + _pthread_descr __ba_waiting; /* Queue of waiting threads */ +} pthread_barrier_t; + +/* barrier attribute */ +typedef struct { + int __pshared; +} pthread_barrierattr_t; + #endif diff --git a/linuxthreads/sysdeps/pthread/pthread.h b/linuxthreads/sysdeps/pthread/pthread.h index e82c4deb99..da39e7a923 100644 --- a/linuxthreads/sysdeps/pthread/pthread.h +++ b/linuxthreads/sysdeps/pthread/pthread.h @@ -109,6 +109,13 @@ enum #define PTHREAD_ONCE_INIT 0 +/* Special constants */ + +#ifdef __USE_XOPEN2K +/* -1 is distinct from 0 and all errno constants */ +# define PTHREAD_BARRIER_SERIAL_THREAD -1 +#endif + /* Cleanup buffers */ struct _pthread_cleanup_buffer @@ -414,7 +421,7 @@ extern int pthread_rwlockattr_setkind_np (pthread_rwlockattr_t *__attr, #endif #ifdef __USE_XOPEN2K -/* The IEEE Std. 10003.1j-2000 introduces functions to implement +/* The IEEE Std. 1003.1j-2000 introduces functions to implement spinlocks. */ /* Initialize the spinlock LOCK. If PSHARED is nonzero the spinlock can @@ -433,6 +440,27 @@ extern int pthread_spin_trylock (pthread_spinlock_t *__lock) __THROW; /* Release spinlock LOCK. */ extern int pthread_spin_unlock (pthread_spinlock_t *__lock) __THROW; + + +/* Barriers are a also a new feature in 1003.1j-2000. */ + +extern int pthread_barrier_init (pthread_barrier_t *__barrier, + __const pthread_barrierattr_t *__attr, + unsigned int __count) __THROW; + +extern int pthread_barrier_destroy (pthread_barrier_t *__barrier) __THROW; + +extern int pthread_barrierattr_init (pthread_barrierattr_t *__attr) __THROW; + +extern int pthread_barrierattr_destroy (pthread_barrierattr_t *__attr) __THROW; + +extern int pthread_barrierattr_getpshared (__const pthread_barrierattr_t *__attr, + int *__pshared) __THROW; + +extern int pthread_barrierattr_setpshared (pthread_barrierattr_t *__attr, + int __pshared) __THROW; + +extern int pthread_barrier_wait (pthread_barrier_t *__barrier) __THROW; #endif diff --git a/linuxthreads/sysdeps/pthread/timer_create.c b/linuxthreads/sysdeps/pthread/timer_create.c index cbefb91831..1dccd30361 100644 --- a/linuxthreads/sysdeps/pthread/timer_create.c +++ b/linuxthreads/sysdeps/pthread/timer_create.c @@ -46,7 +46,7 @@ timer_create (clock_id, evp, timerid) #endif ) { - errno = EINVAL; + __set_errno (EINVAL); return -1; } @@ -54,7 +54,7 @@ timer_create (clock_id, evp, timerid) if (__timer_init_failed) { - errno = ENOMEM; + __set_errno (ENOMEM); return -1; } @@ -63,7 +63,7 @@ timer_create (clock_id, evp, timerid) newtimer = __timer_alloc (); if (__builtin_expect (newtimer == NULL, 0)) { - errno = EAGAIN; + __set_errno (EAGAIN); goto unlock_bail; } @@ -106,13 +106,13 @@ timer_create (clock_id, evp, timerid) break; #endif } - + if (! thread->exists) { if (__builtin_expect (__timer_thread_start (thread), 1) < 0) { - errno = EAGAIN; + __set_errno (EAGAIN); goto unlock_bail; } } @@ -138,7 +138,7 @@ timer_create (clock_id, evp, timerid) /* Out of luck; no threads are available. */ if (__builtin_expect (thread == NULL, 0)) { - errno = EAGAIN; + __set_errno (EAGAIN); goto unlock_bail; } @@ -146,13 +146,13 @@ timer_create (clock_id, evp, timerid) if (! thread->exists && __builtin_expect (! __timer_thread_start (thread), 0)) { - errno = EAGAIN; + __set_errno (EAGAIN); goto unlock_bail; } break; default: - errno = EINVAL; + __set_errno (EINVAL); goto unlock_bail; } diff --git a/linuxthreads/sysdeps/pthread/timer_delete.c b/linuxthreads/sysdeps/pthread/timer_delete.c index 4636bf707a..b7d59fe68d 100644 --- a/linuxthreads/sysdeps/pthread/timer_delete.c +++ b/linuxthreads/sysdeps/pthread/timer_delete.c @@ -36,9 +36,9 @@ timer_delete (timerid) pthread_mutex_lock (&__timer_mutex); timer = timer_id2ptr (timerid); - if (timer == NULL || !timer->inuse) + if (! timer_valid (timer)) /* Invalid timer ID or the timer is not in use. */ - errno = EINVAL; + __set_errno (EINVAL); else { if (timer->armed) @@ -50,16 +50,17 @@ timer_delete (timerid) the mutex is unlocked and timer_delete is aborted. */ pthread_cleanup_push (__timer_mutex_cancel_handler, &__timer_mutex); - /* If timer is currently being serviced, wait for it to finish. */ - while (thread->current_timer == timer) - pthread_cond_wait (&thread->cond, &__timer_mutex); + /* If timer is currently being serviced, wait for it to finish. */ + while (thread->current_timer == timer) + pthread_cond_wait (&thread->cond, &__timer_mutex); - pthread_cleanup_pop (0); + pthread_cleanup_pop (0); } /* Remove timer from whatever queue it may be on and deallocate it. */ + timer->inuse = TIMER_DELETED; list_unlink_ip (&timer->links); - __timer_dealloc (timer); + timer_delref (timer); retval = 0; } diff --git a/linuxthreads/sysdeps/unix/sysv/linux/bits/posix_opt.h b/linuxthreads/sysdeps/unix/sysv/linux/bits/posix_opt.h index 1fde1010b0..a1b5f04b6a 100644 --- a/linuxthreads/sysdeps/unix/sysv/linux/bits/posix_opt.h +++ b/linuxthreads/sysdeps/unix/sysv/linux/bits/posix_opt.h @@ -131,4 +131,7 @@ /* We have POSIX timers. */ #define _POSIX_TIMERS 1 +/* The barrier functions are available. */ +#define _POSIX_BARRIERS 200912L + #endif /* posix_opt.h */ diff --git a/linuxthreads/sysdeps/unix/sysv/linux/i386/bits/posix_opt.h b/linuxthreads/sysdeps/unix/sysv/linux/i386/bits/posix_opt.h index d31206bc72..19acebb8c9 100644 --- a/linuxthreads/sysdeps/unix/sysv/linux/i386/bits/posix_opt.h +++ b/linuxthreads/sysdeps/unix/sysv/linux/i386/bits/posix_opt.h @@ -140,4 +140,7 @@ /* We have POSIX timers. */ #define _POSIX_TIMERS 1 +/* The barrier functions are available. */ +#define _POSIX_BARRIERS 200912L + #endif /* posix_opt.h */ -- cgit v1.2.3