]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/debug/safe_sequence.h
re PR libstdc++/29496 (_M_invalidate function is not thread-safe in GLIBCXX_DEBUG...
[thirdparty/gcc.git] / libstdc++-v3 / include / debug / safe_sequence.h
1 // Safe sequence implementation -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 #ifndef _GLIBCXX_DEBUG_SAFE_SEQUENCE_H
32 #define _GLIBCXX_DEBUG_SAFE_SEQUENCE_H 1
33
34 #include <debug/debug.h>
35 #include <debug/macros.h>
36 #include <debug/functions.h>
37 #include <debug/safe_base.h>
38
39 namespace __gnu_debug
40 {
41 template<typename _Iterator, typename _Sequence>
42 class _Safe_iterator;
43
44 /** A simple function object that returns true if the passed-in
45 * value is not equal to the stored value. It saves typing over
46 * using both bind1st and not_equal.
47 */
48 template<typename _Type>
49 class _Not_equal_to
50 {
51 _Type __value;
52
53 public:
54 explicit _Not_equal_to(const _Type& __v) : __value(__v) { }
55
56 bool
57 operator()(const _Type& __x) const
58 { return __value != __x; }
59 };
60
61 /** A function object that returns true when the given random access
62 iterator is at least @c n steps away from the given iterator. */
63 template<typename _Iterator>
64 class _After_nth_from
65 {
66 typedef typename std::iterator_traits<_Iterator>::difference_type
67 difference_type;
68
69 _Iterator _M_base;
70 difference_type _M_n;
71
72 public:
73 _After_nth_from(const difference_type& __n, const _Iterator& __base)
74 : _M_base(__base), _M_n(__n) { }
75
76 bool
77 operator()(const _Iterator& __x) const
78 { return __x - _M_base >= _M_n; }
79 };
80
81 /**
82 * @brief Base class for constructing a "safe" sequence type that
83 * tracks iterators that reference it.
84 *
85 * The class template %_Safe_sequence simplifies the construction of
86 * "safe" sequences that track the iterators that reference the
87 * sequence, so that the iterators are notified of changes in the
88 * sequence that may affect their operation, e.g., if the container
89 * invalidates its iterators or is destructed. This class template
90 * may only be used by deriving from it and passing the name of the
91 * derived class as its template parameter via the curiously
92 * recurring template pattern. The derived class must have @c
93 * iterator and @const_iterator types that are instantiations of
94 * class template _Safe_iterator for this sequence. Iterators will
95 * then be tracked automatically.
96 */
97 template<typename _Sequence>
98 class _Safe_sequence : public _Safe_sequence_base
99 {
100 public:
101 /** Invalidates all iterators @c x that reference this sequence,
102 are not singular, and for which @c pred(x) returns @c
103 true. The user of this routine should be careful not to make
104 copies of the iterators passed to @p pred, as the copies may
105 interfere with the invalidation. */
106 template<typename _Predicate>
107 void
108 _M_invalidate_if(_Predicate __pred);
109
110 /** Transfers all iterators that reference this memory location
111 to this sequence from whatever sequence they are attached
112 to. */
113 template<typename _Iterator>
114 void
115 _M_transfer_iter(const _Safe_iterator<_Iterator, _Sequence>& __x);
116 };
117
118 template<typename _Sequence>
119 template<typename _Predicate>
120 void
121 _Safe_sequence<_Sequence>::
122 _M_invalidate_if(_Predicate __pred)
123 {
124 typedef typename _Sequence::iterator iterator;
125 typedef typename _Sequence::const_iterator const_iterator;
126
127 __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
128 for (_Safe_iterator_base* __iter = _M_iterators; __iter;)
129 {
130 iterator* __victim = static_cast<iterator*>(__iter);
131 __iter = __iter->_M_next;
132 if (!__victim->_M_singular())
133 {
134 if (__pred(__victim->base()))
135 __victim->_M_invalidate_single();
136 }
137 }
138
139 for (_Safe_iterator_base* __iter2 = _M_const_iterators; __iter2;)
140 {
141 const_iterator* __victim = static_cast<const_iterator*>(__iter2);
142 __iter2 = __iter2->_M_next;
143 if (!__victim->_M_singular())
144 {
145 if (__pred(__victim->base()))
146 __victim->_M_invalidate_single();
147 }
148 }
149 }
150
151 template<typename _Sequence>
152 template<typename _Iterator>
153 void
154 _Safe_sequence<_Sequence>::
155 _M_transfer_iter(const _Safe_iterator<_Iterator, _Sequence>& __x)
156 {
157 _Safe_sequence_base* __from = __x._M_sequence;
158 if (!__from)
159 return;
160
161 typedef typename _Sequence::iterator iterator;
162 typedef typename _Sequence::const_iterator const_iterator;
163
164 __gnu_cxx::__scoped_lock sentry(this->_M_get_mutex());
165 for (_Safe_iterator_base* __iter = __from->_M_iterators; __iter;)
166 {
167 iterator* __victim = static_cast<iterator*>(__iter);
168 __iter = __iter->_M_next;
169 if (!__victim->_M_singular() && __victim->base() == __x.base())
170 __victim->_M_attach_single(static_cast<_Sequence*>(this));
171 }
172
173 for (_Safe_iterator_base* __iter2 = __from->_M_const_iterators;
174 __iter2;)
175 {
176 const_iterator* __victim = static_cast<const_iterator*>(__iter2);
177 __iter2 = __iter2->_M_next;
178 if (!__victim->_M_singular() && __victim->base() == __x.base())
179 __victim->_M_attach_single(static_cast<_Sequence*>(this));
180 }
181 }
182 } // namespace __gnu_debug
183
184 #endif