DPDK patches and discussions
 help / color / mirror / Atom feed
From: Tyler Retzlaff <roretzla@linux.microsoft.com>
To: dev@dpdk.org
Cc: Gaetan Rivet <grive@u256.net>,
	Bruce Richardson <bruce.richardson@intel.com>,
	Thomas Monjalon <thomas@monjalon.net>,
	Nicolas Chautru <nicolas.chautru@intel.com>,
	Yipeng Wang <yipeng1.wang@intel.com>,
	Sameh Gobriel <sameh.gobriel@intel.com>,
	Vladimir Medvedkin <vladimir.medvedkin@intel.com>,
	Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>,
	Konstantin Ananyev <konstantin.v.ananyev@yandex.ru>,
	Anatoly Burakov <anatoly.burakov@intel.com>,
	Olivier Matz <olivier.matz@6wind.com>,
	Andrew Rybchenko <andrew.rybchenko@oktetlabs.ru>,
	Joyce Kong <joyce.kong@arm.com>,
	Erik Gabriel Carrillo <erik.g.carrillo@intel.com>,
	Liang Ma <liangma@liangbit.com>,
	Peter Mccarthy <peter.mccarthy@intel.com>,
	Jerin Jacob <jerinj@marvell.com>,
	Maciej Czekaj <mczekaj@marvell.com>,
	David Hunt <david.hunt@intel.com>,
	Ruifeng Wang <ruifeng.wang@arm.com>,
	Min Zhou <zhoumin@loongson.cn>,
	David Christensen <drc@linux.vnet.ibm.com>,
	Stanislaw Kardach <kda@semihalf.com>,
	david.marchand@redhat.com, stephen@networkplumber.org,
	mb@smartsharesystems.com,
	Tyler Retzlaff <roretzla@linux.microsoft.com>
Subject: [PATCH v2 4/4] eal: adapt rte spinlock and rwlock APIs to use C11 atomics
Date: Mon, 31 Jul 2023 22:03:54 -0700	[thread overview]
Message-ID: <1690866234-28365-5-git-send-email-roretzla@linux.microsoft.com> (raw)
In-Reply-To: <1690866234-28365-1-git-send-email-roretzla@linux.microsoft.com>

Adapt rte_spinlock.h and rte_rwlock.h APIs to use standard C11 atomics.
Update consumers of the spinlock and rwlock APIs for the API break.

Signed-off-by: Tyler Retzlaff <roretzla@linux.microsoft.com>
---
 lib/eal/include/generic/rte_rwlock.h   | 46 ++++++++++++++++++----------------
 lib/eal/include/generic/rte_spinlock.h | 21 ++++++++--------
 lib/eal/x86/include/rte_spinlock.h     |  2 +-
 3 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/lib/eal/include/generic/rte_rwlock.h b/lib/eal/include/generic/rte_rwlock.h
index 9e083bb..b659c4c 100644
--- a/lib/eal/include/generic/rte_rwlock.h
+++ b/lib/eal/include/generic/rte_rwlock.h
@@ -22,6 +22,8 @@
  *  https://locklessinc.com/articles/locks/
  */
 
+#include <stdatomic.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -57,7 +59,7 @@
 #define RTE_RWLOCK_READ	 0x4	/* Reader increment */
 
 typedef struct __rte_lockable {
-	int32_t cnt;
+	int32_t _Atomic cnt;
 } rte_rwlock_t;
 
 /**
@@ -92,21 +94,21 @@
 
 	while (1) {
 		/* Wait while writer is present or pending */
-		while (__atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED)
+		while (atomic_load_explicit(&rwl->cnt, memory_order_relaxed)
 		       & RTE_RWLOCK_MASK)
 			rte_pause();
 
 		/* Try to get read lock */
-		x = __atomic_fetch_add(&rwl->cnt, RTE_RWLOCK_READ,
-				       __ATOMIC_ACQUIRE) + RTE_RWLOCK_READ;
+		x = atomic_fetch_add_explicit(&rwl->cnt, RTE_RWLOCK_READ,
+				       memory_order_acquire) + RTE_RWLOCK_READ;
 
 		/* If no writer, then acquire was successful */
 		if (likely(!(x & RTE_RWLOCK_MASK)))
 			return;
 
 		/* Lost race with writer, backout the change. */
