| 1 | // { dg-do run { target c++20 } } |
| 2 | // { dg-require-effective-target gthreads } |
| 3 | // { dg-additional-options "-pthread" { target pthread } } |
| 4 | // { dg-add-options libatomic } |
| 5 | // { dg-add-options no_pch } |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #ifndef __cpp_lib_atomic_shared_ptr |
| 10 | # error "Feature-test macro for atomic<shared_ptr<T>> missing in <memory>" |
| 11 | #elif __cpp_lib_atomic_shared_ptr != 201711L |
| 12 | # error "Feature-test macro for atomic<shared_ptr<T>> has wrong value in <memory>" |
| 13 | #endif |
| 14 | |
| 15 | #include <thread> |
| 16 | |
| 17 | #include <testsuite_hooks.h> |
| 18 | |
| 19 | // Check constexpr constructor. |
| 20 | constinit std::atomic<std::shared_ptr<int>> a; |
| 21 | // LWG 3661. constinit atomic<shared_ptr<T>> a(nullptr); should work |
| 22 | constinit std::atomic<std::shared_ptr<int>> a2 = nullptr; |
| 23 | |
| 24 | void |
| 25 | test_is_lock_free() |
| 26 | { |
| 27 | using test_type = std::atomic<std::shared_ptr<int>>; |
| 28 | static_assert( test_type::is_always_lock_free == false ); |
| 29 | |
| 30 | test_type p; |
| 31 | VERIFY( p.is_lock_free() == false ); |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | test_atomic_shared_ptr() |
| 36 | { |
| 37 | struct A { int a; int b; }; |
| 38 | |
| 39 | auto a = std::make_shared<A>( 0, 42 ); |
| 40 | using ptr_t = std::shared_ptr<A>; |
| 41 | { |
| 42 | std::atomic<ptr_t> p{ }; |
| 43 | VERIFY( p.load().get() == nullptr ); |
| 44 | } |
| 45 | |
| 46 | std::atomic<ptr_t> p{ a }; |
| 47 | VERIFY( p.load().get() == a.get() ); |
| 48 | auto b = std::make_shared<A>( 42, 0 ); |
| 49 | p.store(b); |
| 50 | VERIFY( p.load().get() != a.get() ); |
| 51 | VERIFY( p.load().get() == b.get() ); |
| 52 | p.exchange(a); |
| 53 | VERIFY( p.load().get() != b.get() ); |
| 54 | VERIFY( p.load().get() == a.get() ); |
| 55 | |
| 56 | { |
| 57 | ptr_t aa{ a }; |
| 58 | VERIFY( p.compare_exchange_strong(aa, b, |
| 59 | std::memory_order_seq_cst, |
| 60 | std::memory_order_seq_cst) == true ); |
| 61 | ptr_t bb{ a }; |
| 62 | VERIFY( p.compare_exchange_strong(bb, b, |
| 63 | std::memory_order_seq_cst, |
| 64 | std::memory_order_seq_cst) == false ); |
| 65 | VERIFY( bb.get() == b.get() ); |
| 66 | } |
| 67 | |
| 68 | { |
| 69 | ptr_t bb{ b }; |
| 70 | VERIFY( p.compare_exchange_weak(bb, a, |
| 71 | std::memory_order_seq_cst, |
| 72 | std::memory_order_seq_cst) == true ); |
| 73 | ptr_t aa{ b }; |
| 74 | VERIFY( p.compare_exchange_weak(aa, a, |
| 75 | std::memory_order_seq_cst, |
| 76 | std::memory_order_seq_cst) == false ); |
| 77 | VERIFY( aa.get() == a.get() ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | test_wait_notify() |
| 83 | { |
| 84 | std::atomic<std::shared_ptr<int>> p; |
| 85 | std::shared_ptr<int> a = std::make_shared<int>(); |
| 86 | std::shared_ptr<int> b = std::make_shared<int>(); |
| 87 | |
| 88 | p.store(a); |
| 89 | p.wait(b); |
| 90 | std::thread t([&] |
| 91 | { |
| 92 | p.store(b); |
| 93 | p.notify_one(); |
| 94 | }); |
| 95 | p.wait(a); |
| 96 | t.join(); |
| 97 | } |
| 98 | |
| 99 | int counter = 0; |
| 100 | |
| 101 | void |
| 102 | test_counting() |
| 103 | { |
| 104 | struct X |
| 105 | { |
| 106 | ~X() { ++counter; } |
| 107 | }; |
| 108 | |
| 109 | { |
| 110 | std::atomic<std::shared_ptr<X>> p{ std::make_shared<X>() }; |
| 111 | std::shared_ptr<X> a = p.load(); |
| 112 | VERIFY( a.use_count() == 2 ); // p, a |
| 113 | p.store({}); |
| 114 | VERIFY( a.use_count() == 1 ); // a |
| 115 | p.store(a); |
| 116 | VERIFY( a.use_count() == 2 ); // p, a |
| 117 | std::shared_ptr<X> b = std::make_shared<X>(); |
| 118 | std::shared_ptr<X> c = p.exchange(b); |
| 119 | VERIFY( a.use_count() == 2 ); // a, c |
| 120 | VERIFY( c == a ); |
| 121 | VERIFY( b.use_count() == 2 ); // p, b |
| 122 | std::atomic<std::shared_ptr<X>> p2{a}; |
| 123 | VERIFY( a.use_count() == 3 ); // p2, a, c |
| 124 | VERIFY( p2.compare_exchange_strong(a, b) ); |
| 125 | VERIFY( a.use_count() == 2 ); // a, c |
| 126 | VERIFY( b.use_count() == 3 ); // p, p2, b |
| 127 | VERIFY ( ! p2.compare_exchange_strong(a, b) ); |
| 128 | VERIFY( a == b ); |
| 129 | VERIFY( a.use_count() == 4 ); // p, p2, a, b |
| 130 | VERIFY( b.use_count() == 4 ); |
| 131 | VERIFY( c.use_count() == 1 ); // c |
| 132 | VERIFY( p.compare_exchange_weak(b, c) ); |
| 133 | VERIFY( b.use_count() == 3 ); // p2, a, b |
| 134 | VERIFY( c.use_count() == 2 ); // p, c |
| 135 | VERIFY( ! p.compare_exchange_weak(a, b) ); |
| 136 | VERIFY( a == c ); |
| 137 | VERIFY( a.use_count() == 3 ); // p, a, c |
| 138 | VERIFY( b.use_count() == 2 ); // p2, b |
| 139 | VERIFY( c.use_count() == 3 ); // p, a, c |
| 140 | a.reset(); |
| 141 | b.reset(); |
| 142 | c.reset(); |
| 143 | VERIFY( counter == 0 ); |
| 144 | } |
| 145 | VERIFY( counter == 2 ); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | test_lwg3893() |
| 150 | { |
| 151 | // LWG 3893. LWG 3661 broke atomic<shared_ptr<T>> a; a = nullptr; |
| 152 | std::atomic<std::shared_ptr<int>> a; |
| 153 | a = nullptr; |
| 154 | } |
| 155 | |
| 156 | int |
| 157 | main() |
| 158 | { |
| 159 | test_is_lock_free(); |
| 160 | test_atomic_shared_ptr(); |
| 161 | test_wait_notify(); |
| 162 | test_counting(); |
| 163 | test_lwg3893(); |
| 164 | } |