diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-11 16:48:47 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-11 16:48:47 +0100 |
commit | 030fe31760169783537162b83af89e551bf120f6 (patch) | |
tree | 92b219784b478405626b9259b82900ab3ca72a8e | |
parent | 56ee5765074b2f2389066f3234a4da21501d3eaa (diff) | |
download | busybox-w32-030fe31760169783537162b83af89e551bf120f6.tar.gz busybox-w32-030fe31760169783537162b83af89e551bf120f6.tar.bz2 busybox-w32-030fe31760169783537162b83af89e551bf120f6.zip |
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>
-rw-r--r-- | libbb/bb_do_delay.c | 18 | ||||
-rw-r--r-- | libbb/platform.c | 1 | ||||
-rw-r--r-- | networking/ifupdown.c | 6 |
3 files changed, 21 insertions, 4 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) | |||
36 | 36 | ||
37 | void FAST_FUNC msleep(unsigned ms) | 37 | void FAST_FUNC msleep(unsigned ms) |
38 | { | 38 | { |
39 | #if 0 | ||
39 | /* 1. usleep(n) is not guaranteed by standards to accept n >= 1000000 | 40 | /* 1. usleep(n) is not guaranteed by standards to accept n >= 1000000 |
40 | * 2. multiplication in usleep(ms * 1000) can overflow if ms > 4294967 | 41 | * 2. multiplication in usleep(ms * 1000) can overflow if ms > 4294967 |
41 | * (sleep of ~71.5 minutes) | 42 | * (sleep of ~71.5 minutes) |
@@ -46,4 +47,21 @@ void FAST_FUNC msleep(unsigned ms) | |||
46 | ms -= 500; | 47 | ms -= 500; |
47 | } | 48 | } |
48 | usleep(ms * 1000); | 49 | usleep(ms * 1000); |
50 | #else | ||
51 | //usleep is often implemented as a call to nanosleep. | ||
52 | //Simply do the same to implement msleep. | ||
53 | //it's marginally larger, but wakes your CPU less often: | ||
54 | //function old new delta | ||
55 | //msleep 45 52 +7 | ||
56 | struct timespec ts; | ||
57 | ts.tv_sec = ms / 1000; | ||
58 | ts.tv_nsec = (ms % 1000) * 1000000; | ||
59 | /* | ||
60 | * If a signal has non-default handler, nanosleep returns early. | ||
61 | * Our version of msleep doesn't return early | ||
62 | * if interrupted by such signals: | ||
63 | */ | ||
64 | while (nanosleep(&ts, &ts) != 0) | ||
65 | continue; | ||
66 | #endif | ||
49 | } | 67 | } |
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) | |||
27 | * If a signal has non-default handler, nanosleep returns early. | 27 | * If a signal has non-default handler, nanosleep returns early. |
28 | * Our version of usleep doesn't return early | 28 | * Our version of usleep doesn't return early |
29 | * if interrupted by such signals: | 29 | * if interrupted by such signals: |
30 | * | ||
31 | */ | 30 | */ |
32 | while (nanosleep(&ts, &ts) != 0) | 31 | while (nanosleep(&ts, &ts) != 0) |
33 | continue; | 32 | continue; |
diff --git a/networking/ifupdown.c b/networking/ifupdown.c index 60ceb5a1f..fedf05aaf 100644 --- a/networking/ifupdown.c +++ b/networking/ifupdown.c | |||
@@ -1357,15 +1357,15 @@ static FILE *open_new_state_file(void) | |||
1357 | IFSTATE_FILE_PATH".new"); | 1357 | IFSTATE_FILE_PATH".new"); |
1358 | } | 1358 | } |
1359 | /* Someone else created the .new file */ | 1359 | /* Someone else created the .new file */ |
1360 | if (cnt > 30 * 1000) { | 1360 | if (cnt > 30) { |
1361 | /* Waited for 30*30/2 = 450 milliseconds, still EEXIST. | 1361 | /* Waited for 30*30/2 = 450 milliseconds, still EEXIST. |
1362 | * Assuming a stale file, rewriting it. | 1362 | * Assuming a stale file, rewriting it. |
1363 | */ | 1363 | */ |
1364 | flags = (O_WRONLY | O_CREAT | O_TRUNC); | 1364 | flags = (O_WRONLY | O_CREAT | O_TRUNC); |
1365 | continue; | 1365 | continue; |
1366 | } | 1366 | } |
1367 | usleep(cnt); | 1367 | msleep(cnt); |
1368 | cnt += 1000; | 1368 | cnt++; |
1369 | } | 1369 | } |
1370 | 1370 | ||
1371 | return xfdopen_for_write(fd); | 1371 | return xfdopen_for_write(fd); |