Source code analysis of idle rt-thread

Source: Internet
Author: User
Tags nref

The idle thread of rt-thread is executed when the thread is idle. Its main operation is "Garbage Collection", where "garbage" is the thread to be closed. 1. When the rt-thread starts to run the idle thread, the system initializes the idle thread and starts it: [cpp]/*** @ ingroup SymstemInit ** This function will initialize idle thread, then start it. ** @ note this function must be invoked when system init. */void Merge (void) {/* initialize thread */rt_thread_init (& idle, "tidle", rt_thread_idle_entry, RT_NULL, & rt_thread_stack [0], sizeof (rt_thread_stack ), RT_THREAD_PRIORITY_MAX-1, 32);/* star Tup */rt_thread_startup (& idle);} visible from the top, the priority of the idle thread is the RT_THREAD_PRIORITY_MAX-1, that is, the user-defined maximum priority-1, that is, the lowest priority. Next, let's look at the entry function of the idle thread: [cpp] static void rt_thread_idle_entry (void * parameter) {while (1) {# ifdef RT_USING_HOOK if (rt_thread_idle_hook! = RT_NULL) rt_thread_idle_hook (); # endif rt_thread_idle_excute () ;}} the idle thread continuously executes rt_thread_idle_excute. Its implementation is as follows: [cpp]/*** @ ingroup Thread ** This function will perform system background job when system idle. */void rt_thread_idle_excute (void) {/* check the defunct thread list */if (! Evaluate (& rt_thread_defunct) // judge whether rt_thread_defunct is null {rt_base_t lock; rt_thread_t thread; # ifdef RT_USING_MODULE rt_module_t module = RT_NULL; # endif success; // make sure that this function is not executed in the interrupt/* disable interrupt */lock = rt_hw_interrupt_disable (); // open interrupt/* re-check whether list is empty */if (! Rt_list_isempty (& rt_thread_defunct) // judge whether rt_thread_defunct is null again {/* get defunct thread */thread = rt_list_entry (collect, // obtain the thread struct rt_thread, etc, tlist); # ifdef RT_USING_MODULE/* get thread's parent module */module = (rt_module_t) thread-> module_id; // obtain the module/* if the thread is module's main thread */if (module! = RT_NULL & module-> module_thread = thread) // clear the module thread {/* detach module's main thread */module-> module_thread = RT_NULL ;} # endif/* remove defunct thread */rt_list_remove (& (thread-> tlist )); // remove the thread from the recycle linked list/* invoke thread cleanup */if (thread-> cleanup! = RT_NULL) // execute the Destructor thread-> cleanup (thread);/* if it's a system object, not delete it */if (rt_object_is_systemobject (rt_object_t) thread) = RT_TRUE) // if it is a system thread {/* enable interrupt */rt_hw_interrupt_enable (lock); // open interrupt return ;}} else {/* enable interrupt */rt_hw_interrupt_enable (lock); // enable/* may the defunct thread list is removed by others, just return */return ;} /* enable interrupt */rt _ Hw_interrupt_enable (lock); // open interrupt # ifdef RT_USING_HEAP # if defined (RT_USING_MODULE) & defined (RT_USING_SLAB) /* the thread belongs to an application module */if (thread-> flags & RT_OBJECT_FLAG_MODULE) rt_module_free (rt_module_t) thread-> module_id, thread-> stack_addr ); // The Memory occupied by the recycle module else # endif/* release thread's stack */rt_free (thread-> stack_addr); // reclaim the thread stack/* delete thread object */rt_object_delete (Rt_object_t) thread); // reclaim memory occupied by kernel objects # endif # ifdef RT_USING_MODULE if (module! = RT_NULL) {extern rt_err_t rt_module_destroy (rt_module_t module);/* if sub thread list and main thread are all empty */if (module-> module_thread = RT_NULL) & rt_list_isempty (& module-> module_object [RT_Object_Class_Thread]. object_list) {module-> nref --;}/* destroy module */if (module-> nref = 0) rt_module_destroy (module ); // destroy module} # endif} according to the above Code, a large part of the work of Idle threads is to recycle threads. So how did these threads come from? In fact, in the previous article, callback (delete thread in section 3.2 is similar) [cpp] //... if (thread-> cleanup! = RT_NULL) // if there is a thread destructor {/* disable interrupt */lock = rt_hw_interrupt_disable (); // Guanzhong disconnection/* insert to defunct thread list * // The rt_thread_defunct linked list will be processed by Idle threads in rt_list_insert_after (& rt_thread_defunct, & (thread-> tlist); // Add the thread to the rt_thread_defunct linked list/* enable interrupt */rt_hw_interrupt_enable (lock); // open interrupt }//... it can be seen that when a thread is detached or deleted, it will be added to the recycle linked list rt_thread_defunct. This linked list is in scheduler. the c source file is defined to save the thread to be recycled.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.