bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC] Enable thread termination


From: Richard Braun
Subject: [RFC] Enable thread termination
Date: Sun, 17 Nov 2013 17:13:23 +0100

The following changes actually apply to Debian eglibc 2.17-96 sources,
and aren't meant to be pushed as such, but rather to review the various
solutions added to support thread termination.

The current state is to never terminate threads, on the assumption that
they can't both terminate and release their stack on their own. Such
resources are recycled by the threading library. This patch makes use
of a new GNU Mach specific call (thread_terminate_release [1]) so that
threads do terminate themselves and release their stack, and in addition
their last self reference, and their reply port.

To avoid right leaks, the sigstate maintains its own reference on its
associated thread. Reusing the self reference created from libpthread
isn't possible because the sigstate of the main thread is created
before (actually during, because of _hurd_critical_section_lock being
called early) libpthread initialization, when there is no self reference
for the main thread yet. This relies on GNU Mach always using the same
name for already existing send rights (i.e. mach_task_self always
returns the same name as long as it's not destroyed).

Joining/detaching relies on pthread structures maintaining a reference
counter. Joinable threads have 2 references, detached threads only one
(a self reference). This replaces the previous mechanism that forced
thread to halt before checking their state.

A few static variables were added to record that initialization had been
done, instead of merely looking at the current number of threads.

Two resources are still recycled: the internal pthread structure
(because it makes ID allocation easy), and thread local storage (TLS)
because that's where the reply port is stored, and it didn't seem
convenient to call _dl_deallocate_tls in sysdeps code.

[1] 
http://git.savannah.gnu.org/cgit/hurd/gnumach.git/commit/?h=rbraun/thread_terminate_release
---
 hurd/hurd/signal.h                                 | 20 ++++--
 hurd/hurdsig.c                                     | 15 ++++-
 libpthread/Makefile                                |  2 +-
 libpthread/Makefile.am                             |  2 +-
 libpthread/pthread/pt-alloc.c                      | 21 ++-----
 libpthread/pthread/pt-create.c                     | 69 ++++++++++-----------
 libpthread/pthread/pt-dealloc.c                    |  5 ++
 libpthread/pthread/pt-detach.c                     | 24 ++------
 libpthread/pthread/pt-exit.c                       | 33 +++++-----
 libpthread/pthread/pt-internal.h                   | 36 ++++++-----
 libpthread/pthread/pt-join.c                       | 18 +-----
 libpthread/sysdeps/mach/hurd/pt-init-specific.c    |  1 -
 libpthread/sysdeps/mach/hurd/pt-sigstate-destroy.c |  2 +-
 libpthread/sysdeps/mach/hurd/pt-sigstate-init.c    |  6 +-
 libpthread/sysdeps/mach/hurd/pt-sysdep.c           |  5 ++
 libpthread/sysdeps/mach/hurd/pt-sysdep.h           |  3 +-
 libpthread/sysdeps/mach/pt-thread-alloc.c          | 31 +++-------
 libpthread/sysdeps/mach/pt-thread-dealloc.c        |  5 +-
 libpthread/sysdeps/mach/pt-thread-halt.c           | 54 ----------------
 libpthread/sysdeps/mach/pt-thread-start.c          |  4 +-
 libpthread/sysdeps/mach/pt-thread-terminate.c      | 72 ++++++++++++++++++++++
 sysdeps/mach/hurd/i386/tls.h                       | 13 ++--
 22 files changed, 215 insertions(+), 226 deletions(-)
 delete mode 100644 libpthread/sysdeps/mach/pt-thread-halt.c
 create mode 100644 libpthread/sysdeps/mach/pt-thread-terminate.c

diff --git a/hurd/hurd/signal.h b/hurd/hurd/signal.h
index 863cdac..3396266 100644
--- a/hurd/hurd/signal.h
+++ b/hurd/hurd/signal.h
@@ -64,7 +64,9 @@ struct hurd_sigstate
 
     spin_lock_t lock;          /* Locks most of the rest of the structure.  */
 
+    /* The signal state holds a reference on the thread port.  */
     thread_t thread;
+
     struct hurd_sigstate *next; /* Linked-list of thread sigstates.  */
 
     sigset_t blocked;          /* What signals are blocked.  */
@@ -118,7 +120,9 @@ extern struct hurd_sigstate *_hurd_sigstates;
 
 extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates.  */
 
-/* Get the sigstate of a given thread, taking its lock.  */
+/* Get the sigstate of a given thread.  If there was no sigstate for
+   the thread, one is created, and the thread gains a reference.  If
+   the given thread is MACH_PORT_NULL, return the global sigstate.  */
 
 extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
 
@@ -161,7 +165,11 @@ _HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
 _hurd_self_sigstate (void)
 {
   if (_hurd_sigstate == NULL)
-    _hurd_sigstate = _hurd_thread_sigstate (__mach_thread_self ());
+    {
+      thread_t self = __mach_thread_self ();
+      _hurd_sigstate = _hurd_thread_sigstate (self);
+      __mach_port_deallocate (__mach_task_self (), self);
+    }
   return _hurd_sigstate;
 }
 #endif
@@ -202,12 +210,14 @@ _hurd_critical_section_lock (void)
   ss = _hurd_sigstate;
   if (ss == NULL)
     {
+      thread_t self = __mach_thread_self ();
+
       /* The thread variable is unset; this must be the first time we've
         asked for it.  In this case, the critical section flag cannot
         possible already be set.  Look up our sigstate structure the slow
-        way; this locks the sigstate lock.  */
-      ss = _hurd_sigstate = _hurd_thread_sigstate (__mach_thread_self ());
-      __spin_unlock (&ss->lock);
+        way.  */
+      ss = _hurd_sigstate = _hurd_thread_sigstate (self);
+      __mach_port_deallocate (__mach_task_self (), self);
     }
 
   if (! __spin_try_lock (&ss->critical_section_lock))
diff --git a/hurd/hurdsig.c b/hurd/hurdsig.c
index 6e2e4d0..fa95ac8 100644
--- a/hurd/hurdsig.c
+++ b/hurd/hurdsig.c
@@ -109,6 +109,8 @@ _hurd_thread_sigstate (thread_t thread)
        }
       else
        {
+         error_t err;
+
          /* Use the global actions as a default for new threads.  */
          struct hurd_sigstate *s = _hurd_global_sigstate;
          if (s)
@@ -122,6 +124,11 @@ _hurd_thread_sigstate (thread_t thread)
 
          ss->next = _hurd_sigstates;
          _hurd_sigstates = ss;
+
+         err = __mach_port_mod_refs (__mach_task_self (), thread,
+                                     MACH_PORT_RIGHT_SEND, 1);
+         if (err)
+           __libc_fatal ("hurd: Can't add reference on Mach thread\n");
        }
     }
   __mutex_unlock (&_hurd_siglock);
