[libc++] Simplify __memory/shared_count.h a bit (#160048)

This removes a few checks that aren't required anymore and moves some
code around to the places where it's actually used.
This commit is contained in:
Nikolas Klauser 2025-09-24 10:40:36 +02:00 committed by GitHub
parent 330522e64e
commit b5bb50d09a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 34 deletions

View File

@ -22,37 +22,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
// should be sufficient for thread safety.
// See https://llvm.org/PR22803
#if (defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && \
defined(__ATOMIC_ACQ_REL)) || \
defined(_LIBCPP_COMPILER_GCC)
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 1
#else
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 0
#endif
template <class _ValueType>
inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
#if _LIBCPP_HAS_THREADS && defined(__ATOMIC_RELAXED) && \
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
return __atomic_load_n(__value, __ATOMIC_RELAXED);
#else
return *__value;
#endif
}
template <class _ValueType>
inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {
#if _LIBCPP_HAS_THREADS && defined(__ATOMIC_ACQUIRE) && \
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
#else
return *__value;
#endif
}
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {
#if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
#if _LIBCPP_HAS_THREADS
return __atomic_add_fetch(std::addressof(__t), 1, __ATOMIC_RELAXED);
#else
return __t += 1;
@ -61,7 +34,7 @@ inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _N
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {
#if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
#if _LIBCPP_HAS_THREADS
return __atomic_add_fetch(std::addressof(__t), -1, __ATOMIC_ACQ_REL);
#else
return __t -= 1;
@ -95,7 +68,13 @@ public:
return false;
}
#endif
_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __libcpp_relaxed_load(&__shared_owners_) + 1; }
_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT {
#if _LIBCPP_HAS_THREADS
return __atomic_load_n(&__shared_owners_, __ATOMIC_RELAXED) + 1;
#else
return __shared_owners_ + 1;
#endif
}
};
class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {

View File

@ -10,10 +10,9 @@
#define _LIBCPP___MUTEX_ONCE_FLAG_H
#include <__config>
#include <__functional/invoke.h>
#include <__memory/addressof.h>
#include <__memory/shared_count.h> // __libcpp_acquire_load
#include <__tuple/tuple_size.h>
#include <__type_traits/invoke.h>
#include <__utility/forward.h>
#include <__utility/integer_sequence.h>
#include <__utility/move.h>
@ -118,6 +117,15 @@ void _LIBCPP_HIDE_FROM_ABI __call_once_proxy(void* __vp) {
_LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile once_flag::_State_type&, void*, void (*)(void*));
template <class _ValueType>
inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {
#if _LIBCPP_HAS_THREADS
return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
#else
return *__value;
#endif
}
#ifndef _LIBCPP_CXX03_LANG
template <class _Callable, class... _Args>

View File

@ -500,6 +500,10 @@ _LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23
# include <typeinfo>
# endif
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
# include <atomic>
# include <concepts>
@ -513,7 +517,6 @@ _LIBCPP_POP_MACROS
# include <stdexcept>
# include <system_error>
# include <type_traits>
# include <typeinfo>
# endif
#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)

View File

@ -669,7 +669,6 @@ mutex ctime
mutex limits
mutex ratio
mutex tuple
mutex typeinfo
mutex version
new version
numbers version

1 algorithm cctype
669 mutex limits
670 mutex ratio
671 mutex tuple
mutex typeinfo
672 mutex version
673 new version
674 numbers version