Module Name: src
Committed By: kamil
Date: Sat Feb 15 23:59:30 UTC 2020
Modified Files:
src/lib/libpthread: pthread.c pthread_int.h pthread_mutex.c
pthread_tsd.c
Log Message:
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.
To generate a diff of this commit:
cvs rdiff -u -r1.164 -r1.165 src/lib/libpthread/pthread.c
cvs rdiff -u -r1.101 -r1.102 src/lib/libpthread/pthread_int.h
cvs rdiff -u -r1.74 -r1.75 src/lib/libpthread/pthread_mutex.c
cvs rdiff -u -r1.18 -r1.19 src/lib/libpthread/pthread_tsd.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/lib/libpthread/pthread.c
diff -u src/lib/libpthread/pthread.c:1.164 src/lib/libpthread/pthread.c:1.165
--- src/lib/libpthread/pthread.c:1.164 Sat Feb 8 17:06:03 2020
+++ src/lib/libpthread/pthread.c Sat Feb 15 23:59:30 2020
@@ -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 @@ pthread__init(void)
* 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 @@ pthread__init(void)
}
}
- /* 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;
}
Index: src/lib/libpthread/pthread_int.h
diff -u src/lib/libpthread/pthread_int.h:1.101 src/lib/libpthread/pthread_int.h:1.102
--- src/lib/libpthread/pthread_int.h:1.101 Wed Feb 5 11:05:10 2020
+++ src/lib/libpthread/pthread_int.h Sat Feb 15 23:59:30 2020
@@ -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 @@ pthread__self(void)
} \
} 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;
Index: src/lib/libpthread/pthread_mutex.c
diff -u src/lib/libpthread/pthread_mutex.c:1.74 src/lib/libpthread/pthread_mutex.c:1.75
--- src/lib/libpthread/pthread_mutex.c:1.74 Sat Feb 1 18:14:16 2020
+++ src/lib/libpthread/pthread_mutex.c Sat Feb 15 23:59:30 2020
@@ -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 @@ pthread_mutex_init(pthread_mutex_t *ptm,
{
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 @@ pthread__mutex_wakeup(pthread_t self, pt
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;
Index: src/lib/libpthread/pthread_tsd.c
diff -u src/lib/libpthread/pthread_tsd.c:1.18 src/lib/libpthread/pthread_tsd.c:1.19
--- src/lib/libpthread/pthread_tsd.c:1.18 Wed Dec 25 00:44:45 2019
+++ src/lib/libpthread/pthread_tsd.c Sat Feb 15 23:59:30 2020
@@ -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 @@ null_destructor(void *p)
#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 @@ pthread_tsd_init(size_t *tlen)
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 *))
{