about summary refs log tree commit diff
path: root/sysdeps/htl/pt-setspecific.c
diff options
context:
space:
mode:
authorSamuel Thibault <samuel.thibault@ens-lyon.org>2023-08-08 12:10:06 +0200
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2023-08-08 12:17:48 +0200
commit644aa127b9b42a899a12b6ccc6644bc035c231e3 (patch)
tree46590a3c1c485dc2f423ded55671f4cb56bed532 /sysdeps/htl/pt-setspecific.c
parentdcad5c8578130dec7f35fd5b0885304b59f9f543 (diff)
downloadglibc-644aa127b9b42a899a12b6ccc6644bc035c231e3.tar.gz
glibc-644aa127b9b42a899a12b6ccc6644bc035c231e3.tar.xz
glibc-644aa127b9b42a899a12b6ccc6644bc035c231e3.zip
htl: Add support for static TSD data
When using jemalloc, malloc() needs to use TSD, while libpthread
initialization needs malloc(). Supporting a static TSD area allows jemalloc
and libpthread to initialize together.
Diffstat (limited to 'sysdeps/htl/pt-setspecific.c')
-rw-r--r--sysdeps/htl/pt-setspecific.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/sysdeps/htl/pt-setspecific.c b/sysdeps/htl/pt-setspecific.c
index 2b9a89dc70..30550e09c7 100644
--- a/sysdeps/htl/pt-setspecific.c
+++ b/sysdeps/htl/pt-setspecific.c
@@ -28,12 +28,34 @@ __pthread_setspecific (pthread_key_t key, const void *value)
   if (key < 0 || key >= __pthread_key_count)
     return EINVAL;
 
+  if (self->thread_specifics == NULL)
+    {
+      if (key < PTHREAD_STATIC_KEYS)
+	{
+	  self->static_thread_specifics[key] = (void *) value;
+	  return 0;
+	}
+    }
+
   if (key >= self->thread_specifics_size)
     {
       /* Amortize reallocation cost.  */
       int newsize = 2 * key + 1;
-      void **new = realloc (self->thread_specifics,
-			    newsize * sizeof (new[0]));
+      void **new;
+
+      if (self->thread_specifics == NULL)
+	{
+	  self->thread_specifics_size = PTHREAD_STATIC_KEYS;
+	  new = malloc (newsize * sizeof (new[0]));
+	  if (new != NULL)
+	    memcpy (new, self->static_thread_specifics,
+		    PTHREAD_STATIC_KEYS * sizeof (new[0]));
+	}
+      else
+	{
+	  new = realloc (self->thread_specifics,
+			 newsize * sizeof (new[0]));
+	}
       if (new == NULL)
 	return ENOMEM;