From 4a2891d8c8f09a64ea9e1479518b0cc969bd5969 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Wed, 4 Mar 2015 16:53:23 -0800 Subject: [PATCH] Better control of pthread keys used in bionic. Change-Id: I1e1bc77c0e7879baead6c3417282ce549a1153b5 --- libc/bionic/locale.cpp | 6 ++---- libc/dns/resolv/res_state.c | 11 +++-------- libc/private/ThreadLocalBuffer.h | 10 ++++++---- libc/private/bionic_tls.h | 4 ++-- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp index 90aa7b8aa..e5a170ff7 100644 --- a/libc/bionic/locale.cpp +++ b/libc/bionic/locale.cpp @@ -36,6 +36,7 @@ #include #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 pthread_once_t g_locale_once = PTHREAD_ONCE_INIT; 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("."); diff --git a/libc/dns/resolv/res_state.c b/libc/dns/resolv/res_state.c index 7533d1957..459f07394 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 +#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 @@ _res_thread_free( void* _rt ) 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 e5bd28c37..cc4731703 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 04f5fd256..724f8965d 100644 --- a/libc/private/bionic_tls.h +++ b/libc/private/bionic_tls.h @@ -78,7 +78,7 @@ enum { * 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 @@ enum { * 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