+ tp = find_thread_pid (ptid);
+ if (tp)
+ /* Found an old thread with the same id. It has to be dead,
+ otherwise we wouldn't be adding a new thread with the same id.
+ The OS is reusing this id --- delete it, and recreate a new
+ one. */
+ {
+ /* In addition to deleting the thread, if this is the current
+ thread, then we need to take care that delete_thread doesn't
+ really delete the thread if it is inferior_ptid. Create a
+ new template thread in the list with an invalid ptid, switch
+ to it, delete the original thread, reset the new thread's
+ ptid, and switch to it. */
+
+ if (ptid_equal (inferior_ptid, ptid))
+ {
+ tp = xmalloc (sizeof (*tp));
+ memset (tp, 0, sizeof (*tp));
+ tp->ptid = minus_one_ptid;
+ tp->num = ++highest_thread_num;
+ tp->next = thread_list;
+ thread_list = tp;
+
+ /* Make switch_to_thread not read from the thread. */
+ tp->state_ = THREAD_EXITED;
+ switch_to_thread (minus_one_ptid);
+
+ /* Now we can delete it. */
+ delete_thread (ptid);
+
+ /* Now reset its ptid, and reswitch inferior_ptid to it. */
+ tp->ptid = ptid;
+ tp->state_ = THREAD_STOPPED;
+ switch_to_thread (ptid);
+
+ observer_notify_new_thread (tp);
+
+ /* All done. */
+ return tp;
+ }
+ else
+ /* Just go ahead and delete it. */
+ delete_thread (ptid);
+ }
+