From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 6C614A0C45;
	Wed, 16 Jun 2021 04:56:19 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id DB16F4111E;
	Wed, 16 Jun 2021 04:56:04 +0200 (CEST)
Received: from foss.arm.com (foss.arm.com [217.140.110.172])
 by mails.dpdk.org (Postfix) with ESMTP id 101AD4111D
 for <dev@dpdk.org>; Wed, 16 Jun 2021 04:56:03 +0200 (CEST)
Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14])
 by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 92AC213A1;
 Tue, 15 Jun 2021 19:56:02 -0700 (PDT)
Received: from net-arm-n1sdp.shanghai.arm.com (net-arm-n1sdp.shanghai.arm.com
 [10.169.208.222])
 by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 79E253F694;
 Tue, 15 Jun 2021 19:55:59 -0700 (PDT)
From: Joyce Kong <joyce.kong@arm.com>
To: thomas@monjalon.net, david.marchand@redhat.com, stephen@networkplumber.org,
 olivier.matz@6wind.com, andrew.rybchenko@oktetlabs.ru,
 harry.van.haaren@intel.com, honnappa.nagarahalli@arm.com,
 ruifeng.wang@arm.com
Cc: dev@dpdk.org,
	nd@arm.com
Date: Tue, 15 Jun 2021 21:54:58 -0500
Message-Id: <20210616025459.22717-8-joyce.kong@arm.com>
X-Mailer: git-send-email 2.17.1
In-Reply-To: <20210616025459.22717-1-joyce.kong@arm.com>
References: <20210604094624.31308-1-joyce.kong@arm.com>
 <20210616025459.22717-1-joyce.kong@arm.com>
Subject: [dpdk-dev] [PATCH v2 7/8] test/service_cores: use GCC atomic
 builtins for lock sync
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>

Convert rte_atomic usages to GCC atomic builtins for lock sync
in service core testcases.

Signed-off-by: Joyce Kong <joyce.kong@arm.com>
Reviewed-by: Ruifeng Wang <ruifeng.wang@arm.com>
Acked-by: Stephen Hemminger <stephen@networkplumber.org>
---
 app/test/test_service_cores.c | 36 +++++++++++++++++++----------------
 1 file changed, 20 insertions(+), 16 deletions(-)

diff --git a/app/test/test_service_cores.c b/app/test/test_service_cores.c
index 37d7172d5..9d908d44e 100644
--- a/app/test/test_service_cores.c
+++ b/app/test/test_service_cores.c
@@ -53,18 +53,20 @@ static int32_t dummy_cb(void *args)
 static int32_t dummy_mt_unsafe_cb(void *args)
 {
 	/* before running test, the initialization has set pass_test to 1.
-	 * If the cmpset in service-cores is working correctly, the code here
+	 * If the cas in service-cores is working correctly, the code here
 	 * should never fail to take the lock. If the lock *is* taken, fail the
 	 * test, because two threads are concurrently in a non-MT safe callback.
 	 */
 	uint32_t *test_params = args;
-	uint32_t *atomic_lock = &test_params[0];
+	uint32_t *lock = &test_params[0];
 	uint32_t *pass_test = &test_params[1];
-	int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
+	uint32_t exp = 0;
+	int lock_taken = __atomic_compare_exchange_n(lock, &exp, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED);
 	if (lock_taken) {
 		/* delay with the lock held */
 		rte_delay_ms(250);
-		rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
+		__atomic_store_n(lock, 0, __ATOMIC_RELAXED);
 	} else {
 		/* 2nd thread will fail to take lock, so set pass flag */
 		*pass_test = 0;
@@ -83,13 +85,15 @@ static int32_t dummy_mt_safe_cb(void *args)
 	 *    that 2 threads are running the callback at the same time: MT safe
 	 */
 	uint32_t *test_params = args;
-	uint32_t *atomic_lock = &test_params[0];
+	uint32_t *lock = &test_params[0];
 	uint32_t *pass_test = &test_params[1];
-	int lock_taken = rte_atomic32_cmpset(atomic_lock, 0, 1);
+	uint32_t exp = 0;
+	int lock_taken = __atomic_compare_exchange_n(lock, &exp, 1, 0,
+					__ATOMIC_RELAXED, __ATOMIC_RELAXED);
 	if (lock_taken) {
 		/* delay with the lock held */
 		rte_delay_ms(250);
-		rte_atomic32_clear((rte_atomic32_t *)atomic_lock);
+		__atomic_store_n(lock, 0, __ATOMIC_RELAXED);
 	} else {
 		/* 2nd thread will fail to take lock, so set pass flag */
 		*pass_test = 1;
@@ -622,9 +626,9 @@ service_threaded_test(int mt_safe)
 	TEST_ASSERT_EQUAL(0, rte_service_lcore_add(slcore_2),
 			"mt safe lcore add fail");
 
-	/* Use atomic locks to verify that two threads are in the same function
-	 * at the same time. These are passed to the unit tests through the
-	 * callback userdata parameter
+	/* Use locks to verify that two threads are in the same function
+	 * at the same time. These are passed to the unit tests through
+	 * the callback userdata parameter.
 	 */
 	uint32_t test_params[2];
 	memset(test_params, 0, sizeof(uint32_t) * 2);
@@ -713,7 +717,7 @@ service_mt_safe_poll(void)
 }
 
 /* tests a NON mt safe service with two cores, the callback is serialized
- * using the atomic cmpset.
+ * using the cas.
  */
 static int
 service_mt_unsafe_poll(void)
@@ -735,17 +739,17 @@ delay_as_a_mt_safe_service(void *args)
 	RTE_SET_USED(args);
 	uint32_t *params = args;
 
-	/* retrieve done flag and atomic lock to inc/dec */
+	/* retrieve done flag and lock to add/sub */
 	uint32_t *done = &params[0];
-	rte_atomic32_t *lock = (rte_atomic32_t *)&params[1];
+	uint32_t *lock = &params[1];
 
 	while (!*done) {
-		rte_atomic32_inc(lock);
+		__atomic_add_fetch(lock, 1, __ATOMIC_RELAXED);
 		rte_delay_us(500);
-		if (rte_atomic32_read(lock) > 1)
+		if (__atomic_load_n(lock, __ATOMIC_RELAXED) > 1)
 			/* pass: second core has simultaneously incremented */
 			*done = 1;
-		rte_atomic32_dec(lock);
+		__atomic_sub_fetch(lock, 1, __ATOMIC_RELAXED);
 	}
 
 	return 0;
-- 
2.17.1