Skip to content

Commit

Permalink
cpu relax
Browse files Browse the repository at this point in the history
  • Loading branch information
vityaman committed Nov 21, 2024
1 parent 15af55e commit 0ac13a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
10 changes: 10 additions & 0 deletions source/coroed/core/relax.h
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 3 additions & 1 deletion source/coroed/core/spinlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#include <assert.h>
#include <stdatomic.h>

#include "relax.h"

void spinlock_init(struct spinlock* lock) {
assert(atomic_is_lock_free(&lock->is_locked));
atomic_store(&lock->is_locked, false);
}

void spinlock_lock(struct spinlock* lock) {
while (!spinlock_try_lock(lock)) {
// CPU not relax
CPU_RELAX();
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/coroed/sched/schedy.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "coroed/api/task.h"
#include "coroed/core/relax.h"
#include "coroed/core/spinlock.h"
#include "kthread.h"
#include "uthread.h"
Expand Down Expand Up @@ -155,7 +155,7 @@ struct task* sched_acquire_next() {
}
spinlock_unlock(&tasks_lock);

sleep(1);
SPINLOOP(2 * attempt);
}

return NULL;
Expand Down

0 comments on commit 0ac13a4

Please sign in to comment.