Change inferior thread list to be a thread map
[deliverable/binutils-gdb.git] / gdb / thread.c
index 03fdc05140c1b5862fa1f71a4e27ed556e3cf1a3..20b83ea3afc2dee708d53c4050c7ece55ca12231 100644 (file)
@@ -226,7 +226,7 @@ init_thread_list (void)
       else
        set_thread_exited (tp, 1);
 
-      inf->thread_list = NULL;
+      inf->thread_map.clear();
     }
 }
 
@@ -238,16 +238,10 @@ new_thread (struct inferior *inf, ptid_t ptid)
 {
   thread_info *tp = new thread_info (inf, ptid);
 
-  if (inf->thread_list == NULL)
-    inf->thread_list = tp;
-  else
-    {
-      struct thread_info *last;
+  /* A thread with this ptid should not exist yet.  */
+  gdb_assert (inf->thread_map.find (ptid) == inf->thread_map.end ());
 
-      for (last = inf->thread_list; last->next != NULL; last = last->next)
-       ;
-      last->next = tp;
-    }
+  inf->thread_map[ptid] = tp;
 
   return tp;
 }
@@ -453,33 +447,25 @@ thread_step_over_chain_remove (struct thread_info *tp)
    THR must not be NULL or a failed assertion will be raised.  */
 
 static void
-delete_thread_1 (thread_info *thr, bool silent)
+delete_thread_1 (thread_info *thr, bool silent, bool remove)
 {
   gdb_assert (thr != nullptr);
 
-  struct thread_info *tp, *tpprev = NULL;
-
-  for (tp = thr->inf->thread_list; tp; tpprev = tp, tp = tp->next)
-    if (tp == thr)
-      break;
+  set_thread_exited (thr, silent);
 
-  if (!tp)
-    return;
-
-  set_thread_exited (tp, silent);
-
-  if (!tp->deletable ())
+  if (!thr->deletable ())
     {
        /* Will be really deleted some other time.  */
        return;
      }
 
-  if (tpprev)
-    tpprev->next = tp->next;
-  else
-    tp->inf->thread_list = tp->next;
+  if (remove)
+    {
+      size_t nr_deleted = thr->inf->thread_map.erase(thr->ptid);
+      gdb_assert (nr_deleted == 1);
+    }
 
-  delete tp;
+  delete thr;
 }
 
 /* Delete thread THREAD and notify of thread exit.  If this is the
@@ -490,13 +476,25 @@ delete_thread_1 (thread_info *thr, bool silent)
 void
 delete_thread (thread_info *thread)
 {
-  delete_thread_1 (thread, false /* not silent */);
+  delete_thread_1 (thread, false /* not silent */, true /* remove */);
+}
+
+void
+delete_thread_noremove (thread_info *thread)
+{
+  delete_thread_1 (thread, false /* silent */, false /* don't remove */);
 }
 
 void
 delete_thread_silent (thread_info *thread)
 {
-  delete_thread_1 (thread, true /* silent */);
+  delete_thread_1 (thread, true /* silent */, true /* remove */);
+}
+
+void
+delete_thread_silent_noremove (thread_info *thread)
+{
+  delete_thread_1 (thread, true /* silent */, false /* don't remove */);
 }
 
 struct thread_info *
@@ -625,7 +623,17 @@ in_thread_list (process_stratum_target *targ, ptid_t ptid)
 thread_info *
 first_thread_of_inferior (inferior *inf)
 {
-  return inf->thread_list;
+  gdb_assert (!inf->thread_map.empty ());
+
+  auto compare_by_per_inf_num = [] (const ptid_thread_map::value_type &a,
+                                   const ptid_thread_map::value_type &b)
+    {
+      return a.second->per_inf_num < b.second->per_inf_num;
+    };
+  auto it = std::min_element (inf->thread_map.begin (), inf->thread_map.end (),
+                             compare_by_per_inf_num);
+
+  return it->second;
 }
 
 thread_info *
@@ -806,7 +814,13 @@ thread_change_ptid (process_stratum_target *targ,
   inf->pid = new_ptid.pid ();
 
   tp = find_thread_ptid (inf, old_ptid);
+  gdb_assert (tp != nullptr);
+
+  int num_erased = inf->thread_map.erase (old_ptid);
+  gdb_assert (num_erased == 1);
+
   tp->ptid = new_ptid;
+  inf->thread_map[new_ptid] = tp;
 
   gdb::observers::thread_ptid_changed.notify (old_ptid, new_ptid);
 }
@@ -1127,88 +1141,102 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
       }
 
     for (inferior *inf : all_inferiors ())
