From ce1d71662c44c361cfbe2e09aa8490204002ec90 Mon Sep 17 00:00:00 2001 From: Shiqiu Zhang Date: Wed, 11 Mar 2026 12:05:08 -0700 Subject: [PATCH] Adapt threadpool for WASM/Emscripten compatibility (#18106) Summary: Disable pthread_atfork-related code in threadpool when building for Emscripten/WASM, since fork() is not supported in WASM environments. Reviewed By: GregoryComer Differential Revision: D94744811 --- extension/threadpool/targets.bzl | 1 + extension/threadpool/threadpool.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/extension/threadpool/targets.bzl b/extension/threadpool/targets.bzl index 7bbdf1169c7..223e19bbc49 100644 --- a/extension/threadpool/targets.bzl +++ b/extension/threadpool/targets.bzl @@ -50,6 +50,7 @@ def define_common_targets(): "ovr_config//os:windows": [":threadpool_lib"], "ovr_config//os:android": [":threadpool_lib"], "ovr_config//os:iphoneos": [":threadpool_lib"], + "ovr_config//runtime:wasm-emscripten": [":threadpool_lib"], # Machines without an operating system shouldn't. "ovr_config//os:none": ["//executorch/runtime/kernel:thread_parallel_interface"], # If we don't know what it is, disable threadpool out of caution. diff --git a/extension/threadpool/threadpool.cpp b/extension/threadpool/threadpool.cpp index 26acc9fb81b..a15a2572669 100644 --- a/extension/threadpool/threadpool.cpp +++ b/extension/threadpool/threadpool.cpp @@ -36,7 +36,7 @@ namespace executorch::extension::threadpool { -#if !(defined(WIN32)) +#if !defined(WIN32) && !defined(__EMSCRIPTEN__) namespace { // After fork, the child process inherits the data-structures of the parent // process' thread-pool, but since those threads don't exist, the thread-pool @@ -153,7 +153,7 @@ ThreadPool* get_threadpool() { // Inheriting from old threadpool to get around segfault issue // commented above at child_atfork -#if !(defined(WIN32)) +#if !defined(WIN32) && !defined(__EMSCRIPTEN__) // @lint-ignore CLANGTIDY facebook-hte-std::once_flag static std::once_flag flag; // @lint-ignore CLANGTIDY facebook-hte-std::call_once