-		__atomic_fetch_sub(&rwl->cnt, RTE_RWLOCK_READ,
-				   __ATOMIC_RELAXED);
+		atomic_fetch_sub_explicit(&rwl->cnt, RTE_RWLOCK_READ,
+				   memory_order_relaxed);
 	}
 }
 
@@ -127,20 +129,20 @@
 {
 	int32_t x;
 
-	x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
+	x = atomic_load_explicit(&rwl->cnt, memory_order_relaxed);
 
 	/* fail if write lock is held or writer is pending */
 	if (x & RTE_RWLOCK_MASK)
 		return -EBUSY;
 
 	/* Try to get read lock */
-	x = __atomic_fetch_add(&rwl->cnt, RTE_RWLOCK_READ,
-			       __ATOMIC_ACQUIRE) + RTE_RWLOCK_READ;
+	x = atomic_fetch_add_explicit(&rwl->cnt, RTE_RWLOCK_READ,
+			       memory_order_acquire) + RTE_RWLOCK_READ;
 
 	/* Back out if writer raced in */
 	if (unlikely(x & RTE_RWLOCK_MASK)) {
-		__atomic_fetch_sub(&rwl->cnt, RTE_RWLOCK_READ,
-				   __ATOMIC_RELEASE);
+		atomic_fetch_sub_explicit(&rwl->cnt, RTE_RWLOCK_READ,
+				   memory_order_release);
 
 		return -EBUSY;
 	}
@@ -158,7 +160,7 @@
 	__rte_unlock_function(rwl)
 	__rte_no_thread_safety_analysis
 {
-	__atomic_fetch_sub(&rwl->cnt, RTE_RWLOCK_READ, __ATOMIC_RELEASE);
+	atomic_fetch_sub_explicit(&rwl->cnt, RTE_RWLOCK_READ, memory_order_release);
 }
 
 /**
@@ -178,10 +180,10 @@
 {
 	int32_t x;
 
-	x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
+	x = atomic_load_explicit(&rwl->cnt, memory_order_relaxed);
 	if (x < RTE_RWLOCK_WRITE &&
-	    __atomic_compare_exchange_n(&rwl->cnt, &x, x + RTE_RWLOCK_WRITE,
-					1, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
+	    atomic_compare_exchange_weak_explicit(&rwl->cnt, &x, x + RTE_RWLOCK_WRITE,
+					memory_order_acquire, memory_order_relaxed))
 		return 0;
 	else
 		return -EBUSY;
@@ -201,22 +203,22 @@
 	int32_t x;
 
 	while (1) {
-		x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
+		x = atomic_load_explicit(&rwl->cnt, memory_order_relaxed);
 
 		/* No readers or writers? */
 		if (likely(x < RTE_RWLOCK_WRITE)) {
 			/* Turn off RTE_RWLOCK_WAIT, turn on RTE_RWLOCK_WRITE */
-			if (__atomic_compare_exchange_n(&rwl->cnt, &x, RTE_RWLOCK_WRITE, 1,
-							__ATOMIC_ACQUIRE, __ATOMIC_RELAXED))
+			if (atomic_compare_exchange_weak_explicit(&rwl->cnt, &x, RTE_RWLOCK_WRITE,
+							memory_order_acquire, memory_order_relaxed))
 				return;
 		}
 
 		/* Turn on writer wait bit */
 		if (!(x & RTE_RWLOCK_WAIT))
-			__atomic_fetch_or(&rwl->cnt, RTE_RWLOCK_WAIT, __ATOMIC_RELAXED);
+			atomic_fetch_or_explicit(&rwl->cnt, RTE_RWLOCK_WAIT, memory_order_relaxed);
 
 		/* Wait until no readers before trying again */
-		while (__atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED) > RTE_RWLOCK_WAIT)
+		while (atomic_load_explicit(&rwl->cnt, memory_order_relaxed) > RTE_RWLOCK_WAIT)
 			rte_pause();
 
 	}
