]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Forward-port addition of __google_pthread_signal_safe_key_create
authorStan Shebs <stanshebs@google.com>
Mon, 12 Mar 2018 21:59:51 +0000 (14:59 -0700)
committerStan Shebs <stanshebs@google.com>
Mon, 12 Mar 2018 21:59:51 +0000 (14:59 -0700)
nptl/Versions
nptl/pthread_key_create.c
nptl/tst-key1.c
nptl/tst-key4.c
sysdeps/nptl/pthread.h
sysdeps/unix/sysv/linux/aarch64/bits/local_lim.h
sysdeps/unix/sysv/linux/alpha/bits/local_lim.h
sysdeps/unix/sysv/linux/bits/local_lim.h
sysdeps/unix/sysv/linux/powerpc/bits/local_lim.h
sysdeps/unix/sysv/linux/sparc/bits/local_lim.h

index 0ae5def464b9c8a77bb79895f37c43e3e3226f29..eeca21b60105011d2feba879fc317086d9c598bd 100644 (file)
@@ -266,6 +266,7 @@ libpthread {
   }
 
   GLIBC_PRIVATE {
+    __google_pthread_signal_safe_key_create;
     __pthread_initialize_minimal;
     __pthread_clock_gettime; __pthread_clock_settime;
     __pthread_unwind; __pthread_get_minstack;
index 0c0e53c44adc948a7f27a1b1de1a72bd9d0941b2..4ff1e05c8d278ec5374f2457670675f07b7b0291 100644 (file)
 
 #include <errno.h>
 #include "pthreadP.h"
+#include <assert.h>
 #include <atomic.h>
 
+static int
+claim_key (pthread_key_t *key, void (*destr) (void *), size_t cnt)
+{
+  uintptr_t seq = __pthread_keys[cnt].seq;
+  if (KEY_UNUSED (seq) && KEY_USABLE (seq)
+      /* We found an unused slot.  Try to allocate it.  */
+      && ! atomic_compare_and_exchange_bool_acq (&__pthread_keys[cnt].seq,
+                                                 seq + 1, seq))
+    {
+      /* Remember the destructor.  */
+      __pthread_keys[cnt].destr = destr;
+
+      /* Return the key to the caller.  */
+      *key = cnt;
+      return 0;
+    }
+  return -1;
+}
 
 int
 __pthread_key_create (pthread_key_t *key, void (*destr) (void *))
 {
   /* Find a slot in __pthread_keys which is unused.  */
-  for (size_t cnt = 0; cnt < PTHREAD_KEYS_MAX; ++cnt)
+  for (size_t cnt = PTHREAD_SIGNAL_SAFE_KEYS_MAX; cnt < PTHREAD_KEYS_MAX; ++cnt)
+    {
+      if (claim_key (key, destr, cnt) == 0)
+        return 0;
+    }
+
+  return EAGAIN;
+}
+
+int __google_pthread_signal_safe_key_create (
+    pthread_key_t *key, void (*destr) (void *))
+{
+  /* Our implementation makes signal safe keys easy: they just have to
+     reside in the first (inline) block. */
+  assert (PTHREAD_SIGNAL_SAFE_KEYS_MAX <= PTHREAD_KEY_1STLEVEL_SIZE);
+  /* Find a slot in __pthread_keys which is unused.  */
+  for (size_t cnt = 0; cnt < PTHREAD_SIGNAL_SAFE_KEYS_MAX; ++cnt)
     {
-      uintptr_t seq = __pthread_keys[cnt].seq;
-
-      if (KEY_UNUSED (seq) && KEY_USABLE (seq)
-         /* We found an unused slot.  Try to allocate it.  */
-         && ! atomic_compare_and_exchange_bool_acq (&__pthread_keys[cnt].seq,
-                                                    seq + 1, seq))
-       {
-         /* Remember the destructor.  */
-         __pthread_keys[cnt].destr = destr;
-
-         /* Return the key to the caller.  */
-         *key = cnt;
-
-         /* The call succeeded.  */
-         return 0;
-       }
+      if (claim_key (key, destr, cnt) == 0)
+        return 0;
     }
 
   return EAGAIN;
index 8227f9e843d2346a755b197dc655adab3252c6bc..3fc7487bcd452ad6bc22a50bcc40efc09e4503e4 100644 (file)
@@ -33,7 +33,11 @@ do_test (void)
 {
   int max;
 #ifdef PTHREAD_KEYS_MAX
+#ifdef PTHREAD_SIGNAL_SAFE_KEYS_MAX
+  max = PTHREAD_KEYS_MAX - PTHREAD_SIGNAL_SAFE_KEYS_MAX;
+#else
   max = PTHREAD_KEYS_MAX;
+#endif
 #else
   max = _POSIX_THREAD_KEYS_MAX;
 #endif
index 810f25b3709cc815715d46d46fe9f3a833a075c9..aa795f4a3c4a332c314887080ccd9fb3f317bb51 100644 (file)
 
 
 #ifdef PTHREAD_KEYS_MAX
+#ifdef PTHREAD_SIGNAL_SAFE_KEYS_MAX
+const int max = PTHREAD_KEYS_MAX - PTHREAD_SIGNAL_SAFE_KEYS_MAX;
+#else
 const int max = PTHREAD_KEYS_MAX;
+#endif
 #else
 const int max = _POSIX_THREAD_KEYS_MAX;
 #endif
index df049abf74d4752280b81d6eacd7dc7e454a61b5..fa125e0e6090d15c9230c36c053f3c0ce5f4d801 100644 (file)
@@ -1113,6 +1113,20 @@ extern int pthread_key_create (pthread_key_t *__key,
                               void (*__destr_function) (void *))
      __THROW __nonnull ((1));
 
+/* Exactly as pthread_key_create, but returns an async-signal-safe key:
+   getspecific, setspecific, and key_delete are async-signal-safe when
+   used with the returned key. The number of signal_safe keys is *extremely*
+   limited (PTHREAD_SIGNAL_SAFE_KEY_MAX); you are highly recommended to use
+   pthread_key_create unless it's absolutely necessary.  This function
+   is async-signal-safe. */
+#if !defined(IS_IN_libpthread)
+__attribute__((weak))
+#endif
+extern int __google_pthread_signal_safe_key_create (pthread_key_t *__key,
+                                                   void (*__destr_function) (void *))
+    __THROW  __nonnull ((1));
+
+
 /* Destroy KEY.  */
 extern int pthread_key_delete (pthread_key_t __key) __THROW;
 
index 37aab9a24dd5280c574d8108cbf7e1a66db63a2a..43286195c341b43184e926fb4fd026b7ef015561 100644 (file)
@@ -63,6 +63,7 @@
 #define _POSIX_THREAD_KEYS_MAX 128
 /* This is the value this implementation supports.  */
 #define PTHREAD_KEYS_MAX       1024
+#define PTHREAD_SIGNAL_SAFE_KEYS_MAX   4
 
 /* Controlling the iterations of destructors for thread-specific data.  */
 #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS    4
index 7805fa510eafa13bc8f644748d53ac436edfc054..904bb5c574ab394a6a826588e09a5813cdf834b5 100644 (file)
@@ -62,6 +62,7 @@
 #define _POSIX_THREAD_KEYS_MAX 128
 /* This is the value this implementation supports.  */
 #define PTHREAD_KEYS_MAX       1024
+#define PTHREAD_SIGNAL_SAFE_KEYS_MAX   4
 
 /* Controlling the iterations of destructors for thread-specific data.  */
 #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS    4
index 2d82ada43cc2b2f9fb6eafc8dc48173ef03f513d..ae2d914b77bbc3bb61848ef9054b05e34694e349 100644 (file)
@@ -62,6 +62,7 @@
 #define _POSIX_THREAD_KEYS_MAX 128
 /* This is the value this implementation supports.  */
 #define PTHREAD_KEYS_MAX       1024
+#define PTHREAD_SIGNAL_SAFE_KEYS_MAX   4
 
 /* Controlling the iterations of destructors for thread-specific data.  */
 #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS    4
index 049094e467159e582ba54482852213a161d168e9..dfcf7288845cbc86b471295b5486ea7325fd83c3 100644 (file)
@@ -62,6 +62,7 @@
 #define _POSIX_THREAD_KEYS_MAX 128
 /* This is the value this implementation supports.  */
 #define PTHREAD_KEYS_MAX       1024
+#define PTHREAD_SIGNAL_SAFE_KEYS_MAX   4
 
 /* Controlling the iterations of destructors for thread-specific data.  */
 #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS    4
index a7ec88e1915b897ec17ac48e29ffafda3fdb328d..954650ed4a008656c153aec2c2573e4d4640500b 100644 (file)
@@ -62,6 +62,7 @@
 #define _POSIX_THREAD_KEYS_MAX 128
 /* This is the value this implementation supports.  */
 #define PTHREAD_KEYS_MAX       1024
+#define PTHREAD_SIGNAL_SAFE_KEYS_MAX   4
 
 /* Controlling the iterations of destructors for thread-specific data.  */
 #define _POSIX_THREAD_DESTRUCTOR_ITERATIONS    4