From cf729a1fa37919987837dec46f34ca78e776214d Mon Sep 17 00:00:00 2001 From: Phillip Tennen Date: Wed, 7 Feb 2024 14:58:46 +0000 Subject: [PATCH] [Kernel] Check in spinlock changes --- kernel/kernel/util/spinlock/spinlock.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/kernel/kernel/util/spinlock/spinlock.c b/kernel/kernel/util/spinlock/spinlock.c index 744062eb..4b21fbf0 100644 --- a/kernel/kernel/util/spinlock/spinlock.c +++ b/kernel/kernel/util/spinlock/spinlock.c @@ -79,7 +79,8 @@ static inline union x86_rflags local_irq_disable_save(void) { static inline void local_irq_restore(union x86_rflags flags) { if (flags.__packed.irqs_enabled) { - set_rflags(flags); + //set_rflags(flags); + asm("sti"); } } @@ -87,8 +88,8 @@ void spinlock_acquire(spinlock_t* lock) { if (!lock) return; assert(lock->name, "Spinlock was used without assigning a name"); - // Keep track of whether we had to wait to acquire the lock /* + // Keep track of whether we had to wait to acquire the lock uint32_t contention_start = 0; if (lock->flag != 0) { @@ -125,18 +126,19 @@ void spinlock_acquire(spinlock_t* lock) { } // Spin until the lock is released - /* while (!atomic_compare_exchange(&lock->flag, 0, 1)) { //task_switch(); } */ + union x86_rflags rflags = local_irq_disable_save(); while (atomic_bit_test_and_set(&lock->flag) == 1) { - local_irq_restore(rflags); + //local_irq_restore(rflags); + // Wait until the lock looks unlocked before retrying while (lock->flag == 1) { - asm volatile ("pause":::"memory"); + //asm volatile ("pause":::"memory"); } - local_irq_disable(); + //local_irq_disable(); } lock->rflags = rflags; lock->owner_pid = getpid(); @@ -159,7 +161,8 @@ void spinlock_acquire(spinlock_t* lock) { else { //printf("Spinlock: Proc %d received uncontended lock 0x%08x %s\n", getpid(), lock, lock->name); } - */ + */ + //spinlock_acquire_spin(lock); } void spinlock_release(spinlock_t* lock) { @@ -176,9 +179,11 @@ void spinlock_release(spinlock_t* lock) { asm("sti"); } //printf("Spinlock: Proc %d freed lock 0x%08x %s\n", getpid(), lock, lock->name); - */ + */ + union x86_rflags rflags = lock->rflags; asm volatile ("":::"memory"); lock->flag = 0; local_irq_restore(rflags); + //spinlock_release_spin(lock); }