]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/lock/4.cc
7ba15cba84b49e7d3c04be99eb94cd35d7582d07
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 std::lock(l1
, l2
, l3
);
81 VERIFY( unreliable_lock::count
== 3 );
94 // test behaviour when a lock is already held
97 unreliable_lock::lock_on
= 1;
98 while (unreliable_lock::lock_on
< 3)
100 unreliable_lock::count
= 0;
101 unreliable_lock l1
, l2
, l3
;
102 std::lock(l1
, l2
, l3
);
103 VERIFY( unreliable_lock::count
> 3 );
107 ++unreliable_lock::lock_on
;
118 // test behaviour when an exception is thrown
119 unreliable_lock::throw_on
= 0;
120 while (unreliable_lock::throw_on
< 3)
122 unreliable_lock::count
= 0;
123 unreliable_lock l1
, l2
, l3
;
127 std::lock(l1
, l2
, l3
);
134 ++unreliable_lock::throw_on
;