[PATCH v3 16/16] RFC: led: migrate from poller to bthread

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Mar 10 08:48:00 GMT 2021


From: Ahmad Fatoum <ahmad at 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 at 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




More information about the barebox mailing list