[PATCH v3 2/4] slice: reschedule bthreads doing File I/O in command context

Ahmad Fatoum a.fatoum at pengutronix.de
Mon May 3 05:45:06 PDT 2021

Currently, without locks, it's unsafe to do file I/O outside of
the main thread, be it in pollers or secondary bthreads.

Pollers side step this issue by deferring file I/O to workqueues
that run in command context: the time when the shell is waiting
for input.

For bthreads, we can do something similar without involving
workqueues: We just suspend threads until they are scheduled in
command context.

This is not a full solution: Not everything that should
assert_command_context() does so, so you still can't do absolutely
everything in a secondary bthread. To get there, we will need
to distribute locks across the code base, but that's a patchset
for another time.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
 include/slice.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/include/slice.h b/include/slice.h
index cf684300a809..abe819163d32 100644
--- a/include/slice.h
+++ b/include/slice.h
@@ -40,8 +40,10 @@ extern int poller_active;
 #define assert_command_context() do { \
 		  "%s called in poller\n", __func__); \
-	WARN_ONCE(IS_ENABLED(CONFIG_BTHREAD) && !bthread_is_main(current), \
-		  "%s called in secondary bthread\n", __func__); \
+	if (IS_ENABLED(CONFIG_BTHREAD) && !bthread_is_main(current)) { \
+		while (slice_acquired(&command_slice)) \
+			bthread_reschedule(); \
+	} \
 } while (0)

More information about the barebox mailing list