From ebd79218c0d71e2657838f72c1f323bd5881d788 Mon Sep 17 00:00:00 2001 From: Alexis Placet Date: Wed, 3 Jun 2026 16:57:47 +0200 Subject: [PATCH 1/5] onetbb: add tbbmalloc tests and force pthread-based resumable tasks --- recipes/recipes_emscripten/onetbb/build.sh | 14 +++++- .../recipes_emscripten/onetbb/build_tests.sh | 5 +- .../onetbb/tests/CMakeLists.txt | 5 ++ .../onetbb/tests/test_onetbb_malloc.cpp | 46 +++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 recipes/recipes_emscripten/onetbb/tests/test_onetbb_malloc.cpp diff --git a/recipes/recipes_emscripten/onetbb/build.sh b/recipes/recipes_emscripten/onetbb/build.sh index 9763c1a546..642ef08816 100755 --- a/recipes/recipes_emscripten/onetbb/build.sh +++ b/recipes/recipes_emscripten/onetbb/build.sh @@ -9,8 +9,16 @@ CXXFLAGS="${CXXFLAGS:-}" LDFLAGS="${LDFLAGS:-}" # Disable ucontext-based resumable tasks — ucontext symbols -# (getcontext/makecontext/swapcontext) are not linkable on WASM. -# This forces oneTBB to use pthread-based coroutines instead. +# (getcontext/makecontext/swapcontext) are not linkable on WASM +# as a main module. Force oneTBB to use pthread-based coroutines. +# +# Both approaches are needed: +# 1. CMake variable (TBB_RESUMABLE_TASKS_USE_THREADS) overrides the +# auto-detection in cmake/resumable_tasks.cmake which finds +# getcontext in the build-host sysroot and would otherwise use +# the ucontext code path. +# 2. Compiler flags in CFLAGS/CXXFLAGS as a belt-and-suspenders +# measure for any source compiled outside the CMake target. export CFLAGS="$CFLAGS $EM_FORGE_SIDE_MODULE_CFLAGS -D__TBB_RESUMABLE_TASKS_USE_THREADS=1" export CXXFLAGS="$CXXFLAGS $EM_FORGE_SIDE_MODULE_CFLAGS -D__TBB_RESUMABLE_TASKS_USE_THREADS=1" export LDFLAGS="$LDFLAGS $EM_FORGE_SIDE_MODULE_LDFLAGS" @@ -24,6 +32,7 @@ emcmake cmake -GNinja \ -DCMAKE_INSTALL_LIBDIR=lib \ -DCMAKE_INSTALL_PREFIX="${PREFIX}" \ -DCMAKE_PREFIX_PATH="${PREFIX}" \ + -DTBB_RESUMABLE_TASKS_USE_THREADS="__TBB_RESUMABLE_TASKS_USE_THREADS=1" \ -DBUILD_SHARED_LIBS=OFF \ -DTBB_STRICT=OFF \ -DTBB_DISABLE_HWLOC_AUTOMATIC_SEARCH=ON \ @@ -33,6 +42,7 @@ emcmake cmake -GNinja \ -DTBBMALLOC_BUILD=ON \ -DTBBMALLOC_PROXY_BUILD=ON \ -DTBB_INSTALL=ON \ + -DCMAKE_C_FLAGS="-Wno-unused-command-line-argument ${CFLAGS}" \ -DCMAKE_CXX_FLAGS="-Wno-unused-command-line-argument ${CXXFLAGS}" \ .. diff --git a/recipes/recipes_emscripten/onetbb/build_tests.sh b/recipes/recipes_emscripten/onetbb/build_tests.sh index 331cba5841..23ed49a27f 100755 --- a/recipes/recipes_emscripten/onetbb/build_tests.sh +++ b/recipes/recipes_emscripten/onetbb/build_tests.sh @@ -15,5 +15,8 @@ emcmake cmake -S tests -B build_tests \ emmake make -C build_tests -j"${CPU_COUNT}" -echo "Running test..." +echo "Running TBB threading test..." node build_tests/test_onetbb.js + +echo "Running TBB malloc test..." +node build_tests/test_onetbb_malloc.js diff --git a/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt b/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt index e3a914bd61..d2b9171037 100644 --- a/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt +++ b/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt @@ -11,5 +11,10 @@ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") find_package(TBB CONFIG REQUIRED) +# Test TBB threading library add_executable(test_onetbb test_onetbb.cpp) target_link_libraries(test_onetbb TBB::tbb) + +# Test TBB malloc library (needed by oneDAL) +add_executable(test_onetbb_malloc test_onetbb_malloc.cpp) +target_link_libraries(test_onetbb_malloc TBB::tbbmalloc) diff --git a/recipes/recipes_emscripten/onetbb/tests/test_onetbb_malloc.cpp b/recipes/recipes_emscripten/onetbb/tests/test_onetbb_malloc.cpp new file mode 100644 index 0000000000..24f97e2a68 --- /dev/null +++ b/recipes/recipes_emscripten/onetbb/tests/test_onetbb_malloc.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +int main() { + // 1. scalable_malloc / scalable_free + void* p = scalable_malloc(1024); + assert(p != nullptr); + std::memset(p, 0xAB, 1024); + scalable_free(p); + + // 2. scalable_calloc + int* arr = static_cast(scalable_calloc(100, sizeof(int))); + assert(arr != nullptr); + for (int i = 0; i < 100; ++i) { + assert(arr[i] == 0); + } + scalable_free(arr); + + // 3. scalable_realloc + char* s = static_cast(scalable_malloc(32)); + assert(s != nullptr); + std::memcpy(s, "hello tbbmalloc", 15); + s = static_cast(scalable_realloc(s, 64)); + assert(s != nullptr); + scalable_free(s); + + // 4. scalable_aligned_malloc / scalable_aligned_free + void* ap = scalable_aligned_malloc(256, 64); + assert(ap != nullptr); + assert(reinterpret_cast(ap) % 64 == 0); + scalable_aligned_free(ap); + + // 5. scalable_posix_memalign + void* pp = nullptr; + int ret = scalable_posix_memalign(&pp, 64, 128); + assert(ret == 0); + assert(pp != nullptr); + assert(reinterpret_cast(pp) % 64 == 0); + scalable_free(pp); + + std::cout << "tbbmalloc tests passed!" << std::endl; + return 0; +} From b2b31b27bb9b0fe5a46e85088aa3a1d93a7e7f2e Mon Sep 17 00:00:00 2001 From: Alexis Placet Date: Wed, 3 Jun 2026 17:00:14 +0200 Subject: [PATCH 2/5] bump build number --- recipes/recipes_emscripten/onetbb/recipe.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recipes/recipes_emscripten/onetbb/recipe.yaml b/recipes/recipes_emscripten/onetbb/recipe.yaml index 4fff27acd2..d61dee05e0 100644 --- a/recipes/recipes_emscripten/onetbb/recipe.yaml +++ b/recipes/recipes_emscripten/onetbb/recipe.yaml @@ -11,7 +11,7 @@ source: sha256: f8767b971ec6aea25dde58ae0f593e94e7aa75a739a86f67967012f69e2199b1 build: - number: 0 + number: 1 script: build.sh requirements: From 67931a7604ab44438f7443edb59508e445e7d20a Mon Sep 17 00:00:00 2001 From: Alexis Placet Date: Thu, 4 Jun 2026 17:07:29 +0200 Subject: [PATCH 3/5] onetbb: use PROXY_TO_PTHREAD in tbbmalloc tests --- recipes/recipes_emscripten/onetbb/build_tests.sh | 6 +++++- recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/recipes/recipes_emscripten/onetbb/build_tests.sh b/recipes/recipes_emscripten/onetbb/build_tests.sh index 23ed49a27f..c052e5fcf3 100755 --- a/recipes/recipes_emscripten/onetbb/build_tests.sh +++ b/recipes/recipes_emscripten/onetbb/build_tests.sh @@ -3,6 +3,9 @@ set -e # -pthread must match libtbb.a build; without it the linker rejects # --shared-memory and the test binary won't link. +# -sPROXY_TO_PTHREAD is recommended by oneTBB WASM docs: +# splits the initial thread into a browser thread + main Web Worker, +# preventing deadlocks from nested Web Worker scheduling. export CFLAGS="${CFLAGS} -pthread" export CXXFLAGS="${CXXFLAGS} -pthread" @@ -10,8 +13,9 @@ emcmake cmake -S tests -B build_tests \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_PREFIX_PATH="${PREFIX}" \ -DCMAKE_FIND_ROOT_PATH="${PREFIX}" \ + -DCMAKE_C_FLAGS="-pthread" \ -DCMAKE_CXX_FLAGS="-pthread" \ - -DCMAKE_EXE_LINKER_FLAGS="-pthread" + -DCMAKE_EXE_LINKER_FLAGS="-pthread -sPROXY_TO_PTHREAD" emmake make -C build_tests -j"${CPU_COUNT}" diff --git a/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt b/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt index d2b9171037..cec109e1b7 100644 --- a/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt +++ b/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt @@ -3,11 +3,13 @@ project(test_onetbb CXX) set(CMAKE_CXX_STANDARD 17) # pthreads are required to match libtbb.a (compiled with -pthread). +# -sPROXY_TO_PTHREAD is recommended by oneTBB WASM docs to prevent +# deadlocks from nested Web Worker scheduling. # These flags must be set BEFORE find_package / add_executable so they # affect both compilation and linking. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread -sPROXY_TO_PTHREAD") find_package(TBB CONFIG REQUIRED) From 1a2f07a9d3fe1a262f052d53b6e6cd69b5f42f76 Mon Sep 17 00:00:00 2001 From: Alexis Placet Date: Fri, 5 Jun 2026 09:00:24 +0200 Subject: [PATCH 4/5] fix --- recipes/recipes_emscripten/onetbb/build.sh | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/recipes/recipes_emscripten/onetbb/build.sh b/recipes/recipes_emscripten/onetbb/build.sh index 642ef08816..e44ced6c86 100755 --- a/recipes/recipes_emscripten/onetbb/build.sh +++ b/recipes/recipes_emscripten/onetbb/build.sh @@ -8,20 +8,8 @@ CFLAGS="${CFLAGS:-}" CXXFLAGS="${CXXFLAGS:-}" LDFLAGS="${LDFLAGS:-}" -# Disable ucontext-based resumable tasks — ucontext symbols -# (getcontext/makecontext/swapcontext) are not linkable on WASM -# as a main module. Force oneTBB to use pthread-based coroutines. -# -# Both approaches are needed: -# 1. CMake variable (TBB_RESUMABLE_TASKS_USE_THREADS) overrides the -# auto-detection in cmake/resumable_tasks.cmake which finds -# getcontext in the build-host sysroot and would otherwise use -# the ucontext code path. -# 2. Compiler flags in CFLAGS/CXXFLAGS as a belt-and-suspenders -# measure for any source compiled outside the CMake target. export CFLAGS="$CFLAGS $EM_FORGE_SIDE_MODULE_CFLAGS -D__TBB_RESUMABLE_TASKS_USE_THREADS=1" export CXXFLAGS="$CXXFLAGS $EM_FORGE_SIDE_MODULE_CFLAGS -D__TBB_RESUMABLE_TASKS_USE_THREADS=1" -export LDFLAGS="$LDFLAGS $EM_FORGE_SIDE_MODULE_LDFLAGS" mkdir -p build cd build @@ -32,7 +20,6 @@ emcmake cmake -GNinja \ -DCMAKE_INSTALL_LIBDIR=lib \ -DCMAKE_INSTALL_PREFIX="${PREFIX}" \ -DCMAKE_PREFIX_PATH="${PREFIX}" \ - -DTBB_RESUMABLE_TASKS_USE_THREADS="__TBB_RESUMABLE_TASKS_USE_THREADS=1" \ -DBUILD_SHARED_LIBS=OFF \ -DTBB_STRICT=OFF \ -DTBB_DISABLE_HWLOC_AUTOMATIC_SEARCH=ON \ From 7aff02acfeea4b5b0761b04874f8f6a6b5559785 Mon Sep 17 00:00:00 2001 From: Alexis Placet Date: Fri, 5 Jun 2026 09:20:51 +0200 Subject: [PATCH 5/5] use EMSCRIPTEN_WITHOUT_PTHREAD --- recipes/recipes_emscripten/onetbb/build.sh | 4 +--- recipes/recipes_emscripten/onetbb/build_tests.sh | 13 +------------ .../recipes_emscripten/onetbb/tests/CMakeLists.txt | 9 --------- 3 files changed, 2 insertions(+), 24 deletions(-) diff --git a/recipes/recipes_emscripten/onetbb/build.sh b/recipes/recipes_emscripten/onetbb/build.sh index e44ced6c86..c1edebe775 100755 --- a/recipes/recipes_emscripten/onetbb/build.sh +++ b/recipes/recipes_emscripten/onetbb/build.sh @@ -8,9 +8,6 @@ CFLAGS="${CFLAGS:-}" CXXFLAGS="${CXXFLAGS:-}" LDFLAGS="${LDFLAGS:-}" -export CFLAGS="$CFLAGS $EM_FORGE_SIDE_MODULE_CFLAGS -D__TBB_RESUMABLE_TASKS_USE_THREADS=1" -export CXXFLAGS="$CXXFLAGS $EM_FORGE_SIDE_MODULE_CFLAGS -D__TBB_RESUMABLE_TASKS_USE_THREADS=1" - mkdir -p build cd build @@ -23,6 +20,7 @@ emcmake cmake -GNinja \ -DBUILD_SHARED_LIBS=OFF \ -DTBB_STRICT=OFF \ -DTBB_DISABLE_HWLOC_AUTOMATIC_SEARCH=ON \ + -DEMSCRIPTEN_WITHOUT_PTHREAD=ON \ -DTBB_EXAMPLES=OFF \ -DTBB_TEST=OFF \ -DTBB4PY_BUILD=OFF \ diff --git a/recipes/recipes_emscripten/onetbb/build_tests.sh b/recipes/recipes_emscripten/onetbb/build_tests.sh index c052e5fcf3..60d8a02667 100755 --- a/recipes/recipes_emscripten/onetbb/build_tests.sh +++ b/recipes/recipes_emscripten/onetbb/build_tests.sh @@ -1,21 +1,10 @@ #!/bin/bash set -e -# -pthread must match libtbb.a build; without it the linker rejects -# --shared-memory and the test binary won't link. -# -sPROXY_TO_PTHREAD is recommended by oneTBB WASM docs: -# splits the initial thread into a browser thread + main Web Worker, -# preventing deadlocks from nested Web Worker scheduling. -export CFLAGS="${CFLAGS} -pthread" -export CXXFLAGS="${CXXFLAGS} -pthread" - emcmake cmake -S tests -B build_tests \ -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_PREFIX_PATH="${PREFIX}" \ - -DCMAKE_FIND_ROOT_PATH="${PREFIX}" \ - -DCMAKE_C_FLAGS="-pthread" \ - -DCMAKE_CXX_FLAGS="-pthread" \ - -DCMAKE_EXE_LINKER_FLAGS="-pthread -sPROXY_TO_PTHREAD" + -DCMAKE_FIND_ROOT_PATH="${PREFIX}" emmake make -C build_tests -j"${CPU_COUNT}" diff --git a/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt b/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt index cec109e1b7..e8850d78c9 100644 --- a/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt +++ b/recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt @@ -2,15 +2,6 @@ cmake_minimum_required(VERSION 3.16) project(test_onetbb CXX) set(CMAKE_CXX_STANDARD 17) -# pthreads are required to match libtbb.a (compiled with -pthread). -# -sPROXY_TO_PTHREAD is recommended by oneTBB WASM docs to prevent -# deadlocks from nested Web Worker scheduling. -# These flags must be set BEFORE find_package / add_executable so they -# affect both compilation and linking. -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") -set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread -sPROXY_TO_PTHREAD") - find_package(TBB CONFIG REQUIRED) # Test TBB threading library