| 1 |
|
| 2 |
#ifndef THREAD_H_ |
| 3 |
#define THREAD_H_ |
| 4 |
|
| 5 |
#include <arch/cpu.h> |
| 6 |
#include <bootstate.h> |
| 7 |
#include <types.h> |
| 8 |
|
| 9 |
struct thread_mutex { |
| 10 |
bool locked; |
| 11 |
}; |
| 12 |
|
| 13 |
enum thread_state { |
| 14 |
THREAD_UNINITIALIZED, |
| 15 |
THREAD_STARTED, |
| 16 |
THREAD_DONE, |
| 17 |
}; |
| 18 |
|
| 19 |
struct thread_handle { |
| 20 |
enum thread_state state; |
| 21 |
|
| 22 |
enum cb_err error; |
| 23 |
}; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
int 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 |
|
| 37 |
|
| 38 |
enum cb_err thread_join(struct thread_handle *handle); |
| 39 |
|
| 40 |
#if ENV_SUPPORTS_COOP |
| 41 |
|
| 42 |
struct thread { |
| 43 |
int id; |
| 44 |
uintptr_t stack_current; |
| 45 |
uintptr_t stack_orig; |
| 46 |
struct thread *next; |
| 47 |
enum cb_err (*entry)(void *); |
| 48 |
void *entry_arg; |
| 49 |
int can_yield; |
| 50 |
struct thread_handle *handle; |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
int thread_yield(void); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
int thread_yield_microseconds(unsigned int microsecs); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
void thread_coop_enable(void); |
| 70 |
void thread_coop_disable(void); |
| 71 |
|
| 72 |
void thread_mutex_lock(struct thread_mutex *mutex); |
| 73 |
void thread_mutex_unlock(struct thread_mutex *mutex); |
| 74 |
|
| 75 |
|
| 76 |
asmlinkage void switch_to_thread(uintptr_t new_stack, uintptr_t *saved_stack); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
void arch_prepare_thread(struct thread *t, |
| 81 |
asmlinkage void (*thread_entry)(void *), void *arg); |
| 82 |
#else |
| 83 |
static inline int thread_yield(void) |
| 84 |
{ |
| 85 |
return -1; |
| 86 |
} |
| 87 |
static inline int thread_yield_microseconds(unsigned int microsecs) |
| 88 |
{ |
| 89 |
return -1; |
| 90 |
} |
| 91 |
static inline void thread_coop_enable(void) {} |
| 92 |
static inline void thread_coop_disable(void) {} |
| 93 |
|
| 94 |
static inline void thread_mutex_lock(struct thread_mutex *mutex) {} |
| 95 |
|
| 96 |
static inline void thread_mutex_unlock(struct thread_mutex *mutex) {} |
| 97 |
#endif |
| 98 |
|
| 99 |
#endif |