Skip to content

Commit

Permalink
Support more features for rt-thread (#3661)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dongsheng28849455 authored Jul 26, 2024
1 parent 4dfdbbb commit 6e727dc
Show file tree
Hide file tree
Showing 15 changed files with 1,277 additions and 89 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*.so
.clangd
.DS_Store
*.o

core/deps/**
core/shared/mem-alloc/tlsf
Expand All @@ -37,4 +38,4 @@ tests/benchmarks/coremark/coremark*
samples/workload/include/**
!samples/workload/include/.gitkeep

# core/iwasm/libraries/wasi-threads
# core/iwasm/libraries/wasi-threads
7 changes: 6 additions & 1 deletion build-scripts/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ if GetDepend(['WAMR_BUILD_LIBC_BUILTIN']):

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

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

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

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

if GetDepend(['WAMR_BUILD_LIB_WASI_THREADS']):
objs += SConscript(os.path.join(IWASM_DIR, 'libraries', 'lib-wasi-threads', 'SConscript'))

objs += SConscript(os.path.join(cwd, 'SConscript_config'));

objs += SConscript(os.path.join(SHARED_DIR, 'platform', 'rt-thread', 'SConscript'))
Expand Down
22 changes: 21 additions & 1 deletion build-scripts/SConscript_config
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,27 @@ if GetDepend(['WAMR_BUILD_CUSTOM_NAME_SECTION']):

if GetDepend(['WAMR_BUILD_TAIL_CALL']):
CPPDEFINES += ['WASM_ENABLE_TAIL_CALL=1']
print('[WAMR] Tail call enabledd')
print('[WAMR] Tail call enabled')

if GetDepend(['WAMR_BUILD_THREAD_MGR']):
CPPDEFINES += ['WASM_ENABLE_THREAD_MGR=1']
print('[WAMR] Thread manager enabled')

if GetDepend(['WAMR_BUILD_LIBC_WASI']):
CPPDEFINES += ['WASM_ENABLE_LIBC_WASI=1']
CPPDEFINES += ['WASM_ENABLE_MODULE_INST_CONTEXT=1']
print('[WAMR] Libc wasi enabled')

if GetDepend(['WAMR_BUILD_LIB_WASI_THREADS']):
CPPDEFINES += ['WASM_ENABLE_LIB_WASI_THREADS=1']
print('[WAMR] Lib wasi threads enabled')

if GetDepend(['WAMR_BUILD_REF_TYPES']):
CPPDEFINES += ['WASM_ENABLE_REF_TYPES=1']
print('[WAMR] enable ref types')

CPPDEFINES += ['BH_MALLOC=wasm_runtime_malloc']
CPPDEFINES += ['BH_FREE=wasm_runtime_free']

LIBS = ['m']

Expand Down
4 changes: 2 additions & 2 deletions core/iwasm/libraries/lib-pthread/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ from building import *
cwd = GetCurrentDir()

src = Split('''
libc_pthread_wrapper.c
lib_pthread_wrapper.c
''')

CPPPATH = [cwd]


group = DefineGroup('iwasm_libc_pthread', src, depend = [''], CPPPATH = CPPPATH)
group = DefineGroup('iwasm_lib_pthread', src, depend = [''], CPPPATH = CPPPATH)

Return('group')
15 changes: 15 additions & 0 deletions core/iwasm/libraries/lib-wasi-threads/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright 2024 Sony Semiconductor Solutions Corporation.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

from building import *

cwd = GetCurrentDir()
src = Glob('*.c')
CPPPATH = [cwd]

group = DefineGroup('iwasm_lib_wasi_threads', src, depend = [''], CPPPATH = CPPPATH)

Return('group')
20 changes: 20 additions & 0 deletions core/shared/platform/common/libc-util/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2024 Sony Semiconductor Solutions Corporation.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

from building import *
import re

Import('rtconfig')

cwd = GetCurrentDir()
src = Split('''
libc_errno.c
''')
CPPPATH = [cwd]

group = DefineGroup('iwasm_libc_util', src, depend = [''], CPPPATH = CPPPATH)

Return('group')
20 changes: 20 additions & 0 deletions core/shared/platform/common/posix/SConscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2024 Sony Semiconductor Solutions Corporation.
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#

from building import *
import re

Import('rtconfig')

cwd = GetCurrentDir()
src = Split('''
posix_file.c
''')
CPPPATH = [cwd]

group = DefineGroup('iwasm_common_posix', src, depend = [''], CPPPATH = CPPPATH)

Return('group')
2 changes: 1 addition & 1 deletion core/shared/platform/common/posix/posix_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* (platform_internal.h)
*/
#if !defined(CONFIG_HAS_D_INO)
#if !defined(__NuttX__)
#if !defined(__NuttX__) && !defined(__RTTHREAD__)
#define CONFIG_HAS_D_INO 1
#define CONFIG_HAS_ISATTY 1
#else
Expand Down
4 changes: 3 additions & 1 deletion core/shared/platform/common/posix/posix_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
*/

#ifndef _GNU_SOURCE
#if !defined(__RTTHREAD__)
#define _GNU_SOURCE
#endif
#endif
#include "platform_api_vmcore.h"
#include "platform_api_extension.h"

Expand Down Expand Up @@ -448,7 +450,7 @@ os_thread_get_stack_boundary()
addr += guard_size;
}
(void)stack_size;
#elif defined(__APPLE__) || defined(__NuttX__)
#elif defined(__APPLE__) || defined(__NuttX__) || defined(__RTTHREAD__)
if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) {
stack_size = pthread_get_stacksize_np(self);

Expand Down
66 changes: 66 additions & 0 deletions core/shared/platform/rt-thread/platform_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@
#ifndef RTTHREAD_PLATFORM_INTERNAL_H
#define RTTHREAD_PLATFORM_INTERNAL_H

#include <sys/ioctl.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#include <poll.h>
#if defined(RT_USING_PTHREADS)
#include <pthread.h>
#else
#include <rtthread.h>
#endif
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>
#include <ctype.h>
#include <dirent.h>
#include <assert.h>

#if defined(WASM_ENABLE_AOT)
#if defined(RTT_WAMR_BUILD_TARGET_THUMB)
Expand All @@ -32,12 +43,67 @@
#endif
#endif /* WASM_ENABLE_AOT */

/* Use rt-thread's definition as default */
#if 0 // defined(RT_USING_PTHREADS)
typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
#else
typedef rt_thread_t korp_tid;
typedef struct rt_mutex korp_mutex;
typedef struct rt_thread korp_cond;
typedef struct rt_thread korp_thread;
#endif
typedef unsigned int korp_sem;

#if !defined(socklen_t) && !defined(SOCKLEN_T_DEFINED)
typedef uint32_t socklen_t;
#endif

#if !defined(SOL_SOCKET)
#define SOL_SOCKET 1
#endif

#if !defined(SO_TYPE)
#define SO_TYPE 3
#endif

#if !defined(SOCK_DGRAM)
#define SOCK_DGRAM 2
#endif

#if !defined(SOCK_STREAM)
#define SOCK_STREAM 1
#endif

#if !defined(UTIME_NOW)
#define UTIME_NOW -2L
#endif

#if !defined(UTIME_OMIT)
#define UTIME_OMIT -1L
#endif

#if !defined(AT_SYMLINK_NOFOLLOW)
#define AT_SYMLINK_NOFOLLOW 2
#endif

#if !defined(AT_SYMLINK_FOLLOW)
#define AT_SYMLINK_FOLLOW 4
#endif

#if !defined(AT_REMOVEDIR)
#define AT_REMOVEDIR 8
#endif

#define DT_BLK 0x06
#define DT_CHR 0x02
#define DT_LNK 0x0A

#define PTHREAD_STACK_MIN 1024
#define BH_THREAD_DEFAULT_PRIORITY 30

/* korp_rwlock is used in platform_api_extension.h,
we just define the type to make the compiler happy */
typedef struct {
Expand Down
Loading

0 comments on commit 6e727dc

Please sign in to comment.