aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2020-12-11 16:48:47 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2020-12-11 16:48:47 +0100
commit030fe31760169783537162b83af89e551bf120f6 (patch)
tree92b219784b478405626b9259b82900ab3ca72a8e /libbb
parent56ee5765074b2f2389066f3234a4da21501d3eaa (diff)
downloadbusybox-030fe31760169783537162b83af89e551bf120f6.tar.gz
libbb: make msleep() result in only one syscall instead of looping
function old new delta msleep 45 52 +7 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/bb_do_delay.c18
-rw-r--r--libbb/platform.c1
2 files changed, 18 insertions, 1 deletions
diff --git a/libbb/bb_do_delay.c b/libbb/bb_do_delay.c
index 3dbf032db..9a84fa24b 100644
--- a/libbb/bb_do_delay.c
+++ b/libbb/bb_do_delay.c
@@ -36,6 +36,7 @@ void FAST_FUNC sleep1(void)
void FAST_FUNC msleep(unsigned ms)
{
+#if 0
/* 1. usleep(n) is not guaranteed by standards to accept n >= 1000000
* 2. multiplication in usleep(ms * 1000) can overflow if ms > 4294967
* (sleep of ~71.5 minutes)
@@ -46,4 +47,21 @@ void FAST_FUNC msleep(unsigned ms)
ms -= 500;
}
usleep(ms * 1000);
+#else
+//usleep is often implemented as a call to nanosleep.
+//Simply do the same to implement msleep.
+//it's marginally larger, but wakes your CPU less often:
+//function old new delta
+//msleep 45 52 +7
+ struct timespec ts;
+ ts.tv_sec = ms / 1000;
+ ts.tv_nsec = (ms % 1000) * 1000000;
+ /*
+ * If a signal has non-default handler, nanosleep returns early.
+ * Our version of msleep doesn't return early
+ * if interrupted by such signals:
+ */
+ while (nanosleep(&ts, &ts) != 0)
+ continue;
+#endif
}
diff --git a/libbb/platform.c b/libbb/platform.c
index d2b263a6d..329b0237e 100644
--- a/libbb/platform.c
+++ b/libbb/platform.c
@@ -27,7 +27,6 @@ int FAST_FUNC usleep(unsigned usec)
* If a signal has non-default handler, nanosleep returns early.
* Our version of usleep doesn't return early
* if interrupted by such signals:
- *
*/
while (nanosleep(&ts, &ts) != 0)
continue;