Repository navigation
Worker, WorkerScope and MessageEvent (Worker step 2) - #81
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Step 2 of
docs/worker-design.md: theWorkerclass, built onMessageQueue<T>from #80.new Worker<In, Out>(body, name?)runsbody(self)on a new thread. The worker then handles each message posted to it until it callsself.close().receive(timeout?)takes the next one.join()hands each toworker.onmessageuntil the worker has ended, then ends its thread.closedandnameare also available.-mm=ownthey move.Design decisions found while building it
-mm=own, a closure given toThread.spawncan't take the constructor'sbodyparameter with it.Workerkeeps aWorkerStartobject and starts the thread withtslang_thread_spawn, the same wayThreadHandlekeeps itsThreadStart. The constructor is marked@keeps("body", "name").onmessageis 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, whichownrejects.own. A handler that capturesselfcompiles, so the handler takes just the event, as in MDN.nameis a plain string, not MDN's{ name }options object.onerrorarrives in step 3: a throw frombodyor from a handler ends the worker, andjoin()throws anErrorwith its message. The queues close in afinally, so the creating thread never waits on a worker that has died.Tests
tests/worker_echo.ts: numbers in and strings out, posting frombody,receivetiming 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,noneandown, compiled and under the JIT, and all passed. Each test passed 6 runs out of 6 per model.message_queue,thread_spawn,thread_moveandatomics_waitstill 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.mdrecords a tslang crash found along the way. A generic class whose constructor spawns a thread from a closure dies with an access violation undergc,rcandnonewhen 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 shippedWorkerdoesn't trigger it.docs/worker-design.mdrecords what step 2 settled. The remaining-mm=ownlimit for the README: a creator-sideonmessagecan't change a local that's used again afterwards, so its state goes in aShared<T>, as the test does.🤖 Generated with Claude Code