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 <cstring>
20
21#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22# pragma GCC system_header
23#endif
24
25_LIBCPP_BEGIN_NAMESPACE_STD
26
27#if _LIBCPP_STD_VER >= 20
28
29// The customisation points to enable the following functions:
30// - __atomic_wait
31// - __atomic_wait_unless
32// - __atomic_notify_one
33// - __atomic_notify_all
34template <class _Tp, class = void>
35struct __atomic_waitable_traits {
36 using __value_type _LIBCPP_NODEBUG = void;
37
38 template <class _AtomicWaitable>
39 static void __atomic_load(_AtomicWaitable&&, memory_order) = delete;
40
41 template <class _AtomicWaitable>
42 static void __atomic_contention_address(_AtomicWaitable&&) = delete;
43};
44
45template <class _Tp>
46concept __atomic_waitable = requires(const _Tp __t, memory_order __order) {
47 typename __atomic_waitable_traits<__decay_t<_Tp> >::__value_type;
48 { __atomic_waitable_traits<__decay_t<_Tp> >::__atomic_load(__t, __order) };
49 { __atomic_waitable_traits<__decay_t<_Tp> >::__atomic_contention_address(__t) };
50};
51
52# ifdef __linux__
53# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(4)
54# elif defined(__APPLE__)
55# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) \
56 _APPLY(4) \
57 _APPLY(8)
58# elif defined(__FreeBSD__) && __SIZEOF_LONG__ == 8
59# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(8)
60# elif defined(_WIN32)
61# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(8)
62# else
63# define _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_APPLY) _APPLY(sizeof(__cxx_contention_t))
64# endif // __linux__
65
66// concepts defines the types are supported natively by the platform's wait
67
68# if defined(_LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE)
69
70template <class _Tp>
71_LIBCPP_HIDE_FROM_ABI constexpr bool __has_native_atomic_wait_impl() {
72 if (alignof(_Tp) % sizeof(_Tp) != 0)
73 return false;
74 switch (sizeof(_Tp)) {
75# define _LIBCPP_MAKE_CASE(n) \
76 case n: \
77 return true;
78 _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_LIBCPP_MAKE_CASE)
79 default:
80 return false;
81# undef _LIBCPP_MAKE_CASE
82 };
83}
84
85template <class _Tp>
86concept __has_native_atomic_wait =
87 has_unique_object_representations_v<_Tp> && is_trivially_copyable_v<_Tp> &&
88 std::__has_native_atomic_wait_impl<_Tp>();
89
90# else // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
91
92template <class _Tp>
93concept __has_native_atomic_wait = is_same_v<_Tp, __cxx_contention_t>;
94
95# endif // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
96
97#endif // C++20
98
99_LIBCPP_END_NAMESPACE_STD
100
101#endif // _LIBCPP___ATOMIC_ATOMIC_WAITABLE_TRAITS_H
102