| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-atomic-cmpxchg-word "" } |
| 3 | // { dg-add-options libatomic } |
| 4 | |
| 5 | #include <atomic> |
| 6 | #include <cstring> |
| 7 | #include <testsuite_hooks.h> |
| 8 | |
| 9 | struct TailPadding { short i; char c; }; |
| 10 | TailPadding ztail{1, 2}; // zeroed-padding |
| 11 | constexpr std::atomic<TailPadding> ctail(TailPadding{1,2}); |
| 12 | std::atomic<TailPadding> gtail(TailPadding{1,2}); |
| 13 | |
| 14 | struct MidPadding { char c; short i; }; |
| 15 | MidPadding zmid{1, 2}; // zeroed-padding |
| 16 | constexpr std::atomic<MidPadding> cmid(MidPadding{1,2}); |
| 17 | std::atomic<MidPadding> gmid(MidPadding{1,2}); |
| 18 | |
| 19 | struct BitPadding { int : 4; int i : 5; int : 4; int j : 5; int : 4; }; |
| 20 | BitPadding zbit{1, 2}; // zeroed-padding |
| 21 | constexpr std::atomic<BitPadding> cbit(BitPadding{1,2}); |
| 22 | std::atomic<BitPadding> gbit(BitPadding{1,2}); |
| 23 | |
| 24 | struct Ctor |
| 25 | { |
| 26 | Ctor() = default; |
| 27 | |
| 28 | constexpr Ctor(char pc, short pi) |
| 29 | : c(pc), i(pi) |
| 30 | {} |
| 31 | |
| 32 | char c; |
| 33 | short i; |
| 34 | }; |
| 35 | |
| 36 | Ctor zctor{1, 2}; // zeroed-padding |
| 37 | constexpr std::atomic<Ctor> cctor(Ctor{1,2}); |
| 38 | std::atomic<Ctor> gctor(Ctor{1,2}); |
| 39 | |
| 40 | template<typename T> |
| 41 | void test_struct(std::atomic<T>& g, const T& zp) |
| 42 | { |
| 43 | T const d{3, 4}; |
| 44 | T t; |
| 45 | |
| 46 | std::memcpy(&t, &zp, sizeof(T)); |
| 47 | VERIFY( g.compare_exchange_strong(t, d) ); |
| 48 | |
| 49 | static std::atomic<T> st(T{1, 2}); |
| 50 | std::memcpy(&t, &zp, sizeof(T)); |
| 51 | VERIFY( st.compare_exchange_strong(t, d) ); |
| 52 | |
| 53 | thread_local std::atomic<T> tl(T{1, 2}); |
| 54 | std::memcpy(&t, &zp, sizeof(T)); |
| 55 | VERIFY( tl.compare_exchange_strong(t, d) ); |
| 56 | |
| 57 | std::atomic<T> l(T{1, 2}); |
| 58 | std::memcpy(&t, &zp, sizeof(T)); |
| 59 | VERIFY( l.compare_exchange_strong(t, d) ); |
| 60 | |
| 61 | std::atomic<T>* h = new std::atomic<T>(T{1, 2}); |
| 62 | std::memcpy(&t, &zp, sizeof(T)); |
| 63 | VERIFY( h->compare_exchange_strong(t, d) ); |
| 64 | delete h; |
| 65 | |
| 66 | constexpr std::atomic<T> cl(T{1, 2}); |
| 67 | } |
| 68 | |
| 69 | int main() |
| 70 | { |
| 71 | test_struct(gtail, ztail); |
| 72 | test_struct(gmid, zmid); |
| 73 | test_struct(gbit, zbit); |
| 74 | test_struct(gctor, zctor); |
| 75 | } |