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.
This commit is contained in:
Minus committed 2026-11-17 22:12:31 +00:00
1 parent 920e672d29
commit 22b8de3040
16 files changed
+103 -4

No files matched your search

+1
View File
@@ -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;
+2 -2
View File
@@ -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;
}
+1
View File
@@ -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);
+1
View File
@@ -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;
+8 -1
View File
@@ -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;
}
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
+1
View File
@@ -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;
}
+1
View File
@@ -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) {
+1
View File
@@ -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;
+1
View File
@@ -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;
}
+1
View File
@@ -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;
+50
View File
@@ -25,6 +25,56 @@
#include <stdlib.h>
#include <string.h>
/* ---- 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)