teak-llvm/libcxx/test/support/cmpxchg_loop.h
Dan Albert 0bb696800f PR20546: Fix tests for compare_exchange_weak.
These calls are allowed to fail spuriously.

29.6.5.25:

    Remark: A weak compare-and-exchange operation may fail spuriously.
    That is, even when the contents of memory referred to by expected
    and object are equal, it may return false and store back to expected
    the same memory contents that were originally there. [ Note: This
    spurious failure enables implementation of compare and-exchange on a
    broader class of machines, e.g., load-locked store-conditional
    machines. A consequence of spurious failure is that nearly all uses
    of weak compare-and-exchange will be in a loop.

To fix this, we replace any assert() that expects
std::atomic::compare_exchange_weak() to return true with a loop. If the
call does not return true within N runs (with N currently equal to 10),
then the test fails.

http://llvm.org/bugs/show_bug.cgi?id=20546

llvm-svn: 217319
2014-09-06 20:38:25 +00:00

52 lines
1.3 KiB
C++

#include <atomic>
template <class A, class T>
bool cmpxchg_weak_loop(A& atomic, T& expected, T desired) {
for (int i = 0; i < 10; i++) {
if (atomic.compare_exchange_weak(expected, desired) == true) {
return true;
}
}
return false;
}
template <class A, class T>
bool cmpxchg_weak_loop(A& atomic, T& expected, T desired,
std::memory_order success,
std::memory_order failure) {
for (int i = 0; i < 10; i++) {
if (atomic.compare_exchange_weak(expected, desired, success,
failure) == true) {
return true;
}
}
return false;
}
template <class A, class T>
bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired) {
for (int i = 0; i < 10; i++) {
if (std::atomic_compare_exchange_weak(atomic, expected, desired) == true) {
return true;
}
}
return false;
}
template <class A, class T>
bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired,
std::memory_order success,
std::memory_order failure) {
for (int i = 0; i < 10; i++) {
if (std::atomic_compare_exchange_weak_explicit(atomic, expected, desired,
success, failure) == true) {
return true;
}
}
return false;
}