jfb created this revision.
jfb added reviewers: mclow.lists, rsmith.
jfb added a subscriber: cfe-commits.

This was voted into C++17 at last week's 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

The clang part of this implementation is in the following code review:
  http://reviews.llvm.org/D17950

http://reviews.llvm.org/D17951

Files:
  include/atomic
  test/std/atomics/atomics.lockfree/lockfree.pass.cpp

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,20 +11,30 @@
 
 // <atomic>
 
+// #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
 // #define ATOMIC_WCHAR_T_LOCK_FREE unspecified
 // #define ATOMIC_SHORT_LOCK_FREE unspecified
 // #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 <atomic>
 #include <cassert>
 
+template <typename T> void checkAlwaysLockFree() {
+  if (std::atomic<T>::is_always_lock_free)
+    assert(std::atomic<T>().is_lock_free());
+}
+
 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 +59,60 @@
     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);
+
+// 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; });
 }
Index: include/atomic
===================================================================
--- include/atomic
+++ include/atomic
@@ -89,6 +89,7 @@
 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 +128,7 @@
 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 +204,7 @@
 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;
@@ -825,13 +828,81 @@
     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 <int _Bytes> _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 <typename T> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free = false;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<bool> = 2 == ATOMIC_BOOL_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<char> = 2 == ATOMIC_CHAR_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<signed char> = 2 == ATOMIC_CHAR_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<unsigned char> = 2 == ATOMIC_CHAR_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<char16_t> = 2 == ATOMIC_CHAR16_T_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<char32_t> = 2 == ATOMIC_CHAR32_T_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<wchar_t> = 2 == ATOMIC_WCHAR_T_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<short> = 2 == ATOMIC_SHORT_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<unsigned short> = 2 == ATOMIC_SHORT_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<int> = 2 == ATOMIC_INT_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<unsigned int> = 2 == ATOMIC_INT_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<long> = 2 == ATOMIC_LONG_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<unsigned long> = 2 == ATOMIC_LONG_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<long long> = 2 == ATOMIC_LLONG_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<unsigned long long> = 2 == ATOMIC_LLONG_LOCK_FREE;
+template <typename T> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<T*> = 2 == ATOMIC_POINTER_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<std::nullptr_t> = 2 == ATOMIC_POINTER_LOCK_FREE;
+template <> _LIBCPP_CONSTEXPR bool __libcpp_always_lock_free<float> =
+#   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<double> =
+#   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<long double> =
+#   if defined(__GCC_ATOMIC_LDOUBLE_LOCK_FREE) && 2 == __GCC_ATOMIC_LDOUBLE_LOCK_FREE
+    true;
+#   else
+    false;
+#   endif
+# endif
+#endif
+
 // general atomic<T>
 
 template <class _Tp, bool = is_integral<_Tp>::value && !is_same<_Tp, bool>::value>
 struct __atomic_base  // false
 {
     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<sizeof(_Tp)>;
+# 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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to