Make uselocale(3) claim its pthread key in an ELF constructor.
pthread_once is nice for decoupling, but it makes resource availability less
predictable, which is a bad thing.
This fixes a test failure if uselocale(3) is called before
pthread.pthread_key_create_lots runs.
Change-Id: Ie2634f986a50e7965582d4bd6e5aaf48cf0d55c8
diff --git a/libc/bionic/locale.cpp b/libc/bionic/locale.cpp
index 4fade84..5ab834d 100644
--- a/libc/bionic/locale.cpp
+++ b/libc/bionic/locale.cpp
@@ -39,8 +39,11 @@
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<char*>(".");
@@ -72,10 +75,6 @@
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 @@
}
locale_t uselocale(locale_t new_locale) {
- pthread_once(&gUselocaleKeyOnce, __uselocale_key_init);
-
locale_t old_locale = static_cast<locale_t>(pthread_getspecific(gUselocaleKey));
// If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.