]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/semaphore/try_acquire_posix.cc
1 // Copyright (C) 2020-2021 Free Software Foundation, Inc.
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)
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-options "-std=gnu++2a -pthread" }
19 // { dg-do run { target c++2a } }
20 // { dg-require-effective-target pthread }
21 // { dg-require-gthreads "" }
24 #ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE
28 #include <testsuite_hooks.h>
32 using namespace std::chrono_literals
;
33 std::__platform_semaphore
s(2);
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
;
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
);
54 using namespace std::chrono_literals
;
55 std::__platform_semaphore
s(1);
56 std::atomic
<int> a(0), b(0);
59 auto const dur
= 250ms
;
60 VERIFY( !s
._M_try_acquire_for(dur
) );
65 VERIFY( s
._M_try_acquire_for(dur
) );
84 using namespace std::chrono_literals
;
85 std::__platform_semaphore
s(2);
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
;
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
);
108 using namespace std::chrono_literals
;
109 std::__platform_semaphore
s(1);
110 std::atomic
<int> a(0), b(0);
113 auto const dur
= 250ms
;
115 auto const at
= std::chrono::system_clock::now() + dur
;
116 VERIFY( !s
._M_try_acquire_until(at
) );
124 auto const at
= std::chrono::system_clock::now() + dur
;
125 VERIFY( s
._M_try_acquire_until(at
) );
146 #ifdef _GLIBCXX_HAVE_POSIX_SEMAPHORE