RT-Thread RTOS
An open source embedded real-time operating system
Thread Management
+ Collaboration diagram for Thread Management:

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_contextrt_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)
 

Detailed Description

See Thread Management.

Macro Definition Documentation

◆ RT_THREAD_INIT

#define RT_THREAD_INIT   0x00

Initialized status

◆ RT_THREAD_CLOSE

#define RT_THREAD_CLOSE   0x01

Closed status

◆ RT_THREAD_READY

#define RT_THREAD_READY   0x02

Ready status

◆ RT_THREAD_RUNNING

#define RT_THREAD_RUNNING   0x03

Running status

◆ RT_THREAD_SUSPEND_INTERRUPTIBLE

#define RT_THREAD_SUSPEND_INTERRUPTIBLE   (RT_THREAD_SUSPEND_MASK)

Suspend interruptable 0x4

◆ RT_THREAD_SUSPEND_KILLABLE

#define RT_THREAD_SUSPEND_KILLABLE   (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK)

Suspend with killable 0x6

◆ RT_THREAD_SUSPEND_UNINTERRUPTIBLE

#define RT_THREAD_SUSPEND_UNINTERRUPTIBLE   (RT_THREAD_SUSPEND_MASK | RT_SIGNAL_COMMON_WAKEUP_MASK | RT_SIGNAL_KILL_WAKEUP_MASK)

Suspend with uninterruptable 0x7

◆ RT_THREAD_STAT_YIELD

#define RT_THREAD_STAT_YIELD   0x08

indicate whether remaining_tick has been reloaded since last schedule

◆ RT_THREAD_STAT_SIGNAL

#define RT_THREAD_STAT_SIGNAL   0x10

task hold signals

◆ RT_THREAD_STAT_SIGNAL_WAIT

#define RT_THREAD_STAT_SIGNAL_WAIT   0x20

task is waiting for signals

◆ RT_THREAD_STAT_SIGNAL_PENDING

#define RT_THREAD_STAT_SIGNAL_PENDING   0x40

signals is held and it has not been procressed

◆ RT_THREAD_CTRL_STARTUP

#define RT_THREAD_CTRL_STARTUP   0x00

thread control command definitions Startup thread.

◆ RT_THREAD_CTRL_CLOSE

#define RT_THREAD_CTRL_CLOSE   0x01

Close thread.

◆ RT_THREAD_CTRL_CHANGE_PRIORITY

#define RT_THREAD_CTRL_CHANGE_PRIORITY   0x02

Change thread priority.

◆ RT_THREAD_CTRL_INFO

#define RT_THREAD_CTRL_INFO   0x03

Get thread information.

◆ RT_THREAD_CTRL_BIND_CPU

#define RT_THREAD_CTRL_BIND_CPU   0x04

Set thread bind cpu.

◆ RT_THREAD_CTRL_RESET_PRIORITY

#define RT_THREAD_CTRL_RESET_PRIORITY   0x05

Reset thread priority.

Typedef Documentation

◆ rt_interrupt_context_t

interrupt/exception frame handling

◆ rt_thread_inited_hookproto_t

typedef void(* rt_thread_inited_hookproto_t) (rt_thread_t thread)

Sets a hook function when a thread is initialized.

Parameters
threadis the target thread that initializing

Function Documentation

◆ rt_system_scheduler_init()

void rt_system_scheduler_init ( void  )

Initialize the system scheduler.

Note
This function must be called before any thread scheduling can occur. It prepares the scheduler data structures for multi-core operation.
This function has both MP version and UP version.

◆ rt_system_scheduler_start()

void rt_system_scheduler_start ( void  )

Start the system scheduler and switch to the highest priority thread.

Note
This function will not return after successful execution. It performs the initial thread switch during system startup. The scheduler must be initialized before calling this function.
This function has both MP version and UP version.

◆ rt_scheduler_ipi_handler()

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.

Parameters
vectorThe number of IPI interrupt for system scheduling.
paramNot used, and can be set to RT_NULL.
Note
This function only has MP version.

◆ rt_sched_lock()

rt_err_t rt_sched_lock ( rt_sched_lock_level_t *  plvl)

Lock the system scheduler.

Parameters
plvlPointer to the object where lock level stores to
Returns
rt_err_t
  • RT_EOK on success
  • -RT_EINVAL if plvl is NULL
