From f7850fa8b64d56e688543414d02bca3b1f1c9364 Mon Sep 17 00:00:00 2001 From: Weiming Zhao Date: Mon, 10 Jul 2017 21:37:35 +0000 Subject: [PATCH] [libc++] Refactoring __sync_* builtins; NFC (Reland) Summary: Wrap __sync_* builtins with __libcpp_ functions to facility future customizations as atomic operations are unavailable on some targets. Reviewers: danalbert, EricWF, jroelofs Subscribers: joerg, llvm-commits Differential Revision: https://reviews.llvm.org/D34918 llvm-svn: 307595 --- libcxx/include/__atomic_support | 31 ++++++++++++++++++++++ libcxx/include/__refstring | 9 ++++--- libcxx/src/locale.cpp | 3 ++- libcxx/src/support/runtime/exception_fallback.ipp | 10 +++---- .../src/support/runtime/new_handler_fallback.ipp | 6 +++-- 5 files changed, 47 insertions(+), 12 deletions(-) create mode 100644 libcxx/include/__atomic_support diff --git a/libcxx/include/__atomic_support b/libcxx/include/__atomic_support new file mode 100644 index 0000000..e53140c --- /dev/null +++ b/libcxx/include/__atomic_support @@ -0,0 +1,31 @@ +// -*- 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 diff --git a/libcxx/include/__refstring b/libcxx/include/__refstring index 7f417a0..de89b79 100644 --- a/libcxx/include/__refstring +++ b/libcxx/include/__refstring @@ -14,6 +14,7 @@ #include #include #include +#include <__atomic_support> #ifdef __APPLE__ #include #include @@ -83,7 +84,7 @@ __libcpp_refstring::__libcpp_refstring(const __libcpp_refstring &s) _NOEXCEPT : __imp_(s.__imp_) { if (__uses_refcount()) - __sync_add_and_fetch(&rep_from_data(__imp_)->count, 1); + __libcpp_sync_add_and_fetch(&rep_from_data(__imp_)->count, 1); } inline @@ -92,10 +93,10 @@ __libcpp_refstring& __libcpp_refstring::operator=(__libcpp_refstring const& s) _ struct _Rep_base *old_rep = rep_from_data(__imp_); __imp_ = s.__imp_; if (__uses_refcount()) - __sync_add_and_fetch(&rep_from_data(__imp_)->count, 1); + __libcpp_sync_add_and_fetch(&rep_from_data(__imp_)->count, 1); if (adjust_old_count) { - if (__sync_add_and_fetch(&old_rep->count, count_t(-1)) < 0) + if (__libcpp_sync_add_and_fetch(&old_rep->count, count_t(-1)) < 0) { ::operator delete(old_rep); } @@ -107,7 +108,7 @@ inline __libcpp_refstring::~__libcpp_refstring() { if (__uses_refcount()) { _Rep_base* rep = rep_from_data(__imp_); - if (__sync_add_and_fetch(&rep->count, count_t(-1)) < 0) { + if (__libcpp_sync_add_and_fetch(&rep->count, count_t(-1)) < 0) { ::operator delete(rep); } } diff --git a/libcxx/src/locale.cpp b/libcxx/src/locale.cpp index 3b4c83a..e73e6d5 100644 --- a/libcxx/src/locale.cpp +++ b/libcxx/src/locale.cpp @@ -28,6 +28,7 @@ #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" @@ -667,7 +668,7 @@ locale::id::__get() void locale::id::__init() { - __id_ = __sync_add_and_fetch(&__next_id, 1); + __id_ = __libcpp_sync_add_and_fetch(&__next_id, 1); } // template <> class collate_byname diff --git a/libcxx/src/support/runtime/exception_fallback.ipp b/libcxx/src/support/runtime/exception_fallback.ipp index 69c06a9..cdf008a 100644 --- a/libcxx/src/support/runtime/exception_fallback.ipp +++ b/libcxx/src/support/runtime/exception_fallback.ipp @@ -9,6 +9,7 @@ //===----------------------------------------------------------------------===// #include +#include <__atomic_support> namespace std { @@ -20,13 +21,13 @@ _LIBCPP_SAFE_STATIC static std::unexpected_handler __unexpected_handler; unexpected_handler set_unexpected(unexpected_handler func) _NOEXCEPT { - return __sync_lock_test_and_set(&__unexpected_handler, func); + return __libcpp_sync_lock_test_and_set(&__unexpected_handler, func); } unexpected_handler get_unexpected() _NOEXCEPT { - return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); + return __libcpp_sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); } @@ -41,14 +42,13 @@ void unexpected() terminate_handler set_terminate(terminate_handler func) _NOEXCEPT { - return __sync_lock_test_and_set(&__terminate_handler, func); + return __libcpp_sync_lock_test_and_set(&__terminate_handler, func); } terminate_handler get_terminate() _NOEXCEPT { - return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); - + return __libcpp_sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); } #ifndef __EMSCRIPTEN__ // We provide this in JS diff --git a/libcxx/src/support/runtime/new_handler_fallback.ipp b/libcxx/src/support/runtime/new_handler_fallback.ipp index b7092d5..75f985d 100644 --- a/libcxx/src/support/runtime/new_handler_fallback.ipp +++ b/libcxx/src/support/runtime/new_handler_fallback.ipp @@ -8,6 +8,8 @@ // //===----------------------------------------------------------------------===// +#include <__atomic_support> + namespace std { _LIBCPP_SAFE_STATIC static std::new_handler __new_handler; @@ -15,13 +17,13 @@ _LIBCPP_SAFE_STATIC static std::new_handler __new_handler; new_handler set_new_handler(new_handler handler) _NOEXCEPT { - return __sync_lock_test_and_set(&__new_handler, handler); + return __libcpp_sync_lock_test_and_set(&__new_handler, handler); } new_handler get_new_handler() _NOEXCEPT { - return __sync_fetch_and_add(&__new_handler, nullptr); + return __libcpp_sync_fetch_and_add(&__new_handler, nullptr); } } // namespace std -- 2.7.4