[libcxx-commits] [libcxx] [libc++] Use a custom once_flag implementation for locale::id::__get() (PR #212235)

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 12 04:04:44 PDT 2026


================
@@ -563,9 +563,29 @@ void locale::facet::__on_zero_shared() noexcept { delete this; }
 // locale::id
 
 long locale::id::__get() {
-  constinit static int32_t next_id = 0;
-  call_once(__flag_, [&] { __id_ = __libcpp_atomic_add(&next_id, 1); });
-  return __id_ - 1;
+  constexpr __id_type uninitialized = 0;
+  constexpr __id_type pending       = 1;
+  constexpr __id_type first_id      = 2;
+
+  constinit static __id_type next_id = first_id;
+
+  auto val = __libcpp_atomic_load(&__id_, _AO_Acquire);
----------------
philnik777 wrote:

I'm pretty sure it's not supposed to be. The `locale::id`s are globals and are essentially initialized on first use, so e.g. any two streams that use e.g. the `classic()` locale would potentially race. Though I think we can get away with a relaxed load, which results in the same instructions on every major platform: https://godbolt.org/z/7T5jej6Mz

https://github.com/llvm/llvm-project/pull/212235


More information about the libcxx-commits mailing list