@@ -129,8 +136,7 @@ _hurd_thread_sigstate (thread_t thread)
 }
 
 /* Destroy a sigstate structure.  Called by libpthread just before the
- * corresponding thread is terminated (the kernel thread port must remain valid
- * until this function is called.) */
+ * corresponding thread is terminated.  */
 void
 _hurd_sigstate_delete (thread_t thread)
 {
@@ -147,7 +153,10 @@ _hurd_sigstate_delete (thread_t thread)
 
   __mutex_unlock (&_hurd_siglock);
   if (ss)
-    free (ss);
+    {
+      __mach_port_deallocate (__mach_task_self (), ss->thread);
+      free (ss);
+    }
 }
 
 /* Make SS a global receiver, with pthread signal semantics.  */
diff --git a/libpthread/Makefile b/libpthread/Makefile
index 6d57fc5..3195834 100644
--- a/libpthread/Makefile
+++ b/libpthread/Makefile
@@ -121,7 +121,7 @@ libpthread-routines := pt-attr pt-attr-destroy 
pt-attr-getdetachstate           \
        pt-thread-alloc                                                     \
        pt-thread-dealloc                                                   \
        pt-thread-start                                                     \
-       pt-thread-halt                                                      \
+       pt-thread-terminate                                                 \
        pt-startup                                                          \
                                                                            \
        pt-getconcurrency pt-setconcurrency                                 \
diff --git a/libpthread/Makefile.am b/libpthread/Makefile.am
index 430d7f4..4a9add0 100644
--- a/libpthread/Makefile.am
+++ b/libpthread/Makefile.am
@@ -115,7 +115,7 @@ libpthread_a_SOURCES = pt-attr.c pt-attr-destroy.c 
pt-attr-getdetachstate.c \
        pt-thread-alloc.c                                                   \
        pt-thread-dealloc.c                                                 \
        pt-thread-start.c                                                   \
