summaryrefslogtreecommitdiffstats
path: root/libc/bionic
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2013-02-11 20:18:47 +0000
committerElliott Hughes <enh@google.com>2013-02-11 12:20:33 -0800
commit44b53ad6818de344e0b499ad8fdbb21fcb0ff2b6 (patch)
tree9a2c45004114f53c4cf1f9468a58fe54c58320fa /libc/bionic
parent8397cdba9424febeaed4068829a5b0174ee1138c (diff)
downloadbionic-44b53ad6818de344e0b499ad8fdbb21fcb0ff2b6.zip
bionic-44b53ad6818de344e0b499ad8fdbb21fcb0ff2b6.tar.gz
bionic-44b53ad6818de344e0b499ad8fdbb21fcb0ff2b6.tar.bz2
Revert "Revert "Pull the pthread_key_t functions out of pthread.c.""
This reverts commit 6260553d48f6fd87ca220270bea8bafdde5726ec (Removing the accidental libm/Android.mk change.) Change-Id: I6cddd9857c31facc05636e8221505b3d2344cb75
Diffstat (limited to 'libc/bionic')
-rw-r--r--libc/bionic/pthread.c304
-rw-r--r--libc/bionic/pthread_internal.h5
-rw-r--r--libc/bionic/pthread_key.cpp280
-rw-r--r--libc/bionic/sysconf.cpp12
4 files changed, 294 insertions, 307 deletions
diff --git a/libc/bionic/pthread.c b/libc/bionic/pthread.c
index 88a972d..e1ace7d 100644
--- a/libc/bionic/pthread.c
+++ b/libc/bionic/pthread.c
@@ -101,8 +101,8 @@ static const pthread_attr_t gDefaultPthreadAttr = {
.sched_priority = 0
};
-static pthread_internal_t* gThreadList = NULL;
-static pthread_mutex_t gThreadListLock = PTHREAD_MUTEX_INITIALIZER;
+__LIBC_HIDDEN__ pthread_internal_t* gThreadList = NULL;
+__LIBC_HIDDEN__ pthread_mutex_t gThreadListLock = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t gDebuggerNotificationLock = PTHREAD_MUTEX_INITIALIZER;
static void _pthread_internal_remove_locked(pthread_internal_t* thread) {
@@ -550,9 +550,6 @@ void __pthread_cleanup_pop( __pthread_cleanup_t* c, int execute )
c->__cleanup_routine(c->__cleanup_arg);
}
-/* used by pthread_exit() to clean all TLS keys of the current thread */
-static void pthread_key_clean_all(void);
-
void pthread_exit(void * retval)
{
pthread_internal_t* thread = __get_thread();
@@ -1780,303 +1777,6 @@ int pthread_cond_timeout_np(pthread_cond_t *cond,
}
-
-/* A technical note regarding our thread-local-storage (TLS) implementation:
- *
- * There can be up to TLSMAP_SIZE independent TLS keys in a given process,
- * though the first TLSMAP_START keys are reserved for Bionic to hold
- * special thread-specific variables like errno or a pointer to
- * the current thread's descriptor.
- *
- * while stored in the TLS area, these entries cannot be accessed through
- * pthread_getspecific() / pthread_setspecific() and pthread_key_delete()
- *
- * also, some entries in the key table are pre-allocated (see tlsmap_lock)
- * to greatly simplify and speedup some OpenGL-related operations. though the
- * initialy value will be NULL on all threads.
- *
- * you can use pthread_getspecific()/setspecific() on these, and in theory
- * you could also call pthread_key_delete() as well, though this would
- * probably break some apps.
- *
- * The 'tlsmap_t' type defined below implements a shared global map of
- * currently created/allocated TLS keys and the destructors associated
- * with them. You should use tlsmap_lock/unlock to access it to avoid
- * any race condition.
- *
- * the global TLS map simply contains a bitmap of allocated keys, and
- * an array of destructors.
- *
- * each thread has a TLS area that is a simple array of TLSMAP_SIZE void*
- * pointers. the TLS area of the main thread is stack-allocated in
- * __libc_init_common, while the TLS area of other threads is placed at
- * the top of their stack in pthread_create.
- *
- * when pthread_key_create() is called, it finds the first free key in the
- * bitmap, then set it to 1, saving the destructor altogether
- *
- * when pthread_key_delete() is called. it will erase the key's bitmap bit
- * and its destructor, and will also clear the key data in the TLS area of
- * all created threads. As mandated by Posix, it is the responsability of
- * the caller of pthread_key_delete() to properly reclaim the objects that
- * were pointed to by these data fields (either before or after the call).
- *
- */
-
-/* TLS Map implementation
- */
-
-#define TLSMAP_START (TLS_SLOT_MAX_WELL_KNOWN+1)
-#define TLSMAP_SIZE BIONIC_TLS_SLOTS
-#define TLSMAP_BITS 32
-#define TLSMAP_WORDS ((TLSMAP_SIZE+TLSMAP_BITS-1)/TLSMAP_BITS)
-#define TLSMAP_WORD(m,k) (m)->map[(k)/TLSMAP_BITS]
-#define TLSMAP_MASK(k) (1U << ((k)&(TLSMAP_BITS-1)))
-
-/* this macro is used to quickly check that a key belongs to a reasonable range */
-#define TLSMAP_VALIDATE_KEY(key) \
- ((key) >= TLSMAP_START && (key) < TLSMAP_SIZE)
-
-/* the type of tls key destructor functions */
-typedef void (*tls_dtor_t)(void*);
-
-typedef struct {
- int init; /* see comment in tlsmap_lock() */
- uint32_t map[TLSMAP_WORDS]; /* bitmap of allocated keys */
- tls_dtor_t dtors[TLSMAP_SIZE]; /* key destructors */
-} tlsmap_t;
-
-static pthread_mutex_t _tlsmap_lock = PTHREAD_MUTEX_INITIALIZER;
-static tlsmap_t _tlsmap;
-
-/* lock the global TLS map lock and return a handle to it */
-static __inline__ tlsmap_t* tlsmap_lock(void)
-{
- tlsmap_t* m = &_tlsmap;
-
- pthread_mutex_lock(&_tlsmap_lock);
- /* we need to initialize the first entry of the 'map' array
- * with the value TLS_DEFAULT_ALLOC_MAP. doing it statically
- * when declaring _tlsmap is a bit awkward and is going to
- * produce warnings, so do it the first time we use the map
- * instead
- */
- if (__unlikely(!m->init)) {
- TLSMAP_WORD(m,0) = TLS_DEFAULT_ALLOC_MAP;
- m->init = 1;
- }
- return m;
-}
-
-/* unlock the global TLS map */
-static __inline__ void tlsmap_unlock(tlsmap_t* m)
-{
- pthread_mutex_unlock(&_tlsmap_lock);
- (void)m; /* a good compiler is a happy compiler */
-}
-
-/* test to see wether a key is allocated */
-static __inline__ int tlsmap_test(tlsmap_t* m, int key)
-{
- return (TLSMAP_WORD(m,key) & TLSMAP_MASK(key)) != 0;
-}
-
-/* set the destructor and bit flag on a newly allocated key */
-static __inline__ void tlsmap_set(tlsmap_t* m, int key, tls_dtor_t dtor)
-{
- TLSMAP_WORD(m,key) |= TLSMAP_MASK(key);
- m->dtors[key] = dtor;
-}
-
-/* clear the destructor and bit flag on an existing key */
-static __inline__ void tlsmap_clear(tlsmap_t* m, int key)
-{
- TLSMAP_WORD(m,key) &= ~TLSMAP_MASK(key);
- m->dtors[key] = NULL;
-}
-
-/* allocate a new TLS key, return -1 if no room left */
-static int tlsmap_alloc(tlsmap_t* m, tls_dtor_t dtor)
-{
- int key;
-
- for ( key = TLSMAP_START; key < TLSMAP_SIZE; key++ ) {
- if ( !tlsmap_test(m, key) ) {
- tlsmap_set(m, key, dtor);
- return key;
- }
- }
- return -1;
-}
-
-
-int pthread_key_create(pthread_key_t *key, void (*destructor_function)(void *))
-{
- uint32_t err = ENOMEM;
- tlsmap_t* map = tlsmap_lock();
- int k = tlsmap_alloc(map, destructor_function);
-
- if (k >= 0) {
- *key = k;
- err = 0;
- }
- tlsmap_unlock(map);
- return err;
-}
-
-
-/* This deletes a pthread_key_t. note that the standard mandates that this does
- * not call the destructor of non-NULL key values. Instead, it is the
- * responsibility of the caller to properly dispose of the corresponding data
- * and resources, using any means it finds suitable.
- *
- * On the other hand, this function will clear the corresponding key data
- * values in all known threads. this prevents later (invalid) calls to
- * pthread_getspecific() to receive invalid/stale values.
- */
-int pthread_key_delete(pthread_key_t key)
-{
- uint32_t err;
- pthread_internal_t* thr;
- tlsmap_t* map;
-
- if (!TLSMAP_VALIDATE_KEY(key)) {
- return EINVAL;
- }
-
- map = tlsmap_lock();
-
- if (!tlsmap_test(map, key)) {
- err = EINVAL;
- goto err1;
- }
-
- /* clear value in all threads */
- pthread_mutex_lock(&gThreadListLock);
- for ( thr = gThreadList; thr != NULL; thr = thr->next ) {
- /* avoid zombie threads with a negative 'join_count'. these are really
- * already dead and don't have a TLS area anymore.
- *
- * similarly, it is possible to have thr->tls == NULL for threads that
- * were just recently created through pthread_create() but whose
- * startup trampoline (__thread_entry) hasn't been run yet by the
- * scheduler. thr->tls will also be NULL after it's stack has been
- * unmapped but before the ongoing pthread_join() is finished.
- * so check for this too.
- */
- if (thr->join_count < 0 || !thr->tls)
- continue;
-
- thr->tls[key] = NULL;
- }
- tlsmap_clear(map, key);
-
- pthread_mutex_unlock(&gThreadListLock);
- err = 0;
-
-err1:
- tlsmap_unlock(map);
- return err;
-}
-
-
-int pthread_setspecific(pthread_key_t key, const void *ptr)
-{
- int err = EINVAL;
- tlsmap_t* map;
-
- if (TLSMAP_VALIDATE_KEY(key)) {
- /* check that we're trying to set data for an allocated key */
- map = tlsmap_lock();
- if (tlsmap_test(map, key)) {
- ((uint32_t *)__get_tls())[key] = (uint32_t)ptr;
- err = 0;
- }
- tlsmap_unlock(map);
- }
- return err;
-}
-
-void * pthread_getspecific(pthread_key_t key)
-{
- if (!TLSMAP_VALIDATE_KEY(key)) {
- return NULL;
- }
-
- /* for performance reason, we do not lock/unlock the global TLS map
- * to check that the key is properly allocated. if the key was not
- * allocated, the value read from the TLS should always be NULL
- * due to pthread_key_delete() clearing the values for all threads.
- */
- return (void *)(((unsigned *)__get_tls())[key]);
-}
-
-/* Posix mandates that this be defined in <limits.h> but we don't have
- * it just yet.
- */
-#ifndef PTHREAD_DESTRUCTOR_ITERATIONS
-# define PTHREAD_DESTRUCTOR_ITERATIONS 4
-#endif
-
-/* this function is called from pthread_exit() to remove all TLS key data
- * from this thread's TLS area. this must call the destructor of all keys
- * that have a non-NULL data value (and a non-NULL destructor).
- *
- * because destructors can do funky things like deleting/creating other
- * keys, we need to implement this in a loop
- */
-static void pthread_key_clean_all(void)
-{
- tlsmap_t* map;
- void** tls = (void**)__get_tls();
- int rounds = PTHREAD_DESTRUCTOR_ITERATIONS;
-
- map = tlsmap_lock();
-
- for (rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; rounds--)
- {
- int kk, count = 0;
-
- for (kk = TLSMAP_START; kk < TLSMAP_SIZE; kk++) {
- if ( tlsmap_test(map, kk) )
- {
- void* data = tls[kk];
- tls_dtor_t dtor = map->dtors[kk];
-
- if (data != NULL && dtor != NULL)
- {
- /* we need to clear the key data now, this will prevent the
- * destructor (or a later one) from seeing the old value if
- * it calls pthread_getspecific() for some odd reason
- *
- * we do not do this if 'dtor == NULL' just in case another
- * destructor function might be responsible for manually
- * releasing the corresponding data.
- */
- tls[kk] = NULL;
-
- /* because the destructor is free to call pthread_key_create
- * and/or pthread_key_delete, we need to temporarily unlock
- * the TLS map
- */
- tlsmap_unlock(map);
- (*dtor)(data);
- map = tlsmap_lock();
-
- count += 1;
- }
- }
- }
-
- /* if we didn't call any destructor, there is no need to check the
- * TLS data again
- */
- if (count == 0)
- break;
- }
- tlsmap_unlock(map);
-}
-
// man says this should be in <linux/unistd.h>, but it isn't
extern int tgkill(int tgid, int tid, int sig);
diff --git a/libc/bionic/pthread_internal.h b/libc/bionic/pthread_internal.h
index bc68291..24b420c 100644
--- a/libc/bionic/pthread_internal.h
+++ b/libc/bionic/pthread_internal.h
@@ -60,6 +60,11 @@ int _init_thread(pthread_internal_t* thread, pid_t kernel_id, const pthread_attr
void _pthread_internal_add( pthread_internal_t* thread );
pthread_internal_t* __get_thread(void);
+__LIBC_HIDDEN__ void pthread_key_clean_all(void);
+
+extern pthread_internal_t* gThreadList;
+extern pthread_mutex_t gThreadListLock;
+
/* needed by posix-timers.c */
static __inline__ void timespec_add( struct timespec* a, const struct timespec* b )
diff --git a/libc/bionic/pthread_key.cpp b/libc/bionic/pthread_key.cpp
new file mode 100644
index 0000000..00dacba
--- /dev/null
+++ b/libc/bionic/pthread_key.cpp
@@ -0,0 +1,280 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <malloc.h>
+#include <memory.h>
+#include <pthread.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/atomics.h>
+#include <sys/mman.h>
+#include <sys/prctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "bionic_atomic_inline.h"
+#include "bionic_futex.h"
+#include "bionic_pthread.h"
+#include "bionic_ssp.h"
+#include "bionic_tls.h"
+#include "debug_format.h"
+#include "pthread_internal.h"
+#include "thread_private.h"
+
+/* A technical note regarding our thread-local-storage (TLS) implementation:
+ *
+ * There can be up to BIONIC_TLS_SLOTS independent TLS keys in a given process,
+ * The keys below TLS_SLOT_FIRST_USER_SLOT are reserved for Bionic to hold
+ * special thread-specific variables like errno or a pointer to
+ * the current thread's descriptor. These entries cannot be accessed through
+ * pthread_getspecific() / pthread_setspecific() or pthread_key_delete()
+ *
+ * The 'tls_map_t' type defined below implements a shared global map of
+ * currently created/allocated TLS keys and the destructors associated
+ * with them.
+ *
+ * The global TLS map simply contains a bitmap of allocated keys, and
+ * an array of destructors.
+ *
+ * Each thread has a TLS area that is a simple array of BIONIC_TLS_SLOTS void*
+ * pointers. the TLS area of the main thread is stack-allocated in
+ * __libc_init_common, while the TLS area of other threads is placed at
+ * the top of their stack in pthread_create.
+ *
+ * When pthread_key_delete() is called it will erase the key's bitmap bit
+ * and its destructor, and will also clear the key data in the TLS area of
+ * all created threads. As mandated by Posix, it is the responsibility of
+ * the caller of pthread_key_delete() to properly reclaim the objects that
+ * were pointed to by these data fields (either before or after the call).
+ */
+
+#define TLSMAP_BITS 32
+#define TLSMAP_WORDS ((BIONIC_TLS_SLOTS+TLSMAP_BITS-1)/TLSMAP_BITS)
+#define TLSMAP_WORD(m,k) (m).map[(k)/TLSMAP_BITS]
+#define TLSMAP_MASK(k) (1U << ((k)&(TLSMAP_BITS-1)))
+
+static inline bool IsValidUserKey(pthread_key_t key) {
+ return (key >= TLS_SLOT_FIRST_USER_SLOT && key < BIONIC_TLS_SLOTS);
+}
+
+typedef void (*key_destructor_t)(void*);
+
+struct tls_map_t {
+ bool is_initialized;
+
+ /* bitmap of allocated keys */
+ uint32_t map[TLSMAP_WORDS];
+
+ key_destructor_t key_destructors[BIONIC_TLS_SLOTS];
+};
+
+class ScopedTlsMapAccess {
+ public:
+ ScopedTlsMapAccess() {
+ Lock();
+
+ // If this is the first time the TLS map has been accessed,
+ // mark the slots belonging to well-known keys as being in use.
+ // This isn't currently necessary because the well-known keys
+ // can only be accessed directly by bionic itself, do not have
+ // destructors, and all the functions that touch the TLS map
+ // start after the maximum well-known slot.
+ if (!s_tls_map_.is_initialized) {
+ for (pthread_key_t key = 0; key < TLS_SLOT_FIRST_USER_SLOT; ++key) {
+ SetInUse(key, NULL);
+ }
+ s_tls_map_.is_initialized = true;
+ }
+ }
+
+ ~ScopedTlsMapAccess() {
+ Unlock();
+ }
+
+ int CreateKey(pthread_key_t* result, void (*key_destructor)(void*)) {
+ // Take the first unallocated key.
+ for (int key = 0; key < BIONIC_TLS_SLOTS; ++key) {
+ if (!IsInUse(key)) {
+ SetInUse(key, key_destructor);
+ *result = key;
+ return 0;
+ }
+ }
+
+ // We hit PTHREAD_KEYS_MAX. POSIX says EAGAIN for this case.
+ return EAGAIN;
+ }
+
+ void DeleteKey(pthread_key_t key) {
+ TLSMAP_WORD(s_tls_map_, key) &= ~TLSMAP_MASK(key);
+ s_tls_map_.key_destructors[key] = NULL;
+ }
+
+ bool IsInUse(pthread_key_t key) {
+ return (TLSMAP_WORD(s_tls_map_, key) & TLSMAP_MASK(key)) != 0;
+ }
+
+ void SetInUse(pthread_key_t key, void (*key_destructor)(void*)) {
+ TLSMAP_WORD(s_tls_map_, key) |= TLSMAP_MASK(key);
+ s_tls_map_.key_destructors[key] = key_destructor;
+ }
+
+ // Called from pthread_exit() to remove all TLS key data
+ // from this thread's TLS area. This must call the destructor of all keys
+ // that have a non-NULL data value and a non-NULL destructor.
+ void CleanAll() {
+ void** tls = (void**)__get_tls();
+
+ // Because destructors can do funky things like deleting/creating other
+ // keys, we need to implement this in a loop.
+ for (int rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; --rounds) {
+ size_t called_destructor_count = 0;
+ for (int key = 0; key < BIONIC_TLS_SLOTS; ++key) {
+ if (IsInUse(key)) {
+ void* data = tls[key];
+ void (*key_destructor)(void*) = s_tls_map_.key_destructors[key];
+
+ if (data != NULL && key_destructor != NULL) {
+ // we need to clear the key data now, this will prevent the
+ // destructor (or a later one) from seeing the old value if
+ // it calls pthread_getspecific() for some odd reason
+
+ // we do not do this if 'key_destructor == NULL' just in case another
+ // destructor function might be responsible for manually
+ // releasing the corresponding data.
+ tls[key] = NULL;
+
+ // because the destructor is free to call pthread_key_create
+ // and/or pthread_key_delete, we need to temporarily unlock
+ // the TLS map
+ Unlock();
+ (*key_destructor)(data);
+ Lock();
+ ++called_destructor_count;
+ }
+ }
+ }
+
+ // If we didn't call any destructors, there is no need to check the TLS data again.
+ if (called_destructor_count == 0) {
+ break;
+ }
+ }
+ }
+
+ private:
+ static tls_map_t s_tls_map_;
+ static pthread_mutex_t s_tls_map_lock_;
+
+ void Lock() {
+ pthread_mutex_lock(&s_tls_map_lock_);
+ }
+
+ void Unlock() {
+ pthread_mutex_unlock(&s_tls_map_lock_);
+ }
+};
+
+tls_map_t ScopedTlsMapAccess::s_tls_map_;
+pthread_mutex_t ScopedTlsMapAccess::s_tls_map_lock_;
+
+__LIBC_HIDDEN__ void pthread_key_clean_all() {
+ ScopedTlsMapAccess tls_map;
+ tls_map.CleanAll();
+}
+
+int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) {
+ ScopedTlsMapAccess tls_map;
+ return tls_map.CreateKey(key, key_destructor);
+}
+
+// Deletes a pthread_key_t. note that the standard mandates that this does
+// not call the destructors for non-NULL key values. Instead, it is the
+// responsibility of the caller to properly dispose of the corresponding data
+// and resources, using any means it finds suitable.
+int pthread_key_delete(pthread_key_t key) {
+ ScopedTlsMapAccess tls_map;
+
+ if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
+ return EINVAL;
+ }
+
+ // Clear value in all threads.
+ pthread_mutex_lock(&gThreadListLock);
+ for (pthread_internal_t* t = gThreadList; t != NULL; t = t->next) {
+ // Avoid zombie threads with a negative 'join_count'. These are really
+ // already dead and don't have a TLS area anymore.
+
+ // Similarly, it is possible to have t->tls == NULL for threads that
+ // were just recently created through pthread_create() but whose
+ // startup trampoline (__thread_entry) hasn't been run yet by the
+ // scheduler. t->tls will also be NULL after it's stack has been
+ // unmapped but before the ongoing pthread_join() is finished.
+ // so check for this too.
+ if (t->join_count < 0 || !t->tls) {
+ continue;
+ }
+
+ t->tls[key] = NULL;
+ }
+ tls_map.DeleteKey(key);
+
+ pthread_mutex_unlock(&gThreadListLock);
+ return 0;
+}
+
+void* pthread_getspecific(pthread_key_t key) {
+ if (!IsValidUserKey(key)) {
+ return NULL;
+ }
+
+ // For performance reasons, we do not lock/unlock the global TLS map
+ // to check that the key is properly allocated. If the key was not
+ // allocated, the value read from the TLS should always be NULL
+ // due to pthread_key_delete() clearing the values for all threads.
+ return (void *)(((unsigned *)__get_tls())[key]);
+}
+
+int pthread_setspecific(pthread_key_t key, const void* ptr) {
+ ScopedTlsMapAccess tls_map;
+
+ if (!IsValidUserKey(key) || !tls_map.IsInUse(key)) {
+ return EINVAL;
+ }
+
+ ((uint32_t *)__get_tls())[key] = (uint32_t)ptr;
+ return 0;
+}
diff --git a/libc/bionic/sysconf.cpp b/libc/bionic/sysconf.cpp
index 6555a66..f4845e1 100644
--- a/libc/bionic/sysconf.cpp
+++ b/libc/bionic/sysconf.cpp
@@ -55,7 +55,6 @@
/* the following depends on our implementation */
#define SYSTEM_ATEXIT_MAX 65536 /* our implementation is unlimited */
-#define SYSTEM_THREAD_KEYS_MAX BIONIC_TLS_SLOTS
#define SYSTEM_THREAD_STACK_MIN 32768 /* lower values may be possible, but be conservative */
#define SYSTEM_THREAD_THREADS_MAX 2048 /* really unlimited */
@@ -302,10 +301,13 @@ int sysconf(int name) {
// GETPW_R_SIZE_MAX ?
case _SC_LOGIN_NAME_MAX: return SYSTEM_LOGIN_NAME_MAX;
-#ifdef _POSIX_THREAD_DESTRUCTOR_ITERATIONS
- case _SC_THREAD_DESTRUCTOR_ITERATIONS: return _POSIX_THREAD_DESTRUCTOR_ITERATIONS;
-#endif
- case _SC_THREAD_KEYS_MAX: return SYSTEM_THREAD_KEYS_MAX;
+
+ case _SC_THREAD_DESTRUCTOR_ITERATIONS:
+ return _POSIX_THREAD_DESTRUCTOR_ITERATIONS;
+
+ case _SC_THREAD_KEYS_MAX:
+ return (BIONIC_TLS_SLOTS - TLS_SLOT_FIRST_USER_SLOT);
+
case _SC_THREAD_STACK_MIN: return SYSTEM_THREAD_STACK_MIN;
case _SC_THREAD_THREADS_MAX: return SYSTEM_THREAD_THREADS_MAX;
case _SC_TTY_NAME_MAX: return SYSTEM_TTY_NAME_MAX;