summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--nptl/ChangeLog14
-rw-r--r--nptl/Makefile2
-rw-r--r--nptl/allocatestack.c20
-rw-r--r--nptl/init.c3
-rw-r--r--nptl/pthreadP.h3
-rw-r--r--nptl/pthread_create.c14
-rw-r--r--nptl/sysdeps/pthread/Makefile4
-rw-r--r--nptl/sysdeps/pthread/pthread-functions.h2
-rw-r--r--nptl/tst-basic3.c87
-rw-r--r--sysdeps/generic/libc-start.c27
11 files changed, 157 insertions, 24 deletions
diff --git a/ChangeLog b/ChangeLog
index e2aab89815..78ca01e101 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2003-02-14  Ulrich Drepper  <drepper@redhat.com>
+
+	* sysdeps/generic/libc-start.c [HAVE_PTR_NTHREADS]: Decrement thread
+	counter and only call __exit_thread if this is not the last thread.
+
 2003-02-13  Jakub Jelinek  <jakub@redhat.com>
 
 	* sysdeps/alpha/dl-machine.h (elf_machine_rela): Add instead of
diff --git a/nptl/ChangeLog b/nptl/ChangeLog
index 3b02b45b88..dcf34211b9 100644
--- a/nptl/ChangeLog
+++ b/nptl/ChangeLog
@@ -1,5 +1,19 @@
 2003-02-14  Ulrich Drepper  <drepper@redhat.com>
 
+	* pthreadP.h: Add declaraction for __nptl_nthreads.
+	* pthread_create.c: Define __nptl_nthreads
+	(start_thread): Increment __nptl_nthreads at beginning.  Decrement
+	after thread is done.  If Then zero call exit(0).
+	* sysdeps/pthread/pthread-functions.h (struct pthread_functions):
+	Add ptr_nthreads.  Define HAVE_PTR_NTHREADS.
+	* init.c (pthread_functions): Initialize ptr_nthreads.
+	* allocatestack.c (nptl_nthreads): Remove definition and all uses.
+	(__reclaim_stacks): Decrement __nptl_nthreads.
+	* sysdeps/pthread/Makefile [$(subdir)==csu] (CFLAGS-libc-start.c):
+	Define.
+	* Makefile (tests): Add tst-basic3.
+	* tst-basic3.c: New file.
+
 	* descr.h: Define CANCELING_BIT and CANCELING_BITMASK.  Introduce
 	after CANCELTYPE_BIT, move the other bits up.  Update CANCEL_RESTMASK.
 	* init.c (sigcancel_handler): Also set CANCELING_BITMASK bit in newval.
diff --git a/nptl/Makefile b/nptl/Makefile
index 65c90c2182..198d7b895c 100644
--- a/nptl/Makefile
+++ b/nptl/Makefile
@@ -136,7 +136,7 @@ tests = tst-attr1 tst-attr2 \
 	tst-key1 tst-key2 tst-key3 \
 	tst-sem1 tst-sem2 tst-sem3 tst-sem4 tst-sem5 \
 	tst-barrier1 tst-barrier2 tst-barrier3 \
-	tst-basic1 tst-basic2 \
+	tst-basic1 tst-basic2 tst-basic3 \
 	tst-join1 tst-join2 tst-join3 tst-join4 tst-join5 \
 	tst-tsd1 tst-tsd2 \
 	tst-fork1 tst-fork2 tst-fork3 \
diff --git a/nptl/allocatestack.c b/nptl/allocatestack.c
index 1a4bf44367..9d7f7618ec 100644
--- a/nptl/allocatestack.c
+++ b/nptl/allocatestack.c
@@ -74,9 +74,6 @@ static LIST_HEAD (stack_used);
 list_t __stack_user __attribute__ ((nocommon));
 hidden_def (__stack_user)
 
-/* Number of threads running.  */
-static unsigned int nptl_nthreads = 1;
-
 /* Number of threads created.  */
 static unsigned int nptl_ncreated;
 
@@ -142,9 +139,6 @@ get_cached_stack (size_t *sizep, void **memp)
   /* And add to the list of stacks in use.  */
   list_add (&result->header.data.list, &stack_used);
 
-  /* One more thread.  */
-  ++nptl_nthreads;
-
   /* And decrease the cache size.  */
   stack_cache_actsize -= result->stackblock_size;
 
