public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/google/grte/v5-2.27/master] Forward-port addition of __google_pthread_signal_safe_key_create
@ 2021-08-28 0:39 Fangrui Song
0 siblings, 0 replies; only message in thread
From: Fangrui Song @ 2021-08-28 0:39 UTC (permalink / raw)
To: glibc-cvs
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=491d419cdde1d5ded66dda5d08016cd014f8d655
commit 491d419cdde1d5ded66dda5d08016cd014f8d655
Author: Stan Shebs <stanshebs@google.com>
Date: Mon Mar 12 14:59:51 2018 -0700
Forward-port addition of __google_pthread_signal_safe_key_create
Diff:
---
nptl/Versions | 1 +
nptl/pthread_key_create.c | 55 ++++++++++++++++--------
nptl/tst-key1.c | 4 ++
nptl/tst-key4.c | 4 ++
sysdeps/nptl/pthread.h | 14 ++++++
sysdeps/unix/sysv/linux/aarch64/bits/local_lim.h | 1 +
sysdeps/unix/sysv/linux/alpha/bits/local_lim.h | 1 +
sysdeps/unix/sysv/linux/bits/local_lim.h | 1 +
sysdeps/unix/sysv/linux/powerpc/bits/local_lim.h | 1 +
sysdeps/unix/sysv/linux/sparc/bits/local_lim.h | 1 +
10 files changed, 66 insertions(+), 17 deletions(-)
diff --git a/nptl/Versions b/nptl/Versions
index 0ae5def464..eeca21b601 100644
--- a/nptl/Versions
+++ b/nptl/Versions
@@ -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;
diff --git a/nptl/pthread_key_create.c b/nptl/pthread_key_create.c
index 0c0e53c44a..4ff1e05c8d 100644
--- a/nptl/pthread_key_create.c
+++ b/nptl/pthread_key_create.c
@@ -18,31 +18,52 @@
#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;
diff --git a/nptl/tst-key1.c b/nptl/tst-key1.c
index 8227f9e843..3fc7487bcd 100644
--- a/nptl/tst-key1.c
+++ b/nptl/tst-key1.c
@@ -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
diff --git a/nptl/tst-key4.c b/nptl/tst-key4.c
index 810f25b370..aa795f4a3c 100644
--- a/nptl/tst-key4.c
+++ b/nptl/tst-key4.c
@@ -24,7 +24,11 @@
#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
diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h
index df049abf74..fa125e0e60 100644
--- a/sysdeps/nptl/pthread.h
+++ b/sysdeps/nptl/pthread.h
@@ -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;
diff --git a/sysdeps/unix/sysv/linux/aarch64/bits/local_lim.h b/sysdeps/unix/sysv/linux/aarch64/bits/local_lim.h
index 37aab9a24d..43286195c3 100644
--- a/sysdeps/unix/sysv/linux/aarch64/bits/local_lim.h
+++ b/sysdeps/unix/sysv/linux/aarch64/bits/local_lim.h
@@ -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
diff --git a/sysdeps/unix/sysv/linux/alpha/bits/local_lim.h b/sysdeps/unix/sysv/linux/alpha/bits/local_lim.h
index 7805fa510e..904bb5c574 100644
--- a/sysdeps/unix/sysv/linux/alpha/bits/local_lim.h
+++ b/sysdeps/unix/sysv/linux/alpha/bits/local_lim.h
@@ -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
diff --git a/sysdeps/unix/sysv/linux/bits/local_lim.h b/sysdeps/unix/sysv/linux/bits/local_lim.h
index 2d82ada43c..ae2d914b77 100644
--- a/sysdeps/unix/sysv/linux/bits/local_lim.h
+++ b/sysdeps/unix/sysv/linux/bits/local_lim.h
@@ -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
diff --git a/sysdeps/unix/sysv/linux/powerpc/bits/local_lim.h b/sysdeps/unix/sysv/linux/powerpc/bits/local_lim.h
index 049094e467..dfcf728884 100644
--- a/sysdeps/unix/sysv/linux/powerpc/bits/local_lim.h
+++ b/sysdeps/unix/sysv/linux/powerpc/bits/local_lim.h
@@ -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
diff --git a/sysdeps/unix/sysv/linux/sparc/bits/local_lim.h b/sysdeps/unix/sysv/linux/sparc/bits/local_lim.h
index a7ec88e191..954650ed4a 100644
--- a/sysdeps/unix/sysv/linux/sparc/bits/local_lim.h
+++ b/sysdeps/unix/sysv/linux/sparc/bits/local_lim.h
@@ -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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-08-28 0:39 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-28 0:39 [glibc/google/grte/v5-2.27/master] Forward-port addition of __google_pthread_signal_safe_key_create Fangrui Song
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).