Skip to content

origins.mojo on threads-mojo 0.3.0: the state by ref, the task by mut - #5

Merged
winding-lines merged 1 commit into
mainfrom
typed
Sep 3, 2026
Merged

winding-lines merged 1 commit into
mainfrom
typed

Conversation

@winding-lines

Copy link
Copy Markdown
Contributor

src/origins.mojo rewritten in place — same file, same link — against threads-mojo 0.3.0's typed parallel_for:

def task(i: Int, mut totals: Totals) -> None:
    _ = counter(totals.sum).fetch_add(Int64(i))

def main() raises:
    var totals = Totals(0)
    parallel_for[task](1000, totals)
    print("cores:", num_cpus(), " sum:", totals.sum)

Ctx, share and run are gone: the library now binds the origin to the ref argument and does the one void * erasure itself. The docstring keeps a paragraph on the earlier version for anyone arriving from the posted file.

Tests follow the API. share_immutable/share_temporary → state_immutable/state_temporary, same diagnostic from parallel_for's ref argument. return_ctx_to_local and ctx_outlives_block are removed — there is no user-visible value carrying the origin any more, so nothing to leak. opaque_escapes_origin now calls the opaque overload on opaque_ptr(Int(Pointer(to=totals))): Int(…) is where the origin stops. The other three uncaught cases are the same bugs through the typed call.

pixi.lock regenerated with pixi 0.78.0 (CI's pin). Both programs print sum: 499500; pixi run check: 6 passed, 0 failed.

🤖 Generated with Claude Code

https://claude.ai/code/session_015or6oTZmb7EQTXTbALbBXY

The typed parallel_for does in the library what this file did in user
code: the origin of `totals` is bound to a ref argument, alive for the
whole call, and the void * erasure happens once, out of sight. So Ctx,
share and run go, and the program is the post's listing minus the
pointer plumbing. Rewritten in place — the file has been linked to.

Tests follow the API. The two Ctx-lifetime cases (returning one to a
local, one outliving its block) have no equivalent now that there is no
user-visible value carrying the origin, and are removed. share_* become
state_*, the same diagnostic from parallel_for's ref argument.
opaque_escapes_origin now uses the opaque overload directly: Int(…) on
a tracked Pointer is where the origin stops. 6 passed, 0 failed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015or6oTZmb7EQTXTbALbBXY
@winding-lines
winding-lines merged commit 97b610a into main Sep 3, 2026
2 checks passed
@winding-lines
winding-lines deleted the typed branch September 3, 2026 14:34
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