BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / e8e484f3952a9a3b7df1c3f5763a794a51ea6966 / object / manager.c

// Related

Nucleus

Barry Object locking e8e484f (3 years, 3 months ago)
/*
 * This is the Object Manager.  It implements the basic operations each object
 * needs and leaves the rest up to the respective subsystem that implements that
 * object.  The object manager is a resource manager which should help improve
 * memory safety within the kernel.  It reference counts each object and
 * controls their instantiation and deletion.
 */

#include <nucleus/object.h>

void acquire(Spinlock *lock);
void release(Spinlock *lock);

/* Obtain a reference to an object */
void *
get(void *addr)
{
	Object *obj = addr;
	__atomic_add_fetch(&obj->type->usage, 1, __ATOMIC_RELAXED);
	__atomic_add_fetch(&obj->usage, 1, __ATOMIC_RELAXED);
	return addr;
}

/* Release a reference to an object */
void
put(void *addr)
{
	Object *obj = addr;
	__atomic_sub_fetch(&obj->type->usage, 1, __ATOMIC_RELAXED);
	if (__atomic_sub_fetch(&obj->usage, 1, __ATOMIC_RELAXED))
		return;
	__atomic_sub_fetch(&obj->type->count, 1, __ATOMIC_RELAXED);
	obj->type->delete(obj);
}

/* Create a new instance of an object */
void *
new(ObjectType *type)
{
	Object *obj = type->new();
	obj->type = type;
	__atomic_add_fetch(&type->count, 1, __ATOMIC_RELAXED);
	return get(obj);
}

/* Lock an object */
void
lock(void *addr)
{
	Object *obj = addr;
	acquire(&obj->lock);
}

/* Unlock an object */
void
unlock(void *addr)
{
	Object *obj = addr;
	release(&obj->lock);
}