1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___ATOMIC_ATOMIC_WAITABLE_TRAITS_H
10#define _LIBCPP___ATOMIC_ATOMIC_WAITABLE_TRAITS_H
11
12#include <__atomic/contention_t.h>
13#include <__atomic/memory_order.h>
14#include <__config>
15#include <__type_traits/decay.h>
16#include <__type_traits/has_unique_object_representation.h>
17#include <__type_traits/is_same.h>
18#include <__type_traits/is_trivially_copyable.h>
19#include <__type_traits/void_t.h>
20#include <__utility/declval.h>
21#include <cstring>
22
23#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24# pragma GCC system_header
25#endif
26
27#if _LIBCPP_STD_VER >= 17
28
29_LIBCPP_BEGIN_NAMESPACE_STD
30
31// The customisation points to enable the following functions:
32// - __atomic_wait
33// - __atomic_wait_unless
34// - __atomic_notify_one
35// - __atomic_notify_all
36template <class _Tp, class = void>
37struct __atomic_waitable_traits {
38 using __value_type _LIBCPP_NODEBUG = void;
39
40 template <class _AtomicWaitable>
41 static void __atomic_load(_AtomicWaitable&&, memory_order) = delete;
42
43 template <class _AtomicWaitable>
44 static void __atomic_contention_address(_AtomicWaitable&&) = delete;
45};
46
47template <class _Tp, class = void>
48constexpr inline bool __atomic_waitable = false;
49template <class _Tp>
50constexpr inline bool __atomic_waitable<
51 _Tp,
52 __void_t<typename __atomic_waitable_traits<__decay_t<_Tp> >::__value_type,
53 decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_load(
54 std::declval<const _Tp&>(), std::declval<memory_order>())),
55 decltype(__atomic_waitable_traits<__decay_t<_Tp> >::__atomic_contention_address(
56 std::declval<const _Tp&>())) > > = true;
57
58# ifdef __linux__
59# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(4)
60# elif defined(__APPLE__)
61# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) \
62 _APPLY(4) \
63 _APPLY(8)
64# elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
65# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(8)
66# elif defined(_WIN32)
67# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(8)
68# else
69# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(sizeof(__cxx_contention_t))
70# endif // __linux__
71
72// concepts defines the types are supported natively by the platform's wait
73
74# if defined(_LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE)
75
76template <class _Tp>
77_LIBCPP_HIDE_FROM_ABI constexpr bool __has_native_atomic_wait_impl() {
78 if (alignof(_Tp) % sizeof(_Tp) != 0)
79 return false;
80 switch (sizeof(_Tp)) {
81# define _LIBCPP_MAKE_CASE(n) \
82 case n: \
83 return true;
84 _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_LIBCPP_MAKE_CASE)
85 default:
86 return false;
87# undef _LIBCPP_MAKE_CASE
88 };
89}
90
91template <class _Tp>
92constexpr inline bool __has_native_atomic_wait =
93 has_unique_object_representations_v<_Tp> && is_trivially_copyable_v<_Tp> &&
94 std::__has_native_atomic_wait_impl<_Tp>();
95
96# else // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
97
98template <class _Tp>
99constexpr inline bool __has_native_atomic_wait = is_same_v<_Tp, __cxx_contention_t>;
100
101# endif // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
102
103_LIBCPP_END_NAMESPACE_STD
104
105#endif // _LIBCPP_STD_VER >= 17
106
107#endif // _LIBCPP___ATOMIC_ATOMIC_WAITABLE_TRAITS_H
108