Skip to content

Modernize the concurrent-ruby-ext C extension#1105

Draft
joshuay03 wants to merge 1 commit into
masterfrom
modernize-c-ext
Draft

Modernize the concurrent-ruby-ext C extension#1105
joshuay03 wants to merge 1 commit into
masterfrom
modernize-c-ext

Conversation

@joshuay03

Copy link
Copy Markdown
Contributor

Replace the per-platform CAS and memory-barrier code in CAtomicReference, CAtomicBoolean, and CAtomicFixnum with the abstractions in ruby/atomic.h, and switch the three classes from the old DATA_PTR / RUBY_NEVER_FREE pattern to TypedData_Make_Struct with dmark, dfree, dsize, and (on Ruby >= 2.7) dcompact. Stores go through RB_OBJ_WRITE and successful CAS calls fire RB_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.h only became a public extension header in Ruby 3.0, so extconf.rb probes for it via have_header. When it isn't available the C code falls back to GCC/Clang __atomic_* builtins.
  • GC compaction APIs were added in Ruby 2.7. A CR_GC_COMPACTION macro gated on RUBY_API_VERSION_CODE selects between rb_gc_mark_movable / rb_gc_location plus the dcompact slot and a plain rb_gc_mark on older Rubies.

extconf.rb also 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.

@joshuay03 joshuay03 self-assigned this Jun 14, 2026
@eregon

eregon commented Jun 19, 2026

Copy link
Copy Markdown
Member

Sounds great, is it ready for review?

@joshuay03

Copy link
Copy Markdown
Contributor Author

Sounds great, is it ready for review?

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.

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`.
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.

2 participants