BarryServer : Git

All the code for all my projects
// BarryServer : Git / libBLOC / commit / 22e0342f07b1426081405425d5a0a250404c5335

// Related

libBLOC

Barry Restructuring object core + portable locking 22e0342 (2 years, 11 months ago)
diff --git a/Makefile b/Makefile
index 0314ac3..00e1173 100644
--- a/Makefile
+++ b/Makefile
@@ -5,8 +5,7 @@ CFLAGS=-Iinclude/
 
 AR=ar
 
-SRCS := $(wildcard *.c)
-SRCS += $(wildcard **/*.c)
+SRCS := $(wildcard **/*.c)
 OBJS = $(addprefix build/,$(SRCS:.c=.o))
 
 .PHONY: clean all install
diff --git a/include/BLOC/object.h b/include/BLOC/object.h
index 64d685c..f71e290 100644
--- a/include/BLOC/object.h
+++ b/include/BLOC/object.h
@@ -33,16 +33,17 @@ struct ObjectType {
 	void (*delete)(void *);
 };
 
-int obj_verify(void *addr);
-unsigned int obj_usage(void *addr);
 void *obj_get(void *addr);
 void obj_put(void *addr);
 void *obj_new(struct ObjectType *type);
-struct ObjectType *obj_type(void *addr);
 
 #define obj_drop(v) ({ obj_put(v); v = NULL; })
 #define obj_swap(v,o) ({ obj_put(v); v = obj_get(o); })
 
+int obj_verify(void *addr);
+unsigned int obj_usage(void *addr);
+struct ObjectType *obj_type(void *addr);
+
 void obj_lock(void *addr);
 void obj_unlock(void *addr);
 
diff --git a/object.c b/object/object.c
similarity index 63%
rename from object.c
rename to object/object.c
index 52fbe89..39051ee 100644
--- a/object.c
+++ b/object/object.c
@@ -25,31 +25,11 @@
  * reference counts each object and controls their instantiation and deletion.
  */
 
-#define _GNU_SOURCE
-#include <stdlib.h>
-#include <unistd.h>
 #include <string.h>
 #include <BLOC/object.h>
 #include "../assert.h"
 #include "object.h"
 
-/* Verify that a pointer points to a valid object */
-int
-obj_verify(void *addr)
-{
-	struct ObjectHeader *obj = object_header(addr);
-	return (int) (obj->magic == OBJECT_MAGIC);
-}
-
-/* Count the number of references an object has */
-unsigned int
-obj_usage(void *addr)
-{
-	struct ObjectHeader *obj = object_header(addr);
-	ASSERT(obj->magic == OBJECT_MAGIC);
-	return (unsigned int) obj->usage;
-}
-
 /* Obtain a reference to an object */
 void *
 obj_get(void *addr)
@@ -97,39 +77,3 @@ obj_new(struct ObjectType *type)
 
 	return obj_get(body);
 }
-
-/* Return the object type of an object */
-struct ObjectType *
-obj_type(void *addr)
-{
-	struct ObjectHeader *obj = object_header(addr);
-	ASSERT(obj->magic == OBJECT_MAGIC);
-	return obj->type;
-}
-
-/* Lock an object */
-void
-obj_lock(void *addr)
-{
-	struct ObjectHeader *obj = object_header(addr);
-	ASSERT(obj->magic == OBJECT_MAGIC);
-	pid_t owner = gettid();
-	if (obj->owner != owner) {
-		while (__atomic_test_and_set(&(obj->lock), __ATOMIC_ACQUIRE))
-			__builtin_ia32_pause();
-		obj->owner = owner;
-	}
-	__atomic_add_fetch(&obj->locks, 1, __ATOMIC_RELAXED);
-}
-
-/* Unlock an object */
-void
-obj_unlock(void *addr)
-{
-	struct ObjectHeader *obj = object_header(addr);
-	ASSERT(obj->magic == OBJECT_MAGIC);
-	if (__atomic_sub_fetch(&obj->locks, 1, __ATOMIC_RELAXED))
-		return;
-	__atomic_clear(&(obj->lock), __ATOMIC_RELEASE);
-	obj->owner = 0;
-}
diff --git a/object.h b/object/object.h
similarity index 98%
rename from object.h
rename to object/object.h
index e657a94..4b10de5 100644
--- a/object.h
+++ b/object/object.h
@@ -29,7 +29,7 @@ struct ObjectHeader {
 	refcount_t usage;
 
 	spinlock_t lock;
-	pid_t owner;
+	unsigned int owner;
 	refcount_t locks;
 };
 #define OBJECT_MAGIC 0x01020304
