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