]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/try_lock/4.cc
09ed5e7da6e8f5889a3060724a270b991f4a8258
1 // { dg-do run { target *-*-freebsd* *-*-netbsd* *-*-linux* *-*-solaris* *-*-cygwin *-*-darwin* alpha*-*-osf* mips-sgi-irix6* powerpc-ibm-aix* } }
2 // { dg-options " -std=gnu++0x -pthread" { target *-*-freebsd* *-*-netbsd* *-*-linux* alpha*-*-osf* mips-sgi-irix6* powerpc-ibm-aix* } }
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, 2011, 2012 Free Software Foundation, Inc.
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)
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/>.
27 #include <testsuite_hooks.h>
29 struct unreliable_lock
32 std::unique_lock
<std::mutex
> l
;
38 unreliable_lock() : l(m
, std::defer_lock
) { }
42 bool test
__attribute__((unused
)) = true;
43 VERIFY( !l
.owns_lock() );
48 if (count
== throw_on
)
55 if (count
== throw_on
)
57 std::unique_lock
<std::mutex
> l2(m
, std::defer_lock
);
66 bool test
__attribute__((unused
)) = true;
67 VERIFY( l
.owns_lock() );
73 int unreliable_lock::count
= 0;
74 int unreliable_lock::throw_on
= -1;
75 int unreliable_lock::lock_on
= -1;
79 bool test
__attribute__((unused
)) = true;
81 unreliable_lock l1
, l2
, l3
;
85 unreliable_lock::count
= 0;
86 int result
= std::try_lock(l1
, l2
, l3
);
87 VERIFY( result
== -1 );
88 VERIFY( unreliable_lock::count
== 3 );
101 bool test
__attribute__((unused
)) = true;
103 unreliable_lock l1
, l2
, l3
;
107 // test behaviour when a lock is already held
108 unreliable_lock::lock_on
= 0;
109 while (unreliable_lock::lock_on
< 3)
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
;
125 bool test
__attribute__((unused
)) = true;
127 unreliable_lock l1
, l2
, l3
;
131 // test behaviour when an exception is thrown
132 unreliable_lock::throw_on
= 0;
133 while (unreliable_lock::throw_on
< 3)
135 unreliable_lock::count
= 0;
136 int failed
= std::try_lock(l1
, l2
, l3
);
137 VERIFY( failed
== unreliable_lock::throw_on
);
138 ++unreliable_lock::throw_on
;