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

Vladislav Semykin via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 27 23:08:40 PDT 2026


ViNN280801 wrote:

> Moved from PR message to decrease information.

## Related Links

- Proposal - https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0718r2.html
- LWG 3661 - https://cplusplus.github.io/LWG/lwg-defects.html#3661
- LWG 3893 - https://cplusplus.github.io/LWG/lwg-defects.html#3893
- [util.smartptr.atomic] - https://eel.is/c++draft/util.smartptr.atomic
- [util.smartptr.atomic.shared] - https://eel.is/c++draft/util.smartptr.atomic.shared
- [util.smartptr.atomic.weak] - https://eel.is/c++draft/util.smartptr.atomic.weak
- [atomics.wait] - https://eel.is/c++draft/atomics.wait
- Last try - #78317 
- Issue - #99980
- GCC libstdc++ implementation - https://github.com/gcc-mirror/gcc/blob/master/libstdc++-v3/include/bits/shared_ptr_atomic.h
- Microsoft STL implementation - https://github.com/microsoft/STL/blob/main/stl/inc/memory

---

- [Williams DWCAS + split refcount - Github repo](https://github.com/anthonywilliams/atomic_shared_ptr)
- [Anderson hazard pointer - Concurrent Deffered Reference Counting with Constant-Time Overhead](https://www.cs.cmu.edu/~guyb/papers/3453483.3454060.pdf)
- [Anderson hazard pointer - Github repo](https://github.com/DanielLiamAnderson/atomic_shared_ptr)
- [Big Atomics - PPoPP 2025 DOI](https://dl.acm.org/doi/10.1145/3710848.3710874)
- [P2530R3](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2530r3.pdf) 

Not so relevant for libc++ because require 48-bit VA (broken on LA57/ARM 52-bit), dependents from libstdc++ internals:
- [Folly PackedSyncPtr](https://github.com/facebook/folly/blob/main/folly/PackedSyncPtr.h)
- [Folly AtomicSharedPtr](https://github.com/facebook/folly/blob/main/folly/concurrency/AtomicSharedPtr.h)

MSVC analysis:
- [Raymond Chen Blog](https://devblogs.microsoft.com/oldnewthing/20241219-00/?p=110663)

---

Konstantin Vladimirov's lectures:
- [Atomics part I](https://youtu.be/dRlOwdj8BHI?si=fpJMz386mb9CRtrl)
- [Atomics part II](https://youtu.be/hikc1u-zOhQ?si=TF-cBB5iieaZQFYM)

---

## Testing

All the tests are passed:

```log
[14/15] Running libcxx tests
llvm-lit: /home/loveit0/Documents/Prog/llvm-project/libcxx/utils/libcxx/test/config.py:24: note: (llvm-libc++-shared.cfg.in) All available features: add-latomic-workaround, buildhost=linux, c++26, c++experimental, can-create-symlinks, character-conversion-warnings, clang, clang-23, clang-23.0, clang-23.0.0, diagnose-if-support, enable-benchmarks=dry-run, gcc-style-warnings, has-1024-bit-atomics, has-64-bit-atomics, has-fblocks, has-fconstexpr-steps, has-unix-headers, host-has-gdb-with-python, large_tests, libcpp-abi-version=1, libcpp-hardening-mode=none, libcpp-has-no-availability-markup, libcpp-has-thread-api-pthread, linux, locale.en_US.UTF-8, locale.ru_RU.UTF-8, long_tests, objective-c++, optimization=none, std-at-least-c++03, std-at-least-c++11, std-at-least-c++14, std-at-least-c++17, std-at-least-c++20, std-at-least-c++23, std-at-least-c++26, stdlib=libc++, stdlib=llvm-libc++, target=x86_64-unknown-linux-gnu, verify-support

Testing Time: 1245.71s

Total Discovered Tests: 11409
  Unsupported      :  1029 (9.02%)
  Passed           : 10353 (90.74%)
  Expectedly Failed:    27 (0.24%)
./build/bin/llvm-lit -sv build/runtimes/runtimes-bins/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared.atomic
llvm-lit: /home/loveit0/Documents/Prog/llvm-project/libcxx/utils/libcxx/test/config.py:24: note: (llvm-libc++-shared.cfg.in) All available features: add-latomic-workaround, buildhost=linux, c++26, c++experimental, can-create-symlinks, character-conversion-warnings, clang, clang-23, clang-23.0, clang-23.0.0, diagnose-if-support, enable-benchmarks=run, gcc-style-warnings, has-1024-bit-atomics, has-64-bit-atomics, has-fblocks, has-fconstexpr-steps, has-unix-headers, host-has-gdb-with-python, large_tests, libcpp-abi-version=1, libcpp-hardening-mode=none, libcpp-has-no-availability-markup, libcpp-has-thread-api-pthread, linux, locale.en_US.UTF-8, locale.ru_RU.UTF-8, long_tests, objective-c++, optimization=none, std-at-least-c++03, std-at-least-c++11, std-at-least-c++14, std-at-least-c++17, std-at-least-c++20, std-at-least-c++23, std-at-least-c++26, stdlib=libc++, stdlib=llvm-libc++, target=x86_64-unknown-linux-gnu, verify-support

Testing Time: 3.16s

Total Discovered Tests: 18
  Passed: 18 (100.00%)
```

## Additional information

This is not a lock-free implementation, but I'm still on thoughts how to properly implement it, and I need more time to investigate existing imlpementations and think about the best way to do it.

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


More information about the libcxx-commits mailing list