[binutils-gdb] Remove for_each_thread

Tom Tromey via Gdb-cvs <[email protected]> Wed, 22 Jul 2026 18:21:59 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D2e9aaf945511=
8cee476ea6c4e6aa78477fa84a8e

commit 2e9aaf9455118cee476ea6c4e6aa78477fa84a8e
Author: Tom Tromey <[email protected]>
Date:   Wed May 13 09:57:06 2026 -0600

    Remove for_each_thread
   =20
    This patch removes the for_each_thread function, changing the callers
    to use 'foreach' loops instead.  In general I think loops with
    iterators should be preferred over callback-based approaches -- they
    are easier to read and often result in less source code as well.  For
    example, in this patch a helper function is inlined into its sole
    caller.
   =20
    Approved-By: Simon Marchi <[email protected]>

Diff:
---
 gdb/breakpoint.c |  6 ++----
 gdb/gdbthread.h  | 16 ----------------
 gdb/infcmd.c     | 55 ++++++++++++++++++++++++++--------------------------=
---
 gdb/mi/mi-main.c | 36 ++++++++++++++++--------------------
 gdb/thread.c     |  9 ---------
 5 files changed, 44 insertions(+), 78 deletions(-)

diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index e4df4df04a7..ca600a845e5 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -12677,10 +12677,8 @@ delete_breakpoint (struct breakpoint *bpt)
      event-top.c won't do anything, and temporary breakpoints with
      commands won't work.  */
=20
-  for_each_thread ([&] (struct thread_info *th)
-    {
-      bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
-    });
+  for (auto &th : all_threads ())
+    bpstat_remove_bp_location (th.control.stop_bpstat, bpt);
=20
   /* Now that breakpoint is removed from breakpoint list, update the
      global location list.  This will remove locations that used to
diff --git a/gdb/gdbthread.h b/gdb/gdbthread.h
index 1bdbf621982..224f1cc8621 100644
--- a/gdb/gdbthread.h
+++ b/gdb/gdbthread.h
@@ -791,22 +791,6 @@ extern struct thread_info *any_live_thread_of_inferior=
 (inferior *inf);
 void thread_change_ptid (process_stratum_target *targ,
 			 ptid_t old_ptid, ptid_t new_ptid);
=20
-/* Callback function type for function for_each_thread.  */
-
-using for_each_thread_callback_ftype
-  =3D gdb::function_view<void (thread_info *)>;
-
-/* Call CALLBACK once for each known thread.
-
-   CALLBACK must not delete the thread.  To delete threads, use:
-
-     for (thread_info &t : all_threads_safe ())
-       if (some_condition ())
-	 delete &t;
-*/
-
-extern void for_each_thread (for_each_thread_callback_ftype callback);
-
 /* Callback function type for function find_thread.  */
=20
 using find_thread_callback_ftype =3D gdb::function_view<bool (thread_info =
*)>;
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index f1aa552d9bc..3e943123519 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -685,29 +685,6 @@ starti_command (const char *args, int from_tty)
   run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN);
 }