Note
This function has both MP version and UP version.

◆ rt_sched_unlock()

rt_err_t rt_sched_unlock ( rt_sched_lock_level_t  level)

Unlock the scheduler and restore the interrupt level.

Parameters
levelThe interrupt level to restore (previously saved by rt_sched_lock)
Returns
rt_err_t Always returns RT_EOK
Note
This function has both MP version and UP version.

◆ rt_sched_is_locked()

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.

Returns
rt_bool_t Returns RT_TRUE if scheduler is locked, RT_FALSE otherwise
Note
This function only has MP version.

◆ rt_sched_unlock_n_resched()

rt_err_t rt_sched_unlock_n_resched ( rt_sched_lock_level_t  level)

Unlock scheduler and perform rescheduling if needed.

Parameters
levelThe scheduler lock level obtained from rt_sched_lock()
Returns
rt_err_t
  • RT_EOK: Success
  • -RT_EBUSY: Scheduler not available
  • -RT_ESCHEDISR: Called in interrupt context
  • -RT_ESCHEDLOCKED: Scheduler still locked by others

    This function:

  • Releases scheduler lock at specified level
  • Checks if rescheduling is needed
  • If needed, finds highest priority thread and switches to it
  • Processes pending signals for current thread
  • Handles various error conditions
Note
Must be called in pair with rt_sched_lock() May trigger context switch if conditions met
This function has both MP version and UP version.

◆ rt_schedule()

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.

Note
This function has both MP version and UP version.

◆ rt_scheduler_do_irq_switch()

void rt_scheduler_do_irq_switch ( void *  context)

Perform thread scheduling after an interrupt context switch.

Parameters
contextThe interrupt context pointer

This function handles scheduling when returning from interrupt context:

  • Checks if scheduling is needed (irq_switch_flag set)
  • If needed, finds highest priority ready thread
  • Performs context switch to new thread if available
  • Handles cases where scheduler is locked or still in interrupt context
  • Processes pending signals before scheduling
Note
This function only has MP version.

◆ rt_sched_insert_thread()

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.

Parameters
threadThe thread to be inserted.
Note
Please do not invoke this function in user application. Caller must hold the scheduler lock
This function has both MP version and UP version.

◆ rt_sched_remove_thread()

void rt_sched_remove_thread ( struct rt_thread thread)

This function will remove a thread from system ready queue.

Parameters
threadThe thread to be removed.
Note
Please do not invoke this function in user application.
This function has both MP version and UP version.

◆ rt_sched_thread_init_priv()

void rt_sched_thread_init_priv ( struct rt_thread thread,
rt_uint32_t  tick,
rt_uint8_t  priority 
)

Initialize thread's scheduling private data.

Parameters
threadThe thread to be initialized
tickInitial time slice value for the thread
priorityInitial priority of the thread

This function performs the following initialization:

  • Initializes thread's ready list node
  • Sets initial and current priority (must be < RT_THREAD_PRIORITY_MAX)
  • Initializes priority bitmasks (handles >32 priorities if needed)
  • Sets initial time slice values
  • For SMP systems, initializes critical section nesting counter
Note
This function has both MP version and UP version.

◆ rt_sched_thread_startup()

void rt_sched_thread_startup ( struct rt_thread thread)

Initialize thread scheduling attributes for startup.

Parameters
threadThe thread to be initialized

This function:

  • For systems with >32 priorities:
    • Sets priority number (5 bits)
    • Initializes number_mask and high_mask (3 bits)
  • For systems with <=32 priorities:
    • Initializes number_mask
  • Sets thread state to SUSPEND
Note
This is a lockless operation as it's called during thread creation when no concurrent access is possible
This function has both MP version and UP version.

◆ rt_sched_post_ctx_switch()

void rt_sched_post_ctx_switch ( struct rt_thread thread)

Update thread scheduling status after context switch.

Parameters
threadThe thread that will be running after the context switch

This function performs critical post-context-switch operations:

  • Verifies interrupts are disabled (RT_ASSERT)
  • Releases scheduler lock from previous thread if exists
  • Updates current thread pointer for the CPU
  • Must be called with interrupts disabled
Note
this operation is taken as an atomic operation of the update of SP. Since the local irq is disabled, it's okay to assume that the stack will not be modified meanwhile.
This function only has MP version.

