libBLOC
Barry Restructuring object core + portable locking 22e0342 (2 years, 11 months ago)
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;
+}