llvm-project/libcxx/test/std/atomics/atomics.general/replace_failure_order.pass.cpp
Louis Dionne 92832e4889 [libc++] Enable <atomic> when threads are disabled
std::atomic is, for the most part, just a thin veneer on top of compiler
builtins. Hence, it should be available even when threads are not available
on the system, and in fact there has been requests for such support.

This patch:
- Moves __libcpp_thread_poll_with_backoff to its own header so it can
  be used in <atomic> when threads are disabled.
- Adds a dummy backoff policy for atomic polling that doesn't know about
  threads.
- Adjusts the <atomic> feature-test macros so they are provided even when
  threads are disabled.
- Runs the <atomic> tests when threads are disabled.

rdar://77873569

Differential Revision: https://reviews.llvm.org/D114109
2021-11-17 23:02:58 -05:00

43 lines
1.7 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// This test verifies behavior specified by [atomics.types.operations.req]/21:
//
// When only one memory_order argument is supplied, the value of success is
// order, and the value of failure is order except that a value of
// memory_order_acq_rel shall be replaced by the value memory_order_acquire
// and a value of memory_order_release shall be replaced by the value
// memory_order_relaxed.
//
// Clang's atomic intrinsics do this for us, but GCC's do not. We don't actually
// have visibility to see what these memory orders are lowered to, but we can at
// least check that they are lowered at all (otherwise there is a compile
// failure with GCC).
#include <atomic>
#include "test_macros.h"
int main(int, char**) {
std::atomic<int> i;
volatile std::atomic<int> v;
int exp = 0;
(void) i.compare_exchange_weak(exp, 0, std::memory_order_acq_rel);
(void) i.compare_exchange_weak(exp, 0, std::memory_order_release);
i.compare_exchange_strong(exp, 0, std::memory_order_acq_rel);
i.compare_exchange_strong(exp, 0, std::memory_order_release);
(void) v.compare_exchange_weak(exp, 0, std::memory_order_acq_rel);
(void) v.compare_exchange_weak(exp, 0, std::memory_order_release);
v.compare_exchange_strong(exp, 0, std::memory_order_acq_rel);
v.compare_exchange_strong(exp, 0, std::memory_order_release);
return 0;
}