[RFC PATCH 06/10] uthread: add cooperative multi-tasking interface

Jerome Forissier jerome.forissier at linaro.org
Mon Feb 17 10:48:27 CET 2025



On 2/14/25 19:25, Yao Zi wrote:
> On Fri, Feb 14, 2025 at 03:00:21PM +0100, Jerome Forissier wrote:
>> Add an new internal API called uthread (Kconfig symbol: UTHREAD) which
>> provides cooperative multi-tasking. The goal is to be able to improve
>> the performance of some parts of U-Boot by overlapping lengthy
>> operations. Each uthread has its own stack allocated on the heap. The
>> default stack size is defined by the UTHREAD_STACK_SIZE symbol and is
>> used when uthread_create() is passed zero for the stack_sz argument.
>>
>> The implementation is based on context-switching via initjmp()/setjmp()/
>> longjmp() and is inspired from barebox threads [1].
>>
>> [1] https://github.com/barebox/barebox/blob/master/common/bthread.c
>>
>> Signed-off-by: Jerome Forissier <jerome.forissier at linaro.org>
>> ---
>>  include/uthread.h |  31 ++++++++++++++
>>  lib/Kconfig       |  19 ++++++++
>>  lib/Makefile      |   2 +
>>  lib/uthread.c     | 107 ++++++++++++++++++++++++++++++++++++++++++++++
>>  4 files changed, 159 insertions(+)
>>  create mode 100644 include/uthread.h
>>  create mode 100644 lib/uthread.c
>>
>> diff --git a/lib/Kconfig b/lib/Kconfig
>> index 1a683dea670..c3416bbd0be 100644
>> --- a/lib/Kconfig
>> +++ b/lib/Kconfig
>> @@ -1255,6 +1255,25 @@ config PHANDLE_CHECK_SEQ
>>  	  enable this config option to distinguish them using
>>  	  phandles in fdtdec_get_alias_seq() function.
>>  
>> +config UTHREAD
>> +	bool "Enable thread support"
>> +	depends on HAVE_INITJMP
>> +	help
>> +	  Implement a simple form of cooperative multi-tasking based on
>> +	  context-switching via initjmp(), setjmp() and longjmp(). The
>> +	  uthread_ interface enables the main thread of execution to create
>> +	  one or more secondary threads and schedule them until they all have
>> +	  returned. At any point a thread may suspend its execution and
>> +	  schedule another thread, which allows for the efficient multiplexing
>> +	  of leghthy operations.
>> +
>> +config UTHREAD_STACK_SIZE
>> +	int "Default uthread stack size"
>> +	depends on UTHREAD
>> +	default 32168
> 
> Why choose 32168? It's even not a power of two. Typo for 32768?

It is indeed a typo which I will fix in v2. 32 KiB is quite arbitrary and this
value is used only when the stack_sz argument to uthread_create() is zero
(hence the wording "default stack size"). I believe it is large enough for
most needs, and not too large to not exhaust the heap when a few threads are
created.

> 
>> +	help
>> +	  The default stask size for uthreads. Each uthread has its own stack.
>> +
>>  endmenu
>>  
>>  source "lib/fwu_updates/Kconfig"
> 
> Thanks,
> Yao Zi

Thanks,
-- 
Jerome


More information about the U-Boot mailing list