[libcxx-commits] [libcxx] [libcxx] Implementation of non lock-free atomic shared_ptr (PR #194215)

Hristo Hristov via libcxx-commits libcxx-commits at lists.llvm.org
Sun Apr 26 04:08:09 PDT 2026


================
@@ -0,0 +1,491 @@
+// -*- 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 <__atomic/atomic_sync_lite.h>
+#include <__atomic/check_memory_order.h>
+#include <__atomic/memory_order.h>
+#include <__atomic/support.h>
+#include <__config>
+#include <__cstddef/nullptr_t.h>
+#include <__memory/shared_count.h>
+#include <__utility/move.h>
+
+#include <cstdint>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#if defined(__SANITIZE_THREAD__) || (defined(__has_feature) && __has_feature(thread_sanitizer))
+#  if __has_include(<sanitizer/tsan_interface.h>)
+#    include <sanitizer/tsan_interface.h>
+#    define _LIBCPP_ATOMIC_SHARED_PTR_TSAN 1
+#  endif
+#endif
+
+_LIBCPP_PUSH_MACROS
+#include <__undef_macros>
+
+// TSAN annotations model the lock-bit protocol on __ctrl_.
+#if defined(_LIBCPP_ATOMIC_SHARED_PTR_TSAN)
+#  define _LIBCPP_ATOMIC_SP_TSAN_PRE_LOCK(addr)                                                                        \
+    ::__tsan_mutex_pre_lock(reinterpret_cast<void*>(const_cast<__cxx_atomic_impl<uintptr_t>*>(addr)), 0)
+#  define _LIBCPP_ATOMIC_SP_TSAN_POST_LOCK(addr)                                                                       \
+    ::__tsan_mutex_post_lock(reinterpret_cast<void*>(const_cast<__cxx_atomic_impl<uintptr_t>*>(addr)), 0, 0)
+#  define _LIBCPP_ATOMIC_SP_TSAN_PRE_UNLOCK(addr)                                                                      \
+    ::__tsan_mutex_pre_unlock(reinterpret_cast<void*>(const_cast<__cxx_atomic_impl<uintptr_t>*>(addr)), 0)
+#  define _LIBCPP_ATOMIC_SP_TSAN_POST_UNLOCK(addr)                                                                     \
+    ::__tsan_mutex_post_unlock(reinterpret_cast<void*>(const_cast<__cxx_atomic_impl<uintptr_t>*>(addr)), 0)
+#else
+#  define _LIBCPP_ATOMIC_SP_TSAN_PRE_LOCK(addr) ((void)(addr))
+#  define _LIBCPP_ATOMIC_SP_TSAN_POST_LOCK(addr) ((void)(addr))
+#  define _LIBCPP_ATOMIC_SP_TSAN_PRE_UNLOCK(addr) ((void)(addr))
+#  define _LIBCPP_ATOMIC_SP_TSAN_POST_UNLOCK(addr) ((void)(addr))
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+#if _LIBCPP_STD_VER >= 20 && _LIBCPP_HAS_THREADS && _LIBCPP_HAS_ATOMIC_HEADER
+
+template <class _Tp>
+class shared_ptr;
+
+template <class _Tp>
+class weak_ptr;
+
+template <class _Tp>
+struct atomic;
+
+// Split state into pointer word and control word.
+// The control word stores control-block pointer plus lock/notify bits.
+struct __atomic_smart_ptr_storage {
+  static constexpr uintptr_t __lock_bit_   = uintptr_t{1};
+  static constexpr uintptr_t __notify_bit_ = uintptr_t{2};
+  static constexpr uintptr_t __ptr_mask_   = ~(__lock_bit_ | __notify_bit_);
+
+  _LIBCPP_HIDE_FROM_ABI static uintptr_t __encode(__shared_weak_count* __ctrl, uintptr_t __bits) _NOEXCEPT {
+    return (reinterpret_cast<uintptr_t>(__ctrl) & __ptr_mask_) | (__bits & ~__ptr_mask_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI static __shared_weak_count* __decode(uintptr_t __word) _NOEXCEPT {
+    return reinterpret_cast<__shared_weak_count*>(__word & __ptr_mask_);
+  }
+
+  _LIBCPP_HIDE_FROM_ABI static bool __has_lock(uintptr_t __word) _NOEXCEPT { return (__word & __lock_bit_) != 0; }
+  _LIBCPP_HIDE_FROM_ABI static bool __has_notify(uintptr_t __word) _NOEXCEPT { return (__word & __notify_bit_) != 0; }
+};
+
+_LIBCPP_HIDE_FROM_ABI inline void __atomic_smart_ptr_notify_one(const void* __address) _NOEXCEPT {
+#  if _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
+  std::__atomic_notify_one_global_table(__address);
+#  else
+  std::__cxx_atomic_notify_one(reinterpret_cast<void const volatile*>(__address));
+#  endif
+}
+
+_LIBCPP_HIDE_FROM_ABI inline void __atomic_smart_ptr_notify_all(const void* __address) _NOEXCEPT {
+#  if _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
+  std::__atomic_notify_all_global_table(__address);
+#  else
+  std::__cxx_atomic_notify_all(reinterpret_cast<void const volatile*>(__address));
+#  endif
+}
+
+template <class _Poll>
+_LIBCPP_HIDE_FROM_ABI inline void __atomic_smart_ptr_wait_on_address(const void* __address, _Poll&& __poll) _NOEXCEPT {
+  while (!__poll()) {
+#  if _LIBCPP_AVAILABILITY_HAS_NEW_SYNC
+    auto __monitor_value = std::__atomic_monitor_global(__address);
+    if (__poll())
+      return;
+    std::__atomic_wait_global_table(__address, __monitor_value);
+#  else
+    void const volatile* __volatile_address = reinterpret_cast<void const volatile*>(__address);
+    auto __monitor_value                    = std::__libcpp_atomic_monitor(__volatile_address);
+    if (__poll())
+      return;
+    std::__libcpp_atomic_wait(__volatile_address, __monitor_value);
+#  endif
+  }
+}
+
+template <class _Element>
+struct __atomic_smart_ptr_fields {
+  mutable __cxx_atomic_impl<_Element*> __ptr_;
+  mutable __cxx_atomic_impl<uintptr_t> __ctrl_;
+
+  _LIBCPP_HIDE_FROM_ABI __atomic_smart_ptr_fields(_Element* __p, __shared_weak_count* __c) _NOEXCEPT
+      : __ptr_(__p),
+        __ctrl_(__atomic_smart_ptr_storage::__encode(__c, 0)) {}
+
+  _LIBCPP_HIDE_FROM_ABI const void* __ctrl_address() const _NOEXCEPT {
+    return static_cast<const void*>(__builtin_addressof(__ctrl_));
+  }
+
+  // Acquire lock bit on __ctrl_. Contended path sets notify bit and waits.
+  _LIBCPP_HIDE_FROM_ABI void __lock() const _NOEXCEPT {
+    _LIBCPP_ATOMIC_SP_TSAN_PRE_LOCK(&__ctrl_);
+    uintptr_t __expected = std::__cxx_atomic_load(__builtin_addressof(__ctrl_), memory_order_relaxed);
+    for (;;) {
+      if (!__atomic_smart_ptr_storage::__has_lock(__expected)) {
+        uintptr_t __desired = __expected | __atomic_smart_ptr_storage::__lock_bit_;
+        if (std::__cxx_atomic_compare_exchange_weak(
+                __builtin_addressof(__ctrl_),
+                __builtin_addressof(__expected),
+                __desired,
+                memory_order_acquire,
+                memory_order_relaxed)) {
+          _LIBCPP_ATOMIC_SP_TSAN_POST_LOCK(&__ctrl_);
+          return;
+        }
+        continue;
+      }
+
+      uintptr_t __with_notify = __expected | __atomic_smart_ptr_storage::__notify_bit_;
+      if (!__atomic_smart_ptr_storage::__has_notify(__expected)) {
+        if (!std::__cxx_atomic_compare_exchange_weak(
+                __builtin_addressof(__ctrl_),
+                __builtin_addressof(__expected),
+                __with_notify,
+                memory_order_relaxed,
+                memory_order_relaxed))
+          continue;
+        __expected = __with_notify;
+      }
+
+      std::__atomic_smart_ptr_wait_on_address(__ctrl_address(), [&] {
+        __expected = std::__cxx_atomic_load(__builtin_addressof(__ctrl_), memory_order_relaxed);
+        return !__atomic_smart_ptr_storage::__has_lock(__expected);
+      });
+    }
+  }
+
+  // Publish new control pointer, clear bits, and notify waiters if needed.
+  _LIBCPP_HIDE_FROM_ABI void __unlock(__shared_weak_count* __ctrl_to_publish) const _NOEXCEPT {
+    _LIBCPP_ATOMIC_SP_TSAN_PRE_UNLOCK(&__ctrl_);
+    uintptr_t __new_word = __atomic_smart_ptr_storage::__encode(__ctrl_to_publish, 0);
+    uintptr_t __previous = std::__cxx_atomic_exchange(__builtin_addressof(__ctrl_), __new_word, memory_order_release);
+    if (__atomic_smart_ptr_storage::__has_notify(__previous))
+      std::__atomic_smart_ptr_notify_all(__ctrl_address());
+    _LIBCPP_ATOMIC_SP_TSAN_POST_UNLOCK(&__ctrl_);
+  }
+};
+
+// [util.smartptr.atomic.shared]: same stored pointer and same ownership, or both empty.
+template <class _Element>
+_LIBCPP_HIDE_FROM_ABI inline bool __atomic_smart_ptr_equivalent(
+    _Element* __ptr,
+    __shared_weak_count* __ctrl,
+    _Element* __expected_ptr,
+    __shared_weak_count* __expected_ctrl) _NOEXCEPT {
+  if (__ctrl == nullptr && __expected_ctrl == nullptr)
+    return true;
+  return __ptr == __expected_ptr && __ctrl == __expected_ctrl;
+}
+
+template <class _Tp>
+struct atomic<shared_ptr<_Tp>> {
+  using value_type = shared_ptr<_Tp>;
+
+  static constexpr bool is_always_lock_free = false;
+
+  _LIBCPP_HIDE_FROM_ABI atomic() _NOEXCEPT : __fields_(nullptr, nullptr) {}
+  _LIBCPP_HIDE_FROM_ABI constexpr atomic(nullptr_t) _NOEXCEPT : __fields_(nullptr, nullptr) {}
+  _LIBCPP_HIDE_FROM_ABI atomic(shared_ptr<_Tp> __desired) _NOEXCEPT : __fields_(__desired.__ptr_, __desired.__cntrl_) {
+    __desired.__ptr_   = nullptr;
+    __desired.__cntrl_ = nullptr;
+  }
+
+  atomic(const atomic&)            = delete;
+  atomic& operator=(const atomic&) = delete;
+
+  _LIBCPP_HIDE_FROM_ABI ~atomic() {
+    if (auto* __c = __atomic_smart_ptr_storage::__decode(
+            std::__cxx_atomic_load(__builtin_addressof(__fields_.__ctrl_), memory_order_relaxed)))
+      __c->__release_shared();
+  }
+
+  _LIBCPP_HIDE_FROM_ABI bool is_lock_free() const _NOEXCEPT { return false; }
----------------
H-G-Hristov wrote:

```suggestion
  `[[nodiscard]]` _LIBCPP_HIDE_FROM_ABI bool is_lock_free() const noexcept { return false; }
```

Please add `[[nodiscard]]` according to: https://libcxx.llvm.org/CodingGuidelines.html#apply-nodiscard-where-relevant where it is relevant.

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


More information about the libcxx-commits mailing list