linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools/nolibc: Implement msleep()
@ 2021-05-11 11:01 Mark Brown
  2021-05-12  5:33 ` Willy Tarreau
  0 siblings, 1 reply; 2+ messages in thread
From: Mark Brown @ 2021-05-11 11:01 UTC (permalink / raw)
  To: Willy Tarreau; +Cc: linux-kernel, Mark Brown

Allow users to implement shorter delays than a full second by implementing
msleep().

Signed-off-by: Mark Brown <broonie@kernel.org>
---
 tools/include/nolibc/nolibc.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index 8b7a9830dd22..d045d8bb5f0a 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -2243,6 +2243,14 @@ unsigned int sleep(unsigned int seconds)
 		return 0;
 }
 
+static __attribute__((unused))
+void msleep(unsigned int msecs)
+{
+	struct timeval my_timeval = { 0, msecs * 1000 };
+
+	sys_select(0, 0, 0, 0, &my_timeval);
+}
+
 static __attribute__((unused))
 int stat(const char *path, struct stat *buf)
 {
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] tools/nolibc: Implement msleep()
  2021-05-11 11:01 [PATCH] tools/nolibc: Implement msleep() Mark Brown
@ 2021-05-12  5:33 ` Willy Tarreau
  0 siblings, 0 replies; 2+ messages in thread
From: Willy Tarreau @ 2021-05-12  5:33 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-kernel

Hi Mark,

On Tue, May 11, 2021 at 12:01:59PM +0100, Mark Brown wrote:
> +static __attribute__((unused))
> +void msleep(unsigned int msecs)
> +{
> +	struct timeval my_timeval = { 0, msecs * 1000 };
> +
> +	sys_select(0, 0, 0, 0, &my_timeval);
> +}
> +

Just a quick question, is there any reason for not keeping most of the
precision like this and allow applications to use it beyond 4294 seconds
like this ?

	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };

Another thing that comes to my mind is that sleep() returns the remaining
number of seconds if the syscall was interrupted, and I think it could be
very useful in small tests programs to do the same at the subsecond level
in simple scheduling loops for example. Copying what we're doing in sleep()
we could have this:

        if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
                return my_timeval.tv_sec * 1000 + (my_timeval.tv_usec + 999) / 1000;
        else
                return 0;

And since that's an inline function it will be optimized away if the result
is not used anyway, resulting in the same code as the void version in this
case.

What do you think ?

Thanks!
Willy

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-12  5:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-11 11:01 [PATCH] tools/nolibc: Implement msleep() Mark Brown
2021-05-12  5:33 ` Willy Tarreau

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).