+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.
 
   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>
     }
 }
 
+/* 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.  */
 
 
   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
 
 {
   int count = 0;
 
-  for_each_thread ([&] (thread_info *thread) {
-    if (thread->id.pid () == pid)
+  for_each_thread (pid, [&] (thread_info *thread)
+    {
       count++;
-  });
+    });
 
   return count;
 }