[v3,1/4] eal: use C11 atomic builtins for already initialized check
diff mbox series

Message ID 1600925968-18278-2-git-send-email-phil.yang@arm.com
State Accepted
Delegated to: David Marchand
Headers show
Series
  • use C11 atomic builtins for libs
Related show

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Phil Yang Sept. 24, 2020, 5:39 a.m. UTC
Since rte_atomicXX APIs are not allowed to be used, use C11 builtins to
check if EAL is already initialized.

Signed-off-by: Phil Yang <phil.yang@arm.com>
Reviewed-by: Honnappa Nagarahalli <honnappa.nagarahalli@arm.com>
Reviewed-by: Ruifeng Wang <ruifeng.wang@arm.com>
---
 lib/librte_eal/freebsd/eal.c | 19 ++++++++++---------
 lib/librte_eal/linux/eal.c   | 21 +++++++++++----------
 2 files changed, 21 insertions(+), 19 deletions(-)

Patch
diff mbox series

diff --git a/lib/librte_eal/freebsd/eal.c b/lib/librte_eal/freebsd/eal.c
index 798add0..ccea60a 100644
--- a/lib/librte_eal/freebsd/eal.c
+++ b/lib/librte_eal/freebsd/eal.c
@@ -41,7 +41,6 @@ 
 #include <rte_devargs.h>
 #include <rte_version.h>
 #include <rte_vfio.h>
-#include <rte_atomic.h>
 #include <malloc_heap.h>
 #include <rte_telemetry.h>
 
@@ -665,7 +664,8 @@  rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
 	char thread_name[RTE_MAX_THREAD_NAME_LEN];
 	const struct rte_config *config = rte_eal_get_configuration();
@@ -679,7 +679,8 @@  rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -705,7 +706,7 @@  rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -715,20 +716,20 @@  rte_eal_init(int argc, char **argv)
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_trace_init() < 0) {
 		rte_eal_init_alert("Cannot init trace");
 		rte_errno = EFAULT;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -762,7 +763,7 @@  rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -790,7 +791,7 @@  rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
diff --git a/lib/librte_eal/linux/eal.c b/lib/librte_eal/linux/eal.c
index 0960f01..9cf0e2e 100644
--- a/lib/librte_eal/linux/eal.c
+++ b/lib/librte_eal/linux/eal.c
@@ -47,7 +47,6 @@ 
 #include <rte_dev.h>
 #include <rte_devargs.h>
 #include <rte_version.h>
-#include <rte_atomic.h>
 #include <malloc_heap.h>
 #include <rte_vfio.h>
 #include <rte_telemetry.h>
@@ -960,7 +959,8 @@  rte_eal_init(int argc, char **argv)
 {
 	int i, fctret, ret;
 	pthread_t thread_id;
-	static rte_atomic32_t run_once = RTE_ATOMIC32_INIT(0);
+	static uint32_t run_once;
+	uint32_t has_run = 0;
 	const char *p;
 	static char logid[PATH_MAX];
 	char cpuset[RTE_CPU_AFFINITY_STR_LEN];
@@ -977,7 +977,8 @@  rte_eal_init(int argc, char **argv)
 		return -1;
 	}
 
-	if (!rte_atomic32_test_and_set(&run_once)) {
+	if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
 		rte_eal_init_alert("already called initialization.");
 		rte_errno = EALREADY;
 		return -1;
@@ -1005,14 +1006,14 @@  rte_eal_init(int argc, char **argv)
 	if (fctret < 0) {
 		rte_eal_init_alert("Invalid 'command line' arguments.");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
 	if (eal_plugins_init() < 0) {
 		rte_eal_init_alert("Cannot init plugins");
 		rte_errno = EINVAL;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1024,7 +1025,7 @@  rte_eal_init(int argc, char **argv)
 
 	if (eal_option_device_parse()) {
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1064,7 +1065,7 @@  rte_eal_init(int argc, char **argv)
 	if (rte_bus_scan()) {
 		rte_eal_init_alert("Cannot scan the buses for devices");
 		rte_errno = ENODEV;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1138,7 +1139,7 @@  rte_eal_init(int argc, char **argv)
 		if (ret < 0) {
 			rte_eal_init_alert("Cannot get hugepage information.");
 			rte_errno = EACCES;
-			rte_atomic32_clear(&run_once);
+			__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 			return -1;
 		}
 	}
@@ -1162,7 +1163,7 @@  rte_eal_init(int argc, char **argv)
 	if (rte_eal_log_init(logid, internal_conf->syslog_facility) < 0) {
 		rte_eal_init_alert("Cannot init logging.");
 		rte_errno = ENOMEM;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 
@@ -1170,7 +1171,7 @@  rte_eal_init(int argc, char **argv)
 	if (rte_eal_vfio_setup() < 0) {
 		rte_eal_init_alert("Cannot init VFIO");
 		rte_errno = EAGAIN;
-		rte_atomic32_clear(&run_once);
+		__atomic_store_n(&run_once, 0, __ATOMIC_RELAXED);
 		return -1;
 	}
 #endif