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

@ -5,8 +5,7 @@
#include "app_manager.h"
#include "app_manager_host.h"
#include "bh_queue.h"
#include "bh_thread.h"
#include "bh_platform.h"
#include "bi-inc/attr_container.h"
#include "event.h"
#include "watchdog.h"
@ -38,7 +37,7 @@ void app_manager_post_applets_update_event()
return;
}
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
m_data = module_data_list;
while (m_data) {
@ -80,7 +79,8 @@ void app_manager_post_applets_update_event()
app_manager_printf("Post applets update event success!\n");
attr_container_dump(attr_cont);
fail: vm_mutex_unlock(&module_data_list_lock);
fail:
os_mutex_unlock(&module_data_list_lock);
attr_container_destroy(attr_cont);
}
@ -89,7 +89,7 @@ static int get_applets_count()
module_data *m_data;
int num = 0;
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
m_data = module_data_list;
while (m_data) {
@ -97,7 +97,7 @@ static int get_applets_count()
m_data = m_data->next;
}
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
return num;
}
@ -118,7 +118,7 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
return false;
}
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
m_data = module_data_list;
while (m_data) {
@ -186,7 +186,8 @@ static bool app_manager_query_applets(request_t *msg, const char *name)
app_manager_printf("Query Applets success!\n");
attr_container_dump(attr_cont);
fail: vm_mutex_unlock(&module_data_list_lock);
fail:
os_mutex_unlock(&module_data_list_lock);
attr_container_destroy(attr_cont);
return ret;
}
@ -368,9 +369,11 @@ void app_manager_startup(host_interface *interface)
/* Enter loop run */
bh_queue_enter_loop_run(g_app_mgr_queue, app_manager_queue_callback, NULL);
fail2: module_data_list_destroy();
fail2:
module_data_list_destroy();
fail1: bh_queue_destroy(g_app_mgr_queue);
fail1:
bh_queue_destroy(g_app_mgr_queue);
}
#include "module_config.h"

View File

