diff mbox series

[v2,08/14] lib: time: hook uthread_schedule() into udelay()

Message ID f9568a6c5140b99cbd5b48de21f4d5b12a76d92e.1740499185.git.jerome.forissier@linaro.org
State New
Headers show
Series Uthreads | expand

Commit Message

Jerome Forissier Feb. 25, 2025, 4:34 p.m. UTC
Introduce a uthread scheduling loop into udelay() when CONFIG_UTHREAD
is enabled. This means that any uthread calling into udelay() may yield
to uthread and be scheduled again later.

While not strictly necessary since uthread_schedule() is already called
by schedule(), tests show that it is desirable to call it in a tight
loop instead of calling __usleep(). It gives more opportunities for
other threads to make progress and results in better performances.

Signed-off-by: Jerome Forissier <jerome.forissier@linaro.org>
---
 lib/time.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/time.c b/lib/time.c
index d88edafb196..d1a1a66f301 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -17,6 +17,7 @@ 
 #include <asm/global_data.h>
 #include <asm/io.h>
 #include <linux/delay.h>
+#include <uthread.h>
 
 #ifndef CFG_WD_PERIOD
 # define CFG_WD_PERIOD	(10 * 1000 * 1000)	/* 10 seconds default */
@@ -197,7 +198,14 @@  void udelay(unsigned long usec)
 	do {
 		schedule();
 		kv = usec > CFG_WD_PERIOD ? CFG_WD_PERIOD : usec;
-		__udelay(kv);
+		if (CONFIG_IS_ENABLED(UTHREAD)) {
+			ulong t0 = timer_get_us();
+			while (timer_get_us() < t0 + kv)
+				uthread_schedule();
+		} else {
+			__udelay(kv);
+		}
 		usec -= kv;
 	} while(usec);
+
 }