Implement is_always_lock_free
Summary: This was voted into C++17 at the Jacksonville meeting. The final P0152R1 paper will be in the upcoming post-Jacksonville mailing, and is also available here: http://jfbastien.github.io/papers/P0152R1.html Reviewers: mclow.lists, rsmith Subscribers: cfe-commits Differential Revision: http://reviews.llvm.org/D17951 llvm-svn: 264413
This commit is contained in:
parent
9706dcf93b
commit
b95ee819f2
@ -17,6 +17,10 @@
|
||||
namespace std
|
||||
{
|
||||
|
||||
// feature test macro
|
||||
|
||||
#define __cpp_lib_atomic_is_always_lock_free // as specified by SG10
|
||||
|
||||
// order and consistency
|
||||
|
||||
typedef enum memory_order
|
||||
@ -89,6 +93,7 @@ void
|
||||
template <class T>
|
||||
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 +132,7 @@ struct atomic
|
||||
template <>
|
||||
struct atomic<integral>
|
||||
{
|
||||
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 +208,7 @@ struct atomic<integral>
|
||||
template <class T>
|
||||
struct atomic<T*>
|
||||
{
|
||||
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;
|
||||
@ -540,6 +547,11 @@ void atomic_signal_fence(memory_order m) noexcept;
|
||||
#error <atomic> is not implemented
|
||||
#endif
|
||||
|
||||
#if _LIBCPP_STD_VER > 14
|
||||
// FIXME: use the right feature test macro value as chose by SG10.
|
||||
# define __cpp_lib_atomic_is_always_lock_free 201603L
|
||||
#endif
|
||||
|
||||
_LIBCPP_BEGIN_NAMESPACE_STD
|
||||
|
||||
typedef enum memory_order
|
||||
@ -825,6 +837,17 @@ kill_dependency(_Tp __y) _NOEXCEPT
|
||||
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
|
||||
|
||||
// general atomic<T>
|
||||
|
||||
template <class _Tp, bool = is_integral<_Tp>::value && !is_same<_Tp, bool>::value>
|
||||
@ -832,6 +855,10 @@ struct __atomic_base // false
|
||||
{
|
||||
mutable _Atomic(_Tp) __a_;
|
||||
|
||||
#if defined(__cpp_lib_atomic_is_always_lock_free)
|
||||
static _LIBCPP_CONSTEXPR bool is_always_lock_free = __atomic_always_lock_free(sizeof(__a_), 0);
|
||||
#endif
|
||||
|
||||
_LIBCPP_INLINE_VISIBILITY
|
||||
bool is_lock_free() const volatile _NOEXCEPT
|
||||
{
|
||||
@ -920,6 +947,11 @@ private:
|
||||
#endif // _LIBCPP_HAS_NO_DELETED_FUNCTIONS
|
||||
};
|
||||
|
||||
#if defined(__cpp_lib_atomic_is_always_lock_free)
|
||||
template <class _Tp, bool __b>
|
||||
_LIBCPP_CONSTEXPR bool __atomic_base<_Tp, __b>::is_always_lock_free;
|
||||
#endif
|
||||
|
||||
// atomic<Integral>
|
||||
|
||||
template <class _Tp>
|
||||
@ -1789,17 +1821,6 @@ typedef atomic<uintmax_t> atomic_uintmax_t;
|
||||
#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
|
||||
|
||||
@ -0,0 +1,101 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
|
||||
// <atomic>
|
||||
|
||||
// static constexpr bool is_always_lock_free;
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
|
||||
#if !defined(__cpp_lib_atomic_is_always_lock_free)
|
||||
# error Feature test macro missing.
|
||||
#endif
|
||||
|
||||
template <typename T> void checkAlwaysLockFree() {
|
||||
if (std::atomic<T>::is_always_lock_free)
|
||||
assert(std::atomic<T>().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<type>(); \
|
||||
} 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<bool>::is_always_lock_free == (2 == ATOMIC_BOOL_LOCK_FREE));
|
||||
static_assert(std::atomic<char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE));
|
||||
static_assert(std::atomic<signed char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE));
|
||||
static_assert(std::atomic<unsigned char>::is_always_lock_free == (2 == ATOMIC_CHAR_LOCK_FREE));
|
||||
static_assert(std::atomic<char16_t>::is_always_lock_free == (2 == ATOMIC_CHAR16_T_LOCK_FREE));
|
||||
static_assert(std::atomic<char32_t>::is_always_lock_free == (2 == ATOMIC_CHAR32_T_LOCK_FREE));
|
||||
static_assert(std::atomic<wchar_t>::is_always_lock_free == (2 == ATOMIC_WCHAR_T_LOCK_FREE));
|
||||
static_assert(std::atomic<short>::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE));
|
||||
static_assert(std::atomic<unsigned short>::is_always_lock_free == (2 == ATOMIC_SHORT_LOCK_FREE));
|
||||
static_assert(std::atomic<int>::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE));
|
||||
static_assert(std::atomic<unsigned int>::is_always_lock_free == (2 == ATOMIC_INT_LOCK_FREE));
|
||||
static_assert(std::atomic<long>::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE));
|
||||
static_assert(std::atomic<unsigned long>::is_always_lock_free == (2 == ATOMIC_LONG_LOCK_FREE));
|
||||
static_assert(std::atomic<long long>::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE));
|
||||
static_assert(std::atomic<unsigned long long>::is_always_lock_free == (2 == ATOMIC_LLONG_LOCK_FREE));
|
||||
static_assert(std::atomic<void*>::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE));
|
||||
static_assert(std::atomic<std::nullptr_t>::is_always_lock_free == (2 == ATOMIC_POINTER_LOCK_FREE));
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user