Skip to content
Open
Show file tree
Hide file tree
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
9 changes: 2 additions & 7 deletions recipes/recipes_emscripten/onetbb/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ CFLAGS="${CFLAGS:-}"
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.
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

Expand All @@ -27,12 +20,14 @@ 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 \
-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}" \
..

Expand Down
14 changes: 5 additions & 9 deletions recipes/recipes_emscripten/onetbb/build_tests.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#!/bin/bash
set -e

# -pthread must match libtbb.a build; without it the linker rejects
# --shared-memory and the test binary won't link.
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_CXX_FLAGS="-pthread" \
-DCMAKE_EXE_LINKER_FLAGS="-pthread"
-DCMAKE_FIND_ROOT_PATH="${PREFIX}"

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
2 changes: 1 addition & 1 deletion recipes/recipes_emscripten/onetbb/recipe.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ source:
sha256: f8767b971ec6aea25dde58ae0f593e94e7aa75a739a86f67967012f69e2199b1

build:
number: 0
number: 1
script: build.sh

requirements:
Expand Down
12 changes: 5 additions & 7 deletions recipes/recipes_emscripten/onetbb/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ 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).
# 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")

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)
46 changes: 46 additions & 0 deletions recipes/recipes_emscripten/onetbb/tests/test_onetbb_malloc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <tbb/scalable_allocator.h>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <cstring>

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<int*>(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<char*>(scalable_malloc(32));
assert(s != nullptr);
std::memcpy(s, "hello tbbmalloc", 15);
s = static_cast<char*>(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<std::uintptr_t>(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<std::uintptr_t>(pp) % 64 == 0);
scalable_free(pp);

std::cout << "tbbmalloc tests passed!" << std::endl;
return 0;
}