Debian Codegraph @

libstdc++-v3/testsuite/29_atomics/atomic/wait_notify/100334.cc

raw
1 // { dg-do run { target c++20 } }
2 // { dg-require-gthreads "" }
3 // { dg-additional-options "-pthread" { target pthread } }
4 // { dg-add-options libatomic }
5
6 // Copyright (C) 2021-2025 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23 #include <atomic>
24 #include <future>
25
26 #include <testsuite_hooks.h>
27
28 template <typename T>
29 struct atomics_sharing_same_waiter
30 {
31 std::atomic<T> tmp[49 * 4] = {};
32 std::atomic<T>* a[4] = {
33 { &tmp[0] },
34 { &tmp[16 * 4] },
35 { &tmp[32 * 4] },
36 { &tmp[48 * 4] }
37 };
38 };
39
40 constexpr unsigned key(void * a)
41 {
42 constexpr uintptr_t ct = 16;
43 return (uintptr_t(a) >> 2) % ct;
44 }
45
46 int
47 main()
48 {
49 // all atomic share the same waiter
50 // atomics_sharing_same_waiter<char> atomics;
51 atomics_sharing_same_waiter<char> atomics;
52 for (auto& atom : atomics.a)
53 {
54 atom->store(0);
55 }
56
57 auto a = &std::__detail::__waiter_pool_base::_S_for(reinterpret_cast<char *>(atomics.a[0]));
58 auto b = &std::__detail::__waiter_pool_base::_S_for(reinterpret_cast<char *>(atomics.a[1]));
59 VERIFY( a == b );
60
61 auto fut0 = std::async(std::launch::async, [&] { atomics.a[0]->wait(0); });
62 auto fut1 = std::async(std::launch::async, [&] { atomics.a[1]->wait(0); });
63 auto fut2 = std::async(std::launch::async, [&] { atomics.a[2]->wait(0); });
64 auto fut3 = std::async(std::launch::async, [&] { atomics.a[3]->wait(0); });
65
66 // make sure the all threads already await
67 std::this_thread::sleep_for(std::chrono::milliseconds{100});
68
69 atomics.a[2]->store(1);
70 atomics.a[2]->notify_one();
71
72 VERIFY(std::future_status::timeout == fut0.wait_for(std::chrono::milliseconds{100}));
73 VERIFY(atomics.a[0]->load() == 0);
74
75 VERIFY(std::future_status::timeout == fut1.wait_for(std::chrono::milliseconds{100}));
76 VERIFY(atomics.a[1]->load() == 0);
77
78 VERIFY(std::future_status::ready == fut2.wait_for(std::chrono::milliseconds{100}));
79 VERIFY(atomics.a[2]->load() == 1);
80
81 VERIFY(std::future_status::timeout == fut3.wait_for(std::chrono::milliseconds{100}));
82 VERIFY(atomics.a[3]->load() == 0);
83
84 atomics.a[0]->store(1);
85 atomics.a[0]->notify_one();
86 atomics.a[1]->store(1);
87 atomics.a[1]->notify_one();
88 atomics.a[3]->store(1);
89 atomics.a[3]->notify_one();
90
91 return 0;
92 }