summaryrefslogtreecommitdiff
path: root/tests/timer_loop.c
blob: 77ea956b090fa5cbab3e7df417fbada97357b512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <nnwt/common.h>
#include <nnwt/thread.h>
#include <nnwt/timer.h>
#include <nnwt/time.h>

#define DELAY 100000
#define REPEAT 21

static f64 start;
static u32 iter = 0u;

static void timer_callback(void *userdata, struct nn_timer *timer)
{
    (void)userdata;
    u64 now = nn_get_timestamp();
    u64 diff = (nn_get_tick() - start) * 1000000;
    s64 adj = iter * DELAY - (s64)diff;
    al_printf("[%2u (%llu)] %fs(%s%f)\n", iter, now, diff / 1000000.0, adj >= 0 ? "+" : "", adj / 1000000.0);
    nn_timer_set_repeat(timer, NNWT_TS_FROM_USEC(MAX(DELAY + adj, (s64)1)));
    if (iter++ == 0) nn_timer_again(timer);
    else if (iter == REPEAT) nn_timer_stop(timer);
}

s32 main(void)
{
    if (!nn_common_init(NULL)) return EXIT_FAILURE;

    struct nn_event_loop loop;
    struct nn_timer timer;

    nn_event_loop_init(&loop);

    start = nn_get_tick();
    nn_timer_init(&timer, &loop, timer_callback, NULL);
    nn_timer_set_repeat(&timer, NNWT_TS_FROM_USEC(DELAY));
    timer_callback(NULL, &timer);

    nn_event_loop_run(&loop);
    nn_event_loop_destroy(&loop);

    nn_common_close();

    return EXIT_SUCCESS;
}