From 56ebc0e857435b3111130260cfef77ef991e1cf0 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Fri, 11 Oct 2024 13:47:22 -0400 Subject: [PATCH] Implement rb_gc_impl_ractor_cache_free --- gc/mmtk.c | 23 ++++++++++++++--------- gc/mmtk.h | 2 ++ gc/mmtk/src/api.rs | 8 ++++++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/gc/mmtk.c b/gc/mmtk.c index 594db224a9107c..be40f80b3038c5 100644 --- a/gc/mmtk.c +++ b/gc/mmtk.c @@ -9,6 +9,7 @@ #include "gc/gc_impl.h" #include "gc/mmtk.h" +#include "ccan/list/list.h" #include "darray.h" struct objspace { @@ -27,7 +28,7 @@ struct objspace { struct MMTk_final_job *finalizer_jobs; rb_postponed_job_handle_t finalizer_postponed_job; - struct MMTk_ractor_cache *ractor_caches; + struct ccan_list_head ractor_caches; unsigned long live_ractor_cache_count; pthread_mutex_t mutex; @@ -40,7 +41,7 @@ struct objspace { }; struct MMTk_ractor_cache { - struct MMTk_ractor_cache *next_ractor_cache; + struct ccan_list_node list_node; MMTk_Mutator *mutator; bool gc_mutator_p; @@ -187,12 +188,11 @@ rb_mmtk_get_mutators(void (*visit_mutator)(MMTk_Mutator *mutator, void *data), v { struct objspace *objspace = rb_gc_get_objspace(); - struct MMTk_ractor_cache *ractor_cache = objspace->ractor_caches; + struct MMTk_ractor_cache *ractor_cache; RUBY_ASSERT(ractor_cache != NULL); - while (ractor_cache != NULL) { + ccan_list_for_each(&objspace->ractor_caches, ractor_cache, list_node) { visit_mutator(ractor_cache->mutator, data); - ractor_cache = ractor_cache->next_ractor_cache; } } @@ -429,6 +429,8 @@ rb_gc_impl_objspace_init(void *objspace_ptr) objspace->finalizer_table = st_init_numtable(); objspace->finalizer_postponed_job = rb_postponed_job_preregister(0, gc_run_finalizers, objspace); + ccan_list_head_init(&objspace->ractor_caches); + objspace->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; objspace->cond_world_stopped = (pthread_cond_t)PTHREAD_COND_INITIALIZER; objspace->cond_world_started = (pthread_cond_t)PTHREAD_COND_INITIALIZER; @@ -450,8 +452,7 @@ rb_gc_impl_ractor_cache_alloc(void *objspace_ptr, void *ractor) objspace->live_ractor_cache_count++; struct MMTk_ractor_cache *cache = malloc(sizeof(struct MMTk_ractor_cache)); - cache->next_ractor_cache = objspace->ractor_caches; - objspace->ractor_caches = cache; + ccan_list_add(&objspace->ractor_caches, &cache->list_node); cache->mutator = mmtk_bind_mutator(cache); @@ -459,13 +460,17 @@ rb_gc_impl_ractor_cache_alloc(void *objspace_ptr, void *ractor) } void -rb_gc_impl_ractor_cache_free(void *objspace_ptr, void *cache) +rb_gc_impl_ractor_cache_free(void *objspace_ptr, void *cache_ptr) { - // TODO: implement mmtk_destroy_mutator struct objspace *objspace = objspace_ptr; + struct MMTk_ractor_cache *cache = cache_ptr; + + ccan_list_del(&cache->list_node); RUBY_ASSERT(objspace->live_ractor_cache_count > 1); objspace->live_ractor_cache_count--; + + mmtk_destroy_mutator(cache->mutator); } void rb_gc_impl_set_params(void *objspace_ptr) { } diff --git a/gc/mmtk.h b/gc/mmtk.h index 93ebdf70478324..1f220d9643e9a7 100644 --- a/gc/mmtk.h +++ b/gc/mmtk.h @@ -102,6 +102,8 @@ void mmtk_initialize_collection(MMTk_VMThread tls); MMTk_Mutator *mmtk_bind_mutator(MMTk_VMMutatorThread tls); +void mmtk_destroy_mutator(MMTk_Mutator *mutator); + void mmtk_handle_user_collection_request(MMTk_VMMutatorThread tls); void mmtk_set_gc_enabled(bool enable); diff --git a/gc/mmtk/src/api.rs b/gc/mmtk/src/api.rs index 3c51ba60c24f10..5a8ef5db04bc58 100644 --- a/gc/mmtk/src/api.rs +++ b/gc/mmtk/src/api.rs @@ -81,6 +81,14 @@ pub extern "C" fn mmtk_bind_mutator(tls: VMMutatorThread) -> *mut RubyMutator { Box::into_raw(memory_manager::bind_mutator(mmtk(), tls)) } +#[no_mangle] +pub extern "C" fn mmtk_destroy_mutator(mutator: *mut RubyMutator) { + // notify mmtk-core about destroyed mutator + memory_manager::destroy_mutator(unsafe { &mut *mutator }); + // turn the ptr back to a box, and let Rust properly reclaim it + let _ = unsafe { Box::from_raw(mutator) }; +} + // =============== GC =============== #[no_mangle]