Skip to content

Commit

Permalink
Fix atomic smart pointers with array elements
Browse files Browse the repository at this point in the history
Fix atomic smart pointers with array elements by making the internal type match that of [weak|shared]_ptr::element_type by removing the extent of the arrays.
  • Loading branch information
xmas92 committed Oct 1, 2020
1 parent 975ac24 commit 0fd8850
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions stl/inc/memory
Original file line number Diff line number Diff line change
Expand Up @@ -3812,9 +3812,10 @@ class alignas(2 * sizeof(void*)) _Atomic_ptr_base {
protected:
constexpr _Atomic_ptr_base() noexcept = default;

_Atomic_ptr_base(_Ty* const _Px, _Ref_count_base* const _Ref) noexcept : _Ptr(_Px), _Repptr(_Ref) {}
_Atomic_ptr_base(remove_extent_t<_Ty>* const _Px, _Ref_count_base* const _Ref) noexcept
: _Ptr(_Px), _Repptr(_Ref) {}

void _Wait(_Ty* _Old, memory_order) const noexcept {
void _Wait(remove_extent_t<_Ty>* _Old, memory_order) const noexcept {
for (;;) {
auto _Rep = _Repptr._Lock_and_load();
bool _Equal = _Ptr.load(memory_order_relaxed) == _Old;
Expand All @@ -3834,7 +3835,7 @@ protected:
_Ptr.notify_all();
}

atomic<_Ty*> _Ptr{nullptr};
atomic<remove_extent_t<_Ty>*> _Ptr{nullptr};
mutable _Locked_pointer<_Ref_count_base> _Repptr;
};

Expand All @@ -3854,9 +3855,9 @@ public:

void store(shared_ptr<_Ty> _Value, const memory_order _Order = memory_order_seq_cst) noexcept {
_Check_store_memory_order(_Order);
const auto _Rep = this->_Repptr._Lock_and_load();
_Ty* const _Tmp = _Value._Ptr;
_Value._Ptr = this->_Ptr.load(memory_order_relaxed);
const auto _Rep = this->_Repptr._Lock_and_load();
remove_extent_t<_Ty>* const _Tmp = _Value._Ptr;
_Value._Ptr = this->_Ptr.load(memory_order_relaxed);
this->_Ptr.store(_Tmp, memory_order_relaxed);
this->_Repptr._Store_and_unlock(_Value._Rep);
_Value._Rep = _Rep;
Expand Down Expand Up @@ -3909,8 +3910,8 @@ public:
_Check_memory_order(_Order);
auto _Rep = this->_Repptr._Lock_and_load();
if (this->_Ptr.load(memory_order_relaxed) == _Expected._Ptr && _Rep == _Expected._Rep) {
_Ty* const _Tmp = _Desired._Ptr;
_Desired._Ptr = this->_Ptr.load(memory_order_relaxed);
remove_extent_t<_Ty>* const _Tmp = _Desired._Ptr;
_Desired._Ptr = this->_Ptr.load(memory_order_relaxed);
this->_Ptr.store(_Tmp, memory_order_relaxed);
_STD swap(_Rep, _Desired._Rep);
this->_Repptr._Store_and_unlock(_Rep);
Expand Down Expand Up @@ -3971,9 +3972,9 @@ public:

void store(weak_ptr<_Ty> _Value, const memory_order _Order = memory_order_seq_cst) noexcept {
_Check_store_memory_order(_Order);
const auto _Rep = this->_Repptr._Lock_and_load();
_Ty* const _Tmp = _Value._Ptr;
_Value._Ptr = this->_Ptr.load(memory_order_relaxed);
const auto _Rep = this->_Repptr._Lock_and_load();
remove_extent_t<_Ty>* const _Tmp = _Value._Ptr;
_Value._Ptr = this->_Ptr.load(memory_order_relaxed);
this->_Ptr.store(_Tmp, memory_order_relaxed);
this->_Repptr._Store_and_unlock(_Value._Rep);
_Value._Rep = _Rep;
Expand Down Expand Up @@ -4026,8 +4027,8 @@ public:
_Check_memory_order(_Order);
auto _Rep = this->_Repptr._Lock_and_load();
if (this->_Ptr.load(memory_order_relaxed) == _Expected._Ptr && _Rep == _Expected._Rep) {
_Ty* const _Tmp = _Desired._Ptr;
_Desired._Ptr = this->_Ptr.load(memory_order_relaxed);
remove_extent_t<_Ty>* const _Tmp = _Desired._Ptr;
_Desired._Ptr = this->_Ptr.load(memory_order_relaxed);
this->_Ptr.store(_Tmp, memory_order_relaxed);
_STD swap(_Rep, _Desired._Rep);
this->_Repptr._Store_and_unlock(_Rep);
Expand Down

0 comments on commit 0fd8850

Please sign in to comment.