[libcxx-commits] [libcxx] [libc++] Simplify __memory/shared_count.h a bit (PR #160048)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Sep 22 06:01:55 PDT 2025
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/160048
>From 96709ddbaceb5ec9ca319347fac5951d7fe2849d Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Mon, 22 Sep 2025 10:13:54 +0200
Subject: [PATCH] [libc++] Simplify __memory/shared_count.h a bit
---
libcxx/include/__memory/shared_count.h | 39 +++++--------------
libcxx/include/__mutex/once_flag.h | 12 +++++-
libcxx/include/mutex | 4 ++
.../test/libcxx/transitive_includes/cxx26.csv | 1 -
4 files changed, 23 insertions(+), 33 deletions(-)
diff --git a/libcxx/include/__memory/shared_count.h b/libcxx/include/__memory/shared_count.h
index dad20bcabd7ea..9457bc5550e6d 100644
--- a/libcxx/include/__memory/shared_count.h
+++ b/libcxx/include/__memory/shared_count.h
@@ -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 @@ class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
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 __value + 1;
+#endif
+ }
};
class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {
diff --git a/libcxx/include/__mutex/once_flag.h b/libcxx/include/__mutex/once_flag.h
index e384c15a9f9b6..808b1ea99cc0b 100644
--- a/libcxx/include/__mutex/once_flag.h
+++ b/libcxx/include/__mutex/once_flag.h
@@ -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>
diff --git a/libcxx/include/mutex b/libcxx/include/mutex
index 58474e0ca2b7a..25a5283b2cffd 100644
--- a/libcxx/include/mutex
+++ b/libcxx/include/mutex
@@ -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>
diff --git a/libcxx/test/libcxx/transitive_includes/cxx26.csv b/libcxx/test/libcxx/transitive_includes/cxx26.csv
index 5f906338f4b7c..81c8c41d88756 100644
--- a/libcxx/test/libcxx/transitive_includes/cxx26.csv
+++ b/libcxx/test/libcxx/transitive_includes/cxx26.csv
@@ -669,7 +669,6 @@ mutex ctime
mutex limits
mutex ratio
mutex tuple
-mutex typeinfo
mutex version
new version
numbers version
More information about the libcxx-commits
mailing list