}
GLIBC_PRIVATE {
+ __google_pthread_signal_safe_key_create;
__pthread_initialize_minimal;
__pthread_clock_gettime; __pthread_clock_settime;
__pthread_unwind; __pthread_get_minstack;
#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;
{
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
#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
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;
#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
#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
#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
#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
#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