gcc/libstdc++-v3/testsuite/20_util/shared_ptr
Jonathan Wakely 2ac0649d7b 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.
2022-01-17 00:06:28 +00:00
..
assign Update copyright years. 2022-01-03 10:42:10 +01:00
atomic libstdc++: Implement C++20 atomic<shared_ptr> and atomic<weak_ptr> 2022-01-17 00:06:28 +00:00
casts Update copyright years. 2022-01-03 10:42:10 +01:00
comparison libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
cons Update copyright years. 2022-01-03 10:42:10 +01:00
creation libstdc++: Add C++20 std::make_shared enhancements (P0674R1) 2022-01-14 10:14:25 +00:00
dest Update copyright years. 2022-01-03 10:42:10 +01:00
hash Update copyright years. 2022-01-03 10:42:10 +01:00
misc Update copyright years. 2022-01-03 10:42:10 +01:00
modifiers Update copyright years. 2022-01-03 10:42:10 +01:00
observers Update copyright years. 2022-01-03 10:42:10 +01:00
requirements Update copyright years. 2022-01-03 10:42:10 +01:00
thread Update copyright years. 2022-01-03 10:42:10 +01:00