@@ -299,9 +293,6 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
       /* And add to the list of stacks in use.  */
       list_add (&pd->header.data.list, &__stack_user);
 
-      /* One more thread.  */
-      ++nptl_nthreads;
-
       lll_unlock (stack_cache_lock);
     }
   else
@@ -416,9 +407,6 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
 	  /* And add to the list of stacks in use.  */
 	  list_add (&pd->header.data.list, &stack_used);
 
-	  /* One more thread.  */
-	  ++nptl_nthreads;
-
 	  lll_unlock (stack_cache_lock);
 
 
@@ -444,9 +432,6 @@ allocate_stack (const struct pthread_attr *attr, struct pthread **pdp,
 	      /* Remove the thread from the list.  */
 	      list_del (&pd->header.data.list);
 
-	      /* The thread is gone.  */
-	      --nptl_nthreads;
-
 	      lll_unlock (stack_cache_lock);
 
 	      /* Free the memory regardless of whether the size of the
@@ -510,9 +495,6 @@ __deallocate_stack (struct pthread *pd)
     /* Free the memory associated with the ELF TLS.  */
     _dl_deallocate_tls (pd, false);
 
-  /* One less thread.  */
-  --nptl_nthreads;
-
   lll_unlock (stack_cache_lock);
 }
 
@@ -563,7 +545,7 @@ __reclaim_stacks (void)
     list_add (&self->header.data.list, &stack_used);
 
   /* There is one thread running.  */
-  nptl_nthreads = 1;
+  __nptl_nthreads = 1;
 
   /* Initialize the lock.  */
   stack_cache_lock = LLL_LOCK_INITIALIZER;
diff --git a/nptl/init.c b/nptl/init.c
index 4d30084efb..ddc6f3d939 100644
--- a/nptl/init.c
+++ b/nptl/init.c
@@ -110,7 +110,8 @@ static struct pthread_functions pthread_functions =
     .ptr___pthread_getspecific = __pthread_getspecific_internal,
     .ptr___pthread_setspecific = __pthread_setspecific_internal,
     .ptr__pthread_cleanup_push_defer = __pthread_cleanup_push_defer,
-    .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore
+    .ptr__pthread_cleanup_pop_restore = __pthread_cleanup_pop_restore,
+    .ptr_nthreads = &__nptl_nthreads
   };
 # define ptr_pthread_functions &pthread_functions
 #else
diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h
index 3223291dab..784f62416f 100644
--- a/nptl/pthreadP.h
+++ b/nptl/pthreadP.h
@@ -61,6 +61,9 @@ extern int __concurrency_level attribute_hidden;
 extern struct pthread_key_struct __pthread_keys[PTHREAD_KEYS_MAX];
 hidden_proto (__pthread_keys)
 
+/* Number of threads running.  */
+extern unsigned int __nptl_nthreads attribute_hidden;
+
 /* The library can run in debugging mode where it performs a lot more
    tests.  */
 extern int __pthread_debug attribute_hidden;
diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c
index 2b33243cee..c96a2813e5 100644
--- a/nptl/pthread_create.c
+++ b/nptl/pthread_create.c
@@ -44,6 +44,9 @@ static td_thr_events_t __nptl_threads_events;
 /* Pointer to descriptor with the last event.  */
 static struct pthread *__nptl_last_event;
 
+/* Number of threads running.  */
+unsigned int __nptl_nthreads = 1;
+
 
 /* Code to allocate and deallocate a stack.  */
 #define DEFINE_DEALLOC
@@ -197,6 +200,9 @@ __free_tcb (struct pthread *pd)
 static int
 start_thread (void *arg)
 {
+  /* One more thread.  */
+  atomic_increment (&__nptl_nthreads);
+
   struct pthread *pd = (struct pthread *) arg;
 
 #if HP_TIMING_AVAIL
@@ -215,6 +221,14 @@ start_thread (void *arg)
     }
 
 
