Index: include/atomic =================================================================== --- include/atomic +++ include/atomic @@ -561,6 +561,27 @@ # define __cpp_lib_atomic_is_always_lock_free 201603L #endif + +#if __has_attribute(__enable_if__) && __has_attribute(__unavailable__) +# define _LIBCPP_CHECK_ATOMIC_MEMORY_ORDER +# define _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m) \ + __attribute__ ((__enable_if__(__m == memory_order_release \ + || __m == memory_order_acq_rel, ""))) \ + __attribute__ ((__unavailable__("memory order argument to atomic operation is invalid"))) + +# define _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m) \ + __attribute__ ((__enable_if__(__m == memory_order_consume \ + || __m == memory_order_acquire \ + || __m == memory_order_acq_rel, ""))) \ + __attribute__ ((__unavailable__("memory order argument to atomic operation is invalid"))) + +# define _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f) \ + __attribute__ ((__enable_if__(__f == memory_order_release \ + || __f == memory_order_acq_rel, ""))) \ + __attribute__ ((__unavailable__("memory order argument to atomic operation is invalid"))) +#endif + + _LIBCPP_BEGIN_NAMESPACE_STD typedef enum memory_order @@ -934,6 +955,21 @@ bool compare_exchange_strong(_Tp& __e, _Tp __d, memory_order __m = memory_order_seq_cst) _NOEXCEPT {return __c11_atomic_compare_exchange_strong(&__a_, &__e, __d, __m, __m);} +#if defined(_LIBCPP_CHECK_ATOMIC_MEMORY_ORDER) + void store(_Tp __d, memory_order __m) volatile _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m); + void store(_Tp __d, memory_order __m) _LIBCPP_CHECK_STORE_MEMORY_ORDER(__m); + + _Tp load(memory_order __m) const volatile _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m); + _Tp load(memory_order __m) const _LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m); + bool compare_exchange_weak(_Tp&, _Tp, memory_order __s, memory_order __f) volatile + _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); + bool compare_exchange_weak(_Tp&, _Tp, memory_order __s, memory_order __f) + _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); + bool compare_exchange_strong(_Tp&, _Tp, memory_order __s, memory_order __f) volatile + _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); + bool compare_exchange_strong(_Tp&, _Tp, memory_order __s, memory_order __f) + _LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); +#endif _LIBCPP_INLINE_VISIBILITY #ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS @@ -1345,6 +1381,46 @@ return __o->compare_exchange_strong(*__e, __d, __s, __f); } + +#ifdef _LIBCPP_CHECK_ATOMIC_MEMORY_ORDER +template +void atomic_store_explicit(volatile atomic<_Tp>*, _Tp, memory_order __m) +_LIBCPP_CHECK_STORE_MEMORY_ORDER(__m); + +template +void atomic_store_explicit(atomic<_Tp>*, _Tp, memory_order __m) +_LIBCPP_CHECK_STORE_MEMORY_ORDER(__m); + +template +_Tp atomic_load_explicit(const volatile atomic<_Tp>*, memory_order __m) +_LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m); + +template +_Tp atomic_load_explicit(const atomic<_Tp>*, memory_order __m) +_LIBCPP_CHECK_LOAD_MEMORY_ORDER(__m); + +template +bool atomic_compare_exchange_weak_explicit(volatile atomic<_Tp>*, _Tp*, _Tp, + memory_order __s, memory_order __f) +_LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); + +template +bool atomic_compare_exchange_weak_explicit(atomic<_Tp>*, _Tp*, _Tp, + memory_order __s, memory_order __f) +_LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); + +template +bool atomic_compare_exchange_strong_explicit(volatile atomic<_Tp>*, _Tp*, _Tp, + memory_order __s, memory_order __f) +_LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); + +template +bool atomic_compare_exchange_strong_explicit(atomic<_Tp>*, _Tp*, _Tp, + memory_order __s, memory_order __f) +_LIBCPP_CHECK_EXCHANGE_MEMORY_ORDER(__s, __f); + +#endif + // atomic_fetch_add template Index: test/libcxx/atomics/diagnose_invalid_memory_order.fail.cpp =================================================================== --- /dev/null +++ test/libcxx/atomics/diagnose_invalid_memory_order.fail.cpp @@ -0,0 +1,81 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// The attributes used to diagnose incorrect memory order inputs is clang +// specific. +// UNSUPPORTED: gcc + +// + +// Test that invalid memory order arguments are diagnosed where possible. + +#include + +int main() { + std::atomic x(42); + volatile std::atomic& vx = x; + int val1 = 1; + int val2 = 2; + // load operations + { + x.load(std::memory_order_release); // expected-error {{operation is invalid}} + x.load(std::memory_order_acq_rel); // expected-error {{operation is invalid}} + vx.load(std::memory_order_release); // expected-error {{operation is invalid}} + vx.load(std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } + { + std::atomic_load_explicit(&x, std::memory_order_release); // expected-error {{operation is invalid}} + std::atomic_load_explicit(&x, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + std::atomic_load_explicit(&vx, std::memory_order_release); // expected-error {{operation is invalid}} + std::atomic_load_explicit(&vx, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } + // store operations + { + x.store(42, std::memory_order_consume); // expected-error {{operation is invalid}} + x.store(42, std::memory_order_acquire); // expected-error {{operation is invalid}} + x.store(42, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + vx.store(42, std::memory_order_consume); // expected-error {{operation is invalid}} + vx.store(42, std::memory_order_acquire); // expected-error {{operation is invalid}} + vx.store(42, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } + { + std::atomic_store_explicit(&x, 42, std::memory_order_consume); // expected-error {{operation is invalid}} + std::atomic_store_explicit(&x, 42, std::memory_order_acquire); // expected-error {{operation is invalid}} + std::atomic_store_explicit(&x, 42, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + std::atomic_store_explicit(&vx, 42, std::memory_order_consume); // expected-error {{operation is invalid}} + std::atomic_store_explicit(&vx, 42, std::memory_order_acquire); // expected-error {{operation is invalid}} + std::atomic_store_explicit(&vx, 42, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } + // compare exchange weak + { + x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + x.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + vx.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + vx.compare_exchange_weak(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } + { + std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + std::atomic_compare_exchange_weak_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + std::atomic_compare_exchange_weak_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + std::atomic_compare_exchange_weak_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } + // compare exchange strong + { + x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + x.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + vx.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + vx.compare_exchange_strong(val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } + { + std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + std::atomic_compare_exchange_strong_explicit(&x, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + std::atomic_compare_exchange_strong_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_release); // expected-error {{operation is invalid}} + std::atomic_compare_exchange_strong_explicit(&vx, &val1, val2, std::memory_order_seq_cst, std::memory_order_acq_rel); // expected-error {{operation is invalid}} + } +}