replace_failure_order.pass.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // UNSUPPORTED: libcpp-has-no-threads
  9. // This test verifies behavior specified by [atomics.types.operations.req]/21:
  10. //
  11. // When only one memory_order argument is supplied, the value of success is
  12. // order, and the value of failure is order except that a value of
  13. // memory_order_acq_rel shall be replaced by the value memory_order_acquire
  14. // and a value of memory_order_release shall be replaced by the value
  15. // memory_order_relaxed.
  16. //
  17. // Clang's atomic intrinsics do this for us, but GCC's do not. We don't actually
  18. // have visibility to see what these memory orders are lowered to, but we can at
  19. // least check that they are lowered at all (otherwise there is a compile
  20. // failure with GCC).
  21. #include <atomic>
  22. #include "test_macros.h"
  23. int main(int, char**) {
  24. std::atomic<int> i;
  25. volatile std::atomic<int> v;
  26. int exp = 0;
  27. (void) i.compare_exchange_weak(exp, 0, std::memory_order_acq_rel);
  28. (void) i.compare_exchange_weak(exp, 0, std::memory_order_release);
  29. i.compare_exchange_strong(exp, 0, std::memory_order_acq_rel);
  30. i.compare_exchange_strong(exp, 0, std::memory_order_release);
  31. (void) v.compare_exchange_weak(exp, 0, std::memory_order_acq_rel);
  32. (void) v.compare_exchange_weak(exp, 0, std::memory_order_release);
  33. v.compare_exchange_strong(exp, 0, std::memory_order_acq_rel);
  34. v.compare_exchange_strong(exp, 0, std::memory_order_release);
  35. return 0;
  36. }