All of lore.kernel.org
 help / color / mirror / Atom feed
From: Rasmus Villemoes <ravi@prevas.dk>
To: u-boot@lists.denx.de
Cc: Stefan Roese <sr@denx.de>, Tom Rini <trini@konsulko.com>,
	Rasmus Villemoes <ravi@prevas.dk>
Subject: [PATCH 1/3] cyclic: make cyclic_unregister() idempotent
Date: Wed,  7 May 2025 12:58:19 +0200	[thread overview]
Message-ID: <20250507105821.484442-2-ravi@prevas.dk> (raw)
In-Reply-To: <20250507105821.484442-1-ravi@prevas.dk>

Make cyclic_unregister() safe to call with an already unregistered, or
possibly never registered, struct cyclic_info. This is similar to how
the various timer APIs in the linux kernel work (they all allow
calling delete/cancel/... on an inactive timer object).

This means callers don't have to separately keep track of whether
their cyclic callback is registered or not, and avoids them trying to
peek into the struct cyclic_info for that information - which leads to
somewhat ugly code as it would have to be guarded by ifdef
CONFIG_CYCLIC.

Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
---
 common/cyclic.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/common/cyclic.c b/common/cyclic.c
index b695f092f52..75662d9f613 100644
--- a/common/cyclic.c
+++ b/common/cyclic.c
@@ -28,6 +28,18 @@ struct hlist_head *cyclic_get_list(void)
 	return (struct hlist_head *)&gd->cyclic_list;
 }
 
+static bool cyclic_is_registered(const struct cyclic_info *cyclic)
+{
+	const struct cyclic_info *c;
+
+	hlist_for_each_entry(c, cyclic_get_list(), list) {
+		if (c == cyclic)
+			return true;
+	}
+
+	return false;
+}
+
 void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
 		     uint64_t delay_us, const char *name)
 {
@@ -43,6 +55,9 @@ void cyclic_register(struct cyclic_info *cyclic, cyclic_func_t func,
 
 void cyclic_unregister(struct cyclic_info *cyclic)
 {
+	if (!cyclic_is_registered(cyclic))
+		return;
+
 	hlist_del(&cyclic->list);
 }
 
-- 
2.49.0


  reply	other threads:[~2025-05-07 10:59 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-07 10:58 [PATCH 0/3] make cyclic_(un)register idempotent Rasmus Villemoes
2025-05-07 10:58 ` Rasmus Villemoes [this message]
2025-05-08  7:26   ` [PATCH 1/3] cyclic: make cyclic_unregister() idempotent Stefan Roese
2025-05-07 10:58 ` [PATCH 2/3] cyclic: make cyclic_register safe to call on already-registered info Rasmus Villemoes
2025-05-08  7:27   ` Stefan Roese
2025-05-07 10:58 ` [PATCH 3/3] cyclic: document new guarantees for cyclic_(un)register Rasmus Villemoes
2025-05-08  7:27   ` Stefan Roese
2025-05-16 16:41 ` [PATCH 0/3] make cyclic_(un)register idempotent Stefan Roese

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=20250507105821.484442-2-ravi@prevas.dk \
    --to=ravi@prevas.dk \
    --cc=sr@denx.de \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.