+2016-05-24 Pedro Alves <palves@redhat.com>
+
+ PR gdb/19828
+ * linux-nat.c (lwp_lwpid_htab): New htab.
+ (lwp_info_hash, lwp_lwpid_htab_eq, lwp_lwpid_htab_create)
+ (lwp_lwpid_htab_add_lwp): New functions.
+ (lwp_list): Tweak comment.
+ (lwp_list_add, lwp_list_remove, lwp_lwpid_htab_remove_pid): New
+ functions.
+ (purge_lwp_list): Rewrite, using htab_traverse_noresize.
+ (add_initial_lwp): Add lwp to htab too. Use lwp_list_add.
+ (delete_lwp): Use lwp_list_remove. Remove htab too.
+ (find_lwp_pid): Search in htab.
+ (_initialize_linux_nat): Call lwp_lwpid_htab_create.
+ * linux-nat.h (struct lwp_info) <prev>: New field.
+
+2016-05-24 Pedro Alves <palves@redhat.com>
+
+ PR gdb/19828
+ * linux-nat.c (lwp_lwpid_htab): New htab.
+ (lwp_info_hash, lwp_lwpid_htab_eq, lwp_lwpid_htab_create)
+ (lwp_lwpid_htab_add_lwp): New functions.
+ (lwp_list): Tweak comment.
+ (lwp_list_add, lwp_list_remove, lwp_lwpid_htab_remove_pid): New
+ functions.
+ (purge_lwp_list): Rewrite, using htab_traverse_noresize.
+ (add_initial_lwp): Add lwp to htab too. Use lwp_list_add.
+ (delete_lwp): Use lwp_list_remove. Remove htab too.
+ (find_lwp_pid): Search in htab.
+ (_initialize_linux_nat): Call lwp_lwpid_htab_create.
+ * linux-nat.h (struct lwp_info) <prev>: New field.
+
2016-05-24 Pedro Alves <palves@redhat.com>
PR gdb/19828
return 0;
}
-/* List of known LWPs. */
+/* List of known LWPs, keyed by LWP PID. This speeds up the common
+ case of mapping a PID returned from the kernel to our corresponding
+ lwp_info data structure. */
+static htab_t lwp_lwpid_htab;
+
+/* Calculate a hash from a lwp_info's LWP PID. */
+
+static hashval_t
+lwp_info_hash (const void *ap)
+{
+ const struct lwp_info *lp = (struct lwp_info *) ap;
+ pid_t pid = ptid_get_lwp (lp->ptid);
+
+ return iterative_hash_object (pid, 0);
+}
+
+/* Equality function for the lwp_info hash table. Compares the LWP's
+ PID. */
+
+static int
+lwp_lwpid_htab_eq (const void *a, const void *b)
+{
+ const struct lwp_info *entry = (const struct lwp_info *) a;
+ const struct lwp_info *element = (const struct lwp_info *) b;
+
+ return ptid_get_lwp (entry->ptid) == ptid_get_lwp (element->ptid);
+}
+
+/* Create the lwp_lwpid_htab hash table. */
+
+static void
+lwp_lwpid_htab_create (void)
+{
+ lwp_lwpid_htab = htab_create (100, lwp_info_hash, lwp_lwpid_htab_eq, NULL);
+}
+
+/* Add LP to the hash table. */
+
+static void
+lwp_lwpid_htab_add_lwp (struct lwp_info *lp)
+{
+ void **slot;
+
+ slot = htab_find_slot (lwp_lwpid_htab, lp, INSERT);
+ gdb_assert (slot != NULL && *slot == NULL);
+ *slot = lp;
+}
+
+/* Head of doubly-linked list of known LWPs. Sorted by reverse
+ creation order. This order is assumed in some cases. E.g.,
+ reaping status after killing alls lwps of a process: the leader LWP
+ must be reaped last. */
struct lwp_info *lwp_list;
+
+/* Add LP to sorted-by-reverse-creation-order doubly-linked list. */
+
+static void
+lwp_list_add (struct lwp_info *lp)
+{
+ lp->next = lwp_list;
+ if (lwp_list != NULL)
+ lwp_list->prev = lp;
+ lwp_list = lp;
+}
+
+/* Remove LP from sorted-by-reverse-creation-order doubly-linked
+ list. */
+
+static void
+lwp_list_remove (struct lwp_info *lp)
+{
+ /* Remove from sorted-by-creation-order list. */
+ if (lp->next != NULL)
+ lp->next->prev = lp->prev;
+ if (lp->prev != NULL)
+ lp->prev->next = lp->next;
+ if (lp == lwp_list)
+ lwp_list = lp->next;
+}
+
\f
/* Original signal mask. */
xfree (lp);
}
-/* Remove all LWPs belong to PID from the lwp list. */
+/* Traversal function for purge_lwp_list. */
-static void
-purge_lwp_list (int pid)
+static int
+lwp_lwpid_htab_remove_pid (void **slot, void *info)
{
- struct lwp_info *lp, *lpprev, *lpnext;
-
- lpprev = NULL;
+ struct lwp_info *lp = (struct lwp_info *) *slot;
+ int pid = *(int *) info;
- for (lp = lwp_list; lp; lp = lpnext)
+ if (ptid_get_pid (lp->ptid) == pid)
{
- lpnext = lp->next;
+ htab_clear_slot (lwp_lwpid_htab, slot);
+ lwp_list_remove (lp);
+ lwp_free (lp);
+ }
- if (ptid_get_pid (lp->ptid) == pid)
- {
- if (lp == lwp_list)
- lwp_list = lp->next;
- else
- lpprev->next = lp->next;
+ return 1;
+}
- lwp_free (lp);
- }
- else
- lpprev = lp;
- }
+/* Remove all LWPs belong to PID from the lwp list. */
+
+static void
+purge_lwp_list (int pid)
+{
+ htab_traverse_noresize (lwp_lwpid_htab, lwp_lwpid_htab_remove_pid, &pid);
}
/* Add the LWP specified by PTID to the list. PTID is the first LWP
lp->ptid = ptid;
lp->core = -1;
- lp->next = lwp_list;
- lwp_list = lp;
+ /* Add to sorted-by-reverse-creation-order list. */
+ lwp_list_add (lp);
+
+ /* Add to keyed-by-pid htab. */
+ lwp_lwpid_htab_add_lwp (lp);
return lp;
}
static void
delete_lwp (ptid_t ptid)
{
- struct lwp_info *lp, *lpprev;
+ struct lwp_info *lp;
+ void **slot;
+ struct lwp_info dummy;
- lpprev = NULL;
+ dummy.ptid = ptid;
+ slot = htab_find_slot (lwp_lwpid_htab, &dummy, NO_INSERT);
+ if (slot == NULL)
+ return;
- for (lp = lwp_list; lp; lpprev = lp, lp = lp->next)
- if (ptid_equal (lp->ptid, ptid))
- break;
+ lp = *(struct lwp_info **) slot;
+ gdb_assert (lp != NULL);
- if (!lp)
- return;
+ htab_clear_slot (lwp_lwpid_htab, slot);
- if (lpprev)
- lpprev->next = lp->next;
- else
- lwp_list = lp->next;
+ /* Remove from sorted-by-creation-order list. */
+ lwp_list_remove (lp);
+ /* Release. */
lwp_free (lp);
}
{
struct lwp_info *lp;
int lwp;
+ struct lwp_info dummy;
if (ptid_lwp_p (ptid))
lwp = ptid_get_lwp (ptid);
else
lwp = ptid_get_pid (ptid);
- for (lp = lwp_list; lp; lp = lp->next)
- if (lwp == ptid_get_lwp (lp->ptid))
- return lp;
-
- return NULL;
+ dummy.ptid = ptid_build (0, lwp, 0);
+ lp = (struct lwp_info *) htab_find (lwp_lwpid_htab, &dummy);
+ return lp;
}
/* See nat/linux-nat.h. */
sigdelset (&suspend_mask, SIGCHLD);
sigemptyset (&blocked_mask);
+
+ lwp_lwpid_htab_create ();
}
\f