diff --git a/object/object_lock.c b/object/object_lock.c
new file mode 100644
index 0000000..0cdc3ad
--- /dev/null
+++ b/object/object_lock.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifdef __linux__
+# define _GNU_SOURCE
+# include <unistd.h>
+#endif
+
+#include <BLOC/object.h>
+#include "../assert.h"
+#include "object.h"
+
+/* Get a unique identifier for the current thread */
+static unsigned int
+_obj_get_thread_id(void)
+{
+#if defined(__linux__)
+	return (unsigned int) gettid();
+#else
+# error "No _obj_get_thread_id() for target platform"
+#endif
+}
+
+/* Lock an object */
+void
+obj_lock(void *addr)
+{
+	struct ObjectHeader *obj = object_header(addr);
+	ASSERT(obj->magic == OBJECT_MAGIC);
+	unsigned int owner = _obj_get_thread_id();
+	if (obj->owner != owner) {
+		while (__atomic_test_and_set(&(obj->lock), __ATOMIC_ACQUIRE))
+			__builtin_ia32_pause();
+		obj->owner = owner;
+	}
+	__atomic_add_fetch(&obj->locks, 1, __ATOMIC_RELAXED);
+}
+
+/* Unlock an object */
+void
+obj_unlock(void *addr)
+{
+	struct ObjectHeader *obj = object_header(addr);
+	ASSERT(obj->magic == OBJECT_MAGIC);
+	if (__atomic_sub_fetch(&obj->locks, 1, __ATOMIC_RELAXED))
+		return;
+	__atomic_clear(&(obj->lock), __ATOMIC_RELEASE);
+	obj->owner = 0;
+}
diff --git a/object/object_util.c b/object/object_util.c
new file mode 100644
index 0000000..f1949b4
--- /dev/null
+++ b/object/object_util.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2023 Barry
+ *
+ * This file is part of Barry's Little Objects in C Library (libBLOC).
+ *
+ * libBLOC is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libBLOC is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * libBLOC.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <BLOC/object.h>
+#include "../assert.h"
+#include "object.h"
+
+/* Verify that a pointer points to a valid object */
+int
+obj_verify(void *addr)
+{
+	struct ObjectHeader *obj = object_header(addr);
+	return (int) (obj->magic == OBJECT_MAGIC);
+}
+
+/* Count the number of references an object has */
+unsigned int
+obj_usage(void *addr)
+{
+	struct ObjectHeader *obj = object_header(addr);
+	ASSERT(obj->magic == OBJECT_MAGIC);
+	return (unsigned int) obj->usage;
+}
+
+/* Return the object type of an object */
+struct ObjectType *
+obj_type(void *addr)
+{
+	struct ObjectHeader *obj = object_header(addr);
+	ASSERT(obj->magic == OBJECT_MAGIC);
+	return obj->type;
+}
diff --git a/tree/tree_remove.c b/tree/tree_remove.c
index 6f1ad90..f41dfde 100644
--- a/tree/tree_remove.c
+++ b/tree/tree_remove.c
@@ -20,7 +20,6 @@
 #include <BLOC/object.h>
 #include <BLOC/tree.h>
 #include "../assert.h"
-#include "../object.h"
 #include "tree.h"
 
 /* Find a node recursively */