2 // { dg-options "-pthread" }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-effective-target pthread }
5 // { dg-require-gthreads "" }
6 // { dg-require-debug-mode "" }
7 // Copyright (C) 2010-2025 Free Software Foundation, Inc.
9 // This file is part of the GNU ISO C++ Library. This library is free
10 // software; you can redistribute it and/or modify it under the
11 // terms of the GNU General Public License as published by the
12 // Free Software Foundation; either version 3, or (at your option)
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
25 // This test check for potential deadlock when swaping sequences in debug
26 // 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
;
51 vector
<shared_ptr
<vector
<int> > > vs
;
52 vector
<int> *pv3
= 0, *pv4
= 0;
53 const int nb_attempts
= 100;
54 for (int i
= 0; i
!= nb_attempts
; ++i
)
56 vs
.push_back(shared_ptr
<vector
<int> >(new vector
<int>()));
59 if (get_index(*vs
.back()) == get_index(v1
))
60 pv3
= vs
.back().get();
64 if (get_index(*vs
.back()) == get_index(v2
))
66 pv4
= vs
.back().get();
73 // Maybe an other time...
76 vector
<int> &v3
= *pv3
, &v4
= *pv4
;
78 // v1 and v3 shares the same mutex instance, like v2 and v4
79 // thread t1 lock v1 and v2
80 thread
t1([&v1
, &v2
]()
82 for (int i
= 0; i
!= 1000; ++i
)
85 // thread t2 lock v4 and v3
86 thread
t2([&v3
, &v4
]()
88 for (int i
= 0; i
!= 1000; ++i
)