All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] tools/nolibc: Implement msleep()
@ 2021-05-12 11:47 Mark Brown
  2021-05-12 11:59 ` Willy Tarreau
  0 siblings, 1 reply; 5+ messages in thread
From: Mark Brown @ 2021-05-12 11:47 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>
---

v2:
 - Support delays of more than a second.
 - Return the number of seconds remaining if the delay does not
   complete.

 tools/include/nolibc/nolibc.h | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index 8b7a9830dd22..01400d36ce99 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -2243,6 +2243,17 @@ unsigned int sleep(unsigned int seconds)
 		return 0;
 }
 
+static __attribute__((unused))
+int msleep(unsigned int msecs)
+{
+	struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
+
+	if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
+		return my_timeval.tv_sec + !!my_timeval.tv_usec;
+	else
+		return 0;
+}
+
 static __attribute__((unused))
 int stat(const char *path, struct stat *buf)
 {
-- 
2.20.1


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

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

On Wed, May 12, 2021 at 12:47:28PM +0100, Mark Brown wrote:
> Allow users to implement shorter delays than a full second by implementing
> msleep().
> 
> Signed-off-by: Mark Brown <broonie@kernel.org>
> ---
> 
> v2:
>  - Support delays of more than a second.
>  - Return the number of seconds remaining if the delay does not
>    complete.

But why returning the number of seconds instead of milliseconds ?
The common use case is this:

     delay = delay_before_next_event();
     while ((ret = msleep(delay)) > 0)
            delay -= ret;

Willy

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

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

[-- Attachment #1: Type: text/plain, Size: 539 bytes --]

On Wed, May 12, 2021 at 01:59:49PM +0200, Willy Tarreau wrote:
> On Wed, May 12, 2021 at 12:47:28PM +0100, Mark Brown wrote:

> >  - Return the number of seconds remaining if the delay does not
> >    complete.

> But why returning the number of seconds instead of milliseconds ?
> The common use case is this:

>      delay = delay_before_next_event();
>      while ((ret = msleep(delay)) > 0)
>             delay -= ret;

It seemed to be what you were asking for and I didn't have strong enough
opinions for it to be worth pushing back.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

* Re: [PATCH v2] tools/nolibc: Implement msleep()
  2021-05-12 12:18   ` Mark Brown
@ 2021-05-12 12:50     ` Willy Tarreau
  2021-05-12 12:58       ` Mark Brown
  0 siblings, 1 reply; 5+ messages in thread
From: Willy Tarreau @ 2021-05-12 12:50 UTC (permalink / raw)
  To: Mark Brown; +Cc: linux-kernel

On Wed, May 12, 2021 at 01:18:58PM +0100, Mark Brown wrote:
> On Wed, May 12, 2021 at 01:59:49PM +0200, Willy Tarreau wrote:
> > On Wed, May 12, 2021 at 12:47:28PM +0100, Mark Brown wrote:
> 
> > >  - Return the number of seconds remaining if the delay does not
> > >    complete.
> 
> > But why returning the number of seconds instead of milliseconds ?
> > The common use case is this:
> 
> >      delay = delay_before_next_event();
> >      while ((ret = msleep(delay)) > 0)
> >             delay -= ret;
> 
> It seemed to be what you were asking for and I didn't have strong enough
> opinions for it to be worth pushing back.

Sorry if I wasn't clear then, but I proposed this example which should
return the number of ms left:

        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;

In any case all of this is not critical but I'm fairly convinced that
your addition is useful, especially if done like this, which is why I
was asking.

Willy

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

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

[-- Attachment #1: Type: text/plain, Size: 321 bytes --]

On Wed, May 12, 2021 at 02:50:00PM +0200, Willy Tarreau wrote:

> Sorry if I wasn't clear then, but I proposed this example which should
> return the number of ms left:

Oh, I think what happened is that I took your "copying what we're doing
in sleep()" a bit too literally and didn't properly read the code you
added :/

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

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

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.