Better control of pthread keys used in bionic.
Change-Id: I1e1bc77c0e7879baead6c3417282ce549a1153b5
diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp
index 90aa7b8..e5a170f 100644
--- a/libc/bionic/locale.cpp
+++ b/libc/bionic/locale.cpp
@@ -36,6 +36,7 @@
#include <wchar.h>
#include "private/bionic_macros.h"
+#include "private/ThreadLocalBuffer.h"
// We currently support a single locale, the "C" locale (also known as "POSIX").
@@ -62,10 +63,7 @@
static lconv g_locale;
// We don't use pthread_once for this so that we know when the resource (a TLS slot) will be taken.
-static pthread_key_t g_uselocale_key;
-__attribute__((constructor)) static void __bionic_tls_uselocale_key_init() {
- pthread_key_create(&g_uselocale_key, NULL);
-}
+BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR(g_uselocale_key, NULL);
static void __locale_init() {
g_locale.decimal_point = const_cast<char*>(".");
diff --git a/libc/dns/resolv/res_state.c b/libc/dns/resolv/res_state.c
index 7533d19..459f073 100644
--- a/libc/dns/resolv/res_state.c
+++ b/libc/dns/resolv/res_state.c
@@ -39,6 +39,8 @@
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
#include <sys/_system_properties.h>
+#include "private/ThreadLocalBuffer.h"
+
/* Set to 1 to enable debug traces */
#define DEBUG 0
@@ -50,8 +52,6 @@
# define D(...) do{}while(0)
#endif
-static pthread_key_t _res_key;
-
typedef struct {
int _h_errno;
// TODO: Have one __res_state per network so we don't have to repopulate frequently.
@@ -105,12 +105,7 @@
free(rt);
}
-__attribute__((constructor))
-static void
-_res_init_key( void )
-{
- pthread_key_create( &_res_key, _res_thread_free );
-}
+BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR(_res_key, _res_thread_free);
static _res_thread*
_res_thread_get(void)
diff --git a/libc/private/ThreadLocalBuffer.h b/libc/private/ThreadLocalBuffer.h
index e5bd28c..cc47317 100644
--- a/libc/private/ThreadLocalBuffer.h
+++ b/libc/private/ThreadLocalBuffer.h
@@ -38,15 +38,17 @@
// We used to use pthread_once to initialize the keys, but life is more predictable
// if we allocate them all up front when the C library starts up, via __constructor__.
+#define BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR(key_name, key_destructor) \
+ static pthread_key_t key_name; \
+ __attribute__((constructor)) static void __bionic_tls_ ## key_name ## _key_init() { \
+ pthread_key_create(&key_name, key_destructor); \
+ }
#define GLOBAL_INIT_THREAD_LOCAL_BUFFER(name) \
- static pthread_key_t __bionic_tls_ ## name ## _key; \
static void __bionic_tls_ ## name ## _key_destroy(void* buffer) { \
free(buffer); \
} \
- __attribute__((constructor)) static void __bionic_tls_ ## name ## _key_init() { \
- pthread_key_create(&__bionic_tls_ ## name ## _key, __bionic_tls_ ## name ## _key_destroy); \
- }
+ BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR(__bionic_tls_ ## name ## _key, __bionic_tls_ ## name ## _key_destroy)
// Leaves "name_tls_buffer" and "name_tls_buffer_size" defined and initialized.
#define LOCAL_INIT_THREAD_LOCAL_BUFFER(type, name, byte_count) \
diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h
index 04f5fd2..724f896 100644
--- a/libc/private/bionic_tls.h
+++ b/libc/private/bionic_tls.h
@@ -78,7 +78,7 @@
* Following are current pthread keys used internally by libc:
* basename libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
* dirname libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
- * uselocale libc
+ * uselocale libc (BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR)
* getmntent_mntent libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
* getmntent_strings libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
* ptsname libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
@@ -87,7 +87,7 @@
* strsignal libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
* passwd libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
* group libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER)
- * _res_key libc
+ * _res_key libc (BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR)
*/
#define LIBC_PTHREAD_KEY_RESERVED_COUNT 12