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
70_LIBCPP_HIDE_FROM_ABI constexpr bool __has_native_atomic_wait_impl(size_t __size) {
71 switch (__size) {
72# define _LIBCPP_MAKE_CASE(n) \
73 case n: \
74 return true;
75 _LIBCPP_NATIVE_PLATFORM_WAIT_SIZES(_LIBCPP_MAKE_CASE)
76 default:
77 return false;
78# undef _LIBCPP_MAKE_CASE
79 };
80}
81
82template <class _Tp>
83concept __has_native_atomic_wait =
84 has_unique_object_representations_v<_Tp> && is_trivially_copyable_v<_Tp> &&
85 __has_native_atomic_wait_impl(sizeof(_Tp));
86
87# else // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
88
89template <class _Tp>
90concept __has_native_atomic_wait = is_same_v<_Tp, __cxx_contention_t>;
91
92# endif // _LIBCPP_ABI_ATOMIC_WAIT_NATIVE_BY_SIZE
93
94#endif // C++20
95
96_LIBCPP_END_NAMESPACE_STD
97
98#endif // _LIBCPP___ATOMIC_ATOMIC_WAITABLE_TRAITS_H
99