Skip to content

Commit 7a49c17

Browse files
committed
Stop sweep thread when compiling JIT code
1 parent 3648bd7 commit 7a49c17

8 files changed

Lines changed: 39 additions & 0 deletions

File tree

gc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ typedef struct gc_function_map {
610610
void (*start)(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact);
611611
bool (*during_gc_p)(void *objspace_ptr);
612612
void (*prepare_heap)(void *objspace_ptr);
613+
void (*wait_for_background_sweep)(void *objspace_ptr);
613614
void (*gc_enable)(void *objspace_ptr);
614615
void (*gc_disable)(void *objspace_ptr, bool finish_current_gc);
615616
bool (*gc_enabled_p)(void *objspace_ptr);
@@ -789,6 +790,7 @@ ruby_modular_gc_init(void)
789790
load_modular_gc_func(start);
790791
load_modular_gc_func(during_gc_p);
791792
load_modular_gc_func(prepare_heap);
793+
load_modular_gc_func(wait_for_background_sweep);
792794
load_modular_gc_func(gc_enable);
793795
load_modular_gc_func(gc_disable);
794796
load_modular_gc_func(gc_enabled_p);
@@ -877,6 +879,7 @@ ruby_modular_gc_init(void)
877879
# define rb_gc_impl_start rb_gc_functions.start
878880
# define rb_gc_impl_during_gc_p rb_gc_functions.during_gc_p
879881
# define rb_gc_impl_prepare_heap rb_gc_functions.prepare_heap
882+
# define rb_gc_impl_wait_for_background_sweep rb_gc_functions.wait_for_background_sweep
880883
# define rb_gc_impl_gc_enable rb_gc_functions.gc_enable
881884
# define rb_gc_impl_gc_disable rb_gc_functions.gc_disable
882885
# define rb_gc_impl_gc_enabled_p rb_gc_functions.gc_enabled_p
@@ -5846,6 +5849,12 @@ rb_gc_before_fork(void)
58465849
rb_gc_impl_before_fork(rb_gc_get_objspace());
58475850
}
58485851

5852+
void
5853+
rb_gc_wait_for_background_sweep(void)
5854+
{
5855+
rb_gc_impl_wait_for_background_sweep(rb_gc_get_objspace());
5856+
}
5857+
58495858
void
58505859
rb_gc_after_fork(rb_pid_t pid)
58515860
{

gc/default/default.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11368,6 +11368,15 @@ gc_malloc_allocations(VALUE self)
1136811368
}
1136911369
#endif
1137011370

11371+
void
11372+
rb_gc_impl_wait_for_background_sweep(void *objspace_ptr)
11373+
{
11374+
#if USE_PARALLEL_SWEEP
11375+
rb_objspace_t *objspace = objspace_ptr;
11376+
wait_for_background_sweeping_to_finish(objspace, true, false, "impl_wait_for_background_sweep");
11377+
#endif
11378+
}
11379+
1137111380
void
1137211381
rb_gc_impl_before_fork(void *objspace_ptr)
1137311382
{

gc/gc_impl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ GC_IMPL_FN void rb_gc_impl_ractor_cache_free(void *objspace_ptr, void *cache);
4747
GC_IMPL_FN void rb_gc_impl_start(void *objspace_ptr, bool full_mark, bool immediate_mark, bool immediate_sweep, bool compact);
4848
GC_IMPL_FN bool rb_gc_impl_during_gc_p(void *objspace_ptr);
4949
GC_IMPL_FN void rb_gc_impl_prepare_heap(void *objspace_ptr);
50+
GC_IMPL_FN void rb_gc_impl_wait_for_background_sweep(void *objspace_ptr);
5051
GC_IMPL_FN void rb_gc_impl_gc_enable(void *objspace_ptr);
5152
GC_IMPL_FN void rb_gc_impl_gc_disable(void *objspace_ptr, bool finish_current_gc);
5253
GC_IMPL_FN bool rb_gc_impl_gc_enabled_p(void *objspace_ptr);

gc/mmtk/mmtk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,12 @@ rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr)
13911391
gc_run_finalizers(objspace);
13921392
}
13931393

1394+
void
1395+
rb_gc_impl_wait_for_background_sweep(void *objspace_ptr)
1396+
{
1397+
// MMTk has no background sweep thread.
1398+
}
1399+
13941400
// Forking
13951401

13961402
void

gc/wbcheck/wbcheck.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,12 @@ rb_gc_impl_shutdown_call_finalizer(void *objspace_ptr)
17351735
RB_GC_VM_UNLOCK(lev);
17361736
}
17371737

1738+
void
1739+
rb_gc_impl_wait_for_background_sweep(void *objspace_ptr)
1740+
{
1741+
// Stub implementation
1742+
}
1743+
17381744
// Forking
17391745
void
17401746
rb_gc_impl_before_fork(void *objspace_ptr)

ruby.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,9 @@ ruby_opt_init(ruby_cmdline_options_t *opt)
18331833

18341834
// Register JIT-optimized builtin CMEs before the prelude, which may
18351835
// redefine core methods (e.g. Kernel.prepend via bundler/setup).
1836+
#if USE_YJIT || USE_ZJIT
1837+
rb_gc_wait_for_background_sweep();
1838+
#endif
18361839
#if USE_YJIT
18371840
rb_yjit_init_builtin_cmes();
18381841
#endif
@@ -1877,6 +1880,9 @@ ruby_opt_init(ruby_cmdline_options_t *opt)
18771880
ruby_init_prelude();
18781881

18791882
// Enable JITs after ruby_init_prelude() to avoid JITing prelude code.
1883+
#if USE_YJIT || USE_ZJIT
1884+
rb_gc_wait_for_background_sweep();
1885+
#endif
18801886
#if USE_YJIT
18811887
rb_yjit_init(opt->yjit);
18821888
#endif

yjit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ rb_yjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit
398398
{
399399
RB_VM_LOCKING() {
400400
rb_vm_barrier();
401+
rb_gc_wait_for_background_sweep();
401402

402403
// Compile a block version starting at the current instruction
403404
uint8_t *rb_yjit_iseq_gen_entry_point(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception); // defined in Rust

zjit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ rb_zjit_compile_iseq(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit
3939
{
4040
RB_VM_LOCKING() {
4141
rb_vm_barrier();
42+
rb_gc_wait_for_background_sweep();
4243

4344
// Compile a block version starting at the current instruction
4445
uint8_t *rb_zjit_iseq_gen_entry_point(const rb_iseq_t *iseq, rb_execution_context_t *ec, bool jit_exception); // defined in Rust

0 commit comments

Comments
 (0)