Change inferior thread list to be a thread map
[deliverable/binutils-gdb.git] / gdb / thread-iter.c
CommitLineData
08036331
PA
1/* Thread iterators and ranges for GDB, the GNU debugger.
2
b811d2c2 3 Copyright (C) 2018-2020 Free Software Foundation, Inc.
08036331
PA
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program 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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "gdbthread.h"
22#include "inferior.h"
23
24/* See thread-iter.h. */
25
26all_threads_iterator::all_threads_iterator (begin_t)
27{
28 /* Advance M_INF/M_THR to the first thread's position. */
29 for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next)
cd9629e1
SM
30 {
31 if (!m_inf->thread_map.empty())
32 {
33 this->m_thr_iter = m_inf->thread_map.cbegin ();
34 return;
35 }
36 }
08036331
PA
37}
38
39/* See thread-iter.h. */
40
41void
42all_threads_iterator::advance ()
43{
44 /* The loop below is written in the natural way as-if we'd always
45 start at the beginning of the inferior list. This fast forwards
46 the algorithm to the actual current position. */
47 goto start;
48
49 for (; m_inf != NULL; m_inf = m_inf->next)
50 {
cd9629e1
SM
51 m_thr_iter = m_inf->thread_map.cbegin ();
52 while (m_thr_iter != m_inf->thread_map.cend ())
08036331
PA
53 {
54 return;
55 start:
cd9629e1 56 ++m_thr_iter;
08036331
PA
57 }
58 }
59}
60
cd9629e1
SM
61thread_info *all_threads_iterator::operator* () const
62{
63 gdb_assert (m_inf != NULL);
64 gdb_assert (m_thr_iter != m_inf->thread_map.cend ());
65
66 return m_thr_iter->second;
67}
68
08036331
PA
69/* See thread-iter.h. */
70
71bool
72all_matching_threads_iterator::m_inf_matches ()
73{
5b6d1e4f
PA
74 return ((m_filter_target == nullptr
75 || m_filter_target == m_inf->process_target ())
76 && (m_filter_ptid == minus_one_ptid
77 || m_filter_ptid.pid () == m_inf->pid));
08036331
PA
78}
79
80/* See thread-iter.h. */
81
82all_matching_threads_iterator::all_matching_threads_iterator
5b6d1e4f
PA
83 (process_stratum_target *filter_target, ptid_t filter_ptid)
84 : m_filter_target (filter_target),
85 m_filter_ptid (filter_ptid)
08036331 86{
5b6d1e4f
PA
87 gdb_assert ((filter_target == nullptr && filter_ptid == minus_one_ptid)
88 || filter_target->stratum () == process_stratum);
89
08036331 90 for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next)
cd9629e1
SM
91 {
92 if (!m_inf->thread_map.empty () && m_inf_matches ())
93 {
94 for (m_thr_iter = m_inf->thread_map.cbegin ();
95 m_thr_iter != m_inf->thread_map.cend ();
96 m_thr_iter++)
97 {
98 if (m_thr_iter->second->ptid.matches (m_filter_ptid))
99 return;
100 }
101 }
102 }
08036331
PA
103}
104
105/* See thread-iter.h. */
106
107void
108all_matching_threads_iterator::advance ()
109{
110 /* The loop below is written in the natural way as-if we'd always
111 start at the beginning of the inferior list. This fast forwards
112 the algorithm to the actual current position. */
113 goto start;
114
115 for (; m_inf != NULL; m_inf = m_inf->next)
116 if (m_inf_matches ())
117 {
cd9629e1
SM
118 m_thr_iter = m_inf->thread_map.cbegin ();
119 while (m_thr_iter != m_inf->thread_map.cend ())
08036331 120 {
cd9629e1 121 if (m_thr_iter->second->ptid.matches (m_filter_ptid))
08036331
PA
122 return;
123 start:
cd9629e1 124 ++m_thr_iter;
08036331
PA
125 }
126 }
127}
cd9629e1
SM
128
129thread_info *all_matching_threads_iterator::operator* () const
130{
131 gdb_assert (m_inf != nullptr);
132
133 return m_thr_iter->second;
134}
This page took 0.167971 seconds and 4 git commands to generate.