Index: include/__atomic_support =================================================================== --- include/__atomic_support +++ /dev/null @@ -1,31 +0,0 @@ -// -*- C++ -*- -//===------------------------ __atomic_support -----------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is dual licensed under the MIT and the University of Illinois Open -// Source Licenses. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef _LIBCPP___ATOMIC_SUPPORT -#define _LIBCPP___ATOMIC_SUPPORT - -#include <__config> - -template -T __libcpp_sync_add_and_fetch(T *ptr, T value) { - return __sync_add_and_fetch(ptr, value); -} - -template -T __libcpp_sync_fetch_and_add(T *ptr, T value) { - return __sync_fetch_and_add(ptr, value); -} - -template -T __libcpp_sync_lock_test_and_set(T *ptr, T value) { - return __sync_lock_test_and_set(ptr, value); -} - -#endif //_LIBCPP___ATOMIC_SUPPORT Index: include/__refstring =================================================================== --- include/__refstring +++ include/__refstring @@ -14,7 +14,6 @@ #include #include #include -#include <__atomic_support> #ifdef __APPLE__ #include #include @@ -84,7 +83,7 @@ : __imp_(s.__imp_) { if (__uses_refcount()) - __libcpp_sync_add_and_fetch(&rep_from_data(__imp_)->count, 1); + __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); } inline @@ -93,10 +92,10 @@ struct _Rep_base *old_rep = rep_from_data(__imp_); __imp_ = s.__imp_; if (__uses_refcount()) - __libcpp_sync_add_and_fetch(&rep_from_data(__imp_)->count, 1); + __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1); if (adjust_old_count) { - if (__libcpp_sync_add_and_fetch(&old_rep->count, count_t(-1)) < 0) + if (__libcpp_atomic_add(&old_rep->count, count_t(-1)) < 0) { ::operator delete(old_rep); } @@ -108,7 +107,7 @@ __libcpp_refstring::~__libcpp_refstring() { if (__uses_refcount()) { _Rep_base* rep = rep_from_data(__imp_); - if (__libcpp_sync_add_and_fetch(&rep->count, count_t(-1)) < 0) { + if (__libcpp_atomic_add(&rep->count, count_t(-1)) < 0) { ::operator delete(rep); } } Index: src/exception.cpp =================================================================== --- src/exception.cpp +++ src/exception.cpp @@ -31,6 +31,7 @@ #include "support/runtime/exception_glibcxx.ipp" #include "support/runtime/exception_pointer_glibcxx.ipp" #else +#include "include/atomic_support.h" #include "support/runtime/exception_fallback.ipp" #include "support/runtime/exception_pointer_unimplemented.ipp" #endif Index: src/include/atomic_support.h =================================================================== --- src/include/atomic_support.h +++ src/include/atomic_support.h @@ -16,6 +16,7 @@ #if defined(__clang__) && __has_builtin(__atomic_load_n) \ && __has_builtin(__atomic_store_n) \ && __has_builtin(__atomic_add_fetch) \ + && __has_builtin(__atomic_exchange_n) \ && __has_builtin(__atomic_compare_exchange_n) \ && defined(__ATOMIC_RELAXED) \ && defined(__ATOMIC_CONSUME) \ @@ -84,6 +85,14 @@ template inline _LIBCPP_INLINE_VISIBILITY +_ValueType __libcpp_atomic_exchange(_ValueType* __target, + _ValueType __value, int __order = _AO_Seq) +{ + return __atomic_exchange_n(__target, __value, __order); +} + +template +inline _LIBCPP_INLINE_VISIBILITY bool __libcpp_atomic_compare_exchange(_ValueType* __val, _ValueType* __expected, _ValueType __after, int __success_order = _AO_Seq, @@ -137,6 +146,17 @@ template inline _LIBCPP_INLINE_VISIBILITY +_ValueType __libcpp_atomic_exchange(_ValueType* __target, + _ValueType __value, int __order = _AO_Seq) +{ + _ValueType old; + old = *__target; + *__target = __value; + return old; +} + +template +inline _LIBCPP_INLINE_VISIBILITY bool __libcpp_atomic_compare_exchange(_ValueType* __val, _ValueType* __expected, _ValueType __after, int = 0, int = 0) Index: src/locale.cpp =================================================================== --- src/locale.cpp +++ src/locale.cpp @@ -28,7 +28,6 @@ #define _CTYPE_DISABLE_MACROS #endif #include "cwctype" -#include "__atomic_support" #include "__sso_allocator" #if defined(_LIBCPP_MSVCRT) || defined(__MINGW32__) #include "support/win32/locale_win32.h" @@ -37,6 +36,7 @@ #endif #include #include +#include "include/atomic_support.h" #include "__undef_macros" // On Linux, wint_t and wchar_t have different signed-ness, and this causes @@ -668,7 +668,7 @@ void locale::id::__init() { - __id_ = __libcpp_sync_add_and_fetch(&__next_id, 1); + __id_ = __libcpp_atomic_add(&__next_id, 1); } // template <> class collate_byname Index: src/new.cpp =================================================================== --- src/new.cpp +++ src/new.cpp @@ -12,6 +12,7 @@ #include #include "new" +#include "include/atomic_support.h" #if defined(_LIBCPP_ABI_MICROSOFT) // nothing todo Index: src/stdexcept.cpp =================================================================== --- src/stdexcept.cpp +++ src/stdexcept.cpp @@ -11,6 +11,7 @@ #include "new" #include "string" #include "system_error" +#include "include/atomic_support.h" #include "__refstring" /* For _LIBCPPABI_VERSION */ Index: src/support/runtime/exception_fallback.ipp =================================================================== --- src/support/runtime/exception_fallback.ipp +++ src/support/runtime/exception_fallback.ipp @@ -9,7 +9,6 @@ //===----------------------------------------------------------------------===// #include -#include <__atomic_support> namespace std { @@ -21,13 +20,13 @@ unexpected_handler set_unexpected(unexpected_handler func) _NOEXCEPT { - return __libcpp_sync_lock_test_and_set(&__unexpected_handler, func); + return __libcpp_atomic_exchange(&__unexpected_handler, func); } unexpected_handler get_unexpected() _NOEXCEPT { - return __libcpp_sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); + return __libcpp_atomic_load(&__unexpected_handler); } @@ -42,13 +41,13 @@ terminate_handler set_terminate(terminate_handler func) _NOEXCEPT { - return __libcpp_sync_lock_test_and_set(&__terminate_handler, func); + return __libcpp_atomic_exchange(&__terminate_handler, func); } terminate_handler get_terminate() _NOEXCEPT { - return __libcpp_sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); + return __libcpp_atomic_load(&__terminate_handler); } #ifndef __EMSCRIPTEN__ // We provide this in JS Index: src/support/runtime/new_handler_fallback.ipp =================================================================== --- src/support/runtime/new_handler_fallback.ipp +++ src/support/runtime/new_handler_fallback.ipp @@ -8,7 +8,6 @@ // //===----------------------------------------------------------------------===// -#include <__atomic_support> namespace std { @@ -17,13 +16,13 @@ new_handler set_new_handler(new_handler handler) _NOEXCEPT { - return __libcpp_sync_lock_test_and_set(&__new_handler, handler); + return __libcpp_atomic_exchange(&__new_handler, handler); } new_handler get_new_handler() _NOEXCEPT { - return __libcpp_sync_fetch_and_add(&__new_handler, nullptr); + return __libcpp_atomic_load(&__new_handler); } } // namespace std