gdb: make inferior_list use intrusive_list
[binutils-gdb.git] / gdb / thread.c
index 87b6cbf74fd76bfc73d1dde9b8fdcc304441566f..506e93cf4016593d8973c43877691b437104794d 100644 (file)
@@ -177,9 +177,9 @@ clear_thread_inferior_resources (struct thread_info *tp)
   clear_inline_frame_state (tp);
 }
 
-/* Set the TP's state as exited.  */
+/* See gdbthread.h.  */
 
-static void
+void
 set_thread_exited (thread_info *tp, bool silent)
 {
   /* Dead threads don't need to step-over.  Remove from chain.  */
@@ -203,17 +203,8 @@ init_thread_list (void)
 {
   highest_thread_num = 0;
 
-  for (thread_info *tp : all_threads_safe ())
-    {
-      inferior *inf = tp->inf;
-
-      if (tp->deletable ())
-       delete tp;
-      else
-       set_thread_exited (tp, 1);
-
-      inf->thread_list = NULL;
-    }
+  for (inferior *inf : all_inferiors ())
+    inf->clear_thread_list (true);
 }
 
 /* Allocate a new thread of inferior INF with target id PTID and add
@@ -224,21 +215,7 @@ 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;
-
-      for (last = inf->thread_list; last->next != NULL; last = last->next)
-       gdb_assert (ptid != last->ptid
-                   || last->state == THREAD_EXITED);
-
-      gdb_assert (ptid != last->ptid
-                 || last->state == THREAD_EXITED);
-
-      last->next = tp;
-    }
+  inf->thread_list.push_back (*tp);
 
   return tp;
 }
@@ -462,29 +439,18 @@ delete_thread_1 (thread_info *thr, bool silent)
 {
   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;
-
-  if (!tp)
-    return;
-
-  set_thread_exited (tp, silent);
+  set_thread_exited (thr, 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;
+  auto it = thr->inf->thread_list.iterator_to (*thr);
+  thr->inf->thread_list.erase (it);
 
-  delete tp;
+  delete thr;
 }
 
 /* See gdbthread.h.  */
@@ -629,7 +595,10 @@ in_thread_list (process_stratum_target *targ, ptid_t ptid)
 thread_info *
 first_thread_of_inferior (inferior *inf)
 {
-  return inf->thread_list;
+  if (inf->thread_list.empty ())
+    return nullptr;
+
+  return &inf->thread_list.front ();
 }
 
 thread_info *
@@ -1184,8 +1153,7 @@ print_thread_info_1 (struct ui_out *uiout, const char *requested_threads,
            }
          else
            {
-             uiout->field_string ("target-id",
-                                  thread_target_id_str (tp).c_str ());
+             uiout->field_string ("target-id", thread_target_id_str (tp));
            }
 
          if (tp->state == THREAD_RUNNING)
@@ -1427,7 +1395,11 @@ show_thread_that_caused_stop (void)
 int
 show_inferior_qualified_tids (void)
 {
-  return (inferior_list->next != NULL || inferior_list->num != 1);
+  auto inf = inferior_list.begin ();
+  if (inf->num != 1)
+    return true;
+  ++inf;
+  return inf != inferior_list.end ();
 }
 
 /* See gdbthread.h.  */
@@ -2019,7 +1991,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_list.empty ())
        {
          targ->threads_executing = true;
          return;
@@ -2134,10 +2106,13 @@ Options:\n\
   c = add_info ("threads", info_threads_command, info_threads_help.c_str ());
   set_cmd_completer_handle_brkchars (c, info_threads_command_completer);
 
-  add_prefix_cmd ("thread", class_run, thread_command, _("\
+  cmd_list_element *thread_cmd
+    = add_prefix_cmd ("thread", class_run, thread_command, _("\
 Use this command to switch between threads.\n\
 The new thread ID must be currently known."),
-                 &thread_cmd_list, 1, &cmdlist);
+                     &thread_cmd_list, 1, &cmdlist);
+
+  add_com_alias ("t", thread_cmd, class_run, 1);
 
 #define THREAD_APPLY_OPTION_HELP "\
 Prints per-inferior thread number and target system's thread id\n\
@@ -2204,8 +2179,6 @@ Usage: thread find REGEXP\n\
 Will display thread ids whose name, target ID, or extra info matches REGEXP."),
           &thread_cmd_list);
 
-  add_com_alias ("t", "thread", class_run, 1);
-
   add_setshow_boolean_cmd ("thread-events", no_class,
                           &print_thread_events, _("\
 Set printing of thread events (such as thread start and exit)."), _("\