Index: pthread.c =================================================================== RCS file: /cvsroot/src/lib/libpthread/pthread.c,v retrieving revision 1.164 diff -u -r1.164 pthread.c --- pthread.c 8 Feb 2020 17:06:03 -0000 1.164 +++ pthread.c 15 Feb 2020 12:47:00 -0000 @@ -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; } Index: pthread_int.h =================================================================== RCS file: /cvsroot/src/lib/libpthread/pthread_int.h,v retrieving revision 1.101 diff -u -r1.101 pthread_int.h --- pthread_int.h 5 Feb 2020 11:05:10 -0000 1.101 +++ pthread_int.h 15 Feb 2020 12:47:00 -0000 @@ -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; Index: pthread_mutex.c =================================================================== RCS file: /cvsroot/src/lib/libpthread/pthread_mutex.c,v retrieving revision 1.74 diff -u -r1.74 pthread_mutex.c --- pthread_mutex.c 1 Feb 2020 18:14:16 -0000 1.74 +++ pthread_mutex.c 15 Feb 2020 12:47:00 -0000 @@ -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; Index: pthread_tsd.c =================================================================== RCS file: /cvsroot/src/lib/libpthread/pthread_tsd.c,v retrieving revision 1.18 diff -u -r1.18 pthread_tsd.c --- pthread_tsd.c 25 Dec 2019 00:44:45 -0000 1.18 +++ pthread_tsd.c 15 Feb 2020 12:47:00 -0000 @@ -61,27 +61,13 @@ #include #include -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 *)) {