aboutsummaryrefslogtreecommitdiffstats
path: root/internal-linux.h
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2020-04-20 18:58:00 +0200
committerMattias Andrée <maandree@kth.se>2020-04-20 18:58:00 +0200
commitda8b1c1d1baafb9cf4a0cc9a88362f161f8d1319 (patch)
tree0fb0676b127996064ccb75b695e9aaf9657ce9f1 /internal-linux.h
downloadlibaxl-da8b1c1d1baafb9cf4a0cc9a88362f161f8d1319.tar.gz
libaxl-da8b1c1d1baafb9cf4a0cc9a88362f161f8d1319.tar.bz2
libaxl-da8b1c1d1baafb9cf4a0cc9a88362f161f8d1319.tar.xz
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'internal-linux.h')
-rw-r--r--internal-linux.h38
1 files changed, 38 insertions, 0 deletions
diff --git a/internal-linux.h b/internal-linux.h
new file mode 100644
index 0000000..4fb2ee3
--- /dev/null
+++ b/internal-linux.h
@@ -0,0 +1,38 @@
+/* See LICENSE file for copyright and license details. */
+
+#include <linux/futex.h>
+#include <sys/syscall.h>
+
+#include "internal-llmutex.h"
+
+
+typedef volatile _Atomic uint32_t MUTEX;
+_Static_assert(sizeof(MUTEX) == sizeof(uint32_t));
+
+
+#define INIT_MUTEX(MUTEX)\
+ ((void)0) /* TODO atomic_init(&(MUTEX), 0) */
+
+#define _TRYLOCK(MUTEX)\
+ !atomic_exchange(&(MUTEX), 1)
+
+#define _LOCK(MUTEX)\
+ do {\
+ while (!_TRYLOCK(MUTEX))\
+ _WAIT(MUTEX);\
+ } while (0)
+
+#define _UNLOCK(MUTEX)\
+ do {\
+ atomic_store(&(MUTEX), 0);\
+ if (syscall(SYS_futex, &(MUTEX), FUTEX_PRIVATE_FLAG | FUTEX_WAKE, INT_MAX, NULL, 0, 0) < 0) {\
+ /* TODO */\
+ }\
+ } while (0)
+
+#define _WAIT(MUTEX)\
+ do {\
+ if (syscall(SYS_futex, &(MUTEX), FUTEX_PRIVATE_FLAG | FUTEX_WAIT, 1, NULL, 0, 0) < 0) {\
+ /* TODO */\
+ }\
+ } while (0)