Skip to contentSkip to conversation

tools/nolibc: Implement msleep()

PATCH Superseded Mark Brown 2 participants 2 messages May 11, 2021 – May 12, 2021 Nolibc header file

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 tools/nolibc: Implement msleep()

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

---
 tools/include/nolibc/nolibc.h | 8 ++++++++
 1 file changed, 8 insertions(+)
tools/include/nolibc/nolibc.h +8 -0
index 8b7a9830dd22..d045d8bb5f0a 100644
@@ -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)
 {
Signature · 1 line
2.20.1
maintainer
#1 lore raw

Hi Mark,

tools/include/nolibc/nolibc.h unsigned int sleep(unsigned int seconds) · line 2253

Quoted 8 lines · Mark Brown
tools/include/nolibc/nolibc.h +8 -0
+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