Skip to content

Atomic reference counts under -mm=rc: --atomic-rc, Shared.share and @atomic class - #786

Merged
ASDAlexander77 merged 4 commits into
mainfrom
atomic-rc
Nov 8, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
atomic-rc

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Under -mm=rc, changing a count is a load, an add and a store. When two threads take or drop references to the same object at once, one of the updates is lost, so the object is either freed while still held or never freed. This PR gives rc three ways to change counts atomically.

What changes

  • --atomic-rc (needs -mm=rc): every count change in the module is atomic. A retain is a relaxed add; a release is a subtract with release ordering, and the thread that drops the last reference takes an acquire fence before freeing the block. The flag is part of the JIT cache key.

  • A shared bit in the header word. The top bit of a counted block's header means other threads may hold it. The count helpers test the word once:

    • not negative: only this thread holds the block, and it counts plainly as before;
    • negative: the block is shared or immortal, and a shared block takes the atomic path.

    Shared.count reads the count without the bit. On the ray tracer this costs about 4.5%, whether or not anything is marked.

  • Shared.share(v) (ts.MarkShared) sets the bit on every counted block v reaches: fields, elements, an array's data block, a closure's capture box and cells, and whatever an any, an interface or a union holds. The last group is found through a new type-descriptor slot, TYPE_DESCR_SHARE. A block that is already shared stops the walk, which ends it on a cycle, and immortal blocks are left alone. The op is erased under every model except rc.

  • @atomic class marks each new object once its constructor has run, and marks every value stored into one of its fields before the store. A class extending an @atomic class is @atomic too.

Compatibility

The type-descriptor record gains a slot, so code built before this change can't be mixed with code built after it. Every default-library build has to be rebuilt.

Limits (documented in docs/memory-models.md)

  • Shared.share marks the object graph as it is now. A value stored into a shared object later must be shared before another thread can see it.
  • The @atomic store barrier doesn't see stores through an interface or a base type that isn't @atomic, or an element added to an array that a field holds (obj.items.push(x)).
  • --atomic-rc only affects the code compiled with it. The default library, and any other library built without it, still counts an unmarked block plainly.
  • Keeping the objects' contents consistent is still the program's job (@atomic variables, Atomics).

Testing

  • test-compile-atomic-rc: a C++ host runs 8 threads × 200,000 calls into an rc DLL, all taking and dropping references to one shared object graph. The count afterwards must equal the count before.
    • Exact with --atomic-rc, with Shared.share, with @atomic class, and with marked objects under --atomic-rc.
    • A plain build is reported only; it drifts or corrupts the heap, which shows the test can see the race.
    • When a default library is built, the same check runs against its DLL with a shared RegExp and strings from split, calling RegExp.test on every thread. Exact with Shared.share, @atomic, and both combined.
  • 00shared_share runs under gc, rc and none, in both JIT and compile tests. It covers a cycle, a literal, every kind of field, and an @atomic subclass.
  • Six existing rc programs, plus 00shared_share, also run with --atomic-rc.
  • Full suite against a rebuilt rc default library: 6,707 of 6,709 pass. test-own-facts-objects and test-own-keepers-objects failed in the parallel run and pass when rerun alone. They flaked the same way on the A library tslang built no longer exports the heap entry points #784 branch, which didn't have these changes.

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits November 8, 2026 15:49
A retain is a relaxed atomic add; a release is an atomic subtract with
release ordering, and the thread that drops the last reference takes an
acquire fence before the block is freed. Without the flag the helpers
are unchanged. The flag is part of the JIT cache key, and asking for it
without -mm=rc is an error.

test-compile-atomic-rc: eight threads of a host take and drop references
to one object of an rc library; the count afterwards is exactly what it
was (without the flag it drifts, 1 -> 7775). test-runner takes
-atomic-rc, and six rc programs run with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The top bit of a counted block's header says other threads may hold
references to it. The count helpers test the word once - not negative is
a block only this thread holds, which counts as before with a plain load,
add and store; negative is immortal or shared, and a shared block takes
an atomic add, or an atomic subtract with release ordering and an acquire
fence before the last reference frees it. --atomic-rc sends every mortal
block down the shared path. Shared.count reads the count without the bit.

Nothing sets the bit yet. The ray tracer under -mm=rc: 106 -> 111 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Shared.share(v) (ts.MarkShared) sets the shared bit of every counted
block v reaches - fields, elements, an array's data block, a closure's
capture box and cells, and through a new descriptor slot
(TYPE_DESCR_SHARE) whatever an any, an interface or a union holds. A
block already shared stops the walk, which ends it on a cycle; an
immortal one is left alone. Erased under every model but rc.

An @atomic class marks each new object once its constructor has run, and
a value stored into one of its fields is marked before the store. A class
extending one is @atomic too.

The descriptor record grows a slot, so code built before this cannot be
mixed with code built after it: the default library has to be rebuilt.

test-compile-atomic-rc builds the library three more ways - marked by
Shared.share, its root an @atomic class, and marked under --atomic-rc -
each exact after eight threads; 00shared_share covers a cycle, a literal
and every kind of field on one thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a default library has been built (DEFAULT_LIB_CANDIDATES,
DEFAULT_LIB_PATH), the test also links a library against its DLL whose
shared objects are the default library's - a RegExp and strings split
made - with RegExp.test running in that DLL on eight threads at once.
The descriptor whose share routine marks them is the default library's,
so a library built before the share slot would show here. Shared.share,
@atomic and both under --atomic-rc keep the count exact; without a
default library this part is skipped.

--atomic-rc alone is only reported: the default library is built
without it, and its own code still counts an unmarked block plainly.
memory-models.md says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit eadd831 into main Nov 8, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the atomic-rc branch November 8, 2026 16:24
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