Author: weimingz
Date: Tue Sep 19 16:18:03 2017
New Revision: 313694

URL: http://llvm.org/viewvc/llvm-project?rev=313694&view=rev
Log:
[libc++] Replace __sync_* functions with __libcpp_atomic_* functions

Summary:
This patch replaces __sync_* with __libcpp_atomic_* and adds a wrapper
function for __atomic_exchange to support _LIBCPP_HAS_NO_THREADS.

Reviewers: EricWF, jroelofs, mclow.lists, compnerd

Reviewed By: EricWF, compnerd

Subscribers: compnerd, efriedma, cfe-commits, joerg, llvm-commits

Differential Revision: https://reviews.llvm.org/D35235

Modified:
    libcxx/trunk/src/exception.cpp
    libcxx/trunk/src/include/atomic_support.h
    libcxx/trunk/src/include/refstring.h
    libcxx/trunk/src/locale.cpp
    libcxx/trunk/src/new.cpp
    libcxx/trunk/src/support/runtime/exception_fallback.ipp
    libcxx/trunk/src/support/runtime/new_handler_fallback.ipp

Modified: libcxx/trunk/src/exception.cpp
URL: 
http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/exception.cpp?rev=313694&r1=313693&r2=313694&view=diff
==============================================================================
--- libcxx/trunk/src/exception.cpp (original)
+++ libcxx/trunk/src/exception.cpp Tue Sep 19 16:18:03 2017
@@ -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

