Fix timer setTimeout issue and some coding style issues (#459)

This commit is contained in:
Wenyong Huang
2020-12-04 18:07:52 +08:00
committed by GitHub
parent 2f530e67fc
commit 5176fe2595
4 changed files with 196 additions and 155 deletions

View File

@ -8,17 +8,19 @@
#include "../app-manager/module_wasm_app.h"
#include "timer_native_api.h"
static bool timer_thread_run = true;
bh_list g_timer_ctx_list;
korp_cond g_timer_ctx_list_cond;
korp_mutex g_timer_ctx_list_mutex;
typedef struct {
bh_list_link l;
timer_ctx_t timer_ctx;
} timer_ctx_node_t;
void wasm_timer_callback(timer_id_t id, unsigned int mod_id)
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)
@ -29,13 +31,14 @@ void wasm_timer_callback(timer_id_t id, unsigned int mod_id)
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.
/**
* 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)
{
@ -68,37 +71,41 @@ void * thread_modulers_timer_check(void * arg)
return NULL;
}
void wakeup_modules_timer_thread(timer_ctx_t ctx)
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);
}
void init_wasm_timer()
void
init_wasm_timer()
{
korp_tid tm_tid;
bh_list_init(&g_timer_ctx_list);
os_cond_init(&g_timer_ctx_list_cond);
/* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
/* temp solution for: thread_modulers_timer_check thread
would recursive lock the mutex */
os_recursive_mutex_init(&g_timer_ctx_list_mutex);
os_thread_create(&tm_tid, thread_modulers_timer_check,
NULL, BH_APPLET_PRESERVED_STACK_SIZE);
}
void exit_wasm_timer()
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
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);
prealloc_num, module_id);
if (ctx == NULL)
return NULL;
@ -119,11 +126,14 @@ timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
return ctx;
}
void destroy_module_timer_ctx(unsigned int module_id)
void
destroy_module_timer_ctx(unsigned int module_id)
{
timer_ctx_node_t* elem;
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);
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);
@ -137,7 +147,8 @@ void destroy_module_timer_ctx(unsigned int module_id)
os_mutex_unlock(&g_timer_ctx_list_mutex);
}
timer_ctx_t get_wasm_timer_ctx(wasm_module_inst_t module_inst)
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);
@ -184,8 +195,6 @@ wasm_timer_restart(wasm_exec_env_t exec_env,
sys_timer_restart(timer_ctx, timer_id, interval);
}
extern uint32 get_sys_tick_ms();
uint32
wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
{