From 8ecf2f53bb9f9689e16c749271c89fa579af8422 Mon Sep 17 00:00:00 2001 From: Akash Kollipara Date: Wed, 10 Jul 2024 19:07:22 +0530 Subject: [PATCH] Added support for bakerylock - Added sources for bakerylock - Moved lock realted files to liblocks for adding more lock sources Issue: #301 --- src/lib/build.mk | 1 + src/lib/liblocks/bakerylock.c | 44 +++++++++++++++++++ src/lib/liblocks/build.mk | 20 +++++++++ src/lib/liblocks/include/lock/bakerylock.h | 16 +++++++ src/{ => lib/liblocks}/include/lock/lock.h | 0 .../liblocks}/include/lock/spinlock.h | 0 6 files changed, 81 insertions(+) create mode 100644 src/lib/liblocks/bakerylock.c create mode 100644 src/lib/liblocks/build.mk create mode 100644 src/lib/liblocks/include/lock/bakerylock.h rename src/{ => lib/liblocks}/include/lock/lock.h (100%) rename src/{ => lib/liblocks}/include/lock/spinlock.h (100%) diff --git a/src/lib/build.mk b/src/lib/build.mk index e65f5ba8..f3658326 100644 --- a/src/lib/build.mk +++ b/src/lib/build.mk @@ -16,6 +16,7 @@ include $(LIB_DIR)/libc/build.mk include $(LIB_DIR)/libresource/build.mk include $(LIB_DIR)/libsyslog/build.mk include $(LIB_DIR)/libccfs/build.mk +include $(LIB_DIR)/liblocks/build.mk #================================================== include $(LIB_DIR)/libnmath/build.mk diff --git a/src/lib/liblocks/bakerylock.c b/src/lib/liblocks/bakerylock.c new file mode 100644 index 00000000..dfca1a61 --- /dev/null +++ b/src/lib/liblocks/bakerylock.c @@ -0,0 +1,44 @@ + +#include +#include +#include +#include + +void bakerylock_acquire(bakerylock_t *key) +{ + unsigned int tid = arch_core_index(); + key->protect[tid] = 1; + arch_dmb(); + + unsigned int i, n_cust, n_max = 0; + for(i = 0; i < N_CORES; i++) + { + n_cust = key->thread_count[i]; + n_max = n_cust > n_max ? n_cust : n_max; + } + + key->thread_count[tid] = n_max + 1; + + arch_dmb(); + key->protect[tid] = 0; + arch_dmb(); + + for(i = 0; i < N_CORES; i++) + { + while(key->protect[i]); + arch_dmb(); + + while(key->thread_count[i] && ( + (key->thread_count[i] < key->thread_count[tid]) || + ((key->thread_count[i] == key->thread_count[tid]) && + (i < tid)))); + } +} + +void bakerylock_release(bakerylock_t *key) +{ + unsigned int tid = arch_core_index(); + arch_dmb(); + key->thread_count[tid] = 0; +} + diff --git a/src/lib/liblocks/build.mk b/src/lib/liblocks/build.mk new file mode 100644 index 00000000..e2c21674 --- /dev/null +++ b/src/lib/liblocks/build.mk @@ -0,0 +1,20 @@ +# +# CYANCORE LICENSE +# Copyrights (C) 2024, Cyancore Team +# +# File Name : build.mk +# Description : This file accumulates sources of locks +# Primary Author : Akash Kollipara [akashkollipara@gmail.com] +# Organisation : Cyancore Core-Team +# + +LIBLOCKS_PATH := $(GET_PATH) +LIB_OBJS := + +LIB += liblocks.a +LIB_INCLUDE += $(LIBLOCKS_PATH)/include +DEP_LIBS_ARG += -llocks + + +DIR := $(LIBLOCKS_PATH) +include mk/lib.mk diff --git a/src/lib/liblocks/include/lock/bakerylock.h b/src/lib/liblocks/include/lock/bakerylock.h new file mode 100644 index 00000000..b22d1bd3 --- /dev/null +++ b/src/lib/liblocks/include/lock/bakerylock.h @@ -0,0 +1,16 @@ + +#pragma once +#define _BAKERYLOCK_H_ + +#include + +#pragma pack(1) +typedef struct +{ + volatile uint8_t thread_count[N_CORES]; + volatile bool protect[N_CORES]; +} bakerylock_t; +#pragma pack() + +extern void bakerylock_acquire(bakerylock_t *); +extern void bakerylock_release(bakerylock_t *); diff --git a/src/include/lock/lock.h b/src/lib/liblocks/include/lock/lock.h similarity index 100% rename from src/include/lock/lock.h rename to src/lib/liblocks/include/lock/lock.h diff --git a/src/include/lock/spinlock.h b/src/lib/liblocks/include/lock/spinlock.h similarity index 100% rename from src/include/lock/spinlock.h rename to src/lib/liblocks/include/lock/spinlock.h