Skip to content

Commit

Permalink
Reduce concurrency to 1 on the main browser thread (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisauke committed Aug 4, 2024
1 parent 6320dfe commit d5c2ba0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Uses libvips v8.15.2, compiled with Emscripten v3.1.64.
[#70](https://github.com/kleisauke/wasm-vips/pull/70)
[@marcosc90](https://github.com/marcosc90)

### Changed

- Reduce concurrency to 1 when wasm-vips was initialized on the main
browser thread.
[#69](https://github.com/kleisauke/wasm-vips/issues/69)

## [v0.0.9] - 2024-06-01

Uses libvips v8.15.2, compiled with Emscripten v3.1.60.
Expand Down
9 changes: 5 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ set(MAIN_LINK_OPTIONS
-sINITIAL_MEMORY=1GB
-sSTACK_SIZE=256KB
-sALLOW_TABLE_GROWTH
-sALLOW_BLOCKING_ON_MAIN_THREAD
-sTEXTDECODER=2
-sASSERTIONS=0
-sFORCE_FILESYSTEM
Expand Down Expand Up @@ -174,12 +173,14 @@ if ("web" IN_LIST ENVIRONMENT)
-sMIN_CHROME_VERSION=91
)

# Build with `-sPTHREAD_POOL_SIZE="navigator.hardwareConcurrency + 2 + 1"` since `vips_sink_disc` uses two
# write-behind background threads, plus we need another one to run a maximum of two pipelines simultaneously.
# libvips requires spawning at least VIPS_CONCURRENCY threads synchronously, with a minimum of 3 threads per
# pipeline. This count includes the two write-behind background threads used by `vips_sink_disc`. To support up to
# two concurrent pipelines, we need to double that number. Therefore, build with:
# `-sPTHREAD_POOL_SIZE='Math.max(navigator.hardwareConcurrency, 6)'`.
# Only needed on the web, pthreads on Node.js can be spawned synchronously without waiting for an event loop tick.
set(WEB_LINK_OPTIONS
--use-preload-plugins
-sPTHREAD_POOL_SIZE='navigator.hardwareConcurrency+2+1'
-sPTHREAD_POOL_SIZE='navigator.hardwareConcurrency>6?navigator.hardwareConcurrency:6'
${WEB_MIN_TARGETS}
)

Expand Down
7 changes: 7 additions & 0 deletions src/vips-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ var LibraryVips = {
addOnPreRun(() => {
// Enforce a fixed thread pool by default on web
ENV['VIPS_MAX_THREADS'] = {{{ PTHREAD_POOL_SIZE }}};

// We cannot safely spawn dedicated workers when wasm-vips was initialized on the main browser thread.
// Therefore, to avoid any potential deadlocks, we reduce the concurrency to 1. For more details,
// see: https://emscripten.org/docs/porting/pthreads.html#blocking-on-the-main-browser-thread
if (!ENVIRONMENT_IS_WORKER) {
ENV['VIPS_CONCURRENCY'] = 1;
}
});
#endif

Expand Down

0 comments on commit d5c2ba0

Please sign in to comment.