diff mbox

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

Message ID 1417101181-13500-5-git-send-email-ola.liljedahl@linaro.org
State Accepted
Commit e5d88e31579d24464e10de4668c1da2d1e3462b8
Headers show

Commit Message

Ola Liljedahl Nov. 27, 2014, 3:13 p.m. UTC
Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org>
---
(This document/code contribution attached is provided under the terms of
agreement LES-LTM-21309)

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(-)

Comments

Bill Fischofer Nov. 28, 2014, 12:52 p.m. UTC | #1
On Thu, Nov 27, 2014 at 10:13 AM, Ola Liljedahl <ola.liljedahl@linaro.org>
wrote:

> Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org>
>

Reviewed-by: Bill Fischofer <bill.fischofer@linaro.org>


> ---
> (This document/code contribution attached is provided under the terms of
> agreement LES-LTM-21309)
>
> 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 --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..4dfd3cf 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;
>  }
> --
> 1.9.1
>
>
> _______________________________________________
> lng-odp mailing list
> lng-odp@lists.linaro.org
> http://lists.linaro.org/mailman/listinfo/lng-odp
>
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..4dfd3cf 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;
 }