-       pt-thread-halt.c                                                    \
+       pt-thread-terminate.c                                               \
        pt-startup.c                                                        \
        pt-getconcurrency.c pt-setconcurrency.c                             \
        pt-block.c                                                          \
diff --git a/libpthread/pthread/pt-alloc.c b/libpthread/pthread/pt-alloc.c
index acd4672..b9ae3aa 100644
--- a/libpthread/pthread/pt-alloc.c
+++ b/libpthread/pthread/pt-alloc.c
@@ -47,7 +47,7 @@ struct __pthread *__pthread_free_threads;
 pthread_mutex_t __pthread_free_threads_lock;
 
 static inline error_t
-initialize_pthread (struct __pthread *new, int recycling)
+initialize_pthread (struct __pthread *new)
 {
   error_t err;
 
@@ -55,6 +55,7 @@ initialize_pthread (struct __pthread *new, int recycling)
   if (err)
     return err;
 
+  new->nr_refs = 1;
   new->cancel_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
   new->cancel_hook = NULL;
   new->cancel_hook_arg = NULL;
@@ -62,14 +63,6 @@ initialize_pthread (struct __pthread *new, int recycling)
   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_t) PTHREAD_MUTEX_INITIALIZER;
   new->state_cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
 
@@ -117,12 +110,6 @@ __pthread_alloc (struct __pthread **pthread)
 
   if (new)
     {
-      /* The thread may still be running.  Make sure it is stopped.
-        If this is the case, then the thread is either at the end of
-        __pthread_dealloc or in __pthread_thread_halt.  In both
-        cases, we are interrupt it.  */
-      __pthread_thread_halt (new);
-
 #ifdef ENABLE_TLS
       if (new->tcb)
        {
@@ -132,7 +119,7 @@ __pthread_alloc (struct __pthread **pthread)
        }
 #endif /* ENABLE_TLS */
 
-      err = initialize_pthread (new, 1);
+      err = initialize_pthread (new);
       if (! err)
        *pthread = new;
       return err;
@@ -143,7 +130,7 @@ __pthread_alloc (struct __pthread **pthread)
   if (new == NULL)
     return ENOMEM;
 
-  err = initialize_pthread (new, 0);
+  err = initialize_pthread (new);
   if (err)
     {
       free (new);
diff --git a/libpthread/pthread/pt-create.c b/libpthread/pthread/pt-create.c
index 9b6afa5..d3ed065 100644
--- a/libpthread/pthread/pt-create.c
+++ b/libpthread/pthread/pt-create.c
@@ -103,47 +103,30 @@ __pthread_create_internal (struct __pthread **thread,
   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 or changes the size,
-     we cannot use the old one.  Right now, we simply deallocate it.  */
-  if (pthread->stack)
     {
-      if ((setup->stackaddr && setup->stackaddr != pthread->stackaddr)
-       || (setup->stacksize != pthread->stacksize))
-       {
-         __pthread_stack_dealloc (pthread->stackaddr,
-                                  pthread->stacksize);
-         pthread->stackaddr = setup->stackaddr;
-         pthread->stacksize = setup->stacksize;
-       }
+      pthread->stackaddr = setup->stackaddr;
+
+      /* If the user supplied a stack, it is not our responsibility to
+        setup a stack guard.  */
+      pthread->guardsize = 0;
+      pthread->stack = 0;
     }
   else
     {
-      pthread->stacksize = setup->stacksize;
-
-      if (setup->stackaddr)
-       pthread->stackaddr = setup->stackaddr;
-      else
-       {
-         err = __pthread_stack_alloc (&pthread->stackaddr,
-                                      setup->stacksize);
-         if (err)
-           goto failed_stack_alloc;
-
-         pthread->stack = 1;
-       }
+      /* Allocate a stack.  */
+      err = __pthread_stack_alloc (&pthread->stackaddr,
+                                  setup->stacksize);
+      if (err)
+       goto failed_stack_alloc;
+
+      pthread->guardsize = (setup->guardsize <= setup->stacksize
+                          ? setup->guardsize : setup->stacksize);
+      pthread->stack = 1;
     }
 
+  pthread->stacksize = setup->stacksize;
+
   /* Allocate the kernel thread and other required resources.  */
   err = __pthread_thread_alloc (pthread);
   if (err)
@@ -169,6 +152,10 @@ __pthread_create_internal (struct __pthread **thread,
   if (err)
     goto failed_sigstate;
 
+  /* If the new thread is joinable, add a reference for the caller.  */
+  if (pthread->state == PTHREAD_JOINABLE)
+    pthread->nr_refs++;
+
   /* 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
@@ -216,6 +203,10 @@ __pthread_create_internal (struct __pthread **thread,
   return 0;
 
  failed_starting:
+  /* If joinable, a reference was added for the caller.  */
+  if (pthread->state == PTHREAD_JOINABLE)
+    __pthread_dealloc (pthread);
+
   __pthread_setid (pthread->thread, NULL);
   __atomic_dec (&__pthread_total);
  failed_sigstate:
@@ -227,10 +218,14 @@ __pthread_create_internal (struct __pthread **thread,
  failed_thread_tls_alloc:
 #endif /* ENABLE_TLS */
   __pthread_thread_dealloc (pthread);
-  __pthread_thread_halt (pthread);
+  __pthread_thread_terminate (pthread);
+
+  /* __pthread_thread_terminate has taken care of deallocating the stack and
+     the thread structure.  */
+  goto failed;
  failed_thread_alloc:
-  __pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize);
-  pthread->stack = 0;
+  if (pthread->stack)
+    __pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize);
  failed_stack_alloc:
   __pthread_dealloc (pthread);
  failed:
diff --git a/libpthread/pthread/pt-dealloc.c b/libpthread/pthread/pt-dealloc.c
index 92fe1fd..e324800 100644
--- a/libpthread/pthread/pt-dealloc.c
+++ b/libpthread/pthread/pt-dealloc.c
@@ -23,6 +23,8 @@
 
 #include <pt-internal.h>
 
+#include <bits/pt-atomic.h>
+
 /* List of thread structures corresponding to free thread IDs.  */
 extern struct __pthread *__pthread_free_threads;
 extern pthread_mutex_t __pthread_free_threads_lock;
@@ -34,6 +36,9 @@ __pthread_dealloc (struct __pthread *pthread)
 {
   assert (pthread->state != PTHREAD_TERMINATED);
 
+  if (! __atomic_dec_and_test (&pthread->nr_refs))
+    return;
+
   /* Withdraw this thread from the thread ID lookup table.  */
   __pthread_setid (pthread->thread, NULL);
 
diff --git a/libpthread/pthread/pt-detach.c b/libpthread/pthread/pt-detach.c
index 4ed8d2c..3431f1b 100644
--- a/libpthread/pthread/pt-detach.c
+++ b/libpthread/pthread/pt-detach.c
@@ -50,30 +50,16 @@ pthread_detach (pthread_t thread)
         consequences instead of blocking indefinitely.  */
       pthread_cond_broadcast (&pthread->state_cond);
       __pthread_mutex_unlock (&pthread->state_lock);
+
+      __pthread_dealloc (pthread);
       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);
 
-      /* Make sure the thread is not running before we remove its
-         stack.  (The only possibility is that it is in a call to
-         __pthread_thread_halt itself, but that is enough to cause a
-         sigsegv.)  */
-      __pthread_thread_halt (pthread);
-
-      /* Destroy the stack, the kernel resources and the control
-        block.  */
-      if (pthread->stack)
-       {
-         __pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize);
-         pthread->stack = 0;
-       }
-
-      __pthread_thread_dealloc (pthread);
-
+      /* THREAD has already exited.  PTHREAD remained after the thread
+        exited in order to provide the exit status, but it turns out
+        it won't be needed.  */
       __pthread_dealloc (pthread);
       break;
 
diff --git a/libpthread/pthread/pt-exit.c b/libpthread/pthread/pt-exit.c
index f2d5eef..ea61732 100644
--- a/libpthread/pthread/pt-exit.c
+++ b/libpthread/pthread/pt-exit.c
@@ -48,12 +48,6 @@ __pthread_exit (void *status)
 
   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))
