xref: /llvm-project/libcxx/test/std/atomics/atomics.lockfree/is_always_lock_free.pass.cpp (revision f4db221258cb44a8f9804ce852c0403328de39b2)
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 // XFAIL: !has-64-bit-atomics
11 
12 // <atomic>
13 //
14 // template <class T>
15 // class atomic;
16 //
17 // static constexpr bool is_always_lock_free;
18 
19 // Ignore diagnostic about vector types changing the ABI on some targets, since
20 // that is irrelevant for this test.
21 // ADDITIONAL_COMPILE_FLAGS(gcc-style-warnings): -Wno-psabi
22 
23 #include <atomic>
24 #include <cassert>
25 #include <concepts>
26 #include <cstddef>
27 
28 #include "test_macros.h"
29 #include "atomic_helpers.h"
30 
31 template <typename T>
32 void check_always_lock_free(std::atomic<T> const& a) {
33   using InfoT = LockFreeStatusInfo<T>;
34 
35   constexpr auto is_always_lock_free = std::atomic<T>::is_always_lock_free;
36   ASSERT_SAME_TYPE(decltype(is_always_lock_free), bool const);
37 
38   // If we know the status of T for sure, validate the exact result of the function.
39   if constexpr (InfoT::status_known) {
40     constexpr LockFreeStatus known_status = InfoT::value;
41     if constexpr (known_status == LockFreeStatus::always) {
42       static_assert(is_always_lock_free, "is_always_lock_free is inconsistent with known lock-free status");
43       assert(a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
44     } else if constexpr (known_status == LockFreeStatus::never) {
45       static_assert(!is_always_lock_free, "is_always_lock_free is inconsistent with known lock-free status");
46       assert(!a.is_lock_free() && "is_lock_free() is inconsistent with known lock-free status");
47     } else {
48       assert(a.is_lock_free() || !a.is_lock_free()); // This is kinda dumb, but we might as well call the function once.
49     }
50   }
51 
52   // In all cases, also sanity-check it based on the implication always-lock-free => lock-free.
53   if (is_always_lock_free) {
54     auto is_lock_free = a.is_lock_free();
55     ASSERT_SAME_TYPE(decltype(is_lock_free), bool);
56     assert(is_lock_free);
57   }
58   ASSERT_NOEXCEPT(a.is_lock_free());
59 }
60 
61 #define CHECK_ALWAYS_LOCK_FREE(T)                                                                                      \
62   do {                                                                                                                 \
63     typedef T type;                                                                                                    \
64     type obj{};                                                                                                        \
65     std::atomic<type> a(obj);                                                                                          \
66     check_always_lock_free(a);                                                                                         \
67   } while (0)
68 
69 void test() {
70   char c = 'x';
71   check_always_lock_free(std::atomic<char>(c));
72 
73   int i = 0;
74   check_always_lock_free(std::atomic<int>(i));
75 
76   float f = 0.f;
77   check_always_lock_free(std::atomic<float>(f));
78 
79   int* p = &i;
80   check_always_lock_free(std::atomic<int*>(p));
81 
82   CHECK_ALWAYS_LOCK_FREE(bool);
83   CHECK_ALWAYS_LOCK_FREE(char);
84   CHECK_ALWAYS_LOCK_FREE(signed char);
85   CHECK_ALWAYS_LOCK_FREE(unsigned char);
86 #if TEST_STD_VER > 17 && defined(__cpp_char8_t)
87   CHECK_ALWAYS_LOCK_FREE(char8_t);
88 #endif
89   CHECK_ALWAYS_LOCK_FREE(char16_t);
90   CHECK_ALWAYS_LOCK_FREE(char32_t);
91   CHECK_ALWAYS_LOCK_FREE(wchar_t);
92   CHECK_ALWAYS_LOCK_FREE(short);
93   CHECK_ALWAYS_LOCK_FREE(unsigned short);
94   CHECK_ALWAYS_LOCK_FREE(int);
95   CHECK_ALWAYS_LOCK_FREE(unsigned int);
96   CHECK_ALWAYS_LOCK_FREE(long);
97   CHECK_ALWAYS_LOCK_FREE(unsigned long);
98   CHECK_ALWAYS_LOCK_FREE(long long);
99   CHECK_ALWAYS_LOCK_FREE(unsigned long long);
100   CHECK_ALWAYS_LOCK_FREE(std::nullptr_t);
101   CHECK_ALWAYS_LOCK_FREE(void*);
102   CHECK_ALWAYS_LOCK_FREE(float);
103   CHECK_ALWAYS_LOCK_FREE(double);
104   CHECK_ALWAYS_LOCK_FREE(long double);
105 #if __has_attribute(vector_size) && defined(_LIBCPP_VERSION)
106   CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(1 * sizeof(int)))));
107   CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(2 * sizeof(int)))));
108   CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(4 * sizeof(int)))));
109   CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(16 * sizeof(int)))));
110   CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(32 * sizeof(int)))));
111   CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(1 * sizeof(float)))));
112   CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(2 * sizeof(float)))));
113   CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(4 * sizeof(float)))));
114   CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(16 * sizeof(float)))));
115   CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(32 * sizeof(float)))));
116   CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(1 * sizeof(double)))));
117   CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(2 * sizeof(double)))));
118   CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(4 * sizeof(double)))));
119   CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(16 * sizeof(double)))));
120   CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(32 * sizeof(double)))));
121 #endif // __has_attribute(vector_size) && defined(_LIBCPP_VERSION)
122   CHECK_ALWAYS_LOCK_FREE(struct Empty{});
123   CHECK_ALWAYS_LOCK_FREE(struct OneInt { int i; });
124   CHECK_ALWAYS_LOCK_FREE(struct IntArr2 { int i[2]; });
125   CHECK_ALWAYS_LOCK_FREE(struct FloatArr3 { float i[3]; });
126   CHECK_ALWAYS_LOCK_FREE(struct LLIArr2 { long long int i[2]; });
127   CHECK_ALWAYS_LOCK_FREE(struct LLIArr4 { long long int i[4]; });
128   CHECK_ALWAYS_LOCK_FREE(struct LLIArr8 { long long int i[8]; });
129   CHECK_ALWAYS_LOCK_FREE(struct LLIArr16 { long long int i[16]; });
130   CHECK_ALWAYS_LOCK_FREE(struct Padding {
131     char c; /* padding */
132     long long int i;
133   });
134   CHECK_ALWAYS_LOCK_FREE(union IntFloat {
135     int i;
136     float f;
137   });
138   CHECK_ALWAYS_LOCK_FREE(enum class CharEnumClass : char{foo});
139 
140   // C macro and static constexpr must be consistent.
141   enum class CharEnumClass : char { foo };
142   static_assert(std::atomic<bool>::is_always_lock_free == (2 == ATOMIC_BOOL_LOCK_FREE), "");
143   static_assert(std::atomic<char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
144   static_assert(std::atomic<CharEnumClass>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
145   static_assert(std::atomic<signed char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
146   static_assert(std::atomic<unsigned char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE), "");
147 #if TEST_STD_VER > 17 && defined(__cpp_char8_t)
148   static_assert(std::atomic<char8_t>::is_always_lock_free == (2 == ATOMIC_CHAR8_T_LOCK_FREE), "");
149 #endif
150   static_assert(std::atomic<char16_t>::is_always_lock_free == (2 == ATOMIC_CHAR16_T_LOCK_FREE), "");
151   static_assert(std::atomic<char32_t>::is_always_lock_free == (2 == ATOMIC_CHAR32_T_LOCK_FREE), "");
152   static_assert(std::atomic<wchar_t>::is_always_lock_free == (2 == ATOMIC_WCHAR_T_LOCK_FREE), "");
153   static_assert(std::atomic<short>::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE), "");
154   static_assert(std::atomic<unsigned short>::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE), "");
155   static_assert(std::atomic<int>::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE), "");
156   static_assert(std::atomic<unsigned int>::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE), "");
157   static_assert(std::atomic<long>::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE), "");
158   static_assert(std::atomic<unsigned long>::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE), "");
159   static_assert(std::atomic<long long>::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE), "");
160   static_assert(std::atomic<unsigned long long>::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE), "");
161   static_assert(std::atomic<void*>::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE), "");
162   static_assert(std::atomic<std::nullptr_t>::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE), "");
163 
164 #if TEST_STD_VER >= 20
165   static_assert(std::atomic_signed_lock_free::is_always_lock_free, "");
166   static_assert(std::atomic_unsigned_lock_free::is_always_lock_free, "");
167 #endif
168 }
169 
170 int main(int, char**) {
171   test();
172   return 0;
173 }
174