X Tutup
Skip to content

Commit 496db33

Browse files
committed
tree-wide: use usec_add() and usec_sub_unsigned()
1 parent e6283cb commit 496db33

File tree

14 files changed

+30
-36
lines changed

14 files changed

+30
-36
lines changed

src/ask-password/ask-password.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static int run(int argc, char *argv[]) {
166166
return r;
167167

168168
if (arg_timeout > 0)
169-
timeout = now(CLOCK_MONOTONIC) + arg_timeout;
169+
timeout = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
170170
else
171171
timeout = 0;
172172

src/basic/process-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
756756

757757
/* Drop into a sigtimewait-based timeout. Waiting for the
758758
* pid to exit. */
759-
until = now(CLOCK_MONOTONIC) + timeout;
759+
until = usec_add(now(CLOCK_MONOTONIC), timeout);
760760
for (;;) {
761761
usec_t n;
762762
siginfo_t status = {};

src/basic/ratelimit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bool ratelimit_below(RateLimit *r) {
1919
ts = now(CLOCK_MONOTONIC);
2020

2121
if (r->begin <= 0 ||
22-
ts - r->begin > r->interval) {
22+
usec_sub_unsigned(ts, r->begin) > r->interval) {
2323
r->begin = ts;
2424

2525
/* Reset counter */

src/basic/terminal-util.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,11 @@ int acquire_terminal(
440440

441441
assert(ts != USEC_INFINITY);
442442

443-
n = now(CLOCK_MONOTONIC);
444-
if (ts + timeout < n)
443+
n = usec_sub_unsigned(now(CLOCK_MONOTONIC), ts);
444+
if (n >= timeout)
445445
return -ETIMEDOUT;
446446

447-
r = fd_wait_for_event(notify, POLLIN, ts + timeout - n);
447+
r = fd_wait_for_event(notify, POLLIN, usec_sub_unsigned(timeout, n));
448448
if (r < 0)
449449
return r;
450450
if (r == 0)

src/cgtop/cgtop.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ static int run(int argc, char *argv[]) {
953953

954954
t = now(CLOCK_MONOTONIC);
955955

956-
if (t >= last_refresh + arg_delay || immediate_refresh) {
956+
if (t >= usec_add(last_refresh, arg_delay) || immediate_refresh) {
957957

958958
r = refresh(root, a, b, iteration++);
959959
if (r < 0)
@@ -976,9 +976,9 @@ static int run(int argc, char *argv[]) {
976976
fflush(stdout);
977977

978978
if (arg_batch)
979-
(void) usleep(last_refresh + arg_delay - t);
979+
(void) usleep(usec_add(usec_sub_unsigned(last_refresh, t), arg_delay));
980980
else {
981-
r = read_one_char(stdin, &key, last_refresh + arg_delay - t, NULL);
981+
r = read_one_char(stdin, &key, usec_add(usec_sub_unsigned(last_refresh, t), arg_delay), NULL);
982982
if (r == -ETIMEDOUT)
983983
continue;
984984
if (r < 0)
@@ -1053,10 +1053,7 @@ static int run(int argc, char *argv[]) {
10531053
break;
10541054

10551055
case '+':
1056-
if (arg_delay < USEC_PER_SEC)
1057-
arg_delay += USEC_PER_MSEC*250;
1058-
else
1059-
arg_delay += USEC_PER_SEC;
1056+
arg_delay = usec_add(arg_delay, arg_delay < USEC_PER_SEC ? USEC_PER_MSEC * 250 : USEC_PER_SEC);
10601057

10611058
fprintf(stdout, "\nIncreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0));
10621059
fflush(stdout);
@@ -1066,10 +1063,8 @@ static int run(int argc, char *argv[]) {
10661063
case '-':
10671064
if (arg_delay <= USEC_PER_MSEC*500)
10681065
arg_delay = USEC_PER_MSEC*250;
1069-
else if (arg_delay < USEC_PER_MSEC*1250)
1070-
arg_delay -= USEC_PER_MSEC*250;
10711066
else
1072-
arg_delay -= USEC_PER_SEC;
1067+
arg_delay = usec_sub_unsigned(arg_delay, arg_delay < USEC_PER_MSEC * 1250 ? USEC_PER_MSEC * 250 : USEC_PER_SEC);
10731068

10741069
fprintf(stdout, "\nDecreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0));
10751070
fflush(stdout);

src/cryptsetup/cryptsetup.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,10 +1503,9 @@ static int run(int argc, char *argv[]) {
15031503

15041504
flags = determine_flags();
15051505

1506-
if (arg_timeout == USEC_INFINITY)
1506+
until = usec_add(now(CLOCK_MONOTONIC), arg_timeout);
1507+
if (until == USEC_INFINITY)
15071508
until = 0;
1508-
else
1509-
until = now(CLOCK_MONOTONIC) + arg_timeout;
15101509

15111510
arg_key_size = (arg_key_size > 0 ? arg_key_size : (256 / 8));
15121511

src/journal/journald-server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static int cache_space_refresh(Server *s, JournalStorage *storage) {
144144

145145
ts = now(CLOCK_MONOTONIC);
146146

147-
if (space->timestamp != 0 && space->timestamp + RECHECK_SPACE_USEC > ts)
147+
if (space->timestamp != 0 && usec_add(space->timestamp, RECHECK_SPACE_USEC) > ts)
148148
return 0;
149149

150150
r = determine_path_usage(s, storage->path, &vfs_used, &vfs_avail);
@@ -1206,7 +1206,7 @@ int server_flush_to_var(Server *s, bool require_flag_file) {
12061206
server_driver_message(s, 0, NULL,
12071207
LOG_MESSAGE("Time spent on flushing to %s is %s for %u entries.",
12081208
s->system_storage.path,
1209-
format_timespan(ts, sizeof(ts), now(CLOCK_MONOTONIC) - start, 0),
1209+
format_timespan(ts, sizeof(ts), usec_sub_unsigned(now(CLOCK_MONOTONIC), start), 0),
12101210
n),
12111211
NULL);
12121212

src/libsystemd/sd-bus/sd-bus.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,9 @@ _public_ int sd_bus_send_to(sd_bus *bus, sd_bus_message *m, const char *destinat
22052205
static usec_t calc_elapse(sd_bus *bus, uint64_t usec) {
22062206
assert(bus);
22072207

2208-
if (usec == (uint64_t) -1)
2208+
assert_cc(sizeof(usec_t) == sizeof(uint64_t));
2209+
2210+
if (usec == USEC_INFINITY)
22092211
return 0;
22102212

22112213
/* We start all timeouts the instant we enter BUS_HELLO/BUS_RUNNING state, so that the don't run in parallel
@@ -2215,7 +2217,7 @@ static usec_t calc_elapse(sd_bus *bus, uint64_t usec) {
22152217
if (IN_SET(bus->state, BUS_WATCH_BIND, BUS_OPENING, BUS_AUTHENTICATING))
22162218
return usec;
22172219
else
2218-
return now(CLOCK_MONOTONIC) + usec;
2220+
return usec_add(now(CLOCK_MONOTONIC), usec);
22192221
}
22202222

22212223
static int timeout_compare(const void *a, const void *b) {

src/libsystemd/sd-event/sd-event.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,10 +2594,11 @@ _public_ int sd_event_source_set_time_relative(sd_event_source *s, uint64_t usec
25942594
if (r < 0)
25952595
return r;
25962596

2597-
if (usec >= USEC_INFINITY - t)
2597+
usec = usec_add(t, usec);
2598+
if (usec == USEC_INFINITY)
25982599
return -EOVERFLOW;
25992600

2600-
return sd_event_source_set_time(s, t + usec);
2601+
return sd_event_source_set_time(s, usec);
26012602
}
26022603

26032604
_public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {

src/libsystemd/sd-journal/sd-journal.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,10 +2725,7 @@ _public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
27252725
return r;
27262726

27272727
if (t != (uint64_t) -1) {
2728-
usec_t n;
2729-
2730-
n = now(CLOCK_MONOTONIC);
2731-
t = t > n ? t - n : 0;
2728+
t = usec_sub_unsigned(t, now(CLOCK_MONOTONIC));
27322729

27332730
if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
27342731
timeout_usec = t;

0 commit comments

Comments
 (0)
X Tutup