return (current_thread != NULL);
}
-/* Structure used to look up a thread to use as current when accessing
- memory. */
-
-struct thread_search
-{
- /* The PTID of the current general thread. This is an input
- parameter. */
- ptid_t current_gen_ptid;
-
- /* The first thread found. */
- struct thread_info *first;
-
- /* The first stopped thread found. */
- struct thread_info *stopped;
-
- /* The current general thread, if found. */
- struct thread_info *current;
-};
-
-/* Callback for find_inferior. Search for a thread to use as current
- when accessing memory. */
-
-static int
-thread_search_callback (thread_info *thread, void *args)
-{
- struct thread_search *s = (struct thread_search *) args;
-
- if (thread->id.pid () == ptid_get_pid (s->current_gen_ptid)
- && mythread_alive (ptid_of (thread)))
- {
- if (s->stopped == NULL
- && the_target->thread_stopped != NULL
- && thread_stopped (thread))
- s->stopped = thread;
-
- if (s->first == NULL)
- s->first = thread;
-
- if (s->current == NULL && s->current_gen_ptid == thread->id)
- s->current = thread;
- }
-
- return 0;
-}
-
/* The thread that was current before prepare_to_access_memory was
called. done_accessing_memory uses this to restore the previous
selected thread. */
int
prepare_to_access_memory (void)
{
- struct thread_search search;
- struct thread_info *thread;
+ /* The first thread found. */
+ struct thread_info *first = NULL;
+ /* The first stopped thread found. */
+ struct thread_info *stopped = NULL;
+ /* The current general thread, if found. */
+ struct thread_info *current = NULL;
- memset (&search, 0, sizeof (search));
- search.current_gen_ptid = general_thread;
+ /* Save the general thread value, since prepare_to_access_memory could change
+ it. */
prev_general_thread = general_thread;
if (the_target->prepare_to_access_memory != NULL)
return res;
}
- find_inferior (&all_threads, thread_search_callback, &search);
+ for_each_thread (prev_general_thread.pid (), [&] (thread_info *thread)
+ {
+ if (mythread_alive (thread->id))
+ {
+ if (stopped == NULL && the_target->thread_stopped != NULL
+ && thread_stopped (thread))
+ stopped = thread;
+
+ if (first == NULL)
+ first = thread;
+
+ if (current == NULL && prev_general_thread == thread->id)
+ current = thread;
+ }
+ });
+
+ /* The thread we end up choosing. */
+ struct thread_info *thread;
/* Prefer a stopped thread. If none is found, try the current
thread. Otherwise, take the first thread in the process. If
none is found, undo the effects of
target->prepare_to_access_memory() and return error. */
- if (search.stopped != NULL)
- thread = search.stopped;
- else if (search.current != NULL)
- thread = search.current;
- else if (search.first != NULL)
- thread = search.first;
+ if (stopped != NULL)
+ thread = stopped;
+ else if (current != NULL)
+ thread = current;
+ else if (first != NULL)
+ thread = first;
else
{
done_accessing_memory ();