Skip to content

Commit f1a0e75

Browse files
re-org platform APIs, simplify porting process (#201)
Co-authored-by: Xu Jun <[email protected]>
1 parent ef5ceff commit f1a0e75

File tree

177 files changed

+2943
-7893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+2943
-7893
lines changed

build-scripts/config_common.cmake

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,6 @@
33

44
string(TOUPPER ${WAMR_BUILD_TARGET} WAMR_BUILD_TARGET)
55

6-
# Add definitions for the build platform
7-
if (WAMR_BUILD_PLATFORM STREQUAL "linux")
8-
add_definitions(-DBH_PLATFORM_LINUX)
9-
elseif (WAMR_BUILD_PLATFORM STREQUAL "linux-sgx")
10-
add_definitions(-DBH_PLATFORM_LINUX_SGX)
11-
elseif (WAMR_BUILD_PLATFORM STREQUAL "zephyr")
12-
add_definitions(-DBH_PLATFORM_ZEPHYR)
13-
elseif (WAMR_BUILD_PLATFORM STREQUAL "vxworks")
14-
add_definitions(-DBH_PLATFORM_VXWORKS)
15-
elseif (WAMR_BUILD_PLATFORM STREQUAL "darwin")
16-
add_definitions(-DBH_PLATFORM_DARWIN)
17-
elseif (WAMR_BUILD_PLATFORM STREQUAL "alios-things")
18-
add_definitions(-DBH_PLATFORM_ALIOS_THINGS)
19-
elseif (WAMR_BUILD_PLATFORM STREQUAL "android")
20-
add_definitions(-DBH_PLATFORM_ANDROID)
21-
else ()
22-
message (WARNING "-- WAMR build platform isn't set")
23-
endif ()
246

257
# Add definitions for the build target
268
if (WAMR_BUILD_TARGET STREQUAL "X86_64")
@@ -53,6 +35,10 @@ else ()
5335
message (FATAL_ERROR "-- WAMR build target isn't set")
5436
endif ()
5537

38+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
39+
add_definitions(-DBH_DEBUG=1)
40+
endif ()
41+
5642
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
5743
if (WAMR_BUILD_TARGET STREQUAL "X86_64" OR WAMR_BUILD_TARGET STREQUAL "AMD_64")
5844
# Add -fPIC flag if build as 64-bit

core/app-framework/app-native-shared/attr_container.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,7 @@ typedef union jvalue {
1616
double d;
1717
} jvalue;
1818

19-
#ifndef bh_memcpy_s
20-
int b_memcpy_s(void * s1, unsigned int s1max,
21-
const void * s2, unsigned int n);
22-
#define bh_memcpy_s(dest, dlen, src, slen) do { \
23-
int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \
24-
(void)_ret; \
25-
} while (0)
26-
#endif
19+
2720

2821
static inline int16_t get_int16(const char *buf)
2922
{

core/app-framework/app-native-shared/native_interface.h

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,6 @@
99
/* Note: the bh_plaform.h is the only head file separately
1010
implemented by both [app] and [native] worlds */
1111
#include "bh_platform.h"
12-
#include "wasm_export.h"
13-
14-
#define get_module_inst(exec_env) \
15-
wasm_runtime_get_module_inst(exec_env)
16-
17-
#define validate_app_addr(offset, size) \
18-
wasm_runtime_validate_app_addr(module_inst, offset, size)
19-
20-
#define validate_app_str_addr(offset) \
21-
wasm_runtime_validate_app_str_addr(module_inst, offset)
22-
23-
#define addr_app_to_native(offset) \
24-
wasm_runtime_addr_app_to_native(module_inst, offset)
25-
26-
#define addr_native_to_app(ptr) \
27-
wasm_runtime_addr_native_to_app(module_inst, ptr)
28-
29-
#define module_malloc(size, p_native_addr) \
30-
wasm_runtime_module_malloc(module_inst, size, p_native_addr)
31-
32-
#define module_free(offset) \
33-
wasm_runtime_module_free(module_inst, offset)
34-
35-
/*char *wa_strdup(const char *);*/
3612

3713

3814
#endif /* end of _NATIVE_INTERFACE_H */

core/app-framework/base/app/bh_platform.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,3 @@ char *wa_strdup(const char *s)
7979
memcpy(s1, s, strlen(s) + 1);
8080
return s1;
8181
}
82-
83-
#define RSIZE_MAX 0x7FFFFFFF
84-
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
85-
{
86-
char *dest = (char*) s1;
87-
char *src = (char*) s2;
88-
if (n == 0) {
89-
return 0;
90-
}
91-
92-
if (s1 == NULL || s1max > RSIZE_MAX) {
93-
return -1;
94-
}
95-
if (s2 == NULL || n > s1max) {
96-
memset(dest, 0, s1max);
97-
return -1;
98-
}
99-
memcpy(dest, src, n);
100-
return 0;
101-
}

core/app-framework/base/app/bh_platform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ typedef int int32;
3535
#define WA_FREE free
3636
#endif
3737

