This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 3/3] Remove find_inferior usage for thread_search


From: Simon Marchi <simon.marchi@ericsson.com>

Replace it with for_each_thread.  While at it, we can inline the
callback code.  One little change is that I am using the
prev_general_thread variable instead of current_gen_ptid, since they
should have the same value.

gdb/gdbserver/ChangeLog:

	* target.c (struct thread_search): Remove.
	(thread_search_callback): Remove.
	(prepare_to_access_memory): Use for_each_thread instead of
	find_inferior.  Inline code from thread_search_callback.
---
 gdb/gdbserver/target.c | 88 ++++++++++++++++++--------------------------------
 1 file changed, 32 insertions(+), 56 deletions(-)

diff --git a/gdb/gdbserver/target.c b/gdb/gdbserver/target.c
index 8f757c0a5d..204f941f20 100644
--- a/gdb/gdbserver/target.c
+++ b/gdb/gdbserver/target.c
@@ -32,51 +32,6 @@ set_desired_thread ()
   return (current_thread != NULL);
 }
 
-/* Structure used to look up a thread to use as current when accessing
-   memory.  */
-
-struct thread_search
-{
-  /* The PTID of the current general thread.  This is an input
-     parameter.  */
-  ptid_t current_gen_ptid;
-
-  /* The first thread found.  */
-  struct thread_info *first;
-
-  /* The first stopped thread found.  */
-  struct thread_info *stopped;
-
-  /* The current general thread, if found.  */
-  struct thread_info *current;
-};
-
-/* Callback for find_inferior.  Search for a thread to use as current
-   when accessing memory.  */
-
-static int
-thread_search_callback (thread_info *thread, void *args)
-{
-  struct thread_search *s = (struct thread_search *) args;
-
-  if (thread->id.pid () == ptid_get_pid (s->current_gen_ptid)
-      && mythread_alive (ptid_of (thread)))
-    {
-      if (s->stopped == NULL
-	  && the_target->thread_stopped != NULL
-	  && thread_stopped (thread))
-	s->stopped = thread;
-
-      if (s->first == NULL)
-	s->first = thread;
-
-      if (s->current == NULL && s->current_gen_ptid == thread->id)
-	s->current = thread;
-    }
-
-  return 0;
-}
-
 /* The thread that was current before prepare_to_access_memory was
    called.  done_accessing_memory uses this to restore the previous
    selected thread.  */
@@ -87,11 +42,15 @@ static ptid_t prev_general_thread;
 int
 prepare_to_access_memory (void)
 {
-  struct thread_search search;
-  struct thread_info *thread;
+  /* The first thread found.  */
+  struct thread_info *first = NULL;
+  /* The first stopped thread found.  */
+  struct thread_info *stopped = NULL;
+  /* The current general thread, if found.  */
+  struct thread_info *current = NULL;
 
-  memset (&search, 0, sizeof (search));
-  search.current_gen_ptid = general_thread;
+  /* Save the general thread value, since prepare_to_access_memory could change
+     it.  */
   prev_general_thread = general_thread;
 
   if (the_target->prepare_to_access_memory != NULL)
@@ -103,18 +62,35 @@ prepare_to_access_memory (void)
 	return res;
     }
 
-  find_inferior (&all_threads, thread_search_callback, &search);
+  for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
+    {
+      if (mythread_alive (thread->id))
+	{
+	  if (stopped == NULL && the_target->thread_stopped != NULL
+	      && thread_stopped (thread))
+	    stopped = thread;
+
+	  if (first == NULL)
+	    first = thread;
+
+	  if (current == NULL && prev_general_thread == thread->id)
+	    current = thread;
+	}
+    });
+
+  /* The thread we end up choosing.  */
+  struct thread_info *thread;
 
   /* Prefer a stopped thread.  If none is found, try the current
      thread.  Otherwise, take the first thread in the process.  If
      none is found, undo the effects of
      target->prepare_to_access_memory() and return error.  */
-  if (search.stopped != NULL)
-    thread = search.stopped;
-  else if (search.current != NULL)
-    thread = search.current;
-  else if (search.first != NULL)
-    thread = search.first;
+  if (stopped != NULL)
+    thread = stopped;
+  else if (current != NULL)
+    thread = current;
+  else if (first != NULL)
+    thread = first;
   else
     {
       done_accessing_memory ();
-- 
2.14.2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]