[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

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@307591 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Weiming Zhao
2017-07-10 21:02:54 +00:00
parent 6bfa7316ad
commit f7ed02c939
5 changed files with 46 additions and 12 deletions

31
include/__atomic_support Normal file
View File

@@ -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

View File

@@ -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(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);
}
}