From 8ac06490db10cea20fdc07784555f93e60130805 Mon Sep 17 00:00:00 2001 From: James Ring Date: Mon, 23 Dec 2024 15:25:52 -0800 Subject: [PATCH] Set thread information earlier in exec_env creation (#3967) For boundary checking, WAMR calls `pthread_attr_np`, which is unfortunately quite slow on Linux when not called on the main thread (see https://github.com/bytecodealliance/wasm-micro-runtime/issues/3966 for discussion). This change moves the cost of stack bounds checking earlier in the wasm_exec_env creation process. The idea is that it's perhaps better to pay the price when creating the execution environment rather than in the first function call. The original code is left in place inside `call_wasm_with_hw_bound_check` in case the `wasm_exec_env` is created via `wasm_runtime_spawn_exec_env`. --- core/shared/platform/common/posix/posix_thread.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/shared/platform/common/posix/posix_thread.c b/core/shared/platform/common/posix/posix_thread.c index 5ec957e5..1d102460 100644 --- a/core/shared/platform/common/posix/posix_thread.c +++ b/core/shared/platform/common/posix/posix_thread.c @@ -712,6 +712,13 @@ os_thread_signal_init(os_signal_handler handler) #if WASM_DISABLE_STACK_HW_BOUND_CHECK == 0 sigalt_stack_base_addr = map_addr; #endif + +#if defined(os_thread_local_attribute) + // calculate and cache the new stack boundary. + // see https://github.com/bytecodealliance/wasm-micro-runtime/issues/3966 + (void)os_thread_get_stack_boundary(); +#endif + signal_handler = handler; thread_signal_inited = true; return 0;