diff mbox

[5/7] linux-generic: odp_rwlock.c: use odp_atomic.h and GCC __atomic (tmp)

Message ID 1416484428-23849-6-git-send-email-ola.liljedahl@linaro.org
State Superseded
Headers show

Commit Message

Ola Liljedahl Nov. 20, 2014, 11:53 a.m. UTC
Signed-off-by: Ola Liljedahl <ola.liljedahl@linaro.org>
---
 platform/linux-generic/odp_rwlock.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
diff mbox

Patch

diff --git a/platform/linux-generic/odp_rwlock.c b/platform/linux-generic/odp_rwlock.c
index 11c8dd7..2f6a255 100644
--- a/platform/linux-generic/odp_rwlock.c
+++ b/platform/linux-generic/odp_rwlock.c
@@ -4,6 +4,7 @@ 
  * SPDX-License-Identifier:     BSD-3-Clause
  */
 
+#include <stdbool.h>
 #include <odp_atomic.h>
 #include <odp_rwlock.h>
 
@@ -26,15 +27,18 @@  void odp_rwlock_read_lock(odp_rwlock_t *rwlock)
 			odp_spin();
 			continue;
 		}
-		is_locked = odp_atomic_cmpset_u32(
-					(volatile uint32_t *)&rwlock->cnt,
-					      cnt, cnt + 1);
+		is_locked = __atomic_compare_exchange_n(&rwlock->cnt,
+				&cnt,
+				cnt + 1,
+				false/*strong*/,
+				__ATOMIC_ACQUIRE,
+				__ATOMIC_RELAXED);
 	}
 }
 
 void odp_rwlock_read_unlock(odp_rwlock_t *rwlock)
 {
-	odp_atomic_dec_u32((odp_atomic_u32_t *)(intptr_t)&rwlock->cnt);
+	(void)__atomic_sub_fetch(&rwlock->cnt, 1, __ATOMIC_RELEASE);
 }
 
 void odp_rwlock_write_lock(odp_rwlock_t *rwlock)
@@ -43,19 +47,23 @@  void odp_rwlock_write_lock(odp_rwlock_t *rwlock)
 	int is_locked = 0;
 
 	while (is_locked == 0) {
+		int32_t zero = 0;
 		cnt = rwlock->cnt;
 		/* lock aquired, wait */
 		if (cnt != 0) {
 			odp_spin();
 			continue;
 		}
-		is_locked = odp_atomic_cmpset_u32(
-					(volatile uint32_t *)&rwlock->cnt,
-					      0, -1);
+		is_locked = __atomic_compare_exchange_n(&rwlock->cnt,
+				&zero,
+				-1,
+				false/*strong*/,
+				__ATOMIC_ACQUIRE,
+				__ATOMIC_RELAXED);
 	}
 }
 
 void odp_rwlock_write_unlock(odp_rwlock_t *rwlock)
 {
-	odp_atomic_inc_u32((odp_atomic_u32_t *)(intptr_t)&rwlock->cnt);
+	(void)__atomic_add_fetch(&rwlock->cnt, 1, __ATOMIC_RELEASE);
 }