reftable/system: add abstraction to retrieve time in milliseconds

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 2, 2026 at 09:31 UTC cb0882de1979522b2fc3dc4c3064b0ad21d50b06
3 files changed +13 -23
reftable/stack.c
+4 -23
@@ -365,45 +365,26 @@ done:
365 return err;
366 }
367
368 -/* return negative if a before b. */
369 -static int tv_cmp(struct timeval *a, struct timeval *b)
370 -{
371 - time_t diff = a->tv_sec - b->tv_sec;
372 - int udiff = a->tv_usec - b->tv_usec;
373 -
374 - if (diff != 0)
375 - return diff;
376 -
377 - return udiff;
378 -}
379 -
368 static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
369 int reuse_open)
370 {
371 char **names = NULL, **names_after = NULL;
384 - struct timeval deadline;
372 + uint64_t deadline;
373 int64_t delay = 0;
374 int tries = 0, err;
375 int fd = -1;
376
389 - err = gettimeofday(&deadline, NULL);
390 - if (err < 0)
391 - goto out;
392 - deadline.tv_sec += 3;
377 + deadline = reftable_time_ms() + 3000;
378
379 while (1) {
395 - struct timeval now;
396 -
397 - err = gettimeofday(&now, NULL);
398 - if (err < 0)
399 - goto out;
380 + uint64_t now = reftable_time_ms();
381
382 /*
383 * Only look at deadlines after the first few times. This
384 * simplifies debugging in GDB.
385 */
386 tries++;
406 - if (tries > 3 && tv_cmp(&now, &deadline) >= 0)
387 + if (tries > 3 && now >= deadline)
388 goto out;
389
390 fd = open(st->list_file, O_RDONLY);
reftable/system.c
+6
@@ -4,6 +4,7 @@
4 #include "basics.h"
5 #include "reftable-error.h"
6 #include "../lockfile.h"
7 +#include "../trace.h"
8 #include "../tempfile.h"
9 #include "../write-or-die.h"
10
@@ -137,3 +138,8 @@ int reftable_fsync(int fd)
138 {
139 return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
140 }
141 +
142 +uint64_t reftable_time_ms(void)
143 +{
144 + return getnanotime() / 1000000;
145 +}
reftable/system.h
+3
@@ -111,4 +111,7 @@ int flock_release(struct reftable_flock *l);
111 */
112 int flock_commit(struct reftable_flock *l);
113
114 +/* Report the time in milliseconds. */
115 +uint64_t reftable_time_ms(void);
116 +
117 #endif