RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
|
结构体 | |
struct | rt_cpu_usage_stats |
struct | rt_cpu |
struct | rt_interrupt_context |
struct | rt_thread |
类型定义 | |
typedef struct rt_cpu_usage_stats * | rt_cpu_usage_stats_t |
typedef struct rt_cpu * | rt_cpu_t |
typedef struct rt_interrupt_context * | rt_interrupt_context_t |
typedef void(* | rt_thread_cleanup_t) (struct rt_thread *tid) |
typedef struct rt_thread * | rt_thread_t |
typedef void(* | rt_thread_inited_hookproto_t) (rt_thread_t thread) |
枚举 | |
enum | { RT_INTERRUPTIBLE = 0 , RT_KILLABLE , RT_UNINTERRUPTIBLE } |
the thread management
RT-Thread operating system supports multitask systems, which are based on thread scheduling.
#define RT_THREAD_SUSPEND_INTERRUPTIBLE (RT_THREAD_SUSPEND_MASK) |
#define RT_THREAD_SUSPEND RT_THREAD_SUSPEND_INTERRUPTIBLE |
#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_YIELD_MASK RT_THREAD_STAT_YIELD |
#define RT_THREAD_STAT_SIGNAL_READY (RT_THREAD_STAT_SIGNAL | RT_THREAD_READY) |
#define RT_THREAD_STAT_SIGNAL_PENDING 0x40 |
#define RT_THREAD_CTRL_STARTUP 0x00 |
#define rt_current_thread rt_thread_self() |
#define RT_SCHEDULER_STACK_CHECK | ( | thr | ) |
在文件 rtthread.h 第 228 行定义.
typedef struct rt_cpu_usage_stats* rt_cpu_usage_stats_t |
typedef struct rt_interrupt_context * rt_interrupt_context_t |
interrupt/exception frame handling
typedef struct rt_thread* rt_thread_t |
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 |
在文件 rtthread.h 第 197 行定义.
anonymous enum |
枚举值 | ||
---|---|---|
RT_INTERRUPTIBLE | 0 | |
RT_KILLABLE | ||
RT_UNINTERRUPTIBLE |
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 ) |
This function will initialize a thread. It's used to initialize a static thread object.
thread | is the static thread object. |
name | is the name of thread, which shall be unique. |
entry | is the entry function of thread. |
parameter | is the parameter of thread enter function. |
stack_start | is the start address of thread stack. |
stack_size | is the size of thread stack. |
priority | is the priority of thread. |
tick | is the time slice if there are same priority thread. |
引用了 rt_thread::entry, rt_thread::parameter, RT_ASSERT, RT_NULL, RT_Object_Class_Thread, rt_object_init() , 以及 rt_thread::stack_size.
RTM_EXPORT | ( | rt_thread_init | ) |
rt_thread_t rt_thread_self | ( | void | ) |
This function will return self thread object.
引用了 rt_cpu::current_thread, rt_cpu_self(), rt_hw_local_irq_disable , 以及 rt_hw_local_irq_enable.
RTM_EXPORT | ( | rt_thread_self | ) |
rt_err_t rt_thread_startup | ( | rt_thread_t | thread | ) |
This function will start a thread and put it to system ready queue.
thread | is the thread to be started. |
引用了 LOG_D, rt_object::name, rt_thread::parent, RT_ASSERT, RT_NULL, RT_Object_Class_Thread, rt_object_get_type(), RT_SCHED_CTX, RT_THREAD_INIT, rt_thread_resume(), RT_THREAD_STAT_MASK , 以及 stat().
RTM_EXPORT | ( | rt_thread_startup | ) |
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_ASSERT, rt_critical_level(), rt_sched_lock(), rt_sched_thread_close(), rt_sched_thread_get_stat(), rt_sched_unlock(), RT_THREAD_CLOSE, RT_THREAD_INIT, rt_thread_self(), rt_timer_detach() , 以及 rt_thread::thread_timer.
RTM_EXPORT | ( | rt_thread_close | ) |
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 | is the thread to be deleted. |
引用了 RT_ASSERT, RT_NULL, RT_Object_Class_Thread, rt_object_get_type() , 以及 rt_object_is_systemobject().
RTM_EXPORT | ( | rt_thread_detach | ) |
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 ) |
This function will create a thread object and allocate thread object memory. and stack.
name | is the name of thread, which shall be unique. |
entry | is the entry function of thread. |
parameter | is the parameter of thread enter function. |
stack_size | is the size of thread stack. |
priority | is the priority of thread. |
tick | is the time slice if there are same priority thread. |
引用了 rt_thread::entry, rt_thread::parameter, RT_ASSERT, RT_KERNEL_MALLOC, RT_NULL, rt_object_allocate(), RT_Object_Class_Thread, rt_object_delete() , 以及 rt_thread::stack_size.
RTM_EXPORT | ( | rt_thread_create | ) |
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 | is the thread to be deleted. |
引用了 RT_ASSERT, RT_FALSE, RT_NULL, RT_Object_Class_Thread, rt_object_get_type() , 以及 rt_object_is_systemobject().
RTM_EXPORT | ( | rt_thread_delete | ) |
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_sched_lock(), rt_sched_thread_yield(), rt_sched_unlock_n_resched() , 以及 rt_thread_self().
RTM_EXPORT | ( | rt_thread_yield | ) |
This function will let current thread delay for some ticks.
tick | is the delay ticks. |
RTM_EXPORT | ( | rt_thread_delay | ) |
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_thread::error, RT_ASSERT, rt_enter_critical(), rt_exit_critical_safe(), RT_NULL, RT_Object_Class_Thread, rt_object_get_type(), rt_schedule(), rt_thread_self(), rt_thread_suspend_with_flag(), rt_tick_get(), rt_timer_control(), RT_TIMER_CTRL_SET_TIME, rt_timer_start(), RT_UNINTERRUPTIBLE , 以及 rt_thread::thread_timer.
RTM_EXPORT | ( | rt_thread_delay_until | ) |
rt_err_t rt_thread_mdelay | ( | rt_int32_t | ms | ) |
This function will let current thread delay for some milliseconds.
ms | is the delay ms time. |
引用了 rt_tick_from_millisecond().
RTM_EXPORT | ( | rt_thread_mdelay | ) |
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 | is the specified thread to be controlled. |
cmd | is the control command, which includes. RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread. RT_THREAD_CTRL_STARTUP for starting a thread. RT_THREAD_CTRL_CLOSE for delete a thread. RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU. |
arg | is the argument of control command. |
引用了 rt_thread::error, RT_ASSERT, RT_NULL, RT_Object_Class_Thread, rt_object_get_type(), rt_object_is_systemobject(), rt_sched_lock(), rt_sched_thread_change_priority(), rt_sched_unlock(), rt_schedule(), RT_THREAD_CTRL_BIND_CPU, RT_THREAD_CTRL_CHANGE_PRIORITY, RT_THREAD_CTRL_CLOSE, RT_THREAD_CTRL_STARTUP, rt_thread_delete(), rt_thread_detach(), rt_thread_startup() , 以及 RT_TRUE.
RTM_EXPORT | ( | rt_thread_control | ) |
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.
引用了 LOG_D, rt_object::name, rt_thread::parent, RT_ASSERT, RT_NULL, RT_Object_Class_Thread, rt_object_get_type(), RT_OBJECT_HOOK_CALL, rt_sched_lock(), rt_sched_thread_get_stat(), rt_sched_thread_timer_stop(), rt_sched_unlock(), rt_susp_list_enqueue(), RT_THREAD_READY, RT_THREAD_RUNNING, rt_thread_self() , 以及 stat().
RTM_EXPORT | ( | rt_thread_suspend_to_list | ) |
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_NULL , 以及 rt_thread_suspend_to_list().
RTM_EXPORT | ( | rt_thread_suspend_with_flag | ) |
rt_err_t rt_thread_suspend | ( | rt_thread_t | thread | ) |
RTM_EXPORT | ( | rt_thread_suspend | ) |
rt_err_t rt_thread_resume | ( | rt_thread_t | thread | ) |
This function will resume a thread and put it to system ready queue.
thread | is the thread to be resumed. |
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::error, LOG_D, rt_object::name, rt_thread::parent, RT_ASSERT, RT_NULL, RT_Object_Class_Thread, rt_object_get_type(), RT_OBJECT_HOOK_CALL, rt_sched_lock(), rt_sched_thread_ready(), rt_sched_unlock() , 以及 rt_sched_unlock_n_resched().
RTM_EXPORT | ( | rt_thread_resume | ) |
rt_thread_t rt_thread_find | ( | char * | name | ) |
This function will find the specified thread.
name | is the name of thread finding. |
引用了 RT_Object_Class_Thread , 以及 rt_object_find().
RTM_EXPORT | ( | rt_thread_find | ) |
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 |
引用了 rt_thread::parent, RT_NULL , 以及 rt_object_get_name().
RTM_EXPORT | ( | rt_thread_get_name | ) |
void rt_thread_suspend_sethook | ( | void(* | hook )(rt_thread_t thread) | ) |
void rt_thread_resume_sethook | ( | void(* | hook )(rt_thread_t thread) | ) |
RT_OBJECT_HOOKLIST_DECLARE | ( | rt_thread_inited_hookproto_t | , |
rt_thread_inited | ) |
void rt_thread_idle_init | ( | void | ) |
This function will initialize idle thread, then start it.
引用了 _CPUS_NR, rt_cpu::idle_thread, rt_cpu_index(), RT_NULL, rt_spin_lock_init(), rt_thread_control(), RT_THREAD_CTRL_BIND_CPU, rt_thread_init() , 以及 rt_thread_startup().
rt_err_t rt_thread_idle_sethook | ( | void(* | hook )(void) | ) |
rt_err_t rt_thread_idle_delhook | ( | void(* | hook )(void) | ) |
delete the idle hook on hook list.
hook | the specified hook function. |
引用了 RT_IDLE_HOOK_LIST_SIZE, RT_NULL, rt_spin_lock_irqsave() , 以及 rt_spin_unlock_irqrestore().
rt_thread_t rt_thread_idle_gethandler | ( | void | ) |
void rt_system_scheduler_init | ( | void | ) |
This function will initialize the system scheduler.
在文件 scheduler_mp.c 第 403 行定义.
引用了 rt_cpu::current_thread, LOG_D, rt_cpu_index(), rt_list_init(), RT_NULL, rt_spin_lock_init() , 以及 rt_thread_priority_table.
void rt_system_scheduler_start | ( | void | ) |
This function will startup the scheduler. It will select one thread with the highest priority level, then switch to it.
legacy rt_cpus_lock. some bsp codes still use it as for it's critical region. Since scheduler is never touching this, here we just release it on the entry.
for the accessing of the scheduler context. Noted that we don't have current_thread at this point
在文件 scheduler_mp.c 第 453 行定义.
引用了 _cpus_lock, _fast_spin_lock(), _fast_spin_unlock(), rt_cpu::current_thread, LOG_D, rt_object::name, rt_thread::parent, RT_ASSERT, rt_cpu_self(), rt_hw_context_switch_to(), rt_hw_cpu_id(), rt_hw_local_irq_disable, rt_hw_spin_unlock, RT_SCHED_CTX, RT_SCHED_PRIV, RT_THREAD_RUNNING , 以及 rt_thread::sp.
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 | ) |
This function checks whether a scheduling is needed after an IRQ context switching. If yes, it will select one thread with the highest priority level, and then switch to it.
context | is the context to be switched to. |
void rt_enter_critical | ( | void | ) |
This function will lock the thread scheduler.
void rt_exit_critical | ( | void | ) |
This function will unlock the thread scheduler.
void rt_exit_critical_safe | ( | rt_base_t | critical_level | ) |
rt_uint16_t rt_critical_level | ( | void | ) |
Get the scheduler lock level.
void rt_scheduler_sethook | ( | void(* | hook )(rt_thread_t from, rt_thread_t to) | ) |
void rt_scheduler_switch_sethook | ( | void(* | hook )(struct rt_thread *tid) | ) |
void rt_scheduler_ipi_handler | ( | int | vector, |
void * | param ) |
This function will handle IPI interrupt and do a scheduling in system.
vector | is the number of IPI interrupt for system scheduling. |
param | is not used, and can be set to RT_NULL. |
void rt_schedule_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 | is the thread to be inserted. |
void rt_schedule_remove_thread | ( | struct rt_thread * | thread | ) |
This function will remove a thread from system ready queue.
thread | is the thread to be removed. |