From 799a23014a32d759e3099f7fb5315c5b9dcc3ec9 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 2 Feb 2023 21:00:40 +0000 Subject: [PATCH] sandbox: add locking primitives --- sandbox/Makefile | 2 +- sandbox/locks/include/socks/locks.h | 11 +++++++++++ sandbox/locks/spinlock.c | 14 ++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 sandbox/locks/include/socks/locks.h create mode 100644 sandbox/locks/spinlock.c diff --git a/sandbox/Makefile b/sandbox/Makefile index 7a65408..81a5206 100644 --- a/sandbox/Makefile +++ b/sandbox/Makefile @@ -6,7 +6,7 @@ BUILD_DIR := $(SANDBOX_BASE_DIR)/../build/sandbox include $(SANDBOX_BASE_DIR)/../tools/make/gcc-host.mk EXEC_NAME := sandbox -DIR_LIST := memblock vm base queue btree util +DIR_LIST := memblock vm base queue btree util locks INCLUDE_DIRS := $(foreach dir,$(DIR_LIST),-I$(dir)/include) SRC := $(foreach dir,$(DIR_LIST),$(wildcard $(dir)/*.c)) OBJ := $(addprefix $(BUILD_DIR)/,$(SRC:.c=.o)) diff --git a/sandbox/locks/include/socks/locks.h b/sandbox/locks/include/socks/locks.h new file mode 100644 index 0000000..9b07aae --- /dev/null +++ b/sandbox/locks/include/socks/locks.h @@ -0,0 +1,11 @@ +#ifndef SOCKS_LOCKS_H_ +#define SOCKS_LOCKS_H_ + +typedef int __attribute__((aligned(8))) spin_lock_t; + +#define SPIN_LOCK_INIT ((spin_lock_t)0) + +extern void spin_lock_irqsave(spin_lock_t *lck, unsigned long *flags); +extern void spin_unlock_irqrestore(spin_lock_t *lck, unsigned long flags); + +#endif diff --git a/sandbox/locks/spinlock.c b/sandbox/locks/spinlock.c new file mode 100644 index 0000000..0e247d9 --- /dev/null +++ b/sandbox/locks/spinlock.c @@ -0,0 +1,14 @@ +#include + +void spin_lock_irqsave(spin_lock_t *lck, unsigned long *flags) +{ + while (!__sync_bool_compare_and_swap(lck, 0, 1)) { + /* pause */ + } +} + +void spin_unlock_irqrestore(spin_lock_t *lck, unsigned long flags) +{ + __sync_lock_release(lck); +} +