diff --git a/arch/riscv/include/arch/spinlock.h b/arch/riscv/include/arch/spinlock.h index a73b79e79..22ed69049 100644 --- a/arch/riscv/include/arch/spinlock.h +++ b/arch/riscv/include/arch/spinlock.h @@ -15,7 +15,7 @@ __BEGIN_CDECLS #define SPIN_LOCK_INITIAL_VALUE (0) -typedef volatile unsigned int spin_lock_t; +typedef volatile uint32_t spin_lock_t; typedef unsigned long spin_lock_saved_state_t; typedef unsigned int spin_lock_save_flags_t; diff --git a/arch/riscv/spinlock.c b/arch/riscv/spinlock.c index 441f5fdc7..c12f8c3eb 100644 --- a/arch/riscv/spinlock.c +++ b/arch/riscv/spinlock.c @@ -7,17 +7,19 @@ */ #include +#include + // super simple spin lock implementation int riscv_spin_trylock(spin_lock_t *lock) { - unsigned long val = arch_curr_cpu_num() + 1UL; + // use a full 32/64 type since amoswap overwrites the entire register unsigned long old; __asm__ __volatile__( "amoswap.w %0, %2, %1\n" "fence r, rw\n" : "=r"(old), "+A"(*lock) - : "r" (val) + : "r" (1u) : "memory" ); @@ -25,19 +27,19 @@ int riscv_spin_trylock(spin_lock_t *lock) { } void riscv_spin_lock(spin_lock_t *lock) { - unsigned long val = arch_curr_cpu_num() + 1UL; - for (;;) { if (*lock) { + // TODO: use a yield instruction here? continue; } + // use a full 32/64 type since amoswap overwrites the entire register unsigned long old; __asm__ __volatile__( "amoswap.w %0, %2, %1\n" "fence r, rw\n" : "=r"(old), "+A"(*lock) - : "r" (val) + : "r" (1u) : "memory" );