summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/app/timer.c
blob: 692626ca36fbf31cb587c853fe7a00b41fb37c94 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include <stdlib.h>
#include <string.h>

#include "wa-inc/timer_wasm_app.h"
#include "timer_api.h"

#if 1
#include <stdio.h>
#else
#define printf (...)
#endif

struct user_timer {
    struct user_timer *next;
    int timer_id;
    void (*user_timer_callback)(user_timer_t);
};

struct user_timer *g_timers = NULL;

user_timer_t
api_timer_create(int interval, bool is_period, bool auto_start,
                 on_user_timer_update_f on_timer_update)
{

    int timer_id = wasm_create_timer(interval, is_period, auto_start);

    // TODO
    struct user_timer *timer =
        (struct user_timer *)malloc(sizeof(struct user_timer));
    if (timer == NULL) {
        // TODO: remove the timer_id
        printf("### api_timer_create malloc faild!!! \n");
        return NULL;
    }

    memset(timer, 0, sizeof(*timer));
    timer->timer_id = timer_id;
    timer->user_timer_callback = on_timer_update;

    if (g_timers == NULL)
        g_timers = timer;
    else {
        timer->next = g_timers;
        g_timers = timer;
    }

    return timer;
}

void
api_timer_cancel(user_timer_t timer)
{
    user_timer_t t = g_timers, prev = NULL;

    wasm_timer_cancel(timer->timer_id);

    while (t) {
        if (t == timer) {
            if (prev == NULL) {
                g_timers = t->next;
                free(t);
            }
            else {
                prev->next = t->next;
                free(t);
            }
            return;
        }
        else {
            prev = t;
            t = t->next;
        }
    }
}

void
api_timer_restart(user_timer_t timer, int interval)
{
    wasm_timer_restart(timer->timer_id, interval);
}

void
on_timer_callback(int timer_id)
{
    struct user_timer *t = g_timers;

    while (t) {
        if (t->timer_id == timer_id) {
            t->user_timer_callback(t);
            break;
        }
        t = t->next;
    }
}