Skip to content

A per-thread ambient value with a nesting scope #1175

Closed

Description

DoxReloaded writes one pattern 21 times in 13 files: a [ThreadStatic] field plus a struct whose Dispose restores the previous value. Examples: Generators/PuzzleGenerator.cs:966-1293 (9 copies), 11 recorder classes such as Generators/Moves/CopyFeedLog.cs:29, and Generators/Moves/CandidateLists.cs:80. Each scopes a recorder or an option to one generation call on one worker thread.

RestorableGlobal<T> (3.7.1 Core/Helper/RestorableGlobal.cs:107) holds one value per instance, behind a lock and delegate calls. Its docs say two threads borrowing at once are "still last-writer-wins", so it cannot replace these.

Ask: ThreadAmbient<T>, or a per-thread mode on RestorableGlobal: T Current, and Push(T value) returning a readonly struct scope that restores the saved value. Safe when nested and when disposed twice; no lock, no allocation.

Why: generators and solvers run on worker threads, and each call needs its own scoped context.

From Ambiguous-Interactive/DoxReloaded#1172.

DISCLOSURE: LLM-GENERATED TEXT

Activity

  1. wallstop commented on Nov 12, 2026

    @wallstop
    CollaboratorAuthor

    DISCLOSURE: LLM-GENERATED TEXT

    PR #1188 implements ThreadAmbient.Current and Push(value), returning a readonly disposable scope. Values are independent per thread and shared by callers using the same closed T. Copies release once through monotonic scope identities; a unique atomic thread identity prevents cross-thread disposal from changing another context.

    Scopes restore in reverse order on their originating thread. Early or cross-thread disposal is ignored and may be retried after children finish. Values do not flow across async continuations. No locks or per-scope heap objects are used; runtime thread/type initialization is distinct from the warmed allocation contract.

    All 16 dedicated native cases pass: default/null/reference values, nested and equal-valued scopes, copies and boxing, exception unwinding, 64-level restoration, real 1/2/4-worker isolation and foreign disposal. The warmed nested-scope allocation assertion first runs the existing positive allocation control; the native instrument detects the control and the subject passes without skipping.

    The complete current runtime cohort passes 824/824 with stable inputs and healthy native process receipts. CI/review remains pending. The PR is configured to close this issue after merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions