malloc patch for atfork

Wolfram Gloger wmglo@dent.med.uni-muenchen.de
Fri Jun 22 04:27:00 GMT 2001


Hi,

I suggest that the following small patch is applied for 2.2.4, even
though I can't yet provide a testcase which is fixed by this change.
Several people are currently examining the problem and a test case
will emerge when the problem is analyzed more completely.  The code
that is still hanging with an SMP Linux system can be found at:

http://www.malloc.de/tests/fork-malloc.c

I have tested the patch for several weeks now on UP and dual-SMP.

Regards,
Wolfram.

2001-06-01  Wolfram Gloger  <wg@malloc.de>

	* malloc/malloc.c (malloc_atfork, free_atfork): Use a unique value
	ATFORK_ARENA_PTR, not 0, for the thread-specific arena pointer
	when malloc_atfork is in use.

--- malloc/malloc.c	2001/02/14 22:27:17	1.1.1.22
+++ malloc/malloc.c	2001/06/01 15:38:53
@@ -1590,6 +1590,11 @@
 
 #ifndef NO_THREADS
 
+/* Magic value for the thread-specific arena pointer when
+   malloc_atfork() is in use.  */
+
+#define ATFORK_ARENA_PTR ((Void_t*)-1)
+
 /* The following two functions are registered via thread_atfork() to
    make sure that the mutexes remain in a consistent state in the
    fork()ed version of a thread.  Also adapt the malloc and free hooks
@@ -1620,7 +1625,7 @@
   __free_hook = free_atfork;
   /* Only the current thread may perform malloc/free calls now. */
   tsd_getspecific(arena_key, save_arena);
-  tsd_setspecific(arena_key, (Void_t*)0);
+  tsd_setspecific(arena_key, ATFORK_ARENA_PTR);
 #endif
 }
 
@@ -4140,6 +4145,8 @@
 
 #ifndef NO_THREADS
   tsd_getspecific(arena_key, vptr);
+  if(vptr == ATFORK_ARENA_PTR)
+    vptr = (Void_t*)&main_arena;
 #endif
   malloc_update_mallinfo((vptr ? (arena*)vptr : &main_arena), &mi);
   return mi;
@@ -4687,7 +4694,8 @@
   mchunkptr victim;
 
   tsd_getspecific(arena_key, vptr);
-  if(!vptr) {
+  if(vptr == ATFORK_ARENA_PTR) {
+    /* We are the only thread that may allocate at all.  */
     if(save_malloc_hook != malloc_check) {
       if(request2size(sz, nb))
         return 0;
@@ -4735,10 +4743,10 @@
 
   ar_ptr = arena_for_ptr(p);
   tsd_getspecific(arena_key, vptr);
-  if(vptr)
+  if(vptr != ATFORK_ARENA_PTR)
     (void)mutex_lock(&ar_ptr->mutex);
   chunk_free(ar_ptr, p);
-  if(vptr)
+  if(vptr != ATFORK_ARENA_PTR)
     (void)mutex_unlock(&ar_ptr->mutex);
 }
 



More information about the Libc-hacker mailing list