libstdc++: Implement C++20 atomic<shared_ptr> and atomic<weak_ptr>
authorJonathan Wakely <jwakely@redhat.com>
Sun, 16 Jan 2022 20:47:09 +0000 (20:47 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 17 Jan 2022 00:06:28 +0000 (00:06 +0000)
commit2ac0649d7bf3eacbf92add1ec2b54045c401a4c2
treec858f264e67f6e875b9a122ebe865ed95bc6f154
parent86e3b476d5defaa79c94d40b76cbeec21cd02e5f
libstdc++: Implement C++20 atomic<shared_ptr> and atomic<weak_ptr>

This adds another piece of C++20, the std::atomic specializations for
std::shared_ptr and std::weak_ptr.

The new _Sp_atomic type mimics the structure of shared_ptr<T> and
weak_ptr<T>, holding a T* pointer (the one returned by get() on a
shared_ptr/weak ptr) and a _Sp_counted_base<>* pointer to the
ref-counted control block. For _Sp_atomic the low bit of the control
block pointer is used as a lock bit, to ensure only one thread will
access the object at a time.  The pointer is actually stored as a
uintptr_t to avoid accidental dereferences of the pointer when unlocked
(which would be a race) or when locked (which would dereference the
wrong pointer value due to the low bit being set). To get a raw pointer
to the control block, the lock must be acquired. Converting between a
_Sp_atomic and a shared_ptr or weak_ptr requires manually adjusting the
T* and _Sp_counted_base<>* members of the shared/weak ptr, instead of
going through the public API. This must be done carefully to ensure that
any change in the number of owners is reflected in a ref-count update.

Co-authored-by: Thomas Rodgers <trodgers@redhat.com>
Signed-off-by: Thomas Rodgers <trodgers@redhat.com>
libstdc++-v3/ChangeLog:

* include/bits/shared_ptr_atomic.h (__cpp_lib_atomic_shared_ptr):
New macro.
(_Sp_atomic): New class template.
(atomic<shared_ptr<T>>, atomic<weak_ptr<T>>): New partial
specializations.
* include/bits/shared_ptr_base.h (__shared_count, __weak_count)
(__shared_ptr, __weak_ptr): Declare _Sp_atomic as a friend.
* include/std/version (__cpp_lib_atomic_shared_ptr): New macro.
* testsuite/20_util/shared_ptr/atomic/atomic_shared_ptr.cc: New
test.
* testsuite/20_util/weak_ptr/atomic_weak_ptr.cc: New test.
libstdc++-v3/include/bits/shared_ptr_atomic.h
libstdc++-v3/include/bits/shared_ptr_base.h
libstdc++-v3/include/std/version
libstdc++-v3/testsuite/20_util/shared_ptr/atomic/atomic_shared_ptr.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/weak_ptr/atomic_weak_ptr.cc [new file with mode: 0644]