From e30fcbe3822ab740cef6ff2584dd0a1429ab10a5 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 17 Apr 2025 02:58:20 -0400 Subject: kernel: synch: Add system-wide locking Add system-wide locking for critical code sections. Signed-off-by: Ian Moffett --- sys/kern/kern_synch.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'sys/kern/kern_synch.c') diff --git a/sys/kern/kern_synch.c b/sys/kern/kern_synch.c index 2b64673..57b27d0 100644 --- a/sys/kern/kern_synch.c +++ b/sys/kern/kern_synch.c @@ -38,6 +38,9 @@ #define pr_trace(fmt, ...) kprintf("synch: " fmt, ##__VA_ARGS__) #define pr_error(...) pr_trace(__VA_ARGS__) +/* XXX: Be very careful with this */ +static struct spinlock __syslock; + /* * Returns 0 on success, returns non-zero value * on timeout/failure. @@ -110,3 +113,26 @@ spinlock_release(struct spinlock *lock) { __atomic_clear(&lock->lock, __ATOMIC_RELEASE); } + +/* + * Attempt to hold the system-wide lock, returns 1 + * if already held. + * + * XXX: Only use for CRITICAL code sections. + */ +int +syslock(void) +{ + return spinlock_try_acquire(&__syslock); +} + +/* + * Release the system-wide lock + * + * XXX: Only use for CRITICAL code sections. + */ +void +sysrel(void) +{ + spinlock_release(&__syslock); +} -- cgit v1.2.3