Source-Changes-HG archive

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

[src/trunk]: src/lib/libpthread Enhance the pthread(3) + malloc(3) init model



details:   https://anonhg.NetBSD.org/src/rev/ec102982bbee
branches:  trunk
changeset: 969333:ec102982bbee
user:      kamil <kamil%NetBSD.org@localhost>
date:      Sat Feb 15 23:59:30 2020 +0000

description:
Enhance the pthread(3) + malloc(3) init model

Separate the pthread_atfork(3) call from pthread_tsd_init()
and move it into a distinct function.

Call inside pthread__init() late TSD initialization route, just after
"pthread_atfork(NULL, NULL, pthread__fork_callback);".

Document that malloc(3) initialization is now controlled again and called
during the first pthread_atfork(3) call.

Remove #if 0 code from pthread_mutex.c as we no longer initialize malloc
prematurely.

diffstat:

 lib/libpthread/pthread.c       |  16 ++++++++++++----
 lib/libpthread/pthread_int.h   |   5 +++--
 lib/libpthread/pthread_mutex.c |   9 +++------
 lib/libpthread/pthread_tsd.c   |  39 ++++++++++++++++++++++-----------------
 4 files changed, 40 insertions(+), 29 deletions(-)

diffs (182 lines):

diff -r de6a093f006c -r ec102982bbee lib/libpthread/pthread.c
--- a/lib/libpthread/pthread.c  Sat Feb 15 23:42:01 2020 +0000
+++ b/lib/libpthread/pthread.c  Sat Feb 15 23:59:30 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread.c,v 1.164 2020/02/08 17:06:03 kamil Exp $      */
+/*     $NetBSD: pthread.c,v 1.165 2020/02/15 23:59:30 kamil Exp $      */
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread.c,v 1.164 2020/02/08 17:06:03 kamil Exp $");
+__RCSID("$NetBSD: pthread.c,v 1.165 2020/02/15 23:59:30 kamil Exp $");
 
 #define        __EXPOSE_STACK  1
 
@@ -181,7 +181,7 @@
         * while pthread_keys descriptors are not
         * yet allocated.
         */
-       pthread__main = pthread_tsd_init(&__pthread_st_size);
+       pthread__main = pthread_tsd_earlyinit(&__pthread_st_size);
        if (pthread__main == NULL)
                err(EXIT_FAILURE, "Cannot allocate pthread storage");
 
@@ -257,8 +257,16 @@
                }
        }
 
-       /* Tell libc that we're here and it should role-play accordingly. */
+       /*
+        * Tell libc that we're here and it should role-play accordingly.
+        *
+        * pthread_atfork(3) calls malloc(3) and initializes the system malloc.
+        */
        pthread_atfork(NULL, NULL, pthread__fork_callback);
+
+       /* Requires functional malloc(3). */
+       pthread_tsd_init();
+
        __isthreaded = 1;
 }
 
diff -r de6a093f006c -r ec102982bbee lib/libpthread/pthread_int.h
--- a/lib/libpthread/pthread_int.h      Sat Feb 15 23:42:01 2020 +0000
+++ b/lib/libpthread/pthread_int.h      Sat Feb 15 23:59:30 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_int.h,v 1.101 2020/02/05 11:05:10 kamil Exp $  */
+/*     $NetBSD: pthread_int.h,v 1.102 2020/02/15 23:59:30 kamil Exp $  */
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020
@@ -294,7 +294,8 @@
        }                                                               \
         } while (/*CONSTCOND*/0)
 
-void   *pthread_tsd_init(size_t *) PTHREAD_HIDE;
+void   *pthread_tsd_earlyinit(size_t *) PTHREAD_HIDE;
+void   pthread_tsd_init(void) PTHREAD_HIDE;
 void   pthread__destroy_tsd(pthread_t) PTHREAD_HIDE;
 void   pthread__copy_tsd(pthread_t) PTHREAD_HIDE;
 
