Skip to content

Commit 65b74c3

Browse files
Merge pull request #76 from ruby/mvh-ruby-debug-ractor
Introduce support for ractor_belonging.
2 parents 5a6cd4e + d832004 commit 65b74c3

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

gc/mmtk/mmtk.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@
1616
#include <sys/sysctl.h>
1717
#endif
1818

19+
#ifndef VM_CHECK_MODE
20+
# define VM_CHECK_MODE RUBY_DEBUG
21+
#endif
22+
23+
// From ractor_core.h
24+
#ifndef RACTOR_CHECK_MODE
25+
# define RACTOR_CHECK_MODE (VM_CHECK_MODE || RUBY_DEBUG) && (SIZEOF_UINT64_T == SIZEOF_VALUE)
26+
#endif
27+
28+
#if RACTOR_CHECK_MODE
29+
# define RVALUE_SUFFIX_SIZE sizeof(VALUE)
30+
void rb_ractor_setup_belonging(VALUE obj);
31+
#else
32+
# define RVALUE_SUFFIX_SIZE 0
33+
#endif
34+
1935
struct objspace {
2036
bool measure_gc_time;
2137
bool gc_stress;
@@ -557,7 +573,11 @@ void *
557573
rb_gc_impl_objspace_alloc(void)
558574
{
559575
MMTk_Builder *builder = rb_mmtk_builder_init();
560-
mmtk_init_binding(builder, NULL, &ruby_upcalls);
576+
MMTk_RubyBindingOptions binding_options = {
577+
.ractor_check_mode = RACTOR_CHECK_MODE != 0,
578+
.suffix_size = RVALUE_SUFFIX_SIZE,
579+
};
580+
mmtk_init_binding(builder, &binding_options, &ruby_upcalls);
561581

562582
return calloc(1, sizeof(struct objspace));
563583
}
@@ -885,15 +905,16 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags
885905
mmtk_handle_user_collection_request(ractor_cache, false, false);
886906
}
887907

888-
alloc_size += sizeof(VALUE);
908+
// Layout: [hidden size header (sizeof(VALUE))][payload (alloc_size)][suffix (RVALUE_SUFFIX_SIZE)]
909+
alloc_size += sizeof(VALUE) + RVALUE_SUFFIX_SIZE;
889910

890911
VALUE *alloc_obj = (VALUE *)rb_mmtk_alloc_fast_path(objspace, ractor_cache, alloc_size);
891912
if (!alloc_obj) {
892913
alloc_obj = mmtk_alloc(ractor_cache->mutator, alloc_size, MMTk_MIN_OBJ_ALIGN, 0, MMTK_ALLOCATION_SEMANTICS_DEFAULT);
893914
}
894915

895916
alloc_obj++;
896-
alloc_obj[-1] = alloc_size - sizeof(VALUE);
917+
alloc_obj[-1] = alloc_size - sizeof(VALUE) - RVALUE_SUFFIX_SIZE;
897918
alloc_obj[0] = flags;
898919
alloc_obj[1] = klass;
899920

@@ -905,6 +926,10 @@ rb_gc_impl_new_obj(void *objspace_ptr, void *cache_ptr, VALUE klass, VALUE flags
905926

906927
objspace->total_allocated_objects++;
907928

929+
#if RACTOR_CHECK_MODE
930+
rb_ractor_setup_belonging((VALUE)alloc_obj);
931+
#endif
932+
908933
return (VALUE)alloc_obj;
909934
}
910935

gc/mmtk/mmtk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ bool mmtk_is_reachable(MMTk_ObjectReference object);
9595
MMTk_Builder *mmtk_builder_default(void);
9696

9797
void mmtk_init_binding(MMTk_Builder *builder,
98-
const struct MMTk_RubyBindingOptions *_binding_options,
98+
const struct MMTk_RubyBindingOptions *binding_options,
9999
const struct MMTk_RubyUpcalls *upcalls);
100100

101101
void mmtk_initialize_collection(MMTk_VMThread tls);

gc/mmtk/src/api.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub extern "C" fn mmtk_builder_default() -> *mut MMTKBuilder {
181181
#[no_mangle]
182182
pub unsafe extern "C" fn mmtk_init_binding(
183183
builder: *mut MMTKBuilder,
184-
_binding_options: *const RubyBindingOptions,
184+
binding_options: *const RubyBindingOptions,
185185
upcalls: *const RubyUpcalls,
186186
) {
187187
crate::MUTATOR_THREAD_PANIC_HANDLER
@@ -191,10 +191,7 @@ pub unsafe extern "C" fn mmtk_init_binding(
191191
crate::set_panic_hook();
192192

193193
let builder: Box<MMTKBuilder> = unsafe { Box::from_raw(builder) };
194-
let binding_options = RubyBindingOptions {
195-
ractor_check_mode: false,
196-
suffix_size: 0,
197-
};
194+
let binding_options = unsafe { (*binding_options).clone() };
198195
let mmtk_boxed = mmtk_init(&builder);
199196
let mmtk_static = Box::leak(Box::new(mmtk_boxed));
200197

0 commit comments

Comments
 (0)