1 /* Thread iterators and ranges for GDB, the GNU debugger.
2 Copyright (C) 2018-2020 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "gdbsupport/filtered-iterator.h"
23 #include "gdbsupport/next-iterator.h"
24 #include "gdbsupport/safe-iterator.h"
25 #include "thread-map.h"
27 /* A forward iterator that iterates over a given inferior's
30 using inf_threads_iterator
= next_iterator
<thread_info
>;
32 /* A forward iterator that iterates over all threads of all
35 class all_threads_iterator
38 typedef all_threads_iterator self_type
;
39 typedef struct thread_info
*value_type
;
40 typedef struct thread_info
*&reference
;
41 typedef struct thread_info
**pointer
;
42 typedef std::forward_iterator_tag iterator_category
;
43 typedef int difference_type
;
48 /* Create an iterator that points to the first thread of the first
50 explicit all_threads_iterator (begin_t
);
52 /* Create a one-past-end iterator. */
53 all_threads_iterator ()
57 thread_info
*operator* () const;
59 all_threads_iterator
&operator++ ()
65 bool operator== (const all_threads_iterator
&other
) const
67 if (m_inf
!= other
.m_inf
)
70 /* Inferiors of both iterators are equal. */
72 /* They are both ended iterators. */
75 /* Do they both point to the same thread? */
76 return m_thr_iter
== other
.m_thr_iter
;
80 bool operator!= (const all_threads_iterator
&other
) const
82 return !(*this == other
);
86 /* Advance to the next thread. */
90 /* The current inferior and thread. M_INF is NULL if we reached the
91 end of the threads list of the last inferior. */
93 ptid_thread_map::const_iterator m_thr_iter
;
96 /* Iterate over all threads that match a given PTID. */
98 class all_matching_threads_iterator
101 typedef all_matching_threads_iterator self_type
;
102 typedef struct thread_info
*value_type
;
103 typedef struct thread_info
*&reference
;
104 typedef struct thread_info
**pointer
;
105 typedef std::forward_iterator_tag iterator_category
;
106 typedef int difference_type
;
108 /* Creates an iterator that iterates over all threads that match
110 all_matching_threads_iterator (process_stratum_target
*filter_target
,
113 /* Create a one-past-end iterator. */
114 all_matching_threads_iterator ()
116 m_filter_target (nullptr),
117 m_filter_ptid (minus_one_ptid
)
120 thread_info
*operator* () const;
122 all_matching_threads_iterator
&operator++ ()
128 bool operator== (const all_matching_threads_iterator
&other
) const
130 if (m_inf
!= other
.m_inf
)
133 /* Inferiors of both iterators are equal. */
135 /* They are both ended iterators. */
138 /* Do they both point to the same thread? */
139 return m_thr_iter
== other
.m_thr_iter
;
143 bool operator!= (const all_matching_threads_iterator
&other
) const
145 return !(* this == other
);
149 /* Advance to next thread, skipping filtered threads. */
152 /* True if M_INF matches the process identified by
154 bool m_inf_matches ();
157 /* The current inferior. */
160 /* The current thread. */
161 ptid_thread_map::const_iterator m_thr_iter
;
164 process_stratum_target
*m_filter_target
;
165 ptid_t m_filter_ptid
;
168 /* Filter for filtered_iterator. Filters out exited threads. */
170 struct non_exited_thread_filter
172 bool operator() (struct thread_info
*thr
) const
174 return thr
->state
!= THREAD_EXITED
;
178 /* Iterate over all non-exited threads that match a given PTID. */
180 using all_non_exited_threads_iterator
181 = filtered_iterator
<all_matching_threads_iterator
, non_exited_thread_filter
>;
183 /* Iterate over all non-exited threads of an inferior. */
185 using inf_non_exited_threads_iterator
186 = filtered_iterator
<inf_threads_iterator
, non_exited_thread_filter
>;
188 /* Iterate over all threads of all inferiors, safely. */
190 using all_threads_safe_iterator
191 = basic_safe_iterator
<all_threads_iterator
>;
193 /* Iterate over all threads of an inferior, safely. */
195 using safe_inf_threads_iterator
196 = basic_safe_iterator
<inf_threads_iterator
>;
198 /* A range adapter that makes it possible to iterate over all threads
199 of an inferior with range-for. */
201 using inf_threads_range
202 = next_adapter
<thread_info
, inf_threads_iterator
>;
204 /* A range adapter that makes it possible to iterate over all
205 non-exited threads of an inferior with range-for. */
207 using inf_non_exited_threads_range
208 = next_adapter
<thread_info
, inf_non_exited_threads_iterator
>;
210 /* A range adapter that makes it possible to iterate over all threads
211 of an inferior with range-for, safely. */
213 using safe_inf_threads_range
214 = next_adapter
<thread_info
, safe_inf_threads_iterator
>;
216 /* A range adapter that makes it possible to iterate over all threads
217 of all inferiors with range-for. */
219 struct all_threads_range
221 all_threads_iterator
begin () const
222 { return all_threads_iterator (all_threads_iterator::begin_t
{}); }
223 all_threads_iterator
end () const
224 { return all_threads_iterator (); }
227 /* A range adapter that makes it possible to iterate over all threads
228 with range-for "safely". I.e., it is safe to delete the
229 currently-iterated thread. */
231 struct all_threads_safe_range
233 all_threads_safe_iterator
begin () const
234 { return all_threads_safe_iterator (all_threads_iterator::begin_t
{}); }
235 all_threads_safe_iterator
end () const
236 { return all_threads_safe_iterator (); }
239 /* A range adapter that makes it possible to iterate over all threads
240 that match a PTID filter with range-for. */
242 struct all_matching_threads_range
245 all_matching_threads_range (process_stratum_target
*filter_target
,
247 : m_filter_target (filter_target
), m_filter_ptid (filter_ptid
)
249 all_matching_threads_range ()
250 : m_filter_target (nullptr), m_filter_ptid (minus_one_ptid
)
253 all_matching_threads_iterator
begin () const
254 { return all_matching_threads_iterator (m_filter_target
, m_filter_ptid
); }
255 all_matching_threads_iterator
end () const
256 { return all_matching_threads_iterator (); }
260 process_stratum_target
*m_filter_target
;
261 ptid_t m_filter_ptid
;
264 /* A range adapter that makes it possible to iterate over all
265 non-exited threads of all inferiors, with range-for.
266 Threads/inferiors that do not match FILTER_PTID are filtered
269 class all_non_exited_threads_range
272 all_non_exited_threads_range (process_stratum_target
*filter_target
,
274 : m_filter_target (filter_target
), m_filter_ptid (filter_ptid
)
277 all_non_exited_threads_range ()
278 : m_filter_target (nullptr), m_filter_ptid (minus_one_ptid
)
281 all_non_exited_threads_iterator
begin () const
282 { return all_non_exited_threads_iterator (m_filter_target
, m_filter_ptid
); }
283 all_non_exited_threads_iterator
end () const
284 { return all_non_exited_threads_iterator (); }
287 process_stratum_target
*m_filter_target
;
288 ptid_t m_filter_ptid
;
291 #endif /* THREAD_ITER_H */