-      for (thread_info *tp : inf->threads ())
-       {
-         int core;
+      {
+       /* Print the threads in per-inferior number order.  */
+       std::vector<thread_info *> threads_to_print;
 
-         any_thread = true;
-         if (tp == current_thread && tp->state == THREAD_EXITED)
-           current_exited = true;
+       for (thread_info *tp : inf->threads ())
+         threads_to_print.push_back (tp);
 
-         if (!should_print_thread (requested_threads, default_inf_num,
-                                   global_ids, pid, tp))
-           continue;
+       std::sort (threads_to_print.begin (), threads_to_print.end (),
+                  [] (thread_info *a, thread_info *b)
+         {
+           return a->per_inf_num < b->per_inf_num;
+         });
 
-         ui_out_emit_tuple tuple_emitter (uiout, NULL);
+       for (thread_info *tp : threads_to_print)
+         {
+           int core;
 
-         if (!uiout->is_mi_like_p ())
-           {
-             if (tp == current_thread)
-               uiout->field_string ("current", "*");
-             else
-               uiout->field_skip ("current");
+           any_thread = true;
+           if (tp == current_thread && tp->state == THREAD_EXITED)
+             current_exited = true;
 
-             uiout->field_string ("id-in-tg", print_thread_id (tp));
-           }
+           if (!should_print_thread (requested_threads, default_inf_num,
+                                     global_ids, pid, tp))
+             continue;
 
-         if (show_global_ids || uiout->is_mi_like_p ())
-           uiout->field_signed ("id", tp->global_num);
+           ui_out_emit_tuple tuple_emitter (uiout, NULL);
 
-         /* Switch to the thread (and inferior / target).  */
-         switch_to_thread (tp);
+           if (!uiout->is_mi_like_p ())
+             {
+               if (tp == current_thread)
+                 uiout->field_string ("current", "*");
+               else
+                 uiout->field_skip ("current");
 
-         /* For the CLI, we stuff everything into the target-id field.
-            This is a gross hack to make the output come out looking
-            correct.  The underlying problem here is that ui-out has no
-            way to specify that a field's space allocation should be
-            shared by several fields.  For MI, we do the right thing
-            instead.  */
+               uiout->field_string ("id-in-tg", print_thread_id (tp));
+             }
 
-         if (uiout->is_mi_like_p ())
-           {
-             uiout->field_string ("target-id", target_pid_to_str (tp->ptid));
+           if (show_global_ids || uiout->is_mi_like_p ())
+             uiout->field_signed ("id", tp->global_num);
 
-             const char *extra_info = target_extra_thread_info (tp);
-             if (extra_info != nullptr)
-               uiout->field_string ("details", extra_info);
+           /* Switch to the thread (and inferior / target).  */
+           switch_to_thread (tp);
 
-             const char *name = (tp->name != nullptr
-                                 ? tp->name
-                                 : target_thread_name (tp));
-             if (name != NULL)
-               uiout->field_string ("name", name);
-           }
-         else
-           {
-             uiout->field_string ("target-id",
-                                  thread_target_id_str (tp).c_str ());
-           }
+           /* For the CLI, we stuff everything into the target-id field.
+              This is a gross hack to make the output come out looking
+              correct.  The underlying problem here is that ui-out has no
+              way to specify that a field's space allocation should be
+              shared by several fields.  For MI, we do the right thing
+              instead.  */
 
-         if (tp->state == THREAD_RUNNING)
-           uiout->text ("(running)\n");
-         else
-           {
-             /* The switch above put us at the top of the stack (leaf
-                frame).  */
-             print_stack_frame (get_selected_frame (NULL),
-                                /* For MI output, print frame level.  */
-                                uiout->is_mi_like_p (),
-                                LOCATION, 0);
-           }
+           if (uiout->is_mi_like_p ())
+             {
+               uiout->field_string ("target-id", target_pid_to_str (tp->ptid));
 
-         if (uiout->is_mi_like_p ())
-           {
-             const char *state = "stopped";
+               const char *extra_info = target_extra_thread_info (tp);
+               if (extra_info != nullptr)
+                 uiout->field_string ("details", extra_info);
 
-             if (tp->state == THREAD_RUNNING)
-               state = "running";
-             uiout->field_string ("state", state);
-           }
+               const char *name = (tp->name != nullptr
+                                   ? tp->name
+                                   : target_thread_name (tp));
+               if (name != NULL)
+                 uiout->field_string ("name", name);
+             }
+           else
+             {
+               uiout->field_string ("target-id",
+                                    thread_target_id_str (tp).c_str ());
+             }
 
-         core = target_core_of_thread (tp->ptid);
-         if (uiout->is_mi_like_p () && core != -1)
-           uiout->field_signed ("core", core);
-       }
+           if (tp->state == THREAD_RUNNING)
+             uiout->text ("(running)\n");
+           else
+             {
+               /* The switch above put us at the top of the stack (leaf
+                  frame).  */
+               print_stack_frame (get_selected_frame (NULL),
+                                  /* For MI output, print frame level.  */
+                                  uiout->is_mi_like_p (),
+                                  LOCATION, 0);
+             }
+
+           if (uiout->is_mi_like_p ())
+             {
+               const char *state = "stopped";
+
+               if (tp->state == THREAD_RUNNING)
+                 state = "running";
+               uiout->field_string ("state", state);
+             }
+
+           core = target_core_of_thread (tp->ptid);
+           if (uiout->is_mi_like_p () && core != -1)
+             uiout->field_signed ("core", core);
+         }
+      }
 
     /* This end scope restores the current thread and the frame
        selected before the "info threads" command, and it finishes the
@@ -2099,7 +2127,7 @@ update_threads_executing (void)
 
       /* If the process has no threads, then it must be we have a
         process-exit event pending.  */
-      if (inf->thread_list == NULL)
+      if (inf->thread_map.empty ())
        {
          targ->threads_executing = true;
          return;
This page took 0.027674 seconds and 4 git commands to generate.