Daily bump.
[official-gcc.git] / libstdc++-v3 / testsuite / 30_threads / semaphore / try_acquire_posix.cc
blobf651fa2435ac3ed80ca2a41d2df679f707df558a
1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
18 // { dg-do run { target c++20 } }
19 // { dg-additional-options "-pthread" { target pthread } }
20 // { dg-require-gthreads "" }
21 // { dg-add-options libatomic }
23 #include <semaphore>
24 #ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
25 #include <chrono>
26 #include <thread>
27 #include <atomic>
28 #include <testsuite_hooks.h>
30 void test01()
32 using namespace std::chrono_literals;
33 std::__platform_semaphore s(2);
34 s._M_acquire();
36 auto const dur = 250ms;
38 auto const t0 = std::chrono::steady_clock::now();
39 VERIFY( s._M_try_acquire_for(dur) );
40 auto const diff = std::chrono::steady_clock::now() - t0;
41 VERIFY( diff < dur );
45 auto const t0 = std::chrono::steady_clock::now();
46 VERIFY( !s._M_try_acquire_for(dur) );
47 auto const diff = std::chrono::steady_clock::now() - t0;
48 VERIFY( diff >= dur );
52 void test02()
54 using namespace std::chrono_literals;
55 std::__platform_semaphore s(1);
56 std::atomic<int> a(0), b(0);
57 std::thread t([&] {
58 a.wait(0);
59 auto const dur = 250ms;
60 VERIFY( !s._M_try_acquire_for(dur) );
61 b++;
62 b.notify_one();
64 a.wait(1);
65 VERIFY( s._M_try_acquire_for(dur) );
66 b++;
67 b.notify_one();
68 });
69 t.detach();
71 s._M_acquire();
72 a++;
73 a.notify_one();
74 b.wait(0);
75 s._M_release(1);
76 a++;
77 a.notify_one();
79 b.wait(1);
82 void test03()
84 using namespace std::chrono_literals;
85 std::__platform_semaphore s(2);
86 s._M_acquire();
88 auto const dur = 250ms;
90 auto const at = std::chrono::system_clock::now() + dur;
91 auto const t0 = std::chrono::steady_clock::now();
92 VERIFY( s._M_try_acquire_until(at) );
93 auto const diff = std::chrono::steady_clock::now() - t0;
94 VERIFY( diff < dur );
98 auto const at = std::chrono::system_clock::now() + dur;
99 auto const t0 = std::chrono::steady_clock::now();
100 VERIFY( !s._M_try_acquire_until(at) );
101 auto const diff = std::chrono::steady_clock::now() - t0;
102 VERIFY( diff >= dur );
106 void test04()
108 using namespace std::chrono_literals;
109 std::__platform_semaphore s(1);
110 std::atomic<int> a(0), b(0);
111 std::thread t([&] {
112 a.wait(0);
113 auto const dur = 250ms;
115 auto const at = std::chrono::system_clock::now() + dur;
116 VERIFY( !s._M_try_acquire_until(at) );
118 b++;
119 b.notify_one();
122 a.wait(1);
124 auto const at = std::chrono::system_clock::now() + dur;
125 VERIFY( s._M_try_acquire_until(at) );
127 b++;
128 b.notify_one();
130 t.detach();
132 s._M_acquire();
133 a++;
134 a.notify_one();
135 b.wait(0);
136 s._M_release(1);
137 a++;
138 a.notify_one();
140 b.wait(1);
142 #endif
144 int main()
146 #ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
147 test01();
148 test02();
149 test03();
150 test04();
151 #endif
152 return 0;