Files
gcc/libstdc++-v3/include/debug/safe_unordered_container.tcc
François Dumont 6ff4e7181c libstdc++: [_GLIBCXX_DEBUG] Reduce unordered containers mutex locks/unlocks
The unordered containers have 2 types of iterators, the usual ones and the
local_iterator to iterate through a given bucket. In _GLIBCXX_DEBUG mode there
are then 4 lists of iterators, 2 for iterator/const_iterator and 2 for
local_iterator/const_local_iterator.

This patch is making sure that the unordered container's mutex is only lock/unlock
1 time when those lists of iterators needed to be iterate for invalidation purpose.

Also remove calls to _M_check_rehashed after erase operations. Standard do not permit
to rehash on erase operation so we will never implement it.

libstdc++-v3/ChangeLog

	* include/debug/safe_unordered_container.h
	(_Safe_unordered_container::_M_invalidate_locals): Remove.
	(_Safe_unordered_container::_M_invalidate_all): Lock mutex while calling
	_M_invalidate_if and _M_invalidate_locals.
	(_Safe_unordered_container::_M_invalidate_all_if): New.
	(_Safe_unordered_container::_M_invalidate): New.
	(_Safe_unordered_container::_M_invalidate_if): Make private, add __scoped_lock
	argument.
	(_Safe_unordered_container::_M_invalidate_local_if): Likewise.
	* include/debug/safe_unordered_container.tcc
	(_Safe_unordered_container::_M_invalidate_if): Adapt and remove lock.
	(_Safe_unordered_container::_M_invalidate_local_if): Likewise.
	* include/debug/unordered_map
	(unordered_map::erase(const_iterator, const_iterator)): Lock before loop on
	iterators. Remove _M_check_rehashed call.
	(unordered_map::_M_self): New.
	(unordered_map::_M_invalidate): Remove.
	(unordered_map::_M_erase): Adapt and remove _M_check_rehashed call.
	(unordered_multimap::_M_erase(_Base_iterator, _Base_iterator)): New.
	(unordered_multimap::erase(_Kt&&)): Use latter.
	(unordered_multimap::erase(const key_type&)): Likewise.
	(unordered_multimap::erase(const_iterator, const_iterator)):
	Lock before loop on iterators. Remove _M_check_rehashed.
	(unordered_multimap::_M_self): New.
	(unordered_multimap::_M_invalidate): Remove.
	(unordered_multimap::_M_erase): Adapt. Remove _M_check_rehashed call.
	* include/debug/unordered_set
	(unordered_set::erase(const_iterator, const_iterator)): Add lock before loop
	for iterator invalidation. Remove _M_check_rehashed call.
	(unordered_set::_M_self): New.
	(unordered_set::_M_invalidate): Remove.
	(unordered_set::_M_erase): Adapt and remove _M_check_rehashed call.
	(unordered_multiset::_M_erase(_Base_iterator, _Base_iterator)): New.
	(unordered_multiset::erase(_Kt&&)): Use latter.
	(unordered_multiset::erase(const key_type&)): Likewise.
	(unordered_multiset::erase(const_iterator, const_iterator)):
	Lock before loop on iterators. Remove _M_check_rehashed.
	(unordered_multiset::_M_self): New.
	(unordered_multiset::_M_invalidate): Remove.
	(unordered_multiset::_M_erase): Adapt. Remove _M_check_rehashed call.

Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2026-03-02 19:10:14 +01:00

99 lines
3.1 KiB
C++

// Safe container implementation -*- C++ -*-
// Copyright (C) 2011-2026 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file debug/safe_unordered_container.tcc
* This file is a GNU debug extension to the Standard C++ Library.
*/
#ifndef _GLIBCXX_DEBUG_SAFE_UNORDERED_CONTAINER_TCC
#define _GLIBCXX_DEBUG_SAFE_UNORDERED_CONTAINER_TCC 1
namespace __gnu_debug
{
template<typename _Container>
template<typename _Predicate>
void
_Safe_unordered_container<_Container>::
_M_invalidate_if(_Predicate __pred, const __gnu_cxx::__scoped_lock&)
{
typedef typename _Container::iterator iterator;
typedef typename _Container::const_iterator const_iterator;
for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
{
iterator* __victim = static_cast<iterator*>(__iter);
__iter = __iter->_M_next;
if (!__victim->_M_singular() && __pred(__victim->base()))
{
__victim->_M_invalidate();
}
}
for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
{
const_iterator* __victim = static_cast<const_iterator*>(__iter2);
__iter2 = __iter2->_M_next;
if (!__victim->_M_singular() && __pred(__victim->base()))
{
__victim->_M_invalidate();
}
}
}
template<typename _Container>
template<typename _Predicate>
void
_Safe_unordered_container<_Container>::
_M_invalidate_local_if(_Predicate __pred, const __gnu_cxx::__scoped_lock&)
{
typedef typename _Container::local_iterator local_iterator;
typedef typename _Container::const_local_iterator const_local_iterator;
for (_Safe_iterator_base* __iter = _M_local_iterators; __iter;)
{
local_iterator* __victim = static_cast<local_iterator*>(__iter);
__iter = __iter->_M_next;
if (!__victim->_M_singular() && __pred(__victim->base()))
{
__victim->_M_invalidate();
}
}
for (_Safe_iterator_base* __iter2 = _M_const_local_iterators; __iter2;)
{
const_local_iterator* __victim =
static_cast<const_local_iterator*>(__iter2);
__iter2 = __iter2->_M_next;
if (!__victim->_M_singular() && __pred(__victim->base()))
{
__victim->_M_invalidate();
}
}
}
} // namespace __gnu_debug
#endif