| 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 |
|
| 9 |
#include <atomic> |
| 10 |
|
| 11 |
template <class A> |
| 12 |
bool cmpxchg_weak_loop(A& atomic, typename A::value_type& expected, typename A::value_type desired) { |
| 13 |
for (int i = 0; i < 10; i++) { |
| 14 |
if (atomic.compare_exchange_weak(expected, desired) == true) { |
| 15 |
return true; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
return false; |
| 20 |
} |
| 21 |
|
| 22 |
template <class A> |
| 23 |
bool cmpxchg_weak_loop(A& atomic, typename A::value_type& expected, typename A::value_type desired, |
| 24 |
std::memory_order success, |
| 25 |
std::memory_order failure) { |
| 26 |
for (int i = 0; i < 10; i++) { |
| 27 |
if (atomic.compare_exchange_weak(expected, desired, success, |
| 28 |
failure) == true) { |
| 29 |
return true; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
template <class A> |
| 37 |
bool c_cmpxchg_weak_loop(A* atomic, typename A::value_type* expected, typename A::value_type desired) { |
| 38 |
for (int i = 0; i < 10; i++) { |
| 39 |
if (std::atomic_compare_exchange_weak(atomic, expected, desired) == true) { |
| 40 |
return true; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
template <class A> |
| 48 |
bool c_cmpxchg_weak_loop(A* atomic, typename A::value_type* expected, typename A::value_type desired, |
| 49 |
std::memory_order success, |
| 50 |
std::memory_order failure) { |
| 51 |
for (int i = 0; i < 10; i++) { |
| 52 |
if (std::atomic_compare_exchange_weak_explicit(atomic, expected, desired, |
| 53 |
success, failure) == true) { |
| 54 |
return true; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return false; |
| 59 |
} |