]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_posix.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / semaphore / try_acquire_posix.cc
1 // Copyright (C) 2020-2021 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.
8
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.
13
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/>.
17
18 // { dg-options "-std=gnu++2a -pthread" }
19 // { dg-do run { target c++2a } }
20 // { dg-require-effective-target pthread }
21 // { dg-require-gthreads "" }
22
23 #include <semaphore>
24 #ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
25 #include <chrono>
26 #include <thread>
27 #include <atomic>
28 #include <testsuite_hooks.h>
29
30 void test01()
31 {
32 using namespace std::chrono_literals;
33 std::__platform_semaphore s(2);
34 s._M_acquire();
35
36 auto const dur = 250ms;
37 {
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 );
42 }
43
44 {
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 );
49 }
50 }
51
52 void test02()
53 {
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();
63
64 a.wait(1);
65 VERIFY( s._M_try_acquire_for(dur) );
66 b++;
67 b.notify_one();
68 });
69 t.detach();
70
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();
78
79 b.wait(1);
80 }
81
82 void test03()
83 {
84 using namespace std::chrono_literals;
85 std::__platform_semaphore s(2);
86 s._M_acquire();
87
88 auto const dur = 250ms;
89 {
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 );
95 }
96
97 {
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 );
103 }
104 }
105
106 void test04()
107 {
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;
114 {
115 auto const at = std::chrono::system_clock::now() + dur;
116 VERIFY( !s._M_try_acquire_until(at) );
117
118 b++;
119 b.notify_one();
120 }
121
122 a.wait(1);
123 {
124 auto const at = std::chrono::system_clock::now() + dur;
125 VERIFY( s._M_try_acquire_until(at) );
126 }
127 b++;
128 b.notify_one();
129 });
130 t.detach();
131
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();
139
140 b.wait(1);
141 }
142 #endif
143
144 int main()
145 {
146 #ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
147 test01();
148 test02();
149 test03();
150 test04();
151 #endif
152 return 0;
153 }