Skip to content

Commit cb0882d

Browse files
pks-tgitster
authored andcommitted
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>
1 parent aa89385 commit cb0882d

3 files changed

Lines changed: 13 additions & 23 deletions

File tree

reftable/stack.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -365,45 +365,26 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
365365
return err;
366366
}
367367

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-
380368
static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
381369
int reuse_open)
382370
{
383371
char **names = NULL, **names_after = NULL;
384-
struct timeval deadline;
372+
uint64_t deadline;
385373
int64_t delay = 0;
386374
int tries = 0, err;
387375
int fd = -1;
388376

389-
err = gettimeofday(&deadline, NULL);
390-
if (err < 0)
391-
goto out;
392-
deadline.tv_sec += 3;
377+
deadline = reftable_time_ms() + 3000;
393378

394379
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();
400381

401382
/*
402383
* Only look at deadlines after the first few times. This
403384
* simplifies debugging in GDB.
404385
*/
405386
tries++;
406-
if (tries > 3 && tv_cmp(&now, &deadline) >= 0)
387+
if (tries > 3 && now >= deadline)
407388
goto out;
408389

409390
fd = open(st->list_file, O_RDONLY);

reftable/system.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "basics.h"
55
#include "reftable-error.h"
66
#include "../lockfile.h"
7+
#include "../trace.h"
78
#include "../tempfile.h"
89
#include "../write-or-die.h"
910

@@ -137,3 +138,8 @@ int reftable_fsync(int fd)
137138
{
138139
return fsync_component(FSYNC_COMPONENT_REFERENCE, fd);
139140
}
141+
142+
uint64_t reftable_time_ms(void)
143+
{
144+
return getnanotime() / 1000000;
145+
}

reftable/system.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,7 @@ int flock_release(struct reftable_flock *l);
111111
*/
112112
int flock_commit(struct reftable_flock *l);
113113

114+
/* Report the time in milliseconds. */
115+
uint64_t reftable_time_ms(void);
116+
114117
#endif

0 commit comments

Comments
 (0)