Index: include/atomic =================================================================== --- include/atomic +++ include/atomic @@ -89,6 +89,7 @@ template struct atomic { + static constexpr bool is_always_lock_free; bool is_lock_free() const volatile noexcept; bool is_lock_free() const noexcept; void store(T desr, memory_order m = memory_order_seq_cst) volatile noexcept; @@ -127,6 +128,7 @@ template <> struct atomic { + static constexpr bool is_always_lock_free; bool is_lock_free() const volatile noexcept; bool is_lock_free() const noexcept; void store(integral desr, memory_order m = memory_order_seq_cst) volatile noexcept; @@ -202,6 +204,7 @@ template struct atomic { + static constexpr bool is_always_lock_free; bool is_lock_free() const volatile noexcept; bool is_lock_free() const noexcept; void store(T* desr, memory_order m = memory_order_seq_cst) volatile noexcept; @@ -825,6 +828,66 @@ return __y; } +#define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE +#define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE +#define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE +#define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE +#define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE +#define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE +#define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE +#define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE +#define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE +#define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE + +#if defined(__cpp_lib_atomic_is_always_lock_free) +# if defined(__LLVM_LOCK_FREE_IS_SIZE_BASED) && __LLVM_LOCK_FREE_IS_SIZE_BASED == 1 +# define _LIBCPP_LOCK_FREE_IS_SIZE_BASED +template _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = false; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<1> = __LLVM_ATOMIC_1_BYTES_LOCK_FREE == 2; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<2> = __LLVM_ATOMIC_2_BYTES_LOCK_FREE == 2; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<4> = __LLVM_ATOMIC_4_BYTES_LOCK_FREE == 2; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<8> = __LLVM_ATOMIC_8_BYTES_LOCK_FREE == 2; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<16> = __LLVM_ATOMIC_16_BYTES_LOCK_FREE == 2; +# else +template _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = false; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_BOOL_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_CHAR_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_CHAR_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_CHAR_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_CHAR16_T_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_CHAR32_T_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_WCHAR_T_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_SHORT_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_SHORT_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_INT_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_INT_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_LONG_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_LONG_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_LLONG_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_LLONG_LOCK_FREE; +template _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_POINTER_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = 2 == ATOMIC_POINTER_LOCK_FREE; +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = +# if defined(__GCC_ATOMIC_FLOAT_LOCK_FREE) && 2 == __GCC_ATOMIC_FLOAT_LOCK_FREE + true; +# else + false; +# endif +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = +# if defined(__GCC_ATOMIC_DOUBLE_LOCK_FREE) && 2 == __GCC_ATOMIC_DOUBLE_LOCK_FREE + true; +# else + false; +# endif +template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = +# if defined(__GCC_ATOMIC_LDOUBLE_LOCK_FREE) && 2 == __GCC_ATOMIC_LDOUBLE_LOCK_FREE + true; +# else + false; +# endif +# endif +#endif + // general atomic template ::value && !is_same<_Tp, bool>::value> @@ -832,6 +895,14 @@ { mutable _Atomic(_Tp) __a_; +#if defined(__cpp_lib_atomic_is_always_lock_free) +# if defined(_LIBCPP_LOCK_FREE_IS_SIZE_BASED) + static _LIBCPP_CONSTEXPR bool is_always_lock_free = __libcpp_always_lock_free; +# else + static _LIBCPP_CONSTEXPR bool is_always_lock_free = __libcpp_always_lock_free<_Tp>; +# endif +#endif + _LIBCPP_INLINE_VISIBILITY bool is_lock_free() const volatile _NOEXCEPT { @@ -1789,17 +1860,6 @@ #define ATOMIC_FLAG_INIT {false} #define ATOMIC_VAR_INIT(__v) {__v} -#define ATOMIC_BOOL_LOCK_FREE __GCC_ATOMIC_BOOL_LOCK_FREE -#define ATOMIC_CHAR_LOCK_FREE __GCC_ATOMIC_CHAR_LOCK_FREE -#define ATOMIC_CHAR16_T_LOCK_FREE __GCC_ATOMIC_CHAR16_T_LOCK_FREE -#define ATOMIC_CHAR32_T_LOCK_FREE __GCC_ATOMIC_CHAR32_T_LOCK_FREE -#define ATOMIC_WCHAR_T_LOCK_FREE __GCC_ATOMIC_WCHAR_T_LOCK_FREE -#define ATOMIC_SHORT_LOCK_FREE __GCC_ATOMIC_SHORT_LOCK_FREE -#define ATOMIC_INT_LOCK_FREE __GCC_ATOMIC_INT_LOCK_FREE -#define ATOMIC_LONG_LOCK_FREE __GCC_ATOMIC_LONG_LOCK_FREE -#define ATOMIC_LLONG_LOCK_FREE __GCC_ATOMIC_LLONG_LOCK_FREE -#define ATOMIC_POINTER_LOCK_FREE __GCC_ATOMIC_POINTER_LOCK_FREE - _LIBCPP_END_NAMESPACE_STD #endif // _LIBCPP_ATOMIC Index: test/std/atomics/atomics.lockfree/isalwayslockfree.pass.cpp =================================================================== --- /dev/null +++ test/std/atomics/atomics.lockfree/isalwayslockfree.pass.cpp @@ -0,0 +1,97 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// UNSUPPORTED: libcpp-has-no-threads, c++98, c++03, c++11, c++14 + +// + +// static constexpr bool is_always_lock_free; + +#include +#include + +template void checkAlwaysLockFree() { + if (std::atomic::is_always_lock_free) + assert(std::atomic().is_lock_free()); +} + +int main() +{ +// structs and unions can't be defined in the template invocation. +// Work around this with a typedef. +#define CHECK_ALWAYS_LOCK_FREE(T) \ + do { \ + typedef T type; \ + checkAlwaysLockFree(); \ + } while (0) + + CHECK_ALWAYS_LOCK_FREE(bool); + CHECK_ALWAYS_LOCK_FREE(char); + CHECK_ALWAYS_LOCK_FREE(signed char); + CHECK_ALWAYS_LOCK_FREE(unsigned char); + CHECK_ALWAYS_LOCK_FREE(char16_t); + CHECK_ALWAYS_LOCK_FREE(char32_t); + CHECK_ALWAYS_LOCK_FREE(wchar_t); + CHECK_ALWAYS_LOCK_FREE(short); + CHECK_ALWAYS_LOCK_FREE(unsigned short); + CHECK_ALWAYS_LOCK_FREE(int); + CHECK_ALWAYS_LOCK_FREE(unsigned int); + CHECK_ALWAYS_LOCK_FREE(long); + CHECK_ALWAYS_LOCK_FREE(unsigned long); + CHECK_ALWAYS_LOCK_FREE(long long); + CHECK_ALWAYS_LOCK_FREE(unsigned long long); + CHECK_ALWAYS_LOCK_FREE(std::nullptr_t); + CHECK_ALWAYS_LOCK_FREE(void*); + CHECK_ALWAYS_LOCK_FREE(float); + CHECK_ALWAYS_LOCK_FREE(double); + CHECK_ALWAYS_LOCK_FREE(long double); + CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(1 * sizeof(int))))); + CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(2 * sizeof(int))))); + CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(4 * sizeof(int))))); + CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(16 * sizeof(int))))); + CHECK_ALWAYS_LOCK_FREE(int __attribute__((vector_size(32 * sizeof(int))))); + CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(1 * sizeof(float))))); + CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(2 * sizeof(float))))); + CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(4 * sizeof(float))))); + CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(16 * sizeof(float))))); + CHECK_ALWAYS_LOCK_FREE(float __attribute__((vector_size(32 * sizeof(float))))); + CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(1 * sizeof(double))))); + CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(2 * sizeof(double))))); + CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(4 * sizeof(double))))); + CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(16 * sizeof(double))))); + CHECK_ALWAYS_LOCK_FREE(double __attribute__((vector_size(32 * sizeof(double))))); + CHECK_ALWAYS_LOCK_FREE(struct{}); + CHECK_ALWAYS_LOCK_FREE(struct{ int i; }); + CHECK_ALWAYS_LOCK_FREE(struct{ int i[2]; }); + CHECK_ALWAYS_LOCK_FREE(struct{ long long int i[2]; }); + CHECK_ALWAYS_LOCK_FREE(struct{ long long int i[4]; }); + CHECK_ALWAYS_LOCK_FREE(struct{ long long int i[8]; }); + CHECK_ALWAYS_LOCK_FREE(struct{ long long int i[16]; }); + CHECK_ALWAYS_LOCK_FREE(struct{ char c; /* padding */ long long int i; }); + CHECK_ALWAYS_LOCK_FREE(union{ int i; float f; }); + + // C macro and static constexpr must be consistent. + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_BOOL_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_CHAR16_T_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_CHAR32_T_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_WCHAR_T_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE)); + static_assert(std::atomic::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE)); +} Index: test/std/atomics/atomics.lockfree/lockfree.pass.cpp =================================================================== --- test/std/atomics/atomics.lockfree/lockfree.pass.cpp +++ test/std/atomics/atomics.lockfree/lockfree.pass.cpp @@ -11,6 +11,7 @@ // +// #define ATOMIC_BOOL_LOCK_FREE unspecified // #define ATOMIC_CHAR_LOCK_FREE unspecified // #define ATOMIC_CHAR16_T_LOCK_FREE unspecified // #define ATOMIC_CHAR32_T_LOCK_FREE unspecified @@ -19,12 +20,16 @@ // #define ATOMIC_INT_LOCK_FREE unspecified // #define ATOMIC_LONG_LOCK_FREE unspecified // #define ATOMIC_LLONG_LOCK_FREE unspecified +// #define ATOMIC_POINTER_LOCK_FREE unspecified #include #include int main() { + assert(ATOMIC_BOOL_LOCK_FREE == 0 || + ATOMIC_BOOL_LOCK_FREE == 1 || + ATOMIC_BOOL_LOCK_FREE == 2); assert(ATOMIC_CHAR_LOCK_FREE == 0 || ATOMIC_CHAR_LOCK_FREE == 1 || ATOMIC_CHAR_LOCK_FREE == 2); @@ -49,4 +54,7 @@ assert(ATOMIC_LLONG_LOCK_FREE == 0 || ATOMIC_LLONG_LOCK_FREE == 1 || ATOMIC_LLONG_LOCK_FREE == 2); + assert(ATOMIC_POINTER_LOCK_FREE == 0 || + ATOMIC_POINTER_LOCK_FREE == 1 || + ATOMIC_POINTER_LOCK_FREE == 2); }