@@ -233,7 +235,7 @@
 	__rte_unlock_function(rwl)
 	__rte_no_thread_safety_analysis
 {
-	__atomic_fetch_sub(&rwl->cnt, RTE_RWLOCK_WRITE, __ATOMIC_RELEASE);
+	atomic_fetch_sub_explicit(&rwl->cnt, RTE_RWLOCK_WRITE, memory_order_release);
 }
 
 /**
@@ -247,7 +249,7 @@
 static inline int
 rte_rwlock_write_is_locked(rte_rwlock_t *rwl)
 {
-	if (__atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED) & RTE_RWLOCK_WRITE)
+	if (atomic_load_explicit(&rwl->cnt, memory_order_relaxed) & RTE_RWLOCK_WRITE)
 		return 1;
 
 	return 0;
diff --git a/lib/eal/include/generic/rte_spinlock.h b/lib/eal/include/generic/rte_spinlock.h
index c50ebaa..d92432d 100644
--- a/lib/eal/include/generic/rte_spinlock.h
+++ b/lib/eal/include/generic/rte_spinlock.h
@@ -17,6 +17,8 @@
  * All locks must be initialised before use, and only initialised once.
  */
 
+#include <stdatomic.h>
+
 #include <rte_lcore.h>
 #ifdef RTE_FORCE_INTRINSICS
 #include <rte_common.h>
@@ -28,7 +30,7 @@
  * The rte_spinlock_t type.
  */
 typedef struct __rte_lockable {
-	volatile int locked; /**< lock status 0 = unlocked, 1 = locked */
+	int _Atomic locked; /**< lock status 0 = unlocked, 1 = locked */
 } rte_spinlock_t;
 
 /**
@@ -65,10 +67,10 @@
 {
 	int exp = 0;
 
-	while (!__atomic_compare_exchange_n(&sl->locked, &exp, 1, 0,
-				__ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
-		rte_wait_until_equal_32((volatile uint32_t *)&sl->locked,
-			       0, __ATOMIC_RELAXED);
+	while (!atomic_compare_exchange_strong_explicit(&sl->locked, &exp, 1,
+				memory_order_acquire, memory_order_relaxed)) {
+		rte_wait_until_equal_32((uint32_t _Atomic *)&sl->locked,
+			       0, memory_order_relaxed);
 		exp = 0;
 	}
 }
@@ -89,7 +91,7 @@
 rte_spinlock_unlock(rte_spinlock_t *sl)
 	__rte_no_thread_safety_analysis
 {
-	__atomic_store_n(&sl->locked, 0, __ATOMIC_RELEASE);
+	atomic_store_explicit(&sl->locked, 0, memory_order_release);
 }
 #endif
 
@@ -112,9 +114,8 @@
 	__rte_no_thread_safety_analysis
 {
 	int exp = 0;
-	return __atomic_compare_exchange_n(&sl->locked, &exp, 1,
-				0, /* disallow spurious failure */
-				__ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
+	return atomic_compare_exchange_strong_explicit(&sl->locked, &exp, 1,
+				memory_order_acquire, memory_order_relaxed);
 }
 #endif
 
