[PATCH v3] tools/nolibc: Implement msleep()

From: Mark Brown
Date: Wed May 12 2021 - 08:34:09 EST


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

Signed-off-by: Mark Brown <broonie@xxxxxxxxxx>
---
v3:
- Return the number of milliseconds remaining if the delay does not
complete.
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 | 13 +++++++++++++
1 file changed, 13 insertions(+)

diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index 8b7a9830dd22..ba6b6f1ad846 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -2243,6 +2243,19 @@ 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 * 1000) +
+ (my_timeval.tv_usec / 1000) +
+ !!(my_timeval.tv_usec % 1000);
+ else
+ return 0;
+}
+
static __attribute__((unused))
int stat(const char *path, struct stat *buf)
{
--
2.20.1