From 008c4b3c3115f7f95467773f12bad0db7649e786 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Tue, 21 May 2024 10:46:52 +0200 Subject: cyclic: make clients embed a struct cyclic_info in their own data structure There are of course not a whole lot of examples in-tree yet, but before they appear, let's make this API change: Instead of separately allocating a 'struct cyclic_info', make the users embed such an instance in their own structure, and make the convention that the callback simply receives the 'struct cyclic_info *', from which the clients can get their own data using the container_of() macro. This has a number of advantages. First, it means cyclic_register() simply cannot fail, simplifying the code. The necessary storage will simply be allocated automatically when the client's own structure is allocated (often via uclass_priv_auto or similar). Second, code for which CONFIG_CYCLIC is just an option can more easily be written without #ifdefs, if we just provide an empty struct cyclic_info {}. For example, the nested CONFIG_IS_ENABLED()s in https://lore.kernel.org/u-boot/20240316201416.211480-1-marek.vasut+renesas@mailbox.org/ are mostly due to the existence of the 'struct cyclic_info *' member being guarded by #ifdef CONFIG_CYCLIC. And we do probably want to avoid the extra memory overhead of that member when !CONFIG_CYCLIC. But that is automatic if, instead of a 'struct cyclic_info *', one simply embeds a 'struct cyclic_info', which will have size 0 when !CONFIG_CYCLIC. Also, the no-op cyclic_register() function can just unconditionally be called, and the compiler will see that (1) the callback is referenced, so not emit a warning for a maybe-unused function and (2) see that it can actually never be reached, so not emit any code for it. Reviewed-by: Stefan Roese Signed-off-by: Rasmus Villemoes --- test/common/cyclic.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'test') diff --git a/test/common/cyclic.c b/test/common/cyclic.c index 461f8cf91f4..51a07c576b6 100644 --- a/test/common/cyclic.c +++ b/test/common/cyclic.c @@ -12,22 +12,27 @@ #include /* Test that cyclic function is called */ -static bool cyclic_active = false; +static struct cyclic_test { + struct cyclic_info cyclic; + bool called; +} cyclic_test; -static void cyclic_test(void *ctx) +static void test_cb(struct cyclic_info *c) { - cyclic_active = true; + struct cyclic_test *t = container_of(c, struct cyclic_test, cyclic); + t->called = true; } static int dm_test_cyclic_running(struct unit_test_state *uts) { - cyclic_active = false; - ut_assertnonnull(cyclic_register(cyclic_test, 10 * 1000, "cyclic_demo", - NULL)); + cyclic_test.called = false; + cyclic_register(&cyclic_test.cyclic, test_cb, 10 * 1000, "cyclic_test"); /* Execute all registered cyclic functions */ schedule(); - ut_asserteq(true, cyclic_active); + ut_asserteq(true, cyclic_test.called); + + cyclic_unregister(&cyclic_test.cyclic); return 0; } -- cgit