◆ rt_exit_critical_safe()

void rt_exit_critical_safe ( rt_base_t  critical_level)

Safely exit critical section (non-debug version)

Parameters
critical_levelThe 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.

Note
This function has both MP version and UP version.

◆ rt_enter_critical()

rt_base_t rt_enter_critical ( void  )

Enter a critical section and lock the scheduler.

Returns
The critical level after entering (nesting count) Returns -RT_EINVAL if scheduler is unavailable

This function:

  • Disables interrupts to ensure atomic operation
  • Gets current thread context
  • Increments critical section nesting counter
  • Returns the new nesting level
  • Handles both hardware-assisted and software thread context cases
Note
Must be paired with rt_exit_critical() Can be nested, each call must have matching exit call
This function has both MP version and UP version.

◆ rt_exit_critical()

void rt_exit_critical ( void  )

Exit a critical section and unlock the scheduler.

This function performs the following operations:

  • Gets current thread context (using hardware-assisted or software method)
  • Decrements critical section nesting counter
  • If nesting level reaches 0:
    • Checks if rescheduling is needed
    • Clears critical switch flag
    • Performs rescheduling if needed
  • Verifies critical section nesting is valid (debug assertion)
  • Restores interrupt state
Note
Must be called in pair with rt_enter_critical() Handles both hardware-assisted and software thread context cases May trigger rescheduling if conditions met
This function has both MP version and UP version.

◆ rt_critical_level()

rt_uint16_t rt_critical_level ( void  )

Get the scheduler lock level.

Returns
The level of the scheduler lock. 0 means unlocked.
Note
This function has both MP version and UP version.

◆ rt_sched_thread_bind_cpu()

rt_err_t rt_sched_thread_bind_cpu ( struct rt_thread thread,
int  cpu 
)

Bind a thread to a specific CPU core.

Parameters
threadThe thread to be bound
cpuThe target CPU core index (RT_CPUS_NR for no binding)
Returns
rt_err_t
  • RT_EOK: Operation successful

    This function handles thread-CPU binding with the following logic:

  • If thread is READY:
    • Removes from current ready queue
    • Updates bind CPU information
    • Inserts to new ready queue
    • Triggers rescheduling if needed
  • If thread is RUNNING:
    • Updates bind CPU information
    • Sends IPI to target CPU if binding changed
    • Triggers rescheduling if needed
  • For other states, just updates bind CPU info
Note
Caller must ensure scheduler is not locked before calling This function will acquire scheduler lock internally
This function has both MP version and UP version, but UP version does nothing and always returns -RT_EINVAL.

◆ rt_thread_init()

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.

Parameters
threadThread handle. Thread handle is provided by the user and points to the corresponding thread control block memory address.
nameName 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.
entryEntry function of thread.
parameterParameter of thread entry function.
stack_startStart address of thread stack.
stack_sizeSize 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).
priorityPriority 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.
tickTime 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.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_self()

rt_thread_t rt_thread_self ( void  )

This function will return self thread object.

Returns
The self thread object. If returns RT_NULL, it means that the scheduler has not started yet.

◆ rt_thread_startup()

rt_err_t rt_thread_startup ( rt_thread_t  thread)

This function will start a thread and put it to system ready queue.

Parameters
threadHandle of the thread to be started.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_close()

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.

Parameters
threadis the thread to be closed.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_detach()

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.

Parameters
threadHandle of the thread to be deleted. The thread must be initialized by rt_thread_init().
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_create()

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.

Parameters
nameThe 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.
entryEntry function of thread.
parameterParameter of thread entry function.
stack_sizeSize of thread stack in bytes.
priorityPriority 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.
tickTime 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.
Returns
If the return value is a rt_thread structure pointer, the function is successfully executed. If the return value is RT_NULL, it means this operation failed.

◆ rt_thread_delete()

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.

Parameters
threadHandle of the thread to be deleted.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_yield()

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.

Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_delay()

rt_err_t rt_thread_delay ( rt_tick_t  tick)

This function will let current thread delay for some ticks.

Parameters
tickThe delay ticks, in units of 1 OS Tick.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_delay_until()

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).

Parameters
tickis the tick of last wakeup.
inc_tickis the increment tick.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_mdelay()

rt_err_t rt_thread_mdelay ( rt_int32_t  ms)

This function will let current thread delay for some milliseconds.

Parameters
msThe delay time in units of 1ms.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_control()

rt_err_t rt_thread_control ( rt_thread_t  thread,
int  cmd,
void *  arg 
)

This function will control thread behaviors according to control command.

Parameters
threadHandle of the thread to be controlled.
cmdControl command, which includes.
  • RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread.
  • RT_THREAD_CTRL_STARTUP for starting a thread, equivalent to the rt_thread_startup() function call.
  • RT_THREAD_CTRL_CLOSE for closing a thread, equivalent to the rt_thread_delete() function call.
  • RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
  • RT_THREAD_CTRL_RESET_PRIORITY for reset priority level of thread.
argArgument of control command.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_suspend_to_list()

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.

Note
This function ONLY can suspend current thread itself. rt_thread_suspend(rt_thread_self());

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.

Parameters
threadthe thread to be suspended.
susp_listthe list thread enqueued to. RT_NULL if no list.
ipc_flagsis 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_flagstatus flag of the thread to be suspended.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

enqueue thread on the push list before leaving critical region of scheduler, so we won't miss notification of async events.

◆ rt_thread_suspend_with_flag()

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.

Note
This function ONLY can suspend current thread itself. rt_thread_suspend(rt_thread_self());

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.

Parameters
threadthe thread to be suspended.
suspend_flagstatus flag of the thread to be suspended.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_suspend()

rt_err_t rt_thread_suspend ( rt_thread_t  thread)

This function will suspend the specified thread and change it to suspend state.

Note
This function can suspend both the current thread itself and other threads. Please use this API with extreme caution when suspending other threads.

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.

Warning
Suspending other threads arbitrarily can lead to:
  • Deadlock situations
  • Resource starvation
  • System instability
  • Unpredictable behavior

Only suspend other threads when you have full control over the system state and understand the implications.

Parameters
threadHandle of the thread to be suspended. Can be the current thread (rt_thread_self()) or any other thread.
Returns
Return the operation status. If the return value is RT_EOK, the function is successfully executed. If the return value is any other values, it means this operation failed.

◆ rt_thread_resume()

rt_err_t rt_thread_resume ( rt_thread_t  thread)

This function will resume a thread and put it to system ready queue.

Parameters
threadHandle of the thread to be resumed.
Returns
Return the operation status. If the return value is 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_find()

rt_thread_t rt_thread_find ( char *  name)

This function will find the specified thread.

Note
Please don't invoke this function in interrupt status.
Parameters
nameis the name of thread finding.
Returns
If the return value is a rt_thread structure pointer, the function is successfully executed. If the return value is RT_NULL, it means this operation failed.

◆ rt_thread_get_name()

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.

Note
Please don't invoke this function in interrupt status
Parameters
threadthe thread to retrieve thread name
namebuffer to store the thread name string
name_sizemaximum size of the buffer to store the thread name
Returns
If the return value is RT_EOK, the function is successfully executed If the return value is -RT_EINVAL, it means this operation failed

◆ rt_thread_kill()

int rt_thread_kill ( rt_thread_t  tid,
int  sig 
)

This function can be used to send any signal to any thread.

Parameters
tidis a pointer to the thread that receives the signal.
sigis a specific signal value (range: 0 ~ RT_SIG_MAX).
Returns
Return the operation status. When the return value is RT_EOK, the operation is successful. If the return value is any other values, it means that the signal send failed.

◆ rt_thread_idle_init()

void rt_thread_idle_init ( void  )

This function will initialize idle thread, then start it.

Note
this function must be invoked when system init.

◆ rt_thread_idle_sethook()

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.

Parameters
hookthe specified hook function.
Returns
RT_EOK: set OK. -RT_EFULL: hook list is full.
Note
the hook function must be simple and never be blocked or suspend.

◆ rt_thread_idle_delhook()

rt_err_t rt_thread_idle_delhook ( void(*)(void)  hook)

delete the idle hook on hook list.

Parameters
hookthe specified hook function.
Returns
RT_EOK: delete OK. -RT_ENOSYS: hook was not found.

◆ rt_thread_idle_gethandler()

rt_thread_t rt_thread_idle_gethandler ( void  )

This function will get the handler of the idle thread.