Support Emscripten
diff --git a/configure.py b/configure.py
index 4b1e93f..75fa736 100755
--- a/configure.py
+++ b/configure.py
@@ -12,7 +12,11 @@
build.export_cpath("include", ["pthreadpool.h"])
with build.options(source_dir="src", extra_include_dirs="src", deps=build.deps.fxdiv):
- build.static_library("pthreadpool", build.cc("pthreadpool.c"))
+ if build.target.is_emscripten:
+ source = "threadpool-shim.c"
+ else:
+ source = "threadpool-pthreads.c"
+ build.static_library("pthreadpool", build.cc(source))
with build.options(source_dir="test", deps=[build, build.deps.googletest]):
build.unittest("pthreadpool-test", build.cxx("pthreadpool.cc"))
diff --git a/src/pthreadpool.c b/src/threadpool-pthreads.c
similarity index 100%
rename from src/pthreadpool.c
rename to src/threadpool-pthreads.c
diff --git a/src/threadpool-shim.c b/src/threadpool-shim.c
new file mode 100644
index 0000000..6a9262f
--- /dev/null
+++ b/src/threadpool-shim.c
@@ -0,0 +1,73 @@
+/* Standard C headers */
+#include <stddef.h>
+
+/* Library header */
+#include <pthreadpool.h>
+
+static inline size_t min(size_t a, size_t b) {
+ return a < b ? a : b;
+}
+
+struct pthreadpool* pthreadpool_create(size_t threads_count) {
+ return NULL;
+}
+
+size_t pthreadpool_get_threads_count(struct pthreadpool* threadpool) {
+ return 1;
+}
+
+void pthreadpool_compute_1d(
+ struct pthreadpool* threadpool,
+ pthreadpool_function_1d_t function,
+ void* argument,
+ size_t range)
+{
+ for (size_t i = 0; i < range; i++) {
+ function(argument, i);
+ }
+}
+
+void pthreadpool_compute_1d_tiled(
+ pthreadpool_t threadpool,
+ pthreadpool_function_1d_tiled_t function,
+ void* argument,
+ size_t range,
+ size_t tile)
+{
+ for (size_t i = 0; i < range; i += tile) {
+ function(argument, i, min(range - i, tile));
+ }
+}
+
+void pthreadpool_compute_2d(
+ struct pthreadpool* threadpool,
+ pthreadpool_function_2d_t function,
+ void* argument,
+ size_t range_i,
+ size_t range_j)
+{
+ for (size_t i = 0; i < range_i; i++) {
+ for (size_t j = 0; j < range_j; j++) {
+ function(argument, i, j);
+ }
+ }
+}
+
+void pthreadpool_compute_2d_tiled(
+ pthreadpool_t threadpool,
+ pthreadpool_function_2d_tiled_t function,
+ void* argument,
+ size_t range_i,
+ size_t range_j,
+ size_t tile_i,
+ size_t tile_j)
+{
+ for (size_t i = 0; i < range_i; i += tile_i) {
+ for (size_t j = 0; j < range_j; j += tile_j) {
+ function(argument, i, j, min(range_i - i, tile_i), min(range_j - j, tile_j));
+ }
+ }
+}
+
+void pthreadpool_destroy(struct pthreadpool* threadpool) {
+}