Managing threads and program execution. More...
![]() |
Typedefs | |
typedef void(* | sys_thread_func_t) (void *arg) |
Thread function signatureFunction signature for thread entry points. More... | |
Functions | |
void | sys_abort (void) |
Aborts the current process immediately. More... | |
void | sys_panicf (const char *fmt,...) |
Prints a formatted panic message and aborts the process. More... | |
uint8_t | sys_thread_numcores (void) |
Returns the number of CPU cores available on the host system. More... | |
bool | sys_thread_create (sys_thread_func_t func, void *arg) |
Create a thread on any available core. More... | |
bool | sys_thread_create_on_core (sys_thread_func_t func, void *arg, uint8_t core) |
Create a thread on a specific core. More... | |
uint8_t | sys_thread_core (void) |
Get the CPU core number the current thread is running on. More... | |
void | sys_sleep (int32_t msec) |
Pauses the execution of the current thread for a specified time. More... | |
Managing threads and program execution.
This module declares types and functions for thread management, including thread creation, synchronization, and communication. It is generally aware of the number of "cores" available on the system, and allows creating threads that can run on a specific core, or on any available core.
However, it really depends on the platform's capabilities and requirements. as to whether you should call sys_thread_create() or sys_thread_create_on_core(). On the Pico platform, for example, you should use sys_thread_create_on_core() to ensure the thread runs on a specific core. If you call this method when an existing thread is already running on that core, the core will be reset and the new thread will be scheduled to run on that core, so it's important to ensure that there isn't an existing thread already running on the specified core before calling this function.
The implementation of threads is like Go's goroutines, where the execution threads are "fire and forget" and do not require joining. If you wish to communicate between threads, use the event queue. If you wish to co-ordinate when threads are running or to synchronize their execution, you can use the provided synchronization primitives like waitgroups.
There are also methods for suspending the current thread for a specified duration, and for halting execution of the process immediately, in case of catastrophic failure.
typedef void(* sys_thread_func_t) (void *arg) |
void sys_abort | ( | void | ) |
Aborts the current process immediately.
This function terminates the current process abnormally and immediately. It does not perform any cleanup operations and does not call exit handlers or destructors.
void sys_panicf | ( | const char * | fmt, |
... | |||
) |
Prints a formatted panic message and aborts the process.
fmt | A printf-style format string specifying the panic message. |
... | Additional arguments corresponding to format specifiers in fmt. |
void sys_sleep | ( | int32_t | msec | ) |
Pauses the execution of the current thread for a specified time.
msec | The number of milliseconds to sleep. |
uint8_t sys_thread_core | ( | void | ) |
Get the CPU core number the current thread is running on.
This function queries the system to determine which CPU core the calling thread is currently scheduled on. Note that threads may migrate between cores, so this value may change over time unless thread affinity is set.
bool sys_thread_create | ( | sys_thread_func_t | func, |
void * | arg | ||
) |
Create a thread on any available core.
func | Function to execute in the new thread |
arg | Argument to pass to the thread function |
Creates a new thread that executes the specified function with the given argument. The thread runs independently and terminates when the function returns. No cleanup or joining is required - the thread is fire-and-forget. On systems with multiple cores, the thread may be scheduled on any available core.
bool sys_thread_create_on_core | ( | sys_thread_func_t | func, |
void * | arg, | ||
uint8_t | core | ||
) |
Create a thread on a specific core.
func | Function to execute in the new thread |
arg | Argument to pass to the thread function |
core | Core number to run the thread on (0-based) |
Creates a new thread that executes the specified function on a specific CPU core. The thread runs independently and terminates when the function returns. If the specified core is not available or already in use, the function returns false. Core 0 is typically the main/boot core.
uint8_t sys_thread_numcores | ( | void | ) |
Returns the number of CPU cores available on the host system.
This function queries the system to determine the total number of processing cores available.