Merge from mainline (167278:168000).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / testsuite / 30_threads / lock / 4.cc
blobc71a8a0aeaceb5ff25c038c023e1196d83bedbed
1 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* } }
2 // { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* } }
3 // { dg-options " -std=gnu++0x -pthreads" { target *-*-solaris* } }
4 // { dg-options " -std=gnu++0x " { target *-*-cygwin *-*-darwin* } }
5 // { dg-require-cstdint "" }
6 // { dg-require-gthreads "" }
8 // Copyright (C) 2010 Free Software Foundation, Inc.
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.
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.
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/>.
26 #include <mutex>
27 #include <testsuite_hooks.h>
29 struct unreliable_lock
31 std::mutex m;
32 std::unique_lock<std::mutex> l;
34 static int count;
35 static int throw_on;
36 static int lock_on;
38 unreliable_lock() : l(m, std::defer_lock) { }
40 ~unreliable_lock()
42 bool test __attribute__((unused)) = true;
43 VERIFY( !l.owns_lock() );
46 void lock()
48 if (count == throw_on)
49 throw throw_on;
50 ++count;
51 l.lock();
53 bool try_lock()
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();
64 void unlock()
66 bool test __attribute__((unused)) = true;
67 VERIFY( l.owns_lock() );
68 l.unlock();
73 int unreliable_lock::count = 0;
74 int unreliable_lock::throw_on = -1;
75 int unreliable_lock::lock_on = -1;
77 void test01()
79 bool test __attribute__((unused)) = true;
81 unreliable_lock l1, l2, l3;
83 try
85 unreliable_lock::count = 0;
86 std::lock(l1, l2, l3);
87 VERIFY( unreliable_lock::count == 3 );
88 l1.unlock();
89 l2.unlock();
90 l3.unlock();
92 catch (...)
94 VERIFY( false );
98 void test02()
100 bool test __attribute__((unused)) = true;
102 // test behaviour when a lock is already held
105 unreliable_lock::lock_on = 1;
106 while (unreliable_lock::lock_on < 3)
108 unreliable_lock::count = 0;
109 unreliable_lock l1, l2, l3;
110 std::lock(l1, l2, l3);
111 VERIFY( unreliable_lock::count > 3 );
112 l1.unlock();
113 l2.unlock();
114 l3.unlock();
115 ++unreliable_lock::lock_on;
118 catch (...)
120 VERIFY( false );
124 void test03()
126 // test behaviour when an exception is thrown
127 unreliable_lock::throw_on = 0;
128 while (unreliable_lock::throw_on < 3)
130 unreliable_lock::count = 0;
131 unreliable_lock l1, l2, l3;
132 bool test = false;
135 std::lock(l1, l2, l3);
137 catch (...)
139 test = true;
141 VERIFY( test );
142 ++unreliable_lock::throw_on;
146 int main()
148 test01();
149 test02();
150 test03();
151 return 0;