=20
-static void
-proceed_thread_callback (struct thread_info *thread)
-{
-  /* We go through all threads individually instead of compressing
-     into a single target `resume_all' request, because some threads
-     may be stopped in internal breakpoints/events, or stopped waiting
-     for its turn in the displaced stepping queue (that is, they are
-     running from the user's perspective but internally stopped).  The
-     target side has no idea about why the thread is stopped, so a
-     `resume_all' command would resume too much.  If/when GDB gains a
-     way to tell the target `hold this thread stopped until I say
-     otherwise', then we can optimize this.  */
-  if (thread->state () !=3D THREAD_STOPPED)
-    return;
-
-  if (!thread->inf->has_execution ())
-    return;
-
-  switch_to_thread (thread);
-  clear_proceed_status (0);
-  proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
-}
-
 static void
 ensure_valid_thread (void)
 {
@@ -762,15 +739,35 @@ continue_1 (bool all_threads_p)
       scoped_disable_commit_resumed disable_commit_resumed
 	("continue all threads in non-stop");
=20
-      for_each_thread (proceed_thread_callback);
+      for (auto &thread : all_threads ())
+	{
+	  /* We go through all threads individually instead of compressing
+	     into a single target `resume_all' request, because some threads
+	     may be stopped in internal breakpoints/events, or stopped waiting
+	     for its turn in the displaced stepping queue (that is, they are
+	     running from the user's perspective but internally stopped).  The
+	     target side has no idea about why the thread is stopped, so a
+	     `resume_all' command would resume too much.  If/when GDB gains a
+	     way to tell the target `hold this thread stopped until I say
+	     otherwise', then we can optimize this.  */
+	  if (thread.state () !=3D THREAD_STOPPED)
+	    continue;
+
+	  if (!thread.inf->has_execution ())
+	    continue;
+
+	  switch_to_thread (&thread);
+	  clear_proceed_status (0);
+	  proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
+	}
=20
       if (current_ui->prompt_state =3D=3D PROMPT_BLOCKED)
 	{
-	  /* If all threads in the target were already running,
-	     proceed_thread_callback ends up never calling proceed,
-	     and so nothing calls this to put the inferior's terminal
-	     settings in effect and remove stdin from the event loop,
-	     which we must when running a foreground command.  E.g.:
+	  /* If all threads in the target were already running, the
+	     above ends up never calling proceed, and so nothing calls
+	     this to put the inferior's terminal settings in effect
+	     and remove stdin from the event loop, which we must when
+	     running a foreground command.  E.g.:
=20
 	      (gdb) c -a&
 	      Continuing.
diff --git a/gdb/mi/mi-main.c b/gdb/mi/mi-main.c
index cc3ed6d3358..a3c78f9f20a 100644
--- a/gdb/mi/mi-main.c
+++ b/gdb/mi/mi-main.c
@@ -278,10 +278,8 @@ exec_continue (const char *const *argv, int argc)
 	      pid =3D inf->pid;
 	    }
=20
-	  for_each_thread ([&] (struct thread_info *thread)
-	    {
-	      proceed_thread (thread, pid);
-	    });
+	  for (auto &thread : all_threads ())
+	    proceed_thread (&thread, pid);
 	  disable_commit_resumed.reset_and_commit ();
 	}
       else
@@ -361,16 +359,16 @@ mi_cmd_exec_interrupt (const char *command, const cha=
r *const *argv, int argc)
       scoped_disable_commit_resumed disable_commit_resumed
 	("interrupting all threads of thread group");
=20
-      for_each_thread ([&] (struct thread_info *thread)
+      for (auto &thread : all_threads ())
 	{
-	  if (thread->state () !=3D THREAD_RUNNING)
-	    return;
+	  if (thread.state () !=3D THREAD_RUNNING)
+	    continue;
=20
-	  if (thread->ptid.pid () !=3D inf->pid)
-	    return;
+	  if (thread.ptid.pid () !=3D inf->pid)
+	    continue;
=20
-	  target_stop (thread->ptid);
-	});
+	  target_stop (thread.ptid);
+	}
     }
   else
     {
@@ -605,16 +603,14 @@ print_one_inferior (struct inferior *inferior, bool r=
ecurse,
=20
       if (inferior->pid !=3D 0)
 	{
-	  for_each_thread ([&] (struct thread_info *ti)
-	    {
-	      if (ti->ptid.pid () =3D=3D inferior->pid)
-		{
-		  int core =3D target_core_of_thread (ti->ptid);
+	  for (auto &ti : all_threads ())
+	    if (ti.ptid.pid () =3D=3D inferior->pid)
+	      {
+		int core =3D target_core_of_thread (ti.ptid);
=20
-		  if (core !=3D -1)
-		    cores.insert (core);
-		}
-	    });
+		if (core !=3D -1)
+		  cores.insert (core);
+	      }
 	}
=20
       if (!cores.empty ())
diff --git a/gdb/thread.c b/gdb/thread.c
index 96c733e0629..7815d76b5e2 100644
--- a/gdb/thread.c
+++ b/gdb/thread.c
@@ -602,15 +602,6 @@ find_thread_by_handle (gdb::array_view<const gdb_byte>=
 handle,
=20
 /* See gdbthread.h.  */
=20
-void
-for_each_thread (for_each_thread_callback_ftype callback)
-{
-  for (thread_info &tp : all_threads ())
-    callback (&tp);
-}
-
-/* See gdbthread.h.  */
-
 struct thread_info *
 find_thread (find_thread_callback_ftype callback)
 {