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:
1 parent
920e672d29
commit
22b8de3040
16 files changed
+102
-3
No files matched your search
+31
-1
@@ -18,6 +18,7 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user