From 22b8de3040ad9f2f6b16b9e40f9bce528b6640e2 Mon Sep 17 00:00:00 2001 From: Minus Date: Tue, 17 Nov 2026 22:12:31 +0000 Subject: [PATCH] value: thread-safe reference counting foundation Add an atomic reference count to the common value header and the retain/release API, without changing any existing ownership behaviour (containers still do not free their children, so nothing new is freed and the suite stays green). - CAT_VALUE_Value gains `_Atomic long refcnt`. - cat_value_retain / cat_value_release use atomic RMW with acq_rel ordering; the last release calls vtable->destroy after an acquire fence. cat_value_refcount and cat_value_make_immortal complete the API. - every heap constructor sets refcnt = 1 (its creator's reference). - singletons are immortal (refcnt = -1): the empty object and true/false, so they are never freed and need no locking. This makes the COUNT safe to share across threads. It does not make a mutable object safe under concurrent writes. --- include/cat/value.h | 32 +++++++++++++++++++++++- src/cat/types/ascii_text.c | 1 + src/cat/types/bool.c | 4 +-- src/cat/types/byte.c | 1 + src/cat/types/char.c | 1 + src/cat/types/empty.c | 9 ++++++- src/cat/types/float.c | 1 + src/cat/types/function.c | 1 + src/cat/types/generator.c | 1 + src/cat/types/int.c | 1 + src/cat/types/list.c | 1 + src/cat/types/memory.c | 1 + src/cat/types/native.c | 1 + src/cat/types/table.c | 1 + src/cat/types/utf8_text.c | 1 + src/cat/value.c | 50 ++++++++++++++++++++++++++++++++++++++ 16 files changed, 103 insertions(+), 4 deletions(-) diff --git a/include/cat/value.h b/include/cat/value.h index f8fea66..42445ee 100644 --- a/include/cat/value.h +++ b/include/cat/value.h @@ -18,6 +18,7 @@ #include #include +#include struct CAT_ERROR_Error; struct CAT_VALUE_Value; @@ -151,11 +152,40 @@ struct CAT_VALUE_Vtable { void (*destroy)(CAT_VALUE_Value *self); }; -/* The common header shared by every concrete value. */ +/* The common header shared by every concrete value. + * + * `refcnt` is a thread-safe reference count: + * - a live, heap-owned value starts at 1 (its creator's reference); + * - `cat_value_retain`/`cat_value_release` add/drop references with + * atomic operations, and the last release calls vtable->destroy; + * - a value with a negative count is IMMORTAL (singletons such as the + * empty object and true/false, or stack-allocated wrappers): retain + * and release are no-ops for it, so it is never freed. + * + * Note: atomic refcounts make the COUNT safe across threads; they do NOT + * make a mutable object (a table being written from two threads) safe. */ struct CAT_VALUE_Value { CAT_VALUE_Vtable *vtable; + _Atomic long refcnt; }; +/* ---- reference counting (thread-safe) ---- */ + +/* Add a reference. No-op for NULL and immortal values. */ +void cat_value_retain(CAT_VALUE_Value *v); + +/* Drop a reference. When the last reference goes, calls vtable->destroy. + * No-op for NULL and immortal values. */ +void cat_value_release(CAT_VALUE_Value *v); + +/* The current reference count (for tests/diagnostics); negative means + * immortal, 0 means unowned/NULL. */ +long cat_value_refcount(CAT_VALUE_Value *v); + +/* Mark a value immortal (never freed). Used for singletons and for + * wrappers whose storage lives on the C stack. */ +void cat_value_make_immortal(CAT_VALUE_Value *v); + /* ---- dispatch helpers used by compiler/VM and embedders ---- */ int cat_value_add(CAT_VALUE_Value *a, CAT_VALUE_Value *b, diff --git a/src/cat/types/ascii_text.c b/src/cat/types/ascii_text.c index 8081f97..544fd1e 100644 --- a/src/cat/types/ascii_text.c +++ b/src/cat/types/ascii_text.c @@ -46,6 +46,7 @@ CAT_VALUE_Value *cat_types_ascii_text_new(const char *s) } memcpy(t->owned, s, n + 1); t->text.base.vtable = &g_ascii_vtable; + t->text.base.refcnt = 1; t->text.bytes = t->owned; t->text.len = (int)n; return &t->text.base; diff --git a/src/cat/types/bool.c b/src/cat/types/bool.c index ba83b19..02a1390 100644 --- a/src/cat/types/bool.c +++ b/src/cat/types/bool.c @@ -24,12 +24,12 @@ CAT_VALUE_Vtable *cat_types_bool_vtable(void) CAT_VALUE_Value *cat_types_bool_true(void) { - static CAT_TYPES_BOOL_Bool t = { { &g_bool_vtable }, 1 }; + static CAT_TYPES_BOOL_Bool t = { { &g_bool_vtable, -1 }, 1 }; return &t.base; } CAT_VALUE_Value *cat_types_bool_false(void) { - static CAT_TYPES_BOOL_Bool f = { { &g_bool_vtable }, 0 }; + static CAT_TYPES_BOOL_Bool f = { { &g_bool_vtable, -1 }, 0 }; return &f.base; } diff --git a/src/cat/types/byte.c b/src/cat/types/byte.c index 1ee2032..108a4bb 100644 --- a/src/cat/types/byte.c +++ b/src/cat/types/byte.c @@ -39,6 +39,7 @@ CAT_VALUE_Value *cat_types_byte_new(unsigned int v) if (n == NULL) return NULL; n->number.base.vtable = &g_byte_vtable; + n->number.base.refcnt = 1; n->number.is_float = 0; n->number.i = (long long)(v & 0xFFu); n->number.f = (double)(v & 0xFFu); diff --git a/src/cat/types/char.c b/src/cat/types/char.c index 4068d24..e93ae77 100644 --- a/src/cat/types/char.c +++ b/src/cat/types/char.c @@ -59,6 +59,7 @@ CAT_VALUE_Value *cat_types_char_new(int width, const char *s) } c->buf[width] = '\0'; c->text.base.vtable = cat_types_char_vtable(width); + c->text.base.refcnt = 1; c->text.bytes = c->buf; c->text.len = width; return &c->text.base; diff --git a/src/cat/types/empty.c b/src/cat/types/empty.c index faabed8..388fb98 100644 --- a/src/cat/types/empty.c +++ b/src/cat/types/empty.c @@ -44,7 +44,14 @@ CAT_VALUE_Vtable *cat_types_empty_vtable(void) CAT_VALUE_Value *cat_types_empty(void) { static CAT_VALUE_Value g_empty; - g_empty.vtable = &g_empty_vtable; + static int inited = 0; + + if (!inited) { + g_empty.vtable = &g_empty_vtable; + /* immortal: the empty object is shared and never freed */ + atomic_store_explicit(&g_empty.refcnt, -1, memory_order_relaxed); + inited = 1; + } return &g_empty; } diff --git a/src/cat/types/float.c b/src/cat/types/float.c index 12f8bed..afd3461 100644 --- a/src/cat/types/float.c +++ b/src/cat/types/float.c @@ -39,6 +39,7 @@ CAT_VALUE_Value *cat_types_float_new(double v) if (n == NULL) return NULL; n->number.base.vtable = &g_float_vtable; + n->number.base.refcnt = 1; n->number.is_float = 1; n->number.f = v; n->number.i = (long long)v; diff --git a/src/cat/types/function.c b/src/cat/types/function.c index e841a8b..78e1e94 100644 --- a/src/cat/types/function.c +++ b/src/cat/types/function.c @@ -105,6 +105,7 @@ CAT_VALUE_Value *cat_types_function_new(CAT_BYTECODE_Function *proto) if (cl == NULL) return NULL; cl->base.vtable = &g_function_vtable; + cl->base.refcnt = 1; cl->proto = proto; cl->nupvals = 0; cl->upvals = NULL; diff --git a/src/cat/types/generator.c b/src/cat/types/generator.c index de57480..a5377ed 100644 --- a/src/cat/types/generator.c +++ b/src/cat/types/generator.c @@ -58,6 +58,7 @@ CAT_VALUE_Value *cat_types_generator_new(CAT_VM_State *vm, if (g == NULL) return cat_types_empty(); g->base.vtable = &g_generator_vtable; + g->base.refcnt = 1; g->vm = vm; g->cl = cl; g->frame.fn = cl->proto; diff --git a/src/cat/types/int.c b/src/cat/types/int.c index 4e38824..a3462ee 100644 --- a/src/cat/types/int.c +++ b/src/cat/types/int.c @@ -39,6 +39,7 @@ CAT_VALUE_Value *cat_types_int_new(long long v) if (n == NULL) return NULL; n->number.base.vtable = &g_int_vtable; + n->number.base.refcnt = 1; n->number.is_float = 0; n->number.i = v; n->number.f = (double)v; diff --git a/src/cat/types/list.c b/src/cat/types/list.c index 3108f52..0b4fcc9 100644 --- a/src/cat/types/list.c +++ b/src/cat/types/list.c @@ -97,6 +97,7 @@ CAT_VALUE_Value *cat_types_list_new(void) if (l == NULL) return NULL; l->base.vtable = &g_list_vtable; + l->base.refcnt = 1; return &l->base; } diff --git a/src/cat/types/memory.c b/src/cat/types/memory.c index a3d3a0a..268d934 100644 --- a/src/cat/types/memory.c +++ b/src/cat/types/memory.c @@ -275,6 +275,7 @@ CAT_VALUE_Value *cat_types_memory_new(size_t size) if (m == NULL) return NULL; m->base.vtable = &g_memory_vtable; + m->base.refcnt = 1; m->size = size; m->data = calloc(1, size ? size : 1); if (m->data == NULL) { diff --git a/src/cat/types/native.c b/src/cat/types/native.c index bbb0a93..8ff7ff2 100644 --- a/src/cat/types/native.c +++ b/src/cat/types/native.c @@ -74,6 +74,7 @@ CAT_VALUE_Value *cat_types_native_new_doc(const char *name, if (nf == NULL) return NULL; nf->base.vtable = &g_native_vtable; + nf->base.refcnt = 1; nf->name = strdup(name ? name : "native"); nf->doc = doc ? strdup(doc) : NULL; nf->cb = cb; diff --git a/src/cat/types/table.c b/src/cat/types/table.c index 21a7a63..cad8d26 100644 --- a/src/cat/types/table.c +++ b/src/cat/types/table.c @@ -104,6 +104,7 @@ CAT_VALUE_Value *cat_types_table_new(void) if (t == NULL) return NULL; t->base.vtable = &g_table_vtable; + t->base.refcnt = 1; return &t->base; } diff --git a/src/cat/types/utf8_text.c b/src/cat/types/utf8_text.c index 3c914cb..52aa289 100644 --- a/src/cat/types/utf8_text.c +++ b/src/cat/types/utf8_text.c @@ -69,6 +69,7 @@ CAT_VALUE_Value *cat_types_utf8_text_new(const char *s) } memcpy(t->owned, s, n + 1); t->text.base.vtable = &g_utf8_vtable; + t->text.base.refcnt = 1; t->text.bytes = t->owned; t->text.len = (int)n; return &t->text.base; diff --git a/src/cat/value.c b/src/cat/value.c index a21b88b..64e21b5 100644 --- a/src/cat/value.c +++ b/src/cat/value.c @@ -25,6 +25,56 @@ #include #include +/* ---- reference counting (thread-safe) ------------------------------- + * + * The count lives in the common value header. retain/release use atomic + * read-modify-write with acquire/release ordering, so the object graph + * can be shared across threads safely. Destroying the object runs on + * whichever thread drops the last reference. + * + * A negative count marks an immortal value (a singleton, or a wrapper + * whose storage lives on the C stack); retain/release ignore it. */ +void cat_value_retain(CAT_VALUE_Value *v) +{ + if (v == NULL) + return; + if (atomic_load_explicit(&v->refcnt, memory_order_relaxed) < 0) + return; + atomic_fetch_add_explicit(&v->refcnt, 1, memory_order_relaxed); +} + +void cat_value_release(CAT_VALUE_Value *v) +{ + long prev; + + if (v == NULL) + return; + prev = atomic_load_explicit(&v->refcnt, memory_order_relaxed); + if (prev < 0) + return; + if (atomic_fetch_sub_explicit(&v->refcnt, 1, + memory_order_acq_rel) == 1) { + /* Last reference: publish our writes, then destroy. */ + atomic_thread_fence(memory_order_acquire); + if (v->vtable != NULL && v->vtable->destroy != NULL) + v->vtable->destroy(v); + } +} + +long cat_value_refcount(CAT_VALUE_Value *v) +{ + if (v == NULL) + return 0; + return atomic_load_explicit(&v->refcnt, memory_order_relaxed); +} + +void cat_value_make_immortal(CAT_VALUE_Value *v) +{ + if (v == NULL) + return; + atomic_store_explicit(&v->refcnt, -1, memory_order_relaxed); +} + const char *cat_value_type_name(CAT_VALUE_Value *v) { if (v == NULL || v->vtable == NULL || v->vtable->type_name == NULL)