BarryServer : Git

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

// Related

Nucleus

Barry Object locking e8e484f (3 years, 3 months ago)
diff --git a/object/manager.c b/object/manager.c
index 301b6ca..a330d15 100644
--- a/object/manager.c
+++ b/object/manager.c
@@ -8,13 +8,16 @@
 
 #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;
-	obj->type->usage++;
-	obj->usage++;
+	__atomic_add_fetch(&obj->type->usage, 1, __ATOMIC_RELAXED);
+	__atomic_add_fetch(&obj->usage, 1, __ATOMIC_RELAXED);
 	return addr;
 }
 
@@ -23,10 +26,10 @@ void
 put(void *addr)
 {
 	Object *obj = addr;
-	obj->type->usage--;
-	if (--obj->usage)
+	__atomic_sub_fetch(&obj->type->usage, 1, __ATOMIC_RELAXED);
+	if (__atomic_sub_fetch(&obj->usage, 1, __ATOMIC_RELAXED))
 		return;
-	obj->type->count--;
+	__atomic_sub_fetch(&obj->type->count, 1, __ATOMIC_RELAXED);
 	obj->type->delete(obj);
 }
 
@@ -36,6 +39,22 @@ new(ObjectType *type)
 {
 	Object *obj = type->new();
 	obj->type = type;
-	type->count++;
+	__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);
+}