]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/lock/4.cc
01b3c1776a30f8ab636ff13d7be831592babb677
2 // { dg-options "-pthread" }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-effective-target pthread }
5 // { dg-require-gthreads "" }
7 // Copyright (C) 2010-2020 Free Software Foundation, Inc.
9 // This file is part of the GNU ISO C++ Library. This library is free
10 // software; you can redistribute it and/or modify it under the
11 // terms of the GNU General Public License as published by the
12 // Free Software Foundation; either version 3, or (at your option)
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
26 #include <testsuite_hooks.h>
28 struct unreliable_lock
31 std::unique_lock
<std::mutex
> l
;
37 unreliable_lock() : l(m
, std::defer_lock
) { }
41 VERIFY( !l
.owns_lock() );
46 if (count
== throw_on
)
53 if (count
== throw_on
)
55 std::unique_lock
<std::mutex
> l2(m
, std::defer_lock
);
64 VERIFY( l
.owns_lock() );
70 int unreliable_lock::count
= 0;
71 int unreliable_lock::throw_on
= -1;
72 int unreliable_lock::lock_on
= -1;
76 unreliable_lock l1
, l2
, l3
;
80 unreliable_lock::count
= 0;
81 std::lock(l1
, l2
, l3
);
82 VERIFY( unreliable_lock::count
== 3 );
95 // test behaviour when a lock is already held
98 unreliable_lock::lock_on
= 1;
99 while (unreliable_lock::lock_on
< 3)
101 unreliable_lock::count
= 0;
102 unreliable_lock l1
, l2
, l3
;
103 std::lock(l1
, l2
, l3
);
104 VERIFY( unreliable_lock::count
> 3 );
108 ++unreliable_lock::lock_on
;
119 // test behaviour when an exception is thrown
120 unreliable_lock::throw_on
= 0;
121 while (unreliable_lock::throw_on
< 3)
123 unreliable_lock::count
= 0;
124 unreliable_lock l1
, l2
, l3
;
128 std::lock(l1
, l2
, l3
);
135 ++unreliable_lock::throw_on
;