From patchwork Thu Aug 8 19:47:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 143032 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 453614576E; Thu, 8 Aug 2024 21:48:41 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 193F042E0C; Thu, 8 Aug 2024 21:48:16 +0200 (CEST) Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by mails.dpdk.org (Postfix) with ESMTP id C0D8242DB1 for ; Thu, 8 Aug 2024 21:48:11 +0200 (CEST) Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-70d2ae44790so1063859b3a.2 for ; Thu, 08 Aug 2024 12:48:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1723146491; x=1723751291; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rFBnp8Lh6Mk4fP2ithFemfGBD9iluovj29NuWtDI1fw=; b=sj6D+LsYr2HNIZqkPeNlh0fkjPDJoK40wEjBRnOJi1s3I9WHzRdTXtuWFxT7xpEr7z OP8uvJr2jdbp5+G6jZh/jxy2H+HsFdAleMsiqCNWbZm6R0bsS7S8GNxReVOx1qOgQUrv rF6hYn9TWYHq6lcB6iZH34cKFwod9FuZWe3KUhlkaYr9UAFVShmcCCPi946eQpdd4dgB 8tH4daTuljp8WEEUVBhg7DHzPbVKCmZ8wtmLMCJlASa9rOhQGCAe45Lf+ahGGl9LD97i /BVypP2L8HJD0vxA3O4fWzmaMw8M4py3qYjY4eZTd0oIQJVa+PdHYB/bpSYIVyYrpmLe JQrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723146491; x=1723751291; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rFBnp8Lh6Mk4fP2ithFemfGBD9iluovj29NuWtDI1fw=; b=d2SIqTe7wlXoJN3BvPMBSjJbA+GCh/pcA66UEGL54Kz9eAtIc9g+lrCZkCHK6nZA6C peikICBuRrYatOYpKFvlOvDrGW0Xpj1aVn3/5kWzc9B1GnDdgRC7+WeRek+pEPSk3WiW sJ14he9Xh8b4LbTb76fwBQgnJAnrtLnXZ0piHVeXA2qtqS7nrR/q3SUiI8H2vFPYwrQD X7er3I8p3FzTM8NLk6l/KgKCSi3f+CUgx/07/R2lWeJSUtBN5p+yknsEq/30mhvYI0me 0zsfstUPmV83Eex4UtAm7Wk/Aq/V8TIQwK6WJ4p+mieojFr5OfXsV0uz22ouJiQmpNy+ NAVA== X-Gm-Message-State: AOJu0YwCemQI1u4TKYP4I/1TnxRFkSB7BIOM/YPkcI9VvFJZUzTAud4e 2LPiYxwwOwrWP1i7t7nV+CKMBF/3+PtWtlZvhDeXTy63FUvmWOd/w3sK6dSEX3QSS2kEjYu/zJ6 n X-Google-Smtp-Source: AGHT+IGK4TC12jTVP2MBjt5PLm95iYAo6huf+1RpLK48sZKiVvFkMQRz6uuD/JROCb0oqEMDHZi3mg== X-Received: by 2002:a05:6a00:3e02:b0:70d:2b23:a720 with SMTP id d2e1a72fcca58-710cad45100mr3289522b3a.5.1723146490823; Thu, 08 Aug 2024 12:48:10 -0700 (PDT) Received: from hermes.local (204-195-96-226.wavecable.com. [204.195.96.226]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-710cb2d376csm1469804b3a.111.2024.08.08.12.48.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Aug 2024 12:48:10 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH 5/5] eal: simplify eal alarm cancel by using LIST_FOREACH_SAFE Date: Thu, 8 Aug 2024 12:47:01 -0700 Message-ID: <20240808194756.167664-6-stephen@networkplumber.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240808194756.167664-1-stephen@networkplumber.org> References: <20240808194756.167664-1-stephen@networkplumber.org> MIME-Version: 1.0 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.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 --- 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);