re-org platform APIs, simplify porting process (#201)

Co-authored-by: Xu Jun <jun1.xu@intel.com>
This commit is contained in:
Xu Jun
2020-03-16 16:43:57 +08:00
committed by GitHub
parent ef5ceffe71
commit f1a0e75ab7
177 changed files with 2954 additions and 7904 deletions

View File

@ -16,14 +16,7 @@ typedef union jvalue {
double d;
} jvalue;
#ifndef bh_memcpy_s
int b_memcpy_s(void * s1, unsigned int s1max,
const void * s2, unsigned int n);
#define bh_memcpy_s(dest, dlen, src, slen) do { \
int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \
(void)_ret; \
} while (0)
#endif
static inline int16_t get_int16(const char *buf)
{

View File

@ -9,30 +9,6 @@
/* Note: the bh_plaform.h is the only head file separately
implemented by both [app] and [native] worlds */
#include "bh_platform.h"
#include "wasm_export.h"
#define get_module_inst(exec_env) \
wasm_runtime_get_module_inst(exec_env)
#define validate_app_addr(offset, size) \
wasm_runtime_validate_app_addr(module_inst, offset, size)
#define validate_app_str_addr(offset) \
wasm_runtime_validate_app_str_addr(module_inst, offset)
#define addr_app_to_native(offset) \
wasm_runtime_addr_app_to_native(module_inst, offset)
#define addr_native_to_app(ptr) \
wasm_runtime_addr_native_to_app(module_inst, ptr)
#define module_malloc(size, p_native_addr) \
wasm_runtime_module_malloc(module_inst, size, p_native_addr)
#define module_free(offset) \
wasm_runtime_module_free(module_inst, offset)
/*char *wa_strdup(const char *);*/
#endif /* end of _NATIVE_INTERFACE_H */

View File

@ -79,23 +79,3 @@ char *wa_strdup(const char *s)
memcpy(s1, s, strlen(s) + 1);
return s1;
}
#define RSIZE_MAX 0x7FFFFFFF
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
{
char *dest = (char*) s1;
char *src = (char*) s2;
if (n == 0) {
return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memcpy(dest, src, n);
return 0;
}

View File

@ -35,14 +35,14 @@ typedef int int32;
#define WA_FREE free
#endif
char *wa_strdup(const char *s);
uint32 htonl(uint32 value);
uint32 ntohl(uint32 value);
uint16 htons(uint16 value);
uint16 ntohs(uint16 value);
int
b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
// We are not worried for the WASM world since the sandbox will catch it.
#define bh_memcpy_s(dst, dst_len, src, src_len) memcpy(dst, src, src_len)
#endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */

View File

@ -3,12 +3,9 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "runtime_timer.h"
#include "bh_platform.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "bh_list.h"
#include "bh_thread.h"
#include "bh_time.h"
#include "timer_native_api.h"
static bool timer_thread_run = true;
@ -46,7 +43,7 @@ void * thread_modulers_timer_check(void * arg)
while (timer_thread_run) {
ms_to_expiry = -1;
vm_mutex_lock(&g_timer_ctx_list_mutex);
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) {
@ -58,14 +55,14 @@ void * thread_modulers_timer_check(void * arg)
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_timer_ctx_list_mutex);
os_mutex_unlock(&g_timer_ctx_list_mutex);
if (ms_to_expiry == -1)
ms_to_expiry = 60 * 1000;
vm_mutex_lock(&g_timer_ctx_list_mutex);
vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
ms_to_expiry);
vm_mutex_unlock(&g_timer_ctx_list_mutex);
os_mutex_lock(&g_timer_ctx_list_mutex);
os_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
ms_to_expiry * 1000);
os_mutex_unlock(&g_timer_ctx_list_mutex);
}
return NULL;
@ -73,9 +70,9 @@ void * thread_modulers_timer_check(void * arg)
void wakeup_modules_timer_thread(timer_ctx_t ctx)
{
vm_mutex_lock(&g_timer_ctx_list_mutex);
vm_cond_signal(&g_timer_ctx_list_cond);
vm_mutex_unlock(&g_timer_ctx_list_mutex);
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()
@ -83,11 +80,11 @@ void init_wasm_timer()
korp_tid tm_tid;
bh_list_init(&g_timer_ctx_list);
vm_cond_init(&g_timer_ctx_list_cond);
os_cond_init(&g_timer_ctx_list_cond);
/* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
os_recursive_mutex_init(&g_timer_ctx_list_mutex);
vm_thread_create(&tm_tid, thread_modulers_timer_check,
os_thread_create(&tm_tid, thread_modulers_timer_check,
NULL, BH_APPLET_PRESERVED_STACK_SIZE);
}
@ -115,16 +112,16 @@ timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
memset(node, 0, sizeof(*node));
node->timer_ctx = ctx;
vm_mutex_lock(&g_timer_ctx_list_mutex);
os_mutex_lock(&g_timer_ctx_list_mutex);
bh_list_insert(&g_timer_ctx_list, node);
vm_mutex_unlock(&g_timer_ctx_list_mutex);
os_mutex_unlock(&g_timer_ctx_list_mutex);
return ctx;
}
void destroy_module_timer_ctx(unsigned int module_id)
{
vm_mutex_lock(&g_timer_ctx_list_mutex);
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) {
@ -137,7 +134,7 @@ void destroy_module_timer_ctx(unsigned int module_id)
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_timer_ctx_list_mutex);
os_mutex_unlock(&g_timer_ctx_list_mutex);
}
timer_ctx_t get_wasm_timer_ctx(wasm_module_inst_t module_inst)
@ -192,6 +189,6 @@ extern uint32 get_sys_tick_ms();
uint32
wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
{
return (uint32) bh_get_tick_ms();
return (uint32)bh_get_tick_ms();
}

View File

@ -11,14 +11,12 @@
*/
#include "connection_lib.h"
#include "bh_thread.h"
#include "bh_platform.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "conn_tcp.h"
#include "conn_udp.h"
#include "conn_uart.h"
#include "bh_common.h"
#include "bh_assert.h"
#include <unistd.h>
#include <sys/epoll.h>
@ -96,7 +94,7 @@ connection_interface_t connection_impl = {
static void add_connection(sys_connection_t *conn)
{
vm_mutex_lock(&g_lock);
os_mutex_lock(&g_lock);
g_handle_max++;
if (g_handle_max == -1)
@ -110,7 +108,7 @@ static void add_connection(sys_connection_t *conn)
g_connections = conn;
}
vm_mutex_unlock(&g_lock);
os_mutex_unlock(&g_lock);
}
#define FREE_CONNECTION(conn) do { \
@ -124,7 +122,7 @@ static int get_app_conns_num(uint32 module_id)
sys_connection_t *conn;
int num = 0;
vm_mutex_lock(&g_lock);
os_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
@ -133,7 +131,7 @@ static int get_app_conns_num(uint32 module_id)
conn = conn->next;
}
vm_mutex_unlock(&g_lock);
os_mutex_unlock(&g_lock);
return num;
}
@ -142,7 +140,7 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found)
{
sys_connection_t *conn, *prev = NULL;
vm_mutex_lock(&g_lock);
os_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
@ -154,7 +152,7 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found)
g_connections = conn->next;
}
}
vm_mutex_unlock(&g_lock);
os_mutex_unlock(&g_lock);
return conn;
} else {
prev = conn;
@ -162,7 +160,7 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found)
}
}
vm_mutex_unlock(&g_lock);
os_mutex_unlock(&g_lock);
return NULL;
}
@ -171,7 +169,7 @@ static void cleanup_connections(uint32 module_id)
{
sys_connection_t *conn, *prev = NULL;
vm_mutex_lock(&g_lock);
os_mutex_lock(&g_lock);
conn = g_connections;
while (conn) {
@ -194,7 +192,7 @@ static void cleanup_connections(uint32 module_id)
}
}
vm_mutex_unlock(&g_lock);
os_mutex_unlock(&g_lock);
}
static conn_type_t get_conn_type(const char *name)
@ -546,13 +544,13 @@ void app_mgr_connection_event_callback(module_data *m_data, bh_message_t msg)
bool init_connection_framework()
{
korp_thread tid;
korp_tid tid;
epollfd = epoll_create(MAX_EVENTS);
if (epollfd == -1)
return false;
if (vm_mutex_init(&g_lock) != 0) {
if (os_mutex_init(&g_lock) != 0) {
close(epollfd);
return false;
}
@ -566,7 +564,7 @@ bool init_connection_framework()
goto fail;
}
if (vm_thread_create(&tid,
if (os_thread_create(&tid,
polling_thread_routine,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE) != 0) {
@ -576,7 +574,7 @@ bool init_connection_framework()
return true;
fail:
vm_mutex_destroy(&g_lock);
os_mutex_destroy(&g_lock);
close(epollfd);
return false;
}

View File

@ -6,10 +6,7 @@
#include "runtime_sensor.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "bh_thread.h"
#include "bh_time.h"
#include "bh_common.h"
#include "bh_assert.h"
#include "bh_platform.h"
static sys_sensor_t * g_sys_sensors = NULL;
static int g_sensor_id_max = 0;
@ -103,11 +100,11 @@ wasm_sensor_config(wasm_exec_env_t exec_env,
module_inst);
bh_assert(mod_id != ID_NONE);
vm_mutex_lock(&s->lock);
os_mutex_lock(&s->lock);
c = find_sensor_client(s, mod_id, false);
if (c == NULL) {
vm_mutex_unlock(&s->lock);
os_mutex_unlock(&s->lock);
return false;
}
@ -115,7 +112,7 @@ wasm_sensor_config(wasm_exec_env_t exec_env,
c->bit_cfg = bit_cfg;
c->delay = delay;
vm_mutex_unlock(&s->lock);
os_mutex_unlock(&s->lock);
if (s->config != NULL) {
attr_cont = attr_container_create("config sensor");
@ -149,19 +146,19 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
module_inst);
bh_assert(mod_id != ID_NONE);
vm_mutex_lock(&s->lock);
os_mutex_lock(&s->lock);
c = find_sensor_client(s, mod_id, false);
if (c) {
// the app already opened this sensor
vm_mutex_unlock(&s->lock);
os_mutex_unlock(&s->lock);
return -1;
}
sensor_client_t * client = (sensor_client_t*) wasm_runtime_malloc(
sizeof(sensor_client_t));
if (client == NULL) {
vm_mutex_unlock(&s->lock);
os_mutex_unlock(&s->lock);
return -1;
}
@ -172,7 +169,7 @@ wasm_sensor_open(wasm_exec_env_t exec_env,
client->next = s->clients;
s->clients = client;
vm_mutex_unlock(&s->lock);
os_mutex_unlock(&s->lock);
refresh_read_interval(s);
@ -218,10 +215,10 @@ wasm_sensor_close(wasm_exec_env_t exec_env, uint32 sensor)
if (s == NULL)
return false;
vm_mutex_lock(&s->lock);
os_mutex_lock(&s->lock);
if ((c = find_sensor_client(s, client_id, true)) != NULL)
wasm_runtime_free(c);
vm_mutex_unlock(&s->lock);
os_mutex_unlock(&s->lock);
refresh_read_interval(s);
@ -251,7 +248,7 @@ void refresh_read_interval(sensor_obj_t sensor)
{
sensor_client_t *c;
uint32 interval = sensor->default_interval;
vm_mutex_lock(&sensor->lock);
os_mutex_lock(&sensor->lock);
c = sensor->clients;
if (c)
@ -263,7 +260,7 @@ void refresh_read_interval(sensor_obj_t sensor)
c = c->next;
}
vm_mutex_unlock(&sensor->lock);
os_mutex_unlock(&sensor->lock);
sensor->read_interval = interval;
}
@ -310,7 +307,7 @@ add_sys_sensor(char * name, char * description, int instance,
g_sys_sensors = s;
}
vm_mutex_init(&s->lock);
os_mutex_init(&s->lock);
return s;
}
@ -366,7 +363,7 @@ sensor_client_t *find_sensor_client(sys_sensor_t * sensor,
int check_sensor_timers()
{
int ms_to_next_check = -1;
uint32 now = (uint32) bh_get_tick_ms();
uint32 now = (uint32)bh_get_tick_ms();
sys_sensor_t * s = g_sys_sensors;
while (s) {
@ -412,11 +409,11 @@ void sensor_cleanup_callback(uint32 module_id)
while (s) {
sensor_client_t *c;
vm_mutex_lock(&s->lock);
os_mutex_lock(&s->lock);
if ((c = find_sensor_client(s, module_id, true)) != NULL) {
wasm_runtime_free(c);
}
vm_mutex_unlock(&s->lock);
os_mutex_unlock(&s->lock);
s = s->next;
}
}

View File

@ -3,9 +3,7 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_common.h"
#include "bh_queue.h"
#include "bh_thread.h"
#include "bh_platform.h"
#include "runtime_sensor.h"
#include "bi-inc/attr_container.h"
#include "module_wasm_app.h"
@ -91,15 +89,15 @@ static void thread_sensor_check(void * arg)
int ms_to_expiry = check_sensor_timers();
if (ms_to_expiry == -1)
ms_to_expiry = 5000;
vm_mutex_lock(&mutex);
vm_cond_reltimedwait(&cond, &mutex, ms_to_expiry);
vm_mutex_unlock(&mutex);
os_mutex_lock(&mutex);
os_cond_reltimedwait(&cond, &mutex, ms_to_expiry * 1000);
os_mutex_unlock(&mutex);
}
}
static void cb_wakeup_thread()
{
vm_cond_signal(&cond);
os_cond_signal(&cond);
}
void set_sensor_reshceduler(void (*callback)());
@ -107,8 +105,8 @@ void set_sensor_reshceduler(void (*callback)());
void init_sensor_framework()
{
// init the mutext and conditions
vm_cond_init(&cond);
vm_mutex_init(&mutex);
os_cond_init(&cond);
os_mutex_init(&mutex);
set_sensor_reshceduler(cb_wakeup_thread);
@ -123,9 +121,9 @@ void init_sensor_framework()
void start_sensor_framework()
{
korp_thread tid;
korp_tid tid;
vm_thread_create(&tid,
os_thread_create(&tid,
(void *)thread_sensor_check,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE);

View File

@ -6,8 +6,7 @@
#include "lvgl.h"
#include "app_manager_export.h"
#include "module_wasm_app.h"
#include "bh_list.h"
#include "bh_thread.h"
#include "bh_platform.h"
#include "wgl_native_utils.h"
#include "wgl.h"
@ -39,6 +38,10 @@ static korp_mutex g_object_list_mutex;
static bool lv_task_handler_thread_run = true;
static korp_mutex task_handler_lock;
static korp_cond task_handler_cond;
static void app_mgr_object_event_callback(module_data *m_data, bh_message_t msg)
{
uint32 argv[2];
@ -79,7 +82,7 @@ static void cleanup_object_list(uint32 module_id)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
os_mutex_lock(&g_object_list_mutex);
while (true) {
bool found = false;
@ -104,7 +107,7 @@ static void cleanup_object_list(uint32 module_id)
break;
}
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
}
static bool init_object_event_callback_framework()
@ -128,20 +131,20 @@ bool wgl_native_validate_object(int32 obj_id, lv_obj_t **obj)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
os_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj_id == elem->obj_id) {
if (obj != NULL)
*obj = elem->obj;
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
return true;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
return false;
}
@ -165,9 +168,9 @@ bool wgl_native_add_object(lv_obj_t *obj, uint32 module_id, uint32 *obj_id)
node->obj_id = g_obj_id_max;
node->module_id = module_id;
vm_mutex_lock(&g_object_list_mutex);
os_mutex_lock(&g_object_list_mutex);
bh_list_insert(&g_object_list, node);
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
if (obj_id != NULL)
*obj_id = node->obj_id;
@ -194,20 +197,20 @@ static void _obj_del_recursive(lv_obj_t *obj)
i = i_next;
}
vm_mutex_lock(&g_object_list_mutex);
os_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj == elem->obj) {
bh_list_remove(&g_object_list, elem);
wasm_runtime_free(elem);
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
return;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
}
static void _obj_clean_recursive(lv_obj_t *obj)
@ -255,52 +258,55 @@ static void internal_lv_obj_event_cb(lv_obj_t *obj, lv_event_t event)
{
object_node_t *elem;
vm_mutex_lock(&g_object_list_mutex);
os_mutex_lock(&g_object_list_mutex);
elem = (object_node_t *)bh_list_first_elem(&g_object_list);
while (elem) {
if (obj == elem->obj) {
post_widget_msg_to_module(elem, event);
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
return;
}
elem = (object_node_t *) bh_list_elem_next(elem);
}
vm_mutex_unlock(&g_object_list_mutex);
os_mutex_unlock(&g_object_list_mutex);
}
static void* lv_task_handler_thread_routine (void *arg)
{
korp_sem sem;
if (vm_sem_init(&sem, 1) != 0) {
printf("Init semaphore for lvgl task handler thread fail!\n");
return NULL;
}
os_mutex_lock(&task_handler_lock);
while (lv_task_handler_thread_run) {
vm_sem_reltimedwait(&sem, 100);
os_cond_reltimedwait(&task_handler_cond, &task_handler_lock,
100 * 1000);
lv_task_handler();
}
vm_sem_destroy(&sem);
os_mutex_unlock(&task_handler_lock);
return NULL;
}
void wgl_init(void)
{
korp_thread tid;
korp_tid tid;
if (os_mutex_init(&task_handler_lock) != 0)
return;
if (os_cond_init(&task_handler_cond) != 0) {
os_mutex_destroy(&task_handler_lock);
return;
}
lv_init();
bh_list_init(&g_object_list);
vm_recursive_mutex_init(&g_object_list_mutex);
os_recursive_mutex_init(&g_object_list_mutex);
init_object_event_callback_framework();
/* new a thread, call lv_task_handler periodically */
vm_thread_create(&tid,
os_thread_create(&tid,
lv_task_handler_thread_routine,
NULL,
BH_APPLET_PRESERVED_STACK_SIZE);
@ -309,6 +315,8 @@ void wgl_init(void)
void wgl_exit(void)
{
lv_task_handler_thread_run = false;
os_cond_destroy(&task_handler_cond);
os_mutex_destroy(&task_handler_lock);
}
/* -------------------------------------------------------------------------