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 "" }
7 // { dg-require-debug-mode "" }
8 // Copyright (C) 2010 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/>.
26 // This test check for potential deadlock when swaping sequences in debug
27 // mode as it requires acquiring 2 locks at the same time.
32 #include <testsuite_hooks.h>
34 // The following function mimic the one in src/debug.cc to associate a mutex
35 // to a given safe sequence instance.
37 get_index(std::vector<int>& v)
39 const size_t mask = 0xf;
40 // We have to check the address of the internal safe sequence that starts
41 // after the normal vector memory footprint that is to say a 3 pointers
43 void* __address = reinterpret_cast<char*>(&v) + 3 * sizeof(void*);
44 return std::_Hash_impl::hash(__address) & mask;
50 bool test __attribute__((unused)) = true;
52 vector<shared_ptr<vector<int> > > vs;
53 vector<int> *pv3 = 0, *pv4 = 0;
54 const int nb_attempts = 100;
55 for (int i = 0; i != nb_attempts; ++i)
57 vs.push_back(shared_ptr<vector<int> >(new vector<int>()));
60 if (get_index(*vs.back()) == get_index(v1))
61 pv3 = vs.back().get();
65 if (get_index(*vs.back()) == get_index(v2))
67 pv4 = vs.back().get();
74 // Maybe an other time...
77 vector<int> &v3 = *pv3, &v4 = *pv4;
79 // v1 and v3 shares the same mutex instance, like v2 and v4
80 // thread t1 lock v1 and v2
81 thread t1([&v1, &v2]()
83 for (int i = 0; i != 1000; ++i)
86 // thread t2 lock v4 and v3
87 thread t2([&v3, &v4]()
89 for (int i = 0; i != 1000; ++i)