summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/app-framework/base/native/timer_wrapper.c
blob: 246868849dfb115fb31de53f551ac1eb213bbd18 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "bh_platform.h"
#include "app_manager_export.h"
#include "../app-manager/module_wasm_app.h"
#include "timer_native_api.h"

typedef struct {
    bh_list_link l;
    timer_ctx_t timer_ctx;
} timer_ctx_node_t;

static bool timer_thread_run = true;

static bh_list g_timer_ctx_list;
static korp_cond g_timer_ctx_list_cond;
static korp_mutex g_timer_ctx_list_mutex;

void
wasm_timer_callback(timer_id_t id, unsigned int mod_id)
{
    module_data *module = module_data_list_lookup_id(mod_id);
    if (module == NULL)
        return;

    // !!! the length parameter must be 0, so the receiver will
    //     not free the payload pointer.
    bh_post_msg(module->queue, TIMER_EVENT_WASM, (char *)(uintptr_t)id, 0);
}

/**
 * why we create a separate link for module timer contexts
 * rather than traverse the module list?
 * It helps to reduce the lock frequency for the module list.
 * Also when we lock the module list and then call the callback for
 * timer expire, the callback is request the list lock again for lookup
 * the module from module id. It is for avoiding that situation.
 */

void *
thread_modulers_timer_check(void *arg)
{
    uint32 ms_to_expiry;
    uint64 us_to_wait;

    while (timer_thread_run) {
        ms_to_expiry = (uint32)-1;
        os_mutex_lock(&g_timer_ctx_list_mutex);
        timer_ctx_node_t *elem =
            (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list);
        while (elem) {
            uint32 next = check_app_timers(elem->timer_ctx);
            if (next != (uint32)-1) {
                if (ms_to_expiry == (uint32)-1 || ms_to_expiry > next)
                    ms_to_expiry = next;
            }

            elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
        }
        os_mutex_unlock(&g_timer_ctx_list_mutex);

        if (ms_to_expiry == (uint32)-1)
            us_to_wait = BHT_WAIT_FOREVER;
        else
            us_to_wait = (uint64)ms_to_expiry * 1000;
        os_mutex_lock(&g_timer_ctx_list_mutex);
        os_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
                             us_to_wait);
        os_mutex_unlock(&g_timer_ctx_list_mutex);
    }

    return NULL;
}

void
wakeup_modules_timer_thread(timer_ctx_t ctx)
{
    os_mutex_lock(&g_timer_ctx_list_mutex);
    os_cond_signal(&g_timer_ctx_list_cond);
    os_mutex_unlock(&g_timer_ctx_list_mutex);
}

bool
init_wasm_timer()
{
    korp_tid tm_tid;
    bh_list_init(&g_timer_ctx_list);

    if (os_cond_init(&g_timer_ctx_list_cond) != 0) {
        return false;
    }
    /* temp solution for: thread_modulers_timer_check thread
       would recursive lock the mutex */
    if (os_recursive_mutex_init(&g_timer_ctx_list_mutex) != 0) {
        goto fail1;
    }

    if (0
        != os_thread_create(&tm_tid, thread_modulers_timer_check, NULL,
                            BH_APPLET_PRESERVED_STACK_SIZE)) {
        goto fail2;
    }

    return true;

fail2:
    os_mutex_destroy(&g_timer_ctx_list_mutex);

fail1:
    os_cond_destroy(&g_timer_ctx_list_cond);

    return false;
}

void
exit_wasm_timer()
{
    timer_thread_run = false;
}

timer_ctx_t
create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
{
    timer_ctx_t ctx =
        create_timer_ctx(wasm_timer_callback, wakeup_modules_timer_thread,
                         prealloc_num, module_id);

    if (ctx == NULL)
        return NULL;

    timer_ctx_node_t *node =
        (timer_ctx_node_t *)wasm_runtime_malloc(sizeof(timer_ctx_node_t));
    if (node == NULL) {
        destroy_timer_ctx(ctx);
        return NULL;
    }
    memset(node, 0, sizeof(*node));
    node->timer_ctx = ctx;

    os_mutex_lock(&g_timer_ctx_list_mutex);
    bh_list_insert(&g_timer_ctx_list, node);
    os_mutex_unlock(&g_timer_ctx_list_mutex);

    return ctx;
}

void
destroy_module_timer_ctx(unsigned int module_id)
{
    timer_ctx_node_t *elem;

    os_mutex_lock(&g_timer_ctx_list_mutex);
    elem = (timer_ctx_node_t *)bh_list_first_elem(&g_timer_ctx_list);
    while (elem) {
        if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
            bh_list_remove(&g_timer_ctx_list, elem);
            destroy_timer_ctx(elem->timer_ctx);
            wasm_runtime_free(elem);
            break;
        }

        elem = (timer_ctx_node_t *)bh_list_elem_next(elem);
    }
    os_mutex_unlock(&g_timer_ctx_list_mutex);
}

timer_ctx_t
get_wasm_timer_ctx(wasm_module_inst_t module_inst)
{
    module_data *m = app_manager_get_module_data(Module_WASM_App, module_inst);
    if (m == NULL)
        return NULL;
    return m->timer_ctx;
}

timer_id_t
wasm_create_timer(wasm_exec_env_t exec_env, int interval, bool is_period,
                  bool auto_start)
{
    wasm_module_inst_t module_inst = get_module_inst(exec_env);
    timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
    bh_assert(timer_ctx);
    return sys_create_timer(timer_ctx, interval, is_period, auto_start);
}

void
wasm_timer_destroy(wasm_exec_env_t exec_env, timer_id_t timer_id)
{
    wasm_module_inst_t module_inst = get_module_inst(exec_env);
    timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
    bh_assert(timer_ctx);
    sys_timer_destroy(timer_ctx, timer_id);
}

void
wasm_timer_cancel(wasm_exec_env_t exec_env, timer_id_t timer_id)
{
    wasm_module_inst_t module_inst = get_module_inst(exec_env);
    timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
    bh_assert(timer_ctx);
    sys_timer_cancel(timer_ctx, timer_id);
}

void
wasm_timer_restart(wasm_exec_env_t exec_env, timer_id_t timer_id, int interval)
{
    wasm_module_inst_t module_inst = get_module_inst(exec_env);
    timer_ctx_t timer_ctx = get_wasm_timer_ctx(module_inst);
    bh_assert(timer_ctx);
    sys_timer_restart(timer_ctx, timer_id, interval);
}

uint32
wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
{
    return (uint32)bh_get_tick_ms();
}