#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").
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<char*>(".");
g_locale.int_n_sign_posn = CHAR_MAX;
}
-size_t __ctype_get_mb_cur_max() {
- locale_t l = reinterpret_cast<locale_t>(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 ||
return const_cast<char*>(__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<locale_t>(pthread_getspecific(g_uselocale_key));
// If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
/*
* 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)