Modernize the concurrent-ruby-ext C extension#1105
Draft
joshuay03 wants to merge 1 commit into
Draft
Conversation
5f72004 to
ce0adbd
Compare
Member
|
Sounds great, is it ready for review? |
Contributor
Author
Not yet, I'll ping you when it is. This is hopefully the first PR of a few to modernize the extension and lay the groundwork for opt-in Ractor support across the atomic primitives. |
ce0adbd to
3c5abdc
Compare
Replace the per-platform CAS and memory-barrier code in `CAtomicReference`, `CAtomicBoolean`, and `CAtomicFixnum` with the helpers in `ruby/atomic.h`, and switch the three classes from the old `DATA_PTR` / `RUBY_NEVER_FREE` pattern to `TypedData_Make_Struct` with proper `dmark`, `dfree`, `dsize`, and (on Ruby >= 2.7) `dcompact`. All atomic primitives (load, set, CAS, exchange) are `static inline` in `atomic_reference.h` and shared across the three classes via a `CR_DEFINE_ATOMIC_DATA_TYPE` macro that keeps the three `rb_data_type_t` definitions in lock-step. Stores publish via `RUBY_ATOMIC_VALUE_SET` (or `__atomic_store_n` on Ruby < 4.0) so the "java volatile" semantics the docstring advertises are preserved across the modernization, and the corresponding `RB_OBJ_WRITTEN` announces the write barrier to the GC. `AtomicReference#get_and_set` now uses a single atomic exchange instead of a CAS retry loop. Each primitive prefers the `ruby/atomic.h` helper when its specific macro is defined and falls back to GCC/Clang `__atomic_*` builtins otherwise, since `RUBY_ATOMIC_PTR_LOAD` only landed in Ruby 3.3 and `RUBY_ATOMIC_VALUE_SET` in 4.0. GC compaction support is gated on `RUBY_API_VERSION_CODE >= 20700` via the `CR_GC_COMPACTION` macro; older Rubies fall back to `rb_gc_mark`. `extconf.rb` drops the `libkern/OSAtomic.h` probe and the `-march` flags, which no longer matter once we're on `ruby/atomic.h` or the compiler atomic builtins. No behavioral change to any public API; the spec suite passes unchanged, including the atomic specs under `GC.stress`.
3c5abdc to
3556c35
Compare
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.
Replace the per-platform CAS and memory-barrier code in
CAtomicReference,CAtomicBoolean, andCAtomicFixnumwith the abstractions inruby/atomic.h, and switch the three classes from the oldDATA_PTR/RUBY_NEVER_FREEpattern toTypedData_Make_Structwithdmark,dfree,dsize, and (on Ruby >= 2.7)dcompact. Stores go throughRB_OBJ_WRITEand successful CAS calls fireRB_OBJ_WRITTEN, so write barriers are honored and GC compaction is supported.Two version-gated fallbacks keep us building back to Ruby 2.3:
ruby/atomic.honly became a public extension header in Ruby 3.0, soextconf.rbprobes for it viahave_header. When it isn't available the C code falls back to GCC/Clang__atomic_*builtins.CR_GC_COMPACTIONmacro gated onRUBY_API_VERSION_CODEselects betweenrb_gc_mark_movable/rb_gc_locationplus thedcompactslot and a plainrb_gc_markon older Rubies.extconf.rbalso drops thelibkern/OSAtomic.hprobe and the-marchflags, which no longer matter once we're onruby/atomic.hor the compiler atomic builtins.No behavioral change to any public API; the spec suite passes unchanged, including the atomic specs under
GC.stress.