+  /* If this is the last thread we terminate the process now.  We
+     do not notify the debugger, it might just irritate it if there
+     is no thread left.  */
+  if (atomic_decrement_and_test (&__nptl_nthreads))
+    /* This was the last thread.  */
+    exit (0);
+
+
   /* Report the death of the thread if this is wanted.  */
   if (__builtin_expect (pd->report_events, 0))
     {
diff --git a/nptl/sysdeps/pthread/Makefile b/nptl/sysdeps/pthread/Makefile
index 17f18cb928..1d248bf720 100644
--- a/nptl/sysdeps/pthread/Makefile
+++ b/nptl/sysdeps/pthread/Makefile
@@ -17,6 +17,10 @@
 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 # 02111-1307 USA.
 
+ifeq ($(subdir),csu)
+CFLAGS-libc-start.c += -I../nptl
+endif
+
 ifeq ($(subdir),nptl)
 libpthread-sysdep_routines += errno-loc
 endif
diff --git a/nptl/sysdeps/pthread/pthread-functions.h b/nptl/sysdeps/pthread/pthread-functions.h
index 45c7f900c6..f6922a0d6c 100644
--- a/nptl/sysdeps/pthread/pthread-functions.h
+++ b/nptl/sysdeps/pthread/pthread-functions.h
@@ -83,6 +83,8 @@ struct pthread_functions
 					   void (*) (void *), void *);
   void (*ptr__pthread_cleanup_pop_restore) (struct _pthread_cleanup_buffer *,
 					    int);
+#define HAVE_PTR_NTHREADS
+  int *ptr_nthreads;
 };
 
 /* Variable in libc.so.  */
diff --git a/nptl/tst-basic3.c b/nptl/tst-basic3.c
new file mode 100644
index 0000000000..cb4816d2aa
--- /dev/null
+++ b/nptl/tst-basic3.c
@@ -0,0 +1,87 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+   Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
+
+   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.  */
+
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+static int nrunning = 1;
+
+
+static void
+final_test (void)
+{
+  puts ("final_test has been called");
+
+#define THE_SIGNAL SIGUSR1
+  kill (getpid (), SIGUSR1);
+}
+
+
+static void *
+tf (void *a)
+{
+  if (pthread_join ((pthread_t) a, NULL) != 0)
+    {
+      printf ("join failed while %d are running\n", nrunning);
+      _exit (1);
+    }
+
+  printf ("%2d left\n", --nrunning);
+
+  return NULL;
+}
+
+
+int
+do_test (void)
+{
+#define N 20
+  pthread_t t[N];
+  pthread_t last = pthread_self ();
+  int i;
+
+  atexit (final_test);
+
+  printf ("starting %d + 1 threads\n", N);
+  for (i = 0; i < N; ++i)
+    {
+      if (pthread_create (&t[i], NULL, tf, (void *) last) != 0)
+	{
+	  puts ("create failed");
+	  _exit (1);
+	}
+
+      ++nrunning;
+
+      last = t[i];
+    }
+
+  printf ("%2d left\n", --nrunning);
+
+  pthread_exit (NULL);
+}
+
+
+#define EXPECTED_SIGNAL THE_SIGNAL
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/sysdeps/generic/libc-start.c b/sysdeps/generic/libc-start.c
index b4c29dd3c8..a6dbebe123 100644
--- a/sysdeps/generic/libc-start.c
+++ b/sysdeps/generic/libc-start.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+/* Copyright (C) 1998,1999,2000,2001,2002,2003 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
@@ -38,6 +38,11 @@ extern void __pthread_initialize_minimal (void)
      ;
 #endif
 
+#ifdef HAVE_PTR_NTHREADS
+/* We need atomic operations.  */
+# include <atomic.h>
+#endif
+
 
 extern int BP_SYM (__libc_start_main) (int (*main) (int, char **, char **),
 				       int argc,
@@ -148,8 +153,24 @@ BP_SYM (__libc_start_main) (int (*main) (int, char **, char **),
     }
 #ifdef HAVE_CANCELBUF
   else
-    /* Not much left to do but to exit the thread, not the process.  */
-    __exit_thread (0);
+    {
+# ifdef HAVE_PTR_NTHREADS
+      /* One less thread.  Decrement the counter.  If it is zero we
+	 terminate the entire process.  */
+      result = 0;
+      int *const ptr;
+#  ifdef SHARED
+      ptr = __libc_pthread_functions.ptr_nthreads;
+#  else
+      extern int __nptl_nthreads __attribute ((weak));
+      ptr = &__nptl_nthreads;
+#  endif
+
+      if (! atomic_decrement_and_test (ptr))
+# endif
+	/* Not much left to do but to exit the thread, not the process.  */
+	__exit_thread (0);
+    }
 #endif
 
   exit (result);