Skip to content

Commit

Permalink
wasm2c: Cleanup TLS: check for __thread and declare TLS vars only whe…
Browse files Browse the repository at this point in the history
…n needed
  • Loading branch information
shravanrn committed Oct 27, 2024
1 parent d6e97af commit 24d3c1d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions wasm2c/wasm-rt-impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ WASM_RT_THREAD_LOCAL uint32_t wasm_rt_saved_call_stack_depth;
static WASM_RT_THREAD_LOCAL void* g_alt_stack = NULL;
#endif

#ifndef WASM_RT_TRAP_HANDLER
WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf g_wasm_rt_jmp_buf;
#endif

#ifdef WASM_RT_TRAP_HANDLER
extern void WASM_RT_TRAP_HANDLER(wasm_rt_trap_t code);
Expand Down Expand Up @@ -162,7 +164,7 @@ static bool os_has_altstack_installed() {
/* check for altstack already in place */
stack_t ss;
if (sigaltstack(NULL, &ss) != 0) {
perror("sigaltstack failed");
perror("sigaltstack check failed");
abort();
}

Expand Down Expand Up @@ -192,7 +194,7 @@ static void os_allocate_and_install_altstack(void) {
ss.ss_flags = 0;
ss.ss_size = SIGSTKSZ;
if (sigaltstack(&ss, NULL) != 0) {
perror("sigaltstack failed");
perror("sigaltstack install failed");
abort();
}
}
Expand All @@ -205,7 +207,7 @@ static void os_disable_and_deallocate_altstack(void) {
/* verify altstack was still in place */
stack_t ss;
if (sigaltstack(NULL, &ss) != 0) {
perror("sigaltstack failed");
perror("sigaltstack uninstall check failed");
abort();
}

Expand All @@ -219,7 +221,7 @@ static void os_disable_and_deallocate_altstack(void) {
/* disable and free */
ss.ss_flags = SS_DISABLE;
if (sigaltstack(&ss, NULL) != 0) {
perror("sigaltstack failed");
perror("sigaltstack uninstall failed");
abort();
}
assert(!os_has_altstack_installed());
Expand Down
2 changes: 2 additions & 0 deletions wasm2c/wasm-rt-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
extern "C" {
#endif

#ifndef WASM_RT_TRAP_HANDLER
/** A setjmp buffer used for handling traps. */
extern WASM_RT_THREAD_LOCAL wasm_rt_jmp_buf g_wasm_rt_jmp_buf;
#endif

#if WASM_RT_STACK_DEPTH_COUNT
/** Saved call stack depth that will be restored in case a trap occurs. */
Expand Down
4 changes: 4 additions & 0 deletions wasm2c/wasm-rt.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ extern "C" {

#ifdef _MSC_VER
#define WASM_RT_THREAD_LOCAL __declspec(thread)
// We use __thread on POSIXy systems. This is disabled on Apple systems right
// now due to sporadic test failures.
#elif (defined(__GNUC__) || defined(__clang__)) && !defined(__APPLE__)
#define WASM_RT_THREAD_LOCAL __thread
#elif defined(WASM_RT_C11_AVAILABLE)
#define WASM_RT_THREAD_LOCAL _Thread_local
#else
Expand Down

0 comments on commit 24d3c1d

Please sign in to comment.