diff -r de6a093f006c -r ec102982bbee lib/libpthread/pthread_mutex.c
--- a/lib/libpthread/pthread_mutex.c    Sat Feb 15 23:42:01 2020 +0000
+++ b/lib/libpthread/pthread_mutex.c    Sat Feb 15 23:59:30 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_mutex.c,v 1.74 2020/02/01 18:14:16 kamil Exp $ */
+/*     $NetBSD: pthread_mutex.c,v 1.75 2020/02/15 23:59:30 kamil Exp $ */
 
 /*-
  * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -47,7 +47,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_mutex.c,v 1.74 2020/02/01 18:14:16 kamil Exp $");
+__RCSID("$NetBSD: pthread_mutex.c,v 1.75 2020/02/15 23:59:30 kamil Exp $");
 
 #include <sys/types.h>
 #include <sys/lwpctl.h>
@@ -122,14 +122,12 @@
 {
        uintptr_t type, proto, val, ceil;
 
-#if 0
        /*
         * Always initialize the mutex structure, maybe be used later
         * and the cost should be minimal.
         */
        if (__predict_false(__uselibcstub))
                return __libc_mutex_init_stub(ptm, attr);
-#endif
 
        pthread__error(EINVAL, "Invalid mutes attribute",
            attr == NULL || attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
@@ -619,10 +617,9 @@
 int
 pthread_mutexattr_init(pthread_mutexattr_t *attr)
 {
-#if 0
+
        if (__predict_false(__uselibcstub))
                return __libc_mutexattr_init_stub(attr);
-#endif
 
        attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
        attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
diff -r de6a093f006c -r ec102982bbee lib/libpthread/pthread_tsd.c
--- a/lib/libpthread/pthread_tsd.c      Sat Feb 15 23:42:01 2020 +0000
+++ b/lib/libpthread/pthread_tsd.c      Sat Feb 15 23:59:30 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_tsd.c,v 1.18 2019/12/25 00:44:45 joerg Exp $   */
+/*     $NetBSD: pthread_tsd.c,v 1.19 2020/02/15 23:59:30 kamil Exp $   */
 
 /*-
  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_tsd.c,v 1.18 2019/12/25 00:44:45 joerg Exp $");
+__RCSID("$NetBSD: pthread_tsd.c,v 1.19 2020/02/15 23:59:30 kamil Exp $");
 
 /* Functions and structures dealing with thread-specific data */
 #include <errno.h>
@@ -61,27 +61,13 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-static void
-pthread_tsd_prefork(void)
-{
-       pthread_mutex_lock(&tsd_mutex);
-}
-
-static void
-pthread_tsd_postfork(void)
-{
-       pthread_mutex_unlock(&tsd_mutex);
-}
-
 void *
-pthread_tsd_init(size_t *tlen)
+pthread_tsd_earlyinit(size_t *tlen)
 {
        char *pkm;
        size_t alen;
        char *arena;
 
-       pthread_atfork(pthread_tsd_prefork, pthread_tsd_postfork, pthread_tsd_postfork);
-
        if ((pkm = pthread__getenv("PTHREAD_KEYS_MAX")) != NULL) {
                pthread_keys_max = (int)strtol(pkm, NULL, 0);
                if (pthread_keys_max < _POSIX_THREAD_KEYS_MAX)
@@ -113,6 +99,25 @@
        return arena;
 }
 
+static void
+pthread_tsd_prefork(void)
+{
+       pthread_mutex_lock(&tsd_mutex);
+}
+
+static void
+pthread_tsd_postfork(void)
+{
+       pthread_mutex_unlock(&tsd_mutex);
+}
+
+void
+pthread_tsd_init(void)
+{
+
+       pthread_atfork(pthread_tsd_prefork, pthread_tsd_postfork, pthread_tsd_postfork);
+}
+
 int
 pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
 {



Home | Main Index | Thread Index | Old Index