From 0ac13a40c8c145c11ed11f1bb4c49b96bbb2b08f Mon Sep 17 00:00:00 2001 From: vityaman Date: Thu, 21 Nov 2024 11:07:00 +0300 Subject: [PATCH] cpu relax --- source/coroed/core/relax.h | 10 ++++++++++ source/coroed/core/spinlock.c | 4 +++- source/coroed/sched/schedy.c | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 source/coroed/core/relax.h diff --git a/source/coroed/core/relax.h b/source/coroed/core/relax.h new file mode 100644 index 0000000..961967a --- /dev/null +++ b/source/coroed/core/relax.h @@ -0,0 +1,10 @@ +#pragma once + +#define CPU_RELAX() __asm__ volatile("pause" ::: "memory") /* NOLINT */ + +#define SPINLOOP(spins) \ + do { \ + for (int i = 0; i < (spins); ++i) { \ + CPU_RELAX(); \ + } \ + } while (false) diff --git a/source/coroed/core/spinlock.c b/source/coroed/core/spinlock.c index a433d4b..008aafc 100644 --- a/source/coroed/core/spinlock.c +++ b/source/coroed/core/spinlock.c @@ -3,6 +3,8 @@ #include #include +#include "relax.h" + void spinlock_init(struct spinlock* lock) { assert(atomic_is_lock_free(&lock->is_locked)); atomic_store(&lock->is_locked, false); @@ -10,7 +12,7 @@ void spinlock_init(struct spinlock* lock) { void spinlock_lock(struct spinlock* lock) { while (!spinlock_try_lock(lock)) { - // CPU not relax + CPU_RELAX(); } } diff --git a/source/coroed/sched/schedy.c b/source/coroed/sched/schedy.c index f1b96ac..82a1ad4 100644 --- a/source/coroed/sched/schedy.c +++ b/source/coroed/sched/schedy.c @@ -8,9 +8,9 @@ #include #include #include -#include #include "coroed/api/task.h" +#include "coroed/core/relax.h" #include "coroed/core/spinlock.h" #include "kthread.h" #include "uthread.h" @@ -155,7 +155,7 @@ struct task* sched_acquire_next() { } spinlock_unlock(&tasks_lock); - sleep(1); + SPINLOOP(2 * attempt); } return NULL;