]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Update.
authorUlrich Drepper <drepper@redhat.com>
Fri, 14 Feb 2003 23:31:11 +0000 (23:31 +0000)
committerUlrich Drepper <drepper@redhat.com>
Fri, 14 Feb 2003 23:31:11 +0000 (23:31 +0000)
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.

ChangeLog
nptl/ChangeLog
nptl/Makefile
nptl/allocatestack.c
nptl/init.c
nptl/pthreadP.h
nptl/pthread_create.c
nptl/sysdeps/pthread/Makefile
nptl/sysdeps/pthread/pthread-functions.h
nptl/tst-basic3.c [new file with mode: 0644]
sysdeps/generic/libc-start.c

index e2aab898155f7dfed7839af3e11a8c64d45340f1..78ca01e10146c265ff1387a9ad429ca446254150 100644 (file)
--- 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
index 3b02b45b88720929b06c29be5d176d7644daf393..dcf34211b9cdab42b268c03e0955a66ff49dbf3c 100644 (file)
@@ -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.
index 65c90c2182db913b76705dbf06285a96bdf322b1..198d7b895cce13080cad96d74ae65ec04c5a3ee4 100644 (file)
@@ -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 \
index 1a4bf44367ef88ec3acec0f94d2df1750a9e5aa2..9d7f7618ec299c14f88661fc4519213df577402e 100644 (file)
@@ -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;
index 4d30084efb2d8eee0a2f1c769a09fafeabbd6b7d..ddc6f3d93941f7799a6b60165274f172c41ca224 100644 (file)
@@ -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
index 3223291dab913586eb78411aaff7b2ed9be9cb54..784f62416ff71a39b4948364f33b786dd0517539 100644 (file)
@@ -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;
index 2b33243ceeacd119671f3cd2d194746aa39a7b4d..c96a2813e5baf4acbdc39050ce54205fd1716b81 100644 (file)
@@ -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))
     {
index 17f18cb9285331b3e25d80ae1be67ec5fce7a168..1d248bf7207a9bfd71cca2ab22bd5ff27f305dcf 100644 (file)
 # 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
index 45c7f900c6020bfec16e6a30c9a31ea73f89bbf3..f6922a0d6c39f233109fbef048ee8758123f1544 100644 (file)
@@ -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 (file)
index 0000000..cb4816d
--- /dev/null
@@ -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"
index b4c29dd3c8cf7e6051435a902057f651546bba8f..a6dbebe123ab47205d1ca803d8c2aada2b4abafa 100644 (file)
@@ -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);