[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 Oct 14 00:28:53 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_Relaxed);
+  if (val >= first_id)
+    return val - first_id;
+
+  if (val == uninitialized && __libcpp_atomic_compare_exchange_strong(&__id_, &val, pending)) {
----------------
philnik777 wrote:

We can't. `__libcpp_atomic_compare_exchange_strong` disables atomic for when we don't have threads. This should be cleaned up, but that's a bigger change.

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


More information about the libcxx-commits mailing list