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

@ -66,6 +66,9 @@ typedef struct RefcountContext {
#ifdef REFCOUNT_HAS_THREADS
mtx_t so_mtx; //<! Mutex protecting static_objects.
mtx_t gr_mtx; //<! Mutex protecting gc_roots.
_Atomic bool doing_gc;
#else
bool doing_gc;
#endif
} RefcountContext;
@ -195,6 +198,20 @@ static inline void refcount_context_unref_as_callback(void *obj, void *ctx) {
ptrdiff_t refcount_context_garbage_collect(RefcountContext *ctx);
/**
* Test weather a give context is currently performing a GC operation.
* @param ctx The #RefcountContext
* @return True if the context is currently doing a GC operation, false
* otherwise
*/
static bool refcount_context_is_doing_gc(const RefcountContext *ctx) {
#ifdef REFCOUNT_HAS_THREADS
return atomic_load_explicit(&ctx->doing_gc, memory_order_relaxed);
#else
return ctx->doing_gc;
#endif
}
RefcountWeakref *refcount_context_make_weakref(const RefcountContext *ctx,
void *obj);
@ -320,7 +337,7 @@ static inline void refcount_unref_as_callback(void *obj) {
}
/**
* Same as #refcount_context_garbage_collect, but only operates on the globa
* Same as #refcount_context_garbage_collect, but only operates on the global
* context.
* @return The number of object's freed, or -1 if an error occurred
*/
@ -328,6 +345,16 @@ static inline ptrdiff_t refcount_garbage_collect(void) {
return refcount_context_garbage_collect(refcount_default_context);
}
/**
* Same as #refcount_context_is_doing_gc, but only operates on the global
* context.
* @return True if the context is currently doing a GC operation, false
* otherwise
*/
static inline bool refcount_is_doing_gc(void) {
return refcount_context_is_doing_gc(refcount_default_context);
}
/**
* Same as #refcount_context_make_weakref, but operates on the global context.
* @param obj The object to reference