blob: 059ba52a2c39464fc882a47c7772f4855fe3deb5 [file] [log] [blame]
Angel Pons32859fc2020-04-02 23:48:27 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin4409a5e2013-05-06 12:20:52 -05002#ifndef THREAD_H_
3#define THREAD_H_
4
Aaron Durbin4409a5e2013-05-06 12:20:52 -05005#include <arch/cpu.h>
Raul E Rangelcc01da52021-07-12 13:43:48 -06006#include <bootstate.h>
Raul E Rangela3b29072021-11-05 16:58:12 -06007#include <types.h>
Aaron Durbin4409a5e2013-05-06 12:20:52 -05008
Raul E Rangelb29f9d42021-07-12 13:49:59 -06009struct thread_mutex {
10 bool locked;
11};
12
Raul E Rangelcc01da52021-07-12 13:43:48 -060013enum thread_state {
14 THREAD_UNINITIALIZED,
15 THREAD_STARTED,
16 THREAD_DONE,
17};
18
19struct thread_handle {
20 enum thread_state state;
21 /* Only valid when state == THREAD_DONE */
22 enum cb_err error;
23};
24
Raul E Rangel5dd76022021-07-15 13:26:52 -060025/* Run func(arg) on a new thread. Return 0 on successful start of thread, < 0
26 * when thread could not be started. The thread handle if populated, will
27 * reflect the state and return code of the thread.
28 */
29int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg);
30
31/* thread_run_until is the same as thread_run() except that it blocks state
32 * transitions from occurring in the (state, seq) pair of the boot state
33 * machine. */
34int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg,
35 boot_state_t state, boot_state_sequence_t seq);
36
Raul E Rangelcc01da52021-07-12 13:43:48 -060037/* Waits until the thread has terminated and returns the error code */
38enum cb_err thread_join(struct thread_handle *handle);
39
Arthur Heymans6acc05e2022-05-12 18:01:13 +020040#if ENV_SUPPORTS_COOP
Aaron Durbin4409a5e2013-05-06 12:20:52 -050041
42struct thread {
43 int id;
44 uintptr_t stack_current;
45 uintptr_t stack_orig;
46 struct thread *next;
Raul E Rangelcc01da52021-07-12 13:43:48 -060047 enum cb_err (*entry)(void *);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050048 void *entry_arg;
49 int can_yield;
Raul E Rangelcc01da52021-07-12 13:43:48 -060050 struct thread_handle *handle;
Aaron Durbin4409a5e2013-05-06 12:20:52 -050051};
52
Raul E Rangeld5dca212021-07-15 11:48:48 -060053/* Return 0 on successful yield, < 0 when thread did not yield. */
54int thread_yield(void);
55
Aaron Durbin4409a5e2013-05-06 12:20:52 -050056/* Return 0 on successful yield for the given amount of time, < 0 when thread
57 * did not yield. */
Lee Leahy0ca2a062017-03-06 18:01:04 -080058int thread_yield_microseconds(unsigned int microsecs);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050059
60/* Allow and prevent thread cooperation on current running thread. By default
Martin Roth0cb07e32013-07-09 21:46:01 -060061 * all threads are marked to be cooperative. That means a thread can yield
Raul E Rangelbe60a0d2021-07-15 13:52:03 -060062 * to another thread at a pre-determined switch point. i.e., udelay,
63 * thread_yield, or thread_yield_microseconds.
64 *
65 * These methods should be used to guard critical sections so a dead lock does
66 * not occur. The critical sections can be nested. Just make sure the methods
67 * are used in pairs.
68 */
Raul E Rangel9ba36ab2021-07-15 17:34:05 -060069void thread_coop_enable(void);
70void thread_coop_disable(void);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050071
Raul E Rangelb29f9d42021-07-12 13:49:59 -060072void thread_mutex_lock(struct thread_mutex *mutex);
73void thread_mutex_unlock(struct thread_mutex *mutex);
74
Aaron Durbin4409a5e2013-05-06 12:20:52 -050075/* Architecture specific thread functions. */
Lee Leahy22c28e02017-03-07 15:47:44 -080076asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050077/* Set up the stack frame for a new thread so that a switch_to_thread() call
78 * will enter the thread_entry() function with arg as a parameter. The
79 * saved_stack field in the struct thread needs to be updated accordingly. */
80void arch_prepare_thread(struct thread *t,
Lee Leahy746d4af2017-03-07 15:31:49 -080081 asmlinkage void (*thread_entry)(void *), void *arg);
Aaron Durbin4409a5e2013-05-06 12:20:52 -050082#else
Raul E Rangeld5dca212021-07-15 11:48:48 -060083static inline int thread_yield(void)
84{
85 return -1;
86}
Lee Leahy0ca2a062017-03-06 18:01:04 -080087static inline int thread_yield_microseconds(unsigned int microsecs)
88{
89 return -1;
90}
Raul E Rangel9ba36ab2021-07-15 17:34:05 -060091static inline void thread_coop_enable(void) {}
92static inline void thread_coop_disable(void) {}
Raul E Rangelb29f9d42021-07-12 13:49:59 -060093
94static inline void thread_mutex_lock(struct thread_mutex *mutex) {}
95
96static inline void thread_mutex_unlock(struct thread_mutex *mutex) {}
Aaron Durbin4409a5e2013-05-06 12:20:52 -050097#endif
98
99#endif /* THREAD_H_ */