From 9c795fbdbf445d144d331ff2a19a0f42fe0fc190 Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Mon, 30 May 2022 19:22:08 +0900 Subject: [PATCH] utils: Use WFE/SEV in spinlock This should reduce memory traffic spam and power usage from lock contention when threads are blocked on a spinlock. Signed-off-by: Hector Martin --- src/utils.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index 3c7133ac..314ba0d8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -133,8 +133,10 @@ void spin_lock(spinlock_t *lock) s64 free = -1; while (!__atomic_compare_exchange_n(&lock->lock, &free, me, false, __ATOMIC_ACQUIRE, - __ATOMIC_RELAXED)) + __ATOMIC_RELAXED)) { free = -1; + sysop("wfe"); + } assert(__atomic_load_n(&lock->lock, __ATOMIC_RELAXED) == me); lock->count++; @@ -147,6 +149,7 @@ void spin_unlock(spinlock_t *lock) assert(lock->count > 0); if (!--lock->count) __atomic_store_n(&lock->lock, -1L, __ATOMIC_RELEASE); + sysop("sev"); } bool is_heap(void *addr)