diff --git a/base_c/src/mutex.c b/base_c/src/mutex.c index 7e29b43..601e16b 100644 --- a/base_c/src/mutex.c +++ b/base_c/src/mutex.c @@ -25,6 +25,7 @@ */ #include +#include #include #include @@ -125,10 +126,15 @@ int embb_spin_init(embb_spinlock_t* spinlock) { int embb_spin_lock(embb_spinlock_t* spinlock) { int expected = 0; + int spins = 1; // try to swap the while (0 == embb_atomic_compare_and_swap_int( &spinlock->atomic_spin_variable_, &expected, 1)) { + if (0 == (spins & 1023)) { + embb_thread_yield(); + } + spins++; // reset expected, as CAS might change it... expected = 0; }