time-util: add macros around timespec_store() that operates on compund literal allocated timespec struct

This way we can convert usec_t to timespec on-the-fly, without a buffer.

No actual behaviour change just some shortening of code.
This commit is contained in:
Lennart Poettering
2022-03-18 17:13:36 +01:00
parent de0988f9d2
commit 52bb308c13
10 changed files with 17 additions and 26 deletions
+1 -2
View File
@@ -159,7 +159,6 @@ int pipe_eof(int fd) {
}
int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
struct timespec ts;
int r;
assert(fds || nfds == 0);
@@ -167,7 +166,7 @@ int ppoll_usec(struct pollfd *fds, size_t nfds, usec_t timeout) {
if (nfds == 0)
return 0;
r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : timespec_store(&ts, timeout), NULL);
r = ppoll(fds, nfds, timeout == USEC_INFINITY ? NULL : TIMESPEC_STORE(timeout), NULL);
if (r < 0)
return -errno;
if (r == 0)
+1 -2
View File
@@ -820,13 +820,12 @@ int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
for (;;) {
usec_t n;
siginfo_t status = {};
struct timespec ts;
n = now(CLOCK_MONOTONIC);
if (n >= until)
break;
r = RET_NERRNO(sigtimedwait(&mask, NULL, timespec_store(&ts, until - n)));
r = RET_NERRNO(sigtimedwait(&mask, NULL, TIMESPEC_STORE(until - n)));
/* Assuming we woke due to the child exiting. */
if (waitid(P_PID, pid, &status, WEXITED|WNOHANG) == 0) {
if (status.si_pid == pid) {
+4
View File
@@ -115,9 +115,13 @@ nsec_t timespec_load_nsec(const struct timespec *ts) _pure_;
struct timespec* timespec_store(struct timespec *ts, usec_t u);
struct timespec* timespec_store_nsec(struct timespec *ts, nsec_t n);
#define TIMESPEC_STORE(u) timespec_store(&(struct timespec) {}, (u))
usec_t timeval_load(const struct timeval *tv) _pure_;
struct timeval* timeval_store(struct timeval *tv, usec_t u);
#define TIMEVAL_STORE(u) timeval_store(&(struct timeval) {}, (u))
char* format_timestamp_style(char *buf, size_t l, usec_t t, TimestampStyle style) _warn_unused_result_;
char* format_timestamp_relative(char *buf, size_t l, usec_t t) _warn_unused_result_;
char* format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) _warn_unused_result_;