In Rt-thread real-time operating system, the task is implemented by thread, which is the most basic dispatching unit in Rt-thread, it describes the context of a task execution, and also describes the priority level of the task. Important tasks can have a relatively high priority, non-critical task priority can be lowered, and can be similar to Linux with the effect of time-sharing. A thread control block is a data structure used by the operating system to control a thread, which holds some information about the thread, such as the priority, the thread name, and so on, including the linked list structure between threads and threads, the thread waits for the event collection, and so on.
1. Thread control block (rtdef.h)
/** * Thread structure*/structrt_thread{/*Rt_object*///Here is the structure of rt_object, in fact, can also be defined with the Rt_object parent, it is estimated that the thread did not do so earlier, and then did not change. CharName[rt_name_max];/**< the name of threadthe name of the *///objectrt_uint8_t type; /**< type of Object*///Object Type, here is Rt_object_class_threadrt_uint8_t flags; /**< thread ' s flags*///Object Flags#ifdef Rt_using_modulevoid*module_id;/**< ID of application modulethe module ID where the *///thread resides#endifrt_list_t list; /**< the object list*///Kernel object linked list node
rt_list_t tlist; /**< the thread list*///line threads, generally used as a ready queue element node /*stack Point and entry*/ void*SP;/**< Stack point*///A thread's stack pointer, which is primarily a static thread, because dynamic threads allocate memory on the heap void*entry;/**< Entry*///Thread Entry function void*parameter;/**< parameterparameters corresponding to the *///entry function void*STACK_ADDR;/**< Stack Address*///thread Stack addressrt_uint16_t stack_size; /**< Stack size*///thread Stack size /*Error code*/rt_err_t error; /**< Error code*///thread error number, used in the IPC mechanism, whether the flag has been successfully obtainedrt_uint8_t Stat; /**< Thread Stat*///The current state of a thread, such as ready, suspended, etc. /* Priority*/rt_uint8_t current_priority; /**< Current Priority*///Current Priorityrt_uint8_t init_priority; /**< initialized priority*///initial priority level#ifRt_thread_priority_max > 32//define maximum thread priority in Rtconfig.h, default is 32, maximum value is 256rt_uint8_t number; Number, High_mask, Number_mask is related to the algorithm that gets the current highest-priority thread when the thread is dispatched, and the algorithm does not do much explaining rt_uint8_t High_mask;#endifrt_uint32_t Number_mask;#ifDefined (rt_using_event)//Some parameters related to IPC mechanism events /*Thread Event*/rt_uint32_t Event_set; //The event received by this thread rt_uint8_t event_info; Event filtering information for this thread, used to filter events, preserving only events of interest#endifrt_ubase_t Init_tick; /**< thread ' s initialized tick*///thread Initial clock beat numberrt_ubase_t Remaining_tick; /**< remaining tick*///thread runs the remaining number of ticks at the time of the run , minus 1 per 1 Tick,init_tick of the thread. structRt_timer Thread_timer;/**< Built-in thread timer*///Thread Timer void(*cleanup) (structRt_thread *tid);/**< cleanup function when thread exit*///When a thread exits, the cleanup function needs to be performed, equivalent to the thread's destructor, to do some follow-up when the thread is destroyedrt_uint32_t User_data; /**< Private user data beyond this thread*///User Data};typedefstructRt_thread *rt_thread_t;
2. Thread-related interfaces
static thread initialization: Static threading means that thread control blocks, line Cheng stacks are generally set to global variables, are determined at compile time, allocated for processing, and the kernel is not responsible for dynamically allocating memory space. It is important to note that the user-supplied stack head address needs to be system byte aligned. rt_err_t Rt_thread_init (structRt_thread *thread,//thread Handle Const Char*name,//The thread name, maximum length is specified by the RT_NAME_MAX macro defined in Rtconfig.h, and the extra part is automatically truncated void(*entry) (void*parameter),//Thread Entry function void*parameter,//Thread Entry function parameters void*stack_start,//thread stack start addressrt_uint32_t Stack_size,//thread stack size, in bytesrt_uint8_t priority,//the priority of the thread. The priority range is based on the system configuration (defined by the Rt_thread_priority_max macro in rtconfig.h), if the 256-level priority is supported, the range is from 0 to 255, the lower the priority, the higher the value, and 0 represents the highest priority. rt_uint32_t tick)//the time slice size of the thread. The unit of Time slice (tick) is the clock beat of the operating system. When the same priority thread exists in the system, this parameter specifies the maximum length of time that the thread can run at a single dispatch. At the end of this time slice run, the scheduler automatically selects the next ready state of the same priority thread to run.
Dynamic Thread creation: When this function is called, a line Cheng (the TCB, the thread control block) is allocated from the dynamic heap memory and the corresponding space is allocated from the dynamic heap memory as specified in the parameter's stack size. The allocated stack space is aligned according to the configuration rt_align_size mode in Rtconfig.h. rt_thread_t Rt_thread_create (Const Char*name,//The thread name, maximum length is specified by the RT_NAME_MAX macro defined in Rtconfig.h, and the extra part is automatically truncated void(*entry) (void*parameter),//Thread Entry function void*parameter,//Thread Entry function parametersrt_uint32_t Stack_size,//thread heap Size, units are bytesrt_uint8_t priority,//the priority of the thread. The priority range is based on the system configuration (defined by the Rt_thread_priority_max macro in rtconfig.h), if the 256-level priority is supported, the range is from 0 to 255, the lower the priority, the higher the value, and 0 represents the highest priority. rt_uint32_t tick)//the time slice size of the thread. The unit of Time slice (tick) is the clock beat of the operating system. When the same priority thread exists in the system, this parameter specifies the maximum length of time that the thread can run at a single dispatch. At the end of this time slice run, the scheduler automatically selects the next ready state of the same priority thread to run.
thread detachment: the object that the Rt_thread_detach () function operates on is use Rt_thread_ The init () function initializes the static thread control block . The thread itself should not call this interface out of the thread itself. rt_err_t Rt_thread_detach (rt_thread_t thread); thread deletion: When a dynamic thread runs or ends automatically, the system automatically removes the thread and does not need to call the Rt_thread_delete () function interface again. This interface should not be called by the thread itself to remove the thread itself, and can generally be invoked only by other threads or in the Timer timeout function. rt_err_t rt_thread_delete (rt_thread_t thread); After the function is called, the thread object is moved to the outgoing queue and removed from the Kernel object manager, the stack space occupied by the thread is also freed, and the reclaimed space is re-used for other memory allocations. In fact, remove the thread interface with the Rt_thread_delete function simply by changing the corresponding thread state to rt_thread_close state and then putting it into the Rt_thread_ Defunct queue, while the real delete action (release thread control block and release line stacks) requires the idle thread to complete the last thread delete action when the next time the idle thread is executed.
thread start: The state of the thread object that was created (initialized) is in the initial condition and does not go into the scheduled queue of the ready thread, we can invoke the following function interface to start a thread: rt_err_t Rt_ Thread_startup (rt_thread_t thread); When this function is called, will change the state of the thread to the ready state and put it in the corresponding priority queue for scheduling. If the newly-started thread has a higher priority than the current thread, it will switch to this thread immediately. Thread hangs: rt_err_t rt_thread_suspend (rt_thread_t thread); This function should not normally be used to suspend the thread itself, if you do need to suspend the current task with the Rt_thread_suspend function, You need to call the Rt_schedule () function immediately after calling the Rt_thread_suspend () function for a manual thread context switch. Thread recovery: thread recovery is to put the suspended thread back into a ready state, and if the recovered thread is in the first bit of the highest priority list in all ready-made threads, the system will switch the thread context. rt_err_t rt_thread_resume (rt_thread_t thread);
thread yield: when the current thread is out of time or when the thread automatically asks for a processor resource, it no longer occupies the processor, and the scheduler chooses the next highest-priority thread to execute. at this point, the thread that discarded the processor resource is still in the ready queue. rt_err_t Rt_thread_yield ( void ) After calling the function, .
note: the Rt_thread_yield () function is similar to the Rt_schedule () function, but when other ready-to-state threads with the same priority exist , the behavior of the system is completely different. After the
executes the Rt_thread_yield () function, the current thread is swapped out and the next ready thread of the same priority is executed. After the
executes the Rt_schedule () function, the current thread does not have to be swapped out, even if it is swapped out, it is not placed at the end of the thread threads, but rather the highest-priority thread in the system is selected for execution (if there is no thread with a higher priority in the system than the current thread exists, then the Rt_ Schedule () function, the system continues to execute the current thread)
Thread sleep: In real-world applications, we sometimes need to delay the running current thread for a period of time and run it again after the specified time , which is called "Thread sleep." rt_err_t rt_thread_sleep (rt_tick_t tick); rt_err_t rt_thread_delay (rt_tick_t tick); These two function interfaces work the same Calling them can cause the current thread to suspend for a specified amount of time, and when that time elapses, the thread wakes up and enters the ready state again . This function takes a parameter that specifies the sleep time of the thread (in the OS tick clock ticks).
line programmed: rt_err_t Rt_thread_control (rt_thread_t thread, rt_uint8_t cmd,void*arg); indicates that command CMD currently supports commands including • rt_thread_ctrl_change_priority -dynamically changing the priority of a thread; • Rt_thread_ctrl_startup -start running a thread, equivalent to the Rt_thread_startup () function call; • Rt_thread_ctrl_close -closes a thread, equivalent to the Rt_thread_delete () function call. Find thread: rt_thread_t rt_thread_find (Char*name); The lookup thread is found through the Kernel object management system, finds the corresponding kernel object list based on the type of the kernel object, and iterates over it, compares the name, and returns if found.
Current thread:During the course of the program, the same piece of code may be executed by multiple threads, and at execution time the currently executing thread handle can be obtained through the following function interface.
rt_thread_t rt_thread_self (void);
do not call this function in the Interrupt service program because it does not accurately obtain the current execution thread. When the scheduler is not started, this interface returns Rt_null
3. Thread Status
In the process of running a thread, only one thread is allowed to run in the processor in a single time, and from the process of running, the thread has many different running states, such as running state, non-operational state, etc. In the Rt-thread real-time operating system, the thread contains five states, and the operating system dynamically adjusts its state according to the situation it is running. The five thread states in Rt-thread are as follows:
Rt_thread_init The initial state of the thread. It is in this state when the thread has just started to create and is not running, in which case the thread does not participate in scheduling
rt_thread_suspend suspend state, blocking state . The thread is suspended at this time: it may suspend waiting because the resource is unavailable, or the thread is suspended for a period of time. In this state, the thread does not participate in scheduling
Rt_thread_ready ready state . A thread is running, or the operating system looks for the highest priority ready-to-run thread after the current thread has run out of the processor
rt_thread_running running state . The thread is currently running, and in a single-core system, only the thread returned by the Rt_thread_self () function is in this state and is not subject to this restriction in multicore systems.
rt_thread_close thread End state . The thread will be in this state when it finishes running. Threads in this state do not participate in the scheduling of threads.
Rt-thread Kernel line Routines Code analysis