Repository navigation
Atomic reference counts under -mm=rc: --atomic-rc, Shared.share and @atomic class - #786
Merged
Merged
Conversation
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>
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.
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 givesrcthree 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:
Shared.countreads 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 blockvreaches: fields, elements, an array's data block, a closure's capture box and cells, and whatever anany, 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 exceptrc.@atomic classmarks 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@atomicclass is@atomictoo.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.sharemarks the object graph as it is now. A value stored into a shared object later must be shared before another thread can see it.@atomicstore 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-rconly affects the code compiled with it. The default library, and any other library built without it, still counts an unmarked block plainly.@atomicvariables,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.--atomic-rc, withShared.share, with@atomic class, and with marked objects under--atomic-rc.RegExpand strings fromsplit, callingRegExp.teston every thread. Exact withShared.share,@atomic, and both combined.00shared_shareruns under gc, rc and none, in both JIT and compile tests. It covers a cycle, a literal, every kind of field, and an@atomicsubclass.00shared_share, also run with--atomic-rc.test-own-facts-objectsandtest-own-keepers-objectsfailed 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