]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/30_threads/try_lock/4.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / try_lock / 4.cc
CommitLineData
37d13ae6 1// { dg-do run { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* *-*-solaris* *-*-cygwin *-*-rtems* *-*-darwin* powerpc-ibm-aix* } }
4415f7a5
PC
2// { dg-options " -std=gnu++11 -pthread" { target *-*-freebsd* *-*-dragonfly* *-*-netbsd* *-*-linux* *-*-gnu* powerpc-ibm-aix* } }
3// { dg-options " -std=gnu++11 -pthreads" { target *-*-solaris* } }
37d13ae6 4// { dg-options " -std=gnu++11 " { target *-*-cygwin *-*-rtems* *-*-darwin* } }
9b2b801a
JW
5// { dg-require-cstdint "" }
6// { dg-require-gthreads "" }
7
818ab71a 8// Copyright (C) 2010-2016 Free Software Foundation, Inc.
9b2b801a
JW
9//
10// This file is part of the GNU ISO C++ Library. This library is free
11// software; you can redistribute it and/or modify it under the
12// terms of the GNU General Public License as published by the
13// Free Software Foundation; either version 3, or (at your option)
14// any later version.
15
16// This library is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20
21// You should have received a copy of the GNU General Public License along
22// with this library; see the file COPYING3. If not see
23// <http://www.gnu.org/licenses/>.
24
25
26#include <mutex>
27#include <testsuite_hooks.h>
28
29struct unreliable_lock
30{
31 std::mutex m;
32 std::unique_lock<std::mutex> l;
33
34 static int count;
35 static int throw_on;
36 static int lock_on;
37
38 unreliable_lock() : l(m, std::defer_lock) { }
39
40 ~unreliable_lock()
41 {
42 bool test __attribute__((unused)) = true;
43 VERIFY( !l.owns_lock() );
44 }
45
46 void lock()
47 {
48 if (count == throw_on)
49 throw throw_on;
50 ++count;
51 l.lock();
52 }
53 bool try_lock()
54 {
55 if (count == throw_on)
56 throw throw_on;
57 std::unique_lock<std::mutex> l2(m, std::defer_lock);
58 if (count == lock_on)
59 l2.lock();
60 ++count;
61 return l.try_lock();
62 }
63
64 void unlock()
65 {
66 bool test __attribute__((unused)) = true;
67 VERIFY( l.owns_lock() );
68 l.unlock();
69 }
70
71};
72
73int unreliable_lock::count = 0;
74int unreliable_lock::throw_on = -1;
75int unreliable_lock::lock_on = -1;
76
77void test01()
78{
79 bool test __attribute__((unused)) = true;
80
81 unreliable_lock l1, l2, l3;
82
83 try
84 {
85 unreliable_lock::count = 0;
86 int result = std::try_lock(l1, l2, l3);
87 VERIFY( result == -1 );
88 VERIFY( unreliable_lock::count == 3 );
89 l1.unlock();
90 l2.unlock();
91 l3.unlock();
92 }
93 catch (...)
94 {
95 VERIFY( false );
96 }
97}
98
99void test02()
100{
101 bool test __attribute__((unused)) = true;
102
103 unreliable_lock l1, l2, l3;
104
105 try
106 {
107 // test behaviour when a lock is already held
108 unreliable_lock::lock_on = 0;
109 while (unreliable_lock::lock_on < 3)
110 {
111 unreliable_lock::count = 0;
112 int failed = std::try_lock(l1, l2, l3);
113 VERIFY( failed == unreliable_lock::lock_on );
114 ++unreliable_lock::lock_on;
115 }
116 }
117 catch (...)
118 {
119 VERIFY( false );
120 }
121}
122
123void test03()
124{
125 bool test __attribute__((unused)) = true;
126
127 unreliable_lock l1, l2, l3;
128
129 try
130 {
131 // test behaviour when an exception is thrown
132 unreliable_lock::throw_on = 0;
133 while (unreliable_lock::throw_on < 3)
134 {
135 unreliable_lock::count = 0;
726d3136
JW
136 try
137 {
138 std::try_lock(l1, l2, l3);
139 VERIFY( false );
140 }
141 catch (int e)
142 {
143 VERIFY( e == unreliable_lock::throw_on );
144 }
9b2b801a
JW
145 ++unreliable_lock::throw_on;
146 }
147 }
148 catch (...)
149 {
150 VERIFY( false );
151 }
152}
153
154int main()
155{
156 test01();
157 test02();
158 test03();
159 return 0;
160}