@@ -77,15 +71,8 @@ __pthread_exit (void *status)
       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 and TLS.  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:
@@ -105,12 +92,22 @@ __pthread_exit (void *status)
       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.  */
+  /* Destroy any thread specific data.  */
+  __pthread_destroy_specific (self);
+
+  /* Destroy any signal state.  */
+  __pthread_sigstate_destroy (self);
+
+  /* Kernel resources may be used to implement synchronization objects,
+     release them late.  */
+  __pthread_thread_dealloc (self);
+
+  /* Self terminating requires TLS, so defer the release of the TCB until
+     the thread structure is reused.  */
 
-  __pthread_thread_halt (self);
+  /* Terminate the kernel thread, release the stack and drop the
+     self reference.  */
+  __pthread_thread_terminate (self);
 
   /* NOTREACHED */
   abort ();
diff --git a/libpthread/pthread/pt-internal.h b/libpthread/pthread/pt-internal.h
index 8c7ab07..9cc330c 100644
--- a/libpthread/pthread/pt-internal.h
+++ b/libpthread/pthread/pt-internal.h
@@ -72,6 +72,12 @@ struct __pthread
   /* Thread ID.  */
   pthread_t thread;
 
+  __atomic_t nr_refs;   /* Detached threads have a self reference only,
+                          while joinable threads have two references.
+                          These are used to keep the structure valid at
+                          thread destruction.  Detaching/joining a thread
+                          drops a reference.  */
+
   /* Cancellation.  */
   pthread_mutex_t cancel_lock;  /* Protect cancel_xxx members.  */
   void (*cancel_hook)(void *); /* Called to unblock a thread blocking
@@ -206,12 +212,13 @@ extern int __pthread_create_internal (struct __pthread 
**__restrict pthread,
                                      void *__restrict arg);
 
 /* Allocate a new thread structure and a pthread thread ID (but not a
-   kernel thread or a stack).  */
+   kernel thread or a stack).  THREAD has one reference.  */
 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).  */
