Angel Pons | 32859fc | 2020-04-02 23:48:27 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 2 | #ifndef THREAD_H_ |
| 3 | #define THREAD_H_ |
| 4 | |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 5 | #include <arch/cpu.h> |
Raul E Rangel | cc01da5 | 2021-07-12 13:43:48 -0600 | [diff] [blame] | 6 | #include <bootstate.h> |
| 7 | #include <commonlib/bsd/cb_err.h> |
Raul E Rangel | a3b2907 | 2021-11-05 16:58:12 -0600 | [diff] [blame] | 8 | #include <types.h> |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 9 | |
Raul E Rangel | b29f9d4 | 2021-07-12 13:49:59 -0600 | [diff] [blame] | 10 | struct thread_mutex { |
| 11 | bool locked; |
| 12 | }; |
| 13 | |
Raul E Rangel | cc01da5 | 2021-07-12 13:43:48 -0600 | [diff] [blame] | 14 | enum thread_state { |
| 15 | THREAD_UNINITIALIZED, |
| 16 | THREAD_STARTED, |
| 17 | THREAD_DONE, |
| 18 | }; |
| 19 | |
| 20 | struct thread_handle { |
| 21 | enum thread_state state; |
| 22 | /* Only valid when state == THREAD_DONE */ |
| 23 | enum cb_err error; |
| 24 | }; |
| 25 | |
Raul E Rangel | 5dd7602 | 2021-07-15 13:26:52 -0600 | [diff] [blame] | 26 | /* Run func(arg) on a new thread. Return 0 on successful start of thread, < 0 |
| 27 | * when thread could not be started. The thread handle if populated, will |
| 28 | * reflect the state and return code of the thread. |
| 29 | */ |
| 30 | int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg); |
| 31 | |
| 32 | /* thread_run_until is the same as thread_run() except that it blocks state |
| 33 | * transitions from occurring in the (state, seq) pair of the boot state |
| 34 | * machine. */ |
| 35 | int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg, |
| 36 | boot_state_t state, boot_state_sequence_t seq); |
| 37 | |
Raul E Rangel | cc01da5 | 2021-07-12 13:43:48 -0600 | [diff] [blame] | 38 | /* Waits until the thread has terminated and returns the error code */ |
| 39 | enum cb_err thread_join(struct thread_handle *handle); |
| 40 | |
Raul E Rangel | ca0606e | 2021-07-21 14:34:00 -0600 | [diff] [blame] | 41 | #if (ENV_RAMSTAGE || ENV_ROMSTAGE) && CONFIG(COOP_MULTITASKING) |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 42 | |
| 43 | struct thread { |
| 44 | int id; |
| 45 | uintptr_t stack_current; |
| 46 | uintptr_t stack_orig; |
| 47 | struct thread *next; |
Raul E Rangel | cc01da5 | 2021-07-12 13:43:48 -0600 | [diff] [blame] | 48 | enum cb_err (*entry)(void *); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 49 | void *entry_arg; |
| 50 | int can_yield; |
Raul E Rangel | cc01da5 | 2021-07-12 13:43:48 -0600 | [diff] [blame] | 51 | struct thread_handle *handle; |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 52 | }; |
| 53 | |
Raul E Rangel | d5dca21 | 2021-07-15 11:48:48 -0600 | [diff] [blame] | 54 | /* Return 0 on successful yield, < 0 when thread did not yield. */ |
| 55 | int thread_yield(void); |
| 56 | |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 57 | /* Return 0 on successful yield for the given amount of time, < 0 when thread |
| 58 | * did not yield. */ |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 59 | int thread_yield_microseconds(unsigned int microsecs); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 60 | |
| 61 | /* Allow and prevent thread cooperation on current running thread. By default |
Martin Roth | 0cb07e3 | 2013-07-09 21:46:01 -0600 | [diff] [blame] | 62 | * all threads are marked to be cooperative. That means a thread can yield |
Raul E Rangel | be60a0d | 2021-07-15 13:52:03 -0600 | [diff] [blame] | 63 | * to another thread at a pre-determined switch point. i.e., udelay, |
| 64 | * thread_yield, or thread_yield_microseconds. |
| 65 | * |
| 66 | * These methods should be used to guard critical sections so a dead lock does |
| 67 | * not occur. The critical sections can be nested. Just make sure the methods |
| 68 | * are used in pairs. |
| 69 | */ |
Raul E Rangel | 9ba36ab | 2021-07-15 17:34:05 -0600 | [diff] [blame] | 70 | void thread_coop_enable(void); |
| 71 | void thread_coop_disable(void); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 72 | |
Raul E Rangel | b29f9d4 | 2021-07-12 13:49:59 -0600 | [diff] [blame] | 73 | void thread_mutex_lock(struct thread_mutex *mutex); |
| 74 | void thread_mutex_unlock(struct thread_mutex *mutex); |
| 75 | |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 76 | /* Architecture specific thread functions. */ |
Lee Leahy | 22c28e0 | 2017-03-07 15:47:44 -0800 | [diff] [blame] | 77 | asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 78 | /* Set up the stack frame for a new thread so that a switch_to_thread() call |
| 79 | * will enter the thread_entry() function with arg as a parameter. The |
| 80 | * saved_stack field in the struct thread needs to be updated accordingly. */ |
| 81 | void arch_prepare_thread(struct thread *t, |
Lee Leahy | 746d4af | 2017-03-07 15:31:49 -0800 | [diff] [blame] | 82 | asmlinkage void (*thread_entry)(void *), void *arg); |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 83 | #else |
Raul E Rangel | d5dca21 | 2021-07-15 11:48:48 -0600 | [diff] [blame] | 84 | static inline int thread_yield(void) |
| 85 | { |
| 86 | return -1; |
| 87 | } |
Lee Leahy | 0ca2a06 | 2017-03-06 18:01:04 -0800 | [diff] [blame] | 88 | static inline int thread_yield_microseconds(unsigned int microsecs) |
| 89 | { |
| 90 | return -1; |
| 91 | } |
Raul E Rangel | 9ba36ab | 2021-07-15 17:34:05 -0600 | [diff] [blame] | 92 | static inline void thread_coop_enable(void) {} |
| 93 | static inline void thread_coop_disable(void) {} |
Raul E Rangel | b29f9d4 | 2021-07-12 13:49:59 -0600 | [diff] [blame] | 94 | |
| 95 | static inline void thread_mutex_lock(struct thread_mutex *mutex) {} |
| 96 | |
| 97 | static inline void thread_mutex_unlock(struct thread_mutex *mutex) {} |
Aaron Durbin | 4409a5e | 2013-05-06 12:20:52 -0500 | [diff] [blame] | 98 | #endif |
| 99 | |
| 100 | #endif /* THREAD_H_ */ |