@@ -128,7 +129,7 @@
  */
 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
 {
-	return __atomic_load_n(&sl->locked, __ATOMIC_ACQUIRE);
+	return atomic_load_explicit(&sl->locked, memory_order_acquire);
 }
 
 /**
diff --git a/lib/eal/x86/include/rte_spinlock.h b/lib/eal/x86/include/rte_spinlock.h
index 0b20ddf..3e7f8ac 100644
--- a/lib/eal/x86/include/rte_spinlock.h
+++ b/lib/eal/x86/include/rte_spinlock.h
@@ -78,7 +78,7 @@ static inline int rte_tm_supported(void)
 }
 
 static inline int
-rte_try_tm(volatile int *lock)
+rte_try_tm(int _Atomic *lock)
 {
 	int i, retries;
 
-- 
1.8.3.1


  parent reply	other threads:[~2023-08-01  5:04 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-31 21:07 [PATCH 0/4] eal: update public API to use stdatomic atomics Tyler Retzlaff
2023-07-31 21:07 ` [PATCH 1/4] build: require minimum c11 compiler Tyler Retzlaff
2023-07-31 21:07 ` [PATCH 2/4] devtools: forbid use of GCC atomic builtins Tyler Retzlaff
2023-07-31 21:07 ` [PATCH 3/4] eal: adapt rte pause APIs to use C11 atomics Tyler Retzlaff
2023-07-31 21:07 ` [PATCH 4/4] eal: adapt rte spinlock and rwlock " Tyler Retzlaff
2023-08-01  5:03 ` [PATCH v2 0/4] eal: update public API to use stdatomic atomics Tyler Retzlaff
2023-08-01  5:03   ` [PATCH v2 1/4] build: require minimum c11 compiler Tyler Retzlaff
2023-08-01  5:03   ` [PATCH v2 2/4] devtools: forbid use of GCC atomic builtins Tyler Retzlaff
2023-08-01  5:03   ` [PATCH v2 3/4] eal: adapt rte pause APIs to use C11 atomics Tyler Retzlaff
2023-08-01  5:03   ` Tyler Retzlaff [this message]
2023-08-01  7:33   ` [PATCH v2 0/4] eal: update public API to use stdatomic atomics Morten Brørup
2023-08-01 17:07   ` Tyler Retzlaff
2023-08-02  5:13 ` [PATCH v3 " Tyler Retzlaff
2023-08-02  5:13   ` [PATCH v3 1/4] build: require minimum c11 compiler Tyler Retzlaff
2023-08-02  5:13   ` [PATCH v3 2/4] devtools: forbid use of GCC atomic builtins Tyler Retzlaff
2023-08-02  5:13   ` [PATCH v3 3/4] eal: adapt rte pause APIs to use C11 atomics Tyler Retzlaff
2023-08-02  5:13   ` [PATCH v3 4/4] eal: adapt rte spinlock and rwlock " Tyler Retzlaff
2023-08-02  5:31 ` [PATCH v4 0/4] eal: update public API to use stdatomic atomics Tyler Retzlaff
2023-08-02  5:31   ` [PATCH v4 1/4] build: require minimum c11 compiler Tyler Retzlaff
2023-08-02  5:31   ` [PATCH v4 2/4] devtools: forbid use of GCC atomic builtins Tyler Retzlaff
2023-08-02  5:31   ` [PATCH v4 3/4] eal: adapt rte pause APIs to use C11 atomics Tyler Retzlaff
2023-08-02  5:31   ` [PATCH v4 4/4] eal: adapt rte spinlock and rwlock " Tyler Retzlaff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1690866234-28365-5-git-send-email-roretzla@linux.microsoft.com \
    --to=roretzla@linux.microsoft.com \
    --cc=anatoly.burakov@intel.com \
    --cc=andrew.rybchenko@oktetlabs.ru \
    --cc=bruce.richardson@intel.com \
    --cc=david.hunt@intel.com \
    --cc=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=drc@linux.vnet.ibm.com \
    --cc=erik.g.carrillo@intel.com \
    --cc=grive@u256.net \
    --cc=honnappa.nagarahalli@arm.com \
    --cc=jerinj@marvell.com \
    --cc=joyce.kong@arm.com \
    --cc=kda@semihalf.com \
    --cc=konstantin.v.ananyev@yandex.ru \
    --cc=liangma@liangbit.com \
    --cc=mb@smartsharesystems.com \
    --cc=mczekaj@marvell.com \
    --cc=nicolas.chautru@intel.com \
    --cc=olivier.matz@6wind.com \
    --cc=peter.mccarthy@intel.com \
    --cc=ruifeng.wang@arm.com \
    --cc=sameh.gobriel@intel.com \
    --cc=stephen@networkplumber.org \
    --cc=thomas@monjalon.net \
    --cc=vladimir.medvedkin@intel.com \
    --cc=yipeng1.wang@intel.com \
    --cc=zhoumin@loongson.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).