Change inferior thread list to be a thread map
[deliverable/binutils-gdb.git] / gdb / thread-iter.h
CommitLineData
08036331 1/* Thread iterators and ranges for GDB, the GNU debugger.
b811d2c2 2 Copyright (C) 2018-2020 Free Software Foundation, Inc.
08036331
PA
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19#ifndef THREAD_ITER_H
20#define THREAD_ITER_H
21
268a13a5
TT
22#include "gdbsupport/filtered-iterator.h"
23#include "gdbsupport/next-iterator.h"
24#include "gdbsupport/safe-iterator.h"
cd9629e1 25#include "thread-map.h"
08036331
PA
26
27/* A forward iterator that iterates over a given inferior's
28 threads. */
29
7cf47dc4 30using inf_threads_iterator = next_iterator<thread_info>;
08036331
PA
31
32/* A forward iterator that iterates over all threads of all
33 inferiors. */
34
35class all_threads_iterator
36{
37public:
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;
44
45 /* Tag type. */
46 struct begin_t {};
47
48 /* Create an iterator that points to the first thread of the first
49 inferior. */
50 explicit all_threads_iterator (begin_t);
51
52 /* Create a one-past-end iterator. */
53 all_threads_iterator ()
cd9629e1 54 : m_inf (nullptr)
08036331
PA
55 {}
56
cd9629e1 57 thread_info *operator* () const;
08036331
PA
58
59 all_threads_iterator &operator++ ()
60 {
61 advance ();
62 return *this;
63 }
64
65 bool operator== (const all_threads_iterator &other) const
cd9629e1
SM
66 {
67 if (m_inf != other.m_inf)
68 return false;
69
70 /* Inferiors of both iterators are equal. */
71 if (m_inf == NULL) {
72 /* They are both ended iterators. */
73 return true;
74 } else {
75 /* Do they both point to the same thread? */
76 return m_thr_iter == other.m_thr_iter;
77 }
78 }
08036331
PA
79
80 bool operator!= (const all_threads_iterator &other) const
cd9629e1
SM
81 {
82 return !(*this == other);
83 }
08036331
PA
84
85private:
86 /* Advance to the next thread. */
87 void advance ();
88
89private:
cd9629e1 90 /* The current inferior and thread. M_INF is NULL if we reached the
08036331
PA
91 end of the threads list of the last inferior. */
92 inferior *m_inf;
cd9629e1 93 ptid_thread_map::const_iterator m_thr_iter;
08036331
PA
94};
95
96/* Iterate over all threads that match a given PTID. */
97
98class all_matching_threads_iterator
99{
100public:
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;
107
108 /* Creates an iterator that iterates over all threads that match
109 FILTER_PTID. */
5b6d1e4f
PA
110 all_matching_threads_iterator (process_stratum_target *filter_target,
111 ptid_t filter_ptid);
08036331
PA
112
113 /* Create a one-past-end iterator. */
114 all_matching_threads_iterator ()
115 : m_inf (nullptr),
5b6d1e4f 116 m_filter_target (nullptr),
08036331
PA
117 m_filter_ptid (minus_one_ptid)
118 {}
119
cd9629e1 120 thread_info *operator* () const;
08036331
PA
121
122 all_matching_threads_iterator &operator++ ()
123 {
124 advance ();
125 return *this;
126 }
127
128 bool operator== (const all_matching_threads_iterator &other) const
cd9629e1
SM
129 {
130 if (m_inf != other.m_inf)
131 return false;
132
133 /* Inferiors of both iterators are equal. */
134 if (m_inf == NULL) {
135 /* They are both ended iterators. */
136 return true;
137 } else {
138 /* Do they both point to the same thread? */
139 return m_thr_iter == other.m_thr_iter;
140 }
141 }
08036331
PA
142
143 bool operator!= (const all_matching_threads_iterator &other) const
cd9629e1
SM
144 {
145 return !(* this == other);
146 }
08036331
PA
147
148private:
149 /* Advance to next thread, skipping filtered threads. */
150 void advance ();
151
152 /* True if M_INF matches the process identified by
153 M_FILTER_PTID. */
154 bool m_inf_matches ();
155
156private:
157 /* The current inferior. */
158 inferior *m_inf;
159
160 /* The current thread. */
cd9629e1 161 ptid_thread_map::const_iterator m_thr_iter;
08036331
PA
162
163 /* The filter. */
5b6d1e4f 164 process_stratum_target *m_filter_target;
08036331
PA
165 ptid_t m_filter_ptid;
166};
167
168/* Filter for filtered_iterator. Filters out exited threads. */
169
170struct non_exited_thread_filter
171{
172 bool operator() (struct thread_info *thr) const
173 {
174 return thr->state != THREAD_EXITED;
175 }
176};
177
178/* Iterate over all non-exited threads that match a given PTID. */
179
180using all_non_exited_threads_iterator
181 = filtered_iterator<all_matching_threads_iterator, non_exited_thread_filter>;
182
183/* Iterate over all non-exited threads of an inferior. */
184
185using inf_non_exited_threads_iterator
186 = filtered_iterator<inf_threads_iterator, non_exited_thread_filter>;
187
188/* Iterate over all threads of all inferiors, safely. */
189
190using all_threads_safe_iterator
191 = basic_safe_iterator<all_threads_iterator>;
192
193/* Iterate over all threads of an inferior, safely. */
194
195using safe_inf_threads_iterator
196 = basic_safe_iterator<inf_threads_iterator>;
197
198/* A range adapter that makes it possible to iterate over all threads
199 of an inferior with range-for. */
200
201using inf_threads_range
7cf47dc4 202 = next_adapter<thread_info, inf_threads_iterator>;
08036331
PA
203
204/* A range adapter that makes it possible to iterate over all
205 non-exited threads of an inferior with range-for. */
206
207using inf_non_exited_threads_range
7cf47dc4 208 = next_adapter<thread_info, inf_non_exited_threads_iterator>;
08036331
PA
209
210/* A range adapter that makes it possible to iterate over all threads
211 of an inferior with range-for, safely. */
212
213using safe_inf_threads_range
7cf47dc4 214 = next_adapter<thread_info, safe_inf_threads_iterator>;
08036331
PA
215
216/* A range adapter that makes it possible to iterate over all threads
217 of all inferiors with range-for. */
218
219struct all_threads_range
220{
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 (); }
225};
226
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. */
230
231struct all_threads_safe_range
232{
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 (); }
237};
238
239/* A range adapter that makes it possible to iterate over all threads
240 that match a PTID filter with range-for. */
241
242struct all_matching_threads_range
243{
244public:
5b6d1e4f
PA
245 all_matching_threads_range (process_stratum_target *filter_target,
246 ptid_t filter_ptid)
247 : m_filter_target (filter_target), m_filter_ptid (filter_ptid)
08036331
PA
248 {}
249 all_matching_threads_range ()
5b6d1e4f 250 : m_filter_target (nullptr), m_filter_ptid (minus_one_ptid)
08036331
PA
251 {}
252
253 all_matching_threads_iterator begin () const
5b6d1e4f 254 { return all_matching_threads_iterator (m_filter_target, m_filter_ptid); }
08036331
PA
255 all_matching_threads_iterator end () const
256 { return all_matching_threads_iterator (); }
257
258private:
259 /* The filter. */
5b6d1e4f 260 process_stratum_target *m_filter_target;
08036331
PA
261 ptid_t m_filter_ptid;
262};
263
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
267 out. */
268
269class all_non_exited_threads_range
270{
271public:
5b6d1e4f
PA
272 all_non_exited_threads_range (process_stratum_target *filter_target,
273 ptid_t filter_ptid)
274 : m_filter_target (filter_target), m_filter_ptid (filter_ptid)
08036331
PA
275 {}
276
277 all_non_exited_threads_range ()
5b6d1e4f 278 : m_filter_target (nullptr), m_filter_ptid (minus_one_ptid)
08036331
PA
279 {}
280
281 all_non_exited_threads_iterator begin () const
5b6d1e4f 282 { return all_non_exited_threads_iterator (m_filter_target, m_filter_ptid); }
08036331
PA
283 all_non_exited_threads_iterator end () const
284 { return all_non_exited_threads_iterator (); }
285
286private:
5b6d1e4f 287 process_stratum_target *m_filter_target;
08036331
PA
288 ptid_t m_filter_ptid;
289};
290
291#endif /* THREAD_ITER_H */
This page took 0.174019 seconds and 4 git commands to generate.