Use compiler builtins for spin_unlock, correct initialization for spinlock_init

This commit is contained in:
Imbus 2025-09-02 04:43:55 +02:00
parent e15b705eda
commit a02c1fcebd

View file

@ -3,6 +3,7 @@
* (Not mutexes as these are spinning locks).
*/
#include "stddef.h"
#include <panic.h>
#include <proc.h>
#include <riscv.h>
@ -79,6 +80,7 @@ uint32_t pop_off(void) {
void spinlock_init(spinlock_t *l) {
l->v = 0;
l->cpu = NULL;
}
__attribute__((warn_unused_result)) bool spin_trylock(spinlock_t *l) {
@ -92,15 +94,11 @@ void spin_unlock(spinlock_t *l) {
if (!spin_is_holding(l))
PANIC("Unlocking a spinlock that is not held by the locking cpu!");
l->cpu = 0;
// Release: store 0 with .rl ordering.
uint32_t dummy;
__asm__ volatile("amoswap.w.rl %0, %2, (%1)\n" : "=&r"(dummy) : "r"(&l->v), "r"(0u) : "memory");
__sync_synchronize(); // No loads/stores after this point
// __sync_lock_release(&lk->locked); // Essentially lk->locked = 0
/* TODO: Replace with corresponding __atomic builtins */
__sync_lock_release(&l->v);
__sync_synchronize();
l->cpu = NULL;
pop_off();
}