[libcxx] r307591 - [libc++] Refactoring __sync_* builtins; NFC

Weiming Zhao via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 10 14:02:54 PDT 2017


Author: weimingz
Date: Mon Jul 10 14:02:54 2017
New Revision: 307591

URL: http://llvm.org/viewvc/llvm-project?rev=307591&view=rev
Log:
[libc++] Refactoring __sync_* builtins; NFC

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

Added:
    libcxx/trunk/include/__atomic_support
Modified:
    libcxx/trunk/include/__refstring
    libcxx/trunk/src/locale.cpp
    libcxx/trunk/src/support/runtime/exception_fallback.ipp
    libcxx/trunk/src/support/runtime/new_handler_fallback.ipp

Added: libcxx/trunk/include/__atomic_support
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__atomic_support?rev=307591&view=auto
==============================================================================
--- libcxx/trunk/include/__atomic_support (added)
+++ libcxx/trunk/include/__atomic_support Mon Jul 10 14:02:54 2017
@@ -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<typename T>
+T __libcpp_sync_add_and_fetch(T *ptr, T value) {
+  return __sync_add_and_fetch(ptr, value);
+}
+
+template<typename T>
+T __libcpp_sync_fetch_and_add(T *ptr, T value) {
+  return __sync_fetch_and_add(ptr, value);
+}
+
+template<typename T>
+T __libcpp_sync_lock_test_and_set(T *ptr, T value) {
+  return __sync_lock_test_and_set(ptr, value);
+}
+
+#endif //_LIBCPP___ATOMIC_SUPPORT

Modified: libcxx/trunk/include/__refstring
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__refstring?rev=307591&r1=307590&r2=307591&view=diff
==============================================================================
--- libcxx/trunk/include/__refstring (original)
+++ libcxx/trunk/include/__refstring Mon Jul 10 14:02:54 2017
@@ -14,6 +14,7 @@
 #include <stdexcept>
 #include <cstddef>
 #include <cstring>
+#include <__atomic_support>
 #ifdef __APPLE__
 #include <dlfcn.h>
 #include <mach-o/dyld.h>
@@ -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_sync_add_and_fetch(&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_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);
         }
     }

Modified: libcxx/trunk/src/locale.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/locale.cpp?rev=307591&r1=307590&r2=307591&view=diff
==============================================================================
--- libcxx/trunk/src/locale.cpp (original)
+++ libcxx/trunk/src/locale.cpp Mon Jul 10 14:02:54 2017
@@ -667,7 +667,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<char>

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=307591&r1=307590&r2=307591&view=diff
==============================================================================
--- libcxx/trunk/src/support/runtime/exception_fallback.ipp (original)
+++ libcxx/trunk/src/support/runtime/exception_fallback.ipp Mon Jul 10 14:02:54 2017
@@ -9,6 +9,7 @@
 //===----------------------------------------------------------------------===//
 
 #include <cstdio>
+#include <__atomic_support>
 
 namespace std {
 
@@ -20,13 +21,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_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

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=307591&r1=307590&r2=307591&view=diff
==============================================================================
--- libcxx/trunk/src/support/runtime/new_handler_fallback.ipp (original)
+++ libcxx/trunk/src/support/runtime/new_handler_fallback.ipp Mon Jul 10 14:02:54 2017
@@ -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_hand
 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>(&__new_handler, nullptr);
 }
 
 } // namespace std




More information about the cfe-commits mailing list