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// UNSUPPORTED: c++03, c++11, c++14
10
11// <atomic>
12
13// static constexpr bool is_always_lock_free;
14
15#include <atomic>
16#include <cassert>
17#include <cstddef>
18
19#include "test_macros.h"
20
21template <typename T>
22void checkAlwaysLockFree() {
23 if (std::atomic<T>::is_always_lock_free) {
24 assert(std::atomic<T>().is_lock_free());
25 }
26}
27
28void run()
29{
30// structs and unions can't be defined in the template invocation.
31// Work around this with a typedef.
32#define CHECK_ALWAYS_LOCK_FREE(T) \
33 do { \
34 typedef T type; \
35 checkAlwaysLockFree<type>(); \
36 } while (0)
37
38 CHECK_ALWAYS_LOCK_FREE(bool);
39 CHECK_ALWAYS_LOCK_FREE(char);
40 CHECK_ALWAYS_LOCK_FREE(signed char);
41 CHECK_ALWAYS_LOCK_FREE(unsigned char);
42#if TEST_STD_VER > 17 && defined(__cpp_char8_t)
43 CHECK_ALWAYS_LOCK_FREE(char8_t);
44#endif
45 CHECK_ALWAYS_LOCK_FREE(char16_t);
46 CHECK_ALWAYS_LOCK_FREE(char32_t);
47 CHECK_ALWAYS_LOCK_FREE(wchar_t);
48 CHECK_ALWAYS_LOCK_FREE(short);
49 CHECK_ALWAYS_LOCK_FREE(unsigned short);
50 CHECK_ALWAYS_LOCK_FREE(int);
51 CHECK_ALWAYS_LOCK_FREE(unsigned int);
52 CHECK_ALWAYS_LOCK_FREE(long);
53 CHECK_ALWAYS_LOCK_FREE(unsigned long);
54 CHECK_ALWAYS_LOCK_FREE(long long);
55 CHECK_ALWAYS_LOCK_FREE(unsigned long long);
56 CHECK_ALWAYS_LOCK_FREE(std::nullptr_t);
57 CHECK_ALWAYS_LOCK_FREE(void*);
58 CHECK_ALWAYS_LOCK_FREE(float);
59 CHECK_ALWAYS_LOCK_FREE(double);
60 CHECK_ALWAYS_LOCK_FREE(long double);
61#if __has_attribute(vector_size) && defined(_LIBCPP_VERSION)
62 CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(1 * sizeof(int)))));
63 CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(2 * sizeof(int)))));
64 CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(4 * sizeof(int)))));
65 CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(16 * sizeof(int)))));
66 CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(32 * sizeof(int)))));
67 CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(1 * sizeof(float)))));
68 CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(2 * sizeof(float)))));
69 CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(4 * sizeof(float)))));
70 CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(16 * sizeof(float)))));
71 CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(32 * sizeof(float)))));
72 CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(1 * sizeof(double)))));
73 CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(2 * sizeof(double)))));
74 CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(4 * sizeof(double)))));
75 CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(16 * sizeof(double)))));
76 CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(32 * sizeof(double)))));
77#endif // __has_attribute(vector_size) && defined(_LIBCPP_VERSION)
78 CHECK_ALWAYS_LOCK_FREE(struct Empty {});
79 CHECK_ALWAYS_LOCK_FREE(struct OneInt { int i; });
80 CHECK_ALWAYS_LOCK_FREE(struct IntArr2 { int i[2]; });
81 CHECK_ALWAYS_LOCK_FREE(struct FloatArr3 { float i[3]; });
82 CHECK_ALWAYS_LOCK_FREE(struct LLIArr2 { long long int i[2]; });
83 CHECK_ALWAYS_LOCK_FREE(struct LLIArr4 { long long int i[4]; });
84 CHECK_ALWAYS_LOCK_FREE(struct LLIArr8 { long long int i[8]; });
85 CHECK_ALWAYS_LOCK_FREE(struct LLIArr16 { long long int i[16]; });
86 CHECK_ALWAYS_LOCK_FREE(struct Padding { char c; /* padding */ long long int i; });
87 CHECK_ALWAYS_LOCK_FREE(union IntFloat { int i; float f; });
88 CHECK_ALWAYS_LOCK_FREE(enum class CharEnumClass : char { foo });
89
90 // C macro and static constexpr must be consistent.
91 enum class CharEnumClass : char { foo };
92 static_assert(std::atomic<bool>::is_always_lock_free == (2 == ATOMIC_BOOL_LOCK_FREE), "");
93 static_assert(std::atomic<char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
94 static_assert(std::atomic<CharEnumClass>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
95 static_assert(std::atomic<signed char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
96 static_assert(std::atomic<unsigned char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
97#if TEST_STD_VER > 17 && defined(__cpp_char8_t)
98 static_assert(std::atomic<char8_t>::is_always_lock_free == (2 == ATOMIC_CHAR8_T_LOCK_FREE), "");
99#endif
100 static_assert(std::atomic<char16_t>::is_always_lock_free == (2 == ATOMIC_CHAR16_T_LOCK_FREE), "");
101 static_assert(std::atomic<char32_t>::is_always_lock_free == (2 == ATOMIC_CHAR32_T_LOCK_FREE), "");
102 static_assert(std::atomic<wchar_t>::is_always_lock_free == (2 == ATOMIC_WCHAR_T_LOCK_FREE), "");
103 static_assert(std::atomic<short>::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE), "");
104 static_assert(std::atomic<unsigned short>::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE), "");
105 static_assert(std::atomic<int>::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE), "");
106 static_assert(std::atomic<unsigned int>::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE), "");
107 static_assert(std::atomic<long>::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE), "");
108 static_assert(std::atomic<unsigned long>::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE), "");
109 static_assert(std::atomic<long long>::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE), "");
110 static_assert(std::atomic<unsigned long long>::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE), "");
111 static_assert(std::atomic<void*>::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE), "");
112 static_assert(std::atomic<std::nullptr_t>::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE), "");
113
114#if TEST_STD_VER >= 20
115 static_assert(std::atomic_signed_lock_free::is_always_lock_free, "");
116 static_assert(std::atomic_unsigned_lock_free::is_always_lock_free, "");
117#endif
118}
119
120int main(int, char**) { run(); return 0; }
121

source code of libcxx/test/std/atomics/atomics.lockfree/isalwayslockfree.pass.cpp