2020-04-02 23:48:27 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2013-05-06 19:20:52 +02:00
|
|
|
#ifndef THREAD_H_
|
|
|
|
#define THREAD_H_
|
|
|
|
|
|
|
|
#include <arch/cpu.h>
|
2021-07-12 21:43:48 +02:00
|
|
|
#include <bootstate.h>
|
2021-11-05 23:58:12 +01:00
|
|
|
#include <types.h>
|
2013-05-06 19:20:52 +02:00
|
|
|
|
2021-07-12 21:49:59 +02:00
|
|
|
struct thread_mutex {
|
|
|
|
bool locked;
|
|
|
|
};
|
|
|
|
|
2021-07-12 21:43:48 +02:00
|
|
|
enum thread_state {
|
|
|
|
THREAD_UNINITIALIZED,
|
|
|
|
THREAD_STARTED,
|
|
|
|
THREAD_DONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct thread_handle {
|
|
|
|
enum thread_state state;
|
|
|
|
/* Only valid when state == THREAD_DONE */
|
|
|
|
enum cb_err error;
|
|
|
|
};
|
|
|
|
|
2021-07-15 21:26:52 +02:00
|
|
|
/* Run func(arg) on a new thread. Return 0 on successful start of thread, < 0
|
|
|
|
* when thread could not be started. The thread handle if populated, will
|
|
|
|
* reflect the state and return code of the thread.
|
|
|
|
*/
|
|
|
|
int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg);
|
|
|
|
|
|
|
|
/* thread_run_until is the same as thread_run() except that it blocks state
|
|
|
|
* transitions from occurring in the (state, seq) pair of the boot state
|
|
|
|
* machine. */
|
|
|
|
int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg,
|
|
|
|
boot_state_t state, boot_state_sequence_t seq);
|
|
|
|
|
2021-07-12 21:43:48 +02:00
|
|
|
/* Waits until the thread has terminated and returns the error code */
|
|
|
|
enum cb_err thread_join(struct thread_handle *handle);
|
|
|
|
|
2021-11-22 21:43:49 +01:00
|
|
|
#if ENV_STAGE_SUPPORTS_COOP
|
2013-05-06 19:20:52 +02:00
|
|
|
|
|
|
|
struct thread {
|
|
|
|
int id;
|
|
|
|
uintptr_t stack_current;
|
|
|
|
uintptr_t stack_orig;
|
|
|
|
struct thread *next;
|
2021-07-12 21:43:48 +02:00
|
|
|
enum cb_err (*entry)(void *);
|
2013-05-06 19:20:52 +02:00
|
|
|
void *entry_arg;
|
|
|
|
int can_yield;
|
2021-07-12 21:43:48 +02:00
|
|
|
struct thread_handle *handle;
|
2013-05-06 19:20:52 +02:00
|
|
|
};
|
|
|
|
|
2021-07-15 19:48:48 +02:00
|
|
|
/* Return 0 on successful yield, < 0 when thread did not yield. */
|
|
|
|
int thread_yield(void);
|
|
|
|
|
2013-05-06 19:20:52 +02:00
|
|
|
/* Return 0 on successful yield for the given amount of time, < 0 when thread
|
|
|
|
* did not yield. */
|
2017-03-07 03:01:04 +01:00
|
|
|
int thread_yield_microseconds(unsigned int microsecs);
|
2013-05-06 19:20:52 +02:00
|
|
|
|
|
|
|
/* Allow and prevent thread cooperation on current running thread. By default
|
2013-07-10 05:46:01 +02:00
|
|
|
* all threads are marked to be cooperative. That means a thread can yield
|
2021-07-15 21:52:03 +02:00
|
|
|
* to another thread at a pre-determined switch point. i.e., udelay,
|
|
|
|
* thread_yield, or thread_yield_microseconds.
|
|
|
|
*
|
|
|
|
* These methods should be used to guard critical sections so a dead lock does
|
|
|
|
* not occur. The critical sections can be nested. Just make sure the methods
|
|
|
|
* are used in pairs.
|
|
|
|
*/
|
2021-07-16 01:34:05 +02:00
|
|
|
void thread_coop_enable(void);
|
|
|
|
void thread_coop_disable(void);
|
2013-05-06 19:20:52 +02:00
|
|
|
|
2021-07-12 21:49:59 +02:00
|
|
|
void thread_mutex_lock(struct thread_mutex *mutex);
|
|
|
|
void thread_mutex_unlock(struct thread_mutex *mutex);
|
|
|
|
|
2013-05-06 19:20:52 +02:00
|
|
|
/* Architecture specific thread functions. */
|
2017-03-08 00:47:44 +01:00
|
|
|
asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack);
|
2013-05-06 19:20:52 +02:00
|
|
|
/* Set up the stack frame for a new thread so that a switch_to_thread() call
|
|
|
|
* will enter the thread_entry() function with arg as a parameter. The
|
|
|
|
* saved_stack field in the struct thread needs to be updated accordingly. */
|
|
|
|
void arch_prepare_thread(struct thread *t,
|
2017-03-08 00:31:49 +01:00
|
|
|
asmlinkage void (*thread_entry)(void *), void *arg);
|
2013-05-06 19:20:52 +02:00
|
|
|
#else
|
2021-07-15 19:48:48 +02:00
|
|
|
static inline int thread_yield(void)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2017-03-07 03:01:04 +01:00
|
|
|
static inline int thread_yield_microseconds(unsigned int microsecs)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2021-07-16 01:34:05 +02:00
|
|
|
static inline void thread_coop_enable(void) {}
|
|
|
|
static inline void thread_coop_disable(void) {}
|
2021-07-12 21:49:59 +02:00
|
|
|
|
|
|
|
static inline void thread_mutex_lock(struct thread_mutex *mutex) {}
|
|
|
|
|
|
|
|
static inline void thread_mutex_unlock(struct thread_mutex *mutex) {}
|
2013-05-06 19:20:52 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* THREAD_H_ */
|