diff mbox

[4/4] linux-generic: odp_spinlock[ch]: use odp_atomic_internal.h

Message ID 1416955068-6580-5-git-send-email-ola.liljedahl@linaro.org
State New
Headers show

Commit Message

Ola Liljedahl Nov. 25, 2014, 10:37 p.m. UTC
Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org>
---
Use definitions from odp_atomic_internal.h.

 platform/linux-generic/include/api/odp_spinlock.h |  2 +-
 platform/linux-generic/odp_spinlock.c             | 17 +++++++++++------
 2 files changed, 12 insertions(+), 7 deletions(-)
diff mbox

Patch

diff --git a/platform/linux-generic/include/api/odp_spinlock.h b/platform/linux-generic/include/api/odp_spinlock.h
index 462ff97..7c299c8 100644
--- a/platform/linux-generic/include/api/odp_spinlock.h
+++ b/platform/linux-generic/include/api/odp_spinlock.h
@@ -30,7 +30,7 @@  extern "C" {
  * ODP spinlock
  */
 typedef struct odp_spinlock_t {
-	volatile int lock;  /**< @private Lock */
+	char lock;  /**< @private Lock */
 } odp_spinlock_t;
 
 
diff --git a/platform/linux-generic/odp_spinlock.c b/platform/linux-generic/odp_spinlock.c
index 4eba015..5c7ef39 100644
--- a/platform/linux-generic/odp_spinlock.c
+++ b/platform/linux-generic/odp_spinlock.c
@@ -5,36 +5,41 @@ 
  */
 
 #include <odp_spinlock.h>
+#include <odp_atomic_internal.h>
 #include <odp_spin_internal.h>
 
 
 void odp_spinlock_init(odp_spinlock_t *spinlock)
 {
-	__sync_lock_release(&spinlock->lock);
+	odp_atomic_flag_init(&spinlock->lock, 0);
 }
 
 
 void odp_spinlock_lock(odp_spinlock_t *spinlock)
 {
-	while (__sync_lock_test_and_set(&spinlock->lock, 1))
-		while (spinlock->lock)
+	/* While the lock is already taken... */
+	while (odp_atomic_flag_tas(&spinlock->lock))
+		/* ...spin reading the flag (relaxed MM),
+		 * the loop will exit when the lock becomes available
+		 * and we will retry the TAS operation above */
+		while (odp_atomic_flag_load(&spinlock->lock))
 			odp_spin();
 }
 
 
 int odp_spinlock_trylock(odp_spinlock_t *spinlock)
 {
-	return (__sync_lock_test_and_set(&spinlock->lock, 1) == 0);
+	return (odp_atomic_flag_tas(&spinlock->lock) == 0);
 }
 
 
 void odp_spinlock_unlock(odp_spinlock_t *spinlock)
 {
-	__sync_lock_release(&spinlock->lock);
+	odp_atomic_flag_clear(&spinlock->lock);
 }
 
 
 int odp_spinlock_is_locked(odp_spinlock_t *spinlock)
 {
-	return spinlock->lock != 0;
+	return odp_atomic_flag_load(&spinlock->lock) != 0;
 }