1 /* Thread iterators and ranges for GDB, the GNU debugger.
2 
3    Copyright (C) 2018-2021 Free Software Foundation, Inc.
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 
all_threads_iterator(begin_t)26 all_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)
30     if ((m_thr = m_inf->thread_list) != NULL)
31       return;
32 }
33 
34 /* See thread-iter.h.  */
35 
36 void
advance()37 all_threads_iterator::advance ()
38 {
39   /* The loop below is written in the natural way as-if we'd always
40      start at the beginning of the inferior list.  This fast forwards
41      the algorithm to the actual current position.  */
42   goto start;
43 
44   for (; m_inf != NULL; m_inf = m_inf->next)
45     {
46       m_thr = m_inf->thread_list;
47       while (m_thr != NULL)
48 	{
49 	  return;
50 	start:
51 	  m_thr = m_thr->next;
52 	}
53     }
54 }
55 
56 /* See thread-iter.h.  */
57 
58 bool
m_inf_matches()59 all_matching_threads_iterator::m_inf_matches ()
60 {
61   return ((m_filter_target == nullptr
62 	   || m_filter_target == m_inf->process_target ())
63 	  && (m_filter_ptid == minus_one_ptid
64 	      || m_filter_ptid.pid () == m_inf->pid));
65 }
66 
67 /* See thread-iter.h.  */
68 
all_matching_threads_iterator(process_stratum_target * filter_target,ptid_t filter_ptid)69 all_matching_threads_iterator::all_matching_threads_iterator
70   (process_stratum_target *filter_target, ptid_t filter_ptid)
71     : m_filter_target (filter_target),
72       m_filter_ptid (filter_ptid)
73 {
74   gdb_assert ((filter_target == nullptr && filter_ptid == minus_one_ptid)
75 	      || filter_target->stratum () == process_stratum);
76 
77   m_thr = nullptr;
78   for (m_inf = inferior_list; m_inf != NULL; m_inf = m_inf->next)
79     if (m_inf_matches ())
80       for (m_thr = m_inf->thread_list; m_thr != NULL; m_thr = m_thr->next)
81 	if (m_thr->ptid.matches (m_filter_ptid))
82 	  return;
83 }
84 
85 /* See thread-iter.h.  */
86 
87 void
advance()88 all_matching_threads_iterator::advance ()
89 {
90   /* The loop below is written in the natural way as-if we'd always
91      start at the beginning of the inferior list.  This fast forwards
92      the algorithm to the actual current position.  */
93   goto start;
94 
95   for (; m_inf != NULL; m_inf = m_inf->next)
96     if (m_inf_matches ())
97       {
98 	m_thr = m_inf->thread_list;
99 	while (m_thr != NULL)
100 	  {
101 	    if (m_thr->ptid.matches (m_filter_ptid))
102 	      return;
103 	  start:
104 	    m_thr = m_thr->next;
105 	  }
106       }
107 }
108