[threads] Correct the spin waiting in 'sleep_interruptable' (#3544)
authorEmil Sandstø <emilalexer@hotmail.com>
Mon, 12 Sep 2016 10:19:00 +0000 (12:19 +0200)
committerLudovic Henry <ludovic@xamarin.com>
Mon, 12 Sep 2016 10:19:00 +0000 (12:19 +0200)
After https://github.com/mono/mono/commit/089c47f1c07bf250d76c36e04675569fc6f5b4ba#diff-e7e458b6256eaa730c145f14a666652aR1141
the code started to use nanoseconds instead of milliseconds. The problem was that this caused 'sleep_interruptable' to spin wait the last millisecond before the wait was over, https://bugzilla.xamarin.com/show_bug.cgi?id=44132.

Fix https://bugzilla.xamarin.com/show_bug.cgi?id=44132

mono/utils/mono-threads.c

index 3ec45d07c89be9165e9487df3ca5f3b97f7c6648..d0eedf683e9d4b4931c83ddec4d8a71d5eefd79c 100644 (file)
@@ -1284,7 +1284,7 @@ sleep_interruptable (guint32 ms, gboolean *alerted)
        *alerted = FALSE;
 
        if (ms != INFINITE)
-               end = mono_100ns_ticks () + (ms * 1000 * 10);
+               end = mono_msec_ticks() + ms;
 
        mono_lazy_initialize (&sleep_init, sleep_initialize);
 
@@ -1292,8 +1292,8 @@ sleep_interruptable (guint32 ms, gboolean *alerted)
 
        for (;;) {
                if (ms != INFINITE) {
-                       now = mono_100ns_ticks ();
-                       if (now > end)
+                       now = mono_msec_ticks();
+                       if (now >= end)
                                break;
                }
 
@@ -1304,7 +1304,7 @@ sleep_interruptable (guint32 ms, gboolean *alerted)
                }
 
                if (ms != INFINITE)
-                       mono_coop_cond_timedwait (&sleep_cond, &sleep_mutex, (end - now) / 10 / 1000);
+                       mono_coop_cond_timedwait (&sleep_cond, &sleep_mutex, end - now);
                else
                        mono_coop_cond_wait (&sleep_cond, &sleep_mutex);