bug-gnulib
[Top][All Lists]
Advanced

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

[PATCH 0/1] xnanosleep: Use pause for infinite duration.


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

Users have discovered that the "sleep" command accepts "infinity" as
its duration argument. However, specifying the "infinity" duration
does not actually cause GNU sleep to sleep for an infinite
duration. This is because the duration was being unconditionally
converted to a timespec value, and infinities are not representable as
timespecs.

Address this by using the "pause" system call (instead of "nanosleep")
when the duration is a positive infinity. This allows us to avoid
burdening the operating system with a timer which likely will never be
reached.

---

Commentary: Yes, this patch isn't too serious. There is an interesting
discussion here [1] which compares a few ways to get a process to
simply block indefinitely. I thought it was a shame that the most
elegant-looking solution, "sleep infinity", turned out to be inferior
to the other, more complicated ones.

Possibly xnanosleep is not the best way to perform this change. The
downside is that it changes the behavior of xnanosleep - it is no
longer just a nanosleep wrapper. The upside is that this the place
where the lossy conversion occurs, so the alternative would mean that
all nanosleep callers would have to check the argument.

I'm not sure if isfinite is the best way to check for infinity, as far
as the tradeoff between portability and dependencies goes. An
alternative would be "static const double infinity = 1.0 / 0.0; if
(seconds == infinity)" but I don't know how good that is in terms of
portability. I noticed that e.g. vasnprintf.c defines a private
isfinite-like function.

By the way, shouldn't xnanosleep be using the second nanosleep
parameter to track how much time there's left to sleep in case of an
interruption?

 [1]: https://stackoverflow.com/a/41655546/21501

---

Vladimir Panteleev (1):
  xnanosleep: Use pause for infinite duration.

 lib/xnanosleep.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)

-- 
2.25.0




reply via email to

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