From f7e3b3e48ab3ffbf7bbce5102ce1739c200093cb Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Thu, 5 Mar 2015 20:08:21 -0800 Subject: [PATCH] Use pthread_once for g_uselocale_key creation. Bug: 19625804 Change-Id: I57ec4c965067dc0c157c795c1f7217a3ca403286 --- libc/bionic/locale.cpp | 31 ++++++++++++++++++------------- libc/private/bionic_tls.h | 4 ++-- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp index e5a170ff7..b42b440c6 100644 --- a/libc/bionic/locale.cpp +++ b/libc/bionic/locale.cpp @@ -36,7 +36,6 @@ #include #include "private/bionic_macros.h" -#include "private/ThreadLocalBuffer.h" // We currently support a single locale, the "C" locale (also known as "POSIX"). @@ -59,12 +58,18 @@ struct __locale_t { DISALLOW_COPY_AND_ASSIGN(__locale_t); }; +size_t __ctype_get_mb_cur_max() { + locale_t l = uselocale(NULL); + if (l == LC_GLOBAL_LOCALE) { + return __bionic_current_locale_is_utf8 ? 4 : 1; + } else { + return l->mb_cur_max; + } +} + 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. -BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR(g_uselocale_key, NULL); - static void __locale_init() { g_locale.decimal_point = const_cast("."); @@ -95,15 +100,6 @@ static void __locale_init() { g_locale.int_n_sign_posn = CHAR_MAX; } -size_t __ctype_get_mb_cur_max() { - locale_t l = reinterpret_cast(pthread_getspecific(g_uselocale_key)); - if (l == nullptr || l == LC_GLOBAL_LOCALE) { - return __bionic_current_locale_is_utf8 ? 4 : 1; - } else { - return l->mb_cur_max; - } -} - static bool __is_supported_locale(const char* locale) { return (strcmp(locale, "") == 0 || strcmp(locale, "C") == 0 || @@ -160,7 +156,16 @@ char* setlocale(int category, const char* locale_name) { return const_cast(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C"); } +// We can't use a constructor to create g_uselocal_key, because it may be used in constructors. +static pthread_once_t g_uselocale_once = PTHREAD_ONCE_INIT; +static pthread_key_t g_uselocale_key; + +static void g_uselocale_key_init() { + pthread_key_create(&g_uselocale_key, NULL); +} + locale_t uselocale(locale_t new_locale) { + pthread_once(&g_uselocale_once, g_uselocale_key_init); locale_t old_locale = static_cast(pthread_getspecific(g_uselocale_key)); // If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE. diff --git a/libc/private/bionic_tls.h b/libc/private/bionic_tls.h index 724f8965d..1ab8d4a26 100644 --- a/libc/private/bionic_tls.h +++ b/libc/private/bionic_tls.h @@ -72,13 +72,13 @@ enum { /* * Bionic uses some pthread keys internally. All pthread keys used internally - * should be created in constructors. + * should be created in constructors, except for keys that may be used in or before constructors. * We need to manually maintain the count of pthread keys used internally, but * pthread_test should fail if we forget. * 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 (BIONIC_PTHREAD_KEY_WITH_CONSTRUCTOR) + * uselocale libc (can be used in constructors) * getmntent_mntent libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER) * getmntent_strings libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER) * ptsname libc (GLOBAL_INIT_THREAD_LOCAL_BUFFER) -- 2.11.0