Skip to content

Design for a Worker class on top of Thread and Atomics - #78

Merged
ASDAlexander77 merged 1 commit into
mainfrom
worker-design
Nov 9, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
worker-design

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

Adds docs/worker-design.md: a design for an MDN-style Worker in the defaultlib, built on Thread.spawn / join, Atomics.wait / notify and Shared<T>, which are already on main. This PR has no code, only the design.

Summary of the design

  • new Worker<In, Out>((self) => { self.onmessage = ...; }). The worker's code is a function, since there is no script URL to load. It has typed postMessage in both directions, plus onmessage, onerror, close() and terminate().
  • tslang has no event loop, so the library runs the worker's message loop for it. On the creating side, the program asks for its messages with dispatch(timeout), receive() or join().
  • Messages are handed over, not copied, because there is no structured clone. JSON.stringify gives a real copy.
  • terminate() stops the worker between handlers rather than killing the thread, and drops queued messages in both directions.
  • The queue is written in TypeScript (an array plus Atomics) so queued messages stay visible to the collector (gc) and keep their counts (rc). If that can't work under own, it falls back to a C++ queue.

For reviewers

  • The main open question is -mm=own: can objects go in the queue, and does postMessage move its argument or only borrow it? The first step of the work plan is a queue test with class instances, so this gets settled before anything is built on top.
  • Not covered by this design: loading by URL, deep structured clone, MessageChannel, addEventListener and waitAsync.
  • Every worker must be joined or terminated before main returns. The README will say so; nothing enforces it.

🤖 Generated with Claude Code

docs/worker-design.md: the Web Worker API shape (postMessage,
onmessage, onerror, close, terminate) over Thread.spawn and
Atomics.wait / notify, with dispatch / receive / join standing in for
the event loop tslang does not have. Messages are handed over, not
copied; terminate stops the worker between handlers. Lists what -mm=own
has to be checked for first, the order of work and the tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 5afe12b into main Nov 9, 2026
4 checks passed
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.

1 participant