8a8a6d61e63da02ec16996a34a6e097eddca8251
[llvm-toolchain-11.git] /
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 // UNSUPPORTED: libcpp-has-no-threads
10 //  ... assertion fails line 38
11
12 // <atomic>
13
14 // template <class T>
15 //     bool
16 //     atomic_compare_exchange_weak_explicit(volatile atomic<T>* obj, T* expc,
17 //                                           T desr,
18 //                                           memory_order s, memory_order f);
19 //
20 // template <class T>
21 //     bool
22 //     atomic_compare_exchange_weak_explicit(atomic<T>* obj, T* expc, T desr,
23 //                                           memory_order s, memory_order f);
24
25 #include <atomic>
26 #include <type_traits>
27 #include <cassert>
28
29 #include <cmpxchg_loop.h>
30
31 #include "test_macros.h"
32 #include "atomic_helpers.h"
33
34 template <class T>
35 struct TestFn {
36   void operator()() const {
37     {
38         typedef std::atomic<T> A;
39         A a;
40         T t(T(1));
41         std::atomic_init(&a, t);
42         assert(c_cmpxchg_weak_loop(&a, &t, T(2),
43                std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
44         assert(a == T(2));
45         assert(t == T(1));
46         assert(std::atomic_compare_exchange_weak_explicit(&a, &t, T(3),
47                std::memory_order_seq_cst, std::memory_order_seq_cst) == false);
48         assert(a == T(2));
49         assert(t == T(2));
50     }
51     {
52         typedef std::atomic<T> A;
53         volatile A a;
54         T t(T(1));
55         std::atomic_init(&a, t);
56         assert(c_cmpxchg_weak_loop(&a, &t, T(2),
57                std::memory_order_seq_cst, std::memory_order_seq_cst) == true);
58         assert(a == T(2));
59         assert(t == T(1));
60         assert(std::atomic_compare_exchange_weak_explicit(&a, &t, T(3),
61                std::memory_order_seq_cst, std::memory_order_seq_cst) == false);
62         assert(a == T(2));
63         assert(t == T(2));
64     }
65   }
66 };
67
68 int main(int, char**)
69 {
70     TestEachAtomicType<TestFn>()();
71
72   return 0;
73 }