diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp index 4fade848d..5ab834dcc 100644 --- a/libc/bionic/locale.cpp +++ b/libc/bionic/locale.cpp @@ -39,8 +39,11 @@ struct __locale_t { static pthread_once_t gLocaleOnce = PTHREAD_ONCE_INIT; static lconv gLocale; -static pthread_once_t gUselocaleKeyOnce = PTHREAD_ONCE_INIT; +// 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 gUselocaleKey; +__attribute__((constructor)) static void __bionic_tls_uselocale_key_init() { + pthread_key_create(&gUselocaleKey, NULL); +} static void __locale_init() { gLocale.decimal_point = const_cast("."); @@ -72,10 +75,6 @@ static void __locale_init() { gLocale.int_n_sign_posn = CHAR_MAX; } -static void __uselocale_key_init() { - pthread_key_create(&gUselocaleKey, NULL); -} - static bool __is_supported_locale(const char* locale) { return (strcmp(locale, "") == 0 || strcmp(locale, "C") == 0 || strcmp(locale, "POSIX") == 0); } @@ -139,8 +138,6 @@ char* setlocale(int category, char const* locale_name) { } locale_t uselocale(locale_t new_locale) { - pthread_once(&gUselocaleKeyOnce, __uselocale_key_init); - locale_t old_locale = static_cast(pthread_getspecific(gUselocaleKey)); // 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 a42a8abed..d0a02011e 100644 --- a/libc/private/bionic_tls.h +++ b/libc/private/bionic_tls.h @@ -80,7 +80,7 @@ enum { * pthread_key_create; grep for GLOBAL_INIT_THREAD_LOCAL_BUFFER to find those. We need to manually * maintain that second number, but pthread_test will fail if we forget. */ -#define GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT 4 +#define GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT 5 #define BIONIC_ALIGN(x, a) (((x) + (a - 1)) & ~(a - 1))