kernel: rebuild object ref-counting using atomic types
This commit is contained in:
@@ -74,90 +74,28 @@ struct object *object_create(struct object_type *type)
|
||||
obj->ob_lock = SPIN_LOCK_INIT;
|
||||
obj->ob_magic = OBJECT_MAGIC;
|
||||
obj->ob_refcount = 1;
|
||||
obj->ob_handles = 0;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
struct object *object_ref(struct object *obj)
|
||||
{
|
||||
obj->ob_refcount++;
|
||||
atomic_add_fetch(&obj->ob_refcount, 1);
|
||||
return obj;
|
||||
}
|
||||
|
||||
static void __cleanup(struct object *obj, struct queue *queue)
|
||||
{
|
||||
if (HAS_OP(obj, destroy)) {
|
||||
obj->ob_type->ob_ops.destroy(obj, queue);
|
||||
}
|
||||
|
||||
vm_cache_free(&obj->ob_type->ob_cache, obj);
|
||||
}
|
||||
|
||||
static void object_cleanup(struct object *obj, unsigned long flags)
|
||||
{
|
||||
if (obj->ob_refcount > 0 || obj->ob_handles > 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
return;
|
||||
}
|
||||
|
||||
struct queue queue = QUEUE_INIT;
|
||||
__cleanup(obj, &queue);
|
||||
|
||||
if (!HAS_OP(obj, destroy_recurse)) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (!queue_empty(&queue)) {
|
||||
struct queue_entry *entry = queue_pop_front(&queue);
|
||||
struct object *child = NULL;
|
||||
obj->ob_type->ob_ops.destroy_recurse(entry, &child);
|
||||
if (!child) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (child->ob_refcount > 1) {
|
||||
child->ob_refcount--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (child->ob_refcount == 0 && child->ob_handles == 0) {
|
||||
__cleanup(child, &queue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void object_unref(struct object *obj)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&obj->ob_lock, &flags);
|
||||
|
||||
if (obj->ob_refcount == 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
int ref = atomic_sub_fetch(&obj->ob_refcount, 1);
|
||||
if (ref > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
obj->ob_refcount--;
|
||||
object_cleanup(obj, flags);
|
||||
}
|
||||
|
||||
void object_add_handle(struct object *obj)
|
||||
{
|
||||
obj->ob_handles++;
|
||||
}
|
||||
|
||||
void object_remove_handle(struct object *obj)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&obj->ob_lock, &flags);
|
||||
|
||||
if (obj->ob_handles == 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
return;
|
||||
if (HAS_OP(obj, destroy)) {
|
||||
obj->ob_type->ob_ops.destroy(obj);
|
||||
}
|
||||
|
||||
obj->ob_handles--;
|
||||
object_cleanup(obj, flags);
|
||||
vm_cache_free(&obj->ob_type->ob_cache, obj);
|
||||
}
|
||||
|
||||
void object_lock(struct object *obj)
|
||||
|
||||
Reference in New Issue
Block a user