Source-Changes-HG archive

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

[src/trunk]: src/lib/libpthread Since pthread_setspecific requires locks, ens...



details:   https://anonhg.NetBSD.org/src/rev/63d837fe580b
branches:  trunk
changeset: 967869:63d837fe580b
user:      joerg <joerg%NetBSD.org@localhost>
date:      Wed Dec 25 00:44:45 2019 +0000

description:
Since pthread_setspecific requires locks, ensure that they are acquired
before fork and dropped in both parent and child. At least Python
depends on TSD after fork, even though it is undefined behavior in
POSIX.

diffstat:

 lib/libpthread/pthread_tsd.c |  18 ++++++++++++++++--
 1 files changed, 16 insertions(+), 2 deletions(-)

diffs (46 lines):

diff -r 48e6193cef33 -r 63d837fe580b lib/libpthread/pthread_tsd.c
--- a/lib/libpthread/pthread_tsd.c      Tue Dec 24 23:41:05 2019 +0000
+++ b/lib/libpthread/pthread_tsd.c      Wed Dec 25 00:44:45 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pthread_tsd.c,v 1.17 2019/03/05 01:35:52 christos Exp $        */
+/*     $NetBSD: pthread_tsd.c,v 1.18 2019/12/25 00:44:45 joerg Exp $   */
 
 /*-
  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_tsd.c,v 1.17 2019/03/05 01:35:52 christos Exp $");
+__RCSID("$NetBSD: pthread_tsd.c,v 1.18 2019/12/25 00:44:45 joerg Exp $");
 
 /* Functions and structures dealing with thread-specific data */
 #include <errno.h>
@@ -61,6 +61,18 @@
 #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)
 {
@@ -68,6 +80,8 @@
        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)



Home | Main Index | Thread Index | Old Index