]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/lock/4.cc
7ba15cba84b49e7d3c04be99eb94cd35d7582d07
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / lock / 4.cc
1 // { dg-do run }
2 // { dg-additional-options "-pthread" { target pthread } }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-gthreads "" }
5
6 // Copyright (C) 2010-2021 Free Software Foundation, Inc.
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
24 #include <mutex>
25 #include <testsuite_hooks.h>
26
27 struct unreliable_lock
28 {
29 std::mutex m;
30 std::unique_lock<std::mutex> l;
31
32 static int count;
33 static int throw_on;
34 static int lock_on;
35
36 unreliable_lock() : l(m, std::defer_lock) { }
37
38 ~unreliable_lock()
39 {
40 VERIFY( !l.owns_lock() );
41 }
42
43 void lock()
44 {
45 if (count == throw_on)
46 throw throw_on;
47 ++count;
48 l.lock();
49 }
50 bool try_lock()
51 {
52 if (count == throw_on)
53 throw throw_on;
54 std::unique_lock<std::mutex> l2(m, std::defer_lock);
55 if (count == lock_on)
56 l2.lock();
57 ++count;
58 return l.try_lock();
59 }
60
61 void unlock()
62 {
63 VERIFY( l.owns_lock() );
64 l.unlock();
65 }
66
67 };
68
69 int unreliable_lock::count = 0;
70 int unreliable_lock::throw_on = -1;
71 int unreliable_lock::lock_on = -1;
72
73 void test01()
74 {
75 unreliable_lock l1, l2, l3;
76
77 try
78 {
79 unreliable_lock::count = 0;
80 std::lock(l1, l2, l3);
81 VERIFY( unreliable_lock::count == 3 );
82 l1.unlock();
83 l2.unlock();
84 l3.unlock();
85 }
86 catch (...)
87 {
88 VERIFY( false );
89 }
90 }
91
92 void test02()
93 {
94 // test behaviour when a lock is already held
95 try
96 {
97 unreliable_lock::lock_on = 1;
98 while (unreliable_lock::lock_on < 3)
99 {
100 unreliable_lock::count = 0;
101 unreliable_lock l1, l2, l3;
102 std::lock(l1, l2, l3);
103 VERIFY( unreliable_lock::count > 3 );
104 l1.unlock();
105 l2.unlock();
106 l3.unlock();
107 ++unreliable_lock::lock_on;
108 }
109 }
110 catch (...)
111 {
112 VERIFY( false );
113 }
114 }
115
116 void test03()
117 {
118 // test behaviour when an exception is thrown
119 unreliable_lock::throw_on = 0;
120 while (unreliable_lock::throw_on < 3)
121 {
122 unreliable_lock::count = 0;
123 unreliable_lock l1, l2, l3;
124 bool test = false;
125 try
126 {
127 std::lock(l1, l2, l3);
128 }
129 catch (...)
130 {
131 test = true;
132 }
133 VERIFY( test );
134 ++unreliable_lock::throw_on;
135 }
136 }
137
138 int main()
139 {
140 test01();
141 test02();
142 test03();
143 return 0;
144 }