Skip to content

Commit

Permalink
Merge pull request #60 from wks/feature/fork
Browse files Browse the repository at this point in the history
Remove coordinator and support forking
  • Loading branch information
wks authored Apr 9, 2024
2 parents 7d68ea9 + 491e1e7 commit f014f8b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
6 changes: 4 additions & 2 deletions internal/mmtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ typedef uint32_t MMTk_AllocationSemantics;

#define MMTK_MIN_OBJ_ALIGN 8

#define MMTK_GC_THREAD_KIND_CONTROLLER 0

#define MMTK_GC_THREAD_KIND_WORKER 1

typedef struct RubyBindingOptions {
Expand Down Expand Up @@ -146,6 +144,10 @@ bool mmtk_will_never_move(MMTk_ObjectReference object);

void mmtk_initialize_collection(MMTk_VMThread tls);

void mmtk_prepare_to_fork(void);

void mmtk_after_fork(MMTk_VMThread tls);

void mmtk_enable_collection(void);

void mmtk_disable_collection(void);
Expand Down
4 changes: 4 additions & 0 deletions internal/mmtk_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ void rb_mmtk_pin_object(VALUE obj);
void rb_mmtk_assert_is_pinned(VALUE obj);
void rb_mmtk_pin_array_buffer(VALUE array, volatile VALUE *stack_slot);

// Forking support
void rb_mmtk_shutdown_gc_threads(void);
void rb_mmtk_respawn_gc_threads(void);

// MMTk-specific Ruby module (GC::MMTk)
void rb_mmtk_define_gc_mmtk_module(void);
VALUE rb_mmtk_plan_name(VALUE _);
Expand Down
14 changes: 14 additions & 0 deletions mmtk_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,20 @@ rb_mmtk_pin_array_buffer(VALUE array, volatile VALUE *stack_slot)
}
}

////////////////////////////////////////////////////////////////////////////////
// Forking support
////////////////////////////////////////////////////////////////////////////////
void
rb_mmtk_shutdown_gc_threads(void)
{
mmtk_prepare_to_fork();
}

void rb_mmtk_respawn_gc_threads(void)
{
mmtk_after_fork(GET_THREAD());
}

////////////////////////////////////////////////////////////////////////////////
// MMTk-specific Ruby module (GC::MMTk)
////////////////////////////////////////////////////////////////////////////////
Expand Down
18 changes: 10 additions & 8 deletions process.c
Original file line number Diff line number Diff line change
Expand Up @@ -1677,11 +1677,21 @@ static void
before_fork_ruby(void)
{
before_exec();
#if USE_MMTK
if (rb_mmtk_enabled_p()) {
rb_mmtk_shutdown_gc_threads();
}
#endif
}

static void
after_fork_ruby(rb_pid_t pid)
{
#if USE_MMTK
if (rb_mmtk_enabled_p()) {
rb_mmtk_respawn_gc_threads();
}
#endif
rb_threadptr_pending_interrupt_clear(GET_THREAD());
if (pid == 0) {
// child
Expand Down Expand Up @@ -9236,15 +9246,7 @@ InitVM_process(void)
#endif

rb_define_singleton_method(rb_mProcess, "exec", f_exec, -1);
#if USE_MMTK
// FIXME: MMTk currently doesn't support forking, so Process will not respond to :fork for not.
// We should add necessary mechanism to mmtk-core in order to support forking.
if (!rb_mmtk_enabled_p()) {
#endif
rb_define_singleton_method(rb_mProcess, "fork", rb_f_fork, 0);
#if USE_MMTK
}
#endif
rb_define_singleton_method(rb_mProcess, "spawn", rb_f_spawn, -1);
rb_define_singleton_method(rb_mProcess, "exit!", rb_f_exit_bang, -1);
rb_define_singleton_method(rb_mProcess, "exit", f_exit, -1);
Expand Down
5 changes: 3 additions & 2 deletions test/ruby/test_process.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,8 @@ def test_daemon_detached
end
end

if File.directory?("/proc/self/task") && /netbsd[a-z]*[1-6]/ !~ RUBY_PLATFORM
# Note: MMTk has many GC worker threads, and they will be visible in /proc/self/task.
if File.directory?("/proc/self/task") && /netbsd[a-z]*[1-6]/ !~ RUBY_PLATFORM && !defined?(GC::MMTk)
def test_daemon_no_threads
pid, data = IO.popen("-", "r+") do |f|
break f.pid, f.readlines if f
Expand All @@ -1931,7 +1932,7 @@ def test_daemon_no_threads
assert_include(1..2, data.size, bug4920)
assert_not_include(data.map(&:to_i), pid)
end
else # darwin
else # darwin or MMTk
def test_daemon_no_threads
data = EnvUtil.timeout(3) do
IO.popen("-") do |f|
Expand Down

0 comments on commit f014f8b

Please sign in to comment.