DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH 5/5] eal: simplify eal alarm cancel by using LIST_FOREACH_SAFE
Date: Thu,  8 Aug 2024 12:47:01 -0700	[thread overview]
Message-ID: <20240808194756.167664-6-stephen@networkplumber.org> (raw)
In-Reply-To: <20240808194756.167664-1-stephen@networkplumber.org>

The code for rte_eal_alarm_cancel was using two loops and duplicate
code to handle cancel at the start of the list. Introduce the
LIST_FOREACH_SAFE() macro from FreeBsd which makes writing
the loop cleaner and simpler.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 lib/eal/linux/eal_alarm.c | 51 +++++++++++++++------------------------
 1 file changed, 19 insertions(+), 32 deletions(-)

diff --git a/lib/eal/linux/eal_alarm.c b/lib/eal/linux/eal_alarm.c
index eeb096213b..3f3b722421 100644
--- a/lib/eal/linux/eal_alarm.c
+++ b/lib/eal/linux/eal_alarm.c
@@ -53,6 +53,13 @@ static struct rte_intr_handle *intr_handle;
 static int handler_registered = 0;
 static void eal_alarm_callback(void *arg);
 
+#ifndef LIST_FOREACH_SAFE
+#define LIST_FOREACH_SAFE(var, head, field, tvar)			\
+	for ((var) = LIST_FIRST((head));				\
+	    (var) && ((tvar) = LIST_NEXT((var), field), 1);		\
+	    (var) = (tvar))
+#endif
+
 void
 rte_eal_alarm_cleanup(void)
 {
@@ -194,7 +201,7 @@ rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg)
 int
 rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 {
-	struct alarm_entry *ap, *ap_prev;
+	struct alarm_entry *ap, *tmp;
 	int count = 0;
 	int err = 0;
 	int executing;
@@ -207,46 +214,26 @@ rte_eal_alarm_cancel(rte_eal_alarm_callback cb_fn, void *cb_arg)
 	do {
 		executing = 0;
 		rte_spinlock_lock(&alarm_list_lk);
-		/* remove any matches at the start of the list */
-		while ((ap = LIST_FIRST(&alarm_list)) != NULL &&
-				cb_fn == ap->cb_fn &&
-				(cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
-
-			if (ap->executing == 0) {
-				LIST_REMOVE(ap, next);
-				free(ap);
-				count++;
-			} else {
-				/* If calling from other context, mark that alarm is executing
-				 * so loop can spin till it finish. Otherwise we are trying to
-				 * cancel our self - mark it by EINPROGRESS */
-				if (pthread_equal(ap->executing_id, pthread_self()) == 0)
-					executing++;
-				else
-					err = EINPROGRESS;
-
-				break;
-			}
-		}
-		ap_prev = ap;
 
-		/* now go through list, removing entries not at start */
-		LIST_FOREACH(ap, &alarm_list, next) {
+		LIST_FOREACH_SAFE(ap, &alarm_list, next, tmp) {
 			/* this won't be true first time through */
 			if (cb_fn == ap->cb_fn &&
 					(cb_arg == (void *)-1 || cb_arg == ap->cb_arg)) {
-
 				if (ap->executing == 0) {
 					LIST_REMOVE(ap, next);
 					free(ap);
 					count++;
-					ap = ap_prev;
-				} else if (pthread_equal(ap->executing_id, pthread_self()) == 0)
-					executing++;
-				else
-					err = EINPROGRESS;
+				} else {
+					/* If calling from other context, mark that alarm is executing
+					 * so loop can spin till it finish. Otherwise we are trying to
+					 * cancel our self - mark it by EINPROGRESS
+					 */
+					if (pthread_equal(ap->executing_id, pthread_self()) == 0)
+						executing++;
+					else
+						err = EINPROGRESS;
+				}
 			}
-			ap_prev = ap;
 		}
 		rte_spinlock_unlock(&alarm_list_lk);
 	} while (executing != 0);
-- 
2.43.0


  parent reply	other threads:[~2024-08-08 19:48 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-08 19:46 [PATCH 0/5] alarm related patches Stephen Hemminger
2024-08-08 19:46 ` [PATCH 1/5] eal: add missing parameter check to rte_eal_alarm_set on Windows Stephen Hemminger
2024-08-08 19:46 ` [PATCH 2/5] Revert "test/alarm: disable bad time cases on Windows" Stephen Hemminger
2024-08-09  7:23   ` Dmitry Kozlyuk
2024-08-09  7:33     ` Dmitry Kozlyuk
2024-08-09 14:59     ` Stephen Hemminger
2024-08-08 19:46 ` [PATCH 3/5] test: support alarm test on FreeBSD Stephen Hemminger
2024-08-08 19:47 ` [PATCH 4/5] test/alarm: rewrite the alarm test Stephen Hemminger
2024-08-08 19:47 ` Stephen Hemminger [this message]
2024-08-09  8:33   ` [PATCH 5/5] eal: simplify eal alarm cancel by using LIST_FOREACH_SAFE Morten Brørup
2024-08-09 15:00     ` Stephen Hemminger
2024-08-09 15:24 ` [PATCH v2 0/3] alarm test fixes Stephen Hemminger
2024-08-09 15:24   ` [PATCH v2 1/3] eal: add missing parameter check to rte_eal_alarm_set on Windows Stephen Hemminger
2024-08-09 15:24   ` [PATCH v2 2/3] test: support alarm test on FreeBSD Stephen Hemminger
2024-08-09 15:24   ` [PATCH v2 3/3] test/alarm: rewrite the alarm test Stephen Hemminger
2024-09-23  2:17   ` [PATCH v2 0/3] alarm test fixes fengchengwen
2024-10-04 11:58   ` David Marchand

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=20240808194756.167664-6-stephen@networkplumber.org \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    /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).