38-
char *wa_strdup(const char *s);
3938

4039
uint32 htonl(uint32 value);
4140
uint32 ntohl(uint32 value);
4241
uint16 htons(uint16 value);
4342
uint16 ntohs(uint16 value);
4443

45-
int
46-
b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
44+
45+
// We are not worried for the WASM world since the sandbox will catch it.
46+
#define bh_memcpy_s(dst, dst_len, src, src_len) memcpy(dst, src, src_len)
4747

4848
#endif /* DEPS_IWASM_APP_LIBS_BASE_BH_PLATFORM_H_ */

core/app-framework/base/native/timer_wrapper.c

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
*/
55

6-
#include "runtime_timer.h"
6+
#include "bh_platform.h"
77
#include "app_manager_export.h"
88
#include "module_wasm_app.h"
9-
#include "bh_list.h"
10-
#include "bh_thread.h"
11-
#include "bh_time.h"
129
#include "timer_native_api.h"
1310

1411
static bool timer_thread_run = true;
@@ -46,7 +43,7 @@ void * thread_modulers_timer_check(void * arg)
4643

4744
while (timer_thread_run) {
4845
ms_to_expiry = -1;
49-
vm_mutex_lock(&g_timer_ctx_list_mutex);
46+
os_mutex_lock(&g_timer_ctx_list_mutex);
5047
timer_ctx_node_t* elem = (timer_ctx_node_t*)
5148
bh_list_first_elem(&g_timer_ctx_list);
5249
while (elem) {
@@ -58,36 +55,36 @@ void * thread_modulers_timer_check(void * arg)
5855

5956
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
6057
}
61-
vm_mutex_unlock(&g_timer_ctx_list_mutex);
58+
os_mutex_unlock(&g_timer_ctx_list_mutex);
6259

6360
if (ms_to_expiry == -1)
6461
ms_to_expiry = 60 * 1000;
65-
vm_mutex_lock(&g_timer_ctx_list_mutex);
66-
vm_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
67-
ms_to_expiry);
68-
vm_mutex_unlock(&g_timer_ctx_list_mutex);
62+
os_mutex_lock(&g_timer_ctx_list_mutex);
63+
os_cond_reltimedwait(&g_timer_ctx_list_cond, &g_timer_ctx_list_mutex,
64+
ms_to_expiry * 1000);
65+
os_mutex_unlock(&g_timer_ctx_list_mutex);
6966
}
7067

7168
return NULL;
7269
}
7370

7471
void wakeup_modules_timer_thread(timer_ctx_t ctx)
7572
{
76-
vm_mutex_lock(&g_timer_ctx_list_mutex);
77-
vm_cond_signal(&g_timer_ctx_list_cond);
78-
vm_mutex_unlock(&g_timer_ctx_list_mutex);
73+
os_mutex_lock(&g_timer_ctx_list_mutex);
74+
os_cond_signal(&g_timer_ctx_list_cond);
75+
os_mutex_unlock(&g_timer_ctx_list_mutex);
7976
}
8077

8178
void init_wasm_timer()
8279
{
8380
korp_tid tm_tid;
8481
bh_list_init(&g_timer_ctx_list);
8582

86-
vm_cond_init(&g_timer_ctx_list_cond);
83+
os_cond_init(&g_timer_ctx_list_cond);
8784
/* temp solution for: thread_modulers_timer_check thread would recursive lock the mutex */
88-
vm_recursive_mutex_init(&g_timer_ctx_list_mutex);
85+
os_recursive_mutex_init(&g_timer_ctx_list_mutex);
8986

90-
vm_thread_create(&tm_tid, thread_modulers_timer_check,
87+
os_thread_create(&tm_tid, thread_modulers_timer_check,
9188
NULL, BH_APPLET_PRESERVED_STACK_SIZE);
9289
}
9390

@@ -115,16 +112,16 @@ timer_ctx_t create_wasm_timer_ctx(unsigned int module_id, int prealloc_num)
115112
memset(node, 0, sizeof(*node));
116113
node->timer_ctx = ctx;
117114

118-
vm_mutex_lock(&g_timer_ctx_list_mutex);
115+
os_mutex_lock(&g_timer_ctx_list_mutex);
119116
bh_list_insert(&g_timer_ctx_list, node);
120-
vm_mutex_unlock(&g_timer_ctx_list_mutex);
117+
os_mutex_unlock(&g_timer_ctx_list_mutex);
121118

122119
return ctx;
123120
}
124121

125122
void destroy_module_timer_ctx(unsigned int module_id)
126123
{
127-
vm_mutex_lock(&g_timer_ctx_list_mutex);
124+
os_mutex_lock(&g_timer_ctx_list_mutex);
128125
timer_ctx_node_t* elem = (timer_ctx_node_t*)
129126
bh_list_first_elem(&g_timer_ctx_list);
130127
while (elem) {
@@ -137,7 +134,7 @@ void destroy_module_timer_ctx(unsigned int module_id)
137134

138135
elem = (timer_ctx_node_t*) bh_list_elem_next(elem);
139136
}
140-
vm_mutex_unlock(&g_timer_ctx_list_mutex);
137+
os_mutex_unlock(&g_timer_ctx_list_mutex);
141138
}
142139

