diff options
author | Neal H. Walfield <neal@gnu.org> | 2002-10-10 23:05:06 +0000 |
---|---|---|
committer | Neal H. Walfield <neal@gnu.org> | 2002-10-10 23:05:06 +0000 |
commit | 291996a60567617f17aeb5e96159290b1ba22340 (patch) | |
tree | 6ea49340f541fdfe5cdac36ba0949b8718fd1e0b |
Import libpthread.
190 files changed, 9364 insertions, 0 deletions
diff --git a/.cvsignore b/.cvsignore new file mode 100644 index 00000000..70845e08 --- /dev/null +++ b/.cvsignore @@ -0,0 +1 @@ +Makefile.in diff --git a/lockfile.c b/lockfile.c new file mode 100644 index 00000000..040cbfb4 --- /dev/null +++ b/lockfile.c @@ -0,0 +1,65 @@ +/* lockfile - Handle locking and unlocking of streams. Hurd cthreads version. + Copyright (C) 2000,01,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <cthreads.h> +#include <pthread.h> /* Must come before <stdio.h>! */ +#include <stdio.h> + +#ifdef _STDIO_USES_IOSTREAM + +void +_cthreads_flockfile (_IO_FILE *fp) +{ + _IO_lock_lock (*fp->_lock); +} + +void +_cthreads_funlockfile (_IO_FILE *fp) +{ + _IO_lock_unlock (*fp->_lock); +} + +int +_cthreads_ftrylockfile (_IO_FILE *fp) +{ + return __libc_lock_trylock_recursive (*fp->_lock); +} + +# undef _IO_flockfile +# undef _IO_funlockfile +# undef _IO_ftrylockfile +# undef flockfile +# undef funlockfile +# undef ftrylockfile + +void _IO_flockfile (_IO_FILE *) + __attribute__ ((alias ("_cthreads_flockfile"))); +void _IO_funlockfile (_IO_FILE *) + __attribute__ ((alias ("_cthreads_funlockfile"))); +int _IO_ftrylockfile (_IO_FILE *) + __attribute__ ((alias ("_cthreads_ftrylockfile"))); + +void flockfile (_IO_FILE *) + __attribute__ ((alias ("_cthreads_flockfile"))); +void funlockfile (_IO_FILE *) + __attribute__ ((alias ("_cthreads_funlockfile"))); +int ftrylockfile (_IO_FILE *) + __attribute__ ((alias ("_cthreads_ftrylockfile"))); + +#endif /* _STDIO_USES_IOSTREAM */ diff --git a/not-in-libc.h b/not-in-libc.h new file mode 100644 index 00000000..516d4d13 --- /dev/null +++ b/not-in-libc.h @@ -0,0 +1,11 @@ +#define __mach_port_insert_right mach_port_insert_right +#define __mach_port_allocate mach_port_allocate +#define __mig_init mig_init +#define __thread_create thread_create +#define __thread_get_state thread_get_state +#define __thread_resume thread_resume +#define __thread_set_state thread_set_state +#define __thread_terminate thread_terminate +#define __vm_allocate vm_allocate +#define __vm_deallocate vm_deallocate +#define __sched_yield sched_yield diff --git a/pthread/Versions b/pthread/Versions new file mode 100644 index 00000000..e4b4e8bf --- /dev/null +++ b/pthread/Versions @@ -0,0 +1,15 @@ +libc { + GLIBC_2.2 { + # XXX + __vm_deallocate; __mach_port_insert_right; __mach_reply_port; + __mig_init; __vm_allocate; + + # functions used in inline functions or macros + __pthread_spin_destroy; __pthread_spin_init; __pthread_spin_lock; + _pthread_spin_lock; __pthread_spin_trylock; __pthread_spin_unlock; + + # p* + pthread_spin_destroy; pthread_spin_init; pthread_spin_lock; + pthread_spin_trylock; pthread_spin_unlock; + } +} diff --git a/pthread/cthreads-compat.c b/pthread/cthreads-compat.c new file mode 100644 index 00000000..ccea03c9 --- /dev/null +++ b/pthread/cthreads-compat.c @@ -0,0 +1,100 @@ +/* Compatibility routines for cthreads. + Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <pthread.h> + +#include <cthreads.h> + +void +cthread_detach (cthread_t thread) +{ + int err; + + err = pthread_detach ((pthread_t) thread); + assert_perror (err); +} + +cthread_t +cthread_fork (cthread_fn_t func, void *arg) +{ + pthread_t thread; + int err; + + err = pthread_create (&thread, NULL, func, arg); + assert_perror (err); + + return (cthread_t) thread; +} + +int +cthread_keycreate (cthread_key_t *key) +{ + error_t err; + + err = pthread_key_create (key, 0); + if (err) + { + errno = err; + *key = CTHREAD_KEY_INVALID; + err = -1; + } + + return err; +} + +int +cthread_getspecific (cthread_key_t key, void **val) +{ + *val = pthread_getspecific (key); + return 0; +} + +void * +__libc_getspecific (cthread_key_t key) +{ + return pthread_getspecific (key); +} + +int +cthread_setspecific (cthread_key_t key, void *val) +{ + error_t err; + + err = pthread_setspecific (key, (const void *) val); + if (err) + { + errno = err; + err = -1; + } + + return err; +} + +void +__mutex_lock_solid (void *lock) +{ + __pthread_mutex_lock (lock); +} + +void +__mutex_unlock_solid (void *lock) +{ + __pthread_mutex_unlock (lock); +} diff --git a/pthread/pt-alloc.c b/pthread/pt-alloc.c new file mode 100644 index 00000000..5cdc85df --- /dev/null +++ b/pthread/pt-alloc.c @@ -0,0 +1,215 @@ +/* Allocate a new thread structure. + Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <pthread.h> +#include <stdlib.h> +#include <string.h> + +#include <pt-internal.h> + +#include <bits/atomic.h> + +/* This braindamage is necessary because the standard says that some + of the threads functions "shall fail" if "No thread could be found + corresponding to that specified by the given thread ID." */ + +/* Thread ID lookup table. */ +struct __pthread **__pthread_threads; + +/* The size of the thread ID lookup table. */ +int __pthread_max_threads; + +/* The total number of thread IDs currently in use, or on the list of + available thread IDs. */ +int __pthread_num_threads; + +/* A lock for the table, and the other variables above. */ +pthread_rwlock_t __pthread_threads_lock; + + +/* List of thread structures corresponding to free thread IDs. */ +__atomicptr_t __pthread_free_threads; + +static inline error_t +initialize_pthread (struct __pthread *new, int recycling) +{ + error_t err; + + err = __pthread_init_specific (new); + if (err) + return err; + + new->cancel_state = PTHREAD_CANCEL_ENABLE; + new->cancel_type = PTHREAD_CANCEL_DEFERRED; + new->cancel_pending = 0; + + if (recycling) + /* Since we are recycling PTHREAD, we can assume certains things + about PTHREAD's current state and save some cycles by not + rewriting the memory. */ + return 0; + + new->stack = 0; + + new->state_lock = PTHREAD_MUTEX_INITIALIZER; + new->state_cond = PTHREAD_COND_INITIALIZER; + + new->cancelation_handlers = 0; + + new->next = 0; + new->prevp = 0; + + return 0; +} + + +/* Allocate a new thread structure and its pthread thread ID (but not + a kernel thread). */ +int +__pthread_alloc (struct __pthread **pthread) +{ + error_t err; + + struct __pthread *new; + struct __pthread **threads; + struct __pthread **old_threads; + int max_threads; + int new_max_threads; + + /* Try to re-use a thread structure before creating a new one. */ + while ((new = (struct __pthread *)__pthread_free_threads)) + { + if (__atomicptr_compare_and_swap (&__pthread_free_threads, + new, new->next)) + { + /* Yes, we managed to get one. The thread number in the + thread structure still refers to the correct slot. */ + err = initialize_pthread (new, 1); + if (err) + /* An error occured, however, we cannot just free NEW as + there may be resources attached to it. We must return + it to the free list. */ + while (1) + { + new->next = (struct __pthread *)__pthread_free_threads; + if (__atomicptr_compare_and_swap (&__pthread_free_threads, + new->next, new)) + break; + } + + if (! err) + *pthread = new; + + return err; + } + } + + /* Allocate a new thread structure. */ + new = malloc (sizeof (struct __pthread)); + if (new == NULL) + return ENOMEM; + + err = initialize_pthread (new, 0); + if (err) + { + free (new); + return err; + } + + retry: + pthread_rwlock_wrlock (&__pthread_threads_lock); + + if (__pthread_num_threads < __pthread_max_threads) + { + /* We have a free slot. Use the slot number as + the thread ID for the new thread. */ + new->thread = __pthread_num_threads++; + __pthread_threads[new->thread] = NULL; + + pthread_rwlock_unlock (&__pthread_threads_lock); + + *pthread = new; + return 0; + } +#ifdef PTHREAD_THREADS_MAX + else if (__pthread_num_threads >= PTHREAD_THREADS_MAX) + { + /* We have reached the limit on the number of threads per process. */ + pthread_rwlock_unlock (&__pthread_threads_lock); + + free (new); + return EAGAIN; + } +#endif + + /* We are going to enlarge the threads table. Save its current + size. We're going to release the lock before doing the necessary + memory allocation, since that's a potentially blocking operation. */ + max_threads = __pthread_max_threads; + + pthread_rwlock_unlock (&__pthread_threads_lock); + + /* Allocate a new lookup table that's twice as large. */ + new_max_threads + = max_threads > 0 ? max_threads * 2 : _POSIX_THREAD_THREADS_MAX; + threads = malloc (new_max_threads * sizeof (struct __pthread *)); + if (threads == NULL) + { + free (new); + return ENOMEM; + } + + pthread_rwlock_wrlock (&__pthread_threads_lock); + + /* Check if nobody else has already enlarged the table. */ + if (max_threads != __pthread_max_threads) + { + /* Yep, they did. */ + pthread_rwlock_unlock (&__pthread_threads_lock); + + /* Free the newly allocated table and try again to allocate a slot. */ + free (threads); + goto retry; + } + + /* Copy over the contents of the old table. */ + memcpy (threads, __pthread_threads, + __pthread_max_threads * sizeof (struct __pthread *)); + + /* Save the location of the old table. We want to deallocate its + storage after we released the lock. */ + old_threads = __pthread_threads; + + /* Replace the table with the new one. */ + __pthread_max_threads = new_max_threads; + __pthread_threads = threads; + + /* And allocate ourselves one of the newly created slots. */ + new->thread = __pthread_num_threads++; + __pthread_threads[new->thread] = NULL; + + pthread_rwlock_unlock (&__pthread_threads_lock); + + free (old_threads); + + *pthread = new; + return 0; +} diff --git a/pthread/pt-cancel.c b/pthread/pt-cancel.c new file mode 100644 index 00000000..d19c557d --- /dev/null +++ b/pthread/pt-cancel.c @@ -0,0 +1,40 @@ +/* Cancel a thread. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +int +pthread_cancel (pthread_t t) +{ + int err = 0; + struct __pthread *p; + + p = __pthread_getid (t); + if (! p) + return ESRCH; + + p->cancel_pending = 1; + if (p->cancel_state == PTHREAD_CANCEL_ENABLE + && p->cancel_type == PTHREAD_CANCEL_ASYNCHRONOUS) + err = __pthread_do_cancel (p); + + return err; +} diff --git a/pthread/pt-cleanup.c b/pthread/pt-cleanup.c new file mode 100644 index 00000000..58865aa4 --- /dev/null +++ b/pthread/pt-cleanup.c @@ -0,0 +1,28 @@ +/* Add a cancelation handler to the stack. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +struct __pthread_cancelation_handler ** +__pthread_get_cleanup_stack (void) +{ + return &_pthread_self ()->cancelation_handlers; +} diff --git a/pthread/pt-create.c b/pthread/pt-create.c new file mode 100644 index 00000000..bbfc6606 --- /dev/null +++ b/pthread/pt-create.c @@ -0,0 +1,194 @@ +/* Thread creation. + Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <pthread.h> +#include <signal.h> + +#include <bits/atomic.h> + +#include <pt-internal.h> + +/* The total number of pthreads currently active. This is defined + here since it would be really stupid to have a threads-using + program that doesn't call `pthread_create'. */ +__atomic_t __pthread_total; + + +/* The entry-point for new threads. */ +static void +entry_point (void *(*start_routine)(void *), void *arg) +{ + pthread_exit (start_routine (arg)); +} + +/* Create a thread with attributes given by ATTR, executing + START_ROUTINE with argument ARG. */ +int +pthread_create (pthread_t *thread, const pthread_attr_t *attr, + void *(*start_routine)(void *), void *arg) +{ + int err; + struct __pthread *pthread; + + err = __pthread_create_internal (&pthread, attr, start_routine, arg); + if (! err) + *thread = pthread->thread; + + return err; +} + +/* Internal version of pthread_create. See comment in + pt-internal.h. */ +int +__pthread_create_internal (struct __pthread **thread, + const pthread_attr_t *attr, + void *(*start_routine)(void *), void *arg) +{ + int err; + struct __pthread *pthread; + const struct __pthread_attr *setup; + sigset_t sigset; + + /* Allocate a new thread structure. */ + err = __pthread_alloc (&pthread); + if (err) + goto failed; + + /* Use the default attributes if ATTR is NULL. */ + setup = attr ? attr : &__pthread_default_attr; + + /* Initialize the thread state. */ + pthread->state = (setup->detachstate == PTHREAD_CREATE_DETACHED + ? PTHREAD_DETACHED : PTHREAD_JOINABLE); + + /* If the user supplied a stack, it is not our responsibility to + setup a stack guard. */ + if (setup->stackaddr) + pthread->guardsize = 0; + else + pthread->guardsize = (setup->guardsize <= setup->stacksize + ? setup->guardsize : setup->stacksize); + + /* Find a stack. There are several scenarios: if a detached thread + kills itself, it has no way to deallocate its stack, thus it + leaves PTHREAD->stack set to true. We try to reuse it here, + however, if the user supplied a stack, we cannot use the old one. + Right now, we simply deallocate it. */ + if (pthread->stack) + { + if (setup->stackaddr != __pthread_default_attr.stackaddr) + { + __pthread_stack_dealloc (pthread->stackaddr, + pthread->stacksize); + pthread->stackaddr = setup->stackaddr; + pthread->stacksize = setup->stacksize; + } + } + else + { + err = __pthread_stack_alloc (&pthread->stackaddr, + setup->stacksize); + if (err) + goto failed_stack_alloc; + + pthread->stacksize = setup->stacksize; + pthread->stack = 1; + } + + /* Allocate the kernel thread and other required resources. */ + err = __pthread_thread_alloc (pthread); + if (err) + goto failed_thread_alloc; + + /* And initialize the rest of the machine context. This may include + additional machine- and system-specific initializations that + prove convenient. */ + err = __pthread_setup (pthread, entry_point, start_routine, arg); + if (err) + goto failed_setup; + + /* Initialize the system-specific signal state for the new + thread. */ + err = __pthread_sigstate_init (pthread); + if (err) + goto failed_sigstate; + + /* Set the new thread's signal mask and set the pending signals to + empty. POSIX says: "The signal mask shall be inherited from the + creating thread. The set of signals pending for the new thread + shall be empty." If the currnet thread is not a pthread then we + just inherit the process' sigmask. */ + if (__pthread_num_threads == 1) + err = sigprocmask (0, 0, &sigset); + else + err = __pthread_sigstate (_pthread_self (), 0, 0, &sigset, 0); + assert_perror (err); + + err = __pthread_sigstate (pthread, SIG_SETMASK, &sigset, 0, 1); + assert_perror (err); + + /* Increase the total number of threads. We do this before actually + starting the new thread, since the new thread might immediately + call `pthread_exit' which decreases the number of threads and + calls `exit' if the number of threads reaches zero. Increasing + the number of threads from within the new thread isn't an option + since this thread might return and call `pthread_exit' before the + new thread runs. */ + __atomic_inc (&__pthread_total); + + /* Store a pointer to this thread in the thread ID lookup table. We + could use __thread_setid, however, we only lock for reading as no + other thread should be using this entry (we also assume that the + store is atomic). */ + pthread_rwlock_rdlock (&__pthread_threads_lock); + __pthread_threads[pthread->thread] = pthread; + pthread_rwlock_unlock (&__pthread_threads_lock); + + /* At this point it is possible to guess our pthread ID. We have to + make sure that all functions taking a pthread_t argument can + handle the fact that this thread isn't really running yet. */ + + /* Schedule the new thread. */ + err = __pthread_thread_start (pthread); + if (err) + goto failed_starting; + + /* At this point the new thread is up and running. */ + + *thread = pthread; + + return 0; + + failed_starting: + __pthread_setid (pthread->thread, NULL); + __atomic_dec (&__pthread_total); + failed_sigstate: + __pthread_sigstate_destroy (pthread); + failed_setup: + __pthread_thread_halt (pthread); + failed_thread_alloc: + __pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize); + pthread->stack = 0; + failed_stack_alloc: + __pthread_dealloc (pthread); + failed: + return err; +} diff --git a/pthread/pt-dealloc.c b/pthread/pt-dealloc.c new file mode 100644 index 00000000..1fc7a7b3 --- /dev/null +++ b/pthread/pt-dealloc.c @@ -0,0 +1,63 @@ +/* Deallocate a thread structure. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <pthread.h> +#include <stdlib.h> + +#include <pt-internal.h> + +#include <bits/atomic.h> + +/* List of thread structures corresponding to free thread IDs. */ +extern __atomicptr_t __pthread_free_threads; + +/* Deallocate the thread structure for PTHREAD and the resources + associated with it. */ +void +__pthread_dealloc (struct __pthread *pthread) +{ + assert (pthread->state != PTHREAD_TERMINATED); + + /* Withdraw this thread from the thread ID lookup table. */ + __pthread_setid (pthread->thread, NULL); + + /* Mark the thread as terminated. We broadcast the condition + here to prevent pthread_join from waiting for this thread to + exit where it was never really started. Such a call to + pthread_join is completely bogus, but unfortunately allowed + by the standards. */ + __pthread_mutex_lock (&pthread->state_lock); + pthread->state = PTHREAD_TERMINATED; + if (pthread->state != PTHREAD_EXITED) + pthread_cond_broadcast (&pthread->state_cond); + __pthread_mutex_unlock (&pthread->state_lock); + + /* We do not actually deallocate the thread structure, but add it to + a list of re-usable thread structures. */ + while (1) + { + pthread->next = (struct __pthread *)__pthread_free_threads; + if (__atomicptr_compare_and_swap (&__pthread_free_threads, + pthread->next, pthread)) + return; + } + + /* NOTREACHED */ +} diff --git a/pthread/pt-detach.c b/pthread/pt-detach.c new file mode 100644 index 00000000..c22f6a03 --- /dev/null +++ b/pthread/pt-detach.c @@ -0,0 +1,84 @@ +/* Detach a thread. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <errno.h> +#include <pthread.h> +#include <stddef.h> + +#include <pt-internal.h> + +/* Indicate that the storage for THREAD can be reclaimed when it + terminates. */ +int +pthread_detach (pthread_t thread) +{ + struct __pthread *pthread; + int err = 0; + + /* Lookup the thread structure for THREAD. */ + pthread = __pthread_getid (thread); + if (pthread == NULL) + return ESRCH; + + __pthread_mutex_lock (&pthread->state_lock); + + switch (pthread->state) + { + case PTHREAD_JOINABLE: + /* THREAD still running. Mark it as detached such that its + resources can be reclaimed as soon as the thread exits. */ + pthread->state = PTHREAD_DETACHED; + + /* Broadcast the condition. This will make threads that are + waiting to join THREAD continue with hopefully disastrous + consequences instead of blocking indefinitely. */ + pthread_cond_broadcast (&pthread->state_cond); + __pthread_mutex_unlock (&pthread->state_lock); + break; + + case PTHREAD_EXITED: + /* THREAD has already exited. Make sure that nobody can + reference it anymore, and mark it as terminated. */ + + __pthread_mutex_unlock (&pthread->state_lock); + + __pthread_thread_halt (pthread); + + assert (pthread->stack); + __pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize); + pthread->stack = 0; + + __pthread_dealloc (pthread); + break; + + case PTHREAD_TERMINATED: + /* Pretend THREAD wasn't there in the first place. */ + __pthread_mutex_unlock (&pthread->state_lock); + err = ESRCH; + break; + + default: + /* Thou shalt not detach non-joinable threads! */ + __pthread_mutex_unlock (&pthread->state_lock); + err = EINVAL; + break; + } + + return err; +} diff --git a/pthread/pt-exit.c b/pthread/pt-exit.c new file mode 100644 index 00000000..fb9e97c0 --- /dev/null +++ b/pthread/pt-exit.c @@ -0,0 +1,115 @@ +/* Thread termination. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <pthread.h> +#include <stdlib.h> + +#include <pt-internal.h> + +#include <bits/atomic.h> + + +/* Terminate the current thread and make STATUS available to any + thread that might join us. */ +void +pthread_exit (void *status) +{ + struct __pthread *self = _pthread_self (); + struct __pthread_cancelation_handler **handlers; + int oldstate; + + /* Run any cancelation handlers. According to POSIX, the + cancellation cleanup handlers should be called with cancellation + disabled. */ + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate); + + for (handlers = __pthread_get_cleanup_stack (); + *handlers; + *handlers = (*handlers)->next) + (*handlers)->handler ((*handlers)->arg); + + pthread_setcancelstate (oldstate, &oldstate); + + /* Destory any thread specific data. */ + __pthread_destroy_specific (self); + + /* Destroy any signal state. */ + __pthread_sigstate_destroy (self); + + /* Decrease the number of threads. We use an atomic operation to + make sure that only the last thread calls `exit'. */ + if (__atomic_dec_and_test (&__pthread_total)) + /* We are the last thread. */ + exit (0); + + /* Note that after this point the process can be terminated at any + point if another thread calls `pthread_exit' and happens to be + the last thread. */ + + __pthread_mutex_lock (&self->state_lock); + + if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending) + status = PTHREAD_CANCELED; + + switch (self->state) + { + default: + assert (! "This cannot happen!"); + break; + + case PTHREAD_DETACHED: + /* Make sure that nobody can reference this thread anymore, and + mark it as terminated. Our thread ID will immediately become + available for re-use. For obvious reasons, we cannot + deallocate our own stack. However, it will eventually be + reused when this thread structure is recycled. */ + __pthread_mutex_unlock (&self->state_lock); + __pthread_dealloc (self); + + break; + + case PTHREAD_JOINABLE: + /* We need to stay around for a while since another thread + might want to join us. */ + self->state = PTHREAD_EXITED; + + /* We need to remember the exit status. A thread joining us + might ask for it. */ + self->status = status; + + /* Broadcast the condition. This will wake up threads that are + waiting to join us. */ + pthread_cond_broadcast (&self->state_cond); + __pthread_mutex_unlock (&self->state_lock); + + break; + } + + /* Note that after this point the resources used by this thread can + be freed at any moment if another thread joins or detaches us. + This means that before freeing any resources, such a thread + should make sure that this thread is really halted. */ + + __pthread_thread_halt (self); + + /* NOTREACHED */ + abort (); +} diff --git a/pthread/pt-initialize.c b/pthread/pt-initialize.c new file mode 100644 index 00000000..cf32b8b5 --- /dev/null +++ b/pthread/pt-initialize.c @@ -0,0 +1,33 @@ +/* Initialize pthreads library. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <string.h> + +#include <pt-internal.h> +#include <set-hooks.h> + +DEFINE_HOOK (__pthread_init, (void)); + +/* Initialize the pthreads library. */ +void +__pthread_initialize (void) +{ + RUN_HOOK (__pthread_init, ()); +} diff --git a/pthread/pt-internal.h b/pthread/pt-internal.h new file mode 100644 index 00000000..a32345c4 --- /dev/null +++ b/pthread/pt-internal.h @@ -0,0 +1,260 @@ +/* Internal defenitions for pthreads library. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _PT_INTERNAL_H +#define _PT_INTERNAL_H 1 + +#include <pthread.h> +#include <stddef.h> +#include <sched.h> +#include <signal.h> +#include <assert.h> + +#include <bits/atomic.h> + +#include <pt-key.h> + +#include <pt-sysdep.h> +#include <pt-machdep.h> + +/* Thread state. */ +enum pthread_state +{ + PTHREAD_JOINABLE = 0, + PTHREAD_DETACHED, + PTHREAD_EXITED, + PTHREAD_TERMINATED +}; + +#ifndef PTHREAD_KEY_MEMBERS +# define PTHREAD_KEY_MEMBERS +#endif + +#ifndef PTHREAD_SYSDEP_MEMBERS +# define PTHREAD_SYSDEP_MEMBERS +#endif + +/* This structure describes a POSIX thread. */ +struct __pthread +{ + /* Thread ID. */ + pthread_t thread; + + /* Cancellation. */ + int cancel_state; + int cancel_type; + int cancel_pending; + struct __pthread_cancelation_handler *cancelation_handlers; + + /* Thread stack. */ + void *stackaddr; + size_t stacksize; + size_t guardsize; /* Included in STACKSIZE (i.e. total + stack memory is STACKSIZE, not + STACKSIZE + GUARDSIZE). */ + int stack; /* Nonzero if the stack was allocated. */ + + /* Exit status. */ + void *status; + + /* Thread state. */ + enum pthread_state state; + pthread_mutex_t state_lock; /* Locks the state. */ + pthread_cond_t state_cond; /* Signalled when the state changes. */ + + /* Thread context. */ + struct pthread_mcontext mcontext; + + PTHREAD_KEY_MEMBERS + + PTHREAD_SYSDEP_MEMBERS + + struct __pthread *next, **prevp; +}; + +/* Enqueue an element THREAD on the queue *HEAD. */ +static inline void +__pthread_enqueue (struct __pthread **head, struct __pthread *thread) +{ + assert (thread->prevp == 0); + + thread->next = *head; + thread->prevp = head; + if (*head) + (*head)->prevp = &thread->next; + *head = thread; +} + +/* Dequeue the element THREAD from the queue it is connected to. */ +static inline void +__pthread_dequeue (struct __pthread *thread) +{ + assert (thread); + + if (thread->next) + thread->next->prevp = thread->prevp; + *thread->prevp = thread->next; + thread->prevp = 0; +} + +/* Iterate over QUEUE storing each element in ELEMENT. */ +#define __pthread_queue_iterate(queue, element) \ + for (element = queue; element; element = element->next) + +/* Iterate over QUEUE dequeuing each element, storing it in + ELEMENT. */ +#define __pthread_dequeuing_iterate(queue, element) \ + for (element = queue; \ + element && ((element->prevp = 0), 1); \ + element = element->next) + +/* The total number of threads currently active. */ +extern __atomic_t __pthread_total; + +/* The total number of thread IDs currently in use, or on the list of + available thread IDs. */ +extern int __pthread_num_threads; + +/* Concurrency hint. */ +extern int __pthread_concurrency; + +/* Array of __pthread structures and its lock. */ +extern struct __pthread **__pthread_threads; +extern pthread_rwlock_t __pthread_threads_lock; + +#define __pthread_getid(thread) \ + ({ struct __pthread *__t; \ + pthread_rwlock_rdlock (&__pthread_threads_lock); \ + __t = __pthread_threads[thread]; \ + pthread_rwlock_unlock (&__pthread_threads_lock); \ + __t; }) + +#define __pthread_setid(thread, pthread) \ + pthread_rwlock_wrlock (&__pthread_threads_lock); \ + __pthread_threads[thread] = pthread; \ + pthread_rwlock_unlock (&__pthread_threads_lock); + +/* Similar to pthread_self, but returns the thread descriptor instead + of the thread ID. */ +#ifndef _pthread_self +extern struct __pthread *_pthread_self (void); +#endif + + +/* Initialize the pthreads library. */ +extern void __pthread_initialize (void); + +/* Internal version of pthread_create. Rather than return the new + tid, we return the whole __pthread structure in *PTHREAD. */ +extern int __pthread_create_internal (struct __pthread **pthread, + const pthread_attr_t *attr, + void *(*start_routine)(void *), + void *arg); + +/* Allocate a new thread structure and a pthread thread ID (but not a + kernel thread or a stack). */ +extern int __pthread_alloc (struct __pthread **thread); + +/* Deallocate the thread structure. This is the dual of + __pthread_alloc (N.B. it does not call __pthread_stack_alloc nor + __pthread_thread_halt). */ +extern void __pthread_dealloc (struct __pthread *thread); + + +/* Allocate a stack of size STACKSIZE. The stack base shall be + returned in *STACKADDR. */ +extern int __pthread_stack_alloc (void **stackaddr, size_t stacksize); + +/* Deallocate the stack STACKADDR of size STACKSIZE. */ +extern void __pthread_stack_dealloc (void *stackaddr, size_t stacksize); + + +/* Setup thread THREAD's context. */ +extern int __pthread_setup (struct __pthread *thread, + void (*entry_point)(void *(*)(void *), + void *), + void *(*start_routine)(void *), void *arg); + + +/* Allocate a kernel thread for THREAD; it must not be placed on the + run queue. */ +extern int __pthread_thread_alloc (struct __pthread *thread); + +/* Start THREAD making it eligible to run. */ +extern int __pthread_thread_start (struct __pthread *thread); + +/* Stop thread thread and deallocate any kernel resources associated + with THREAD. */ +extern void __pthread_thread_halt (struct __pthread *thread); + + +/* Block THREAD. */ +extern void __pthread_block (struct __pthread *thread); + +/* Block THREAD until *ABSTIME is reached. */ +extern error_t __pthread_timedblock (struct __pthread *thread, + const struct timespec *abstime); + +/* Wakeup THREAD. */ +extern void __pthread_wakeup (struct __pthread *thread); + + +/* Perform a cancelation. */ +extern int __pthread_do_cancel (struct __pthread *thread); + + +/* Initialize the thread specific data structures. THREAD must be the + calling thread. */ +extern error_t __pthread_init_specific (struct __pthread *thread); + +/* Call the destructors on all of the thread specific data in THREAD. + THREAD must be the calling thread. */ +extern void __pthread_destroy_specific (struct __pthread *thread); + + +/* Initialize newly create thread *THREAD's signal state data + structures. */ +extern error_t __pthread_sigstate_init (struct __pthread *thread); + +/* Destroy the signal state data structures associcated with thread + *THREAD. */ +extern void __pthread_sigstate_destroy (struct __pthread *thread); + +/* Modify thread *THREAD's signal state. */ +extern error_t __pthread_sigstate (struct __pthread *thread, int how, + const sigset_t *set, sigset_t *oset, + int clear_pending); + + +/* Default thread attributes. */ +extern const struct __pthread_attr __pthread_default_attr; + +/* Default barrier attributes. */ +extern const struct __pthread_barrierattr __pthread_default_barrierattr; + +/* Default mutex attributes. */ +extern const struct __pthread_mutexattr __pthread_default_mutexattr; + +/* Default rdlock attributes. */ +const struct __pthread_rwlockattr __pthread_default_rwlockattr; + +/* Default condition attributes. */ +const struct __pthread_condattr __pthread_default_condattr; + +#endif /* pt-internal.h */ diff --git a/pthread/pt-join.c b/pthread/pt-join.c new file mode 100644 index 00000000..698b6c9f --- /dev/null +++ b/pthread/pt-join.c @@ -0,0 +1,79 @@ +/* Wait for thread termination. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <errno.h> +#include <pthread.h> +#include <stddef.h> + +#include <pt-internal.h> + +/* Make calling thread wait for termination of thread THREAD. Return + the exit status of the thread in *STATUS. */ +int +pthread_join (pthread_t thread, void **status) +{ + struct __pthread *pthread; + int err = 0; + + /* Lookup the thread structure for THREAD. */ + pthread = __pthread_getid (thread); + if (pthread == NULL) + return ESRCH; + + __pthread_mutex_lock (&pthread->state_lock); + pthread_cleanup_push (__pthread_mutex_unlock, &pthread->state_lock); + + while (pthread->state == PTHREAD_JOINABLE) + pthread_cond_wait (&pthread->state_cond, &pthread->state_lock); + + pthread_cleanup_pop (0); + + switch (pthread->state) + { + case PTHREAD_EXITED: + __pthread_mutex_unlock (&pthread->state_lock); + + /* THREAD has already exited. Salvage its exit status. */ + if (status) + *status = pthread->status; + + /* Make sure nobody can reference it anymore, and mark it as + terminated. */ + assert (pthread->stack); + __pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize); + pthread->stack = 0; + + __pthread_dealloc (pthread); + break; + + case PTHREAD_TERMINATED: + /* Pretend THREAD wasn't there in the first place. */ + __pthread_mutex_unlock (&pthread->state_lock); + err = ESRCH; + break; + + default: + /* Thou shalt not join non-joinable threads! */ + __pthread_mutex_unlock (&pthread->state_lock); + err = EINVAL; + break; + } + + return err; +} diff --git a/pthread/pt-self.c b/pthread/pt-self.c new file mode 100644 index 00000000..e14fe1eb --- /dev/null +++ b/pthread/pt-self.c @@ -0,0 +1,29 @@ +/* Get calling thread's ID. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +/* Return the thread ID of the calling thread. */ +pthread_t +pthread_self (void) +{ + return _pthread_self()->thread; +} diff --git a/pthread/pt-setcancelstate.c b/pthread/pt-setcancelstate.c new file mode 100644 index 00000000..ded58922 --- /dev/null +++ b/pthread/pt-setcancelstate.c @@ -0,0 +1,42 @@ +/* Set the cancel state for the calling thread. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +int +pthread_setcancelstate (int state, int *oldstate) +{ + struct __pthread *p = _pthread_self (); + + switch (state) + { + default: + return EINVAL; + case PTHREAD_CANCEL_ENABLE: + case PTHREAD_CANCEL_DISABLE: + break; + } + + *oldstate = p->cancel_state; + p->cancel_state = state; + + return 0; +} diff --git a/pthread/pt-setcanceltype.c b/pthread/pt-setcanceltype.c new file mode 100644 index 00000000..9511c991 --- /dev/null +++ b/pthread/pt-setcanceltype.c @@ -0,0 +1,42 @@ +/* Set the cancel type for the calling thread. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +int +pthread_setcanceltype (int type, int *oldtype) +{ + struct __pthread *p = _pthread_self (); + + switch (type) + { + default: + return EINVAL; + case PTHREAD_CANCEL_DEFERRED: + case PTHREAD_CANCEL_ASYNCHRONOUS: + break; + } + + *oldtype = p->cancel_type; + p->cancel_type = type; + + return 0; +} diff --git a/pthread/pt-sigmask.c b/pthread/pt-sigmask.c new file mode 100644 index 00000000..1b53873e --- /dev/null +++ b/pthread/pt-sigmask.c @@ -0,0 +1,33 @@ +/* Get or set a thread's signal mask. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <signal.h> + +#include <pt-internal.h> + +int +pthread_sigmask (int how, const sigset_t *set, + sigset_t *oset) +{ + struct __pthread *self = _pthread_self (); + + /* Do not clear SELF's pending signals. */ + return __pthread_sigstate (self, how, set, oset, 0); +} diff --git a/pthread/pt-spin-inlines.c b/pthread/pt-spin-inlines.c new file mode 100644 index 00000000..cfb21dd3 --- /dev/null +++ b/pthread/pt-spin-inlines.c @@ -0,0 +1,34 @@ +/* Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +/* <bits/spin-lock.h> declares some extern inline functions. These + functions are declared additionally here for use when inlining is + not possible. */ + +#define _FORCE_INLINES +#define __PT_SPIN_INLINE /* empty */ + +#include <pthread.h> + +/* Weak aliases for the spin lock functions. Note that + pthread_spin_lock is left out deliberately. We already provide an + implementation for it in pt-spin.c. */ +weak_alias (__pthread_spin_destroy, pthread_spin_destroy); +weak_alias (__pthread_spin_init, pthread_spin_init); +weak_alias (__pthread_spin_trylock, pthread_spin_trylock); +weak_alias (__pthread_spin_unlock, pthread_spin_unlock); diff --git a/pthread/pt-testcancel.c b/pthread/pt-testcancel.c new file mode 100644 index 00000000..01f1ac9c --- /dev/null +++ b/pthread/pt-testcancel.c @@ -0,0 +1,31 @@ +/* Add an explicit cancelation point. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +void +pthread_testcancel (void) +{ + struct __pthread *p = _pthread_self (); + + if (p->cancel_state == PTHREAD_CANCEL_ENABLE && p->cancel_pending) + pthread_exit (PTHREAD_CANCELED); +} diff --git a/sysdeps/generic/bits/barrier-attr.h b/sysdeps/generic/bits/barrier-attr.h new file mode 100644 index 00000000..86f3cb02 --- /dev/null +++ b/sysdeps/generic/bits/barrier-attr.h @@ -0,0 +1,30 @@ +/* Thread barrier attribute type. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_BARRIER_ATTR_H +#define _BITS_BARRIER_ATTR_H 1 + +/* This structure describes the attributes of a POSIX thread barrier. + Note that not all of them are supported on all systems. */ +struct __pthread_barrierattr +{ + enum __pthread_process_shared pshared; +}; + +#endif /* bits/barrier-attr.h */ diff --git a/sysdeps/generic/bits/barrier.h b/sysdeps/generic/bits/barrier.h new file mode 100644 index 00000000..5e559312 --- /dev/null +++ b/sysdeps/generic/bits/barrier.h @@ -0,0 +1,39 @@ +/* Thread barrier attribute type. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_BARRIER_H +#define _BITS_BARRIER_H 1 + +#include <bits/spin-lock.h> + +/* This structure describes the attributes of a POSIX barrier. */ +struct __pthread_barrier +{ + __pthread_spinlock_t lock; + struct __pthread *queue; /* List of waiters. */ + unsigned pending; /* Number of that still need to wait on + barrier. */ + unsigned count; /* Number of threads that must wait before + barrier is passed. */ + struct __pthread_barrierattr *attr; + void *data; +}; + + +#endif /* bits/barrier.h */ diff --git a/sysdeps/generic/bits/cancelation.h b/sysdeps/generic/bits/cancelation.h new file mode 100644 index 00000000..bb6b58a1 --- /dev/null +++ b/sysdeps/generic/bits/cancelation.h @@ -0,0 +1,54 @@ +/* Cancelation. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_CANCELATION_H +#define _BITS_CANCELATION_H 1 + +#include <assert.h> + +struct __pthread_cancelation_handler +{ + void (*handler)(void *); + void *arg; + struct __pthread_cancelation_handler *next; +}; + +/* Returns the thread local location of the cleanup handler stack. */ +struct __pthread_cancelation_handler **__pthread_get_cleanup_stack (void); + +#define pthread_cleanup_push(rt, rtarg) \ + { \ + struct __pthread_cancelation_handler **__handlers \ + = __pthread_get_cleanup_stack (); \ + struct __pthread_cancelation_handler __handler = \ + { \ + handler: (rt), \ + arg: (rtarg), \ + next: *__handlers \ + }; \ + *__handlers = &__handler; + +#define pthread_cleanup_pop(execute) \ + if (execute) \ + __handler.handler (__handler.arg); \ + assert (*__handlers == &__handler); \ + *__handlers = __handler.next; \ + } + +#endif /* _BITS_CANCELATION_H */ diff --git a/sysdeps/generic/bits/condition-attr.h b/sysdeps/generic/bits/condition-attr.h new file mode 100644 index 00000000..2d48dfb6 --- /dev/null +++ b/sysdeps/generic/bits/condition-attr.h @@ -0,0 +1,32 @@ +/* Condition attribute type. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_CONDITION_ATTR_H +#define _BITS_CONDITION_ATTR_H 1 + +#include <time.h> + +/* User visible part of a condition attribute variable. */ +struct __pthread_condattr + { + enum __pthread_process_shared pshared; + clockid_t clock; + }; + +#endif /* bits/condition.h */ diff --git a/sysdeps/generic/bits/condition.h b/sysdeps/generic/bits/condition.h new file mode 100644 index 00000000..cb7e935c --- /dev/null +++ b/sysdeps/generic/bits/condition.h @@ -0,0 +1,40 @@ +/* Condition type. Generic version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_CONDITION_H +#define _BITS_CONDITION_H 1 + +#include <bits/spin-lock.h> + +/* User visible part of a condition variable. */ +struct __pthread_cond + { + __pthread_spinlock_t __lock; + struct __pthread *__queue; + struct __pthread_condattr *__attr; + struct __pthread_condimpl *__impl; + void *__data; + }; + +/* Initializer for a condition variable. */ +#define __PTHREAD_COND_INITIALIZER \ + ((struct __pthread_cond) \ + { __SPIN_LOCK_INITIALIZER, NULL, NULL, NULL, NULL }) + +#endif /* bits/condition.h */ diff --git a/sysdeps/generic/bits/mutex-attr.h b/sysdeps/generic/bits/mutex-attr.h new file mode 100644 index 00000000..883b0746 --- /dev/null +++ b/sysdeps/generic/bits/mutex-attr.h @@ -0,0 +1,33 @@ +/* Mutex attribute type. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_MUTEX_ATTR_H +#define _BITS_MUTEX_ATTR_H 1 + +/* This structure describes the attributes of a POSIX mutex + attribute. */ +struct __pthread_mutexattr +{ + int prioceiling; + enum __pthread_mutex_protocol protocol; + enum __pthread_process_shared pshared; + enum __pthread_mutex_type mutex_type; +}; + +#endif /* bits/mutex-attr.h */ diff --git a/sysdeps/generic/bits/mutex.h b/sysdeps/generic/bits/mutex.h new file mode 100644 index 00000000..2baf0408 --- /dev/null +++ b/sysdeps/generic/bits/mutex.h @@ -0,0 +1,139 @@ +/* Mutex type. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_MUTEX_H + +#ifndef __need_pthread_mutex +# define _BITS_MUTEX_H 1 +#endif + +#ifndef __pthread_mutex_defined +# if defined __need_pthread_mutex || defined _BITS_MUTEX_H +# undef __need_pthread_mutex +# define __pthread_mutex_defined + +# include <bits/spin-lock.h> +# include <bits/mutex-attr.h> + +/* User visible part of a mutex. */ +struct __pthread_mutex + { + __pthread_spinlock_t __held; + __pthread_spinlock_t __lock; + /* In cthreads, mutex_init does not initialized thre third + pointer, as such, we cannot rely on its value for anything. */ + char *cthreadscompat1; + struct __pthread *__queue; + struct __pthread_mutexattr *attr; + void *data; + /* Up to this point, we are completely compatible with cthreads + and what libc expects. */ + void *owner; + unsigned locks; + /* If NULL then the default attributes apply. */ + }; + +/* Initializer for a mutex. N.B. this also happens to be compatible + with the cthread mutex initializer. */ +# define __PTHREAD_MUTEX_INITIALIZER \ + ((struct __pthread_mutex) \ + { __SPIN_LOCK_INITIALIZER, __SPIN_LOCK_INITIALIZER, NULL, NULL, NULL, \ + NULL, 0, 0 }) + +# endif +#endif /* Not __pthread_mutex_defined. */ + +#ifdef _BITS_MUTEX_H + +#include <errno.h> +#include <stddef.h> + +#ifdef __USE_EXTERN_INLINES + +# ifndef _EXTERN_INLINE +# define _EXTERN_INLINE extern __inline +# endif + +_EXTERN_INLINE int +pthread_mutex_init (struct __pthread_mutex *__mutex, + const pthread_mutexattr_t *attr) +{ + extern int _pthread_mutex_init (struct __pthread_mutex *, + const pthread_mutexattr_t *); + + if (attr) + return _pthread_mutex_init (__mutex, attr); + + *__mutex = __PTHREAD_MUTEX_INITIALIZER; + return 0; +} + +_EXTERN_INLINE int +pthread_mutex_destroy (struct __pthread_mutex *__mutex) +{ + extern int _pthread_mutex_destroy (struct __pthread_mutex *); + + if (__mutex->attr || __mutex->data) + return _pthread_mutex_destroy (__mutex); + + return 0; +} + +_EXTERN_INLINE int +__pthread_mutex_lock (struct __pthread_mutex *__mutex) +{ + extern int _pthread_mutex_lock (struct __pthread_mutex *); + + if (__mutex->attr == NULL + && __mutex->data == NULL + && __pthread_spin_trylock (&__mutex->__held) == 0) + return 0; + + return _pthread_mutex_lock (__mutex); +} + +extern inline int +__pthread_mutex_trylock (struct __pthread_mutex *__mutex) +{ + extern int _pthread_mutex_trylock (struct __pthread_mutex *); + + if (__mutex->attr == NULL + && __mutex->data == NULL) + return __pthread_spin_trylock (&__mutex->__held); + + return _pthread_mutex_trylock (__mutex); +} + +extern inline int +pthread_mutex_lock (struct __pthread_mutex *__mutex) +{ + return __pthread_mutex_lock (__mutex); +} + +extern inline int +pthread_mutex_trylock (struct __pthread_mutex *__mutex) +{ + return __pthread_mutex_trylock (__mutex); +} + +#endif /* Use extern inlines. */ + +#endif + +#endif /* bits/mutex.h */ diff --git a/sysdeps/generic/bits/once.h b/sysdeps/generic/bits/once.h new file mode 100644 index 00000000..7f572faf --- /dev/null +++ b/sysdeps/generic/bits/once.h @@ -0,0 +1,34 @@ +/* Dynamic package initialization data structures. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_ONCE_H +#define _BITS_ONCE_H 1 + +#include <bits/spin-lock.h> + +struct __pthread_once +{ + int run; + __pthread_spinlock_t lock; +}; + +#define __PTHREAD_ONCE_INIT \ + { 0, __SPIN_LOCK_INITIALIZER } + +#endif /* bits/once.h */ diff --git a/sysdeps/generic/bits/pthread.h b/sysdeps/generic/bits/pthread.h new file mode 100644 index 00000000..740325d0 --- /dev/null +++ b/sysdeps/generic/bits/pthread.h @@ -0,0 +1,33 @@ +/* Pthread data structures. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_PTHREAD_H +#define _BITS_PTHREAD_H 1 + +typedef int pthread_t; + +/* Return true if __T1 and __T2 both name the same thread. Otherwise, + false. */ +extern inline int +pthread_equal (pthread_t __t1, pthread_t __t2) +{ + return __t1 == __t2; +} + +#endif /* bits/pthread.h */ diff --git a/sysdeps/generic/bits/rwlock-attr.h b/sysdeps/generic/bits/rwlock-attr.h new file mode 100644 index 00000000..44765bd6 --- /dev/null +++ b/sysdeps/generic/bits/rwlock-attr.h @@ -0,0 +1,30 @@ +/* Thread rwlock attribute type. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_RWLOCK_ATTR_H +#define _BITS_RWLOCK_ATTR_H 1 + +/* This structure describes the attributes of a POSIX thread rwlock. + Note that not all of them are supported on all systems. */ +struct __pthread_rwlockattr +{ + enum __pthread_process_shared pshared; +}; + +#endif /* bits/rwlock-attr.h */ diff --git a/sysdeps/generic/bits/rwlock.h b/sysdeps/generic/bits/rwlock.h new file mode 100644 index 00000000..d089b0c6 --- /dev/null +++ b/sysdeps/generic/bits/rwlock.h @@ -0,0 +1,73 @@ +/* rwlock type. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_RWLOCK_H +#define _BITS_RWLOCK_H + +#include <bits/spin-lock.h> + +/* User visible part of a rwlock. If __held is not held and readers + is 0, then the lock is unlocked. If __held is held and readers is + 0, then the lock is held by a writer. If __held is held and + readers is greater than 0, then the lock is held by READERS + readers. */ +struct __pthread_rwlock + { + __pthread_spinlock_t __held; + __pthread_spinlock_t __lock; + int readers; + struct __pthread *readerqueue; + struct __pthread *writerqueue; + struct __pthread_rwlockattr *__attr; + void *__data; + }; + +/* Initializer for a rwlock. */ +#define __PTHREAD_RWLOCK_INITIALIZER \ + ((struct __pthread_rwlock) \ + { __SPIN_LOCK_INITIALIZER, __SPIN_LOCK_INITIALIZER, 0, 0, 0, 0, 0 }) + + +_EXTERN_INLINE int +pthread_rwlock_init (struct __pthread_rwlock *__rwlock, + const struct __pthread_rwlockattr *__attr) +{ + extern int _pthread_rwlock_init (struct __pthread_rwlock *, + const struct __pthread_rwlockattr *); + + if (__attr) + return _pthread_rwlock_init (__rwlock, __attr); + + *__rwlock = __PTHREAD_RWLOCK_INITIALIZER; + return 0; +} + +_EXTERN_INLINE int +pthread_rwlock_destroy (struct __pthread_rwlock *__rwlock) +{ + extern int _pthread_rwlock_destroy (struct __pthread_rwlock *); + + if (__rwlock->__attr + || __rwlock->__data) + return _pthread_rwlock_destroy (__rwlock); + + return 0; +} + +#endif /* bits/rwlock.h */ diff --git a/sysdeps/generic/bits/thread-attr.h b/sysdeps/generic/bits/thread-attr.h new file mode 100644 index 00000000..31630221 --- /dev/null +++ b/sysdeps/generic/bits/thread-attr.h @@ -0,0 +1,39 @@ +/* Thread attribute type. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_THREAD_ATTR_H +#define _BITS_THREAD_ATTR_H 1 + +#include <sched.h> + +/* This structure describes the attributes of a POSIX thread. Note + that not all of them are supported on all systems. */ +struct __pthread_attr +{ + struct sched_param schedparam; + void *stackaddr; + size_t stacksize; + size_t guardsize; + enum __pthread_detachstate detachstate; + enum __pthread_inheritsched inheritsched; + enum __pthread_contentionscope contentionscope; + int schedpolicy; +}; + +#endif /* bits/thread-attr.h */ diff --git a/sysdeps/generic/bits/thread-barrier.h b/sysdeps/generic/bits/thread-barrier.h new file mode 100644 index 00000000..23d51ae5 --- /dev/null +++ b/sysdeps/generic/bits/thread-barrier.h @@ -0,0 +1,30 @@ +/* Thread barrier attribute type. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_THREAD_BARRIER_H +#define _BITS_THREAD_BARRIER_H 1 + +/* This structure describes the attributes of a POSIX thread barrier. + Note that not all of them are supported on all systems. */ +struct __pthread_attr +{ + enum __ +}; + +#endif /* bits/thread-barrier.h */ diff --git a/sysdeps/generic/bits/thread-specific.h b/sysdeps/generic/bits/thread-specific.h new file mode 100644 index 00000000..b42d99e7 --- /dev/null +++ b/sysdeps/generic/bits/thread-specific.h @@ -0,0 +1,25 @@ +/* Thread specific data. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_THREAD_SPECIFIC_H +#define _BITS_THREAD_SPECIFIC_H 1 + +typedef int __pthread_key; + +#endif /* bits/thread-specific.h */ diff --git a/sysdeps/generic/pt-atfork.c b/sysdeps/generic/pt-atfork.c new file mode 100644 index 00000000..32e1e78c --- /dev/null +++ b/sysdeps/generic/pt-atfork.c @@ -0,0 +1,29 @@ +/* Register fork handlers. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_atfork (void (*prepare) (void), + void (*parent) (void), + void (*child) (void)) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-attr-destroy.c b/sysdeps/generic/pt-attr-destroy.c new file mode 100644 index 00000000..c4b9aa54 --- /dev/null +++ b/sysdeps/generic/pt-attr-destroy.c @@ -0,0 +1,27 @@ +/* pthread_attr_destroy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_destroy (pthread_attr_t *attr) +{ + return 0; +} diff --git a/sysdeps/generic/pt-attr-getdetachstate.c b/sysdeps/generic/pt-attr-getdetachstate.c new file mode 100644 index 00000000..be406f1d --- /dev/null +++ b/sysdeps/generic/pt-attr-getdetachstate.c @@ -0,0 +1,29 @@ +/* pthread_attr_getdetachstate. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getdetachstate (const pthread_attr_t *attr, + int *detachstate) +{ + *detachstate = attr->detachstate; + return 0; +} diff --git a/sysdeps/generic/pt-attr-getguardsize.c b/sysdeps/generic/pt-attr-getguardsize.c new file mode 100644 index 00000000..c1d9f13e --- /dev/null +++ b/sysdeps/generic/pt-attr-getguardsize.c @@ -0,0 +1,29 @@ +/* pthread_attr_getguardsize. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getguardsize (const pthread_attr_t *attr, + size_t *guardsize) +{ + *guardsize = attr->guardsize; + return 0; +} diff --git a/sysdeps/generic/pt-attr-getinheritsched.c b/sysdeps/generic/pt-attr-getinheritsched.c new file mode 100644 index 00000000..cca4e3d7 --- /dev/null +++ b/sysdeps/generic/pt-attr-getinheritsched.c @@ -0,0 +1,29 @@ +/* pthread_attr_getinheritsched. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getinheritsched (const pthread_attr_t *attr, + int *inheritsched) +{ + *inheritsched = attr->inheritsched; + return 0; +} diff --git a/sysdeps/generic/pt-attr-getschedparam.c b/sysdeps/generic/pt-attr-getschedparam.c new file mode 100644 index 00000000..25afebd0 --- /dev/null +++ b/sysdeps/generic/pt-attr-getschedparam.c @@ -0,0 +1,31 @@ +/* pthread_attr_getschedparam. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <sched.h> + +#include <pt-internal.h> + +int +pthread_attr_getschedparam (const pthread_attr_t *attr, + struct sched_param *param) +{ + *param = attr->schedparam; + return 0; +} diff --git a/sysdeps/generic/pt-attr-getschedpolicy.c b/sysdeps/generic/pt-attr-getschedpolicy.c new file mode 100644 index 00000000..4349a238 --- /dev/null +++ b/sysdeps/generic/pt-attr-getschedpolicy.c @@ -0,0 +1,29 @@ +/* pthread_attr_getschedpolicy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getschedpolicy (const pthread_attr_t *attr, + int *policy) +{ + *policy = attr->schedpolicy; + return 0; +} diff --git a/sysdeps/generic/pt-attr-getscope.c b/sysdeps/generic/pt-attr-getscope.c new file mode 100644 index 00000000..97198fa5 --- /dev/null +++ b/sysdeps/generic/pt-attr-getscope.c @@ -0,0 +1,29 @@ +/* pthread_attr_getscope. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getscope (const pthread_attr_t *attr, + int *contentionscope) +{ + *contentionscope = attr->contentionscope; + return 0; +} diff --git a/sysdeps/generic/pt-attr-getstack.c b/sysdeps/generic/pt-attr-getstack.c new file mode 100644 index 00000000..7beb79f0 --- /dev/null +++ b/sysdeps/generic/pt-attr-getstack.c @@ -0,0 +1,31 @@ +/* pthread_attr_getstack. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getstack (const pthread_attr_t *attr, + void **stackaddr, + size_t *stacksize) +{ + pthread_attr_getstackaddr (attr, stackaddr); + pthread_attr_getstacksize (attr, stacksize); + return 0; +} diff --git a/sysdeps/generic/pt-attr-getstackaddr.c b/sysdeps/generic/pt-attr-getstackaddr.c new file mode 100644 index 00000000..d43eeff7 --- /dev/null +++ b/sysdeps/generic/pt-attr-getstackaddr.c @@ -0,0 +1,29 @@ +/* pthread_attr_getstackaddr. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getstackaddr (const pthread_attr_t *attr, + void **stackaddr) +{ + *stackaddr = attr->stackaddr; + return 0; +} diff --git a/sysdeps/generic/pt-attr-getstacksize.c b/sysdeps/generic/pt-attr-getstacksize.c new file mode 100644 index 00000000..c1bc6617 --- /dev/null +++ b/sysdeps/generic/pt-attr-getstacksize.c @@ -0,0 +1,29 @@ +/* pthread_attr_getstacksize. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_getstacksize (const pthread_attr_t *attr, + size_t *stacksize) +{ + *stacksize = attr->stacksize; + return 0; +} diff --git a/sysdeps/generic/pt-attr-init.c b/sysdeps/generic/pt-attr-init.c new file mode 100644 index 00000000..6a930e30 --- /dev/null +++ b/sysdeps/generic/pt-attr-init.c @@ -0,0 +1,28 @@ +/* pthread_attr_init. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_init (pthread_attr_t *attr) +{ + *attr = __pthread_default_attr; + return 0; +} diff --git a/sysdeps/generic/pt-attr-setdetachstate.c b/sysdeps/generic/pt-attr-setdetachstate.c new file mode 100644 index 00000000..688ba5d6 --- /dev/null +++ b/sysdeps/generic/pt-attr-setdetachstate.c @@ -0,0 +1,38 @@ +/* pthread_attr_setdetachstate. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_setdetachstate (pthread_attr_t *attr, + int detachstate) +{ + switch (detachstate) + { + case PTHREAD_CREATE_DETACHED: + case PTHREAD_CREATE_JOINABLE: + attr->detachstate = detachstate; + break; + default: + return EINVAL; + } + + return 0; +} diff --git a/sysdeps/generic/pt-attr-setguardsize.c b/sysdeps/generic/pt-attr-setguardsize.c new file mode 100644 index 00000000..b2ba0f0f --- /dev/null +++ b/sysdeps/generic/pt-attr-setguardsize.c @@ -0,0 +1,29 @@ +/* pthread_attr_setguardsize. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_setguardsize (pthread_attr_t *attr, + size_t guardsize) +{ + attr->guardsize = guardsize; + return 0; +} diff --git a/sysdeps/generic/pt-attr-setinheritsched.c b/sysdeps/generic/pt-attr-setinheritsched.c new file mode 100644 index 00000000..e9012c58 --- /dev/null +++ b/sysdeps/generic/pt-attr-setinheritsched.c @@ -0,0 +1,38 @@ +/* pthread_attr_setinheritsched. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_setinheritsched (pthread_attr_t *attr, + int inheritsched) +{ + switch (inheritsched) + { + case PTHREAD_INHERIT_SCHED: + case PTHREAD_EXPLICIT_SCHED: + attr->inheritsched = inheritsched; + break; + default: + return EINVAL; + } + + return 0; +} diff --git a/sysdeps/generic/pt-attr-setschedparam.c b/sysdeps/generic/pt-attr-setschedparam.c new file mode 100644 index 00000000..18a575ec --- /dev/null +++ b/sysdeps/generic/pt-attr-setschedparam.c @@ -0,0 +1,38 @@ +/* pthread_attr_getschedparam. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <sched.h> +#include <string.h> + +#include <pt-internal.h> + +int +pthread_attr_setschedparam (pthread_attr_t *attr, + const struct sched_param *param) +{ + if (memcmp (param, &__pthread_default_attr.schedparam, + sizeof *param) == 0) + { + attr->schedparam = *param; + return 0; + } + + return ENOTSUP; +} diff --git a/sysdeps/generic/pt-attr-setschedpolicy.c b/sysdeps/generic/pt-attr-setschedpolicy.c new file mode 100644 index 00000000..e481d04c --- /dev/null +++ b/sysdeps/generic/pt-attr-setschedpolicy.c @@ -0,0 +1,42 @@ +/* pthread_attr_getschedpolicy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_setschedpolicy (pthread_attr_t *attr, + int policy) +{ + switch (policy) + { + case SCHED_OTHER: + attr->schedpolicy = policy; + break; + + case SCHED_FIFO: + case SCHED_RR: + return ENOTSUP; + + default: + return EINVAL; + } + + return 0; +} diff --git a/sysdeps/generic/pt-attr-setscope.c b/sysdeps/generic/pt-attr-setscope.c new file mode 100644 index 00000000..c74a4f67 --- /dev/null +++ b/sysdeps/generic/pt-attr-setscope.c @@ -0,0 +1,41 @@ +/* pthread_attr_setscope. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_setscope (pthread_attr_t *attr, + int contentionscope) +{ + if (contentionscope == __pthread_default_attr.contentionscope) + { + attr->contentionscope = contentionscope; + return 0; + } + + switch (contentionscope) + { + case PTHREAD_SCOPE_PROCESS: + case PTHREAD_SCOPE_SYSTEM: + return ENOTSUP; + default: + return EINVAL; + } +} diff --git a/sysdeps/generic/pt-attr-setstack.c b/sysdeps/generic/pt-attr-setstack.c new file mode 100644 index 00000000..360d27a3 --- /dev/null +++ b/sysdeps/generic/pt-attr-setstack.c @@ -0,0 +1,51 @@ +/* pthread_attr_setstack. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +int +pthread_attr_setstack (pthread_attr_t *attr, + void *stackaddr, + size_t stacksize) +{ + int err; + size_t s; + + /* pthread_attr_setstack should always succeed, thus we set the size + first as it is more discriminating. */ + pthread_attr_getstacksize (attr, &s); + + err = pthread_attr_setstacksize (attr, stacksize); + if (err) + return err; + + err = pthread_attr_setstackaddr (attr, stackaddr); + if (err) + { + int e = pthread_attr_setstacksize (attr, s); + assert_perror (e); + + return err; + } + + return 0; +} diff --git a/sysdeps/generic/pt-attr-setstackaddr.c b/sysdeps/generic/pt-attr-setstackaddr.c new file mode 100644 index 00000000..431f2d0c --- /dev/null +++ b/sysdeps/generic/pt-attr-setstackaddr.c @@ -0,0 +1,29 @@ +/* pthread_attr_setstackaddr. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_setstackaddr (pthread_attr_t *attr, + void *stackaddr) +{ + attr->stackaddr = stackaddr; + return 0; +} diff --git a/sysdeps/generic/pt-attr-setstacksize.c b/sysdeps/generic/pt-attr-setstacksize.c new file mode 100644 index 00000000..d0e51022 --- /dev/null +++ b/sysdeps/generic/pt-attr-setstacksize.c @@ -0,0 +1,41 @@ +/* pthread_attr_setstacksize. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_attr_setstacksize (pthread_attr_t *attr, + size_t stacksize) +{ + if (stacksize == __pthread_default_attr.stacksize) + { + attr->stacksize = stacksize; + + /* The guard size cannot be larger than the stack itself, as + such, if the new stack size is smaller than the guard size, + we squash the guard size. */ + if (attr->guardsize > attr->stacksize) + attr->guardsize = attr->stacksize; + + return 0; + } + + return ENOTSUP; +} diff --git a/sysdeps/generic/pt-attr.c b/sysdeps/generic/pt-attr.c new file mode 100644 index 00000000..769f292a --- /dev/null +++ b/sysdeps/generic/pt-attr.c @@ -0,0 +1,41 @@ +/* Default attributes. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <sched.h> +#include <stddef.h> +#include <limits.h> + +#include <pt-internal.h> + +const struct __pthread_attr __pthread_default_attr = +{ + schedparam: { sched_priority: 0 }, + stacksize: PTHREAD_STACK_DEFAULT, + stackaddr: NULL, +#ifdef PAGESIZE + guardsize: PAGESIZE, +#else + guardsize: 0, +#endif /* PAGESIZE */ + detachstate: PTHREAD_CREATE_JOINABLE, + inheritsched: PTHREAD_EXPLICIT_SCHED, + contentionscope: PTHREAD_SCOPE_SYSTEM, + schedpolicy: SCHED_OTHER +}; diff --git a/sysdeps/generic/pt-barrier-destroy.c b/sysdeps/generic/pt-barrier-destroy.c new file mode 100644 index 00000000..01b938b4 --- /dev/null +++ b/sysdeps/generic/pt-barrier-destroy.c @@ -0,0 +1,27 @@ +/* pthread_barrier_destroy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_barrier_destroy (pthread_barrier_t *barrier) +{ + return 0; +} diff --git a/sysdeps/generic/pt-barrier-init.c b/sysdeps/generic/pt-barrier-init.c new file mode 100644 index 00000000..57911d51 --- /dev/null +++ b/sysdeps/generic/pt-barrier-init.c @@ -0,0 +1,43 @@ +/* pthread_barrier_init. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <string.h> +#include <assert.h> + +#include <pt-internal.h> + +int +pthread_barrier_init (pthread_barrier_t *barrier, + const pthread_barrierattr_t *attr, + unsigned count) +{ + assert (attr->pshared == PTHREAD_PROCESS_PRIVATE); + + if (count == 0) + return EINVAL; + + memset (barrier, 0, sizeof *barrier); + + barrier->lock = PTHREAD_SPINLOCK_INITIALIZER; + barrier->pending = count; + barrier->count = count; + + return 0; +} diff --git a/sysdeps/generic/pt-barrier-wait.c b/sysdeps/generic/pt-barrier-wait.c new file mode 100644 index 00000000..f1de250a --- /dev/null +++ b/sysdeps/generic/pt-barrier-wait.c @@ -0,0 +1,61 @@ +/* pthread_barrier_wait. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +int +pthread_barrier_wait (pthread_barrier_t *barrier) +{ + __pthread_spin_lock (&barrier->lock); + if (-- barrier->pending == 0) + { + barrier->pending = barrier->count; + + if (barrier->count > 1) + { + struct __pthread *wakeup; + + wakeup = barrier->queue; + barrier->queue = NULL; + __pthread_spin_unlock (&barrier->lock); + + /* We can safely walk the list of waiting threads without + holding the lock since it is decoupled from the barrier + variable now. */ + __pthread_dequeuing_iterate (wakeup, wakeup) + __pthread_wakeup (wakeup); + } + + return PTHREAD_BARRIER_SERIAL_THREAD; + } + else + { + struct __pthread *self = _pthread_self (); + + /* Add ourselves to the list of waiters. */ + __pthread_enqueue (&barrier->queue, self); + __pthread_spin_unlock (&barrier->lock); + + __pthread_block (self); + return 0; + } +} diff --git a/sysdeps/generic/pt-barrier.c b/sysdeps/generic/pt-barrier.c new file mode 100644 index 00000000..d772719f --- /dev/null +++ b/sysdeps/generic/pt-barrier.c @@ -0,0 +1,26 @@ +/* Default barrier attributes. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +const struct __pthread_barrierattr __pthread_default_barrierattr = +{ + pshared: PTHREAD_PROCESS_PRIVATE +}; diff --git a/sysdeps/generic/pt-barrierattr-destroy.c b/sysdeps/generic/pt-barrierattr-destroy.c new file mode 100644 index 00000000..eb396347 --- /dev/null +++ b/sysdeps/generic/pt-barrierattr-destroy.c @@ -0,0 +1,27 @@ +/* pthread_barrierattr_destroy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_barrierattr_destroy (pthread_barrierattr_t *attr) +{ + return 0; +} diff --git a/sysdeps/generic/pt-barrierattr-getpshared.c b/sysdeps/generic/pt-barrierattr-getpshared.c new file mode 100644 index 00000000..ed2cdd24 --- /dev/null +++ b/sysdeps/generic/pt-barrierattr-getpshared.c @@ -0,0 +1,29 @@ +/* pthread_barrierattr_getpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_barrierattr_getpshared (const pthread_barrierattr_t *attr, + int *pshared) +{ + *pshared = attr->pshared; + return 0; +} diff --git a/sysdeps/generic/pt-barrierattr-init.c b/sysdeps/generic/pt-barrierattr-init.c new file mode 100644 index 00000000..45f16cc8 --- /dev/null +++ b/sysdeps/generic/pt-barrierattr-init.c @@ -0,0 +1,28 @@ +/* pthread_barrierattr_init. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_barrierattr_init (pthread_barrierattr_t *attr) +{ + *attr = __pthread_default_barrierattr; + return 0; +} diff --git a/sysdeps/generic/pt-barrierattr-setpshared.c b/sysdeps/generic/pt-barrierattr-setpshared.c new file mode 100644 index 00000000..84ef2df8 --- /dev/null +++ b/sysdeps/generic/pt-barrierattr-setpshared.c @@ -0,0 +1,39 @@ +/* pthread_barrierattr_setpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_barrierattr_setpshared (pthread_barrierattr_t *attr, + int pshared) +{ + switch (pshared) + { + case PTHREAD_PROCESS_PRIVATE: + attr->pshared = pshared; + return 0; + + case PTHREAD_PROCESS_SHARED: + return ENOTSUP; + + default: + return EINVAL; + } +} diff --git a/sysdeps/generic/pt-cond-brdcast.c b/sysdeps/generic/pt-cond-brdcast.c new file mode 100644 index 00000000..3f55e33e --- /dev/null +++ b/sysdeps/generic/pt-cond-brdcast.c @@ -0,0 +1,42 @@ +/* Broadcast a condition. Generic version. + Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +/* Unblock all threads that are blocked on condition variable COND. */ +int +pthread_cond_broadcast (pthread_cond_t *cond) +{ + struct __pthread *wakeup; + + __pthread_spin_lock (&cond->__lock); + + wakeup = cond->__queue; + cond->__queue = NULL; + __pthread_spin_unlock (&cond->__lock); + + /* We can safely walk the list of waiting threads without holding + the lock since it is now decoupled from the condition. */ + __pthread_dequeuing_iterate (wakeup, wakeup) + __pthread_wakeup (wakeup); + + return 0; +} diff --git a/sysdeps/generic/pt-cond-destroy.c b/sysdeps/generic/pt-cond-destroy.c new file mode 100644 index 00000000..eba47784 --- /dev/null +++ b/sysdeps/generic/pt-cond-destroy.c @@ -0,0 +1,27 @@ +/* pthread_cond_destroy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_cond_destroy (pthread_cond_t *cond) +{ + return 0; +} diff --git a/sysdeps/generic/pt-cond-init.c b/sysdeps/generic/pt-cond-init.c new file mode 100644 index 00000000..4afcc94e --- /dev/null +++ b/sysdeps/generic/pt-cond-init.c @@ -0,0 +1,34 @@ +/* pthread_cond_init. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +int +pthread_cond_init (pthread_cond_t *cond, + const pthread_condattr_t *attr) +{ + if (attr) + assert (attr->pshared == PTHREAD_PROCESS_PRIVATE); + + *cond = __PTHREAD_COND_INITIALIZER; + return 0; +} diff --git a/sysdeps/generic/pt-cond-signal.c b/sysdeps/generic/pt-cond-signal.c new file mode 100644 index 00000000..c5e1bc1d --- /dev/null +++ b/sysdeps/generic/pt-cond-signal.c @@ -0,0 +1,54 @@ +/* Signal a condition. Generic version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +static int +cond_signal (struct __pthread_cond *cond, int *unblocked) +{ + struct __pthread *wakeup; + + __pthread_spin_lock (&cond->__lock); + wakeup = cond->__queue; + if (wakeup) + __pthread_dequeue (wakeup); + __pthread_spin_unlock (&cond->__lock); + + if (wakeup) + { + /* We found a thread waiting for the condition to be signalled. + Wake it up! */ + __pthread_wakeup (wakeup); + *unblocked = 1; + } + + return 0; +} + +/* Unblock at least one of the threads that are blocked on condition + variable COND. */ +int +pthread_cond_signal (pthread_cond_t *cond) +{ + int unblocked = 0; + + return cond_signal (cond, &unblocked); +} diff --git a/sysdeps/generic/pt-cond-timedwait.c b/sysdeps/generic/pt-cond-timedwait.c new file mode 100644 index 00000000..99761f3a --- /dev/null +++ b/sysdeps/generic/pt-cond-timedwait.c @@ -0,0 +1,86 @@ +/* Wait on a condition. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond, + pthread_mutex_t *mutex, + const struct timespec *abstime); + +int +pthread_cond_timedwait (pthread_cond_t *cond, + pthread_mutex_t *mutex, + const struct timespec *abstime) +{ + return __pthread_cond_timedwait_internal (cond, mutex, abstime); +} + +/* Block on condition variable COND until ABSTIME. As a GNU + extension, if ABSTIME is NULL, then wait forever. MUTEX should be + held by the calling thread. On return, MUTEX will be held by the + calling thread. */ +int +__pthread_cond_timedwait_internal (pthread_cond_t *cond, + pthread_mutex_t *mutex, + const struct timespec *abstime) +{ + error_t err; + int canceltype; + + void cleanup (void *arg) + { + struct __pthread *self = _pthread_self (); + + __pthread_spin_lock (&cond->__lock); + if (self->prevp) + __pthread_dequeue (self); + __pthread_spin_unlock (&cond->__lock); + + pthread_setcanceltype (canceltype, &canceltype); + __pthread_mutex_lock (mutex); + } + + struct __pthread *self = _pthread_self (); + + /* Add ourselves to the list of waiters. */ + __pthread_spin_lock (&cond->__lock); + __pthread_enqueue (&cond->__queue, self); + __pthread_spin_unlock (&cond->__lock); + + __pthread_mutex_unlock (mutex); + + /* Enter async cancelation mode. If cancelation is disabled, then + this does not change anything which is exactly what we want. */ + pthread_cleanup_push (cleanup, 0); + pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &canceltype); + + if (abstime) + err = __pthread_timedblock (self, abstime); + else + { + err = 0; + __pthread_block (self); + } + + pthread_cleanup_pop (1); + + return err; +} diff --git a/sysdeps/generic/pt-cond-wait.c b/sysdeps/generic/pt-cond-wait.c new file mode 100644 index 00000000..a03476df --- /dev/null +++ b/sysdeps/generic/pt-cond-wait.c @@ -0,0 +1,37 @@ +/* Wait on a condition. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +/* Implemented in pt-cond-timedwait.c. */ +extern int __pthread_cond_timedwait_internal (pthread_cond_t *cond, + pthread_mutex_t *mutex, + const struct timespec *abstime); + + +/* Block on condition variable COND. MUTEX should be held by the + calling thread. On return, MUTEX will be held by the calling + thread. */ +int +pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex) +{ + return __pthread_cond_timedwait_internal (cond, mutex, 0); +} diff --git a/sysdeps/generic/pt-cond.c b/sysdeps/generic/pt-cond.c new file mode 100644 index 00000000..033e767f --- /dev/null +++ b/sysdeps/generic/pt-cond.c @@ -0,0 +1,29 @@ +/* Default condition attributes. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <time.h> + +#include <pt-internal.h> + +const struct __pthread_condattr __pthread_default_condattr = +{ + pshared: PTHREAD_PROCESS_PRIVATE, + clock: CLOCK_REALTIME +}; diff --git a/sysdeps/generic/pt-condattr-destroy.c b/sysdeps/generic/pt-condattr-destroy.c new file mode 100644 index 00000000..c8fd71b9 --- /dev/null +++ b/sysdeps/generic/pt-condattr-destroy.c @@ -0,0 +1,27 @@ +/* pthread_condattr_destroy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_condattr_destroy (pthread_condattr_t *cond) +{ + return 0; +} diff --git a/sysdeps/generic/pt-condattr-getclock.c b/sysdeps/generic/pt-condattr-getclock.c new file mode 100644 index 00000000..5b9f8893 --- /dev/null +++ b/sysdeps/generic/pt-condattr-getclock.c @@ -0,0 +1,31 @@ +/* pthread_condattr_getclock. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <time.h> + +#include <pt-internal.h> + +int +pthread_condattr_getclock (const pthread_condattr_t *attr, + clockid_t *clock) +{ + *clock = attr->clock; + return 0; +} diff --git a/sysdeps/generic/pt-condattr-getpshared.c b/sysdeps/generic/pt-condattr-getpshared.c new file mode 100644 index 00000000..c072a507 --- /dev/null +++ b/sysdeps/generic/pt-condattr-getpshared.c @@ -0,0 +1,29 @@ +/* pthread_condattr_getpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_condattr_getpshared (const pthread_condattr_t *attr, + int *pshared) +{ + *pshared = attr->pshared; + return 0; +} diff --git a/sysdeps/generic/pt-condattr-init.c b/sysdeps/generic/pt-condattr-init.c new file mode 100644 index 00000000..cf9e1989 --- /dev/null +++ b/sysdeps/generic/pt-condattr-init.c @@ -0,0 +1,28 @@ +/* pthread_condattr_init. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_condattr_init (pthread_condattr_t *attr) +{ + *attr = __pthread_default_condattr; + return 0; +} diff --git a/sysdeps/generic/pt-condattr-setclock.c b/sysdeps/generic/pt-condattr-setclock.c new file mode 100644 index 00000000..984c17e2 --- /dev/null +++ b/sysdeps/generic/pt-condattr-setclock.c @@ -0,0 +1,33 @@ +/* pthread_condattr_setclock. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_condattr_setclock (pthread_condattr_t *attr, clockid_t clock) +{ + if (__pthread_default_condattr.clock == clock) + { + attr->clock = clock; + return 0; + } + + return EINVAL; +} diff --git a/sysdeps/generic/pt-condattr-setpshared.c b/sysdeps/generic/pt-condattr-setpshared.c new file mode 100644 index 00000000..b9f0af83 --- /dev/null +++ b/sysdeps/generic/pt-condattr-setpshared.c @@ -0,0 +1,39 @@ +/* pthread_condattr_setpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_condattr_setpshared (pthread_condattr_t *attr, + int pshared) +{ + switch (pshared) + { + case PTHREAD_PROCESS_PRIVATE: + attr->pshared = pshared; + return 0; + + case PTHREAD_PROCESS_SHARED: + return ENOTSUP; + + default: + return EINVAL; + } +} diff --git a/sysdeps/generic/pt-destroy-specific.c b/sysdeps/generic/pt-destroy-specific.c new file mode 100644 index 00000000..b627f872 --- /dev/null +++ b/sysdeps/generic/pt-destroy-specific.c @@ -0,0 +1,28 @@ +/* __pthread_destory_specific. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +void +__pthread_destory_specifc (struct __pthread *thread) +{ + /* Not support, thus there cannot be any. */ + return; +} diff --git a/sysdeps/generic/pt-equal.c b/sysdeps/generic/pt-equal.c new file mode 100644 index 00000000..8fbc519c --- /dev/null +++ b/sysdeps/generic/pt-equal.c @@ -0,0 +1,29 @@ +/* Default attributes. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +/* Return true if __T1 and __T2 both name the same thread. Otherwise, + false. */ +int +pthread_equal (pthread_t __t1, pthread_t __t2) +{ + return __t1 == __t2; +} diff --git a/sysdeps/generic/pt-getconcurrency.c b/sysdeps/generic/pt-getconcurrency.c new file mode 100644 index 00000000..b55c8eef --- /dev/null +++ b/sysdeps/generic/pt-getconcurrency.c @@ -0,0 +1,27 @@ +/* Get the current level of desired concurrency. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_getconcurrency (void) +{ + return __pthread_concurrency; +} diff --git a/sysdeps/generic/pt-getcpuclockid.c b/sysdeps/generic/pt-getcpuclockid.c new file mode 100644 index 00000000..81f6aabd --- /dev/null +++ b/sysdeps/generic/pt-getcpuclockid.c @@ -0,0 +1,34 @@ +/* Return a thread's cpu clockid. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <time.h> + +#include <pt-internal.h> + +int +pthread_getcpuclockid (pthread_t thread, clockid_t *clock) +{ +#ifdef CLOCK_THREAD_CPUTIME_ID + *clock = CLOCK_THREAD_CPUTIME_ID; + return 0; +#else + return ENOSYS; +#endif +} diff --git a/sysdeps/generic/pt-getschedparam.c b/sysdeps/generic/pt-getschedparam.c new file mode 100644 index 00000000..c128d02e --- /dev/null +++ b/sysdeps/generic/pt-getschedparam.c @@ -0,0 +1,28 @@ +/* Get the scheduling parameters for a thread. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_getschedparam (pthread_t thread, int *policy, + struct sched_param *param) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-getspecific.c b/sysdeps/generic/pt-getspecific.c new file mode 100644 index 00000000..2a7c4a9e --- /dev/null +++ b/sysdeps/generic/pt-getspecific.c @@ -0,0 +1,27 @@ +/* pthread_getspecific. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_getspecific (pthread_key_t key) +{ + return EINVAL; +} diff --git a/sysdeps/generic/pt-init-specific.c b/sysdeps/generic/pt-init-specific.c new file mode 100644 index 00000000..00744fb6 --- /dev/null +++ b/sysdeps/generic/pt-init-specific.c @@ -0,0 +1,27 @@ +/* __pthread_init_specific. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +error_t +__pthread_init_specific (struct __pthread *thread) +{ + return 0; +} diff --git a/sysdeps/generic/pt-key-create.c b/sysdeps/generic/pt-key-create.c new file mode 100644 index 00000000..7ef4b991 --- /dev/null +++ b/sysdeps/generic/pt-key-create.c @@ -0,0 +1,27 @@ +/* pthread_key_create. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_key_create (pthread_key_t *key, void (*destructor) (void *)) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-key-delete.c b/sysdeps/generic/pt-key-delete.c new file mode 100644 index 00000000..2e36f044 --- /dev/null +++ b/sysdeps/generic/pt-key-delete.c @@ -0,0 +1,27 @@ +/* pthread_key_delete. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_key_destory (pthread_key_t key) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-key.h b/sysdeps/generic/pt-key.h new file mode 100644 index 00000000..544eb20f --- /dev/null +++ b/sysdeps/generic/pt-key.h @@ -0,0 +1,22 @@ +/* pthread_key internal declatations. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#define PTHREAD_KEY_MEMBERS diff --git a/sysdeps/generic/pt-mutex-destroy.c b/sysdeps/generic/pt-mutex-destroy.c new file mode 100644 index 00000000..d9fb59ce --- /dev/null +++ b/sysdeps/generic/pt-mutex-destroy.c @@ -0,0 +1,35 @@ +/* Destroy a mutex. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <stdlib.h> +#include <assert.h> + +#include <pt-internal.h> + +int +_pthread_mutex_destroy (pthread_mutex_t *mutex) +{ + if (mutex->attr) + free (mutex->attr); + + return 0; +} + +weak_alias (_pthread_mutex_destroy, pthread_mutex_destroy); diff --git a/sysdeps/generic/pt-mutex-getprioceiling.c b/sysdeps/generic/pt-mutex-getprioceiling.c new file mode 100644 index 00000000..eb51639b --- /dev/null +++ b/sysdeps/generic/pt-mutex-getprioceiling.c @@ -0,0 +1,28 @@ +/* Get a mutex' priority ceiling. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutex_getprioceiling (const pthread_mutex_t *mutex, + int *prioceiling) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-mutex-init.c b/sysdeps/generic/pt-mutex-init.c new file mode 100644 index 00000000..2902f6e4 --- /dev/null +++ b/sysdeps/generic/pt-mutex-init.c @@ -0,0 +1,48 @@ +/* Initialize a mutex. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <string.h> +#include <stdlib.h> +#include <assert.h> + +#include <pt-internal.h> + +int +_pthread_mutex_init (pthread_mutex_t *mutex, + const pthread_mutexattr_t *attr) +{ + *mutex = __PTHREAD_MUTEX_INITIALIZER; + + if (! attr + || memcmp (attr, &__pthread_default_mutexattr, sizeof (*attr) == 0)) + /* Use the default attributes. */ + return 0; + + /* Non-default attributes. */ + + mutex->attr = malloc (sizeof *attr); + if (! mutex->attr) + return ENOMEM; + + *mutex->attr = *attr; + return 0; +} + +weak_alias (_pthread_mutex_init, pthread_mutex_init); diff --git a/sysdeps/generic/pt-mutex-lock.c b/sysdeps/generic/pt-mutex-lock.c new file mode 100644 index 00000000..58d3827c --- /dev/null +++ b/sysdeps/generic/pt-mutex-lock.c @@ -0,0 +1,37 @@ +/* Lock a mutex. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +/* Implemented in pt-mutex-timedlock.c. */ +extern int __pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex, + const struct timespec *abstime); + +/* Lock MUTEX, block if we can't get it. */ +int +__pthread_mutex_lock (struct __pthread_mutex *mutex) +{ + return __pthread_mutex_timedlock_internal (mutex, 0); +} + +weak_alias (__pthread_mutex_lock, _pthread_mutex_lock); +weak_alias (__pthread_mutex_lock, pthread_mutex_lock); diff --git a/sysdeps/generic/pt-mutex-setprioceiling.c b/sysdeps/generic/pt-mutex-setprioceiling.c new file mode 100644 index 00000000..9fa8e50d --- /dev/null +++ b/sysdeps/generic/pt-mutex-setprioceiling.c @@ -0,0 +1,28 @@ +/* Set a mutex' priority ceiling. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutex_setprioceiling (pthread_mutex_t *mutex, int prio, + int *oldprio) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-mutex-timedlock.c b/sysdeps/generic/pt-mutex-timedlock.c new file mode 100644 index 00000000..e8c9b466 --- /dev/null +++ b/sysdeps/generic/pt-mutex-timedlock.c @@ -0,0 +1,148 @@ +/* Lock a mutex with a timeout. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +#define LOSE do { * (int *) 0 = 0; } while (1) + +int +pthread_mutex_timedlock (struct __pthread_mutex *mutex, + const struct timespec *abstime) +{ + return __pthread_mutex_timedlock_internal (mutex, abstime); +} + +/* Try to lock MUTEX, block until *ABSTIME if it is already held. As + a GNU extension, if TIMESPEC is NULL then wait forever. */ +int +__pthread_mutex_timedlock_internal (struct __pthread_mutex *mutex, + const struct timespec *abstime) +{ + struct __pthread *self; + + __pthread_spin_lock (&mutex->__lock); + if (__pthread_spin_trylock (&mutex->__held) == 0) + /* Successfully acquired the lock. */ + { + if (mutex->attr) + switch (mutex->attr->mutex_type) + { + case PTHREAD_MUTEX_NORMAL: + break; + + case PTHREAD_MUTEX_RECURSIVE: + mutex->locks = 1; + case PTHREAD_MUTEX_ERRORCHECK: + mutex->owner = _pthread_self (); + break; + + default: + LOSE; + } + + __pthread_spin_unlock (&mutex->__lock); + return 0; + } + + /* The lock is busy. */ + + self = _pthread_self (); + + if (mutex->attr) + { + switch (mutex->attr->mutex_type) + { + case PTHREAD_MUTEX_NORMAL: + break; + + case PTHREAD_MUTEX_ERRORCHECK: + if (mutex->owner == self) + { + __pthread_spin_unlock (&mutex->__lock); + return EDEADLK; + } + break; + + case PTHREAD_MUTEX_RECURSIVE: + if (mutex->owner == self) + { + mutex->locks ++; + __pthread_spin_unlock (&mutex->__lock); + return 0; + } + break; + + default: + LOSE; + } + } + + /* Add ourselves to the queue. */ + __pthread_enqueue (&mutex->__queue, self); + __pthread_spin_unlock (&mutex->__lock); + + /* Block the thread. */ + if (abstime) + { + error_t err; + + err = __pthread_timedblock (self, abstime); + if (err) + /* We timed out. We may need to disconnect ourself from the + waiter queue. + + FIXME: What do we do if we get a wakeup message before we + disconnect ourself? It may remain until the next time we + block. */ + { + assert (err = ETIMEDOUT); + + __pthread_spin_lock (&mutex->__lock); + if (self->prevp) + __pthread_dequeue (self); + __pthread_spin_unlock (&mutex->__lock); + + return err; + } + } + else + __pthread_block (self); + + if (mutex->attr) + switch (mutex->attr->mutex_type) + { + case PTHREAD_MUTEX_NORMAL: + break; + + case PTHREAD_MUTEX_RECURSIVE: + assert (mutex->locks == 0); + mutex->locks = 1; + case PTHREAD_MUTEX_ERRORCHECK: + mutex->owner = self; + break; + + default: + LOSE; + } + + return 0; +} diff --git a/sysdeps/generic/pt-mutex-trylock.c b/sysdeps/generic/pt-mutex-trylock.c new file mode 100644 index 00000000..b3c3fe3a --- /dev/null +++ b/sysdeps/generic/pt-mutex-trylock.c @@ -0,0 +1,89 @@ +/* Try to Lock a mutex. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +#define LOSE do { * (int *) 0 = 0; } while (1) + +/* Lock MUTEX, block if we can't get it. */ +int +__pthread_mutex_trylock (struct __pthread_mutex *mutex) +{ + int err; + struct __pthread *self; + + __pthread_spin_lock (&mutex->__lock); + if (__pthread_spin_trylock (&mutex->__held) == 0) + /* Acquired the lock. */ + { + if (mutex->attr) + switch (mutex->attr->mutex_type) + { + case PTHREAD_MUTEX_NORMAL: + break; + + case PTHREAD_MUTEX_RECURSIVE: + mutex->locks = 1; + case PTHREAD_MUTEX_ERRORCHECK: + mutex->owner = _pthread_self (); + break; + + default: + LOSE; + } + + __pthread_spin_unlock (&mutex->__lock); + return 0; + } + + err = EBUSY; + + if (mutex->attr) + { + self = _pthread_self (); + switch (mutex->attr->mutex_type) + { + case PTHREAD_MUTEX_NORMAL: + break; + + case PTHREAD_MUTEX_ERRORCHECK: + if (mutex->owner == self) + err = EDEADLK; + break; + + case PTHREAD_MUTEX_RECURSIVE: + if (mutex->owner == self) + { + mutex->locks ++; + err = 0; + } + break; + + default: + LOSE; + } + } + + return err; +} + +weak_alias (__pthread_mutex_trylock, _pthread_mutex_trylock); +weak_alias (__pthread_mutex_trylock, pthread_mutex_trylock); diff --git a/sysdeps/generic/pt-mutex-unlock.c b/sysdeps/generic/pt-mutex-unlock.c new file mode 100644 index 00000000..bbbe2b8f --- /dev/null +++ b/sysdeps/generic/pt-mutex-unlock.c @@ -0,0 +1,82 @@ +/* Unlock a mutex. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +#define LOSE do { * (int *) 0 = 0; } while (1) + +/* Unlock MUTEX, rescheduling a waiting thread. */ +int +__pthread_mutex_unlock (pthread_mutex_t *mutex) +{ + struct __pthread *wakeup; + + __pthread_spin_lock (&mutex->__lock); + + if (mutex->attr) + switch (mutex->attr->mutex_type) + { + case PTHREAD_MUTEX_NORMAL: + break; + + case PTHREAD_MUTEX_ERRORCHECK: + case PTHREAD_MUTEX_RECURSIVE: + if (mutex->owner != _pthread_self ()) + { + __pthread_spin_unlock (&mutex->__lock); + return EPERM; + } + + if (mutex->attr->mutex_type == PTHREAD_MUTEX_RECURSIVE) + if (--mutex->locks > 0) + { + __pthread_spin_unlock (&mutex->__lock); + return 0; + } + + mutex->owner = 0; + break; + + default: + LOSE; + } + + if (mutex->__queue == NULL) + { + __pthread_spin_unlock (&mutex->__held); + __pthread_spin_unlock (&mutex->__lock); + return 0; + } + + wakeup = mutex->__queue; + __pthread_dequeue (wakeup); + + /* We do not unlock MUTEX->held: we are transferring the ownership + to the thread that we are waking up. */ + + __pthread_spin_unlock (&mutex->__lock); + __pthread_wakeup (wakeup); + + return 0; +} + +weak_alias (__pthread_mutex_unlock, _pthread_mutex_unlock); +weak_alias (__pthread_mutex_unlock, pthread_mutex_unlock); diff --git a/sysdeps/generic/pt-mutexattr-destroy.c b/sysdeps/generic/pt-mutexattr-destroy.c new file mode 100644 index 00000000..fe7e95d2 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-destroy.c @@ -0,0 +1,27 @@ +/* pthread_mutexattr_destroy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_destroy (pthread_mutexattr_t *attr) +{ + return 0; +} diff --git a/sysdeps/generic/pt-mutexattr-getprioceiling.c b/sysdeps/generic/pt-mutexattr-getprioceiling.c new file mode 100644 index 00000000..d03d7192 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-getprioceiling.c @@ -0,0 +1,28 @@ +/* pthread_mutexattr_getprioceiling. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_getprioceiling (const pthread_mutexattr_t *attr, + int *prioceiling) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-mutexattr-getprotocol.c b/sysdeps/generic/pt-mutexattr-getprotocol.c new file mode 100644 index 00000000..d75021a3 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-getprotocol.c @@ -0,0 +1,29 @@ +/* pthread_mutexattr_getprotocol. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_getprotocol (const pthread_mutexattr_t *attr, + int *protocol) +{ + *protocol = attr->protocol; + return 0; +} diff --git a/sysdeps/generic/pt-mutexattr-getpshared.c b/sysdeps/generic/pt-mutexattr-getpshared.c new file mode 100644 index 00000000..0dad13b8 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-getpshared.c @@ -0,0 +1,29 @@ +/* pthread_mutexattr_getpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_getpshared (const pthread_mutexattr_t *attr, + int *pshared) +{ + *pshared = attr->pshared; + return 0; +} diff --git a/sysdeps/generic/pt-mutexattr-gettype.c b/sysdeps/generic/pt-mutexattr-gettype.c new file mode 100644 index 00000000..ff2caea4 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-gettype.c @@ -0,0 +1,28 @@ +/* pthread_mutexattr_gettype. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_gettype (const pthread_mutexattr_t *attr, int *type) +{ + *type = attr->mutex_type; + return 0; +} diff --git a/sysdeps/generic/pt-mutexattr-init.c b/sysdeps/generic/pt-mutexattr-init.c new file mode 100644 index 00000000..2f23673c --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-init.c @@ -0,0 +1,28 @@ +/* pthread_mutexattr_init. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_init (pthread_mutexattr_t *attr) +{ + *attr = __pthread_default_mutexattr; + return 0; +} diff --git a/sysdeps/generic/pt-mutexattr-setprioceiling.c b/sysdeps/generic/pt-mutexattr-setprioceiling.c new file mode 100644 index 00000000..a9bbd327 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-setprioceiling.c @@ -0,0 +1,28 @@ +/* pthread_mutexattr_setprioceiling. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_setprioceiling (pthread_mutexattr_t *attr, + int prioceiling) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-mutexattr-setprotocol.c b/sysdeps/generic/pt-mutexattr-setprotocol.c new file mode 100644 index 00000000..9a0d1b1f --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-setprotocol.c @@ -0,0 +1,42 @@ +/* pthread_mutexattr_setprotocol. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_setprotocol (pthread_mutexattr_t *attr, + int protocol) +{ + if (protocol == __pthread_default_mutexattr.protocol) + { + attr->protocol = protocol; + return 0; + } + + switch (protocol) + { + case PTHREAD_PRIO_NONE: + case PTHREAD_PRIO_INHERIT: + case PTHREAD_PRIO_PROTECT: + return ENOTSUP; + default: + return EINVAL; + } +} diff --git a/sysdeps/generic/pt-mutexattr-setpshared.c b/sysdeps/generic/pt-mutexattr-setpshared.c new file mode 100644 index 00000000..bcbc4d91 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-setpshared.c @@ -0,0 +1,39 @@ +/* pthread_mutexattr_setpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_setpshared (pthread_mutexattr_t *attr, + int pshared) +{ + switch (pshared) + { + case PTHREAD_PROCESS_PRIVATE: + attr->pshared = pshared; + return 0; + + case PTHREAD_PROCESS_SHARED: + return ENOTSUP; + + default: + return EINVAL; + } +} diff --git a/sysdeps/generic/pt-mutexattr-settype.c b/sysdeps/generic/pt-mutexattr-settype.c new file mode 100644 index 00000000..e7ae213a --- /dev/null +++ b/sysdeps/generic/pt-mutexattr-settype.c @@ -0,0 +1,37 @@ +/* pthread_mutexattr_settype. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_mutexattr_settype (pthread_mutexattr_t *attr, int type) +{ + switch (type) + { + case PTHREAD_MUTEX_NORMAL: + case PTHREAD_MUTEX_ERRORCHECK: + case PTHREAD_MUTEX_RECURSIVE: + attr->mutex_type = type; + return 0; + + default: + return EINVAL; + } +} diff --git a/sysdeps/generic/pt-mutexattr.c b/sysdeps/generic/pt-mutexattr.c new file mode 100644 index 00000000..647db241 --- /dev/null +++ b/sysdeps/generic/pt-mutexattr.c @@ -0,0 +1,29 @@ +/* Default mutex attributes. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +const struct __pthread_mutexattr __pthread_default_mutexattr = +{ + prioceiling: 0, + protocol: PTHREAD_PRIO_NONE, + pshared: PTHREAD_PROCESS_PRIVATE, + mutex_type: PTHREAD_MUTEX_DEFAULT +}; diff --git a/sysdeps/generic/pt-once.c b/sysdeps/generic/pt-once.c new file mode 100644 index 00000000..5be5e484 --- /dev/null +++ b/sysdeps/generic/pt-once.c @@ -0,0 +1,43 @@ +/* pthread_once. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <bits/memory.h> + +#include <pt-internal.h> + +int +pthread_once (pthread_once_t *once_control, void (*init_routine) (void)) +{ + if (once_control->run == 0) + { + __pthread_spin_lock (&once_control->lock); + + if (once_control->run == 0) + { + init_routine (); + __memory_barrier (); + once_control->run = 1; + } + + __pthread_spin_unlock (&once_control->lock); + } + + return 0; +} diff --git a/sysdeps/generic/pt-rwlock-attr.c b/sysdeps/generic/pt-rwlock-attr.c new file mode 100644 index 00000000..1587feef --- /dev/null +++ b/sysdeps/generic/pt-rwlock-attr.c @@ -0,0 +1,26 @@ +/* Default rwlock attributes. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +const struct __pthread_rwlockattr __pthread_default_rwlockattr = +{ + pshared: PTHREAD_PROCESS_PRIVATE +}; diff --git a/sysdeps/generic/pt-rwlock-destroy.c b/sysdeps/generic/pt-rwlock-destroy.c new file mode 100644 index 00000000..f63c84d9 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-destroy.c @@ -0,0 +1,29 @@ +/* Destroy a rwlock. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +_pthread_rwlock_destroy (pthread_rwlock_t *rwlock) +{ + return 0; +} + +weak_alias (_pthread_rwlock_destroy, pthread_rwlock_destroy); diff --git a/sysdeps/generic/pt-rwlock-init.c b/sysdeps/generic/pt-rwlock-init.c new file mode 100644 index 00000000..8fe87644 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-init.c @@ -0,0 +1,31 @@ +/* Initialize a rwlock. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +_pthread_rwlock_init (pthread_rwlock_t *rwlock, + const pthread_rwlockattr_t *attr) +{ + *rwlock = __PTHREAD_RWLOCK_INITIALIZER; + return 0; +} + +weak_alias (_pthread_rwlock_init, pthread_rwlock_init); diff --git a/sysdeps/generic/pt-rwlock-rdlock.c b/sysdeps/generic/pt-rwlock-rdlock.c new file mode 100644 index 00000000..22c11204 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-rdlock.c @@ -0,0 +1,32 @@ +/* Acquire a rwlock for reading. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +/* Implemented in pt-rwlock-timedrdlock.c. */ +extern int __pthread_rwlock_timedrdlock_internal (struct __pthread_mutex *mutex, + const struct timespec *abstime); + +/* Acquire RWLOCK for reading, block if we can't get it. */ +int +pthread_rwlock_rdlock (struct __pthread_rwlock *rwlock) +{ + return __pthread_rwlock_timedrdlock_internal (rwlock, 0); +} diff --git a/sysdeps/generic/pt-rwlock-timedrdlock.c b/sysdeps/generic/pt-rwlock-timedrdlock.c new file mode 100644 index 00000000..3660339b --- /dev/null +++ b/sysdeps/generic/pt-rwlock-timedrdlock.c @@ -0,0 +1,110 @@ +/* Acquire a rwlock for reading. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +int +pthread_rwlock_timedrdlock (struct __pthread_rwlock *rwlock, + const struct timespec *abstime) +{ + return __pthread_rwlock_timedrdlock_internal (rwlock, abstime); +} + +/* Acquire the rwlock *RWLOCK for reading blocking until *ABSTIME if + it is already held. As a GNU extension, if TIMESPEC is NULL then + wait forever. */ +int +__pthread_rwlock_timedrdlock_internal (struct __pthread_rwlock *rwlock, + const struct timespec *abstime) +{ + struct __pthread *self; + + __pthread_spin_lock (&rwlock->__lock); + if (__pthread_spin_trylock (&rwlock->__held) == 0) + /* Successfully acquired the lock. */ + { + assert (rwlock->readerqueue == 0); + assert (rwlock->writerqueue == 0); + assert (rwlock->readers == 0); + + rwlock->readers = 1; + __pthread_spin_unlock (&rwlock->__lock); + return 0; + } + else + /* Lock is held, but is held by a reader? */ + if (rwlock->readers > 0) + /* Just add ourself to number of readers. */ + { + assert (rwlock->readerqueue == 0); + rwlock->readers ++; + __pthread_spin_unlock (&rwlock->__lock); + return 0; + } + + /* The lock is busy. */ + + /* Better be blocked by a writer. */ + assert (rwlock->readers == 0); + + self = _pthread_self (); + + /* Add ourself to the queue. */ + __pthread_enqueue (&rwlock->readerqueue, self); + __pthread_spin_unlock (&rwlock->__lock); + + /* Block the thread. */ + if (abstime) + { + error_t err; + + err = __pthread_timedblock (self, abstime); + if (err) + /* We timed out. We may need to disconnect ourself from the + waiter queue. + + FIXME: What do we do if we get a wakeup message before we + disconnect ourself? It may remain until the next time we + block. */ + { + assert (err = ETIMEDOUT); + + __pthread_spin_lock (&rwlock->__lock); + if (self->prevp) + /* Disconnect ourself. */ + __pthread_dequeue (self); + __pthread_spin_unlock (&rwlock->__lock); + + return err; + } + } + else + __pthread_block (self); + + + /* The reader count has already been increment by whoever woke us + up. */ + + assert (rwlock->readers > 0); + + return 0; +} diff --git a/sysdeps/generic/pt-rwlock-timedwrlock.c b/sysdeps/generic/pt-rwlock-timedwrlock.c new file mode 100644 index 00000000..2f732175 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-timedwrlock.c @@ -0,0 +1,92 @@ +/* Acquire a rwlock for writing. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +int +pthread_rwlock_timedwrlock (struct __pthread_rwlock *rwlock, + const struct timespec *abstime) +{ + return __pthread_rwlock_timedwrlock_internal (rwlock, abstime); +} + +/* Acquire RWLOCK for writing blocking until *ABSTIME if we cannot get + it. As a special GNU extension, if ABSTIME is NULL then the wait + shall not time out. */ +int +__pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock, + const struct timespec *abstime) +{ + struct __pthread *self; + + __pthread_spin_lock (&rwlock->__lock); + if (__pthread_spin_trylock (&rwlock->__held) == 0) + /* Successfully acquired the lock. */ + { + assert (rwlock->readerqueue == 0); + assert (rwlock->writerqueue == 0); + assert (rwlock->readers == 0); + + __pthread_spin_unlock (&rwlock->__lock); + return 0; + } + + /* The lock is busy. */ + + self = _pthread_self (); + + /* Add ourselves to the queue. */ + __pthread_enqueue (&rwlock->writerqueue, self); + __pthread_spin_unlock (&rwlock->__lock); + + /* Block the thread. */ + if (abstime) + { + error_t err; + + err = __pthread_timedblock (self, abstime); + if (err) + /* We timed out. We may need to disconnect ourself from the + waiter queue. + + FIXME: What do we do if we get a wakeup message before we + disconnect ourself? It may remain until the next time we + block. */ + { + assert (err = ETIMEDOUT); + + __pthread_spin_lock (&rwlock->__lock); + if (self->prevp) + /* Disconnect ourself. */ + __pthread_dequeue (self); + __pthread_spin_unlock (&rwlock->__lock); + + return err; + } + } + else + __pthread_block (self); + + assert (rwlock->readers == 0); + + return 0; +} diff --git a/sysdeps/generic/pt-rwlock-tryrdlock.c b/sysdeps/generic/pt-rwlock-tryrdlock.c new file mode 100644 index 00000000..2419efb1 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-tryrdlock.c @@ -0,0 +1,56 @@ +/* Try to acquire a rwlock for reading. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +/* Try to acquire RWLOCK. */ +int +pthread_rwlock_tryrdlock (struct __pthread_rwlock *rwlock) +{ + __pthread_spin_lock (&rwlock->__lock); + if (__pthread_spin_trylock (&rwlock->__held) == 0) + /* Successfully acquired the lock. */ + { + assert (rwlock->readerqueue == 0); + assert (rwlock->writerqueue == 0); + assert (rwlock->readers == 0); + + rwlock->readers = 1; + __pthread_spin_unlock (&rwlock->__lock); + return 0; + } + else + /* Lock is held, but is held by a reader? */ + if (rwlock->readers > 0) + { + assert (rwlock->readerqueue == 0); + rwlock->readers ++; + __pthread_spin_unlock (&rwlock->__lock); + return 0; + } + + /* The lock is busy. */ + + __pthread_spin_unlock (&rwlock->__lock); + + return EBUSY; +} diff --git a/sysdeps/generic/pt-rwlock-trywrlock.c b/sysdeps/generic/pt-rwlock-trywrlock.c new file mode 100644 index 00000000..6b9c5b56 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-trywrlock.c @@ -0,0 +1,46 @@ +/* Try to acquire a rwlock for writing. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +/* Try to acquire RWLOCK for writing. */ +int +pthread_rwlock_trywrlock (struct __pthread_rwlock *rwlock) +{ + __pthread_spin_lock (&rwlock->__lock); + if (__pthread_spin_trylock (&rwlock->__held) == 0) + /* Successfully acquired the lock. */ + { + assert (rwlock->readerqueue == 0); + assert (rwlock->writerqueue == 0); + assert (rwlock->readers == 0); + + __pthread_spin_unlock (&rwlock->__lock); + return 0; + } + + /* The lock is busy. */ + + __pthread_spin_unlock (&rwlock->__lock); + + return EBUSY; +} diff --git a/sysdeps/generic/pt-rwlock-unlock.c b/sysdeps/generic/pt-rwlock-unlock.c new file mode 100644 index 00000000..fb23a0b9 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-unlock.c @@ -0,0 +1,90 @@ +/* Unlock a rwlock. Generic version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +/* Unlock *RWLOCK, rescheduling a waiting writer thread or, if there + are no threads waiting for a write lock, rescheduling the reader + threads. */ +int +pthread_rwlock_unlock (pthread_rwlock_t *rwlock) +{ + struct __pthread *wakeup; + + __pthread_spin_lock (&rwlock->__lock); + + assert (__pthread_spin_trylock (&rwlock->__held) == EBUSY); + + if (rwlock->readers > 1) + /* There are other readers. */ + { + rwlock->readers --; + __pthread_spin_unlock (&rwlock->__lock); + return 0; + } + + if (rwlock->readers == 1) + /* Last reader. */ + rwlock->readers = 0; + + + /* Wake someone else up. Try the writer queue first, then the + reader queue if that is empty. */ + + if (rwlock->writerqueue) + { + wakeup = rwlock->writerqueue; + __pthread_dequeue (wakeup); + + /* We do not unlock RWLOCK->held: we are transferring the ownership + to the thread that we are waking up. */ + + __pthread_spin_unlock (&rwlock->__lock); + __pthread_wakeup (wakeup); + + return 0; + } + + if (rwlock->readerqueue) + { + __pthread_queue_iterate (rwlock->readerqueue, wakeup) + rwlock->readers ++; + + wakeup = rwlock->readerqueue; + rwlock->readerqueue = 0; + + __pthread_spin_unlock (&rwlock->__lock); + + /* We can safely walk the list of waiting threads without holding + the lock since it is now decoupled from the rwlock. */ + __pthread_dequeuing_iterate (wakeup, wakeup) + __pthread_wakeup (wakeup); + + return 0; + } + + + /* Noone is waiting. Just unlock it. */ + + __pthread_spin_unlock (&rwlock->__held); + __pthread_spin_unlock (&rwlock->__lock); + return 0; +} diff --git a/sysdeps/generic/pt-rwlock-wrlock.c b/sysdeps/generic/pt-rwlock-wrlock.c new file mode 100644 index 00000000..be85b906 --- /dev/null +++ b/sysdeps/generic/pt-rwlock-wrlock.c @@ -0,0 +1,34 @@ +/* Acquire a rwlock for writing. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> + +#include <pt-internal.h> + +/* Implemented in pt-rwlock-timedwrlock.c. */ +extern int __pthread_rwlock_timedwrlock_internal (struct __pthread_rwlock *rwlock, + const struct timespec *abstime); + +/* Acquire RWLOCK for writing. */ +int +pthread_rwlock_wrlock (struct __pthread_rwlock *rwlock) +{ + return __pthread_rwlock_timedwrlock_internal (rwlock, 0); +} diff --git a/sysdeps/generic/pt-rwlockattr-destroy.c b/sysdeps/generic/pt-rwlockattr-destroy.c new file mode 100644 index 00000000..eabfcbe8 --- /dev/null +++ b/sysdeps/generic/pt-rwlockattr-destroy.c @@ -0,0 +1,27 @@ +/* pthread_rwlockattr_destroy. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_rwlockattr_destroy (pthread_rwlockattr_t *attr) +{ + return 0; +} diff --git a/sysdeps/generic/pt-rwlockattr-getpshared.c b/sysdeps/generic/pt-rwlockattr-getpshared.c new file mode 100644 index 00000000..e8752c0c --- /dev/null +++ b/sysdeps/generic/pt-rwlockattr-getpshared.c @@ -0,0 +1,29 @@ +/* pthread_rwlockattr_getpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_rwlockattr_getpshared (const pthread_rwlockattr_t *attr, + int *pshared) +{ + *pshared = attr->pshared; + return 0; +} diff --git a/sysdeps/generic/pt-rwlockattr-init.c b/sysdeps/generic/pt-rwlockattr-init.c new file mode 100644 index 00000000..34da6bf0 --- /dev/null +++ b/sysdeps/generic/pt-rwlockattr-init.c @@ -0,0 +1,28 @@ +/* pthread_rwlockattr_init. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_rwlockattr_init (pthread_rwlockattr_t *attr) +{ + *attr = __pthread_default_rwlockattr; + return 0; +} diff --git a/sysdeps/generic/pt-rwlockattr-setpshared.c b/sysdeps/generic/pt-rwlockattr-setpshared.c new file mode 100644 index 00000000..a861d034 --- /dev/null +++ b/sysdeps/generic/pt-rwlockattr-setpshared.c @@ -0,0 +1,39 @@ +/* pthread_rwlockattr_setpshared. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, + int pshared) +{ + switch (pshared) + { + case PTHREAD_PROCESS_PRIVATE: + attr->pshared = pshared; + return 0; + + case PTHREAD_PROCESS_SHARED: + return ENOTSUP; + + default: + return EINVAL; + } +} diff --git a/sysdeps/generic/pt-setconcurrency.c b/sysdeps/generic/pt-setconcurrency.c new file mode 100644 index 00000000..cd55b1ba --- /dev/null +++ b/sysdeps/generic/pt-setconcurrency.c @@ -0,0 +1,34 @@ +/* Set the desired level of concurrency. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int __pthread_concurrency; + +int +pthread_setconcurrency (int new_level) +{ + if (new_level < 0) + return EINVAL; + + __pthread_concurrency = new_level; + + return 0; +} diff --git a/sysdeps/generic/pt-setschedparam.c b/sysdeps/generic/pt-setschedparam.c new file mode 100644 index 00000000..6b624a31 --- /dev/null +++ b/sysdeps/generic/pt-setschedparam.c @@ -0,0 +1,28 @@ +/* Set the scheduling parameters for a thread. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_setschedparam (pthread_t thread, int *policy, + const struct sched_param *param) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-setschedprio.c b/sysdeps/generic/pt-setschedprio.c new file mode 100644 index 00000000..60668eb1 --- /dev/null +++ b/sysdeps/generic/pt-setschedprio.c @@ -0,0 +1,27 @@ +/* Set the scheduling priority of a thread. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_setschedprio (pthread_t thread, int prio) +{ + return ENOSYS; +} diff --git a/sysdeps/generic/pt-setspecific.c b/sysdeps/generic/pt-setspecific.c new file mode 100644 index 00000000..d520c5df --- /dev/null +++ b/sysdeps/generic/pt-setspecific.c @@ -0,0 +1,27 @@ +/* pthread_setspecific. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +int +pthread_setspecific (pthread_key_t key, const void *value) +{ + return EINVAL; +} diff --git a/sysdeps/hurd/pt-destroy-specific.c b/sysdeps/hurd/pt-destroy-specific.c new file mode 100644 index 00000000..82382228 --- /dev/null +++ b/sysdeps/hurd/pt-destroy-specific.c @@ -0,0 +1,77 @@ +/* __pthread_destory_specific. Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <stdlib.h> +#include <hurd/ihash.h> + +#include <pt-internal.h> + +void +__pthread_destroy_specific (struct __pthread *thread) +{ + error_t err; + int i; + int seen_one; + + /* Check if there is any thread specific data. */ + if (! thread->thread_specifics) + return; + + __pthread_key_lock_ready (); + + /* Iterate and call the destructors on any thread specific data. */ + for (;;) + { + seen_one = 0; + + __pthread_mutex_lock (&__pthread_key_lock); + + for (i = 0; i < __pthread_key_count; i ++) + { + void *value; + + if (__pthread_key_destructors[i] == PTHREAD_KEY_INVALID) + break; + + value = ihash_find (thread->thread_specifics, i); + if (value) + { + seen_one = 1; + + err = ihash_remove (thread->thread_specifics, i); + assert (err == 1); + + __pthread_key_destructors[i] (value); + } + } + + __pthread_mutex_unlock (&__pthread_key_lock); + + if (! seen_one) + break; + + /* This may take a very long time. Let those blocking on + pthread_key_create or pthread_key_delete make progress. */ + sched_yield (); + } + + ihash_free (thread->thread_specifics); + thread->thread_specifics = 0; +} diff --git a/sysdeps/hurd/pt-getspecific.c b/sysdeps/hurd/pt-getspecific.c new file mode 100644 index 00000000..b5b5f5e5 --- /dev/null +++ b/sysdeps/hurd/pt-getspecific.c @@ -0,0 +1,37 @@ +/* pthread_getspecific. Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <hurd/ihash.h> + +#include <pt-internal.h> + +void * +pthread_getspecific (pthread_key_t key) +{ + struct __pthread *self; + + assert (key < __pthread_key_count); + + self = _pthread_self (); + if (! self->thread_specifics) + return 0; + + return ihash_find (self->thread_specifics, key); +} diff --git a/sysdeps/hurd/pt-init-specific.c b/sysdeps/hurd/pt-init-specific.c new file mode 100644 index 00000000..c1bacbcb --- /dev/null +++ b/sysdeps/hurd/pt-init-specific.c @@ -0,0 +1,30 @@ +/* __pthread_init_specific. Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <stdlib.h> + +#include <pt-internal.h> + +error_t +__pthread_init_specific (struct __pthread *thread) +{ + thread->thread_specifics = 0; + return 0; +} diff --git a/sysdeps/hurd/pt-key-create.c b/sysdeps/hurd/pt-key-create.c new file mode 100644 index 00000000..b3e01412 --- /dev/null +++ b/sysdeps/hurd/pt-key-create.c @@ -0,0 +1,109 @@ +/* pthread_key_create. Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <stdlib.h> +#include <assert.h> + +#include <pt-internal.h> + +pthread_mutex_t __pthread_key_lock; + +void (**__pthread_key_destructors) (void *arg); +int __pthread_key_size; +int __pthread_key_count; +int __pthread_key_invalid_count; + +int +pthread_key_create (pthread_key_t *key, void (*destructor) (void *)) +{ + /* Where to look for the next key slot. */ + static int index; + + __pthread_key_lock_ready (); + + __pthread_mutex_lock (&__pthread_key_lock); + + do_search: + /* Use the search hint and try to find a free slot. */ + for (; index < __pthread_key_count + && __pthread_key_destructors[index] != PTHREAD_KEY_INVALID; + index ++) + ; + + /* See if we actually found a free element. */ + if (index < __pthread_key_count) + { + assert (__pthread_key_destructors[index] == PTHREAD_KEY_INVALID); + assert (__pthread_key_invalid_count > 0); + + __pthread_key_invalid_count --; + __pthread_key_destructors[index] = destructor; + *key = index ++; + + __pthread_mutex_unlock (&__pthread_key_lock); + return 0; + } + + assert (index == __pthread_key_count); + + /* No space at the end. */ + if (__pthread_key_size == __pthread_key_count) + { + /* See if it is worth looking for a free element. */ + if (__pthread_key_invalid_count > 4 + && __pthread_key_invalid_count > __pthread_key_size / 8) + { + index = 0; + goto do_search; + } + + + /* Resize the array. */ + { + void *t; + int newsize; + + if (__pthread_key_size == 0) + newsize = 8; + else + newsize = __pthread_key_size * 2; + + t = realloc (__pthread_key_destructors, + newsize * sizeof (*__pthread_key_destructors)); + if (! t) + { + __pthread_mutex_unlock (&__pthread_key_lock); + return ENOMEM; + } + + __pthread_key_size = newsize; + __pthread_key_destructors = t; + } + } + + __pthread_key_destructors[index] = destructor; + *key = index; + + index ++; + __pthread_key_count ++; + + __pthread_mutex_unlock (&__pthread_key_lock); + return 0; +} diff --git a/sysdeps/hurd/pt-key-delete.c b/sysdeps/hurd/pt-key-delete.c new file mode 100644 index 00000000..7c1af715 --- /dev/null +++ b/sysdeps/hurd/pt-key-delete.c @@ -0,0 +1,45 @@ +/* pthread_key_delete. Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +int +pthread_key_destory (pthread_key_t key) +{ + error_t err = 0; + + __pthread_key_lock_ready (); + + __pthread_mutex_lock (&__pthread_key_lock); + + if (key < 0 || key >= __pthread_key_count + || __pthread_key_destructors[key] == PTHREAD_KEY_INVALID) + err = EINVAL; + else + { + __pthread_key_destructors[key] = PTHREAD_KEY_INVALID; + __pthread_key_invalid_count ++; + } + + __pthread_mutex_unlock (&__pthread_key_lock); + + return err; +} diff --git a/sysdeps/hurd/pt-key.h b/sysdeps/hurd/pt-key.h new file mode 100644 index 00000000..739fbbad --- /dev/null +++ b/sysdeps/hurd/pt-key.h @@ -0,0 +1,76 @@ +/* pthread_key internal declatations for the Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <hurd/ihash.h> + +#define PTHREAD_KEY_MEMBERS \ + ihash_t thread_specifics; + +#define PTHREAD_KEY_INVALID (void *) (-1) + + +/* __PTHREAD_KEY_DESTRUCTORS is an array of destructors with + __PTHREAD_KEY_SIZE elements. If an element with index less than + __PTHREAD_KEY_COUNT is invalid, it shall contain the value + PTHREAD_KEY_INVALID which shall be distinct from NULL. + + Normally, we just add new keys to the end of the array and realloc + it as necessary. The pthread_key_create routine may decide to + rescan the array if __PTHREAD_KEY_FREE is large. */ +extern void (**__pthread_key_destructors) (void *arg); +extern int __pthread_key_size; +extern int __pthread_key_count; +/* Number of invalid elements in the array. Does not include elements + for which memory has been allocated but which have not yet been + used (i.e. those elements with indexes greater than + __PTHREAD_KEY_COUNT). */ +extern int __pthread_key_invalid_count; + +/* Protects the above variables. This must be a recursive lock: the + destructors may call pthread_key_delete. */ +extern pthread_mutex_t __pthread_key_lock; + +#include <assert.h> + +static inline void +__pthread_key_lock_ready (void) +{ + static pthread_once_t o = PTHREAD_ONCE_INIT; + + void do_init (void) + { + int err; + pthread_mutexattr_t attr; + + err = pthread_mutexattr_init (&attr); + assert_perror (err); + + err = pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); + assert_perror (err); + + err = pthread_mutex_init (&__pthread_key_lock, &attr); + assert_perror (err); + + err = pthread_mutexattr_destroy (&attr); + assert_perror (err); + } + + pthread_once (&o, do_init); +} diff --git a/sysdeps/hurd/pt-kill.c b/sysdeps/hurd/pt-kill.c new file mode 100644 index 00000000..f970e065 --- /dev/null +++ b/sysdeps/hurd/pt-kill.c @@ -0,0 +1,49 @@ +/* pthread_kill. Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> +#include <signal.h> +#include <hurd/signal.h> + +#include <pt-internal.h> + +int +pthread_kill (pthread_t thread, int sig) +{ + struct __pthread *pthread; + struct hurd_signal_detail detail; + struct hurd_sigstate *ss; + + /* Lookup the thread structure for THREAD. */ + pthread = __pthread_getid (thread); + if (pthread == NULL) + return ESRCH; + + ss = _hurd_thread_sigstate (pthread->kernel_thread); + assert (ss); + + detail.exc = 0; + detail.code = sig; + detail.error = 0; + + _hurd_raise_signal (ss, sig, &detail); + + return 0; +} diff --git a/sysdeps/hurd/pt-setspecific.c b/sysdeps/hurd/pt-setspecific.c new file mode 100644 index 00000000..bd752250 --- /dev/null +++ b/sysdeps/hurd/pt-setspecific.c @@ -0,0 +1,43 @@ +/* pthread_setspecific. Generic version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <hurd/ihash.h> + +#include <pt-internal.h> + +int +pthread_setspecific (pthread_key_t key, const void *value) +{ + error_t err; + struct __pthread *self = _pthread_self (); + + if (! self->thread_specifics) + { + err = ihash_create (&self->thread_specifics); + if (err) + return ENOMEM; + } + + err = ihash_add (self->thread_specifics, key, (void *) value, 0); + if (err) + return ENOMEM; + + return 0; +} diff --git a/sysdeps/i386/bits/atomic.h b/sysdeps/i386/bits/atomic.h new file mode 100644 index 00000000..0dfc1f60 --- /dev/null +++ b/sysdeps/i386/bits/atomic.h @@ -0,0 +1,66 @@ +/* Atomic operations. i386 version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_ATOMIC_H +#define _BITS_ATOMIC_H 1 + +typedef __volatile int __atomic_t; + +static inline void +__atomic_inc (__atomic_t *__var) +{ + __asm__ __volatile ("lock; incl %0" : "=m" (*__var) : "m" (*__var)); +} + +static inline void +__atomic_dec (__atomic_t *__var) +{ + __asm__ __volatile ("lock; decl %0" : "=m" (*__var) : "m" (*__var)); +} + +static inline int +__atomic_dec_and_test (__atomic_t *__var) +{ + unsigned char __ret; + + __asm__ __volatile ("lock; decl %0; sete %1" + : "=m" (*__var), "=qm" (__ret) : "m" (*__var)); + return __ret != 0; +} + +/* We assume that an __atomicptr_t is only used for pointers to + word-aligned objects, and use the lowest bit for a simple lock. */ +typedef __volatile int * __atomicptr_t; + +/* Actually we don't implement that yet, and assume that we run on + something that has the i486 instruction set. */ +static inline int +__atomicptr_compare_and_swap (__atomicptr_t *__ptr, void *__oldval, + void * __newval) +{ + char __ret; + int __dummy; + + __asm__ __volatile ("lock; cmpxchgl %3, %1; sete %0" + : "=q" (__ret), "=m" (*__ptr), "=a" (__dummy) + : "r" (__newval), "m" (*__ptr), "a" (__oldval)); + return __ret; +} + +#endif diff --git a/sysdeps/i386/bits/memory.h b/sysdeps/i386/bits/memory.h new file mode 100644 index 00000000..201305b9 --- /dev/null +++ b/sysdeps/i386/bits/memory.h @@ -0,0 +1,40 @@ +/* Memory barrier operations. i386 version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _BITS_MEMORY_H +#define _BITS_MEMORY_H 1 + +/* Prevent read and write reordering across this function. */ +inline void +__memory_barrier (void) +{ + int i; + + /* Any lock'ed instruction will do. We just do a simple + increment. */ + __asm__ __volatile ("lock; incl %0" : "=m" (i) : "m" (i)); +} + +/* Prevent read reordering across this function. */ +#define __memory_read_barrier __memory_barrier + +/* Prevent write reordering across this function. */ +#define __memory_write_barrier __memory_barrier + +#endif diff --git a/sysdeps/i386/bits/spin-lock.h b/sysdeps/i386/bits/spin-lock.h new file mode 100644 index 00000000..175656ef --- /dev/null +++ b/sysdeps/i386/bits/spin-lock.h @@ -0,0 +1,103 @@ +/* Machine-specific definitions for spin locks. i386 version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +/* + * Never include this file directly; use <pthread.h> or <cthreads.h> instead. + */ + +#ifndef _BITS_SPIN_LOCK_H +#define _BITS_SPIN_LOCK_H 1 + +#include <features.h> + +__BEGIN_DECLS + +/* The type of a spin lock object. */ +typedef __volatile int __pthread_spinlock_t; + +/* Initializer for a spin lock object. */ +# define __SPIN_LOCK_INITIALIZER ((__pthread_spinlock_t) 0) + +#if defined __USE_EXTERN_INLINES || defined _FORCE_INLINES + +# ifndef __EBUSY +# include <errno.h> +# define __EBUSY EBUSY +# endif + +# ifndef __PT_SPIN_INLINE +# define __PT_SPIN_INLINE extern __inline +# endif + +__PT_SPIN_INLINE int __pthread_spin_destroy (__pthread_spinlock_t *__lock); + +__PT_SPIN_INLINE int +__pthread_spin_destroy (__pthread_spinlock_t *__lock) +{ + return 0; +} + +__PT_SPIN_INLINE int __pthread_spin_init (__pthread_spinlock_t *__lock, + int __pshared); + +__PT_SPIN_INLINE int +__pthread_spin_init (__pthread_spinlock_t *__lock, int __pshared) +{ + *__lock = __SPIN_LOCK_INITIALIZER; + return 0; +} + +__PT_SPIN_INLINE int __pthread_spin_trylock (__pthread_spinlock_t *__lock); + +__PT_SPIN_INLINE int +__pthread_spin_trylock (__pthread_spinlock_t *__lock) +{ + int __locked; + __asm__ __volatile ("xchgl %0, %1" + : "=&r" (__locked), "=m" (*__lock) : "0" (1)); + return __locked ? __EBUSY : 0; +} + +extern inline int __pthread_spin_lock (__pthread_spinlock_t *__lock); +extern int _pthread_spin_lock (__pthread_spinlock_t *__lock); + +extern inline int +__pthread_spin_lock (__pthread_spinlock_t *__lock) +{ + if (__pthread_spin_trylock (__lock)) + return _pthread_spin_lock (__lock); + return 0; +} + +__PT_SPIN_INLINE int __pthread_spin_unlock (__pthread_spinlock_t *__lock); + +__PT_SPIN_INLINE int +__pthread_spin_unlock (__pthread_spinlock_t *__lock) +{ + int __unlocked; + __asm__ __volatile ("xchgl %0, %1" + : "=&r" (__unlocked), "=m" (*__lock) : "0" (0)); + return 0; +} + +#endif /* Use extern inlines or force inlines. */ + +__END_DECLS + +#endif /* bits/spin-lock.h */ diff --git a/sysdeps/i386/machine-sp.h b/sysdeps/i386/machine-sp.h new file mode 100644 index 00000000..536f6902 --- /dev/null +++ b/sysdeps/i386/machine-sp.h @@ -0,0 +1,31 @@ +/* Machine-specific function to return the stack pointer. i386 version. + Copyright (C) 1994,97,2001 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef _MACHINE_SP_H +#define _MACHINE_SP_H + +/* Return the current stack pointer. */ + +#define __thread_stack_pointer() ({ \ + void *__sp__; \ + __asm__ ("movl %%esp, %0" : "=r" (__sp__)); \ + __sp__; \ +}) + +#endif /* machine-sp.h */ diff --git a/sysdeps/i386/pt-machdep.h b/sysdeps/i386/pt-machdep.h new file mode 100644 index 00000000..6d456367 --- /dev/null +++ b/sysdeps/i386/pt-machdep.h @@ -0,0 +1,29 @@ +/* Machine dependent pthreads internal defenitions. i386 version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _PT_MACHDEP_H +#define _PT_MACHDEP_H 1 + +struct pthread_mcontext +{ + void *pc; + void *sp; +}; + +#endif /* pt-machdep.h */ diff --git a/sysdeps/l4/hurd/i386/pt-machdep.c b/sysdeps/l4/hurd/i386/pt-machdep.c new file mode 100644 index 00000000..dbf5cd7e --- /dev/null +++ b/sysdeps/l4/hurd/i386/pt-machdep.c @@ -0,0 +1,20 @@ +/* Machine dependent pthreads code. Hurd/i386 version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +/* Nothing to do. */ diff --git a/sysdeps/l4/hurd/i386/pt-setup.c b/sysdeps/l4/hurd/i386/pt-setup.c new file mode 100644 index 00000000..fe77d533 --- /dev/null +++ b/sysdeps/l4/hurd/i386/pt-setup.c @@ -0,0 +1,72 @@ +/* Setup thread stack. Hurd/i386 version. + Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <l4/l4.h> + +#include <pt-internal.h> + +/* The stack layout used on the i386 is: + + ----------------- + | ARG | + ----------------- + | START_ROUTINE | + ----------------- + | 0 | + ----------------- */ + +/* Set up the stack for THREAD, such that it appears as if + START_ROUTINE and ARG were passed to the new thread's entry-point. + Return the stack pointer for the new thread. We also take the + opportunity to install THREAD in our utcb. */ +static void * +stack_setup (struct __pthread *thread, + void *(*start_routine)(void *), void *arg) +{ + L4_Word_t *top; + + /* Calculate top of the new stack. */ + top = (L4_Word_t *) ((L4_Word_t) thread->stackaddr + thread->stacksize); + + if (start_routine) + { + /* Set up call frame. */ + *--top = (L4_Word_t) arg; /* Argument to START_ROUTINE. */ + *--top = (L4_Word_t) start_routine; + *--top = 0; /* Fake return address. */ + } + + return top; +} + +int +__pthread_setup (struct __pthread *thread, + void (*entry_point)(void *(*)(void *), void *), + void *(*start_routine)(void *), void *arg) +{ + thread->mcontext.pc = entry_point; + thread->mcontext.sp = stack_setup (thread, start_routine, arg); + + if (L4_SameThreads (thread->threadid, L4_Myself ())) + L4_Set_MyUserDefinedHandle (thread); + else + L4_Set_UserDefinedHandle (thread->threadid, thread); + + return 0; +} diff --git a/sysdeps/l4/hurd/pt-sysdep.c b/sysdeps/l4/hurd/pt-sysdep.c new file mode 100644 index 00000000..265592ca --- /dev/null +++ b/sysdeps/l4/hurd/pt-sysdep.c @@ -0,0 +1,58 @@ +/* System dependent pthreads code. Hurd version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <stddef.h> +#include <stdint.h> + +#include <pt-internal.h> + +/* Forward. */ +static void *init_routine (void); + +/* OK, the name of this variable isn't really appropriate, but I don't + want to change it yet. */ +void *(*_pthread_init_routine)(void) = &init_routine; + +/* This function is called from the Hurd-specific startup code. It + should return a new stack pointer for the main thread. The caller + will switch to this new stack before doing anything serious. */ +static void * +init_routine (void) +{ + struct __pthread *thread; + int err; + + /* Initialize the library. */ + __pthread_initialize (); + + /* Create the pthread structure for the main thread (i.e. us). */ + err = __pthread_create_internal (&thread, 0, 0, 0); + assert_perror (err); + + __pthread_initialize (); + + /* Decrease the number of threads, to take into account that the + signal thread (which will be created by the startup code when we + return from here) shouldn't be seen as a user thread. */ +#warning Need to implement the signal thread. + // __pthread_total--; + + return (void *) thread->mcontext.sp; +} diff --git a/sysdeps/l4/hurd/pt-sysdep.h b/sysdeps/l4/hurd/pt-sysdep.h new file mode 100644 index 00000000..e37beefa --- /dev/null +++ b/sysdeps/l4/hurd/pt-sysdep.h @@ -0,0 +1,50 @@ +/* Internal defenitions for pthreads library. + Copyright (C) 2000, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _PT_SYSDEP_H +#define _PT_SYSDEP_H 1 + +#include <l4/l4.h> +#include <task_client.h> +#include <machine/vmparam.h> + +/* XXX */ +#define _POSIX_THREAD_THREADS_MAX 64 + +/* The default stack size. */ +#define PTHREAD_STACK_DEFAULT (PAGE_SIZE) + +#define PTHREAD_SYSDEP_MEMBERS \ + L4_ThreadId_t threadid; \ + L4_Word_t my_errno; + +extern inline struct __pthread * +_pthread_self (void) +{ + return (struct __pthread *) L4_MyUserDefinedHandle (); +} + +extern inline void +__pthread_stack_dealloc (void *stackaddr, size_t stacksize) +{ + /* XXX: can only implement this once we have a working memory manager. */ + return; +} + +#endif /* pt-sysdep.h */ diff --git a/sysdeps/l4/pt-block.c b/sysdeps/l4/pt-block.c new file mode 100644 index 00000000..050c6947 --- /dev/null +++ b/sysdeps/l4/pt-block.c @@ -0,0 +1,29 @@ +/* Block a thread. L4 version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <l4/l4.h> + +#include <pt-internal.h> + +/* Block THREAD. */ +void +__pthread_block (struct __pthread *thread) +{ + L4_Receive (L4_anylocalthread); +} diff --git a/sysdeps/l4/pt-docancel.c b/sysdeps/l4/pt-docancel.c new file mode 100644 index 00000000..9a3bb26c --- /dev/null +++ b/sysdeps/l4/pt-docancel.c @@ -0,0 +1,51 @@ +/* Cancel a thread. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +static void +call_exit (void) +{ + pthread_exit (0); +} + +int +__pthread_do_cancel (struct __pthread *p) +{ + assert (p->cancel_pending = 1); + assert (p->cancel_state == PTHREAD_CANCEL_ENABLE); + + if (L4_SameThreads (L4_Myself (), p->threadid)) + call_exit (); + else + { + L4_Word_t dummy; + L4_ThreadId_t dummy_id; + + /* Change the ip of the target thread to make it exit. */ + L4_ExchangeRegisters (p->threadid, (1 << 4), 0, call_exit, + 0, 0, L4_nilthread, + &dummy, &dummy, &dummy, &dummy, &dummy, + &dummy_id); + } + + return 0; +} diff --git a/sysdeps/l4/pt-stack-alloc.c b/sysdeps/l4/pt-stack-alloc.c new file mode 100644 index 00000000..e28d5310 --- /dev/null +++ b/sysdeps/l4/pt-stack-alloc.c @@ -0,0 +1,70 @@ +/* Allocate a new stack. L4 Hurd version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <l4/l4.h> +#include <errno.h> + +#include <pt-internal.h> + +#define __pthread_stacksize __pthread_default_attr.stacksize + +#include <l4/sigma0.h> +#include <hurd/debug.h> + +static void * +allocate_page (void) +{ + L4_Fpage_t p; + /* The Kernel Interface page. */ + static L4_KernelInterfacePage_t *kip; + + if (! kip) + kip = L4_GetKernelInterface (); + +#define sigma0_tid() (L4_GlobalId (kip->ThreadInfo.X.UserBase, 1)) + p = L4_Sigma0_GetPage (sigma0_tid (), + L4_Fpage_Set_Attrs (L4_FpageLog2 (-1UL << 10, + PAGE_SHIFT), + L4_FullyAccessible)); + p.raw &= ~0x3ff; + + printf ("%s: Allocated page %x\n", + __FUNCTION__, p.raw); + + return (void *) p.raw; +} + + +/* Allocate a new stack of size STACKSIZE. If successfull, store the + address of the newly allocated stack in *STACKADDR and return 0. + Otherwise return an error code (EINVAL for an invalid stack size, + EAGAIN if the system lacked the necessary resources to allocate a + new stack). */ +int +__pthread_stack_alloc (void **stackaddr, size_t stacksize) +{ + if (stacksize != __pthread_stacksize) + return EINVAL; + + *stackaddr = allocate_page (); + if (! *stackaddr) + return EAGAIN; + + return 0; +} diff --git a/sysdeps/l4/pt-start.c b/sysdeps/l4/pt-start.c new file mode 100644 index 00000000..5a506998 --- /dev/null +++ b/sysdeps/l4/pt-start.c @@ -0,0 +1,103 @@ +/* Start thread. L4 Hurd version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <string.h> + +#include <pt-internal.h> + +#include "task_client.h" + +extern L4_ThreadId_t __system_pager; +extern L4_ThreadId_t __task_server; + +#ifndef WORKING_EXREGS +static void +send_startup_ipc (L4_ThreadId_t id, L4_Word_t ip, L4_Word_t sp) +{ + L4_Msg_t msg; + + printf ("%s: Sending startup message to %x, " + "(ip=%x, sp=%x)\n", + __FUNCTION__, * (L4_Word_t *) &id, ip, sp); + + L4_Clear (&msg); +#ifdef HAVE_PROPAGATION + L4_Set_VirtualSender (pager_tid); + L4_Set_Propagation (&msg.tag); +#endif + L4_Append_Word (&msg, ip); + L4_Append_Word (&msg, sp); +#ifndef HAVE_PROPAGATION + L4_Append_Word (&msg, *(L4_Word_t *) &id); + id = __system_pager; +#if 0 + DODEBUG (2, printf ("%s: Redirecting start request to pager (%x).\n", + __FUNCTION__, * (L4_Word_t *) &id)); +#endif +#endif + L4_LoadMsg (&msg); + L4_Send (id); +} +#endif + +/* Start THREAD. We allocate all system-specific resources, including + a kernel thread, set it up, and get it running. */ +int +__pthread_start (struct __pthread *thread) +{ + error_t err; + + if (__pthread_num_threads == 1) + /* The main thread is already running: do nothing. */ + { + assert (__pthread_total == 1); + thread->threadid = __mach_thread_self (); + } + else + { + CORBA_Environment env; + + env = idl4_default_environment; + err = thread_create (__task_server, + L4_Version (L4_Myself ()), + * (L4_Word_t *) &__system_pager, + (L4_Word_t *) &thread->threadid, &env); + if (err) + return EAGAIN; + + env = idl4_default_environment; + err = thread_resume (__task_server, + * (L4_Word_t *) &thread->threadid, + &env); + assert (! err); + +#ifndef WORKING_EXREGS + L4_AbortIpc_and_stop (thread->threadid); + L4_Start_SpIp (thread->threadid, (L4_Word_t) thread->mcontext.sp, + (L4_Word_t) thread->mcontext.pc); +#endif + send_startup_ipc (thread->threadid, (L4_Word_t) thread->mcontext.pc, + (L4_Word_t) thread->mcontext.sp); + + } + + return 0; +} diff --git a/sysdeps/l4/pt-thread-alloc.c b/sysdeps/l4/pt-thread-alloc.c new file mode 100644 index 00000000..a9d5e212 --- /dev/null +++ b/sysdeps/l4/pt-thread-alloc.c @@ -0,0 +1,52 @@ +/* Start thread. L4 version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <string.h> + +#include <pt-internal.h> + +/* Start THREAD. Get the kernel thread scheduled and running. */ +int +__pthread_thread_start (struct __pthread *thread) +{ + error_t err; + + /* The main thread is already running of course. */ + if (__pthread_num_threads == 1) + { + assert (__pthread_total == 1); + thread->thread_id = L4_Myself (); + } + else + { + CORBA_Environment env; + + env = idl4_default_environment; + err = thread_create (__task_server, + L4_Version (L4_Myself ()), + * (L4_Word_t *) &__system_pager, + (L4_Word_t *) &thread->threadid, &env); + if (err) + return EAGAIN; + } + + return 0; +} diff --git a/sysdeps/l4/pt-thread-halt.c b/sysdeps/l4/pt-thread-halt.c new file mode 100644 index 00000000..04d622f4 --- /dev/null +++ b/sysdeps/l4/pt-thread-halt.c @@ -0,0 +1,38 @@ +/* Deallocate the kernel thread resources. Mach version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <mach.h> + +#include <pt-internal.h> + +extern L4_ThreadId_t __task_server; + +/* Deallocate the kernel thread resources associated with THREAD. */ +void +__pthread_thread_halt (struct __pthread *thread) +{ + CORBA_Environment env = idl4_default_environment; + L4_Word_t *t = (L4_Word_t *) &thread->threadid; + + assert (*t); + assert (thread_terminate (__task_server, *t, &env)); + *t = 0; +} diff --git a/sysdeps/l4/pt-thread-start.c b/sysdeps/l4/pt-thread-start.c new file mode 100644 index 00000000..c428d290 --- /dev/null +++ b/sysdeps/l4/pt-thread-start.c @@ -0,0 +1,92 @@ +/* Start thread. L4 version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <string.h> + +#include <pt-internal.h> + +#include "task_client.h" + +extern L4_ThreadId_t __system_pager; +extern L4_ThreadId_t __task_server; + +#ifndef WORKING_EXREGS +static void +send_startup_ipc (L4_ThreadId_t id, L4_Word_t ip, L4_Word_t sp) +{ + L4_Msg_t msg; + + printf ("%s: Sending startup message to %x, " + "(ip=%x, sp=%x)\n", + __FUNCTION__, * (L4_Word_t *) &id, ip, sp); + + L4_Clear (&msg); +#ifdef HAVE_PROPAGATION + L4_Set_VirtualSender (pager_tid); + L4_Set_Propagation (&msg.tag); +#endif + L4_Append_Word (&msg, ip); + L4_Append_Word (&msg, sp); +#ifndef HAVE_PROPAGATION + L4_Append_Word (&msg, *(L4_Word_t *) &id); + id = __system_pager; +#if 0 + DODEBUG (2, printf ("%s: Redirecting start request to pager (%x).\n", + __FUNCTION__, * (L4_Word_t *) &id)); +#endif +#endif + L4_LoadMsg (&msg); + L4_Send (id); +} +#endif + +/* Start THREAD. Get the kernel thread scheduled and running. */ +int +__pthread_thread_start (struct __pthread *thread) +{ + error_t err; + + /* The main thread is already running of course. */ + if (__pthread_num_threads == 1) + { + assert (__pthread_total == 1); + assert (thread->thread_id == L4_Myself ()); + } + else + { + env = idl4_default_environment; + err = thread_resume (__task_server, + * (L4_Word_t *) &thread->threadid, + &env); + assert (! err); + +#ifndef WORKING_EXREGS + L4_AbortIpc_and_stop (thread->threadid); + L4_Start_SpIp (thread->threadid, (L4_Word_t) thread->mcontext.sp, + (L4_Word_t) thread->mcontext.pc); +#endif + send_startup_ipc (thread->threadid, (L4_Word_t) thread->mcontext.pc, + (L4_Word_t) thread->mcontext.sp); + + } + + return 0; +} diff --git a/sysdeps/l4/pt-wakeup.c b/sysdeps/l4/pt-wakeup.c new file mode 100644 index 00000000..7b00e4f6 --- /dev/null +++ b/sysdeps/l4/pt-wakeup.c @@ -0,0 +1,29 @@ +/* Wakeup a thread. L4 version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <l4/l4.h> + +#include <pt-internal.h> + +/* Wakeup THREAD. */ +void +__pthread_wakeup (struct __pthread *thread) +{ + L4_Send (thread->threadid); +} diff --git a/sysdeps/mach/bits/spin-lock.h b/sysdeps/mach/bits/spin-lock.h new file mode 100644 index 00000000..f295df7a --- /dev/null +++ b/sysdeps/mach/bits/spin-lock.h @@ -0,0 +1,97 @@ +/* Definitions of user-visible names for spin locks. + Copyright (C) 1994, 1997, 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef _BITS_SPIN_LOCK_H +#define _BITS_SPIN_LOCK_H 1 + +#include <features.h> +#include <machine-lock.h> /* This does all the work. */ + +__BEGIN_DECLS + +/* The type of a spin lock object. */ +typedef __spin_lock_t __pthread_spinlock_t; + +/* Initializer for a spin lock object. */ +#ifndef __SPIN_LOCK_INITIALIZER +#error __SPIN_LOCK_INITIALIZER undefined: should be defined by <lock-intern.h>. +#endif + +#if defined __USE_EXTERN_INLINES || defined _FORCE_INLINES + +# ifndef __EBUSY +# include <errno.h> +# define __EBUSY EBUSY +# endif + +# ifndef __PT_SPIN_INLINE +# define __PT_SPIN_INLINE extern __inline +# endif + +__PT_SPIN_INLINE int __pthread_spin_destroy (__pthread_spinlock_t *__lock); + +__PT_SPIN_INLINE int +__pthread_spin_destroy (__pthread_spinlock_t *__lock) +{ + return 0; +} + +__PT_SPIN_INLINE int __pthread_spin_init (__pthread_spinlock_t *__lock, + int __pshared); + +__PT_SPIN_INLINE int +__pthread_spin_init (__pthread_spinlock_t *__lock, int __pshared) +{ + *__lock = __SPIN_LOCK_INITIALIZER; + return 0; +} + +__PT_SPIN_INLINE int __pthread_spin_trylock (__pthread_spinlock_t *__lock); + +__PT_SPIN_INLINE int +__pthread_spin_trylock (__pthread_spinlock_t *__lock) +{ + return __spin_try_lock (__lock) ? 0 : __EBUSY; +} + +extern inline int __pthread_spin_lock (__pthread_spinlock_t *__lock); +extern int _pthread_spin_lock (__pthread_spinlock_t *__lock); + +extern inline int +__pthread_spin_lock (__pthread_spinlock_t *__lock) +{ + if (__pthread_spin_trylock (__lock)) + return _pthread_spin_lock (__lock); + return 0; +} + +__PT_SPIN_INLINE int __pthread_spin_unlock (__pthread_spinlock_t *__lock); + +__PT_SPIN_INLINE int +__pthread_spin_unlock (__pthread_spinlock_t *__lock) +{ + __spin_unlock (__lock); + return 0; +} + +#endif /* Use extern inlines or force inlines. */ + +__END_DECLS + +#endif /* bits/spin-lock.h */ diff --git a/sysdeps/mach/hurd/i386/pt-machdep.c b/sysdeps/mach/hurd/i386/pt-machdep.c new file mode 100644 index 00000000..face46c5 --- /dev/null +++ b/sysdeps/mach/hurd/i386/pt-machdep.c @@ -0,0 +1,54 @@ +/* Machine dependent pthreads code. Hurd/i386 version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <errno.h> + +#include <mach.h> +#include <mach/i386/thread_status.h> +#include <mach/thread_status.h> + +int +__thread_set_pcsp (thread_t thread, + int set_ip, void *ip, + int set_sp, void *sp) +{ + error_t err; + struct i386_thread_state state; + mach_msg_type_number_t state_count; + + state_count = i386_THREAD_STATE_COUNT; + + err = __thread_get_state (thread, i386_THREAD_STATE, + (thread_state_t) &state, &state_count); + if (err) + return err; + + if (set_sp) + state.uesp = (unsigned int) sp; + if (set_ip) + state.eip = (unsigned int) ip; + + err = __thread_set_state (thread, i386_THREAD_STATE, + (thread_state_t) &state, + i386_THREAD_STATE_COUNT); + if (err) + return err; + + return 0; +} diff --git a/sysdeps/mach/hurd/i386/pt-setup.c b/sysdeps/mach/hurd/i386/pt-setup.c new file mode 100644 index 00000000..a2495046 --- /dev/null +++ b/sysdeps/mach/hurd/i386/pt-setup.c @@ -0,0 +1,99 @@ +/* Setup thread stack. Hurd/i386 version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <stdint.h> +#include <assert.h> + +#include <pt-internal.h> + +/* The stack layout used on the i386 is: + + ----------------- + | ARG | + ----------------- + | START_ROUTINE | + ----------------- + | 0 | + ----------------- + | | + | Fast TSD | + | | + ----------------- + + We need to reserve __hurd_threadvar_max `unsigned long int's' of + (fast) thread-specific data (TSD) for the Hurd. */ + +/* Set up the stack for THREAD, such that it appears as if + START_ROUTINE and ARG were passed to the new thread's entry-point. + Return the stack pointer for the new thread. */ +static void * +stack_setup (struct __pthread *thread, + void *(*start_routine)(void *), void *arg) +{ + error_t err; + uintptr_t *bottom, *top; + + /* Calculate the top of the new stack. */ + bottom = thread->stackaddr; + top = (uintptr_t *) ((uintptr_t) bottom + thread->stacksize); + + /* Next, make room for the TSDs. */ + top -= __hurd_threadvar_max; + + /* Save the self pointer. */ + top[_HURD_THREADVAR_THREAD] = thread; + + if (start_routine) + { + /* And then the call frame. */ + *--top = (uintptr_t) arg; /* Argument to START_ROUTINE. */ + *--top = (uintptr_t) start_routine; + *--top = 0; /* Fake return address. */ + } + + if (thread->guardsize) + { + err = __vm_protect (__mach_task_self (), (vm_address_t) bottom, + thread->guardsize, 0, 0); + assert_perror (err); + } + + return top; +} + +int +__pthread_setup (struct __pthread *thread, + void (*entry_point)(void *(*)(void *), void *), + void *(*start_routine)(void *), void *arg) +{ + error_t err; + + thread->mcontext.pc = entry_point; + thread->mcontext.sp = stack_setup (thread, start_routine, arg); + + if (thread->kernel_thread != __mach_thread_self ()) + { + err = __thread_set_pcsp (thread->kernel_thread, + 1, thread->mcontext.pc, + 1, thread->mcontext.sp); + assert_perror (err); + } + + return 0; +} diff --git a/sysdeps/mach/hurd/pt-attr-setstackaddr.c b/sysdeps/mach/hurd/pt-attr-setstackaddr.c new file mode 100644 index 00000000..1225ed5b --- /dev/null +++ b/sysdeps/mach/hurd/pt-attr-setstackaddr.c @@ -0,0 +1,35 @@ +/* pthread_attr_setstackaddr. Hurd on Mach version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +/* We use fixed sized stacks which require proper alignment. */ +#define __pthread_stacksize __pthread_default_attr.stacksize + +int +pthread_attr_setstackaddr (pthread_attr_t *attr, + void *stackaddr) +{ + if ((long) stackaddr & (__pthread_stacksize - 1)) + return EINVAL; + + attr->stackaddr = stackaddr; + return 0; +} diff --git a/sysdeps/mach/hurd/pt-attr-setstacksize.c b/sysdeps/mach/hurd/pt-attr-setstacksize.c new file mode 100644 index 00000000..6471c0a3 --- /dev/null +++ b/sysdeps/mach/hurd/pt-attr-setstacksize.c @@ -0,0 +1,35 @@ +/* pthread_attr_setstacksize. Hurd on Mach version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <pt-internal.h> + +/* We use fixed sized stacks which require proper alignment. */ +#define __pthread_stacksize __pthread_default_attr.stacksize + +int +pthread_attr_setstacksize (pthread_attr_t *attr, + size_t stacksize) +{ + if (stacksize != __pthread_stacksize) + return EINVAL; + + attr->stacksize = stacksize; + return 0; +} diff --git a/sysdeps/mach/hurd/pt-docancel.c b/sysdeps/mach/hurd/pt-docancel.c new file mode 100644 index 00000000..58524ff0 --- /dev/null +++ b/sysdeps/mach/hurd/pt-docancel.c @@ -0,0 +1,57 @@ +/* Cancel a thread. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +static void +call_exit (void) +{ + pthread_exit (0); +} + +int +__pthread_do_cancel (struct __pthread *p) +{ + assert (p->cancel_pending = 1); + assert (p->cancel_state == PTHREAD_CANCEL_ENABLE); + + if (__mach_thread_self () == p->kernel_thread) + call_exit (); + else + { + error_t err; + + err = __thread_suspend (p->kernel_thread); + assert_perror (err); + + err = __thread_abort (p->kernel_thread); + assert_perror (err); + + err = __thread_set_pcsp (p->kernel_thread, + 1, (void *) call_exit, 0, 0); + assert_perror (err); + + err = __thread_resume (p->kernel_thread); + assert_perror (err); + } + + return 0; +} diff --git a/sysdeps/mach/hurd/pt-sigstate-destroy.c b/sysdeps/mach/hurd/pt-sigstate-destroy.c new file mode 100644 index 00000000..8e56c5cf --- /dev/null +++ b/sysdeps/mach/hurd/pt-sigstate-destroy.c @@ -0,0 +1,28 @@ +/* Destroy the signal state. Hurd on Mach version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> + +#include <pt-internal.h> + +void +__pthread_sigstate_destroy (struct __pthread *thread) +{ + /* Nothing to do. */ +} diff --git a/sysdeps/mach/hurd/pt-sigstate-init.c b/sysdeps/mach/hurd/pt-sigstate-init.c new file mode 100644 index 00000000..da5a9455 --- /dev/null +++ b/sysdeps/mach/hurd/pt-sigstate-init.c @@ -0,0 +1,37 @@ +/* Initialize the signal state. Hurd on Mach version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <hurd/threadvar.h> + +#include <pt-internal.h> + +error_t +__pthread_sigstate_init (struct __pthread *thread) +{ + void **location = + (void *) __hurd_threadvar_location_from_sp (_HURD_THREADVAR_SIGSTATE, + thread->stackaddr); + + /* The real initialization happens internally in glibc the first + time that _hurd_thead_sigstate is called. */ + *location = 0; + + return 0; +} diff --git a/sysdeps/mach/hurd/pt-sigstate.c b/sysdeps/mach/hurd/pt-sigstate.c new file mode 100644 index 00000000..68c79c5b --- /dev/null +++ b/sysdeps/mach/hurd/pt-sigstate.c @@ -0,0 +1,69 @@ +/* Set a thread's signal state. Hurd on Mach version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <assert.h> +#include <signal.h> +#include <hurd/signal.h> + +#include <pt-internal.h> + +error_t +__pthread_sigstate (struct __pthread *thread, int how, + const sigset_t *set, sigset_t *oset, + int clear_pending) +{ + error_t err = 0; + struct hurd_sigstate *ss; + + ss = _hurd_thread_sigstate (thread->kernel_thread); + assert (ss); + + __pthread_spin_lock (&ss->lock); + + if (oset) + *oset = ss->blocked; + + if (set) + switch (how) + { + case SIG_BLOCK: + ss->blocked |= *set; + break; + + case SIG_SETMASK: + ss->blocked = *set; + break; + + case SIG_UNBLOCK: + ss->blocked &= ~*set; + break; + + default: + err = EINVAL; + break; + } + + if (! err && clear_pending) + __sigemptyset (&ss->pending); + + __pthread_spin_unlock (&ss->lock); + + return err; +} diff --git a/sysdeps/mach/hurd/pt-sysdep.c b/sysdeps/mach/hurd/pt-sysdep.c new file mode 100644 index 00000000..b42fe255 --- /dev/null +++ b/sysdeps/mach/hurd/pt-sysdep.c @@ -0,0 +1,72 @@ +/* System dependent pthreads code. Hurd version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <stddef.h> +#include <stdint.h> + +#include <mach.h> +#include <mach/mig_support.h> + +#include <hurd/threadvar.h> + +#include <pt-internal.h> + +/* Forward. */ +static int init_routine (void); + +/* OK, the name of this variable isn't really appropriate, but I don't + want to change it yet. */ +int (*_cthread_init_routine)(void) = &init_routine; + +/* This function is called from the Hurd-specific startup code. It + should return a new stack pointer for the main thread. The caller + will switch to this new stack before doing anything serious. */ +static int +init_routine (void) +{ + struct __pthread *thread; + int err; + + /* Initialize the library. */ + __pthread_initialize (); + + /* Create the pthread structure for the main thread (i.e. us). */ + err = __pthread_create_internal (&thread, 0, 0, 0); + assert_perror (err); + + ((void **) (__hurd_threadvar_stack_offset))[_HURD_THREADVAR_THREAD] + = thread; + + /* Decrease the number of threads, to take into account that the + signal thread (which will be created by the glibc startup code + when we return from here) shouldn't be seen as a user thread. */ + __pthread_total--; + + /* Make MiG code thread aware. */ + __mig_init (thread->stackaddr); + + /* Make sure we can find the per-thread variables. */ + __hurd_threadvar_stack_mask = ~(__pthread_default_attr.stacksize - 1); + __hurd_threadvar_stack_offset + = (__pthread_default_attr.stacksize + - __hurd_threadvar_max * sizeof (uintptr_t)); + + return (int) thread->mcontext.sp; +} diff --git a/sysdeps/mach/hurd/pt-sysdep.h b/sysdeps/mach/hurd/pt-sysdep.h new file mode 100644 index 00000000..3addef7c --- /dev/null +++ b/sysdeps/mach/hurd/pt-sysdep.h @@ -0,0 +1,65 @@ +/* Internal defenitions for pthreads library. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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. */ + +#ifndef _PT_SYSDEP_H +#define _PT_SYSDEP_H 1 + +#include <mach.h> + +#include <hurd/threadvar.h> + +/* XXX */ +#define _POSIX_THREAD_THREADS_MAX 64 + +/* The default stack size. */ +#define PTHREAD_STACK_DEFAULT (2 * 1024 * 1024) + +#define PTHREAD_SYSDEP_MEMBERS \ + thread_t kernel_thread; \ + mach_msg_header_t wakeupmsg; + +#define _HURD_THREADVAR_THREAD _HURD_THREADVAR_DYNAMIC_USER + +#define _pthread_self() \ + ({ \ + struct __pthread *thread; \ + \ + assert (__pthread_threads); \ + thread = *(struct __pthread **) \ + __hurd_threadvar_location (_HURD_THREADVAR_THREAD); \ + \ + assert (thread); \ + assert (thread->kernel_thread == __mach_thread_self ()); \ + thread; \ + }) + +extern inline void +__pthread_stack_dealloc (void *stackaddr, size_t stacksize) +{ + __vm_deallocate (__mach_task_self (), (vm_offset_t) stackaddr, stacksize); +} + +/* Change thread THREAD's program counter to PC if SET_PC is true and + its stack pointer to SP if SET_IP is true. */ +extern int __thread_set_pcsp (thread_t thread, + int set_pc, void *pc, + int set_sp, void *sp); + + +#endif /* pt-sysdep.h */ diff --git a/sysdeps/mach/pt-block.c b/sysdeps/mach/pt-block.c new file mode 100644 index 00000000..a947b27c --- /dev/null +++ b/sysdeps/mach/pt-block.c @@ -0,0 +1,39 @@ +/* Block a thread. Mach version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> + +#include <mach.h> +#include <mach/message.h> + +#include <pt-internal.h> + +/* Block THREAD. */ +void +__pthread_block (struct __pthread *thread) +{ + mach_msg_header_t msg; + error_t err; + + err = __mach_msg (&msg, MACH_RCV_MSG, 0, sizeof msg, + thread->wakeupmsg.msgh_remote_port, + MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); + assert_perror (err); +} diff --git a/sysdeps/mach/pt-spin.c b/sysdeps/mach/pt-spin.c new file mode 100644 index 00000000..010faca8 --- /dev/null +++ b/sysdeps/mach/pt-spin.c @@ -0,0 +1,35 @@ +/* Spin locks. Mach version. + Copyright (C) 2002 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <machine-lock.h> + +/* In glibc. */ +extern void __spin_lock_solid (__pthread_spinlock_t *lock); + +/* Lock the spin lock object LOCK. If the lock is held by another + thread spin until it becomes available. */ +int +_pthread_spin_lock (__pthread_spinlock_t *lock) +{ + __spin_lock_solid (lock); + return 0; +} + +weak_alias (_pthread_spin_lock, pthread_spin_lock); diff --git a/sysdeps/mach/pt-stack-alloc.c b/sysdeps/mach/pt-stack-alloc.c new file mode 100644 index 00000000..0956fc7d --- /dev/null +++ b/sysdeps/mach/pt-stack-alloc.c @@ -0,0 +1,74 @@ +/* Allocate a new stack. Mach version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <errno.h> + +#include <mach.h> +#include <mach/machine/vm_param.h> + +#include <pt-internal.h> + +#define __pthread_stacksize __pthread_default_attr.stacksize + +/* The next address to use for stack allocation. */ +static vm_address_t next_stack_base = VM_MIN_ADDRESS; + + +/* Allocate a new stack of size STACKSIZE. If successful, store the + address of the newly allocated stack in *STACKADDR and return 0. + Otherwise return an error code (EINVAL for an invalid stack size, + EAGAIN if the system lacked the necessary resources to allocate a + new stack). */ +int +__pthread_stack_alloc (void **stackaddr, size_t stacksize) +{ + vm_offset_t base; + int i = 0; + + if (stacksize != __pthread_stacksize) + return EINVAL; + + get_stack: + i ++; + for (base = next_stack_base; + base < VM_MAX_ADDRESS + && __vm_allocate (__mach_task_self (), &base, + __pthread_stacksize, FALSE) != KERN_SUCCESS; + base += __pthread_stacksize) + ; + + if (base >= VM_MAX_ADDRESS) + { + if (i == 1) + { + next_stack_base = VM_MIN_ADDRESS; + goto get_stack; + } + else + return EAGAIN; + } + + if (base >= VM_MAX_ADDRESS) + return EAGAIN; + + next_stack_base = base + __pthread_stacksize; + + (*stackaddr) = (void *) base; + return 0; +} diff --git a/sysdeps/mach/pt-start.c b/sysdeps/mach/pt-start.c new file mode 100644 index 00000000..5e8cd5aa --- /dev/null +++ b/sysdeps/mach/pt-start.c @@ -0,0 +1,106 @@ +/* Start thread. Mach version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <string.h> + +#include <mach.h> + +#include <pt-internal.h> + +/* Prepare a wakeup message. */ +static error_t +create_wakeupmsg (struct __pthread *thread) +{ + kern_return_t err; + + /* Build wakeup message. */ + thread->wakeupmsg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0); + thread->wakeupmsg.msgh_size = 0; + + err = __mach_port_allocate (__mach_task_self (), MACH_PORT_RIGHT_RECEIVE, + &thread->wakeupmsg.msgh_remote_port); + if (err) + return EAGAIN; + + thread->wakeupmsg.msgh_local_port = MACH_PORT_NULL; + thread->wakeupmsg.msgh_seqno = 0; + thread->wakeupmsg.msgh_id = 0; + + err = __mach_port_insert_right (__mach_task_self (), + thread->wakeupmsg.msgh_remote_port, + thread->wakeupmsg.msgh_remote_port, + MACH_MSG_TYPE_MAKE_SEND); + if (err) + { + __mach_port_deallocate (__mach_task_self (), + thread->wakeupmsg.msgh_remote_port); + return EAGAIN; + } + + return 0; +} + +/* Start THREAD. We allocate all system-specific resources, including + a kernel thread, set it up, and get it running. */ +int +__pthread_start (struct __pthread *thread) +{ + error_t err; + + err = create_wakeupmsg (thread); + if (err) + return err; + + /* If there are no pthreads in the system then the pthread library + is bootstrapping and the main thread must create initialize + itself. The thread itself is already running, it just has no + pthread context. We want to reuse what it already has (including + the kernel thread), however, we must determine which thread is + the main thread. + + We cannot test if __pthread_total is one as we later decrement it + before creating the signal thread. Currently, we check if + __pthread_num_threads--the number of allocated thread + structures--is one. __pthread_alloc has already been called in + __pthread_create_internal for us. This predicate could be + improved, however, it is sufficient for now. */ + if (__pthread_num_threads == 1) + { + assert (__pthread_total == 1); + thread->kernel_thread = __mach_thread_self (); + } + else + { + err = __thread_create (__mach_task_self (), &thread->kernel_thread); + if (err) + return EAGAIN; + + err = __thread_set_pcsp (thread->kernel_thread, + 1, thread->mcontext.pc, + 1, thread->mcontext.sp); + assert_perror (err); + + err = __thread_resume (thread->kernel_thread); + assert_perror (err); + } + + return 0; +} diff --git a/sysdeps/mach/pt-thread-alloc.c b/sysdeps/mach/pt-thread-alloc.c new file mode 100644 index 00000000..af929533 --- /dev/null +++ b/sysdeps/mach/pt-thread-alloc.c @@ -0,0 +1,98 @@ +/* Start thread. Mach version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <string.h> + +#include <mach.h> + +#include <pt-internal.h> + +/* Prepare a wakeup message. */ +static error_t +create_wakeupmsg (struct __pthread *thread) +{ + kern_return_t err; + + /* Build wakeup message. */ + thread->wakeupmsg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0); + thread->wakeupmsg.msgh_size = 0; + + err = __mach_port_allocate (__mach_task_self (), MACH_PORT_RIGHT_RECEIVE, + &thread->wakeupmsg.msgh_remote_port); + if (err) + return EAGAIN; + + thread->wakeupmsg.msgh_local_port = MACH_PORT_NULL; + thread->wakeupmsg.msgh_seqno = 0; + thread->wakeupmsg.msgh_id = 0; + + err = __mach_port_insert_right (__mach_task_self (), + thread->wakeupmsg.msgh_remote_port, + thread->wakeupmsg.msgh_remote_port, + MACH_MSG_TYPE_MAKE_SEND); + if (err) + { + __mach_port_deallocate (__mach_task_self (), + thread->wakeupmsg.msgh_remote_port); + return EAGAIN; + } + + return 0; +} + +/* Allocate any resouces for THREAD. The new kernel thread should not + be eligible to be scheduled. */ +int +__pthread_thread_alloc (struct __pthread *thread) +{ + error_t err; + + err = create_wakeupmsg (thread); + if (err) + return err; + + /* If there are no pthreads in the system then the pthread library + is bootstrapping and the main thread must create initialize + itself. The thread itself is already running, it just has not + pthread context. We want to reuse what it already has (including + the kernel thread), however, we must determine which thread is + the main thread. + + We cannot test if __pthread_total is one as we later decrement + before creating the signal thread. Currently, we check if + __pthread_num_threads--the number of allocated thread + structures--is one. __pthread_alloc has already been called in + __pthread_create_internal for us. This predicate could be improved, + however, it is sufficient for now. */ + if (__pthread_num_threads == 1) + { + assert (__pthread_total == 0); + thread->kernel_thread = __mach_thread_self (); + } + else + { + err = __thread_create (__mach_task_self (), &thread->kernel_thread); + if (err) + return EAGAIN; + } + + return 0; +} diff --git a/sysdeps/mach/pt-thread-halt.c b/sysdeps/mach/pt-thread-halt.c new file mode 100644 index 00000000..84e6ac8e --- /dev/null +++ b/sysdeps/mach/pt-thread-halt.c @@ -0,0 +1,39 @@ +/* Deallocate the kernel thread resources. Mach version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <mach.h> + +#include <pt-internal.h> + + +/* Deallocate the kernel thread resources associated with THREAD. */ +void +__pthread_thread_halt (struct __pthread *thread) +{ + error_t err; + + err = __mach_port_deallocate (__mach_task_self (), + thread->wakeupmsg.msgh_remote_port); + assert_perror (err); + + err = __thread_terminate (thread->kernel_thread); + assert_perror (err); +} diff --git a/sysdeps/mach/pt-thread-start.c b/sysdeps/mach/pt-thread-start.c new file mode 100644 index 00000000..eade5098 --- /dev/null +++ b/sysdeps/mach/pt-thread-start.c @@ -0,0 +1,45 @@ +/* Start thread. Mach version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <mach.h> + +#include <pt-internal.h> + +/* Start THREAD. Get the kernel thread scheduled and running. */ +int +__pthread_thread_start (struct __pthread *thread) +{ + error_t err; + + if (__pthread_num_threads == 1) + { + /* The main thread is already running: do nothing. */ + assert (__pthread_total == 1); + assert (thread->kernel_thread == __mach_thread_self ()); + } + else + { + err = __thread_resume (thread->kernel_thread); + assert_perror (err); + } + + return 0; +} diff --git a/sysdeps/mach/pt-timedblock.c b/sysdeps/mach/pt-timedblock.c new file mode 100644 index 00000000..6324ae76 --- /dev/null +++ b/sysdeps/mach/pt-timedblock.c @@ -0,0 +1,60 @@ +/* Block a thread with a timeout. Mach version. + Copyright (C) 2000,02 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> +#include <time.h> +#include <sys/time.h> + +#include <mach.h> +#include <mach/message.h> + +#include <pt-internal.h> + +/* Block THREAD. */ +error_t +__pthread_timedblock (struct __pthread *thread, + const struct timespec *abstime) +{ + error_t err; + mach_msg_header_t msg; + mach_msg_timeout_t ms; + struct timeval now; + + /* We have an absolute time and now we have to convert it to a + relative time. Arg. */ + + err = gettimeofday(&now, NULL); + assert (! err); + + ms = abstime->tv_sec * 1000 + (abstime->tv_nsec + 999999) / 1000000 + - now.tv_sec * 1000 - (now.tv_usec + 999) / 1000; + + if (ms <= 0) + return ETIMEDOUT; + + err = __mach_msg (&msg, MACH_RCV_MSG | MACH_RCV_TIMEOUT, 0, + sizeof msg, thread->wakeupmsg.msgh_remote_port, + ms, MACH_PORT_NULL); + if (err == EMACH_RCV_TIMED_OUT) + return ETIMEDOUT; + + assert_perror (err); + return 0; +} diff --git a/sysdeps/mach/pt-wakeup.c b/sysdeps/mach/pt-wakeup.c new file mode 100644 index 00000000..8ab10fb8 --- /dev/null +++ b/sysdeps/mach/pt-wakeup.c @@ -0,0 +1,38 @@ +/* Wakeup a thread. Mach version. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <assert.h> +#include <errno.h> + +#include <mach.h> +#include <mach/message.h> + +#include <pt-internal.h> + +/* Wakeup THREAD. */ +void +__pthread_wakeup (struct __pthread *thread) +{ + error_t err; + + err = __mach_msg (&thread->wakeupmsg, MACH_SEND_MSG, + sizeof (mach_msg_header_t), 0, MACH_PORT_NULL, + MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL); + assert_perror (err); +} diff --git a/sysdeps/posix/pt-spin.c b/sysdeps/posix/pt-spin.c new file mode 100644 index 00000000..cdc482c1 --- /dev/null +++ b/sysdeps/posix/pt-spin.c @@ -0,0 +1,53 @@ +/* Spin locks. + Copyright (C) 2000 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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 <pthread.h> +#include <sched.h> + +/* The default for single processor machines; don't spin, it's + pointless. */ +#ifndef __PTHREAD_SPIN_COUNT +# define __PTHREAD_SPIN_COUNT 1 +#endif + +/* The number of times to spin while trying to lock a spin lock object + before yielding the processor. */ +int __pthread_spin_count = __PTHREAD_SPIN_COUNT; + + +/* Lock the spin lock object LOCK. If the lock is held by another + thread spin until it becomes available. */ +int +_pthread_spin_lock (__pthread_spinlock_t *lock) +{ + int i; + + while (1) + { + for (i = 0; i < __pthread_spin_count; i++) + { + if (__pthread_spin_trylock (lock) == 0) + return 0; + } + + __sched_yield (); + } +} + +weak_alias (_pthread_spin_lock, pthread_spin_lock); diff --git a/tests/.cvsignore b/tests/.cvsignore new file mode 100644 index 00000000..70845e08 --- /dev/null +++ b/tests/.cvsignore @@ -0,0 +1 @@ +Makefile.in diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 00000000..9509c957 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,29 @@ +CFLAGS=-Wall -g + +LDLIBS = -lpthread + +CHECK_SRC := test-1.c test-2.c test-3.c test-6.c test-7.c test-8.c \ + test-9.c test-10.c test-11.c test-12.c test-13.c test-14.c \ + test-15.c test-16.c + +CHECK_OBJS := $(addsuffix .o,$(basename $(notdir $(CHECK_SRC)))) +CHECK_PROGS := $(basename $(notdir $(CHECK_SRC))) \ + $(addsuffix -static, $(basename $(CHECK_SRC))) + +%-static: %.o + $(CC) -static $(CFLAGS) $(LDFLAGS) $< -o $@ $(LDLIBS) + +check: $(CHECK_OBJS) $(CHECK_PROGS) + for i in $(CHECK_PROGS); do \ + echo -n Running $$i...\ ; \ + if ./$$i 2>&1 > $$i.out; \ + then \ + echo Success.; \ + else \ + echo Failure.; \ + fi \ + done + +clean: + rm -f $(CHECK_OBJS) $(CHECK_PROGS) \ + $(addsuffix .out,$(basename $(notdir $(CHECK_PROGS))))
\ No newline at end of file diff --git a/tests/test-1.c b/tests/test-1.c new file mode 100644 index 00000000..86c7c97e --- /dev/null +++ b/tests/test-1.c @@ -0,0 +1,49 @@ +#define _GNU_SOURCE + +#include <pthread.h> +#include <unistd.h> +#include <error.h> +#include <errno.h> +#include <stdio.h> + +#define THREADS 500 + +void * +foo (void *arg) +{ + pthread_mutex_t *mutex = arg; + pthread_mutex_lock (mutex); + pthread_mutex_unlock (mutex); + return mutex; +} + +int +main (int argc, char **argv) +{ + int i; + error_t err; + pthread_t tid[THREADS]; + pthread_mutex_t mutex[THREADS]; + + for (i = 0; i < THREADS; i ++) + { + mutex[i] = PTHREAD_MUTEX_INITIALIZER; + pthread_mutex_lock (&mutex[i]); + err = pthread_create (&tid[i], 0, foo, &mutex[i]); + if (err) + error (1, err, "pthread_create"); + sched_yield (); + } + + for (i = THREADS - 1; i >= 0; i --) + { + void *ret; + pthread_mutex_unlock (&mutex[i]); + err = pthread_join (tid[i], &ret); + if (err) + error (1, err, "pthread_join"); + assert (ret == &mutex[i]); + } + + return 0; +} diff --git a/tests/test-10.c b/tests/test-10.c new file mode 100644 index 00000000..bec05c14 --- /dev/null +++ b/tests/test-10.c @@ -0,0 +1,46 @@ +/* Test error checking mutexes. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <assert.h> +#include <error.h> +#include <errno.h> + +int +main (int argc, char **argv) +{ + error_t err; + pthread_mutexattr_t mattr; + pthread_mutex_t mutex; + + err = pthread_mutexattr_init (&mattr); + if (err) + error (1, err, "pthread_mutexattr_init"); + + err = pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_ERRORCHECK); + if (err) + error (1, err, "pthread_mutexattr_settype"); + + err = pthread_mutex_init (&mutex, &mattr); + if (err) + error (1, err, "pthread_mutex_init"); + + err = pthread_mutexattr_destroy (&mattr); + if (err) + error (1, err, "pthread_mutexattr_destroy"); + + err = pthread_mutex_lock (&mutex); + assert (err == 0); + + err = pthread_mutex_lock (&mutex); + assert (err == EDEADLK); + + err = pthread_mutex_unlock (&mutex); + assert (err == 0); + + err = pthread_mutex_unlock (&mutex); + assert (err == EPERM); + + return 0; +} diff --git a/tests/test-11.c b/tests/test-11.c new file mode 100644 index 00000000..de779a40 --- /dev/null +++ b/tests/test-11.c @@ -0,0 +1,143 @@ +/* Test rwlocks. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <assert.h> +#include <error.h> +#include <errno.h> + +#define THREADS 1 + +int a; +int b; + +/* Get a read lock and assert that a == b. */ +void * +test1 (void *arg) +{ + error_t err; + pthread_rwlock_t *lock = arg; + int i; + + for (i = 0; i < 200; i ++) + { + err = pthread_rwlock_rdlock (lock); + assert (err == 0); + + assert (a == b); + + sched_yield (); + + assert (a == b); + + err = pthread_rwlock_unlock (lock); + assert (err == 0); + } + + return 0; +} + +int +main (int argc, char **argv) +{ + error_t err; + pthread_rwlockattr_t attr; + pthread_rwlock_t lock; + int pshared; + + int i; + pthread_t tid[THREADS]; + void *ret; + + err = pthread_rwlockattr_init (&attr); + if (err) + error (1, err, "pthread_rwlockattr_init"); + + err = pthread_rwlockattr_getpshared (&attr, &pshared); + if (err) + error (1, err, "pthread_rwlockattr_getpshared"); + + /* Assert the default state as mandated by POSIX. */ + assert (pshared == PTHREAD_PROCESS_PRIVATE); + + err = pthread_rwlockattr_setpshared (&attr, pshared); + if (err) + error (1, err, "pthread_rwlockattr_setpshared"); + + err = pthread_rwlock_init (&lock, &attr); + if (err) + error (1, err, "pthread_rwlock_init"); + + err = pthread_rwlockattr_destroy (&attr); + if (err) + error (1, err, "pthread_rwlockattr_destroy"); + + /* Now test the lock. */ + + for (i = 0; i < THREADS; i ++) + { + err = pthread_create (&tid[i], 0, test1, &lock); + if (err) + error (1, err, "pthread_create"); + } + + for (i = 0; i < 10; i ++) + { + sched_yield (); + + /* Get a write lock. */ + pthread_rwlock_wrlock (&lock); + /* Increment a and b giving other threads a chance to run in + between. */ + sched_yield (); + a ++; + sched_yield (); + b ++; + sched_yield (); + /* Unlock. */ + pthread_rwlock_unlock (&lock); + } + + for (i = 0; i < THREADS; i ++) + { + err = pthread_join (tid[i], &ret); + if (err) + error (1, err, "pthread_join"); + } + + /* Read lock it. */ + err = pthread_rwlock_tryrdlock (&lock); + assert (err == 0); + + /* Try to write lock it. It should fail with EBUSY. */ + err = pthread_rwlock_trywrlock (&lock); + assert (err == EBUSY); + + /* Drop the read lock. */ + err = pthread_rwlock_unlock (&lock); + assert (err == 0); + + /* Get a write lock. */ + err = pthread_rwlock_trywrlock (&lock); + assert (err == 0); + + /* Fail trying to acquire another write lock. */ + err = pthread_rwlock_trywrlock (&lock); + assert (err == EBUSY); + + /* Try to get a read lock which should also fail. */ + err = pthread_rwlock_tryrdlock (&lock); + assert (err == EBUSY); + + /* Unlock it. */ + err = pthread_rwlock_unlock (&lock); + assert (err == 0); + + + err = pthread_rwlock_destroy (&lock); + if (err) + error (1, err, "pthread_rwlock_destroy"); + + return 0; +} diff --git a/tests/test-12.c b/tests/test-12.c new file mode 100644 index 00000000..2b784908 --- /dev/null +++ b/tests/test-12.c @@ -0,0 +1,29 @@ +/* Test concurrency level. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <assert.h> +#include <error.h> +#include <errno.h> + +int +main (int argc, char **argv) +{ + int i; + int err; + + i = pthread_getconcurrency (); + assert (i == 0); + + err = pthread_setconcurrency (-1); + assert (err == EINVAL); + + err = pthread_setconcurrency (4); + assert (err == 0); + + i = pthread_getconcurrency (); + assert (i == 4); + + return 0; +} diff --git a/tests/test-13.c b/tests/test-13.c new file mode 100644 index 00000000..13b09051 --- /dev/null +++ b/tests/test-13.c @@ -0,0 +1,66 @@ +/* Test condition attributes and pthread_cond_timedwait. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <stdio.h> +#include <assert.h> +#include <error.h> +#include <errno.h> +#include <sys/time.h> + +int +main (int argc, char **argv) +{ + error_t err; + int i; + pthread_condattr_t attr; + pthread_cond_t cond; + struct timespec ts; + pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; + struct timeval before, after; + int diff; + + err = pthread_condattr_init (&attr); + if (err) + error (1, err, "pthread_condattr_init"); + + err = pthread_condattr_getpshared (&attr, &i); + if (err) + error (1, err, "pthread_condattr_getpshared"); + assert (i == PTHREAD_PROCESS_PRIVATE); + + err = pthread_condattr_setpshared (&attr, PTHREAD_PROCESS_PRIVATE); + assert (err == 0); + + err = pthread_cond_init (&cond, &attr); + if (err) + error (1, err, "pthread_cond_init"); + + err = pthread_condattr_destroy (&attr); + if (err) + error (1, err, "pthread_condattr_destroy"); + + gettimeofday (&before, 0); + ts.tv_sec = before.tv_sec + 1; + ts.tv_nsec = before.tv_usec * 1000; + + printf ("Starting wait @ %d\n", (int) before.tv_sec); + + pthread_mutex_lock (&m); + err = pthread_cond_timedwait (&cond, &m, &ts); + + gettimeofday (&after, 0); + + printf ("End wait @ %d (err = %d)\n", (int) after.tv_sec, err); + + assert (err == ETIMEDOUT); + + diff = after.tv_sec * 1000000 + after.tv_usec + - before.tv_sec * 1000000 - before.tv_usec; + + if (diff < 900000 || diff > 1100000) + error (1, EGRATUITOUS, "pthread_cond_timedwait waited %d us", diff); + + return 0; +} diff --git a/tests/test-14.c b/tests/test-14.c new file mode 100644 index 00000000..b1dbfa66 --- /dev/null +++ b/tests/test-14.c @@ -0,0 +1,44 @@ +/* Test pthread_mutex_timedlock. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <stdio.h> +#include <assert.h> +#include <error.h> +#include <errno.h> +#include <sys/time.h> + +int +main (int argc, char **argv) +{ + error_t err; + struct timespec ts; + pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; + struct timeval before, after; + int diff; + + gettimeofday (&before, 0); + ts.tv_sec = before.tv_sec + 1; + ts.tv_nsec = before.tv_usec * 1000; + + printf ("Starting wait @ %d\n", (int) before.tv_sec); + + pthread_mutex_lock (&m); + /* A default mutex shall dead lock if locked twice. As such we do + not need spawn a second thread. */ + err = pthread_mutex_timedlock (&m, &ts); + assert (err == ETIMEDOUT); + + gettimeofday (&after, 0); + + printf ("End wait @ %d\n", (int) after.tv_sec); + + diff = after.tv_sec * 1000000 + after.tv_usec + - before.tv_sec * 1000000 - before.tv_usec; + + if (diff < 900000 || diff > 1100000) + error (1, EGRATUITOUS, "pthread_mutex_timedlock waited %d us", diff); + + return 0; +} diff --git a/tests/test-15.c b/tests/test-15.c new file mode 100644 index 00000000..173f8b6b --- /dev/null +++ b/tests/test-15.c @@ -0,0 +1,87 @@ +/* Test pthread_rwlock_timedrdlock and pthread_rwlock_timedwrlock. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <stdio.h> +#include <assert.h> +#include <error.h> +#include <errno.h> +#include <sys/time.h> + +#define THREADS 10 + +pthread_rwlock_t rwlock; + +void * +test (void *arg) +{ + error_t err; + int foo = (int) arg; + struct timespec ts; + struct timeval before, after; + int diff; + + gettimeofday (&before, 0); + ts.tv_sec = before.tv_sec + 1; + ts.tv_nsec = before.tv_usec * 1000; + + printf ("Thread %d starting wait @ %d\n", pthread_self (), + (int) before.tv_sec); + + if (foo % 2 == 0) + err = pthread_rwlock_timedrdlock (&rwlock, &ts); + else + err = pthread_rwlock_timedwrlock (&rwlock, &ts); + + assert (err == ETIMEDOUT); + + gettimeofday (&after, 0); + + printf ("Thread %d ending wait @ %d\n", pthread_self (), + (int) after.tv_sec); + + diff = after.tv_sec * 1000000 + after.tv_usec + - before.tv_sec * 1000000 - before.tv_usec; + + if (diff < 900000 || diff > 1100000) + error (1, EGRATUITOUS, "pthread_mutex_timedlock waited %d us", diff); + + return 0; +} + +int +main (int argc, char **argv) +{ + error_t err; + int i; + pthread_t tid[THREADS]; + + err = pthread_rwlock_init (&rwlock, 0); + if (err) + error (1, err, "pthread_rwlock_init"); + + /* Lock it so all the threads will block. */ + err = pthread_rwlock_wrlock (&rwlock); + assert (err == 0); + + for (i = 0; i < THREADS; i ++) + { + err = pthread_create (&tid[i], 0, test, (void *) i); + if (err) + error (1, err, "pthread_create"); + } + + for (i = 0; i < THREADS; i ++) + { + void *ret; + + err = pthread_join (tid[i], &ret); + if (err) + error (1, err, "pthread_join"); + + assert (ret == 0); + } + + return 0; +} diff --git a/tests/test-16.c b/tests/test-16.c new file mode 100644 index 00000000..b6a52d01 --- /dev/null +++ b/tests/test-16.c @@ -0,0 +1,71 @@ +/* Test pthread_kill.c. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <signal.h> +#include <stdio.h> +#include <assert.h> +#include <error.h> +#include <errno.h> +#include <hurd/signal.h> + +pthread_t testthread; + +int i; + +void * +test (void *arg) +{ + error_t err; + + printf ("test: %d\n", pthread_self ()); + + err = pthread_kill (pthread_self (), SIGINFO); + if (err) + error (1, err, "pthread_kill"); + + /* To avoid using condition variables in a signal handler. */ + while (i == 0) + sched_yield (); + + return 0; +} + +static void +handler (int sig, siginfo_t *info, void *context) +{ + assert (pthread_equal (pthread_self (), testthread)); + printf ("handler: %d\n", pthread_self ()); + i = 1; +} + +int +main (int argc, char **argv) +{ + error_t err; + struct sigaction sa; + void *ret; + + printf ("main: %d\n", pthread_self ()); + + sa.sa_handler = handler; + sa.sa_mask = 0; + sa.sa_flags = 0; + + err = sigaction (SIGINFO, &sa, 0); + if (err) + error (1, err, "sigaction"); + + err = pthread_create (&testthread, 0, test, 0); + if (err) + error (1, err, "pthread_create"); + + err = pthread_join (testthread, &ret); + if (err) + error (1, err, "pthread_join"); + + assert (ret == 0); + + return 0; +} diff --git a/tests/test-2.c b/tests/test-2.c new file mode 100644 index 00000000..701462e8 --- /dev/null +++ b/tests/test-2.c @@ -0,0 +1,39 @@ +/* Test detachability. */ +#define _GNU_SOURCE + +#include <pthread.h> +#include <assert.h> +#include <error.h> +#include <errno.h> +#include <unistd.h> + +void * +thread (void *arg) +{ + while (1) + sched_yield (); +} + +int +main (int argc, char **argv) +{ + int err; + pthread_t tid; + void *ret; + + err = pthread_create (&tid, 0, thread, 0); + if (err) + error (1, err, "pthread_create"); + + err = pthread_detach (tid); + if (err) + error (1, err, "pthread_detach"); + + err = pthread_detach (tid); + assert (err == EINVAL); + + err = pthread_join (tid, &ret); + assert (err == EINVAL); + + return 0; +} diff --git a/tests/test-3.c b/tests/test-3.c new file mode 100644 index 00000000..7db2e43f --- /dev/null +++ b/tests/test-3.c @@ -0,0 +1,55 @@ +/* Test the thread attribute get and set methods. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <sched.h> +#include <assert.h> +#include <errno.h> + +int +main (int argc, char *argv[]) +{ + error_t err; + pthread_attr_t attr; + + int i; + struct sched_param sp; + void *p; + size_t sz; + + err = pthread_attr_init (&attr); + assert_perror (err); + + err = pthread_attr_destroy (&attr); + assert_perror (err); + + err = pthread_attr_init (&attr); + assert_perror (err); + +#define TEST1(foo, rv, v) \ + err = pthread_attr_get##foo (&attr, rv); \ + assert_perror (err); \ + \ + err = pthread_attr_set##foo (&attr, v); \ + assert_perror (err); + +#define TEST(foo, rv, v) TEST1(foo, rv, v) + + TEST(inheritsched, &i, i); + TEST(schedparam, &sp, &sp); + TEST(schedpolicy, &i, i); + TEST(scope, &i, i); + TEST(stackaddr, &p, p); + TEST(detachstate, &i, i); + TEST(guardsize, &sz, sz); + TEST(stacksize, &sz, sz); + + err = pthread_attr_getstack (&attr, &p, &sz); + assert_perror (err); + + err = pthread_attr_setstack (&attr, p, sz); + assert_perror (err); + + return 0; +} diff --git a/tests/test-4.c b/tests/test-4.c new file mode 100644 index 00000000..de9c8fe4 --- /dev/null +++ b/tests/test-4.c @@ -0,0 +1,86 @@ +/* Test the stack guard. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <assert.h> +#include <unistd.h> +#include <errno.h> +#include <string.h> +#include <stdio.h> + +size_t stacksize; + +void * +thr (void *arg) +{ + int i; + char *foo; + + foo = alloca (3 * stacksize / 4); + for (i = 0; i < sizeof foo; i ++) + foo[i] = -1; + + return (void *) 1; +} + +int +main (int argc, char *argv[]) +{ + error_t err; + pid_t child; + + child = fork (); + switch (child) + { + case -1: + error (1, errno, "fork"); + break; + + case 0: + { + pthread_attr_t attr; + pthread_t tid; + void *ret; + + err = pthread_attr_init (&attr); + assert_perror (err); + + err = pthread_attr_getstacksize (&attr, &stacksize); + assert_perror (err); + + err = pthread_attr_setguardsize (&attr, stacksize / 2); + if (err == ENOTSUP) + { + printf ("Stack guard attribute not supported.\n"); + return 1; + } + assert_perror (err); + + err = pthread_create (&tid, &attr, thr, 0); + assert_perror (err); + + err = pthread_attr_destroy (&attr); + assert_perror (err); + + err = pthread_join (tid, &ret); + /* Should never be successful. */ + printf ("Thread did not segfault!?!\n"); + assert_perror (err); + return 0; + } + + default: + { + pid_t pid; + int status; + + pid = waitpid (child, &status, 0); + printf ("pid = %d; child = %d; status = %d\n", pid, child, status); + assert (pid == child); + assert (status != 0); + } + } + + return 0; +} diff --git a/tests/test-5.c b/tests/test-5.c new file mode 100644 index 00000000..0f5000b2 --- /dev/null +++ b/tests/test-5.c @@ -0,0 +1,75 @@ +/* Test signals. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <unistd.h> +#include <stdio.h> +#include <errno.h> +#include <error.h> +#include <assert.h> +#include <sys/resource.h> +#include <sys/wait.h> + +void * +thr (void *arg) +{ + * (int *)0 = 0; + return 0; +} + +int foobar; + +int +main (int argc, char *argv[]) +{ + error_t err; + pid_t child; + + struct rlimit limit; + + limit.rlim_cur = 0; + limit.rlim_max = 0; + + err = setrlimit (RLIMIT_CORE, &limit); + if (err) + error (1, err, "setrlimit"); + + child = fork (); + switch (child) + { + case -1: + error (1, errno, "fork"); + break; + + case 0: + { + pthread_t tid; + void *ret; + + err = pthread_create (&tid, 0, thr, 0); + if (err) + error (1, err, "pthread_create"); + + err = pthread_join (tid, &ret); + assert_perror (err); + + /* Should have never returned. Our parent expects us to fail + thus we succeed and indicate the error. */ + return 0; + } + + default: + { + pid_t pid; + int status; + + pid = waitpid (child, &status, 0); + printf ("pid = %d; child = %d; status = %d\n", pid, child, status); + assert (pid == child); + assert (status != 0); + } + } + + return 0; +} diff --git a/tests/test-6.c b/tests/test-6.c new file mode 100644 index 00000000..98aa8ba7 --- /dev/null +++ b/tests/test-6.c @@ -0,0 +1,95 @@ +#define _GNU_SOURCE + +#include <pthread.h> +#include <stdio.h> +#include <error.h> +#include <errno.h> + +#define THREADS 500 +#define WAITS 3 + +void * +dowait (void *arg) +{ + pthread_barrier_t *barrier = arg; + int ret; + + ret = pthread_barrier_wait (barrier); + printf ("%d ", pthread_self ()); + return (void *) ret; +} + +int +main (int argc, char **argv) +{ + pthread_barrierattr_t attr; + pthread_barrier_t barrier; + + int i, j; + error_t err; + pthread_t tid[THREADS]; + + int havesyncs; + + err = pthread_barrierattr_init (&attr); + if (err) + error (1, err, "pthread_barrierattr_init"); + + err = pthread_barrierattr_getpshared (&attr, &i); + if (err) + error (1, err, "pthread_barrierattr_getpshared"); + assert (i == PTHREAD_PROCESS_PRIVATE || i == PTHREAD_PROCESS_SHARED); + + err = pthread_barrierattr_setpshared (&attr, PTHREAD_PROCESS_PRIVATE); + if (err) + error (1, err, "pthread_barrierattr_setpshared"); + + err = pthread_barrier_init (&barrier, &attr, THREADS + 1); + if (err) + error (1, err, "pthread_barrier_init"); + + for (j = 0; j < WAITS; j ++) + { + + for (i = 0; i < THREADS; i ++) + { + err = pthread_create (&tid[i], 0, dowait, &barrier); + if (err) + error (1, err, "pthread_create (%d)", i); + } + + printf ("Manager will now call pthread_barrier_wait.\n"); + + havesyncs + = pthread_barrier_wait (&barrier) == PTHREAD_BARRIER_SERIAL_THREAD + ? 1 : 0; + + for (i = THREADS - 1; i >= 0; i --) + { + void *ret; + err = pthread_join (tid[i], &ret); + if (err) + error (1, err, "pthread_join"); + + switch ((int) ret) + { + case 0: + break; + + case PTHREAD_BARRIER_SERIAL_THREAD: + havesyncs ++; + break; + + default: + assert (! "Unknown value returned from pthread_barrier_wait."); + break; + } + } + + printf ("\n"); + + assert (havesyncs == 1); + } + + return 0; +} diff --git a/tests/test-7.c b/tests/test-7.c new file mode 100644 index 00000000..bd97acfa --- /dev/null +++ b/tests/test-7.c @@ -0,0 +1,66 @@ +#define _GNU_SOURCE + +#include <pthread.h> +#include <stdio.h> +#include <error.h> +#include <errno.h> + +#define THREADS 10 +#define KEYS 400 + +pthread_key_t key[KEYS]; + +void * +thr (void *arg) +{ + error_t err; + int i; + + for (i = 0; i < KEYS; i ++) + { + printf ("pthread_getspecific(%d).\n", key[i]); + assert (pthread_getspecific (key[i]) == NULL); + printf ("pthread_setspecific(%d, %d).\n", key[i], pthread_self ()); + err = pthread_setspecific (key[i], (void *) pthread_self ()); + printf ("pthread_setspecific(%d, %d) => %d.\n", key[i], pthread_self (), err); + assert_perror (err); + } + + return 0; +} + +int +main (int argc, char **argv) +{ + error_t err; + int i; + pthread_t tid[THREADS]; + + void des (void *val) + { + assert ((pthread_t) val == pthread_self ()); + } + + for (i = 0; i < KEYS; i ++) + err = pthread_key_create (&key[i], des); + + for (i = 0; i < THREADS; i ++) + { + err = pthread_create (&tid[i], 0, thr, 0); + if (err) + error (1, err, "pthread_create (%d)", i); + } + + for (i = 0; i < THREADS; i ++) + { + void *ret; + + err = pthread_join (tid[i], &ret); + if (err) + error (1, err, "pthread_join"); + + assert (ret == 0); + } + + return 0; +} diff --git a/tests/test-8.c b/tests/test-8.c new file mode 100644 index 00000000..85a7f8f6 --- /dev/null +++ b/tests/test-8.c @@ -0,0 +1,60 @@ +#define _GNU_SOURCE + +#include <pthread.h> +#include <assert.h> +#include <error.h> +#include <errno.h> + +#define THREADS 10 + +pthread_once_t inc_var_once = PTHREAD_ONCE_INIT; +int var; + +void +inc_var (void) +{ + var ++; +} + +void * +thr (void *arg) +{ + int i; + + for (i = 0; i < 500; i ++) + pthread_once (&inc_var_once, inc_var); + + return 0; +} + +int +main (int argc, char **argv) +{ + error_t err; + int i; + pthread_t tid[THREADS]; + + for (i = 0; i < THREADS; i ++) + { + err = pthread_create (&tid[i], 0, thr, 0); + if (err) + error (1, err, "pthread_create (%d)", i); + } + + assert (thr (0) == 0); + + for (i = 0; i < THREADS; i ++) + { + void *ret; + + err = pthread_join (tid[i], &ret); + if (err) + error (1, err, "pthread_join"); + + assert (ret == 0); + } + + assert (var == 1); + + return 0; +} diff --git a/tests/test-9.c b/tests/test-9.c new file mode 100644 index 00000000..82051570 --- /dev/null +++ b/tests/test-9.c @@ -0,0 +1,88 @@ +/* Test recursive mutexes. */ + +#define _GNU_SOURCE + +#include <pthread.h> +#include <assert.h> +#include <error.h> +#include <errno.h> + +#define THREADS 10 + +int foo; + +void * +thr (void *arg) +{ + int i; + + pthread_mutex_lock (arg); + + foo = pthread_self (); + + for (i = 0; i < 500; i ++) + pthread_mutex_lock (arg); + for (i = 0; i < 500; i ++) + pthread_mutex_unlock (arg); + + assert (foo == pthread_self ()); + + pthread_mutex_unlock (arg); + + return 0; +} + +int +main (int argc, char **argv) +{ + error_t err; + int i; + pthread_t tid[THREADS]; + pthread_mutexattr_t mattr; + pthread_mutex_t mutex; + + err = pthread_mutexattr_init (&mattr); + if (err) + error (1, err, "pthread_mutexattr_init"); + + err = pthread_mutexattr_settype (&mattr, PTHREAD_MUTEX_RECURSIVE); + if (err) + error (1, err, "pthread_mutexattr_settype"); + + err = pthread_mutex_init (&mutex, &mattr); + if (err) + error (1, err, "pthread_mutex_init"); + + err = pthread_mutexattr_destroy (&mattr); + if (err) + error (1, err, "pthread_mutexattr_destroy"); + + pthread_mutex_lock (&mutex); + pthread_mutex_lock (&mutex); + pthread_mutex_unlock (&mutex); + pthread_mutex_unlock (&mutex); + + for (i = 0; i < THREADS; i ++) + { + err = pthread_create (&tid[i], 0, thr, &mutex); + if (err) + error (1, err, "pthread_create (%d)", i); + } + + for (i = 0; i < THREADS; i ++) + { + void *ret; + + err = pthread_join (tid[i], &ret); + if (err) + error (1, err, "pthread_join"); + + assert (ret == 0); + } + + err = pthread_mutex_destroy (&mutex); + if (err) + error (1, err, "pthread_mutex_destroy"); + + return 0; +} |