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
+103
-4
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,
|
||||
|
||||
Reference in new issue
Block a user