From d8aa289af486309861acdf291bfbdedce2c328b1 Mon Sep 17 00:00:00 2001 From: Yorick Hardy Date: Fri, 15 Nov 2024 22:59:46 +0200 Subject: [PATCH] gc: add a function to force the collector to run This requires adding a "forced" stage for the collector, which is the initial stage for a forced collection. Thereafter, the collector continues to the usual stages of collection. --- gc.c | 8 ++++++++ include/cyclone/runtime.h | 5 +++++ include/cyclone/types.h | 3 ++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/gc.c b/gc.c index 434c2bdb..15b39c0c 100644 --- a/gc.c +++ b/gc.c @@ -2534,6 +2534,14 @@ void gc_collector() ck_pr_cas_int(&gc_stage, STAGE_SWEEPING, STAGE_RESTING); } +void gc_force(void) +{ + /* try to force the collector to run */ + ck_pr_cas_int(&gc_stage, STAGE_RESTING, STAGE_FORCING); + /* if the collector thread was idle, then begin collecting */ + ck_pr_cas_int(&gc_stage, STAGE_FORCING, STAGE_CLEAR_OR_MARKING); +} + void *collector_main(void *arg) { int stage; diff --git a/include/cyclone/runtime.h b/include/cyclone/runtime.h index 432be667..860c2dbb 100644 --- a/include/cyclone/runtime.h +++ b/include/cyclone/runtime.h @@ -52,6 +52,11 @@ void GC(void *, closure, object *, int); */ void gc_init_heap(long heap_size); +/** + * \ingroup gc_force + */ +void gc_force(void); + /** * \defgroup prim Primitives * @brief Built-in Scheme functions provided by the runtime library diff --git a/include/cyclone/types.h b/include/cyclone/types.h index c37ae282..32abdaee 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -264,7 +264,7 @@ typedef enum { STATUS_ASYNC, STATUS_SYNC1, STATUS_SYNC2 /** Stages of the Major GC's collector thread */ typedef enum { STAGE_CLEAR_OR_MARKING, STAGE_TRACING //, STAGE_REF_PROCESSING - , STAGE_SWEEPING, STAGE_RESTING + , STAGE_SWEEPING, STAGE_RESTING, STAGE_FORCING } gc_stage_type; // Constant colors are defined here. @@ -377,6 +377,7 @@ struct gc_thread_data_t { /* GC prototypes */ void gc_initialize(void); +void gc_force(void); void gc_add_new_unrunning_mutator(gc_thread_data * thd); void gc_add_mutator(gc_thread_data * thd); void gc_remove_mutator(gc_thread_data * thd);