@ -7,9 +7,6 @@
#define APP_MANAGER_H
#include "bh_platform.h"
#include "bh_common.h"
#include "bh_queue.h"
#include "bh_types.h"
#include "app_manager_export.h"
#include "native_interface.h"
#include "bi-inc/shared_utils.h"
@ -21,8 +18,8 @@ extern "C" {
#define APP_MGR_MALLOC wasm_runtime_malloc
#define APP_MGR_FREE wasm_runtime_free
/* bh_printf is defined in each platform */
#define app_manager_printf bh_printf
/* os_printf is defined in each platform */
#define app_manager_printf os_printf
#define SEND_ERR_RESPONSE(mid, err_msg) do { \
app_manager_printf("%s\n", err_msg); \

View File

@ -3,13 +3,11 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "bh_common.h"
#include "bh_types.h"
#include "bh_platform.h"
#include "app_manager_host.h"
#include "app_manager.h"
#include "app_manager_export.h"
#include "coap_ext.h"
#include "bh_thread.h"
/* host communication interface */
static host_interface host_commu;
@ -235,7 +233,7 @@ int aee_host_msg_callback(void *msg, uint16_t msg_len)
bool app_manager_host_init(host_interface *interface)
{
vm_mutex_init(&host_lock);
os_mutex_init(&host_lock);
memset(&recv_ctx, 0, sizeof(recv_ctx));
host_commu.init = interface->init;
@ -255,7 +253,7 @@ int app_manager_host_send_msg(int msg_type, const unsigned char *buf, int size)
int size_s = size, n;
char header[16];
vm_mutex_lock(&host_lock);
os_mutex_lock(&host_lock);
/* leading bytes */
bh_memcpy_s(header, 2, leadings, 2);
@ -270,13 +268,13 @@ int app_manager_host_send_msg(int msg_type, const unsigned char *buf, int size)
bh_memcpy_s(header + 4, 4, &size_s, 4);
n = host_commu.send(NULL, header, 8);
if (n != 8) {
vm_mutex_unlock(&host_lock);
os_mutex_unlock(&host_lock);
return 0;
}
/* payload */
n = host_commu.send(NULL, buf, size);
vm_mutex_unlock(&host_lock);
os_mutex_unlock(&host_lock);
printf("sent %d bytes to host\n", n);
return n;

View File

@ -269,12 +269,12 @@ check_exception()
jeff_runtime_call_java(method, argc, argv, argt);
if (is_wd_started) {
vm_mutex_lock(&wd_timer->lock);
os_mutex_lock(&wd_timer->lock);
if (!wd_timer->is_interrupting) {
wd_timer->is_stopped = true;
watchdog_timer_stop(wd_timer);
}
vm_mutex_unlock(&wd_timer->lock);
os_mutex_unlock(&wd_timer->lock);
}
return !check_exception();
@ -1395,7 +1395,7 @@ check_exception()
if (applet_data->vm_instance->main_file) {
app_manager_printf("Watchdog cancel applet main thread.\n");
vm_thread_cancel(applet_data->vm_instance->main_tlr.handle);
os_thread_cancel(applet_data->vm_instance->main_tlr.handle);
/* k_thread_abort(applet_data->vm_instance->main_tlr.handle); */
}

View File

@ -5,8 +5,7 @@
#include "app_manager.h"
#include "app_manager_host.h"
#include "bh_queue.h"
#include "bh_thread.h"
#include "bh_platform.h"
#include "bi-inc/attr_container.h"
#include "event.h"
#include "watchdog.h"
@ -21,13 +20,13 @@ module_data *module_data_list;
bool module_data_list_init()
{
module_data_list = NULL;
return !vm_mutex_init(&module_data_list_lock) ? true : false;
return !os_mutex_init(&module_data_list_lock) ? true : false;
}
void module_data_list_destroy()
{
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
if (module_data_list) {
while (module_data_list) {
module_data *p = module_data_list->next;
@ -35,14 +34,14 @@ void module_data_list_destroy()
module_data_list = p;
}
}
vm_mutex_unlock(&module_data_list_lock);
vm_mutex_destroy(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
os_mutex_destroy(&module_data_list_lock);
}
static void module_data_list_add(module_data *m_data)
{
static uint32 module_id_max = 1;
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
// reserve some special ID
// TODO: check the new id is not already occupied!
if (module_id_max == 0xFFFFFFF0)
@ -55,12 +54,12 @@ static void module_data_list_add(module_data *m_data)
m_data->next = module_data_list;
module_data_list = m_data;
}
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
}
void module_data_list_remove(module_data *m_data)
{
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
if (module_data_list) {
if (module_data_list == m_data)
module_data_list = module_data_list->next;
@ -74,46 +73,46 @@ void module_data_list_remove(module_data *m_data)
p->next = p->next->next;
}
}
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
}
module_data*
module_data_list_lookup(const char *module_name)
{
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
if (module_data_list) {
module_data *p = module_data_list;
while (p) {
/* Search by module name */
if (!strcmp(module_name, p->module_name)) {
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
return p;
}
p = p->next;
}
}
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
return NULL;
}
module_data*
module_data_list_lookup_id(unsigned int module_id)
{
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
if (module_data_list) {
module_data *p = module_data_list;
while (p) {
/* Search by module name */
if (module_id == p->id) {
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
return p;
}
p = p->next;
}
}
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
return NULL;
}
@ -201,7 +200,7 @@ void release_module(module_data *m_data)
int check_modules_timer_expiry()
{
vm_mutex_lock(&module_data_list_lock);
os_mutex_lock(&module_data_list_lock);
module_data *p = module_data_list;
int ms_to_expiry = -1;
@ -215,7 +214,7 @@ int check_modules_timer_expiry()
p = p->next;
}
vm_mutex_unlock(&module_data_list_lock);
os_mutex_unlock(&module_data_list_lock);
return ms_to_expiry;
}

View File

