Use find_thread_in_random in select_event_lwp
authorTom Tromey <tromey@adacore.com>
Thu, 4 Apr 2019 22:57:11 +0000 (16:57 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 9 Apr 2019 18:03:25 +0000 (12:03 -0600)
I noticed that find_thread_in_random duplicates the code in
find_thread_in_random, so this patch changes the latter to use the
former.

There are two other spots in gdb that do this, but to unify all of
them would require switching some code from using the "iterate over"
idiom to using iterators.

Another possible improvement is that find_thread_in_random could be
made single-pass using reservoir sampling.

Tested by the buildbot.

gdb/gdbserver/ChangeLog
2019-04-09  Tom Tromey  <tromey@adacore.com>

* linux-low.c (select_event_lwp): Use find_thread_in_random.

gdb/gdbserver/ChangeLog
gdb/gdbserver/linux-low.c

index e82e90156473b1cffcc3625b0326eb27a85227d8..1618e2c4b0f0c7c32d8a4e8d1cdc817d89fab336 100644 (file)
@@ -1,3 +1,7 @@
+2019-04-09  Tom Tromey  <tromey@adacore.com>
+
+       * linux-low.c (select_event_lwp): Use find_thread_in_random.
+
 2019-04-08  Tom Tromey  <tom@tromey.com>
 
        * linux-low.c (linux_detach_one_lwp): Replace throw_exception with
index 94af240a5c2ce92e7b35e1a9a3b84d9f02ca9806..168f4b2abc2db0f33ac65eb574f141f9052b18e6 100644 (file)
@@ -2828,7 +2828,6 @@ linux_wait_for_event (ptid_t ptid, int *wstatp, int options)
 static void
 select_event_lwp (struct lwp_info **orig_lp)
 {
-  int random_selector;
   struct thread_info *event_thread = NULL;
 
   /* In all-stop, give preference to the LWP that is being
@@ -2862,39 +2861,13 @@ select_event_lwp (struct lwp_info **orig_lp)
       /* No single-stepping LWP.  Select one at random, out of those
          which have had events.  */
 
-      /* First see how many events we have.  */
-      int num_events = 0;
-      for_each_thread ([&] (thread_info *thread)
-       {
-         lwp_info *lp = get_thread_lwp (thread);
-
-         /* Count only resumed LWPs that have an event pending. */
-         if (thread->last_status.kind == TARGET_WAITKIND_IGNORE
-             && lp->status_pending_p)
-           num_events++;
-       });
-      gdb_assert (num_events > 0);
-
-      /* Now randomly pick a LWP out of those that have had
-        events.  */
-      random_selector = (int)
-       ((num_events * (double) rand ()) / (RAND_MAX + 1.0));
-
-      if (debug_threads && num_events > 1)
-       debug_printf ("SEL: Found %d SIGTRAP events, selecting #%d\n",
-                     num_events, random_selector);
-
-      event_thread = find_thread ([&] (thread_info *thread)
+      event_thread = find_thread_in_random ([&] (thread_info *thread)
        {
          lwp_info *lp = get_thread_lwp (thread);
 
-         /* Select only resumed LWPs that have an event pending.  */
-         if (thread->last_status.kind == TARGET_WAITKIND_IGNORE
-             && lp->status_pending_p)
-           if (random_selector-- == 0)
-             return true;
-
-         return false;
+         /* Only resumed LWPs that have an event pending. */
+         return (thread->last_status.kind == TARGET_WAITKIND_IGNORE
+                 && lp->status_pending_p);
        });
     }
 
This page took 0.048337 seconds and 4 git commands to generate.