Skip to content

[WIP] Implement Pthread Manager Worker for synchronous thread creation#26979

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:pthread-manager-worker
Open

[WIP] Implement Pthread Manager Worker for synchronous thread creation#26979
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:pthread-manager-worker

Conversation

@sbc100
Copy link
Copy Markdown
Collaborator

@sbc100 sbc100 commented May 19, 2026

[WIP] - Gemini Created initial PR

This change introduces a dedicated 'Pthread Manager' worker that acts as an intermediary for managing the lifecycle of pthread workers.

By moving the responsibility of spawning and managing workers to a dedicated manager worker, we enable synchronous pthread creation on the main browser thread even when it is blocked (e.g. during a join or futex wait). This is because the manager worker and its nested child workers can be started by the browser independently of the main thread's event loop.

Key changes:

  • Add PTHREAD_MANAGER setting to enable this new mode.
  • Update shell.js and parseTools.mjs to detect and support the manager environment.
  • Update libpthread.js to spawn the manager worker and proxy thread creation requests to it.
  • Update runtime_pthread.js with logic to handle the manager worker's responsibilities: spawning, terminating, and relaying messages for child pthreads.
  • Ensure the manager worker is fully initialized before the main application starts using addRunDependency.

Fixes: #18633

This change introduces a dedicated 'Pthread Manager' worker that acts as
an intermediary for managing the lifecycle of pthread workers.

By moving the responsibility of spawning and managing workers to a
dedicated manager worker, we enable synchronous pthread creation on the
main browser thread even when it is blocked (e.g. during a join or
futex wait). This is because the manager worker and its nested child
workers can be started by the browser independently of the main
thread's event loop.

Key changes:
- Add PTHREAD_MANAGER setting to enable this new mode.
- Update shell.js and parseTools.mjs to detect and support the manager
  environment.
- Update libpthread.js to spawn the manager worker and proxy thread
  creation requests to it.
- Update runtime_pthread.js with logic to handle the manager worker's
  responsibilities: spawning, terminating, and relaying messages for
  child pthreads.
- Ensure the manager worker is fully initialized before the main
  application starts using addRunDependency.

Fixes: emscripten-core#18633
@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented May 19, 2026

I had gemini create this. @juj @tlively @dschuff WDYT? Is this is reasonable approach? One thing I'm not sure about is whether we want to re-use the ensure JS file in the worker-mananger so just run a tiny bit of custom JS there.

@sbc100
Copy link
Copy Markdown
Collaborator Author

sbc100 commented May 19, 2026

Kind of an interesting approach that gemini took here, which is to create to have the pthread-manager running the same JS file.

I think this has some advantages in that we can keep the worker spawning code in place where it can run both in manager and non-manager mode.

I also like that avoids adding an additional file (e.g. manager.js) which I think would be non-starter.

The alternative would be to embed some kind of manager.js as a blob inside the main JS. But that could make it hard to maintain both modes simultaneously.

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.

Managing thread lifetimes off the main thread

1 participant