+   __pthread_alloc (N.B. it does not call __pthread_stack_dealloc nor
+   __pthread_thread_terminate).  THREAD looses one reference and is
+   released if the reference counter drops to 0.  */
 extern void __pthread_dealloc (struct __pthread *thread);
 
 
@@ -236,22 +243,23 @@ extern int __pthread_setup (struct __pthread *__restrict 
thread,
    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.  The thread
-   must not be running (that is, if __pthread_thread_start was called,
-   __pthread_thread_halt must first be called).  This function will
-   never be called by a thread on itself.  In the case that a thread
-   exits, its thread structure will be cached and cleaned up
-   later.  */
+/* Deallocate any kernel resources associated with THREAD.  */
 extern void __pthread_thread_dealloc (struct __pthread *thread);
 
 /* Start THREAD making it eligible to run.  */
 extern int __pthread_thread_start (struct __pthread *thread);
 
-/* Stop the kernel thread associated with THREAD.  This function may
-   be called by two threads in parallel.  In particular, by the thread
-   itself and another thread trying to join it.  This function must be
-   implemented such that this is safe.  */
-extern void __pthread_thread_halt (struct __pthread *thread);
+/* Terminate the kernel thread associated with THREAD, and deallocate its
+   stack.  In addition, THREAD looses one reference.
+
+   This function can be called by any thread, including the target thread.
+   Since some resources that are destroyed along the kernel thread are
+   stored in thread-local variables, the conditions required for this
+   function to behave correctly are a bit unusual : as long as the target
+   thread hasn't been started, any thread can terminate it, but once it
+   has started, no other thread can terminate it, so that thread-local
+   variables created by that thread are correctly released.  */
+extern void __pthread_thread_terminate (struct __pthread *thread);
 
 
 /* Called by a thread just before it calls the provided start
diff --git a/libpthread/pthread/pt-join.c b/libpthread/pthread/pt-join.c
index 417f433..122d130 100644
--- a/libpthread/pthread/pt-join.c
+++ b/libpthread/pthread/pt-join.c
@@ -50,27 +50,11 @@ pthread_join (pthread_t thread, void **status)
   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 the thread is not running before we remove its
-         stack.  (The only possibility is that it is in a call to
-         __pthread_thread_halt itself, but that is enough to cause a
-         sigsegv.)  */
-      __pthread_thread_halt (pthread);
-
-      /* Destroy the stack, the kernel resources and the control
-        block.  */
-      if (pthread->stack)
-       {
-         __pthread_stack_dealloc (pthread->stackaddr, pthread->stacksize);
-         pthread->stack = 0;
-       }
-
-      __pthread_thread_dealloc (pthread);
+      __pthread_mutex_unlock (&pthread->state_lock);
 
       __pthread_dealloc (pthread);
       break;
diff --git a/libpthread/sysdeps/mach/hurd/pt-init-specific.c 
b/libpthread/sysdeps/mach/hurd/pt-init-specific.c
index ce866b0..871ab07 100644
--- a/libpthread/sysdeps/mach/hurd/pt-init-specific.c
+++ b/libpthread/sysdeps/mach/hurd/pt-init-specific.c
@@ -24,6 +24,5 @@ error_t
 __pthread_init_specific (struct __pthread *thread)
 {
   thread->thread_specifics = 0;
-  thread->have_kernel_resources = 0;
   return 0;
 }
diff --git a/libpthread/sysdeps/mach/hurd/pt-sigstate-destroy.c 
b/libpthread/sysdeps/mach/hurd/pt-sigstate-destroy.c
index 8e56c5c..d5e28d2 100644
--- a/libpthread/sysdeps/mach/hurd/pt-sigstate-destroy.c
+++ b/libpthread/sysdeps/mach/hurd/pt-sigstate-destroy.c
@@ -24,5 +24,5 @@
 void
 __pthread_sigstate_destroy (struct __pthread *thread)
 {
-  /* Nothing to do.  */
+  _hurd_sigstate_delete (thread->kernel_thread);
 }
diff --git a/libpthread/sysdeps/mach/hurd/pt-sigstate-init.c 
b/libpthread/sysdeps/mach/hurd/pt-sigstate-init.c
index 7c44708..6e6c525 100644
--- a/libpthread/sysdeps/mach/hurd/pt-sigstate-init.c
+++ b/libpthread/sysdeps/mach/hurd/pt-sigstate-init.c
@@ -24,6 +24,8 @@
 error_t
 __pthread_sigstate_init (struct __pthread *thread)
 {
+  static int do_init_global;
+
   /* Mark the thread as a global signal receiver so as to conform with
      the pthread semantics.  However, we must be careful.  The first
      pthread created is the main thread, during libpthread initialization.
@@ -31,11 +33,13 @@ __pthread_sigstate_init (struct __pthread *thread)
      __pthread_create would try to access _hurd_global_sigstate,
      which is not initialized yet.  When glibc runs _hurdsig_init later
      on, the message thread is created, which must not be marked either.  */
-  if (__pthread_num_threads > 2)
+  if (do_init_global)
     {
       struct hurd_sigstate *ss = _hurd_thread_sigstate (thread->kernel_thread);
       _hurd_sigstate_set_global_rcv (ss);
     }
+  else if (__pthread_num_threads >= 2)
+    do_init_global = 1;
 
   return 0;
 }
diff --git a/libpthread/sysdeps/mach/hurd/pt-sysdep.c 
b/libpthread/sysdeps/mach/hurd/pt-sysdep.c
index f979779..8510676 100644
--- a/libpthread/sysdeps/mach/hurd/pt-sysdep.c
+++ b/libpthread/sysdeps/mach/hurd/pt-sysdep.c
@@ -65,6 +65,11 @@ init_routine (void)
   err = __pthread_create_internal (&thread, 0, 0, 0);
   assert_perror (err);
 
+  /* XXX The caller copies the command line arguments and the environment
+     to the new stack.  Pretend it wasn't allocated so that it remains
+     valid if the main thread terminates.  */
+  thread->stack = 0;
+
   ___pthread_self = thread;
 
   /* Decrease the number of threads, to take into account that the
diff --git a/libpthread/sysdeps/mach/hurd/pt-sysdep.h 
b/libpthread/sysdeps/mach/hurd/pt-sysdep.h
index 89592f9..35912a3 100644
--- a/libpthread/sysdeps/mach/hurd/pt-sysdep.h
+++ b/libpthread/sysdeps/mach/hurd/pt-sysdep.h
@@ -30,8 +30,7 @@
 
 #define PTHREAD_SYSDEP_MEMBERS \
   thread_t kernel_thread;      \
-  mach_msg_header_t wakeupmsg; \
-  int have_kernel_resources;
+  mach_msg_header_t wakeupmsg;
 
 extern __thread struct __pthread *___pthread_self;
 #define _pthread_self()                                            \
diff --git a/libpthread/sysdeps/mach/pt-thread-alloc.c 
b/libpthread/sysdeps/mach/pt-thread-alloc.c
index 794f63e..77aa933 100644
--- a/libpthread/sysdeps/mach/pt-thread-alloc.c
+++ b/libpthread/sysdeps/mach/pt-thread-alloc.c
@@ -67,44 +67,29 @@ create_wakeupmsg (struct __pthread *thread)
 int
 __pthread_thread_alloc (struct __pthread *thread)
 {
-  if (thread->have_kernel_resources)
-    return 0;
-
+  static int do_create;
   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)
+  if (! do_create)
     {
       assert (__pthread_total == 0);
       thread->kernel_thread = __mach_thread_self ();
-      /* We implicitly hold a reference drop the one that we just
-        acquired.  */
-      __mach_port_deallocate (__mach_task_self (), thread->kernel_thread);
+      do_create = 1;
     }
   else
     {
       err = __thread_create (__mach_task_self (), &thread->kernel_thread);
       if (err)
-       return EAGAIN;
+       {
+         __mach_port_destroy (__mach_task_self (),
+                              thread->wakeupmsg.msgh_remote_port);
+         return EAGAIN;
+       }
     }
 
-  thread->have_kernel_resources = 1;
-
   return 0;
 }
diff --git a/libpthread/sysdeps/mach/pt-thread-dealloc.c 
b/libpthread/sysdeps/mach/pt-thread-dealloc.c
index 0c4a4fc..e977c3f 100644
--- a/libpthread/sysdeps/mach/pt-thread-dealloc.c
+++ b/libpthread/sysdeps/mach/pt-thread-dealloc.c
@@ -24,8 +24,7 @@
 #include <pt-internal.h>
 
 /* 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.  */
+   terminate the thread itself.  */
 void
 __pthread_thread_dealloc (struct __pthread *thread)
 {
@@ -38,6 +37,4 @@ __pthread_thread_dealloc (struct __pthread *thread)
      assert.  */
   __mach_port_destroy (__mach_task_self (),
                       thread->wakeupmsg.msgh_remote_port);
-
-  thread->have_kernel_resources = 0;
 }
diff --git a/libpthread/sysdeps/mach/pt-thread-halt.c 
b/libpthread/sysdeps/mach/pt-thread-halt.c
deleted file mode 100644
index 808043d..0000000
--- a/libpthread/sysdeps/mach/pt-thread-halt.c
+++ /dev/null
@@ -1,54 +0,0 @@
-/* Deallocate the kernel thread resources.  Mach version.
-   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
-   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>
-
-/* 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.  */
-void
-__pthread_thread_halt (struct __pthread *thread)
-{
-  if (thread->have_kernel_resources)
-    {
-      _hurd_sigstate_delete (thread->kernel_thread);
-
-      if (thread == _pthread_self ())
-       {
-         while (1)
-           {
-             error_t err = __thread_suspend (thread->kernel_thread);
-             assert_perror (err);
-             assert (! "Failed to suspend self.");
-           }
-       }
-      else
-       {
-         error_t err = __thread_terminate (thread->kernel_thread);
-         assert_perror (err);
-       }
-    }
-}
diff --git a/libpthread/sysdeps/mach/pt-thread-start.c 
b/libpthread/sysdeps/mach/pt-thread-start.c
index 11b017f..df490ab 100644
--- a/libpthread/sysdeps/mach/pt-thread-start.c
+++ b/libpthread/sysdeps/mach/pt-thread-start.c
@@ -27,9 +27,10 @@
 int
 __pthread_thread_start (struct __pthread *thread)
 {
+  static int do_start;
   error_t err;
 
-  if (__pthread_num_threads == 1)
+  if (! do_start)
     {
       /* The main thread is already running: do nothing.  */
       assert (__pthread_total == 1);
@@ -38,6 +39,7 @@ __pthread_thread_start (struct __pthread *thread)
                  __mach_port_deallocate (__mach_task_self (),
                                         thread->kernel_thread);
                 ok; }));