143140
timer_ctx_t get_wasm_timer_ctx(wasm_module_inst_t module_inst)
@@ -192,6 +189,6 @@ extern uint32 get_sys_tick_ms();
192189
uint32
193190
wasm_get_sys_tick_ms(wasm_exec_env_t exec_env)
194191
{
195-
return (uint32) bh_get_tick_ms();
192+
return (uint32)bh_get_tick_ms();
196193
}
197194

core/app-framework/connection/native/linux/connection_mgr.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111
*/
1212

1313
#include "connection_lib.h"
14-
#include "bh_thread.h"
14+
#include "bh_platform.h"
1515
#include "app_manager_export.h"
1616
#include "module_wasm_app.h"
1717
#include "conn_tcp.h"
1818
#include "conn_udp.h"
1919
#include "conn_uart.h"
20-
#include "bh_common.h"
21-
#include "bh_assert.h"
2220

2321
#include <unistd.h>
2422
#include <sys/epoll.h>
@@ -96,7 +94,7 @@ connection_interface_t connection_impl = {
9694

9795
static void add_connection(sys_connection_t *conn)
9896
{
99-
vm_mutex_lock(&g_lock);
97+
os_mutex_lock(&g_lock);
10098

10199
g_handle_max++;
102100
if (g_handle_max == -1)
@@ -110,7 +108,7 @@ static void add_connection(sys_connection_t *conn)
110108
g_connections = conn;
111109
}
112110

113-
vm_mutex_unlock(&g_lock);
111+
os_mutex_unlock(&g_lock);
114112
}
115113

116114
#define FREE_CONNECTION(conn) do { \
@@ -124,7 +122,7 @@ static int get_app_conns_num(uint32 module_id)
124122
sys_connection_t *conn;
125123
int num = 0;
126124

127-
vm_mutex_lock(&g_lock);
125+
os_mutex_lock(&g_lock);
128126

129127
conn = g_connections;
130128
while (conn) {
@@ -133,7 +131,7 @@ static int get_app_conns_num(uint32 module_id)
133131
conn = conn->next;
134132
}
135133

136-
vm_mutex_unlock(&g_lock);
134+
os_mutex_unlock(&g_lock);
137135

138136
return num;
139137
}
@@ -142,7 +140,7 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found)
142140
{
143141
sys_connection_t *conn, *prev = NULL;
144142

145-
vm_mutex_lock(&g_lock);
143+
os_mutex_lock(&g_lock);
146144

147145
conn = g_connections;
148146
while (conn) {
@@ -154,15 +152,15 @@ static sys_connection_t *find_connection(uint32 handle, bool remove_found)
154152
g_connections = conn->next;
155153
}
156154
}
157-
vm_mutex_unlock(&g_lock);
155+
os_mutex_unlock(&g_lock);
158156
return conn;
159157
} else {
160158
prev = conn;
161159
conn = conn->next;
162160
}
163161
}
164162

165-
vm_mutex_unlock(&g_lock);
163+
os_mutex_unlock(&g_lock);
166164

167165
return NULL;
168166
}
@@ -171,7 +169,7 @@ static void cleanup_connections(uint32 module_id)
171169
{
172170
sys_connection_t *conn, *prev = NULL;
173171

174-
vm_mutex_lock(&g_lock);
172+
os_mutex_lock(&g_lock);
175173

176174
conn = g_connections;
177175
while (conn) {
@@ -194,7 +192,7 @@ static void cleanup_connections(uint32 module_id)
194192
}
195193
}
196194

197-
vm_mutex_unlock(&g_lock);
195+
os_mutex_unlock(&g_lock);
198196
}
199197

200198
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)
546544

547545
bool init_connection_framework()
548546
{
549-
korp_thread tid;
547+
korp_tid tid;
550548

551549
epollfd = epoll_create(MAX_EVENTS);
552550
if (epollfd == -1)
553551
return false;
554552

555-
if (vm_mutex_init(&g_lock) != 0) {
553+
if (os_mutex_init(&g_lock) != 0) {
556554
close(epollfd);
557555
return false;
558556
}
@@ -566,7 +564,7 @@ bool init_connection_framework()
566564
goto fail;
567565
}
568566

569-
if (vm_thread_create(&tid,
567+
if (os_thread_create(&tid,
570568
polling_thread_routine,
571569
NULL,
572570
BH_APPLET_PRESERVED_STACK_SIZE) != 0) {
@@ -576,7 +574,7 @@ bool init_connection_framework()
576574
return true;
577575

578576
fail:
579-
vm_mutex_destroy(&g_lock);
577+
os_mutex_destroy(&g_lock);
580578
close(epollfd);
581579
return false;
582580
}

0 commit comments

Comments
 (0)