git-jemalloc2
authorGNU Libc Maintainers <debian-glibc@lists.debian.org>
Tue, 3 Oct 2023 17:14:06 +0000 (18:14 +0100)
committerAurelien Jarno <aurel32@debian.org>
Tue, 3 Oct 2023 17:14:06 +0000 (18:14 +0100)
commit 53da64d1cf3694a132287d1ab6e9655031124e32
Author: Samuel Thibault <samuel.thibault@ens-lyon.org>
Date:   Tue Aug 8 12:19:29 2023 +0200

    htl: Initialize ___pthread_self early

    When using jemalloc, malloc() needs to use TSD, while libpthread
    initialization needs malloc(). Having ___pthread_self set early to some
    static storage allows TSD to work early, thus allowing jemalloc and
    libpthread to initialize together.

    This incidentaly simplifies __pthread_enable/disable_asynccancel and
    __pthread_self, now that ___pthread_self is always initialized.

Gbp-Pq: Topic hurd-i386
Gbp-Pq: Name git-jemalloc2.diff

htl/cancellation.c
htl/pt-self.c
sysdeps/mach/hurd/htl/pt-sysdep.c

index 9cc61b9f231e6be2a86fb4591ee153f0bd709057..5a670831886907202374de4279cc43c4e36d747f 100644 (file)
@@ -25,10 +25,6 @@ int __pthread_enable_asynccancel (void)
   struct __pthread *p = _pthread_self ();
   int oldtype;
 
-  if (___pthread_self == NULL)
-    /* We are not initialized yet, we can't be cancelled anyway.  */
-    return PTHREAD_CANCEL_DEFERRED;
-
   __pthread_mutex_lock (&p->cancel_lock);
   oldtype = p->cancel_type;
   p->cancel_type = PTHREAD_CANCEL_ASYNCHRONOUS;
@@ -43,10 +39,6 @@ void __pthread_disable_asynccancel (int oldtype)
 {
   struct __pthread *p = _pthread_self ();
 
-  if (___pthread_self == NULL)
-    /* We are not initialized yet, we can't be cancelled anyway.  */
-    return;
-
   __pthread_mutex_lock (&p->cancel_lock);
   p->cancel_type = oldtype;
   __pthread_mutex_unlock (&p->cancel_lock);
index 26d2b7c5ebe8ca7b7f2e04a17f0ca53d7b6e94fa..ab35e28f15375f13a2885f38b94192687b3e8efa 100644 (file)
 pthread_t
 __pthread_self (void)
 {
-  struct __pthread *self;
-
-  if (___pthread_self == NULL)
-    /* We are not initialized yet, we are the first thread.  */
-    return 1;
-
-  self = _pthread_self ();
-  assert (self != NULL);
-
+  struct __pthread *self = _pthread_self ();
   return self->thread;
 }
 
index 0e8b537bdc4a21f292d65bd5d114666e077a0cc6..3960ffd6dc6b21365bac4ee256738aee3146ce53 100644 (file)
 
 __thread struct __pthread *___pthread_self;
 
+/* Initial thread structure used temporarily during initialization, so various
+ * functions can already work at least basically.  */
+static struct __pthread init_thread;
+
 static void
 reset_pthread_total (void)
 {
@@ -49,6 +53,10 @@ _init_routine (void *stack)
     /* Already initialized */
     return;
 
+  /* Initialize early thread structure.  */
+  init_thread.thread = 1;
+  ___pthread_self = &init_thread;
+
   /* Initialize the library.  */
   ___pthread_init ();
 
@@ -76,6 +84,12 @@ _init_routine (void *stack)
   __pthread_default_attr.__guardsize = __vm_page_size;
 #endif
 
+  /* Copy over the thread-specific state */
+  assert (!init_thread.thread_specifics);
+  memcpy (&thread->static_thread_specifics,
+          &init_thread.static_thread_specifics,
+          sizeof (thread->static_thread_specifics));
+
   ___pthread_self = thread;
 
   /* Decrease the number of threads, to take into account that the