Add flag to check if currently doing GC

This commit is contained in:
2025-09-08 15:55:18 -07:00
parent 06d6faf350
commit b18b59eeb0
3 changed files with 64 additions and 12 deletions

View File

@ -26,19 +26,24 @@
(atomic_fetch_sub_explicit(obj, 1, memory_order_relaxed))
# define lock_entry_mtx(obj) (lock_mutex(&obj->weak_ref->mtx))
# define unlock_entry_mtx(obj) (unlock_mutex(&obj->weak_ref->mtx))
# define store_flag_maybe_atomic(obj, value) \
(atomic_store_explicit(obj, value, memory_order_relaxed))
#else
# define lock_mutex(m) true
# define unlock_mutex(m) true
# define store_wr_count(obj, c) (*(obj) = (c))
# define inc_wr_count(obj) ((*(obj))++)
# define dec_wr_count(obj) ((*(obj))--)
# define lock_entry_mtx(obj) true
# define unlock_entry_mtx(obj) true
# define lock_mutex(m) true
# define unlock_mutex(m) true
# define store_wr_count(obj, c) (*(obj) = (c))
# define inc_wr_count(obj) ((*(obj))++)
# define dec_wr_count(obj) ((*(obj))--)
# define lock_entry_mtx(obj) true
# define unlock_entry_mtx(obj) true
# define store_flag_maybe_atomic(obj, value) (*(obj) = (value))
#endif
/**
* Default context to use for functions that do not take a context. You must
* initialize this before use.
*
* > The default value of this is NULL.
*/
RefcountContext *refcount_default_context = NULL;
@ -85,6 +90,7 @@ refcount_make_context(size_t entry_offset,
ctx->static_objects = NULL;
ctx->gc_roots = NULL;
ctx->doing_gc = false;
return ctx;
}
@ -689,6 +695,7 @@ ptrdiff_t refcount_context_garbage_collect(RefcountContext *ctx) {
if (!lock_mutex(&ctx->gr_mtx)) {
return -1;
}
store_flag_maybe_atomic(&ctx->doing_gc, true);
ptrdiff_t total_cleared = 0;
RefcountList *root = ctx->gc_roots;
while (root) {
@ -698,6 +705,7 @@ ptrdiff_t refcount_context_garbage_collect(RefcountContext *ctx) {
}
total_cleared += res;
}
store_flag_maybe_atomic(&ctx->doing_gc, false);
unlock_mutex(&ctx->gr_mtx);
return total_cleared;
}