Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Add a non-TLS slot implementation of locale.cpp.
Browse files Browse the repository at this point in the history
libandroid_support shouldn't be using Bionic's TLS slot. Add a
thread_local based implementation. We can't use the thread_local
implementation in bionic because the linker needs this and pthread
thread-locals (which is how thread_local is implemented) doesn't work
that early.

Test: make checkbuild
Test: adb shell /data/nativetest/bionic-unit-tests/bionic-unit-tests
Bug: android/ndk#300
Change-Id: I7dcbf554ade2264d541d722fa3f86df04926e67a
  • Loading branch information
DanAlbert committed Jul 12, 2017
1 parent e2fd010 commit 1a8b7f2
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions libc/bionic/locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@

#include "private/bionic_macros.h"

#if defined(__BIONIC_BUILD_FOR_ANDROID_SUPPORT)
#define USE_TLS_SLOT 0
#else
#define USE_TLS_SLOT 1
#endif

#if USE_TLS_SLOT
#include "bionic/pthread_internal.h"
#endif

// We only support two locales, the "C" locale (also known as "POSIX"),
// and the "C.UTF-8" locale (also known as "en_US.UTF-8").
Expand Down Expand Up @@ -70,6 +78,10 @@ size_t __ctype_get_mb_cur_max() {
}
}

#if !USE_TLS_SLOT
static thread_local locale_t g_current_locale;
#endif

static pthread_once_t g_locale_once = PTHREAD_ONCE_INIT;
static lconv g_locale;

Expand Down Expand Up @@ -163,17 +175,24 @@ char* setlocale(int category, const char* locale_name) {
return const_cast<char*>(__bionic_current_locale_is_utf8 ? "C.UTF-8" : "C");
}

static locale_t* get_current_locale_ptr() {
#if USE_TLS_SLOT
return &__get_bionic_tls().locale;
#else
return &g_current_locale;
#endif
}

locale_t uselocale(locale_t new_locale) {
locale_t* locale_storage = &__get_bionic_tls().locale;
locale_t old_locale = *locale_storage;
locale_t old_locale = *get_current_locale_ptr();

// If this is the first call to uselocale(3) on this thread, we return LC_GLOBAL_LOCALE.
if (old_locale == NULL) {
old_locale = LC_GLOBAL_LOCALE;
}

if (new_locale != NULL) {
*locale_storage = new_locale;
*get_current_locale_ptr() = new_locale;
}

return old_locale;
Expand Down

0 comments on commit 1a8b7f2

Please sign in to comment.