![]() |
RT-Thread RTOS
An open source embedded real-time operating system
|
Data Structures | |
struct | rt_cpu_usage_stats |
struct | rt_interrupt_context |
struct | rt_thread |
Macros | |
#define | RT_THREAD_INIT 0x00 |
#define | RT_THREAD_CLOSE 0x01 |
#define | RT_THREAD_READY 0x02 |
#define | RT_THREAD_RUNNING 0x03 |
#define | RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) |
#define | RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK) |
#define | RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK) |
#define | RT_THREAD_STAT_YIELD 0x08 |
#define | RT_THREAD_STAT_SIGNAL 0x10 |
#define | RT_THREAD_STAT_SIGNAL_WAIT 0x20 |
#define | RT_THREAD_STAT_SIGNAL_PENDING 0x40 |
#define | RT_THREAD_CTRL_STARTUP 0x00 |
#define | RT_THREAD_CTRL_CLOSE 0x01 |
#define | RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 |
#define | RT_THREAD_CTRL_INFO 0x03 |
#define | RT_THREAD_CTRL_BIND_CPU 0x04 |
#define | RT_THREAD_CTRL_RESET_PRIORITY 0x05 |
Typedefs | |
typedef struct rt_interrupt_context * | rt_interrupt_context_t |
typedef void(* | rt_thread_inited_hookproto_t) (rt_thread_t thread) |
Functions | |
void | rt_system_scheduler_init (void) |
void | rt_system_scheduler_start (void) |
void | rt_scheduler_ipi_handler (int vector, void *param) |
rt_err_t | rt_sched_lock (rt_sched_lock_level_t *plvl) |
rt_err_t | rt_sched_unlock (rt_sched_lock_level_t level) |
rt_bool_t | rt_sched_is_locked (void) |
rt_err_t | rt_sched_unlock_n_resched (rt_sched_lock_level_t level) |
void | rt_schedule (void) |
void | rt_scheduler_do_irq_switch (void *context) |
void | rt_sched_insert_thread (struct rt_thread *thread) |
void | rt_sched_remove_thread (struct rt_thread *thread) |
void | rt_sched_thread_init_priv (struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority) |
void | rt_sched_thread_startup (struct rt_thread *thread) |
void | rt_sched_post_ctx_switch (struct rt_thread *thread) |
void | rt_exit_critical_safe (rt_base_t critical_level) |
rt_base_t | rt_enter_critical (void) |
void | rt_exit_critical (void) |
rt_uint16_t | rt_critical_level (void) |
rt_err_t | rt_sched_thread_bind_cpu (struct rt_thread *thread, int cpu) |
rt_err_t | rt_thread_init (struct rt_thread *thread, const char *name, void(*entry)(void *parameter), void *parameter, void *stack_start, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) |
rt_thread_t | rt_thread_self (void) |
rt_err_t | rt_thread_startup (rt_thread_t thread) |
rt_err_t | rt_thread_close (rt_thread_t thread) |
rt_err_t | rt_thread_detach (rt_thread_t thread) |
rt_thread_t | rt_thread_create (const char *name, void(*entry)(void *parameter), void *parameter, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) |
rt_err_t | rt_thread_delete (rt_thread_t thread) |
rt_err_t | rt_thread_yield (void) |
rt_err_t | rt_thread_delay (rt_tick_t tick) |
rt_err_t | rt_thread_delay_until (rt_tick_t *tick, rt_tick_t inc_tick) |
rt_err_t | rt_thread_mdelay (rt_int32_t ms) |
rt_err_t | rt_thread_control (rt_thread_t thread, int cmd, void *arg) |
rt_err_t | rt_thread_suspend_to_list (rt_thread_t thread, rt_list_t *susp_list, int ipc_flags, int suspend_flag) |
rt_err_t | rt_thread_suspend_with_flag (rt_thread_t thread, int suspend_flag) |
rt_err_t | rt_thread_suspend (rt_thread_t thread) |
rt_err_t | rt_thread_resume (rt_thread_t thread) |
rt_thread_t | rt_thread_find (char *name) |
rt_err_t | rt_thread_get_name (rt_thread_t thread, char *name, rt_uint8_t name_size) |
int | rt_thread_kill (rt_thread_t tid, int sig) |
void | rt_thread_idle_init (void) |
rt_err_t | rt_thread_idle_sethook (void(*hook)(void)) |
rt_err_t | rt_thread_idle_delhook (void(*hook)(void)) |
rt_thread_t | rt_thread_idle_gethandler (void) |
See Thread Management.
#define RT_THREAD_INIT 0x00 |
Initialized status
#define RT_THREAD_CLOSE 0x01 |
Closed status
#define RT_THREAD_READY 0x02 |
Ready status
#define RT_THREAD_RUNNING 0x03 |
Running status
#define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) |
Suspend interruptable 0x4
#define RT_THREAD_SUSPEND_KILLABLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK) |
Suspend with killable 0x6
#define RT_THREAD_SUSPEND_UNINTERRUPTIBLE (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK) |
Suspend with uninterruptable 0x7
#define RT_THREAD_STAT_YIELD 0x08 |
indicate whether remaining_tick has been reloaded since last schedule
#define RT_THREAD_STAT_SIGNAL 0x10 |
task hold signals
#define RT_THREAD_STAT_SIGNAL_WAIT 0x20 |
task is waiting for signals
#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 |
signals is held and it has not been procressed
#define RT_THREAD_CTRL_STARTUP 0x00 |
thread control command definitions Startup thread.
#define RT_THREAD_CTRL_CLOSE 0x01 |
Close thread.
#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 |
Change thread priority.
#define RT_THREAD_CTRL_INFO 0x03 |
Get thread information.
#define RT_THREAD_CTRL_BIND_CPU 0x04 |
Set thread bind cpu.
#define RT_THREAD_CTRL_RESET_PRIORITY 0x05 |
Reset thread priority.
typedef struct rt_interrupt_context * rt_interrupt_context_t |
interrupt/exception frame handling
typedef void(* rt_thread_inited_hookproto_t) (rt_thread_t thread) |
Sets a hook function when a thread is initialized.
thread | is the target thread that initializing |
void rt_system_scheduler_init | ( | void | ) |
Initialize the system scheduler.
void rt_system_scheduler_start | ( | void | ) |
Start the system scheduler and switch to the highest priority thread.
void rt_scheduler_ipi_handler | ( | int | vector, |
void * | param | ||
) |
This function will handle IPI interrupt and do a scheduling in system.
This function should be invoke or register as ISR in BSP.
vector | The number of IPI interrupt for system scheduling. |
param | Not used, and can be set to RT_NULL. |
rt_err_t rt_sched_lock | ( | rt_sched_lock_level_t * | plvl | ) |
Lock the system scheduler.
plvl | Pointer to the object where lock level stores to |
rt_err_t rt_sched_unlock | ( | rt_sched_lock_level_t | level | ) |
Unlock the scheduler and restore the interrupt level.
level | The interrupt level to restore (previously saved by rt_sched_lock) |
rt_bool_t rt_sched_is_locked | ( | void | ) |
Check if the scheduler is currently locked.
This function checks the scheduler lock status in a thread-safe manner by temporarily disabling interrupts to get consistent state.
rt_err_t rt_sched_unlock_n_resched | ( | rt_sched_lock_level_t | level | ) |
Unlock scheduler and perform rescheduling if needed.
level | The scheduler lock level obtained from rt_sched_lock() |
-RT_ESCHEDLOCKED: Scheduler still locked by others
This function:
void rt_schedule | ( | void | ) |
This function will perform one scheduling. It will select one thread with the highest priority level in global ready queue or local ready queue, then switch to it.
void rt_scheduler_do_irq_switch | ( | void * | context | ) |
Perform thread scheduling after an interrupt context switch.
context | The interrupt context pointer |
This function handles scheduling when returning from interrupt context:
void rt_sched_insert_thread | ( | struct rt_thread * | thread | ) |
This function will insert a thread to the system ready queue. The state of thread will be set as READY and the thread will be removed from suspend queue.
thread | The thread to be inserted. |
void rt_sched_remove_thread | ( | struct rt_thread * | thread | ) |
This function will remove a thread from system ready queue.
thread | The thread to be removed. |
void rt_sched_thread_init_priv | ( | struct rt_thread * | thread, |
rt_uint32_t | tick, | ||
rt_uint8_t | priority | ||
) |
Initialize thread's scheduling private data.
thread | The thread to be initialized |
tick | Initial time slice value for the thread |
priority | Initial priority of the thread |
This function performs the following initialization:
void rt_sched_thread_startup | ( | struct rt_thread * | thread | ) |
Initialize thread scheduling attributes for startup.
thread | The thread to be initialized |
This function:
void rt_sched_post_ctx_switch | ( | struct rt_thread * | thread | ) |
Update thread scheduling status after context switch.
thread | The thread that will be running after the context switch |
This function performs critical post-context-switch operations:
void rt_exit_critical_safe | ( | rt_base_t | critical_level | ) |
Safely exit critical section (non-debug version)
critical_level | The expected critical level (unused in non-debug build) |
This is the non-debug version that simply calls rt_exit_critical(). The critical_level parameter is ignored in this implementation.
rt_base_t rt_enter_critical | ( | void | ) |
Enter a critical section and lock the scheduler.
This function:
void rt_exit_critical | ( | void | ) |
Exit a critical section and unlock the scheduler.
This function performs the following operations:
rt_uint16_t rt_critical_level | ( | void | ) |
Get the scheduler lock level.
rt_err_t rt_sched_thread_bind_cpu | ( | struct rt_thread * | thread, |
int | cpu | ||
) |
Bind a thread to a specific CPU core.
thread | The thread to be bound |
cpu | The target CPU core index (RT_CPUS_NR for no binding) |
RT_EOK: Operation successful
This function handles thread-CPU binding with the following logic:
rt_err_t rt_thread_init | ( | struct rt_thread * | thread, |
const char * | name, | ||
void(*)(void *parameter) | entry, | ||
void * | parameter, | ||
void * | stack_start, | ||
rt_uint32_t | stack_size, | ||
rt_uint8_t | priority, | ||
rt_uint32_t | tick | ||
) |
This function will initialize a thread. It's used to initialize a static thread object.
thread | Thread handle. Thread handle is provided by the user and points to the corresponding thread control block memory address. |
name | Name of the thread (shall be unique); the maximum length of the thread name is specified by the RT_NAME_MAX macro defined in rtconfig.h , and the extra part is automatically truncated. |
entry | Entry function of thread. |
parameter | Parameter of thread entry function. |
stack_start | Start address of thread stack. |
stack_size | Size of thread stack in bytes. Stack space address alignment is required in most systems (for example, alignment to 4-byte addresses in the ARM architecture). |
priority | Priority of thread. The priority range is based on the system configuration (macro definition RT_THREAD_PRIORITY_MAX in rtconfig.h ). If 256 levels of priority are supported, the range is from 0 to 255. The smaller the value, the higher the priority, and 0 is the highest priority. |
tick | Time slice if there are same priority thread. The unit of the time slice (tick) is the tick of the operating system. When there are threads with the same priority in the system, this parameter specifies the maximum length of time of a thread for one schedule. At the end of this time slice run, the scheduler automatically selects the next ready state of the same priority thread to run. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_thread_t rt_thread_self | ( | void | ) |
This function will return self thread object.
RT_NULL
, it means that the scheduler has not started yet. rt_err_t rt_thread_startup | ( | rt_thread_t | thread | ) |
This function will start a thread and put it to system ready queue.
thread | Handle of the thread to be started. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_err_t rt_thread_close | ( | rt_thread_t | thread | ) |
This function will close a thread. The thread object will be removed from thread queue and detached/deleted from the system object management. It's different from rt_thread_delete or rt_thread_detach that this will not enqueue the closing thread to cleanup queue.
thread | is the thread to be closed. |
rt_err_t rt_thread_detach | ( | rt_thread_t | thread | ) |
This function will detach a thread. The thread object will be removed from thread queue and detached/deleted from the system object management.
thread | Handle of the thread to be deleted. The thread must be initialized by rt_thread_init() . |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_thread_t rt_thread_create | ( | const char * | name, |
void(*)(void *parameter) | entry, | ||
void * | parameter, | ||
rt_uint32_t | stack_size, | ||
rt_uint8_t | priority, | ||
rt_uint32_t | tick | ||
) |
This function will create a thread object and allocate thread object memory. and stack.
name | The name of the thread (shall be unique.); the maximum length of the thread name is specified by macro RT_NAME_MAX in rtconfig.h , and the extra part is automatically truncated. |
entry | Entry function of thread. |
parameter | Parameter of thread entry function. |
stack_size | Size of thread stack in bytes. |
priority | Priority of thread. The priority range is based on the system configuration (macro definition RT_THREAD_PRIORITY_MAX in rtconfig.h). If 256-level priority is supported, then the range is from 0 to 255. The smaller the value, the higher the priority, and 0 is the highest priority. |
tick | Time slice if there are same priority thread. The unit of the time slice (tick) is the tick of the operating system. When there are threads with the same priority in the system, this parameter specifies the maximum length of time of a thread for one schedule. At the end of this time slice run, the scheduler automatically selects the next ready state of the same priority thread to run. |
rt_thread
structure pointer, the function is successfully executed. If the return value is RT_NULL
, it means this operation failed. rt_err_t rt_thread_delete | ( | rt_thread_t | thread | ) |
This function will delete a thread. The thread object will be removed from thread queue and deleted from system object management in the idle thread.
thread | Handle of the thread to be deleted. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_err_t rt_thread_yield | ( | void | ) |
This function will let current thread yield processor, and scheduler will choose the highest thread to run. After yield processor, the current thread is still in READY state.
rt_err_t rt_thread_delay | ( | rt_tick_t | tick | ) |
This function will let current thread delay for some ticks.
tick | The delay ticks, in units of 1 OS Tick. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_err_t rt_thread_delay_until | ( | rt_tick_t * | tick, |
rt_tick_t | inc_tick | ||
) |
This function will let current thread delay until (*tick + inc_tick).
tick | is the tick of last wakeup. |
inc_tick | is the increment tick. |
rt_err_t rt_thread_mdelay | ( | rt_int32_t | ms | ) |
This function will let current thread delay for some milliseconds.
ms | The delay time in units of 1ms. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_err_t rt_thread_control | ( | rt_thread_t | thread, |
int | cmd, | ||
void * | arg | ||
) |
This function will control thread behaviors according to control command.
thread | Handle of the thread to be controlled. |
cmd | Control command, which includes.
|
arg | Argument of control command. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_err_t rt_thread_suspend_to_list | ( | rt_thread_t | thread, |
rt_list_t * | susp_list, | ||
int | ipc_flags, | ||
int | suspend_flag | ||
) |
This function will suspend the specified thread and change it to suspend state.
Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while sharing a resouce with other threads and occupying this resouce, starvation can occur very easily.
thread | the thread to be suspended. |
susp_list | the list thread enqueued to. RT_NULL if no list. |
ipc_flags | is a flag for the thread object to be suspended. It determines how the thread is suspended. The flag can be ONE of the following values: RT_IPC_FLAG_PRIO The pending threads will queue in order of priority. RT_IPC_FLAG_FIFO The pending threads will queue in the first-in-first-out method (also known as first-come-first-served (FCFS) scheduling strategy). NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about the first-in-first-out principle, and you clearly understand that all threads involved in this semaphore will become non-real-time threads. |
suspend_flag | status flag of the thread to be suspended. |
enqueue thread on the push list before leaving critical region of scheduler, so we won't miss notification of async events.
rt_err_t rt_thread_suspend_with_flag | ( | rt_thread_t | thread, |
int | suspend_flag | ||
) |
This function will suspend the specified thread and change it to suspend state.
Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while sharing a resouce with other threads and occupying this resouce, starvation can occur very easily.
thread | the thread to be suspended. |
suspend_flag | status flag of the thread to be suspended. |
rt_err_t rt_thread_suspend | ( | rt_thread_t | thread | ) |
This function will suspend the specified thread and change it to suspend state.
When suspending the current thread: rt_thread_suspend(rt_thread_self()); This is generally safe as the thread voluntarily suspends itself.
When suspending other threads: You have no way of knowing what code another thread is executing when you suspend it. If you suspend a thread while it is sharing a resource with other threads and occupying this resource (such as holding a mutex, semaphore, or other synchronization objects), deadlock or resource starvation can occur very easily.
Only suspend other threads when you have full control over the system state and understand the implications.
thread | Handle of the thread to be suspended. Can be the current thread (rt_thread_self()) or any other thread. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. rt_err_t rt_thread_resume | ( | rt_thread_t | thread | ) |
This function will resume a thread and put it to system ready queue.
thread | Handle of the thread to be resumed. |
RT_EOK
, the function is successfully executed. If the return value is any other values, it means this operation failed. RT_ESCHEDLOCKED indicates that the current thread is in a critical section, rather than 'thread' can't be resumed. Therefore, we can ignore this error.
rt_thread_t rt_thread_find | ( | char * | name | ) |
This function will find the specified thread.
name | is the name of thread finding. |
rt_err_t rt_thread_get_name | ( | rt_thread_t | thread, |
char * | name, | ||
rt_uint8_t | name_size | ||
) |
This function will return the name of the specified thread.
thread | the thread to retrieve thread name |
name | buffer to store the thread name string |
name_size | maximum size of the buffer to store the thread name |
int rt_thread_kill | ( | rt_thread_t | tid, |
int | sig | ||
) |
This function can be used to send any signal to any thread.
tid | is a pointer to the thread that receives the signal. |
sig | is a specific signal value (range: 0 ~ RT_SIG_MAX). |
void rt_thread_idle_init | ( | void | ) |
This function will initialize idle thread, then start it.
rt_err_t rt_thread_idle_sethook | ( | void(*)(void) | hook | ) |
This function sets a hook function to idle thread loop. When the system performs idle loop, this hook function should be invoked.
hook | the specified hook function. |
RT_EOK
: set OK. -RT_EFULL
: hook list is full.rt_err_t rt_thread_idle_delhook | ( | void(*)(void) | hook | ) |
delete the idle hook on hook list.
hook | the specified hook function. |
RT_EOK
: delete OK. -RT_ENOSYS
: hook was not found. rt_thread_t rt_thread_idle_gethandler | ( | void | ) |
This function will get the handler of the idle thread.