summaryrefslogtreecommitdiff
path: root/pthread
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@gnu.org>2007-11-20 16:23:24 +0000
committerThomas Schwinge <tschwinge@gnu.org>2009-04-07 23:05:35 +0200
commit43e3246eb6a26162ef931dadcd4a8a649c57289b (patch)
tree80cac95ce1c8b948f2a2a4ff980385b0dcbeec43 /pthread
parent7373049ff9aac3dbfe088da7d30b2724d15fa67a (diff)
parent8caaa8379e5a1500863792514189d6e8f414dcaf (diff)
2007-11-20 Neal H. Walfield <neal@gnu.org>
Merge changes from mainline Hurd. Update L4 bits to compile with those changes. * sysdeps/l4/pt-block.c (__pthread_block): Call l4_receive, not L4_Receive. * sysdeps/l4/pt-create-np.c (pthread_create_from_l4_tid_np): Don't pass TID to __pthread_create_internal. Emit a warning. * sysdeps/l4/pt-stack-alloc.c (allocate_page): Remove function. (__pthread_stack_alloc): Don't require that STACKSIZE is equal to __pthread_stacksize. Call mmap. * sysdeps/l4/pt-thread-halt.c (__pthread_thread_halt): Take additional argument, need_dealloc. Call __pthread_dealloc. Stop the thread. * sysdeps/l4/hurd/pt-sysdep.c (init_routine): When calling __pthread_create_internal, don't pass the tid. * tests/test-1.c (main): Use pthread_mutex_init, not PTHREAD_MUTEX_INITIALIZER. * pthread/pt-alloc.c: Don't include <bits/atomic.h>. Include <atomic.h>. (__pthread_free_threads): Make it an atomicptr_t, not an __atomicptr_t. (__pthread_alloc): Don't use __atomicptr_compare_and_swap, use atomic_compare_and_exchange_val_acq. * pthread/pt-create.c: Don't include <bits/atomic.h>. Include <atomic.h>. (__pthread_total): Make it an atomic_fast32_t, not an __atomic_t. (__pthread_create_internal): Use atomic_increment and atomic_decrement, not __atomic_inc and __atomic_dec. * pthread/pt-dealloc.c: Don't include <bits/atomic.h>. Include <atomic.h>. (__pthread_free_threads): Make it an atomicptr_t, not an __atomicptr_t. (__pthread_dealloc): Use atomic_compare_and_exchange_val_acq, not __atomicptr_compare_and_swap. * pthread/pt-exit.c: Don't include <bits/atomic.h>. Include <atomic.h>. (pthread_exit): Use atomic_decrement_and_test, not __atomic_dec_and_test. * pthread/pt-internal.h: Don't include <bits/atomic.h>. Include <atomic.h>. (__pthread_total): Make it an atomic_fast32_t, not an __atomic_t. * sysdeps/powerpc/bits/atomic.h: Remove file. * sysdeps/ia32/bits/atomic.h: Likewise.
Diffstat (limited to 'pthread')
-rw-r--r--pthread/pt-alloc.c28
-rw-r--r--pthread/pt-create.c33
-rw-r--r--pthread/pt-dealloc.c8
-rw-r--r--pthread/pt-detach.c4
-rw-r--r--pthread/pt-exit.c13
-rw-r--r--pthread/pt-internal.h60
6 files changed, 77 insertions, 69 deletions
diff --git a/pthread/pt-alloc.c b/pthread/pt-alloc.c
index 615b7289..6cf9106b 100644
--- a/pthread/pt-alloc.c
+++ b/pthread/pt-alloc.c
@@ -1,5 +1,5 @@
/* Allocate a new thread structure.
- Copyright (C) 2000, 2002 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2002, 2005, 2007 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
@@ -46,7 +46,7 @@ pthread_rwlock_t __pthread_threads_lock;
/* List of thread structures corresponding to free thread IDs. */
-uatomicptr_t __pthread_free_threads;
+atomicptr_t __pthread_free_threads;
static inline error_t
initialize_pthread (struct __pthread *new, int recycling)
@@ -69,8 +69,8 @@ initialize_pthread (struct __pthread *new, int recycling)
new->stack = 0;
- new->state_lock = (struct __pthread_mutex) PTHREAD_MUTEX_INITIALIZER;
- new->state_cond = (struct __pthread_cond) PTHREAD_COND_INITIALIZER;
+ new->state_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
+ new->state_cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
new->cancelation_handlers = 0;
@@ -98,7 +98,9 @@ __pthread_alloc (struct __pthread **pthread)
while ((new = (struct __pthread *)__pthread_free_threads))
{
if (atomic_compare_and_exchange_val_acq (&__pthread_free_threads,
- new, new->next))
+ (uintptr_t) new->next,
+ (uintptr_t) new)
+ == (uintptr_t) new)
{
/* Yes, we managed to get one. The thread number in the
thread structure still refers to the correct slot. */
@@ -111,7 +113,9 @@ __pthread_alloc (struct __pthread **pthread)
{
new->next = (struct __pthread *)__pthread_free_threads;
if (atomic_compare_and_exchange_val_acq
- (&__pthread_free_threads, new->next, new))
+ (&__pthread_free_threads,
+ (uintptr_t) new, (uintptr_t) new->next)
+ == (uintptr_t) new->next)
break;
}
@@ -139,10 +143,10 @@ __pthread_alloc (struct __pthread **pthread)
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;
+ /* We have a free slot. Use the slot number plus one as the
+ thread ID for the new thread. */
+ new->thread = 1 + __pthread_num_threads++;
+ __pthread_threads[new->thread - 1] = NULL;
pthread_rwlock_unlock (&__pthread_threads_lock);
@@ -203,8 +207,8 @@ __pthread_alloc (struct __pthread **pthread)
__pthread_threads = threads;
/* And allocate ourselves one of the newly created slots. */
- new->thread = __pthread_num_threads++;
- __pthread_threads[new->thread] = NULL;
+ new->thread = 1 + __pthread_num_threads++;
+ __pthread_threads[new->thread - 1] = NULL;
pthread_rwlock_unlock (&__pthread_threads_lock);
diff --git a/pthread/pt-create.c b/pthread/pt-create.c
index 8d348fb9..4f8d043f 100644
--- a/pthread/pt-create.c
+++ b/pthread/pt-create.c
@@ -1,5 +1,5 @@
/* Thread creation.
- Copyright (C) 2000, 2002, 2004 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2002, 2005, 2007 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
@@ -33,7 +33,7 @@
/* 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'. */
-uatomic32_t __pthread_total;
+atomic_fast32_t __pthread_total;
/* The entry-point for new threads. */
@@ -57,7 +57,7 @@ pthread_create (pthread_t *thread, const pthread_attr_t *attr,
int err;
struct __pthread *pthread;
- err = __pthread_create_internal (&pthread, attr, 0, start_routine, arg);
+ err = __pthread_create_internal (&pthread, attr, start_routine, arg);
if (! err)
*thread = pthread->thread;
@@ -69,7 +69,6 @@ pthread_create (pthread_t *thread, const pthread_attr_t *attr,
int
__pthread_create_internal (struct __pthread **thread,
const pthread_attr_t *attr,
- void *provided_thread,
void *(*start_routine)(void *), void *arg)
{
int err;
@@ -123,20 +122,10 @@ __pthread_create_internal (struct __pthread **thread,
pthread->stack = 1;
}
- /* Allocate the kernel thread and other required resources
- if they were not provided with this call. */
- if (!provided_thread)
- {
- err = __pthread_thread_alloc (pthread);
- if (err)
- goto failed_thread_alloc;
- }
- else
- {
- err = __pthread_init_provided_thread (pthread, provided_thread);
- if (err)
- goto failed_thread_alloc;
- }
+ /* 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
@@ -157,8 +146,7 @@ __pthread_create_internal (struct __pthread **thread,
shall be empty." If the currnet thread is not a pthread then we
just inherit the process' sigmask. */
if (__pthread_num_threads == 1)
- /* FIXME no sigprocmask yet */
- err = 0; /* sigprocmask (0, 0, &sigset); */
+ err = sigprocmask (0, 0, &sigset);
else
err = __pthread_sigstate (_pthread_self (), 0, 0, &sigset, 0);
assert_perror (err);
@@ -180,7 +168,7 @@ __pthread_create_internal (struct __pthread **thread,
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_threads[pthread->thread - 1] = pthread;
pthread_rwlock_unlock (&__pthread_threads_lock);
/* At this point it is possible to guess our pthread ID. We have to
@@ -204,7 +192,8 @@ __pthread_create_internal (struct __pthread **thread,
failed_sigstate:
__pthread_sigstate_destroy (pthread);
failed_setup:
- __pthread_thread_halt (pthread);
+ __pthread_thread_dealloc (pthread);
+ __pthread_thread_halt (pthread, 0);
failed_thread_alloc:
__pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize);
pthread->stack = 0;
diff --git a/pthread/pt-dealloc.c b/pthread/pt-dealloc.c
index f89f3545..879608b9 100644
--- a/pthread/pt-dealloc.c
+++ b/pthread/pt-dealloc.c
@@ -26,7 +26,7 @@
#include <atomic.h>
/* List of thread structures corresponding to free thread IDs. */
-extern uatomicptr_t __pthread_free_threads;
+extern atomicptr_t __pthread_free_threads;
/* Deallocate the thread structure for PTHREAD and the resources
associated with it. */
@@ -55,8 +55,10 @@ __pthread_dealloc (struct __pthread *pthread)
{
pthread->next = (struct __pthread *)__pthread_free_threads;
if (atomic_compare_and_exchange_val_acq (&__pthread_free_threads,
- pthread->next, pthread))
- return;
+ (uintptr_t) pthread,
+ (uintptr_t) pthread->next)
+ == (uintptr_t) pthread->next)
+ break;
}
/* NOTREACHED */
diff --git a/pthread/pt-detach.c b/pthread/pt-detach.c
index c22f6a03..42a84080 100644
--- a/pthread/pt-detach.c
+++ b/pthread/pt-detach.c
@@ -1,5 +1,5 @@
/* Detach a thread.
- Copyright (C) 2000 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2005 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
@@ -58,8 +58,6 @@ pthread_detach (pthread_t thread)
__pthread_mutex_unlock (&pthread->state_lock);
- __pthread_thread_halt (pthread);
-
assert (pthread->stack);
__pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize);
pthread->stack = 0;
diff --git a/pthread/pt-exit.c b/pthread/pt-exit.c
index 68f596dc..a8f85b1a 100644
--- a/pthread/pt-exit.c
+++ b/pthread/pt-exit.c
@@ -1,5 +1,5 @@
/* Thread termination.
- Copyright (C) 2000, 2002, 2004 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2002, 2005 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
@@ -35,6 +35,7 @@ pthread_exit (void *status)
struct __pthread *self = _pthread_self ();
struct __pthread_cancelation_handler **handlers;
int oldstate;
+ int need_dealloc;
/* Run any cancelation handlers. According to POSIX, the
cancellation cleanup handlers should be called with cancellation
@@ -69,10 +70,13 @@ pthread_exit (void *status)
if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending)
status = PTHREAD_CANCELED;
+ __pthread_thread_dealloc (self);
+
switch (self->state)
{
default:
- assert (! "This cannot happen!");
+ assert (! "Consistency error: unexpected self->state");
+ abort ();
break;
case PTHREAD_DETACHED:
@@ -82,7 +86,7 @@ pthread_exit (void *status)
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);
+ need_dealloc = 1;
break;
@@ -99,6 +103,7 @@ pthread_exit (void *status)
waiting to join us. */
pthread_cond_broadcast (&self->state_cond);
__pthread_mutex_unlock (&self->state_lock);
+ need_dealloc = 0;
break;
}
@@ -108,7 +113,7 @@ pthread_exit (void *status)
This means that before freeing any resources, such a thread
should make sure that this thread is really halted. */
- __pthread_thread_halt (self);
+ __pthread_thread_halt (self, need_dealloc);
/* NOTREACHED */
abort ();
diff --git a/pthread/pt-internal.h b/pthread/pt-internal.h
index 436f8703..0dd4e9a8 100644
--- a/pthread/pt-internal.h
+++ b/pthread/pt-internal.h
@@ -1,5 +1,5 @@
/* Internal defenitions for pthreads library.
- Copyright (C) 2000, 2004 Free Software Foundation, Inc.
+ Copyright (C) 2000, 2005 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
@@ -21,12 +21,11 @@
#define _PT_INTERNAL_H 1
#include <pthread.h>
-#if 0
#include <stddef.h>
#include <sched.h>
#include <signal.h>
#include <assert.h>
-#endif
+
#include <atomic.h>
#include <pt-key.h>
@@ -126,7 +125,7 @@ __pthread_dequeue (struct __pthread *thread)
element = element->next)
/* The total number of threads currently active. */
-extern uatomic32_t __pthread_total;
+extern atomic_fast32_t __pthread_total;
/* The total number of thread IDs currently in use, or on the list of
available thread IDs. */
@@ -135,20 +134,23 @@ extern int __pthread_num_threads;
/* Concurrency hint. */
extern int __pthread_concurrency;
-/* Array of __pthread structures and its lock. */
+/* Array of __pthread structures and its lock. Indexed by the pthread
+ id minus one. (Why not just use the pthread id? Because some
+ brain-dead users of the pthread interface incorrectly assume that 0
+ is an invalid pthread id.) */
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]; \
+ __t = __pthread_threads[thread - 1]; \
pthread_rwlock_unlock (&__pthread_threads_lock); \
__t; })
#define __pthread_setid(thread, pthread) \
pthread_rwlock_wrlock (&__pthread_threads_lock); \
- __pthread_threads[thread] = pthread; \
+ __pthread_threads[thread - 1] = pthread; \
pthread_rwlock_unlock (&__pthread_threads_lock);
/* Similar to pthread_self, but returns the thread descriptor instead
@@ -163,11 +165,10 @@ 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 *provided_thread,
+extern int __pthread_create_internal (struct __pthread **__restrict pthread,
+ const pthread_attr_t *__restrict attr,
void *(*start_routine)(void *),
- void *arg);
+ void *__restrict arg);
/* Allocate a new thread structure and a pthread thread ID (but not a
kernel thread or a stack). */
@@ -188,33 +189,41 @@ extern void __pthread_stack_dealloc (void *stackaddr, size_t stacksize);
/* Setup thread THREAD's context. */
-extern int __pthread_setup (struct __pthread *thread,
+extern int __pthread_setup (struct __pthread *__restrict thread,
void (*entry_point)(void *(*)(void *),
void *),
- void *(*start_routine)(void *), void *arg);
+ void *(*start_routine)(void *),
+ void *__restrict arg);
-/* Allocate a kernel thread for THREAD; it must not be placed on the
- run queue. */
+/* Allocate a kernel thread (and any miscellaneous system dependent
+ resources) for THREAD; it must not be placed on the run queue. */
extern int __pthread_thread_alloc (struct __pthread *thread);
+/* Deallocate any kernel resources associated with THREAD except don't
+ halt the thread itself. On return, the thread will be marked as
+ dead and __pthread_halt will be called. */
+extern void __pthread_thread_dealloc (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);
+/* Stop the kernel thread associated with THREAD. If NEED_DEALLOC is
+ true, the function must call __pthread_dealloc on THREAD.
+
+ NB: The thread executing this function may be the thread which is
+ being halted, thus the last action should be halting the thread
+ itself. */
+extern void __pthread_thread_halt (struct __pthread *thread,
+ int need_dealloc);
-/* Initialize provided kernel thread. */
-extern int __pthread_init_provided_thread (struct __pthread *thread,
- void *p);
/* 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);
+extern error_t __pthread_timedblock (struct __pthread *__restrict thread,
+ const struct timespec *__restrict abstime);
/* Wakeup THREAD. */
extern void __pthread_wakeup (struct __pthread *thread);
@@ -242,8 +251,9 @@ extern error_t __pthread_sigstate_init (struct __pthread *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,
+extern error_t __pthread_sigstate (struct __pthread *__restrict thread, int how,
+ const sigset_t *__restrict set,
+ sigset_t *__restrict oset,
int clear_pending);