[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
Mon Aug 31 10:15:12 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:

Sure, there are some bugs, yes, and it'd be great to fix those. On Linux, users generally get libatomic.so from gcc, which does work correctly, at least.

The key thing about depending on libatomic to do the spinlock behavior if needed, though, is not that every implementation is bug-free, but that fixing such bugs is already something that needs to be done (so we're not adding _new_ similar sorts of issues), and further, since those are out-of-line functions, it can be done without breaking ABIs.


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


More information about the libcxx-commits mailing list