+      do_start = 1;
     }
   else
     {
diff --git a/libpthread/sysdeps/mach/pt-thread-terminate.c 
b/libpthread/sysdeps/mach/pt-thread-terminate.c
new file mode 100644
index 0000000..a89e505
--- /dev/null
+++ b/libpthread/sysdeps/mach/pt-thread-terminate.c
@@ -0,0 +1,72 @@
+/* Deallocate the kernel thread resources.  Mach version.
+   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
+   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/mig_support.h>
+
+#include <pt-internal.h>
+
+/* Terminate the kernel thread associated with THREAD, and deallocate its
+   right reference and its stack.  The function also drops a reference
+   on THREAD.  */
+void
+__pthread_thread_terminate (struct __pthread *thread)
+{
+  thread_t kernel_thread, self_ktid;
+  mach_port_t reply_port;
+  void *stackaddr;
+  size_t stacksize;
+  error_t err;
+
+  kernel_thread = thread->kernel_thread;
+
+  if (thread->stack)
+    {
+      stackaddr = thread->stackaddr;
+      stacksize = thread->stacksize;
+    }
+  else
+    {
+      stackaddr = NULL;
+      stacksize = 0;
+    }
+
+  /* Each thread has its own reply port, allocated from MiG stub code calling
+     __mig_get_reply_port.  Destroying it is a bit tricky because the calls
+     involved are also RPCs, causing the creation of a new reply port if
+     currently null. The __thread_terminate_release call is actually a one way
+     simple routine designed not to require a reply port.  */
+  self_ktid = __mach_thread_self ();
+  reply_port = (self_ktid == kernel_thread)
+              ? __mig_get_reply_port ()
+              : MACH_PORT_NULL;
+  __mach_port_deallocate (__mach_task_self (), self_ktid);
+
+  /* Finally done with the thread structure.  */
+  __pthread_dealloc (thread);
+
+  /* Terminate and release all that's left.  */
+  err = __thread_terminate_release (kernel_thread, mach_task_self (),
+                                   kernel_thread, reply_port,
+                                   stackaddr, stacksize);
+  assert_perror (err);
+}
diff --git a/sysdeps/mach/hurd/i386/tls.h b/sysdeps/mach/hurd/i386/tls.h
index 77baa90..d1fdaa6 100644
--- a/sysdeps/mach/hurd/i386/tls.h
+++ b/sysdeps/mach/hurd/i386/tls.h
@@ -69,6 +69,7 @@ static inline const char * __attribute__ ((unused))
 _hurd_tls_init (tcbhead_t *tcb, int secondcall)
 {
   HURD_TLS_DESC_DECL (desc, tcb);
+  thread_t self = __mach_thread_self ();
 
   if (!secondcall)
     {
@@ -76,17 +77,14 @@ _hurd_tls_init (tcbhead_t *tcb, int secondcall)
         from the TLS point of view.  */
       tcb->tcb = tcb;
 
-      /* Cache our thread port.  */
-      tcb->self = __mach_thread_self ();
-
       /* Get the first available selector.  */
       int sel = -1;
-      kern_return_t err = __i386_set_gdt (tcb->self, &sel, desc);
+      kern_return_t err = __i386_set_gdt (self, &sel, desc);
       if (err == MIG_BAD_ID)
        {
          /* Old kernel, use a per-thread LDT.  */
          sel = 0x27;
-         err = __i386_set_ldt (tcb->self, sel, &desc, 1);
+         err = __i386_set_ldt (self, sel, &desc, 1);
          assert_perror (err);
          if (err)
            return "i386_set_ldt failed";
@@ -107,19 +105,20 @@ _hurd_tls_init (tcbhead_t *tcb, int secondcall)
       asm ("mov %%gs, %w0" : "=q" (sel) : "0" (0));
       if (__builtin_expect (sel, 0x48) & 4) /* LDT selector */
        {
-         kern_return_t err = __i386_set_ldt (tcb->self, sel, &desc, 1);
+         kern_return_t err = __i386_set_ldt (self, sel, &desc, 1);
          assert_perror (err);
          if (err)
            return "i386_set_ldt failed";
        }
       else
        {
-         kern_return_t err = __i386_set_gdt (tcb->self, &sel, desc);
+         kern_return_t err = __i386_set_gdt (self, &sel, desc);
          assert_perror (err);
          if (err)
            return "i386_set_gdt failed";
        }
     }
+  __mach_port_deallocate (__mach_task_self (), self);
 
   return 0;
 }
-- 
1.8.4.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]