Skip to content

Commit 96709dd

Browse files
committed
[libc++] Simplify __memory/shared_count.h a bit
1 parent 8323ff0 commit 96709dd

File tree

4 files changed

+23
-33
lines changed

4 files changed

+23
-33
lines changed

libcxx/include/__memory/shared_count.h

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,10 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2222
// NOTE: Relaxed and acq/rel atomics (for increment and decrement respectively)
2323
// should be sufficient for thread safety.
2424
// See https://llvm.org/PR22803
25-
#if (defined(__clang__) && __has_builtin(__atomic_add_fetch) && defined(__ATOMIC_RELAXED) && \
26-
defined(__ATOMIC_ACQ_REL)) || \
27-
defined(_LIBCPP_COMPILER_GCC)
28-
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 1
29-
#else
30-
# define _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT 0
31-
#endif
32-
33-
template <class _ValueType>
34-
inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_relaxed_load(_ValueType const* __value) {
35-
#if _LIBCPP_HAS_THREADS && defined(__ATOMIC_RELAXED) && \
36-
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
37-
return __atomic_load_n(__value, __ATOMIC_RELAXED);
38-
#else
39-
return *__value;
40-
#endif
41-
}
42-
43-
template <class _ValueType>
44-
inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {
45-
#if _LIBCPP_HAS_THREADS && defined(__ATOMIC_ACQUIRE) && \
46-
(__has_builtin(__atomic_load_n) || defined(_LIBCPP_COMPILER_GCC))
47-
return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
48-
#else
49-
return *__value;
50-
#endif
51-
}
5225

5326
template <class _Tp>
5427
inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _NOEXCEPT {
55-
#if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
28+
#if _LIBCPP_HAS_THREADS
5629
return __atomic_add_fetch(std::addressof(__t), 1, __ATOMIC_RELAXED);
5730
#else
5831
return __t += 1;
@@ -61,7 +34,7 @@ inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_increment(_Tp& __t) _N
6134

6235
template <class _Tp>
6336
inline _LIBCPP_HIDE_FROM_ABI _Tp __libcpp_atomic_refcount_decrement(_Tp& __t) _NOEXCEPT {
64-
#if _LIBCPP_HAS_BUILTIN_ATOMIC_SUPPORT && _LIBCPP_HAS_THREADS
37+
#if _LIBCPP_HAS_THREADS
6538
return __atomic_add_fetch(std::addressof(__t), -1, __ATOMIC_ACQ_REL);
6639
#else
6740
return __t -= 1;
@@ -95,7 +68,13 @@ class _LIBCPP_EXPORTED_FROM_ABI __shared_count {
9568
return false;
9669
}
9770
#endif
98-
_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT { return __libcpp_relaxed_load(&__shared_owners_) + 1; }
71+
_LIBCPP_HIDE_FROM_ABI long use_count() const _NOEXCEPT {
72+
#if _LIBCPP_HAS_THREADS
73+
return __atomic_load_n(&__shared_owners_, __ATOMIC_RELAXED) + 1;
74+
#else
75+
return __value + 1;
76+
#endif
77+
}
9978
};
10079

10180
class _LIBCPP_EXPORTED_FROM_ABI __shared_weak_count : private __shared_count {

libcxx/include/__mutex/once_flag.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
#define _LIBCPP___MUTEX_ONCE_FLAG_H
1111

1212
#include <__config>
13-
#include <__functional/invoke.h>
1413
#include <__memory/addressof.h>
15-
#include <__memory/shared_count.h> // __libcpp_acquire_load
1614
#include <__tuple/tuple_size.h>
15+
#include <__type_traits/invoke.h>
1716
#include <__utility/forward.h>
1817
#include <__utility/integer_sequence.h>
1918
#include <__utility/move.h>
@@ -118,6 +117,15 @@ void _LIBCPP_HIDE_FROM_ABI __call_once_proxy(void* __vp) {
118117

119118
_LIBCPP_EXPORTED_FROM_ABI void __call_once(volatile once_flag::_State_type&, void*, void (*)(void*));
120119

120+
template <class _ValueType>
121+
inline _LIBCPP_HIDE_FROM_ABI _ValueType __libcpp_acquire_load(_ValueType const* __value) {
122+
#if _LIBCPP_HAS_THREADS
123+
return __atomic_load_n(__value, __ATOMIC_ACQUIRE);
124+
#else
125+
return *__value;
126+
#endif
127+
}
128+
121129
#ifndef _LIBCPP_CXX03_LANG
122130

123131
template <class _Callable, class... _Args>

libcxx/include/mutex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ _LIBCPP_END_NAMESPACE_STD
500500

501501
_LIBCPP_POP_MACROS
502502

503+
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23
504+
# include <typeinfo>
505+
# endif
506+
503507
# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
504508
# include <atomic>
505509
# include <concepts>

libcxx/test/libcxx/transitive_includes/cxx26.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,6 @@ mutex ctime
669669
mutex limits
670670
mutex ratio
671671
mutex tuple
672-
mutex typeinfo
673672
mutex version
674673
new version
675674
numbers version

0 commit comments

Comments
 (0)