Commit a6c4506a by Marcus Winter

Merge branch 'embb540_spinlock_yield' into development

parents d2c1f41d f33bf0f8
...@@ -179,6 +179,9 @@ int embb_spin_init( ...@@ -179,6 +179,9 @@ int embb_spin_init(
/** /**
* Spins until the spinlock can be locked and locks it. * Spins until the spinlock can be locked and locks it.
* *
* \note This method yields the current thread in regular,
* implementation-defined intervals.
*
* \pre \c spinlock is initialized \n * \pre \c spinlock is initialized \n
* \post If successful, \c spinlock is locked. * \post If successful, \c spinlock is locked.
* \return EMBB_SUCCESS if spinlock could be locked. \n * \return EMBB_SUCCESS if spinlock could be locked. \n
......
...@@ -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;
} }
......
...@@ -192,6 +192,9 @@ class Spinlock { ...@@ -192,6 +192,9 @@ class Spinlock {
/** /**
* Waits until the spinlock can be locked and locks it. * Waits until the spinlock can be locked and locks it.
* *
* \note This method yields the current thread in regular,
* implementation-defined intervals.
*
* \pre The spinlock is not locked by the current thread. * \pre The spinlock is not locked by the current thread.
* \post The spinlock is locked. * \post The spinlock is locked.
* \threadsafe * \threadsafe
......
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