Kernel primitives for manipulating threads. More...
Macros | |
#define | STACK_ALIGN |
#define | PRIORITY_INVALID 0x1FFU |
#define | PRIORITY_MAX 0xFFU |
#define | STACK_INVALID 0xFFFFFFFFU |
#define | KERNEL_STRUCTS_INITIALIZED_SIGNATURE 0x434D5258 |
Typedefs | |
typedef uint8_t | Thread_t |
Functions | |
int | os_thread_alloc (Process_t process, uint8_t priority) |
Allocate thread entry in thread table. | |
int | __os_thread_create (Process_t process, entrypoint_t *entrypoint, void *data, uint8_t priority, uint8_t core) |
Full workflow needed to create a thread. | |
bool | os_get_next_thread (uint8_t current_thread, uint8_t *next_thread) |
Obtain next thread to run. | |
int | os_sched_yield (void) |
Kernel implementation of sched_yield() syscall. | |
long | os_sched_timing_callback (long delay_us) |
Kernel callback for timing provider. | |
uint8_t | os_get_current_thread (void) |
Kernel implementation of get_tid() syscall. | |
void | os_set_current_thread (Thread_t new_thread) |
Kernel internal function to override current thread. | |
uint8_t | os_get_current_stack (void) |
Get ID of stack used by current thread. | |
uint8_t | os_get_current_process (void) |
Kernel implementation of get_pid. | |
uint32_t | os_get_micro_time (void) |
Get amount of microseconds elapsed since scheduler start. | |
int | os_idle_thread (void *data) |
CMRX idle thread. | |
int | os_stack_create () |
Find free stack slot and allocate it. | |
uint32_t * | os_stack_get (int stack_id) |
Get address of stack. | |
void | os_stack_dispose (uint32_t stack_id) |
Release stack slot. | |
int | os_thread_exit (int status) |
Kernel implementation of thread_exit() syscall. | |
int | os_thread_kill (uint8_t thread_id, int status) |
Kernel way to kill an arbitrary thread. | |
int | os_thread_stop (uint8_t thread_id) |
Kernel implementation of thread_stop() syscall. | |
struct OS_thread_t * | 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 setpriority() syscall. | |
static 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 thread_join() syscall. | |
int | os_thread_construct (Thread_t tid, entrypoint_t *entrypoint, void *data, uint8_t core) |
Make thread runnable. | |
int | os_thread_create (entrypoint_t *entrypoint, void *data, uint8_t priority) |
Syscall handling thread_create() Creates new thread inside current process using specified entrypoint. | |
void | _os_start (uint8_t start_core) |
Start up scheduler. | |
uint32_t | os_shutdown () |
Kernel implementation of the shutdown() syscall. | |
struct OS_thread_t * | os_thread_get (Thread_t thread_id) |
Get thread descriptor. | |
int | os_thread_migrate (uint8_t thread_id, int target_core) |
Migrate thread between CPU cores. | |
void | os_thread_dispose (void) |
Alias to thread_exit. | |
Variables | |
struct OS_thread_t | os_threads [OS_THREADS] |
List of active threads. | |
struct OS_process_t | os_processes [OS_PROCESSES] |
List of active processes. | |
struct OS_core_state_t | core [OS_NUM_CORES] |
CPU scheduling thread IDs. | |
struct OS_stack_t | os_stacks |
Thread stacks. | |
uint32_t | sched_tick_increment |
Amount of real time advance per one scheduler tick. | |
static uint32_t | sched_microtime = 0 |
Current scheduler real time. | |
struct OS_core_state_t | core [OS_NUM_CORES] |
CPU scheduling thread IDs. | |
Kernel primitives for manipulating threads.
#define KERNEL_STRUCTS_INITIALIZED_SIGNATURE 0x434D5258 |
#define PRIORITY_INVALID 0x1FFU |
#define PRIORITY_MAX 0xFFU |
#define STACK_ALIGN |
#define STACK_INVALID 0xFFFFFFFFU |
typedef uint8_t Thread_t |
int __os_thread_create | ( | Process_t | process, |
entrypoint_t * | entrypoint, | ||
void * | data, | ||
uint8_t | priority, | ||
uint8_t | core | ||
) |
Full workflow needed to create a thread.
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 |
void _os_start | ( | uint8_t | start_core | ) |
Start up scheduler.
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.
[in] | start_core | number of core for which the kernel is started |
|
static |
uint8_t os_get_current_process | ( | void | ) |
Kernel implementation of get_pid.
uint8_t os_get_current_stack | ( | void | ) |
Get ID of stack used by current thread.
uint8_t os_get_current_thread | ( | void | ) |
Kernel implementation of get_tid() syscall.
uint32_t os_get_micro_time | ( | void | ) |
Get amount of microseconds elapsed since scheduler start.
bool os_get_next_thread | ( | uint8_t | current_thread, |
uint8_t * | next_thread | ||
) |
Obtain next thread to run.
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 |
int os_idle_thread | ( | void * | data | ) |
CMRX idle thread.
This thread runs whenever no other CMRX thread is ready to be run. It does nothing useful.
long os_sched_timing_callback | ( | long | delay_us | ) |
Kernel callback for timing provider.
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.
[in] | delay_us | the actual amount of microseconds which happened since the last wakeup |
int os_sched_yield | ( | void | ) |
Kernel implementation of sched_yield() syscall.
Causes scheduler to consider another task to be ran.
void os_set_current_thread | ( | Thread_t | new_thread | ) |
Kernel internal function to override current thread.
DANGEROUS!!!
int os_setpriority | ( | uint8_t | priority | ) |
Kernel implementation of setpriority() syscall.
uint32_t os_shutdown | ( | void | ) |
Kernel implementation of the shutdown() syscall.
int os_stack_create | ( | ) |
Find free stack slot and allocate it.
void os_stack_dispose | ( | uint32_t | stack_id | ) |
Release stack slot.
stack_id | Stack slot which should be released. |
uint32_t * os_stack_get | ( | int | stack_id | ) |
Get address of stack.
stack_id | ID of stack |
int os_thread_alloc | ( | Process_t | process, |
uint8_t | priority | ||
) |
Allocate thread entry in thread table.
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 |
struct OS_thread_t * os_thread_by_id | ( | Thread_t | id | ) |
int os_thread_construct | ( | Thread_t | tid, |
entrypoint_t * | entrypoint, | ||
void * | data, | ||
uint8_t | core | ||
) |
Make thread runnable.
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 |
int os_thread_continue | ( | uint8_t | thread | ) |
Kernel implementation of thread_continue() syscall.
int os_thread_create | ( | entrypoint_t * | entrypoint, |
void * | data, | ||
uint8_t | priority | ||
) |
Syscall handling thread_create() Creates new thread inside current process using specified entrypoint.
Kernel implementation of thread_create() syscall.
void os_thread_dispose | ( | void | ) |
Alias to thread_exit.
This is in fact the same function as thread_exit. The only difference is that if for whatever reason syscall to os_thread_exit() will fail, this asserts.
int os_thread_exit | ( | int | status | ) |
Kernel implementation of thread_exit() syscall.
struct OS_thread_t * os_thread_get | ( | Thread_t | thread_id | ) |
Get thread descriptor.
thread_id | ID of thread |
int os_thread_join | ( | uint8_t | thread_id | ) |
Kernel implementation of thread_join() syscall.
int os_thread_kill | ( | uint8_t | thread_id, |
int | status | ||
) |
Kernel way to kill an arbitrary thread.
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 |
int os_thread_migrate | ( | uint8_t | thread_id, |
int | target_core | ||
) |
Migrate thread between CPU cores.
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() while this call is in progress then the call will fail.
thread_id | ID of thread to be migrated |
target_core | ID of core where the thread should be migrated. |
int os_thread_set_ready | ( | struct OS_thread_t * | thread | ) |
int os_thread_stop | ( | uint8_t | thread | ) |
Kernel implementation of thread_stop() syscall.
struct OS_core_state_t core[OS_NUM_CORES] |
CPU scheduling thread IDs.
|
extern |
CPU scheduling thread IDs.
struct OS_process_t os_processes[OS_PROCESSES] |
List of active processes.
Scheduler notion on existing processes.
struct OS_stack_t os_stacks |
Thread stacks.
Scheduler notion on existing stacks.
struct OS_thread_t os_threads[OS_THREADS] |
List of active threads.
Scheduler notion on existing threads.
|
static |
Current scheduler real time.
|
extern |
Amount of real time advance per one scheduler tick.