[libcxx-commits] [libcxx] [libc++] P0718R2: Implementation of std::atomic<shared_ptr<T>> and std::atomic<weak_ptr<T>> (PR #194215)
James Y Knight via libcxx-commits
libcxx-commits at lists.llvm.org
Sat Aug 29 07:57:56 PDT 2026
================
@@ -0,0 +1,39 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP___MEMORY_ATOMIC_SHARED_PTR_H
+#define _LIBCPP___MEMORY_ATOMIC_SHARED_PTR_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+# pragma GCC system_header
+#endif
+
+// _LIBCPP_FORCE_LOCK_BASED_ATOMIC_SHARED_PTR selects the stolen-bit spinlock
+// even on DWCAS targets. Local A/B-benchmark hook for
+// libcxx/test/benchmarks/atomic_shared_ptr_lock_based.bench.cpp; not a
+// supported public configuration. Must be consistent across the whole program.
+#if defined(_LIBCPP_FORCE_LOCK_BASED_ATOMIC_SHARED_PTR)
+# define _LIBCPP_HAS_LOCKFREE_ATOMIC_SHARED_PTR 0
+#elif _LIBCPP_HAS_THREADS && \
----------------
jyknight wrote:
The PR doesn't look like it uses the same ABI when DCAS is available when compiling a given TU, and when it's not. That can't work, because "having a 16-byte CAS" isn't an immutable property of a platform, and it's supported to work to mix code compiled for newer CPU microarchitectures with code compiled for older microarchitectures.
E.g. on x86-64, it must be possible to compile one TU with `-mcx16` and another with `-mno-cx16`, and link them together and have it work compatibly.
Could you use the "lock free" variant of the code universally, instead? The core `__atomic_*` primitives already implement spinlocks internally when a platform doesn't natively support the desired size atomics.
If you can't, then libc++ will need to implement runtime detection/dispatch too, which seems annoying.
https://github.com/llvm/llvm-project/pull/194215
More information about the libcxx-commits
mailing list