Skip to content

Commit

Permalink
<locks> Added support for bakerylock
Browse files Browse the repository at this point in the history
- Added sources for bakerylock
- Moved lock realted files to liblocks for adding more lock sources

Issue: #301
  • Loading branch information
akashkollipara committed Jul 10, 2024
1 parent 7806018 commit 8ecf2f5
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions src/lib/liblocks/bakerylock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include <status.h>
#include <stdint.h>
#include <arch.h>
#include <lock/bakerylock.h>

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;
}

20 changes: 20 additions & 0 deletions src/lib/liblocks/build.mk
Original file line number Diff line number Diff line change
@@ -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 [[email protected]]
# 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
16 changes: 16 additions & 0 deletions src/lib/liblocks/include/lock/bakerylock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#pragma once
#define _BAKERYLOCK_H_

#include <stdbool.h>

#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 *);
File renamed without changes.
File renamed without changes.

0 comments on commit 8ecf2f5

Please sign in to comment.