Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasi-threads: new proposed ABI of threads-spawn #1892

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions core/iwasm/libraries/lib-wasi-threads/lib_wasi_threads_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
#include "aot_runtime.h"
#endif

typedef struct __wasi_thread_spawn_result_t {
uint8 is_error;
union {
uint8 error;
uint32 tid;
} u;
} __wasi_thread_spawn_result_t;

bh_static_assert(sizeof(__wasi_thread_spawn_result_t) == 8);
bh_static_assert(offsetof(__wasi_thread_spawn_result_t, u.error) == 4);
bh_static_assert(offsetof(__wasi_thread_spawn_result_t, u.tid) == 4);

#define WASI_THREADS_EAGAIN 0

static const char *THREAD_START_FUNCTION = "wasi_thread_start";
static korp_mutex thread_id_lock;
static TidAllocator tid_allocator;
Expand Down Expand Up @@ -71,8 +85,8 @@ thread_start(void *arg)
return NULL;
}

static int32
thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
static void
thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg, void *retaddr)
{
wasm_module_t module = wasm_exec_env_get_module(exec_env);
wasm_module_inst_t module_inst = get_module_inst(exec_env);
Expand All @@ -85,15 +99,18 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
#if WASM_ENABLE_LIBC_WASI != 0
WASIContext *wasi_ctx;
#endif
__wasi_thread_spawn_result_t result;
memset(&result, 0, sizeof(result));

bh_assert(module);
bh_assert(module_inst);

stack_size = ((WASMModuleInstance *)module_inst)->default_wasm_stack_size;

if (!(new_module_inst = wasm_runtime_instantiate_internal(
module, true, stack_size, 0, NULL, 0)))
return -1;
module, true, stack_size, 0, NULL, 0))) {
goto fail;
}

wasm_runtime_set_custom_data_internal(
new_module_inst, wasm_runtime_get_custom_data(module_inst));
Expand Down Expand Up @@ -134,7 +151,9 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
}
os_mutex_unlock(&exec_env->wait_lock);

return thread_id;
result.is_error = 0;
result.u.tid = thread_id;
goto copyout;

thread_spawn_fail:
os_mutex_unlock(&exec_env->wait_lock);
Expand All @@ -146,16 +165,28 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
if (thread_start_arg)
wasm_runtime_free(thread_start_arg);

return -1;
fail:
result.is_error = 1;
result.u.error = WASI_THREADS_EAGAIN;

copyout:
if (!wasm_runtime_validate_native_addr(module_inst, retaddr,
sizeof(result))) {
wasm_runtime_set_exception(module_inst, "out of bounds memory access");
}
else {
memcpy(retaddr, &result, sizeof(result));
}
}

/* clang-format off */
#define REG_NATIVE_FUNC(func_name, signature) \
{ #func_name, func_name##_wrapper, signature, NULL }
#define REG_NATIVE_FUNC(symbol, func_name, signature) \
{ symbol, func_name##_wrapper, signature, NULL }
/* clang-format on */

static NativeSymbol native_symbols_lib_wasi_threads[] = { REG_NATIVE_FUNC(
thread_spawn, "(i)i") };
static NativeSymbol native_symbols_lib_wasi_threads[] = {
REG_NATIVE_FUNC("thread-spawn", thread_spawn, "(i*)"),
};

uint32
get_lib_wasi_threads_export_apis(NativeSymbol **p_lib_wasi_threads_apis)
Expand Down