25#define REF_MAGIC 0x26CB6E4C
34typedef struct weak_ptr
38 void (*callback)(
void* arg);
58 void (*callback)(
void* self);
71#define REF_COUNT(ptr) ((ptr) == NULL ? 0 : atomic_load_explicit(&((ref_t*)(ptr))->count, memory_order_relaxed))
84 ref_t* ref = (ref_t*)(ptr); \
97 ref_t* ref = (ref_t*)(ptr); \
98 ref_inc_try(ref) != NULL ? ptr : NULL; \
111 ref_t* ref = (ref_t*)(ptr); \
122#define UNREF_DEFER(ptr) __attribute__((cleanup(ref_defer_cleanup))) void* CONCAT(p, __COUNTER__) = (ptr)
#define assert(expression)
static void lock_init(lock_t *lock)
Initializes a lock.
static bool lock_try_acquire(lock_t *lock)
Tries to acquire a lock.
#define LOCK_SCOPE(lock)
Acquires a lock for the reminder of the current scope.
static void lock_release(lock_t *lock)
Releases a lock.
static void lock_acquire(lock_t *lock)
Acquires a lock, blocking until it is available.
static void ref_defer_cleanup(void **ptr)
static void * ref_inc_try(void *ptr)
Increment reference count, but only if the current count is not zero.
static void weak_ptr_set(weak_ptr_t *wp, ref_t *ref, void(*callback)(void *), void *arg)
Set a weak pointer.
static void weak_ptr_clear(weak_ptr_t *wp)
Clear a weak pointer.
static void ref_dec(void *ptr)
Decrement reference count.
static void * weak_ptr_get(weak_ptr_t *wp)
Upgrade a weak pointer to a strong pointer.
#define REF_MAGIC
Magic value used in debug builds to check for corruption or invalid use of the ref_t structure.
static void * ref_inc(void *ptr)
Increment reference count.
static void ref_init(ref_t *ref, void *callback)
Initialize a reference counter.
#define ASM(...)
Inline assembly macro.
#define LIST_FOR_EACH(elem, list, member)
Iterates over a list.
static void list_remove(list_entry_t *entry)
Removes a list entry from its current list.
static void list_push_back(list_t *list, list_entry_t *entry)
Pushes an entry to the end of the list.
static void list_entry_init(list_entry_t *entry)
Initializes a list entry.
static void list_init(list_t *list)
Initializes a list.
#define NULL
Pointer error value.
#define atomic_fetch_add_explicit(object, operand, order)
#define atomic_load_explicit(object, order)
#define atomic_thread_fence(order)
#define atomic_compare_exchange_weak_explicit(object, expected, desired, success, failure)
#define atomic_init(obj, value)
#define atomic_fetch_sub_explicit(object, operand, order)
A entry in a doubly linked list.
A simple ticket lock implementation.
Reference counting structure.
void(* callback)(void *self)
Cleanup function called when count reaches zero.
void(* callback)(void *arg)