]> git.ipfire.org Git - thirdparty/git.git/commitdiff
reftable/system: add abstraction to retrieve time in milliseconds
authorPatrick Steinhardt <ps@pks.im>
Thu, 2 Apr 2026 07:31:17 +0000 (09:31 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 2 Apr 2026 17:45:43 +0000 (10:45 -0700)
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>
reftable/stack.c
reftable/system.c
reftable/system.h

index fa87b46c377af2b3b8d484309ef7b682e050b862..1fba96ddb3366f95fab16943768864de27b347a6 100644 (file)
@@ -365,45 +365,26 @@ done:
        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);
index 4d7e366b55b6d6b5551732a74a4ac4189522dea1..cd76e56be8f16f2af8d6a9a00641ea4c6fe2735a 100644 (file)
@@ -4,6 +4,7 @@
 #include "basics.h"
 #include "reftable-error.h"
 #include "../lockfile.h"
+#include "../trace.h"
 #include "../tempfile.h"
 #include "../write-or-die.h"
 
@@ -137,3 +138,8 @@ int reftable_fsync(int fd)
 {
        return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
 }
+
+uint64_t reftable_time_ms(void)
+{
+       return getnanotime() / 1000000;
+}
index a7eb6acd4a4b67c29519d8d2b631a3ed6d5cd864..071bfa3d58962cfe2632e224d2b4657cc7997d77 100644 (file)
@@ -111,4 +111,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