blob: f796a16f25f571805ac0a2303b01275be565f986 [file] [log] [blame]
Jerome Forissierc14cfee2025-04-18 16:09:34 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright 2025 Linaro Limited
4 */
5
6#include <linux/list.h>
7#include <linux/types.h>
8#include <setjmp.h>
9
10#ifndef _UTHREAD_H_
11#define _UTHREAD_H_
12
13/**
14 * DOC: Overview
15 *
16 * The uthread framework is a basic task scheduler that allows to run functions
17 * "in parallel" on a single CPU core. The scheduling is cooperative, not
18 * preemptive -- meaning that context switches from one task to another task is
19 * voluntary, via a call to uthread_schedule(). This characteristic makes thread
20 * synchronization much easier, because a thread cannot be interrupted in the
21 * middle of a critical section (reading from or writing to shared state, for
22 * instance).
23 *
24 * CONFIG_UTHREAD in lib/Kconfig enables the uthread framework. When disabled,
25 * the uthread_create() and uthread_schedule() functions may still be used so
26 * that code differences between uthreads enabled and disabled can be reduced to
27 * a minimum.
28 */
29
30/**
31 * struct uthread - a thread object
32 *
33 * @fn: thread entry point
34 * @arg: argument passed to the entry point when the thread is started
35 * @ctx: context to resume execution of this thread (via longjmp())
36 * @stack: initial stack pointer for the thread
37 * @done: true once @fn has returned, false otherwise
38 * @grp_id: user-supplied identifier for this thread and possibly others. A
39 * thread can belong to zero or one group (not more), and a group may contain
40 * any number of threads.
41 * @list: link in the global scheduler list
42 */
43struct uthread {
44 void (*fn)(void *arg);
45 void *arg;
46 jmp_buf ctx;
47 void *stack;
48 bool done;
49 unsigned int grp_id;
50 struct list_head list;
51};
52
53#ifdef CONFIG_UTHREAD
54
55/**
56 * uthread_create() - Create a uthread object and make it ready for execution
57 *
58 * Threads are automatically deleted when they return from their entry point.
59 *
60 * @uthr: a pointer to a user-allocated uthread structure to store information
61 * about the new thread, or NULL to let the framework allocate and manage its
62 * own structure.
63 * @fn: the thread's entry point
64 * @arg: argument passed to the thread's entry point
65 * @stack_sz: stack size for the new thread (in bytes). The stack is allocated
66 * on the heap.
67 * @grp_id: an optional thread group ID that the new thread should belong to
68 * (zero for no group)
69 */
70int uthread_create(struct uthread *uthr, void (*fn)(void *), void *arg,
71 size_t stack_sz, unsigned int grp_id);
72/**
73 * uthread_schedule() - yield the CPU to the next runnable thread
74 *
75 * This function is called either by the main thread or any secondary thread
76 * (that is, any thread created via uthread_create()) to switch execution to
77 * the next runnable thread.
78 *
79 * Return: true if a thread was scheduled, false if no runnable thread was found
80 */
81bool uthread_schedule(void);
82/**
83 * uthread_grp_new_id() - return a new ID for a thread group
84 *
85 * Return: the new thread group ID
86 */
87unsigned int uthread_grp_new_id(void);
88/**
89 * uthread_grp_done() - test if all threads in a group are done
90 *
91 * @grp_id: the ID of the thread group that should be considered
92 * Return: false if the group contains at least one runnable thread (i.e., one
93 * thread which entry point has not returned yet), true otherwise
94 */
95bool uthread_grp_done(unsigned int grp_id);
96
97#else
98
99static inline int uthread_create(struct uthread *uthr, void (*fn)(void *),
100 void *arg, size_t stack_sz,
101 unsigned int grp_id)
102{
103 fn(arg);
104 return 0;
105}
106
107static inline bool uthread_schedule(void)
108{
109 return false;
110}
111
112static inline unsigned int uthread_grp_new_id(void)
113{
114 return 0;
115}
116
117static inline bool uthread_grp_done(unsigned int grp_id)
118{
119 return true;
120}
121
122#endif /* CONFIG_UTHREAD */
123#endif /* _UTHREAD_H_ */