Skip to content

Worker, WorkerScope and MessageEvent (Worker step 2) - #81

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

ASDAlexander77 merged 1 commit into
mainfrom
worker-class

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

What

Step 2 of docs/worker-design.md: the Worker class, built on MessageQueue<T> from #80.

const worker = new Worker<int, string>((self: WorkerScope<int, string>) => {
    self.onmessage = (e: MessageEvent<int>) => {
        self.postMessage(`squared: ${e.data * e.data}`);
        if (e.data == 3) self.close();
    };
});
worker.postMessage(3);
const reply = worker.receive();   // or set worker.onmessage and call worker.join()
  • new Worker<In, Out>(body, name?) runs body(self) on a new thread. The worker then handles each message posted to it until it calls self.close().
  • tslang has no event loop, so the creating thread asks for replies:
    • receive(timeout?) takes the next one.
    • join() hands each to worker.onmessage until the worker has ended, then ends its thread.
    • closed and name are also available.
  • Messages are handed over, not copied. Under -mm=own they move.

Design decisions found while building it

  • The thread is not started from a closure. Under -mm=own, a closure given to Thread.spawn can't take the constructor's body parameter with it. Worker keeps a WorkerStart object and starts the thread with tslang_thread_spawn, the same way ThreadHandle keeps its ThreadStart. The constructor is marked @keeps("body", "name").
  • onmessage is always a function on both sides, doing nothing until set. Calling it only when set would move the message on some code paths but not others, which own rejects.
  • The browser handler shape works under own. A handler that captures self compiles, so the handler takes just the event, as in MDN.
  • name is a plain string, not MDN's { name } options object.
  • Errors, until onerror arrives in step 3: a throw from body or from a handler ends the worker, and join() throws an Error with its message. The queues close in a finally, so the creating thread never waits on a worker that has died.

Tests

  • tests/worker_echo.ts: numbers in and strings out, posting from body, receive timing out, and a handler that throws.
  • tests/worker_objects.ts: 1,000 class instances each way.

I ran both locally after full release builds of the library under gc, rc, none and own, compiled and under the JIT, and all passed. Each test passed 6 runs out of 6 per model. message_queue, thread_spawn, thread_move and atomics_wait still pass under all four.

Not run locally: the debug configuration. This machine's compiler debug runtime is older than tslang, so CI is the first debug run.

Also in this PR

  • docs/known-issues-worker-ctor-initializer.md records a tslang crash found along the way. A generic class whose constructor spawns a thread from a closure dies with an access violation under gc, rc and none when the class has a field initializer (private joined = false). Assigning the field in the constructor avoids it. Cut-down versions don't crash, so the note keeps the full program as the repro. The shipped Worker doesn't trigger it.
  • docs/worker-design.md records what step 2 settled. The remaining -mm=own limit for the README: a creator-side onmessage can't change a local that's used again afterwards, so its state goes in a Shared<T>, as the test does.

🤖 Generated with Claude Code

new Worker<In, Out>(body, name?) runs body(self) on a new thread; body
sets self.onmessage and the worker then handles each message posted to
it until it calls self.close(). With no event loop, the creating thread
asks for replies: receive(timeout?) takes the next one, and join() hands
each to worker.onmessage until the worker has ended, then ends its
thread. Messages go through two MessageQueues, handed over, not copied.

- Worker keeps a WorkerStart and starts the thread with
  tslang_thread_spawn, as ThreadHandle does: under -mm=own a closure
  given to Thread.spawn can't take the constructor's body with it.
- onmessage is always a function (a no-op until set): calling it only
  when set moves the message on some paths only, which own rejects.
- Until onerror (step 3), a throw from body or a handler ends the
  worker and join() throws an Error with its message; the queues close
  in a finally, so the creator never waits on a dead worker.

Tests: worker_echo (numbers in, strings out, posting from body, receive
timing out, a handler that throws) and worker_objects (1 000 class
instances each way). Pass under gc, rc, none and own, release, compiled
and under the JIT, after full release builds; debug not built locally.

docs/known-issues-worker-ctor-initializer.md: a tslang crash found on
the way - a field initializer in a generic class whose constructor
spawns a thread from a closure dies with an access violation under gc,
rc and none. The design doc records what step 2 settled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit e58e8f1 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