[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

llvm-svn: 313694
Cr-Mirrored-From: sso://chromium.googlesource.com/_direct/external/github.com/llvm/llvm-project
Cr-Mirrored-Commit: fbfaec7089b8bfc1e3ef91dfbc50b4b7cf7a79e7
diff --git a/src/include/refstring.h b/src/include/refstring.h
index f0d5b44..702f2b7 100644
--- a/src/include/refstring.h
+++ b/src/include/refstring.h
@@ -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 @@
     : __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 @@
     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 @@
 __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);
         }
     }