Skip to content

Commit

Permalink
test(itk-wasm): add pthreads and cxx-threads test pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jan 9, 2025
1 parent 3f48cb9 commit 1f6a978
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ add_subdirectory("median-filter-pipeline")
add_subdirectory("mesh-read-write-pipeline")
add_subdirectory("transform-read-write-pipeline")
add_subdirectory("read-image")
add_subdirectory("stdout-stderr-pipeline")
add_subdirectory("stdout-stderr-pipeline")
if (NOT EMSCRIPTEN)
add_subdirectory("pthreads-pipeline")
add_subdirectory("cxx-threads-pipeline")
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.16)
project(cxx-threads-test)

set(CMAKE_CXX_STANDARD 20)

find_package(ITK REQUIRED
COMPONENTS WebAssemblyInterface)
include(${ITK_USE_FILE})
add_executable(cxx-threads-test cxx-threads-test.cxx)
target_link_libraries(cxx-threads-test PUBLIC WebAssemblyInterface)

enable_testing()
add_test(NAME CjXThreadsTest
COMMAND cxx-threads-test
${CMAKE_CURRENT_BINARY_DIR}/cxx-threads-test-output.json
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkPipeline.h"
#include "itkOutputTextStream.h"

#include <sstream>
#include <iostream>
#include <atomic>
#include <thread>
#include <vector>

static std::atomic<int> createdThreads(0);

int main(int argc, char * argv[])
{
itk::wasm::Pipeline pipeline("cxx-threads-test", "A pipeline to test C++11 threads support", argc, argv);

int numberOfThreads = 4;
pipeline.add_option("-n,--number-of-threads", numberOfThreads, "The number of threads to use");

itk::wasm::OutputTextStream outputJson;
pipeline.add_option("output-json", outputJson, "The output json with test results")->type_name("OUTPUT_JSON");

ITK_WASM_PARSE(pipeline);

std::vector<std::thread> threads;
threads.reserve(numberOfThreads);
createdThreads = 0;

for (int i = 0; i < numberOfThreads; ++i)
{
threads.emplace_back([i]()
{
std::cout << " in thread " << i << std::endl;
createdThreads++;
});
}

for (auto &t : threads)
{
t.join();
}

outputJson.Get() << "{ \"createdThreads\": " << createdThreads << " }" << std::endl;
return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.16)
project(pthreads-test)

set(CMAKE_CXX_STANDARD 20)

find_package(ITK REQUIRED
COMPONENTS WebAssemblyInterface)
include(${ITK_USE_FILE})
add_executable(pthreads-test pthreads-test.cxx)
target_link_libraries(pthreads-test PUBLIC WebAssemblyInterface)

enable_testing()
add_test(NAME PThreadsTest
COMMAND pthreads-test
${CMAKE_CURRENT_BINARY_DIR}/pthreads-test-output.json
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include "itkPipeline.h"
#include "itkOutputTextStream.h"

#include <sstream>
#include <iostream>
#include <atomic>

#include "pthread.h"

static std::atomic<int> createdThreads(0);

void *thread_entry_point(void *ctx) {
int id = (int) ctx;
printf(" in thread %d\n", id);
createdThreads++;
return 0;
}

int main( int argc, char * argv[] )
{
itk::wasm::Pipeline pipeline("pthreads-test", "A pipeline to test pthreads support", argc, argv);

int numberOfThreads = 4;
pipeline.add_option("-n,--number-of-threads", numberOfThreads, "The number of threads to use");

itk::wasm::OutputTextStream outputJson;
pipeline.add_option("output-json", outputJson, "The output json with test results")->type_name("OUTPUT_JSON");

ITK_WASM_PARSE(pipeline);

// Based on: https://bytecodealliance.org/articles/wasi-threads
pthread_t threads[256];
createdThreads = 0;
for (int i = 0; i < numberOfThreads; i++) {
int ret = pthread_create(&threads[i], NULL, &thread_entry_point, (void *) i);
if (ret) {
printf("failed to spawn thread: %s", strerror(ret));
}
}

// Wait for all threads to finish
for (int i = 0; i < numberOfThreads; i++)
{
pthread_join(threads[i], NULL);
}

// Write the output JSON
outputJson.Get() << "{ \"createdThreads\": " << createdThreads << " }" << std::endl;

return EXIT_SUCCESS;
}

0 comments on commit 1f6a978

Please sign in to comment.