Modified: libcxx/trunk/src/include/atomic_support.h
URL: 
http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/include/atomic_support.h?rev=313694&r1=313693&r2=313694&view=diff
==============================================================================
--- libcxx/trunk/src/include/atomic_support.h (original)
+++ libcxx/trunk/src/include/atomic_support.h Tue Sep 19 16:18:03 2017
@@ -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 @@ _ValueType __libcpp_atomic_add(_ValueTyp
 
 template <class _ValueType>
 inline _LIBCPP_INLINE_VISIBILITY
+_ValueType __libcpp_atomic_exchange(_ValueType* __target,
+                                    _ValueType __value, int __order = _AO_Seq)
+{
+    return __atomic_exchange_n(__target, __value, __order);
+}
+
+template <class _ValueType>
+inline _LIBCPP_INLINE_VISIBILITY
 bool __libcpp_atomic_compare_exchange(_ValueType* __val,
     _ValueType* __expected, _ValueType __after,
     int __success_order = _AO_Seq,
@@ -136,6 +145,16 @@ _ValueType __libcpp_atomic_add(_ValueTyp
 }
 
 template <class _ValueType>
+inline _LIBCPP_INLINE_VISIBILITY
+_ValueType __libcpp_atomic_exchange(_ValueType* __target,
+                                    _ValueType __value, int __order = _AO_Seq)
+{
+    _ValueType old = *__target;
+    *__target = __value;
+    return old;
+}
+
+template <class _ValueType>
 inline _LIBCPP_INLINE_VISIBILITY
 bool __libcpp_atomic_compare_exchange(_ValueType* __val,
     _ValueType* __expected, _ValueType __after,

Modified: libcxx/trunk/src/include/refstring.h
URL: 
http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/include/refstring.h?rev=313694&r1=313693&r2=313694&view=diff
==============================================================================
--- libcxx/trunk/src/include/refstring.h (original)
+++ libcxx/trunk/src/include/refstring.h Tue Sep 19 16:18:03 2017
@@ -18,6 +18,7 @@
 #include <dlfcn.h>
 #include <mach-o/dyld.h>
 #endif
+#include "atomic_support.h"
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
@@ -83,7 +84,7 @@ __libcpp_refstring::__libcpp_refstring(c
     : __imp_(s.__imp_)
 {
     if (__uses_refcount())
-        __sync_add_and_fetch(&rep_from_data(__imp_)->count, 1);
+        __libcpp_atomic_add(&rep_from_data(__imp_)->count, 1);
 }
 
 inline
@@ -92,10 +93,10 @@ __libcpp_refstring& __libcpp_refstring::
     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_atomic_add(&rep_from_data(__imp_)->count, 1);
     if (adjust_old_count)
     {
-        if (__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);
         }
@@ -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_atomic_add(&rep->count, count_t(-1)) < 0) {
             ::operator delete(rep);
         }
     }

Modified: libcxx/trunk/src/locale.cpp
URL: 
http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/locale.cpp?rev=313694&r1=313693&r2=313694&view=diff
==============================================================================
--- libcxx/trunk/src/locale.cpp (original)
+++ libcxx/trunk/src/locale.cpp Tue Sep 19 16:18:03 2017
@@ -36,6 +36,7 @@
 #endif
 #include <stdlib.h>
 #include <stdio.h>
+#include "include/atomic_support.h"
 #include "__undef_macros"
 
 // On Linux, wint_t and wchar_t have different signed-ness, and this causes
@@ -667,7 +668,7 @@ locale::id::__get()
 void
 locale::id::__init()
 {
-    __id_ = __sync_add_and_fetch(&__next_id, 1);
+    __id_ = __libcpp_atomic_add(&__next_id, 1);
 }
 
 // template <> class collate_byname<char>

Modified: libcxx/trunk/src/new.cpp
URL: 
http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/new.cpp?rev=313694&r1=313693&r2=313694&view=diff
==============================================================================
--- libcxx/trunk/src/new.cpp (original)
+++ libcxx/trunk/src/new.cpp Tue Sep 19 16:18:03 2017
@@ -12,6 +12,7 @@
 #include <stdlib.h>
 
 #include "new"
+#include "include/atomic_support.h"
 
 #if defined(_LIBCPP_ABI_MICROSOFT)
 // nothing todo

Modified: libcxx/trunk/src/support/runtime/exception_fallback.ipp
URL: 
http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/support/runtime/exception_fallback.ipp?rev=313694&r1=313693&r2=313694&view=diff
==============================================================================
--- libcxx/trunk/src/support/runtime/exception_fallback.ipp (original)
+++ libcxx/trunk/src/support/runtime/exception_fallback.ipp Tue Sep 19 16:18:03 
2017
@@ -20,13 +20,13 @@ _LIBCPP_SAFE_STATIC static std::unexpect
 unexpected_handler
 set_unexpected(unexpected_handler func) _NOEXCEPT
 {
-  return __sync_lock_test_and_set(&__unexpected_handler, func);
+  return __libcpp_atomic_exchange(&__unexpected_handler, func);
 }
 
 unexpected_handler
 get_unexpected() _NOEXCEPT
 {
-  return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0);
+  return __libcpp_atomic_load(&__unexpected_handler);
 
 }
 
@@ -41,14 +41,13 @@ void unexpected()
 terminate_handler
 set_terminate(terminate_handler func) _NOEXCEPT
 {
-  return __sync_lock_test_and_set(&__terminate_handler, func);
+  return __libcpp_atomic_exchange(&__terminate_handler, func);
 }
 
 terminate_handler
 get_terminate() _NOEXCEPT
 {
-  return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0);
-
+  return __libcpp_atomic_load(&__terminate_handler);
 }
 
 #ifndef __EMSCRIPTEN__ // We provide this in JS

Modified: libcxx/trunk/src/support/runtime/new_handler_fallback.ipp
URL: 
http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/support/runtime/new_handler_fallback.ipp?rev=313694&r1=313693&r2=313694&view=diff
==============================================================================
--- libcxx/trunk/src/support/runtime/new_handler_fallback.ipp (original)
+++ libcxx/trunk/src/support/runtime/new_handler_fallback.ipp Tue Sep 19 
16:18:03 2017
@@ -15,13 +15,13 @@ _LIBCPP_SAFE_STATIC static std::new_hand
 new_handler
 set_new_handler(new_handler handler) _NOEXCEPT
 {
-    return __sync_lock_test_and_set(&__new_handler, handler);
+    return __libcpp_atomic_exchange(&__new_handler, handler);
 }
 
 new_handler
 get_new_handler() _NOEXCEPT
 {
-    return __sync_fetch_and_add(&__new_handler, nullptr);
+    return __libcpp_atomic_load(&__new_handler);
 }
 
 } // namespace std


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to