]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / timed_mutex / try_lock_until / 57641.cc
CommitLineData
d1236680 1// { dg-do run }
1e42d2f4 2// { dg-additional-options "-pthread" { target pthread } }
71c54f8e 3// { dg-require-effective-target c++11 }
1e42d2f4 4// { dg-require-gthreads "" }
25e00ab6 5
99dee823 6// Copyright (C) 2013-2021 Free Software Foundation, Inc.
25e00ab6
JW
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 <mutex>
24#include <chrono>
25#include <thread>
26#include <testsuite_hooks.h>
27
28// PR libstdc++/57641
29
30namespace C = std::chrono;
31
32// custom clock with epoch 10s before system_clock's
33struct clock
34{
35 typedef C::system_clock::rep rep;
36 typedef C::system_clock::period period;
37 typedef C::system_clock::duration duration;
38 typedef C::time_point<clock> time_point;
39 static constexpr bool is_steady = C::system_clock::is_steady;
40
41 static time_point
42 now()
43 {
44 auto sys_time = C::system_clock::now().time_since_epoch();
45 return time_point(sys_time + C::seconds(10));
46 }
47};
48
49std::timed_mutex mx;
2d5e3740 50bool locked = false;
25e00ab6 51
49638674 52template <typename ClockType>
25e00ab6
JW
53void f()
54{
49638674 55 locked = mx.try_lock_until(ClockType::now() + C::milliseconds(1));
25e00ab6
JW
56}
57
49638674
MC
58template <typename ClockType>
59void test()
25e00ab6 60{
25e00ab6 61 std::lock_guard<std::timed_mutex> l(mx);
49638674
MC
62 auto start = ClockType::now();
63 std::thread t(f<ClockType>);
25e00ab6 64 t.join();
49638674 65 auto stop = ClockType::now();
25e00ab6 66 VERIFY( (stop - start) < C::seconds(9) );
2d5e3740 67 VERIFY( !locked );
25e00ab6 68}
49638674
MC
69
70int main()
71{
72 test<C::system_clock>();
73 test<C::steady_clock>();
74}