summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig21
-rw-r--r--lib/Makefile2
-rw-r--r--lib/time.c9
-rw-r--r--lib/uthread.c165
4 files changed, 196 insertions, 1 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index ac34ec45bb1..b2aecd8a49e 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -1258,6 +1258,27 @@ 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 32768
+ help
+ The default stack size for uthreads. Each uthread has its own stack.
+ When the stack_sz argument to uthread_create() is zero then this
+ value is used.
+
endmenu
source "lib/fwu_updates/Kconfig"
diff --git a/lib/Makefile b/lib/Makefile
index 7178c5a3e3c..18ae0cd87bf 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -159,6 +159,8 @@ obj-$(CONFIG_LIB_ELF) += elf.o
obj-$(CONFIG_$(PHASE_)SEMIHOSTING) += semihosting.o
+obj-$(CONFIG_UTHREAD) += uthread.o
+
#
# Build a fast OID lookup registry from include/linux/oid_registry.h
#
diff --git a/lib/time.c b/lib/time.c
index d88edafb196..0e9b079f9cf 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,13 @@ 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);
}
diff --git a/lib/uthread.c b/lib/uthread.c
new file mode 100644
index 00000000000..062fca7d209
--- /dev/null
+++ b/lib/uthread.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 Ahmad Fatoum, Pengutronix
+ * Copyright (C) 2025 Linaro Limited
+ *
+ * An implementation of cooperative multi-tasking inspired from barebox threads
+ * https://github.com/barebox/barebox/blob/master/common/bthread.c
+ */
+
+#include <compiler.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <malloc.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include <uthread.h>
+
+static struct uthread main_thread = {
+ .list = LIST_HEAD_INIT(main_thread.list),
+};
+
+static struct uthread *current = &main_thread;
+
+/**
+ * uthread_trampoline() - Call the current thread's entry point then resume the
+ * main thread.
+ *
+ * This is a helper function which is used as the @func argument to the
+ * initjmp() function, and ultimately invoked via setjmp(). It does not return
+ * but instead longjmp()'s back to the main thread.
+ */
+static void __noreturn uthread_trampoline(void)
+{
+ struct uthread *curr = current;
+
+ curr->fn(curr->arg);
+ curr->done = true;
+ current = &main_thread;
+ longjmp(current->ctx, 1);
+ /* Not reached */
+ while (true)
+ ;
+}
+
+/**
+ * uthread_free() - Free memory used by a uthread object.
+ */
+static void uthread_free(struct uthread *uthread)
+{
+ if (!uthread)
+ return;
+ free(uthread->stack);
+ free(uthread);
+}
+
+int uthread_create(struct uthread *uthr, void (*fn)(void *), void *arg,
+ size_t stack_sz, unsigned int grp_id)
+{
+ bool user_allocated = false;
+
+ if (!stack_sz)
+ stack_sz = CONFIG_UTHREAD_STACK_SIZE;
+
+ if (uthr) {
+ user_allocated = true;
+ } else {
+ uthr = calloc(1, sizeof(*uthr));
+ if (!uthr)
+ return -1;
+ }
+
+ uthr->stack = memalign(16, stack_sz);
+ if (!uthr->stack)
+ goto err;
+
+ uthr->fn = fn;
+ uthr->arg = arg;
+ uthr->grp_id = grp_id;
+
+ list_add_tail(&uthr->list, &current->list);
+
+ initjmp(uthr->ctx, uthread_trampoline, uthr->stack, stack_sz);
+
+ return 0;
+err:
+ if (!user_allocated)
+ free(uthr);
+ return -1;
+}
+
+/**
+ * uthread_resume() - switch execution to a given thread
+ *
+ * @uthread: the thread object that should be resumed
+ */
+static void uthread_resume(struct uthread *uthread)
+{
+ if (!setjmp(current->ctx)) {
+ current = uthread;
+ longjmp(uthread->ctx, 1);
+ }
+}
+
+bool uthread_schedule(void)
+{
+ struct uthread *next;
+ struct uthread *tmp;
+
+ list_for_each_entry_safe(next, tmp, &current->list, list) {
+ if (!next->done) {
+ uthread_resume(next);
+ return true;
+ }
+ /* Found a 'done' thread, free its resources */
+ list_del(&next->list);
+ uthread_free(next);
+ }
+ return false;
+}
+
+unsigned int uthread_grp_new_id(void)
+{
+ static unsigned int id;
+
+ return ++id;
+}
+
+bool uthread_grp_done(unsigned int grp_id)
+{
+ struct uthread *next;
+
+ list_for_each_entry(next, &main_thread.list, list) {
+ if (next->grp_id == grp_id && !next->done)
+ return false;
+ }
+
+ return true;
+}
+
+int uthread_mutex_lock(struct uthread_mutex *mutex)
+{
+ while (mutex->state == UTHREAD_MUTEX_LOCKED)
+ uthread_schedule();
+
+ mutex->state = UTHREAD_MUTEX_LOCKED;
+ return 0;
+}
+
+int uthread_mutex_trylock(struct uthread_mutex *mutex)
+{
+ if (mutex->state == UTHREAD_MUTEX_UNLOCKED) {
+ mutex->state = UTHREAD_MUTEX_LOCKED;
+ return 0;
+ }
+
+ return -EBUSY;
+}
+
+int uthread_mutex_unlock(struct uthread_mutex *mutex)
+{
+ mutex->state = UTHREAD_MUTEX_UNLOCKED;
+
+ return 0;
+}