← Commit list

030fe3176

Denys Vlasenko
Denys Vlasenko
2020-12-11
SUCCESS Duration: 150s

Subject

libbb: make msleep() result in only one syscall instead of looping

Binary

No binary (build failed)

Architecture & Compiler Flags

Architecture: x86_64-linux-gnu Compiler: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0

—

Build Log

(log yok)

Commit Diff GitHub →

libbb/bb_do_delay.c+18 -0
@@ -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
}
libbb/platform.c+0 -1
@@ -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;
networking/ifupdown.c+3 -3
@@ -1357,15 +1357,15 @@ static FILE *open_new_state_file(void)
IFSTATE_FILE_PATH".new");
}
/* Someone else created the .new file */
- if (cnt > 30 * 1000) {
+ if (cnt > 30) {
/* Waited for 30*30/2 = 450 milliseconds, still EEXIST.
* Assuming a stale file, rewriting it.
*/
flags = (O_WRONLY | O_CREAT | O_TRUNC);
continue;
}
- usleep(cnt);
- cnt += 1000;
+ msleep(cnt);
+ cnt++;
}
return xfdopen_for_write(fd);