Thread scheduling
Type Definitons
typedef uint8_t Thread_t
Variable
Unknown member type os_threads Unknown member type os_processes Unknown member type core Unknown member type os_stacks Unknown member type sched_tick_increment Unknown member type sched_microtime Unknown member type core
Functions
int os_thread_alloc(Process_t process, uint8_t priority)
Will allocate entry in thread table. Thread won’t be runnable after allocation, but thread ID will be reserved for it.
process ID of process owning the thread. Process must be existing already.
priority tread priority Positive values denote thread ID reserved for new thread usable in further calls. Negative value means that there was no free slot in thread table to allocate new thread.
- process ID of process owning the thread. Process must be existing already.
- priority tread priority
Returns Positive values denote thread ID reserved for new thread usable in further calls. Negative value means that there was no free slot in thread table to allocate new thread.
int __os_thread_create(Process_t process, entrypoint_t *entrypoint, void *data, uint8_t priority, uint8_t core)
This function is callable both from syscall and internally from kernel (during e.g. system startup) and performs complete thread creation workflow.
process ID of process owning the thread. Process must already be existing.
entrypoint address of function which shall be executed as entrypoint into the thread
data address of data block which should be passed to entrypoint function as an argument
priority thread priority. Numerically lower values mean higher priorities
core core on which thread will be created Non-negative values denote ID of thread just created, negative values mean errors.
- process ID of process owning the thread. Process must already be existing.
- entrypoint address of function which shall be executed as entrypoint into the thread
- data address of data block which should be passed to entrypoint function as an argument
- priority thread priority. Numerically lower values mean higher priorities
- core core on which thread will be created
Returns Non-negative values denote ID of thread just created, negative values mean errors.
bool os_get_next_thread(uint8_t current_thread, uint8_t *next_thread)
This function performs thread list lookup. It searches for thread, which is in ready state and has highest (numerically lowest) priority.
- current_thread thread which is currently running
- next_thread pointer to variable where next thread ID will be stored
Returns true if any runnable thread (different than current) was found, false otherwise.
int os_sched_yield(void)
Causes scheduler to consider another task to be ran.
long os_sched_timing_callback(long delay_us)
Kernel entrypoint for timed events. Kernel tells the timing provider, what is the delay before the next call, whenever this function is called. Timing provider shall then wait for given amount of time and then call this callback again. If the delay is 0, then timing provider can shutdown itself as there is no expected timed wakeup.
delay_us the actual amount of microseconds which happened since the last wakeup amount of microseconds, which shall pass before next wakeup will happen. If this value is zero, then no next wakeup shall happen.
- delay_us the actual amount of microseconds which happened since the last wakeup
Returns amount of microseconds, which shall pass before next wakeup will happen. If this value is zero, then no next wakeup shall happen.
uint8_t os_get_current_thread(void)
Current thread ID. This is actually an offset in thread table.
void os_set_current_thread(Thread_t new_thread)
DANGEROUS!!!
uint8_t os_get_current_stack(void)
Current active stack ID. This is actually an offset in stack table.
uint8_t os_get_current_process(void)
Current process ID. This is actually an offset in process table.
uint32_t os_get_micro_time(void)
internal kernel soft timer. This gets updated upon each systick handler call. It may jitter a bit. Wraps after about 4 million seconds.
int os_idle_thread(void *data)
This thread runs whenever no other CMRX thread is ready to be run. It does nothing useful.
int os_stack_create()
If there is at least one free stack slot, then return it’s ID. If no free stack is available, return STACK_INVALID constant.
uint32_t * os_stack_get(int stack_id)
- stack_id ID of stack
Returns base address of stack
void os_stack_dispose(uint32_t stack_id)
- stack_id Stack slot which should be released.
int os_thread_exit(int status)
Kernel implementation of
int os_thread_kill(uint8_t thread_id, int status)
This call terminates any thread currently existing. There is no syscall for this right now.
thread_id ID of thread to be terminated. May even be thread currently running.
status thread exit status 0 if operation succeeded, error number otherwise
- thread_id ID of thread to be terminated. May even be thread currently running.
- status thread exit status
Returns 0 if operation succeeded, error number otherwise
int os_thread_stop(uint8_t thread_id)
Kernel implementation of thread_stop() syscall.
struct * os_thread_by_id(Thread_t id)
int os_thread_set_ready(struct OS_thread_t *thread)
int os_thread_continue(uint8_t thread_id)
Kernel implementation of thread_continue() syscall.
int os_setpriority(uint8_t priority)
Kernel implementation of
void cb_thread_join_notify(const void *object, Thread_t thread, int sleeper_id, Event_t event)
int os_thread_join(uint8_t thread_id)
Kernel implementation of
int os_thread_construct(Thread_t tid, entrypoint_t *entrypoint, void *data, uint8_t core)
This function will take previously allocated thread and will construct it’s internal state, so that it is runnable. This includes stack allocation and filling in values, so that thread can be scheduled and run.
tid Thread ID of thread to be constructed
entrypoint pointer to thread entrypoint function
data pointer to thread data. pass NULL pointer if no thread data is used
core ID of core the thread should run at E_OK if thread was constructed, E_OUT_OF_STACKS if there is no free stack available and E_TASK_RUNNING if thread is not in state suitable for construction (either slot is free, or already constructed).
- tid Thread ID of thread to be constructed
- entrypoint pointer to thread entrypoint function
- data pointer to thread data. pass NULL pointer if no thread data is used
- core ID of core the thread should run at
Returns E_OK if thread was constructed, E_OUT_OF_STACKS if there is no free stack available and E_TASK_RUNNING if thread is not in state suitable for construction (either slot is free, or already constructed).
int os_thread_create(entrypoint_t *entrypoint, void *data, uint8_t priority)
Kernel implementation of thread_create()
void _os_start(uint8_t start_core)
This function populates thread table based on thread autostart macro use. It also creates idle thread with priority 255 and starts scheduler. It never returns until you have very bad day.
start_core number of core for which the kernel is started
- start_core number of core for which the kernel is started
uint32_t os_shutdown()
Kernel implementation of the
struct * os_thread_get(Thread_t thread_id)
- thread_id ID of thread
Returns address of thread description strcture or NULL pointer if thread_id out of range.
int os_thread_migrate(uint8_t thread_id, int target_core)
This function takes existing thread which is bound to some core and moved it over to scheduler queue of another core. In order for this call to be successful, the thread must already be stopped. If thread is woken up by e.g. signal arrived from interrupt service handler via isr_kill()
thread_id ID of thread to be migrated
target_core ID of core where the thread should be migrated. E_OK if thread was migrated; E_INVALID if thread is not stopped or call is not made from the core at which the thread is currently running.
- thread_id ID of thread to be migrated
- target_core ID of core where the thread should be migrated.
Returns E_OK if thread was migrated; E_INVALID if thread is not stopped or call is not made from the core at which the thread is currently running.
void os_thread_dispose(void)
This is in fact the same function as thread_exitos_thread_exit()