Skip to content

MessageQueue<T>: a one-way queue between threads (Worker step 1) - #80

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

ASDAlexander77 merged 1 commit into
mainfrom
worker-queue

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

Step 1 of docs/worker-design.md: MessageQueue<T>, the one-way queue between threads that Worker will be built on.

  • MessageQueue<T> in src/generics/lib.generics.ts, after Thread, with push, take(timeout?), close(), clear(), closed and length.
  • Items stay in a TypeScript array, guarded by a lock taken with Atomics.compareExchange. A thread waiting for an item sleeps in Atomics.wait rather than spinning.
    • Under -mm=gc the collector sees queued items.
    • Under -mm=rc, Shared.share makes their counts atomic. It marks every block the value reaches, so a message's fields are covered too.
    • Under -mm=own, a push moves the item in and take moves it out.
  • Both threads reach the queue through a const Shared<MessageQueue<T>> that each function captures, the same way the thread tests share an Int32Array.

Tests

tests/message_queue.ts:

  • Two threads pass 10,000 items in order. Separate runs cover numbers, strings, and class instances sent in both directions.
  • It also checks the timeout on an empty queue, close(), a push after closing, and clear().

Run locally in release under gc, rc, none and own, both compiled and under the JIT, all passing. The compiled test passed 8 runs out of 8 per model. thread_spawn, thread_move, atomics_wait and atomics still pass under all four.

Not run locally: the debug configuration. This machine's compiler debug runtime is older than tslang (it has no __tslang_heap_shared), so the debug library doesn't build here. CI is the first debug run.

Design doc updates

docs/worker-design.md now records what step 1 settled for -mm=own:

  • Objects go through the queue without Shared<T>.
  • A push is a move: using the item afterwards is a compile error.
  • Open point for step 2: a handler stored in the Worker can't capture a local that's used again afterwards. That is a compile error under own, so handler state needs a Shared<T>, or the program uses receive() instead.

🤖 Generated with Claude Code

MessageQueue<T> (push, take with an optional timeout, close, clear,
closed, length) keeps its items in a TypeScript array guarded by a lock
taken with Atomics.compareExchange; a taker sleeps in Atomics.wait. So
the collector sees queued items under -mm=gc, Shared.share keeps their
counts atomic under -mm=rc, and under -mm=own a push moves the item in
and take moves it out.

tests/message_queue.ts: two threads pass 10 000 numbers, strings and
class instances through it in order, both ways for the objects, plus
timeout, close and clear. Passes under gc, rc, none and own, release,
compiled and under the JIT; debug was not built locally.

docs/worker-design.md records what this settled: objects go through
under own without Shared<T>, a push is a move, and the open point for
step 2 - under own a handler stored in the Worker can't capture a local
used again afterwards.

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