tools/nolibc: Implement msleep()
PATCH v2 1/1 tools/nolibc: Implement msleep()
Allow users to implement shorter delays than a full second by implementing msleep().
---
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(+)
tools/include/nolibc/nolibc.h +11 -0
@@ -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) {
Signature · 1 line
2.20.1
Willy Tarreau
maintainerQuoted 10 lines · Mark Brown
Allow users to implement shorter delays than a full second by implementing
msleep().Signed-off-by Mark Brown <[email protected]>---
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
Quoted 11 lines · Willy Tarreau
Quoted 2 lines · Mark Brown
- 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.
Willy Tarreau
maintainerQuoted 9 lines · Mark Brown
Quoted 7 lines · Willy Tarreau
Quoted 2 lines · Mark Brown
- 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:
Quoted 6 lines
Quoted 3 lines
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
Quoted 2 lines · Willy Tarreau
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 :/