Commit 05256a76 by Marcus Winter

base_c: embb_spin_lock yields every 1024 spins

parent d14778ae
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
*/ */
#include <embb/base/c/mutex.h> #include <embb/base/c/mutex.h>
#include <embb/base/c/thread.h>
#include <assert.h> #include <assert.h>
#include <embb/base/c/internal/unused.h> #include <embb/base/c/internal/unused.h>
...@@ -125,10 +126,15 @@ int embb_spin_init(embb_spinlock_t* spinlock) { ...@@ -125,10 +126,15 @@ int embb_spin_init(embb_spinlock_t* spinlock) {
int embb_spin_lock(embb_spinlock_t* spinlock) { int embb_spin_lock(embb_spinlock_t* spinlock) {
int expected = 0; int expected = 0;
int spins = 1;
// try to swap the // try to swap the
while (0 == embb_atomic_compare_and_swap_int( while (0 == embb_atomic_compare_and_swap_int(
&spinlock->atomic_spin_variable_, &expected, 1)) { &spinlock->atomic_spin_variable_, &expected, 1)) {
if (0 == (spins & 1023)) {
embb_thread_yield();
}
spins++;
// reset expected, as CAS might change it... // reset expected, as CAS might change it...
expected = 0; expected = 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment