mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Peter Mamonov <pmamonov@gmail.com>,
	Peter Korsgaard <peter@korsgaard.com>,
	Sascha Hauer <sha@pengutronix.de>
Subject: [PATCH v3 16/16] RFC: led: migrate from poller to bthread
Date: Wed, 10 Mar 2021 09:48:00 +0100	[thread overview]
Message-ID: <20210310084800.3584-17-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20210310084800.3584-1-a.fatoum@pengutronix.de>

From: Ahmad Fatoum <ahmad@a3f.at>

Not meant for merge, just to show how a mechanical transformation of
poller to bthread would look like.

Note that this changes behavior slightly: The heartbeat duration was
not affected by the time needed to toggle the LED, but now it is.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 drivers/led/core.c | 52 ++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 25 deletions(-)

diff --git a/drivers/led/core.c b/drivers/led/core.c
index ab171c61845b..cfdc6c894f74 100644
--- a/drivers/led/core.c
+++ b/drivers/led/core.c
@@ -21,7 +21,7 @@
 #include <errno.h>
 #include <led.h>
 #include <init.h>
-#include <poller.h>
+#include <bthread.h>
 #include <clock.h>
 #include <linux/ctype.h>
 
@@ -119,35 +119,34 @@ int led_set(struct led *led, unsigned int value)
 	return __led_set(led, value);
 }
 
-static void led_blink_func(struct poller_struct *poller)
+static int led_blink_func(void *data)
 {
 	struct led *led;
 
-	list_for_each_entry(led, &leds, list) {
-		const uint64_t now = get_time_ns();
-		int on;
+	while (!bthread_should_stop()) {
+		list_for_each_entry(led, &leds, list) {
+			int on;
 
-		if (!led->blink && !led->flash)
-			continue;
+			if (!led->blink && !led->flash)
+				continue;
 
-		if (led->blink_next_event > now) {
-			continue;
-		}
+			on = !(led->blink_next_state % 2);
+			if (on)
+				on = led->max_value;
 
-		on = !(led->blink_next_state % 2);
-		if (on)
-			on = led->max_value;
+			if (led->flash && !on)
+				led->flash = 0;
 
-		led->blink_next_event = now +
-			(led->blink_states[led->blink_next_state] * MSECOND);
-		led->blink_next_state = (led->blink_next_state + 1) %
-					led->blink_nr_states;
+			__led_set(led, on);
 
-		if (led->flash && !on)
-			led->flash = 0;
+			led->blink_next_state = (led->blink_next_state + 1) %
+				led->blink_nr_states;
 
-		__led_set(led, on);
+			mdelay(led->blink_states[led->blink_next_state]);
+		}
 	}
+
+	return 0;
 }
 
 /**
@@ -203,13 +202,16 @@ int led_flash(struct led *led, unsigned int duration_ms)
 	return 0;
 }
 
-static struct poller_struct led_poller = {
-	.func = led_blink_func,
-};
-
 static int led_blink_init(void)
 {
-	return poller_register(&led_poller, "led");
+	struct bthread *led_bthread;
+
+	led_bthread = bthread_create(led_blink_func, NULL, "led");
+	if (!led_bthread)
+		return -ENOMEM;
+
+	bthread_wake(led_bthread);
+	return 0;
 }
 late_initcall(led_blink_init);
 
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  parent reply	other threads:[~2021-03-10  8:49 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-10  8:47 [PATCH v3 00/16] common: introduce bthreads, co-operative Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 01/16] common: introduce HAS_ARCH_SJLJ Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 02/16] ARM: asm: setjmp: annotate setjmp/longjmp for GCC Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 03/16] ARM: asm: setjmp: implement coroutine dependency initjmp() Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 04/16] sandbox: asm: implement setjmp/longjmp/initjmp Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 05/16] riscv: Add asm/asm.h Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 06/16] riscv: Add asm/linkage.h Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 07/16] riscv: Implement setjmp/longjmp/initjmp for RV32I Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 08/16] mips: Add linkage.h Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 09/16] mips: Implement setjmp/longjmp/initjmp for 32BIT Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 10/16] powerpc: Implement initjmp/setjmp/longjmp Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 11/16] x86: implement setjmp/longjmp/initjmp Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 12/16] common: poller: replace explicit calls to poller_call() with resched() Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 13/16] console: unconditionally run resched() in ctrlc() Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 14/16] common: introduce bthreads, co-operative barebox threads Ahmad Fatoum
2021-03-10  8:47 ` [PATCH v3 15/16] commands: add new bthread test command Ahmad Fatoum
2021-03-10  8:48 ` Ahmad Fatoum [this message]
2021-03-15  8:34 ` [PATCH v3 00/16] common: introduce bthreads, co-operative Sascha Hauer

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=20210310084800.3584-17-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=peter@korsgaard.com \
    --cc=pmamonov@gmail.com \
    --cc=sha@pengutronix.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox