Add overloads of for_each_thread/find_thread that filter on pid
authorSimon Marchi <simon.marchi@ericsson.com>
Sat, 21 Oct 2017 16:20:21 +0000 (12:20 -0400)
committerSimon Marchi <simon.marchi@polymtl.ca>
Sat, 21 Oct 2017 16:20:21 +0000 (12:20 -0400)
It happens often that we want to iterate or find threads restricted to a
given pid.  I think it's worth having an overload to help with this.
Right now there is a single user of each of the find_thread and
for_each_thread overload, but as we replace the usages of find_inferior
with for_each_thread/find_thread, more usages will pop up.

gdb/gdbserver/ChangeLog:

* gdbthread.h (find_thread, for_each_thread): New functions.
* inferiors.c (thread_of_pid): Remove.
(find_any_thread_of_pid): Use find_thread.
* linux-low.c (num_lwps): Use for_each_thread.

gdb/gdbserver/ChangeLog
gdb/gdbserver/gdbthread.h
gdb/gdbserver/inferiors.c
gdb/gdbserver/linux-low.c

index 350843e65f225c77abd53c030a8403d0128dedbf..757265daf3a683253f21bef4cd42ea23b2e2a0f1 100644 (file)
@@ -1,3 +1,10 @@
+2017-10-21  Simon Marchi  <simon.marchi@ericsson.com>
+
+       * gdbthread.h (find_thread, for_each_thread): New functions.
+       * inferiors.c (thread_of_pid): Remove.
+       (find_any_thread_of_pid): Use find_thread.
+       * linux-low.c (num_lwps): Use for_each_thread.
+
 2017-10-17  Yao Qi  <yao.qi@linaro.org>
 
        * Makefile.in: Remove one rule.
index 8ace051036d4b1e3a3502e110090757bc95fb25d..b82d5b0e72ac8ca9673c24046dd62aa2ebb4517e 100644 (file)
@@ -111,6 +111,18 @@ find_thread (Func func)
   return NULL;
 }
 
+/* Like the above, but only consider threads with pid PID.  */
+
+template <typename Func>
+static thread_info *
+find_thread (int pid, Func func)
+{
+  return find_thread ([&] (thread_info *thread)
+    {
+      return thread->id.pid () == pid && func (thread);
+    });
+}
+
 /* Invoke FUNC for each thread.  */
 
 template <typename Func>
@@ -128,6 +140,19 @@ for_each_thread (Func func)
     }
 }
 
+/* Like the above, but only consider threads with pid PID.  */
+
+template <typename Func>
+static void
+for_each_thread (int pid, Func func)
+{
+  for_each_thread ([&] (thread_info *thread)
+    {
+      if (pid == thread->id.pid ())
+       func (thread);
+    });
+}
+
 /* Find the a random thread for which FUNC (THREAD) returns true.  If
    no entry is found then return NULL.  */
 
index 564121fdd995cbfa29c79db612484fb013474ef1..a0ece4d3514726a79d5ca70627e71586d56ab154 100644 (file)
@@ -132,23 +132,14 @@ find_thread_process (const struct process_info *const process)
   return find_any_thread_of_pid (process->pid);
 }
 
-/* Helper for find_any_thread_of_pid.  Returns true if a thread
-   matches a PID.  */
-
-static int
-thread_of_pid (thread_info *entry, void *pid_p)
-{
-  int pid = *(int *) pid_p;
-
-  return (ptid_get_pid (entry->id) == pid);
-}
-
 /* See gdbthread.h.  */
 
 struct thread_info *
 find_any_thread_of_pid (int pid)
 {
-  return find_inferior (&all_threads, thread_of_pid, &pid);
+  return find_thread (pid, [] (thread_info *thread) {
+    return true;
+  });
 }
 
 static void
index 0b93fa20c09858d0dce6b3b2434cfcddcbd43688..b367e535a999ff9181b92464d944f7a2a4f6d22b 100644 (file)
@@ -1869,10 +1869,10 @@ num_lwps (int pid)
 {
   int count = 0;
 
-  for_each_thread ([&] (thread_info *thread) {
-    if (thread->id.pid () == pid)
+  for_each_thread (pid, [&] (thread_info *thread)
+    {
       count++;
-  });
+    });
 
   return count;
 }
This page took 0.035728 seconds and 4 git commands to generate.