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_LIBCPP_BEGIN_NAMESPACE_STD
28
29#if _LIBCPP_STD_VER >= 20
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>
48concept __atomic_waitable = requires(const _Tp __t, memory_order __order) {
49 typename __atomic_waitable_traits<__decay_t<_Tp> >::__value_type;
50 { __atomic_waitable_traits<__decay_t<_Tp> >::__atomic_load(__t, __order) };
51 { __atomic_waitable_traits<__decay_t<_Tp> >::__atomic_contention_address(__t) };
52};
53
54# ifdef __linux__
55# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(4)
56# elif defined(__APPLE__)
57# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) \
58 _APPLY(4) \
59 _APPLY(8)
60# elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
61# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(8)
62# elif defined(_WIN32)
63# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(8)
64# else
65# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(sizeof(__cxx_contention_t))
66# endif // __linux__
67
68// concepts defines the types are supported natively by the platform's wait
69
70# if defined(_LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE)
71
72template <class _Tp>
73_LIBCPP_HIDE_FROM_ABI constexpr bool __has_native_atomic_wait_impl() {
74 if (alignof(_Tp) % sizeof(_Tp) != 0)
75 return false;
76 switch (sizeof(_Tp)) {
77# define _LIBCPP_MAKE_CASE(n) \
78 case n: \
79 return true;
80 _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_LIBCPP_MAKE_CASE)
81 default:
82 return false;
83# undef _LIBCPP_MAKE_CASE
84 };
85}
86
87template <class _Tp>
88concept __has_native_atomic_wait =
89 has_unique_object_representations_v<_Tp> && is_trivially_copyable_v<_Tp> &&
90 std::__has_native_atomic_wait_impl<_Tp>();
91
92# else // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
93
94template <class _Tp>
95concept __has_native_atomic_wait = is_same_v<_Tp, __cxx_contention_t>;
96
97# endif // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
98
99#endif // C++20
100
101_LIBCPP_END_NAMESPACE_STD
102
103#endif // _LIBCPP___ATOMIC_ATOMIC_WAITABLE_TRAITS_H