Skip to content

refactor: dispatch async work queue in shared memory#200

Merged
toyobayashi merged 11 commits into
v1.xfrom
refactor-async-work-js
Mar 5, 2026
Merged

refactor: dispatch async work queue in shared memory#200
toyobayashi merged 11 commits into
v1.xfrom
refactor-async-work-js

Conversation

@toyobayashi

@toyobayashi toyobayashi commented Mar 3, 2026

Copy link
Copy Markdown
Owner
  • Avoids deadlock when main thread block on waiting queued async work starting. Completed work can not be dispatched to main thread that cause no new worker available, then queued work never start.
  • wasm32-wasip1-threads target spawn async worker in JS will use pthread_create, no longer maintain a separate worker pool.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the async-work scheduling path to use a shared-memory queue (mutex/cond + intrusive list) so worker threads can directly fetch queued work without relying on main-thread dispatch, addressing a deadlock scenario where queued work never starts.

Changes:

  • Switch async-work scheduling to a shared-memory queue with an Atomics-based mutex/condition variable.
  • Change the async worker initialization contract to pass a shared globalAddress into initWorker(...) and remove the per-task executeAsyncWork message path.
  • Update WASI threads test linker flags to export/import shared memory and async worker entrypoints.

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/test/CMakeLists.txt Enables required shared-memory + exports for WASI threads tests.
packages/emnapi/src/core/init.ts Updates internal INapiModule worker init signature.
packages/emnapi/src/core/async-work.ts Implements shared-memory async-work queue + worker-side dequeue/execute loop.
packages/core/src/worker.ts Routes async-worker init message to the new initWorker(arg, globalAddress) signature.
packages/core/src/emnapi/index.d.ts Updates public NapiModule typing for the new initWorker signature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/emnapi/src/core/async-work.ts
Comment thread packages/emnapi/src/core/async-work.ts
Comment thread packages/emnapi/src/core/async-work.ts Outdated
Comment thread packages/emnapi/src/core/async-work.ts Outdated
Comment thread packages/emnapi/src/core/async-work.ts

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 6 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/emnapi/src/thread/async_worker_create.c Outdated
Comment thread packages/emnapi/src/core/async-work.ts
Comment thread packages/emnapi/src/core/async-work.ts Outdated
Comment thread packages/emnapi/src/core/async-work.ts
Comment thread packages/emnapi/src/core/async-work.ts
@toyobayashi toyobayashi merged commit 17e63e8 into v1.x Mar 5, 2026
7 checks passed
@toyobayashi toyobayashi deleted the refactor-async-work-js branch March 5, 2026 09:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants