bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] xnanosleep: Use pause for infinite duration.


From: Vladimir Panteleev
Subject: [PATCH] xnanosleep: Use pause for infinite duration.
Date: Mon, 10 Feb 2020 12:26:13 +0000

* lib/xnanosleep.c (xnanosleep): When the duration is the positive
infinity, use pause instead of nanosleep.
---
 lib/xnanosleep.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/lib/xnanosleep.c b/lib/xnanosleep.c
index 5774f75f3..58a2f5254 100644
--- a/lib/xnanosleep.c
+++ b/lib/xnanosleep.c
@@ -25,7 +25,9 @@
 #include <timespec.h>
 
 #include <errno.h>
+#include <math.h>
 #include <time.h>
+#include <unistd.h>
 
 /* Sleep until the time (call it WAKE_UP_TIME) specified as
    SECONDS seconds after the time this function is called.
@@ -37,21 +39,33 @@
 int
 xnanosleep (double seconds)
 {
-  struct timespec ts_sleep = dtotimespec (seconds);
-
-  for (;;)
+  if (!isfinite(seconds) && seconds > 0)
+    {
+      for (;;)
+        {
+          pause();
+          if (errno != EINTR)
+            return -1;
+        }
+    }
+  else
     {
-      /* Linux-2.6.8.1's nanosleep returns -1, but doesn't set errno
-         when resumed after being suspended.  Earlier versions would
-         set errno to EINTR.  nanosleep from linux-2.6.10, as well as
-         implementations by (all?) other vendors, doesn't return -1
-         in that case;  either it continues sleeping (if time remains)
-         or it returns zero (if the wake-up time has passed).  */
-      errno = 0;
-      if (nanosleep (&ts_sleep, NULL) == 0)
-        break;
-      if (errno != EINTR && errno != 0)
-        return -1;
+      struct timespec ts_sleep = dtotimespec (seconds);
+
+      for (;;)
+        {
+          /* Linux-2.6.8.1's nanosleep returns -1, but doesn't set errno
+             when resumed after being suspended.  Earlier versions would
+             set errno to EINTR.  nanosleep from linux-2.6.10, as well as
+             implementations by (all?) other vendors, doesn't return -1
+             in that case;  either it continues sleeping (if time remains)
+             or it returns zero (if the wake-up time has passed).  */
+          errno = 0;
+          if (nanosleep (&ts_sleep, NULL) == 0)
+            break;
+          if (errno != EINTR && errno != 0)
+            return -1;
+        }
     }
 
   return 0;
-- 
2.25.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]