This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: pthread_exit_process


On Thu, Jan 11, 2001 at 11:25:19AM -0800, Ulrich Drepper wrote:
> Jakub Jelinek <jakub@redhat.com> writes:
> 
> > This change breaks process-wide exit from thread other than main.
> 
> Thanks for the test case, I've added it.  To fix it I've done
> something different.

I was playing with this idea today as well, but it cannot work that way.
Actually, what is the functional difference between cxa_on_exit and on_exit?
It records dso handle, but nothing ever uses it, so if you dl_close
libpthread, nothing will happen and it will crash during exit() afterwards
(because it won't be removed during cxa_finalize).
And if you add ef_cxa2 to cxa_finalize, then you suddenly don't know which
value to pass to it (and even ef_cxa2 handler would not be ever called from
exit() directly, since it would be called from _*fini through cxa_finalize
first.
I believe the patch below could work, although I have not tested it yet.
Basically, it removes ef_cxa2 so that cxa_finalize does not have to do 2
compare_and_swaps and passes the status unconditionally to all ef_cxa
callbacks as hidden second argument (if you don't like this part of the
patch, it should work only with the pthread.c change).
The other change is to call __cxa*exit in pthread.c twice (unless libpthread
is built with -z nodelete in which case on_exit is enough), once from
pthread_initialize (ie. early) and once from __pthread_initialize_manager
(ie. late). The late callback which will be called earlier (even before
atexit _fini and _dl_fini) will just record the retcode value which the
earlier callback (called probably from __cxa_finalize when it has no idea on
what status is) will use for its dirty work.

2001-01-11  Jakub Jelinek  <jakub@redhat.com>

	* stdlib/cxa_atexit.c (__cxa_atexit): Cast to (void *, int) func.
	* stdlib/cxa_finalize.c (__cxa_finalize): Add hidden second argument.
	* stdlib/cxa_on_exit.c: Remove.
	* stdlib/Makefile, stdlib/Versions, include/stdlib.h: Revert last
	patch.
        * stdlib/exit.h: Revert last patch.
        (struct exit_function): Add second argument to cxa fn.
        * stdlib/exit.c: Revert last patch.
	(exit): Add hidden second argument.

	* Makefile (CFLAGS-pthread.c): Pass -DHAVE_Z_NODELETE if ld supports
	-z nodelete.
	* pthread.c (pthread_exit_process) Rename to...
	(pthread_onexit_process): ...this.
	(pthread_atexit_process, pthread_atexit_retcode): New.
	(pthread_initialize): Call __cxa_atexit instead of __cxa_on_exit
	and only if HAVE_Z_NODELETE is not defined.
	(__pthread_initialize_manager): Register pthread_atexit_retcode
	with __cxa_atexit.

--- libc/include/stdlib.h.jj	Fri Jan 12 00:08:29 2001
+++ libc/include/stdlib.h	Fri Jan 12 00:09:29 2001
@@ -55,7 +55,6 @@ extern void _quicksort (void *const pbas
 			size_t size, __compar_fn_t cmp);
 
 extern int __cxa_atexit (void (*func) (void *), void *arg, void *d);
-extern int __cxa_on_exit (void (*func) (int, void *), void *arg, void *d);
 
 extern void __cxa_finalize (void *d);
 
--- libc/linuxthreads/Makefile.jj	Thu Jan 11 23:31:06 2001
+++ libc/linuxthreads/Makefile	Thu Jan 11 23:31:14 2001
@@ -54,9 +54,10 @@ endif
 
 include ../Rules
 
+znodelete-yes = -DHAVE_Z_NODELETE
 CFLAGS-mutex.c += -D__NO_WEAK_PTHREAD_ALIASES
 CFLAGS-specific.c += -D__NO_WEAK_PTHREAD_ALIASES
-CFLAGS-pthread.c += -D__NO_WEAK_PTHREAD_ALIASES
+CFLAGS-pthread.c += -D__NO_WEAK_PTHREAD_ALIASES $(znodelete-$(have-z-nodelete))
 CFLAGS-ptfork.c += -D__NO_WEAK_PTHREAD_ALIASES
 CFLAGS-cancel.c += -D__NO_WEAK_PTHREAD_ALIASES
 CFLAGS-unload.c += -DPREFIX=\"$(objpfx)\"
--- libc/linuxthreads/pthread.c.jj	Thu Jan 11 23:09:29 2001
+++ libc/linuxthreads/pthread.c	Thu Jan 11 23:59:29 2001
@@ -216,7 +216,11 @@ const int __linuxthread_pthread_sizeof_d
 
 /* Forward declarations */
 
-static void pthread_exit_process(int retcode, void *arg);
+static void pthread_onexit_process(int retcode, void *arg);
+#ifndef HAVE_Z_NODELETE
+static void pthread_atexit_process(void *arg, int retcode);
+static void pthread_atexit_retcode(void *arg, int retcode);
+#endif
 static void pthread_handle_sigcancel(int sig);
 static void pthread_handle_sigrestart(int sig);
 static void pthread_handle_sigdebug(int sig);
@@ -433,12 +437,14 @@ static void pthread_initialize(void)
   sigprocmask(SIG_BLOCK, &mask, NULL);
   /* Register an exit function to kill all other threads. */
   /* Do it early so that user-registered atexit functions are called
-     before pthread_exit_process. */
+     before pthread_*exit_process. */
+#ifndef HAVE_Z_NODELETE
   if (__builtin_expect (&__dso_handle != NULL, 1))
-    __cxa_on_exit((void (*) (void *)) pthread_exit_process, NULL,
+    __cxa_atexit ((void (*) (void *)) pthread_atexit_process, NULL,
 		  __dso_handle);
   else
-    __on_exit (pthread_exit_process, NULL);
+#endif
+    __on_exit (pthread_onexit_process, NULL);
   /* How many processors.  */
   __pthread_smp_kernel = is_smp_system ();
 }
@@ -456,6 +462,12 @@ int __pthread_initialize_manager(void)
   struct rlimit limit;
   int max_stack;
 
+#ifndef HAVE_Z_NODELETE
+  if (__builtin_expect (&__dso_handle != NULL, 1))
+    __cxa_atexit ((void (*) (void *)) pthread_atexit_retcode, NULL,
+		  __dso_handle);
+#endif
+
   getrlimit(RLIMIT_STACK, &limit);
 #ifdef FLOATING_STACKS
   if (limit.rlim_cur == RLIM_INFINITY)
@@ -723,7 +735,7 @@ weak_alias (__pthread_yield, pthread_yie
 
 /* Process-wide exit() request */
 
-static void pthread_exit_process(int retcode, void *arg)
+static void pthread_onexit_process(int retcode, void *arg)
 {
   if (__builtin_expect (__pthread_manager_request, 0) >= 0) {
     struct pthread_request request;
@@ -744,6 +756,20 @@ static void pthread_exit_process(int ret
       }
   }
 }
+
+#ifndef HAVE_Z_NODELETE
+static int __pthread_atexit_retcode;
+
+static void pthread_atexit_process(void *arg, int retcode)
+{
+  pthread_onexit_process (retcode ?: __pthread_atexit_retcode, arg);
+}
+
+static void pthread_atexit_retcode(void *arg, int retcode)
+{
+  __pthread_atexit_retcode = retcode;
+}
+#endif
 
 /* The handler for the RESTART signal just records the signal received
    in the thread descriptor, and optionally performs a siglongjmp
--- libc/stdlib/Makefile.jj	Thu Jan 11 23:07:36 2001
+++ libc/stdlib/Makefile	Thu Jan 11 23:36:43 2001
@@ -1,4 +1,4 @@
-# Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc.
+# Copyright (C) 1991-1999, 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
@@ -29,7 +29,7 @@ routines	:=							      \
 	abort								      \
 	bsearch qsort msort						      \
 	getenv putenv setenv secure-getenv				      \
-	exit on_exit atexit cxa_atexit cxa_on_exit cxa_finalize		      \
+	exit on_exit atexit cxa_atexit cxa_finalize			      \
 	abs labs llabs							      \
 	div ldiv lldiv							      \
 	mblen mbstowcs mbtowc wcstombs wctomb				      \
--- libc/stdlib/Versions.jj	Thu Jan 11 23:07:36 2001
+++ libc/stdlib/Versions	Thu Jan 11 23:36:43 2001
@@ -94,8 +94,4 @@ libc {
     # used by new G++ ABI
     __cxa_atexit; __cxa_finalize;
   }
-  GLIBC_2.2.1 {
-    # used in the thread library
-    __cxa_on_exit;
-  }
 }
--- libc/stdlib/exit.c.jj	Thu Jan 11 23:07:36 2001
+++ libc/stdlib/exit.c	Thu Jan 11 23:37:45 2001
@@ -57,10 +57,7 @@ exit (int status)
 	      (*f->func.at) ();
 	      break;
 	    case ef_cxa:
-	      (*f->func.cxa.fn) (f->func.cxa.arg);
-	      break;
-	    case ef_cxa2:
-	      (*f->func.cxa2.fn) (status, f->func.cxa2.arg);
+	      (*f->func.cxa.fn) (f->func.cxa.arg, status);
 	      break;
 	    }
 	}
--- libc/stdlib/exit.h.jj	Thu Jan 11 23:07:36 2001
+++ libc/stdlib/exit.h	Thu Jan 11 23:40:11 2001
@@ -26,8 +26,7 @@ enum
   ef_us,
   ef_on,
   ef_at,
-  ef_cxa,
-  ef_cxa2
+  ef_cxa
 };
 
 struct exit_function
@@ -45,16 +44,10 @@ struct exit_function
 	  } on;
 	struct
 	  {
-	    void (*fn) (void *arg);
+	    void (*fn) (void *arg, int status);
 	    void *arg;
 	    void *dso_handle;
 	  } cxa;
-	struct
-	  {
-	    void (*fn) (int status, void *arg);
-	    void *arg;
-	    void *dso_handle;
-	  } cxa2;
       } func;
   };
 struct exit_function_list
--- libc/stdlib/cxa_on_exit.c.jj	Thu Jan 11 20:22:45 2001
+++ libc/stdlib/cxa_on_exit.c	Thu Jan 11 23:37:06 2001
@@ -1,38 +0,0 @@
-/* Copyright (C) 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 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 <stdlib.h>
-#include "exit.h"
-
-/* Register a function to be called by exit or when a shared library
-   is unloaded.  This function is only called from code generated by
-   the C++ compiler.  */
-int
-__cxa_on_exit (void (*func) (int, void *), void *arg, void *d)
-{
-  struct exit_function *new = __new_exitfn ();
-
-  if (new == NULL)
-    return -1;
-
-  new->flavor = ef_cxa2;
-  new->func.cxa2.fn = func;
-  new->func.cxa2.arg = arg;
-  new->func.cxa2.dso_handle = d;
-  return 0;
-}
--- libc/stdlib/cxa_finalize.c.jj	Mon Dec 27 22:47:52 1999
+++ libc/stdlib/cxa_finalize.c	Thu Jan 11 23:38:32 2001
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1999, 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
@@ -39,6 +39,6 @@ __cxa_finalize (void *d)
 	if (d == f->func.cxa.dso_handle
 	    /* We don't want to run this cleanup more than once.  */
 	    && compare_and_swap (&f->flavor, ef_cxa, ef_free))
-	  (*f->func.cxa.fn) (f->func.cxa.arg);
+	  (*f->func.cxa.fn) (f->func.cxa.arg, 0);
     }
 }
--- libc/stdlib/cxa_atexit.c.jj	Mon Dec 27 22:47:26 1999
+++ libc/stdlib/cxa_atexit.c	Thu Jan 11 23:39:17 2001
@@ -1,4 +1,4 @@
-/* Copyright (C) 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1999, 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
@@ -31,7 +31,7 @@ __cxa_atexit (void (*func) (void *), voi
     return -1;
 
   new->flavor = ef_cxa;
-  new->func.cxa.fn = func;
+  new->func.cxa.fn = (void (*) (void *, int)) func;
   new->func.cxa.arg = arg;
   new->func.cxa.dso_handle = d;
   return 0;


	Jakub

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]