]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/try_lock/4.cc
2 // { dg-additional-options "-pthread" { target pthread } }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-gthreads "" }
6 // Copyright (C) 2010-2021 Free Software Foundation, Inc.
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)
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.
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/>.
25 #include <testsuite_hooks.h>
27 struct unreliable_lock
30 std::unique_lock
<std::mutex
> l
;
36 unreliable_lock() : l(m
, std::defer_lock
) { }
40 VERIFY( !l
.owns_lock() );
45 if (count
== throw_on
)
52 if (count
== throw_on
)
54 std::unique_lock
<std::mutex
> l2(m
, std::defer_lock
);
63 VERIFY( l
.owns_lock() );
69 int unreliable_lock::count
= 0;
70 int unreliable_lock::throw_on
= -1;
71 int unreliable_lock::lock_on
= -1;
75 unreliable_lock l1
, l2
, l3
;
79 unreliable_lock::count
= 0;
80 int result
= std::try_lock(l1
, l2
, l3
);
81 VERIFY( result
== -1 );
82 VERIFY( unreliable_lock::count
== 3 );
95 unreliable_lock l1
, l2
, l3
;
99 // test behaviour when a lock is already held
100 unreliable_lock::lock_on
= 0;
101 while (unreliable_lock::lock_on
< 3)
103 unreliable_lock::count
= 0;
104 int failed
= std::try_lock(l1
, l2
, l3
);
105 VERIFY( failed
== unreliable_lock::lock_on
);
106 ++unreliable_lock::lock_on
;
117 unreliable_lock l1
, l2
, l3
;
121 // test behaviour when an exception is thrown
122 unreliable_lock::throw_on
= 0;
123 while (unreliable_lock::throw_on
< 3)
125 unreliable_lock::count
= 0;
128 std::try_lock(l1
, l2
, l3
);
133 VERIFY( e
== unreliable_lock::throw_on
);
135 ++unreliable_lock::throw_on
;