Skip to content

Commit 6e727dc

Browse files
Support more features for rt-thread (#3661)
1, enable thread mgr 2, enable libc wasi 3, enable libc wasi threads 4, specify a function name of the module to run rather main
1 parent 4dfdbbb commit 6e727dc

15 files changed

Lines changed: 1277 additions & 89 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*.so
1313
.clangd
1414
.DS_Store
15+
*.o
1516

1617
core/deps/**
1718
core/shared/mem-alloc/tlsf
@@ -37,4 +38,4 @@ tests/benchmarks/coremark/coremark*
3738
samples/workload/include/**
3839
!samples/workload/include/.gitkeep
3940

40-
# core/iwasm/libraries/wasi-threads
41+
# core/iwasm/libraries/wasi-threads

build-scripts/SConscript

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,21 @@ if GetDepend(['WAMR_BUILD_LIBC_BUILTIN']):
3131

3232
if GetDepend(['WAMR_BUILD_LIBC_WASI']):
3333
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-wasi', 'SConscript'))
34+
objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'common', 'posix', 'SConscript'))
35+
objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'common', 'libc-util', 'SConscript'))
3436

3537
if GetDepend(['WAMR_BUILD_LIB_PTHREAD']):
36-
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-pthread', 'SConscript'))
38+
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'lib-pthread', 'SConscript'))
3739

3840
if GetDepend(['WAMR_BUILD_THREAD_MGR']):
3941
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'thread-mgr', 'SConscript'))
4042

4143
if GetDepend(['WAMR_BUILD_LIBC_EMCC']):
4244
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'libc-emmc', 'SConscript'))
4345

46+
if GetDepend(['WAMR_BUILD_LIB_WASI_THREADS']):
47+
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'lib-wasi-threads', 'SConscript'))
48+
4449
objs += SConscript(os.path.join(cwd, 'SConscript_config'));
4550

4651
objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'rt-thread', 'SConscript'))

build-scripts/SConscript_config

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,27 @@ if GetDepend(['WAMR_BUILD_CUSTOM_NAME_SECTION']):
109109

110110
if GetDepend(['WAMR_BUILD_TAIL_CALL']):
111111
CPPDEFINES += ['WASM_ENABLE_TAIL_CALL=1']
112-
print('[WAMR] Tail call enabledd')
112+
print('[WAMR] Tail call enabled')
113+
114+
if GetDepend(['WAMR_BUILD_THREAD_MGR']):
115+
CPPDEFINES += ['WASM_ENABLE_THREAD_MGR=1']
116+
print('[WAMR] Thread manager enabled')
117+
118+
if GetDepend(['WAMR_BUILD_LIBC_WASI']):
119+
CPPDEFINES += ['WASM_ENABLE_LIBC_WASI=1']
120+
CPPDEFINES += ['WASM_ENABLE_MODULE_INST_CONTEXT=1']
121+
print('[WAMR] Libc wasi enabled')
122+
123+
if GetDepend(['WAMR_BUILD_LIB_WASI_THREADS']):
124+
CPPDEFINES += ['WASM_ENABLE_LIB_WASI_THREADS=1']
125+
print('[WAMR] Lib wasi threads enabled')
126+
127+
if GetDepend(['WAMR_BUILD_REF_TYPES']):
128+
CPPDEFINES += ['WASM_ENABLE_REF_TYPES=1']
129+
print('[WAMR] enable ref types')
130+
131+
CPPDEFINES += ['BH_MALLOC=wasm_runtime_malloc']
132+
CPPDEFINES += ['BH_FREE=wasm_runtime_free']
113133

114134
LIBS = ['m']
115135

core/iwasm/libraries/lib-pthread/SConscript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ from building import *
99
cwd = GetCurrentDir()
1010

1111
src = Split('''
12-
libc_pthread_wrapper.c
12+
lib_pthread_wrapper.c
1313
''')
1414

1515
CPPPATH = [cwd]
1616

1717

18-
group = DefineGroup('iwasm_libc_pthread', src, depend = [''], CPPPATH = CPPPATH)
18+
group = DefineGroup('iwasm_lib_pthread', src, depend = [''], CPPPATH = CPPPATH)
1919

2020
Return('group')
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#
2+
# Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
7+
from building import *
8+
9+
cwd = GetCurrentDir()
10+
src = Glob('*.c')
11+
CPPPATH = [cwd]
12+
13+
group = DefineGroup('iwasm_lib_wasi_threads', src, depend = [''], CPPPATH = CPPPATH)
14+
15+
Return('group')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
7+
from building import *
8+
import re
9+
10+
Import('rtconfig')
11+
12+
cwd = GetCurrentDir()
13+
src = Split('''
14+
libc_errno.c
15+
''')
16+
CPPPATH = [cwd]
17+
18+
group = DefineGroup('iwasm_libc_util', src, depend = [''], CPPPATH = CPPPATH)
19+
20+
Return('group')
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Copyright 2024 Sony Semiconductor Solutions Corporation.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
#
6+
7+
from building import *
8+
import re
9+
10+
Import('rtconfig')
11+
12+
cwd = GetCurrentDir()
13+
src = Split('''
14+
posix_file.c
15+
''')
16+
CPPPATH = [cwd]
17+
18+
group = DefineGroup('iwasm_common_posix', src, depend = [''], CPPPATH = CPPPATH)
19+
20+
Return('group')

core/shared/platform/common/posix/posix_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* (platform_internal.h)
2727
*/
2828
#if !defined(CONFIG_HAS_D_INO)
29-
#if !defined(__NuttX__)
29+
#if !defined(__NuttX__) && !defined(__RTTHREAD__)
3030
#define CONFIG_HAS_D_INO 1
3131
#define CONFIG_HAS_ISATTY 1
3232
#else

core/shared/platform/common/posix/posix_thread.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*/
55

66
#ifndef _GNU_SOURCE
7+
#if !defined(__RTTHREAD__)
78
#define _GNU_SOURCE
89
#endif
10+
#endif
911
#include "platform_api_vmcore.h"
1012
#include "platform_api_extension.h"
1113

@@ -448,7 +450,7 @@ os_thread_get_stack_boundary()
448450
addr += guard_size;
449451
}
450452
(void)stack_size;
451-
#elif defined(__APPLE__) || defined(__NuttX__)
453+
#elif defined(__APPLE__) || defined(__NuttX__) || defined(__RTTHREAD__)
452454
if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) {
453455
stack_size = pthread_get_stacksize_np(self);
454456

core/shared/platform/rt-thread/platform_internal.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,25 @@
77
#ifndef RTTHREAD_PLATFORM_INTERNAL_H
88
#define RTTHREAD_PLATFORM_INTERNAL_H
99

10+
#include <sys/ioctl.h>
11+
#include <fcntl.h>
12+
#include <limits.h>
13+
#include <errno.h>
14+
#include <poll.h>
15+
#if defined(RT_USING_PTHREADS)
16+
#include <pthread.h>
17+
#else
1018
#include <rtthread.h>
19+
#endif
1120
#include <stdbool.h>
1221
#include <string.h>
1322
#include <stdio.h>
1423
#include <stdlib.h>
1524
#include <math.h>
1625
#include <stdint.h>
1726
#include <ctype.h>
27+
#include <dirent.h>
28+
#include <assert.h>
1829

1930
#if defined(WASM_ENABLE_AOT)
2031
#if defined(RTT_WAMR_BUILD_TARGET_THUMB)
@@ -32,12 +43,67 @@
3243
#endif
3344
#endif /* WASM_ENABLE_AOT */
3445

46+
/* Use rt-thread's definition as default */
47+
#if 0 // defined(RT_USING_PTHREADS)
48+
typedef pthread_t korp_tid;
49+
typedef pthread_mutex_t korp_mutex;
50+
typedef pthread_cond_t korp_cond;
51+
typedef pthread_t korp_thread;
52+
#else
3553
typedef rt_thread_t korp_tid;
3654
typedef struct rt_mutex korp_mutex;
3755
typedef struct rt_thread korp_cond;
3856
typedef struct rt_thread korp_thread;
57+
#endif
3958
typedef unsigned int korp_sem;
4059

60+
#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
61+
typedef uint32_t socklen_t;
62+
#endif
63+
64+
#if !defined(SOL_SOCKET)
65+
#define SOL_SOCKET 1
66+
#endif
67+
68+
#if !defined(SO_TYPE)
69+
#define SO_TYPE 3
70+
#endif
71+
72+
#if !defined(SOCK_DGRAM)
73+
#define SOCK_DGRAM 2
74+
#endif
75+
76+
#if !defined(SOCK_STREAM)
77+
#define SOCK_STREAM 1
78+
#endif
79+
80+
#if !defined(UTIME_NOW)
81+
#define UTIME_NOW -2L
82+
#endif
83+
84+
#if !defined(UTIME_OMIT)
85+
#define UTIME_OMIT -1L
86+
#endif
87+
88+
#if !defined(AT_SYMLINK_NOFOLLOW)
89+
#define AT_SYMLINK_NOFOLLOW 2
90+
#endif
91+
92+
#if !defined(AT_SYMLINK_FOLLOW)
93+
#define AT_SYMLINK_FOLLOW 4
94+
#endif
95+
96+
#if !defined(AT_REMOVEDIR)
97+
#define AT_REMOVEDIR 8
98+
#endif
99+
100+
#define DT_BLK 0x06
101+
#define DT_CHR 0x02
102+
#define DT_LNK 0x0A
103+
104+
#define PTHREAD_STACK_MIN 1024
105+
#define BH_THREAD_DEFAULT_PRIORITY 30
106+
41107
/* korp_rwlock is used in platform_api_extension.h,
42108
we just define the type to make the compiler happy */
43109
typedef struct {

0 commit comments

Comments
 (0)