Index: include/memory =================================================================== --- include/memory +++ include/memory @@ -3587,7 +3587,7 @@ __shared_count& operator=(const __shared_count&); protected: - long __shared_owners_; + std::atomic __shared_owners_; virtual ~__shared_count(); private: virtual void __on_zero_shared() _NOEXCEPT = 0; @@ -3606,7 +3606,7 @@ class _LIBCPP_TYPE_VIS __shared_weak_count : private __shared_count { - long __shared_weak_owners_; + std::atomic __shared_weak_owners_; public: _LIBCPP_INLINE_VISIBILITY Index: src/memory.cpp =================================================================== --- src/memory.cpp +++ src/memory.cpp @@ -16,25 +16,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD -namespace -{ - -template -inline T -increment(T& t) _NOEXCEPT -{ - return __sync_add_and_fetch(&t, 1); -} - -template -inline T -decrement(T& t) _NOEXCEPT -{ - return __sync_add_and_fetch(&t, -1); -} - -} // namespace - const allocator_arg_t allocator_arg = allocator_arg_t(); bad_weak_ptr::~bad_weak_ptr() _NOEXCEPT {} @@ -52,13 +33,13 @@ void __shared_count::__add_shared() _NOEXCEPT { - increment(__shared_owners_); + ++__shared_owners_; } bool __shared_count::__release_shared() _NOEXCEPT { - if (decrement(__shared_owners_) == -1) + if (__shared_owners_.fetch_add(-1) == 0) { __on_zero_shared(); return true; @@ -79,7 +60,7 @@ void __shared_weak_count::__add_weak() _NOEXCEPT { - increment(__shared_weak_owners_); + ++__shared_weak_owners_; } void @@ -92,7 +73,7 @@ void __shared_weak_count::__release_weak() _NOEXCEPT { - if (decrement(__shared_weak_owners_) == -1) + if (__shared_weak_owners_.fetch_add(-1) == 0) __on_zero_shared_weak(); } @@ -102,9 +83,8 @@ long object_owners = __shared_owners_; while (object_owners != -1) { - if (__sync_bool_compare_and_swap(&__shared_owners_, - object_owners, - object_owners+1)) + if (__shared_owners_.compare_exchange_strong(object_owners, + object_owners + 1)) return this; object_owners = __shared_owners_; }