| // Copyright 2026 The Fuchsia Authors. All rights reserved. | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #define _ALL_SOURCE // For thrd_create_with_name. | |
| #include <lib/fit/defer.h> | |
| #include <locale.h> | |
| #include <pthread.h> | |
| #include <stdlib.h> | |
| #include <threads.h> | |
| #include "../test/unittest.h" | |
| namespace LIBC_NAMESPACE_DECL { | |
| namespace { | |
| struct ThreadLocaleCheck { | |
| locale_t custom_locale = nullptr; | |
| size_t initial_mb_cur_max = 0; | |
| locale_t initial_locale = nullptr; | |
| locale_t prev_after_set_custom = nullptr; | |
| size_t custom_mb_cur_max = 0; | |
| locale_t prev_after_restore_global = nullptr; | |
| size_t restored_mb_cur_max = 0; | |
| }; | |
| void CheckNewThreadLocale(ThreadLocaleCheck& check) { | |
| // Query MB_CUR_MAX (__ctype_get_mb_cur_max) before calling uselocale() so an | |
| // uninitialized thread->locale faults immediately rather than being masked by | |
| // a prior uselocale() call. | |
| check.initial_mb_cur_max = MB_CUR_MAX; | |
| check.initial_locale = uselocale(static_cast<locale_t>(0)); | |
| if (check.custom_locale != nullptr) { | |
| check.prev_after_set_custom = uselocale(check.custom_locale); | |
| check.custom_mb_cur_max = MB_CUR_MAX; | |
| check.prev_after_restore_global = uselocale(LC_GLOBAL_LOCALE); | |
| check.restored_mb_cur_max = MB_CUR_MAX; | |
| } | |
| } | |
| void* PthreadLocaleEntry(void* arg) { | |
| CheckNewThreadLocale(*static_cast<ThreadLocaleCheck*>(arg)); | |
| return nullptr; | |
| } | |
| int ThrdLocaleEntry(void* arg) { | |
| CheckNewThreadLocale(*static_cast<ThreadLocaleCheck*>(arg)); | |
| return 0; | |
| } | |
| void RunPthreadCheck(ThreadLocaleCheck& check) { | |
| pthread_t thread; | |
| ASSERT_EQ(pthread_create(&thread, nullptr, PthreadLocaleEntry, &check), 0); | |
| ASSERT_EQ(pthread_join(thread, nullptr), 0); | |
| } | |
| void RunThrdCheck(ThreadLocaleCheck& check) { | |
| thrd_t thread; | |
| ASSERT_EQ(thrd_create(&thread, ThrdLocaleEntry, &check), thrd_success); | |
| ASSERT_EQ(thrd_join(thread, nullptr), thrd_success); | |
| } | |
| void RunThrdWithNameCheck(ThreadLocaleCheck& check) { | |
| thrd_t thread; | |
| ASSERT_EQ(thrd_create_with_name(&thread, ThrdLocaleEntry, &check, "locale-init-test"), | |
| thrd_success); | |
| ASSERT_EQ(thrd_join(thread, nullptr), thrd_success); | |
| } | |
| void VerifyInitialGlobalLocale(auto&& run_thread) { | |
| ASSERT_EQ(uselocale(static_cast<locale_t>(0)), LC_GLOBAL_LOCALE); | |
| const size_t global_mb_cur_max = MB_CUR_MAX; | |
| ASSERT_GE(global_mb_cur_max, 1u); | |
| ThreadLocaleCheck check; | |
| ASSERT_NO_FATAL_FAILURE(run_thread(check)); | |
| EXPECT_EQ(check.initial_locale, LC_GLOBAL_LOCALE); | |
| EXPECT_EQ(check.initial_mb_cur_max, global_mb_cur_max); | |
| } | |
| void VerifyDoesNotInheritThreadLocale(auto&& run_thread) { | |
| ASSERT_EQ(uselocale(static_cast<locale_t>(0)), LC_GLOBAL_LOCALE); | |
| const size_t global_mb_cur_max = MB_CUR_MAX; | |
| const char* custom_name = global_mb_cur_max == 1 ? "C.UTF-8" : "C"; | |
| const size_t expected_custom_mb_cur_max = global_mb_cur_max == 1 ? 4 : 1; | |
| locale_t custom_locale = newlocale(LC_CTYPE_MASK, custom_name, static_cast<locale_t>(0)); | |
| ASSERT_NE(custom_locale, static_cast<locale_t>(0)); | |
| locale_t prev_locale = uselocale(custom_locale); | |
| ASSERT_EQ(prev_locale, LC_GLOBAL_LOCALE); | |
| auto restore_parent_locale = fit::defer([prev_locale, custom_locale]() { | |
| uselocale(prev_locale); | |
| freelocale(custom_locale); | |
| }); | |
| ASSERT_EQ(uselocale(static_cast<locale_t>(0)), custom_locale); | |
| ASSERT_EQ(MB_CUR_MAX, expected_custom_mb_cur_max); | |
| ThreadLocaleCheck check{.custom_locale = custom_locale}; | |
| ASSERT_NO_FATAL_FAILURE(run_thread(check)); | |
| // New threads must start on LC_GLOBAL_LOCALE, not inheriting the parent's | |
| // thread-local locale. | |
| EXPECT_EQ(check.initial_locale, LC_GLOBAL_LOCALE); | |
| EXPECT_EQ(check.initial_mb_cur_max, global_mb_cur_max); | |
| // Switching to and from a thread-local locale on the child thread must also | |
| // report LC_GLOBAL_LOCALE as the prior locale and update MB_CUR_MAX. | |
| EXPECT_EQ(check.prev_after_set_custom, LC_GLOBAL_LOCALE); | |
| EXPECT_EQ(check.custom_mb_cur_max, expected_custom_mb_cur_max); | |
| EXPECT_EQ(check.prev_after_restore_global, custom_locale); | |
| EXPECT_EQ(check.restored_mb_cur_max, global_mb_cur_max); | |
| // Parent's thread-local locale must remain unaffected. | |
| EXPECT_EQ(uselocale(static_cast<locale_t>(0)), custom_locale); | |
| EXPECT_EQ(MB_CUR_MAX, expected_custom_mb_cur_max); | |
| } | |
| TEST(LibcThreadCreateTests, PthreadCreateInitialLocale) { | |
| VerifyInitialGlobalLocale(RunPthreadCheck); | |
| } | |
| TEST(LibcThreadCreateTests, ThrdCreateInitialLocale) { VerifyInitialGlobalLocale(RunThrdCheck); } | |
| TEST(LibcThreadCreateTests, ThrdCreateWithNameInitialLocale) { | |
| VerifyInitialGlobalLocale(RunThrdWithNameCheck); | |
| } | |
| TEST(LibcThreadCreateTests, PthreadCreateDoesNotInheritThreadLocale) { | |
| VerifyDoesNotInheritThreadLocale(RunPthreadCheck); | |
| } | |
| TEST(LibcThreadCreateTests, ThrdCreateDoesNotInheritThreadLocale) { | |
| VerifyDoesNotInheritThreadLocale(RunThrdCheck); | |
| } | |
| TEST(LibcThreadCreateTests, ThrdCreateWithNameDoesNotInheritThreadLocale) { | |
| VerifyDoesNotInheritThreadLocale(RunThrdWithNameCheck); | |
| } | |
| } // namespace | |
| } // namespace LIBC_NAMESPACE_DECL |