xref: /minix3/external/bsd/libc++/dist/libcxx/test/support/cmpxchg_loop.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc #include <atomic>
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc template <class A, class T>
cmpxchg_weak_loop(A & atomic,T & expected,T desired)4*0a6a1f1dSLionel Sambuc bool cmpxchg_weak_loop(A& atomic, T& expected, T desired) {
5*0a6a1f1dSLionel Sambuc   for (int i = 0; i < 10; i++) {
6*0a6a1f1dSLionel Sambuc     if (atomic.compare_exchange_weak(expected, desired) == true) {
7*0a6a1f1dSLionel Sambuc       return true;
8*0a6a1f1dSLionel Sambuc     }
9*0a6a1f1dSLionel Sambuc   }
10*0a6a1f1dSLionel Sambuc 
11*0a6a1f1dSLionel Sambuc   return false;
12*0a6a1f1dSLionel Sambuc }
13*0a6a1f1dSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc template <class A, class T>
cmpxchg_weak_loop(A & atomic,T & expected,T desired,std::memory_order success,std::memory_order failure)15*0a6a1f1dSLionel Sambuc bool cmpxchg_weak_loop(A& atomic, T& expected, T desired,
16*0a6a1f1dSLionel Sambuc                        std::memory_order success,
17*0a6a1f1dSLionel Sambuc                        std::memory_order failure) {
18*0a6a1f1dSLionel Sambuc   for (int i = 0; i < 10; i++) {
19*0a6a1f1dSLionel Sambuc     if (atomic.compare_exchange_weak(expected, desired, success,
20*0a6a1f1dSLionel Sambuc                                      failure) == true) {
21*0a6a1f1dSLionel Sambuc       return true;
22*0a6a1f1dSLionel Sambuc     }
23*0a6a1f1dSLionel Sambuc   }
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc   return false;
26*0a6a1f1dSLionel Sambuc }
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc template <class A, class T>
c_cmpxchg_weak_loop(A * atomic,T * expected,T desired)29*0a6a1f1dSLionel Sambuc bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired) {
30*0a6a1f1dSLionel Sambuc   for (int i = 0; i < 10; i++) {
31*0a6a1f1dSLionel Sambuc     if (std::atomic_compare_exchange_weak(atomic, expected, desired) == true) {
32*0a6a1f1dSLionel Sambuc       return true;
33*0a6a1f1dSLionel Sambuc     }
34*0a6a1f1dSLionel Sambuc   }
35*0a6a1f1dSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc   return false;
37*0a6a1f1dSLionel Sambuc }
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc template <class A, class T>
c_cmpxchg_weak_loop(A * atomic,T * expected,T desired,std::memory_order success,std::memory_order failure)40*0a6a1f1dSLionel Sambuc bool c_cmpxchg_weak_loop(A* atomic, T* expected, T desired,
41*0a6a1f1dSLionel Sambuc                          std::memory_order success,
42*0a6a1f1dSLionel Sambuc                          std::memory_order failure) {
43*0a6a1f1dSLionel Sambuc   for (int i = 0; i < 10; i++) {
44*0a6a1f1dSLionel Sambuc     if (std::atomic_compare_exchange_weak_explicit(atomic, expected, desired,
45*0a6a1f1dSLionel Sambuc                                                    success, failure) == true) {
46*0a6a1f1dSLionel Sambuc       return true;
47*0a6a1f1dSLionel Sambuc     }
48*0a6a1f1dSLionel Sambuc   }
49*0a6a1f1dSLionel Sambuc 
50*0a6a1f1dSLionel Sambuc   return false;
51*0a6a1f1dSLionel Sambuc }
52