[PATCH 4/6] reftable/system: add abstraction to retrieve time in milliseconds
- From
Patrick Steinhardt <ps@pks.im>
- Date
- Mar 31, 2026, 11:26 UTC
- Message-ID
- <20260331-pks-reftable-portability-fixes-v1-4-46bfae55c68c@pks.im>
- In-Reply-To
- <20260331-pks-reftable-portability-fixes-v1-0-46bfae55c68c@pks.im>
We directly call gettimeofday(3p), which may not be available on some platforms. Provide the infrastructure to let projects easily use their own implementations of this function.
Signed-off-by: Patrick Steinhardt <ps@pks.im> --- reftable/stack.c | 27 ++++----------------------- reftable/system.c | 6 ++++++ reftable/system.h | 3 +++ 3 files changed, 13 insertions(+), 23 deletions(-)
diff --git a/reftable/stack.c b/reftable/stack.c index f9ae832e3a..2d2c9b1f84 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -372,45 +372,26 @@ static int reftable_stack_reload_once(struct reftable_stack *st, return err; } -/* return negative if a before b. */ -static int tv_cmp(struct timeval *a, struct timeval *b) -{ - time_t diff = a->tv_sec - b->tv_sec; - int udiff = a->tv_usec - b->tv_usec; - - if (diff != 0) - return diff; - - return udiff; -} - static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st, int reuse_open) { char **names = NULL, **names_after = NULL; - struct timeval deadline; + uint64_t deadline; int64_t delay = 0; int tries = 0, err; int fd = -1; - err = gettimeofday(&deadline, NULL); - if (err < 0) - goto out; - deadline.tv_sec += 3; + deadline = reftable_time_ms() + 3000; while (1) { - struct timeval now; - - err = gettimeofday(&now, NULL); - if (err < 0) - goto out; + uint64_t now = reftable_time_ms(); /* * Only look at deadlines after the first few times. This * simplifies debugging in GDB. */ tries++; - if (tries > 3 && tv_cmp(&now, &deadline) >= 0) + if (tries > 3 && now >= deadline) goto out; fd = open(st->list_file, O_RDONLY); diff --git a/reftable/system.c b/reftable/system.c index 725a25844e..7aecd3859d 100644 --- a/reftable/system.c +++ b/reftable/system.c @@ -4,6 +4,7 @@ #include "basics.h" #include "reftable-error.h" #include "../lockfile.h" +#include "../trace.h" #include "../tempfile.h" uint32_t reftable_rand(void) @@ -131,3 +132,8 @@ int flock_commit(struct reftable_flock *l) return 0; } + +uint64_t reftable_time_ms(void) +{ + return getnanotime() / 1000000; +} diff --git a/reftable/system.h b/reftable/system.h index b15768dbdb..6e00cd32a3 100644 --- a/reftable/system.h +++ b/reftable/system.h @@ -110,4 +110,7 @@ int flock_release(struct reftable_flock *l); */ int flock_commit(struct reftable_flock *l); +/* Report the time in milliseconds. */ +uint64_t reftable_time_ms(void); + #endif
-- 2.53.0.1185.g05d4b7b318.dirty