From 59a07e28c5ef018d0c5cecd12d52ff10f5dc5b1a Mon Sep 17 00:00:00 2001 From: Test_User Date: Fri, 19 Jul 2024 23:26:45 -0400 Subject: Minimal support for non-semaphore-supporting systems --- Makefile | 16 ++++++++++++++++ mutex.h | 22 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9a32b59..fe1f263 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ LDFLAGS = -lpthread printf '%s\n' 'LAST_SERVICES_PSEUDOCLIENT = $(SERVICES_PSEUDOCLIENT)' >> .makeopts printf '%s\n' 'LAST_SAFE_STACK = $(SAFE_STACK)' >> .makeopts printf '%s\n' 'LAST_FUTEX = $(FUTEX)' >> .makeopts + printf '%s\n' 'LAST_MISERABLE_SPINLOCKS = $(MISERABLE_SPINLOCKS)' >> .makeopts printf '%s\n' 'LAST_ATOMICS = $(ATOMICS)' >> .makeopts printf '%s\n' 'LAST_CFLAGS = $(ORIGINAL_CFLAGS)' >> .makeopts printf '%s\n' 'LAST_CC = $(CC)' >> .makeopts @@ -223,6 +224,14 @@ else FUTEX := $(LAST_FUTEX) endif +ifneq ($(MISERABLE_SPINLOCKS),) +ifneq ($(MISERABLE_SPINLOCKS),$(LAST_MISERABLE_SPINLOCKS)) +rebuild = 1 +endif +else +MISERABLE_SPINLOCKS := $(LAST_MISERABLE_SPINLOCKS) +endif + ifneq ($(ATOMICS),) ifneq ($(ATOMICS),$(LAST_ATOMICS)) rebuild = 1 @@ -415,6 +424,13 @@ ifeq ($(FUTEX),1) CFLAGS += -DUSE_FUTEX endif +ifeq ($(MISERABLE_SPINLOCKS),1) +ifeq ($(FUTEX),1) +$(error Miserable spinlocks are only enabled when noy using futexes) +endif +CFLAGS += -DUSE_MISERABLE_SPINLOCKS +endif + ifeq ($(ATOMICS),1) CFLAGS += -DUSE_ATOMICS endif diff --git a/mutex.h b/mutex.h index a341152..7d1b4b2 100644 --- a/mutex.h +++ b/mutex.h @@ -26,7 +26,7 @@ #pragma once -#ifdef USE_FUTEX +#if defined(USE_FUTEX) #include #include @@ -61,6 +61,26 @@ inline void mutex_destroy(uint32_t *futex) { return; } +#elif defined(USE_MISERABLE_SPINLOCKS) + +#define MUTEX_TYPE char + +inline void mutex_init(char *lock) { + *lock = 0; +} + +inline void mutex_lock(char *lock) { + while (__sync_lock_test_and_set(lock, 0x1)); +} + +inline void mutex_unlock(char *lock) { + __sync_lock_release(lock); +} + +inline void mutex_destroy(char *lock) { + return; +} + #else #include -- cgit v1.2.3