Merge branch 'no-usleep-on-0' into 'main'

gtimer: Avoid doing anything on g_usleep (0)

See merge request GNOME/glib!3381
This commit is contained in:
Philip Withnall 2023-04-17 11:44:36 +00:00
commit 7c1ef5c9ec
2 changed files with 27 additions and 0 deletions

View File

@ -269,6 +269,9 @@ g_timer_is_active (GTimer *timer)
void void
g_usleep (gulong microseconds) g_usleep (gulong microseconds)
{ {
if G_UNLIKELY (microseconds == 0)
return;
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
/* Round up to the next millisecond */ /* Round up to the next millisecond */
Sleep (microseconds ? (1 + (microseconds - 1) / 1000) : 0); Sleep (microseconds ? (1 + (microseconds - 1) / 1000) : 0);

View File

@ -346,6 +346,29 @@ test_timeval_to_iso8601_overflow (void)
g_assert_null (out); g_assert_null (out);
} }
static void
test_usleep_with_zero_wait (void)
{
GTimer *timer;
gdouble elapsed0, elapsed1;
timer = g_timer_new ();
g_timer_start (timer);
g_usleep (0);
elapsed0 = g_timer_elapsed (timer, NULL);
g_timer_stop (timer);
g_timer_start (timer);
g_usleep (1);
elapsed1 = g_timer_elapsed (timer, NULL);
g_timer_stop (timer);
g_assert_cmpfloat (elapsed0, <=, elapsed1);
g_clear_pointer (&timer, g_timer_destroy);
}
int int
main (int argc, char *argv[]) main (int argc, char *argv[])
{ {
@ -360,6 +383,7 @@ main (int argc, char *argv[])
g_test_add_func ("/timeval/from-iso8601", test_timeval_from_iso8601); g_test_add_func ("/timeval/from-iso8601", test_timeval_from_iso8601);
g_test_add_func ("/timeval/to-iso8601", test_timeval_to_iso8601); g_test_add_func ("/timeval/to-iso8601", test_timeval_to_iso8601);
g_test_add_func ("/timeval/to-iso8601/overflow", test_timeval_to_iso8601_overflow); g_test_add_func ("/timeval/to-iso8601/overflow", test_timeval_to_iso8601_overflow);
g_test_add_func ("/usleep/with-zero-wait", test_usleep_with_zero_wait);
return g_test_run (); return g_test_run ();
} }