[llvm-branch-commits] [libcxx] release/23.x: Make ios_base::xalloc non-atomic with LIBCXX_ENABLE_THREADS=OFF. (#208356) (PR #209631)

Douglas Yung via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jul 22 06:43:52 PDT 2026


https://github.com/dyung updated https://github.com/llvm/llvm-project/pull/209631

>From 2cff4ff7a21c91b3493eb0fd8a7f072aee77fe7d Mon Sep 17 00:00:00 2001
From: Eli Friedman <efriedma at qti.qualcomm.com>
Date: Tue, 14 Jul 2026 13:54:25 -0700
Subject: [PATCH] Make ios_base::xalloc non-atomic with
 LIBCXX_ENABLE_THREADS=OFF. (#208356)

762b77a moved the definition of "xindex" out of the header, and in the
process dropped the _LIBCPP_HAS_THREADS check. Re-add the check to
maintain the status quo.

The discussion on https://github.com/llvm/llvm-project/pull/198994
indicates it's not clear whether LIBCXX_ENABLE_THREADS=OFF is actually
supposed to mean single-threaded. But it clearly does in practice:
atomic_support.h uses non-atomic ops when threads are disabled, and a
few other APIs have explicit non-atomic fallback paths.

My team ran into this trying to run libc++ tests for a RISC-V core
without the "a" extension.

(cherry picked from commit 3eb929be5e17d66900020bd1caa7d2510d4f9601)
---
 libcxx/src/ios.cpp | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/libcxx/src/ios.cpp b/libcxx/src/ios.cpp
index 2e049098740dc..db6ca50f7f6ca 100644
--- a/libcxx/src/ios.cpp
+++ b/libcxx/src/ios.cpp
@@ -122,7 +122,14 @@ static size_t __ios_new_cap(size_t __req_size, size_t __current_cap) { // Precon
 }
 
 int ios_base::xalloc() {
+#if _LIBCPP_HAS_THREADS
   constinit static atomic<int> xindex = 0;
+#else
+  // If we don't have atomics, fall back to single-threaded implementation.
+  // FIXME: Should "single-threaded" be a separate option from
+  // _LIBCPP_HAS_THREADS?
+  static int xindex = 0;
+#endif // _LIBCPP_HAS_THREADS
   return xindex++;
 }
 



More information about the llvm-branch-commits mailing list