WebAssembly Micro Runtime first version
This commit is contained in:
48
core/iwasm/lib/native/base/base-lib-export.c
Normal file
48
core/iwasm/lib/native/base/base-lib-export.c
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "lib-export.h"
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
#include "base-lib-export.h"
|
||||
#endif
|
||||
|
||||
static NativeSymbol extended_native_symbol_defs[] = {
|
||||
/* TODO: use macro EXPORT_WASM_API() or EXPORT_WASM_API2() to
|
||||
add functions to register. */
|
||||
|
||||
#ifdef WASM_ENABLE_BASE_LIB
|
||||
EXPORT_WASM_API(wasm_register_resource),
|
||||
EXPORT_WASM_API(wasm_response_send),
|
||||
EXPORT_WASM_API(wasm_post_request),
|
||||
EXPORT_WASM_API(wasm_sub_event),
|
||||
EXPORT_WASM_API(wasm_create_timer),
|
||||
EXPORT_WASM_API(wasm_timer_destory),
|
||||
EXPORT_WASM_API(wasm_timer_cancel),
|
||||
EXPORT_WASM_API(wasm_timer_restart),
|
||||
EXPORT_WASM_API(wasm_get_sys_tick_ms),
|
||||
#endif
|
||||
};
|
||||
|
||||
int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
|
||||
{
|
||||
*p_base_lib_apis = extended_native_symbol_defs;
|
||||
return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol);
|
||||
}
|
||||
|
||||
24
core/iwasm/lib/native/base/base-lib-export.h
Normal file
24
core/iwasm/lib/native/base/base-lib-export.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef _BASE_LIB_EXPORT_H_
|
||||
#define _BASE_LIB_EXPORT_H_
|
||||
|
||||
#include "attr-container.h"
|
||||
#include "native_interface.h"
|
||||
|
||||
#endif /* end of _BASE_LIB_EXPORT_H_ */
|
||||
|
||||
111
core/iwasm/lib/native/base/request_response.c
Normal file
111
core/iwasm/lib/native/base/request_response.c
Normal file
@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "native_interface.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "coap_ext.h"
|
||||
#include "wasm-export.h"
|
||||
|
||||
extern void module_request_handler(request_t *request, uint32 register_id);
|
||||
|
||||
bool wasm_response_send(int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return false;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
response_t response[1];
|
||||
|
||||
if (NULL == unpack_response(buffer, size, response))
|
||||
return false;
|
||||
|
||||
am_send_response(response);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void wasm_register_resource(int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_addr(url_offset, 1))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
am_register_resource(url, module_request_handler, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
void wasm_post_request(int32 buffer_offset, int size)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *buffer = NULL;
|
||||
|
||||
if (!validate_app_addr(buffer_offset, size))
|
||||
return;
|
||||
|
||||
buffer = addr_app_to_native(buffer_offset);
|
||||
|
||||
if (buffer != NULL) {
|
||||
request_t req[1];
|
||||
|
||||
if (!unpack_request(buffer, size, req))
|
||||
return;
|
||||
|
||||
// TODO: add permission check, ensure app can't do harm
|
||||
|
||||
// set sender to help dispatch the response to the sender ap
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
req->sender = mod_id;
|
||||
|
||||
if (req->action == COAP_EVENT) {
|
||||
am_publish_event(req);
|
||||
return;
|
||||
}
|
||||
|
||||
am_dispatch_request(req);
|
||||
}
|
||||
}
|
||||
|
||||
void wasm_sub_event(int32 url_offset)
|
||||
{
|
||||
wasm_module_inst_t module_inst = get_module_inst();
|
||||
char *url = NULL;
|
||||
|
||||
if (!validate_app_addr(url_offset, 1))
|
||||
return;
|
||||
|
||||
url = addr_app_to_native(url_offset);
|
||||
|
||||
if (url != NULL) {
|
||||
unsigned int mod_id = app_manager_get_module_id(Module_WASM_App);
|
||||
|
||||
am_register_event(url, mod_id);
|
||||
}
|
||||
}
|
||||
|
||||
29
core/iwasm/lib/native/base/runtime_lib.h
Normal file
29
core/iwasm/lib/native/base/runtime_lib.h
Normal file
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef LIB_BASE_RUNTIME_LIB_H_
|
||||
#define LIB_BASE_RUNTIME_LIB_H_
|
||||
|
||||
#include "native_interface.h"
|
||||
|
||||
#include "runtime_timer.h"
|
||||
|
||||
void init_wasm_timer();
|
||||
timer_ctx_t get_wasm_timer_ctx();
|
||||
timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num);
|
||||
void destory_module_timer_ctx(unsigned int module_id);
|
||||
|
||||
#endif /* LIB_BASE_RUNTIME_LIB_H_ */
|
||||
178
core/iwasm/lib/native/base/timer_wrapper.c
Normal file
178
core/iwasm/lib/native/base/timer_wrapper.c
Normal file
@ -0,0 +1,178 @@
|
||||
/*
|
||||
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "runtime_timer.h"
|
||||
#include "app-manager-export.h"
|
||||
#include "module_wasm_app.h"
|
||||
#include "bh_list.h"
|
||||
#include "bh_thread.h"
|
||||
#include "bh_time.h"
|
||||
|
||||
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)
|
||||
{
|
||||
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 *) 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)
|
||||
{
|
||||
|
||||
int ms_to_expiry;
|
||||
while (1) {
|
||||
ms_to_expiry = -1;
|
||||
vm_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) {
|
||||
int next = check_app_timers(elem->timer_ctx);
|
||||
if (next != -1) {
|
||||
if (ms_to_expiry == -1 || ms_to_expiry > next)
|
||||
ms_to_expiry = next;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_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);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void init_wasm_timer()
|
||||
{
|
||||
korp_tid tm_tid;
|
||||
bh_list_init(&g_timer_ctx_list);
|
||||
|
||||
vm_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);
|
||||
|
||||
vm_thread_create(&tm_tid, thread_modulers_timer_check,
|
||||
NULL,
|
||||
BH_APPLET_PRESERVED_STACK_SIZE);
|
||||
}
|
||||
|
||||
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*) bh_malloc(
|
||||
sizeof(timer_ctx_node_t));
|
||||
if (node == NULL) {
|
||||
destroy_timer_ctx(ctx);
|
||||
return NULL;
|
||||
}
|
||||
memset(node, 0, sizeof(*node));
|
||||
node->timer_ctx = ctx;
|
||||
|
||||
vm_mutex_lock(&g_timer_ctx_list_mutex);
|
||||
bh_list_insert(&g_timer_ctx_list, node);
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void destory_module_timer_ctx(unsigned int module_id)
|
||||
{
|
||||
vm_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) {
|
||||
if (timer_ctx_get_owner(elem->timer_ctx) == module_id) {
|
||||
bh_list_remove(&g_timer_ctx_list, elem);
|
||||
destroy_timer_ctx(elem->timer_ctx);
|
||||
bh_free(elem);
|
||||
break;
|
||||
}
|
||||
|
||||
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
|
||||
}
|
||||
vm_mutex_unlock(&g_timer_ctx_list_mutex);
|
||||
}
|
||||
|
||||
timer_ctx_t get_wasm_timer_ctx()
|
||||
{
|
||||
module_data * m = app_manager_get_module_data(Module_WASM_App);
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
return m->timer_ctx;
|
||||
}
|
||||
|
||||
timer_id_t wasm_create_timer(int interval, bool is_period, bool auto_start)
|
||||
{
|
||||
return sys_create_timer(get_wasm_timer_ctx(), interval, is_period,
|
||||
auto_start);
|
||||
}
|
||||
|
||||
void wasm_timer_destory(timer_id_t timer_id)
|
||||
{
|
||||
sys_timer_destory(get_wasm_timer_ctx(), timer_id);
|
||||
}
|
||||
|
||||
void wasm_timer_cancel(timer_id_t timer_id)
|
||||
{
|
||||
sys_timer_cancel(get_wasm_timer_ctx(), timer_id);
|
||||
}
|
||||
|
||||
void wasm_timer_restart(timer_id_t timer_id, int interval)
|
||||
{
|
||||
sys_timer_restart(get_wasm_timer_ctx(), timer_id, interval);
|
||||
}
|
||||
|
||||
extern uint32 get_sys_tick_ms();
|
||||
|
||||
uint32 wasm_get_sys_tick_ms(void)
|
||||
{
|
||||
return (uint32) bh_get_tick_ms();
|
||||
}
|
||||
|
||||
23
core/iwasm/lib/native/base/wasm_lib_base.cmake
Normal file
23
core/iwasm/lib/native/base/wasm_lib_base.cmake
Normal file
@ -0,0 +1,23 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
set (WASM_LIB_BASE_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${WASM_LIB_BASE_DIR})
|
||||
|
||||
|
||||
file (GLOB_RECURSE source_all ${WASM_LIB_BASE_DIR}/*.c)
|
||||
|
||||
set (WASM_LIB_BASE_SOURCE ${source_all})
|
||||
|
||||
Reference in New Issue
Block a user