Eric Fiselier | 38236b5 | 2016-01-19 21:52:04 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Dan Albert | 0bb6968 | 2014-09-06 20:38:25 +0000 | [diff] [blame] | 10 | #include <atomic> |
| 11 | |
| 12 | template <class A, class T> |
| 13 | bool cmpxchg_weak_loop(A& atomic, T& expected, T desired) { |
| 14 | for (int i = 0; i < 10; i++) { |
| 15 | if (atomic.compare_exchange_weak(expected, desired) == true) { |
| 16 | return true; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | return false; |
| 21 | } |
| 22 | |
| 23 | template <class A, class T> |
| 24 | bool cmpxchg_weak_loop(A& atomic, T& expected, T desired, |
| 25 | std::memory_order success, |
| 26 | std::memory_order failure) { |
| 27 | for (int i = 0; i < 10; i++) { |
| 28 | if (atomic.compare_exchange_weak(expected, desired, success, |
| 29 | failure) == true) { |
| 30 | return true; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | template <class A, class T> |
| 38 | bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired) { |
| 39 | for (int i = 0; i < 10; i++) { |
| 40 | if (std::atomic_compare_exchange_weak(atomic, expected, desired) == true) { |
| 41 | return true; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | template <class A, class T> |
| 49 | bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired, |
| 50 | std::memory_order success, |
| 51 | std::memory_order failure) { |
| 52 | for (int i = 0; i < 10; i++) { |
| 53 | if (std::atomic_compare_exchange_weak_explicit(atomic, expected, desired, |
| 54 | success, failure) == true) { |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return false; |
| 60 | } |