Skip to content

Commit c080add

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 832845e commit c080add

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
@@ -372,45 +372,26 @@ static int reftable_stack_reload_once(struct reftable_stack *st,
372372
return err;
373373
}
374374

375-
/* return negative if a before b. */
376-
static int tv_cmp(struct timeval *a, struct timeval *b)
377-
{
378-
time_t diff = a->tv_sec - b->tv_sec;
379-
int udiff = a->tv_usec - b->tv_usec;
380-
381-
if (diff != 0)
382-
return diff;
383-
384-
return udiff;
385-
}
386-
387375
static int reftable_stack_reload_maybe_reuse(struct reftable_stack *st,
388376
int reuse_open)
389377
{
390378
char **names = NULL, **names_after = NULL;
391-
struct timeval deadline;
379+
uint64_t deadline;
392380
int64_t delay = 0;
393381
int tries = 0, err;
394382
int fd = -1;
395383

396-
err = gettimeofday(&deadline, NULL);
397-
if (err < 0)
398-
goto out;
399-
deadline.tv_sec += 3;
384+
deadline = reftable_time_ms() + 3000;
400385

401386
while (1) {
402-
struct timeval now;
403-
404-
err = gettimeofday(&now, NULL);
405-
if (err < 0)
406-
goto out;
387+
uint64_t now = reftable_time_ms();
407388

408389
/*
409390
* Only look at deadlines after the first few times. This
410391
* simplifies debugging in GDB.
411392
*/
412393
tries++;
413-
if (tries > 3 && tv_cmp(&now, &deadline) >= 0)
394+
if (tries > 3 && now >= deadline)
414395
goto out;
415396

416397
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

910
uint32_t reftable_rand(void)
@@ -131,3 +132,8 @@ int flock_commit(struct reftable_flock *l)
131132

132133
return 0;
133134
}
135+
136+
uint64_t reftable_time_ms(void)
137+
{
138+
return getnanotime() / 1000000;
139+
}

reftable/system.h

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

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

0 commit comments

Comments
 (0)