machinarium: set/update clock time

This commit is contained in:
Dmitry Simonenko 2017-04-10 15:55:59 +03:00
parent dbbd2704de
commit b22e65c841
4 changed files with 25 additions and 1 deletions

View File

@ -14,6 +14,7 @@
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <signal.h>
#include <unistd.h>

View File

@ -29,7 +29,7 @@ void mm_clock_init(mm_clock_t *clock)
mm_buf_init(&clock->timers);
clock->timers_count = 0;
clock->timers_seq = 0;
clock->time = 0;
clock->time = 0;
}
void mm_clock_free(mm_clock_t *clock)
@ -118,3 +118,17 @@ int mm_clock_step(mm_clock_t *clock)
clock->timers_count -= timers_hit;
return timers_hit;
}
static uint64_t
mm_clock_gettime(void)
{
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * (uint64_t) 1e9 + t.tv_nsec;
}
void mm_clock_update(mm_clock_t *clock)
{
/* set current time in millisecond precision */
clock->time = mm_clock_gettime() / 1000000;
}

View File

@ -18,6 +18,7 @@ struct mm_clock_t {
void mm_clock_init(mm_clock_t*);
void mm_clock_free(mm_clock_t*);
void mm_clock_update(mm_clock_t*);
int mm_clock_step(mm_clock_t*);
int mm_clock_timer_add(mm_clock_t*, mm_timer_t*);
int mm_clock_timer_del(mm_clock_t*, mm_timer_t*);

View File

@ -30,15 +30,23 @@ int mm_loop_shutdown(mm_loop_t *loop)
int mm_loop_step(mm_loop_t *loop)
{
/* update clock time */
mm_clock_update(&loop->clock);
/* get minimal timer timeout */
int timeout = INT_MAX;
mm_timer_t *min;
min = mm_clock_timer_min(&loop->clock);
if (min == NULL)
timeout = min->timeout;
/* poll for events */
int rc;
rc = loop->poll->iface->step(loop->poll, timeout);
if (rc == -1)
return -1;
/* run timers */
mm_clock_step(&loop->clock);
return 0;
}