@ -7,9 +7,8 @@
#include "native_interface.h" /* for request_t type */
#include "app_manager_host.h"
#include "bh_queue.h"
#include "bh_platform.h"
#include "bi-inc/attr_container.h"
#include "bh_thread.h"
#include "coap_ext.h"
#include "event.h"
#include "watchdog.h"
@ -429,7 +428,6 @@ wasm_app_routine(void *arg)
module_data *m_data = (module_data *) arg;
wasm_data *wasm_app_data = (wasm_data*) m_data->internal_data;
wasm_module_inst_t inst = wasm_app_data->wasm_module_inst;
korp_tid thread = wasm_app_data->thread_id;
/* Set m_data to the VM managed instance's custom data */
wasm_runtime_set_custom_data(inst, m_data);
@ -489,8 +487,6 @@ fail2:
wasm_runtime_call_wasm(wasm_app_data->exec_env, func_onDestroy, 0, NULL);
fail1:
vm_thread_detach(thread);
vm_thread_exit(NULL);
return NULL;
}
@ -847,7 +843,7 @@ wasm_app_module_install(request_t * msg)
}
/* Create WASM app thread. */
if (vm_thread_create(&wasm_app_data->thread_id, wasm_app_routine,
if (os_thread_create(&wasm_app_data->thread_id, wasm_app_routine,
(void*) m_data, APP_THREAD_STACK_SIZE_DEFAULT) != 0) {
module_data_list_remove(m_data);
SEND_ERR_RESPONSE(msg->mid,
@ -941,7 +937,7 @@ wasm_app_module_uninstall(request_t *msg)
/* Wait for wasm app thread to exit */
wasm_app_data = (wasm_data*) m_data->internal_data;
vm_thread_join(wasm_app_data->thread_id, NULL, -1);
os_thread_join(wasm_app_data->thread_id, NULL);
cleanup_app_resource(m_data);
@ -1358,7 +1354,7 @@ wasm_app_module_on_install_request_byte_arrive(uint8 ch,
total_size = (total_size + 3) & ~((uint64)3);
if (total_size >= UINT32_MAX
|| !(section->section_body =
bh_mmap(NULL, (uint32)total_size,
os_mmap(NULL, (uint32)total_size,
map_prot, map_flags))) {
app_manager_printf("Allocate executable memory failed!\n");
goto fail;
@ -1553,7 +1549,7 @@ destroy_all_aot_sections(aot_section_list_t sections)
aot_section_t *next = cur->next;
if (cur->section_body != NULL) {
if (cur->section_type == AOT_SECTION_TYPE_TEXT)
bh_munmap(cur->section_body, cur->section_body_size);
os_munmap(cur->section_body, cur->section_body_size);
else
APP_MGR_FREE(cur->section_body);
}
@ -1583,7 +1579,7 @@ destroy_part_aot_sections(aot_section_list_t *p_sections,
if (cur->section_body != NULL) {
if (cur->section_type == AOT_SECTION_TYPE_TEXT)
bh_munmap(cur->section_body, cur->section_body_size);
os_munmap(cur->section_body, cur->section_body_size);
else
APP_MGR_FREE(cur->section_body);
}

View File

@ -4,8 +4,7 @@
*/
#include "watchdog.h"
#include "bh_queue.h"
#include "bh_thread.h"
#include "bh_platform.h"
#define WATCHDOG_THREAD_PRIORITY 5
@ -20,7 +19,7 @@ static void watchdog_timer_callback(void *timer)
watchdog_timer_stop(wd_timer);
vm_mutex_lock(&wd_timer->lock);
os_mutex_lock(&wd_timer->lock);
if (!wd_timer->is_stopped) {
@ -30,7 +29,7 @@ static void watchdog_timer_callback(void *timer)
sizeof(module_data));
}
vm_mutex_unlock(&wd_timer->lock);
os_mutex_unlock(&wd_timer->lock);
}
#endif
@ -39,12 +38,12 @@ bool watchdog_timer_init(module_data *m_data)
#ifdef WATCHDOG_ENABLED /* TODO */
watchdog_timer *wd_timer = &m_data->wd_timer;
if (0 != vm_mutex_init(&wd_timer->lock))
if (0 != os_mutex_init(&wd_timer->lock))
return false;
if (!(wd_timer->timer_handle =
app_manager_timer_create(watchdog_timer_callback, wd_timer))) {
vm_mutex_destroy(&wd_timer->lock);
os_mutex_destroy(&wd_timer->lock);
return false;
}
@ -59,20 +58,20 @@ void watchdog_timer_destroy(watchdog_timer *wd_timer)
{
#ifdef WATCHDOG_ENABLED /* TODO */
app_manager_timer_destroy(wd_timer->timer_handle);
vm_mutex_destroy(&wd_timer->lock);
os_mutex_destroy(&wd_timer->lock);
#endif
}
void watchdog_timer_start(watchdog_timer *wd_timer)
{
vm_mutex_lock(&wd_timer->lock);
os_mutex_lock(&wd_timer->lock);
wd_timer->is_interrupting = false;
wd_timer->is_stopped = false;
app_manager_timer_start(wd_timer->timer_handle,
wd_timer->module_data->timeout);
vm_mutex_unlock(&wd_timer->lock);
os_mutex_unlock(&wd_timer->lock);
}
void watchdog_timer_stop(watchdog_timer *wd_timer)