rt-thread的內核對象管理系統分析

rt-thread採用內核對象管理系統來訪問和管理所有內核對象.首先來看看rt-thread的內核對象是如何定義的:

1 數據結構

1.1 對象控制塊

在include/rtdef.h頭文件中可以找到內核對象有結構定義:

[cpp] view plain copy
  1. /** 
  2.  * Base structure of Kernel object 
  3.  */  
  4. struct rt_object  
  5. {  
  6.     char       name[RT_NAME_MAX];//名稱  
  7.     rt_uint8_t type;//內核對象類型   
  8.     rt_uint8_t flag;//內核對象標誌          
  9.   
  10. #ifdef RT_USING_MODULE  
  11.     void      *module_id;//模塊ID  
  12. #endif  
  13.     rt_list_t  list;//內核對象鏈表節點  
  14. };  
  15. typedef struct rt_object *rt_object_t;  


這裏需要注意地是,上述內核對象控制塊包含了一rt_list_t類型的成員list,這個是一鏈表節點,便於將此內核對象加入到一鏈表中,其結構如下定義:

[cpp] view plain copy
  1. struct rt_list_node  
  2. {  
  3.     struct rt_list_node *next; //指向下一節點  
  4.     struct rt_list_node *prev; //指向前一節點  
  5. };  
  6. typedef struct rt_list_node rt_list_t;  

 

另內核對象類型取值有如下類型:

[cpp] view plain copy
  1. /** 
  2.  *  The object type can be one of the follows with specific 
  3.  *  macros enabled: 
  4.  *  - Thread 
  5.  *  - Semaphore 
  6.  *  - Mutex 
  7.  *  - Event 
  8.  *  - MailBox 
  9.  *  - MessageQueue 
  10.  *  - MemHeap 
  11.  *  - MemPool 
  12.  *  - Device 
  13.  *  - Timer 
  14.  *  - Module 
  15.  *  - Unknown 
  16.  *  - Static 
  17.  */  
  18. enum rt_object_class_type  
  19. {  
  20.     RT_Object_Class_Thread = 0, //線程  
  21. #ifdef RT_USING_SEMAPHORE  
  22.     RT_Object_Class_Semaphore, //信號量  
  23. #endif  
  24. #ifdef RT_USING_MUTEX  
  25.     RT_Object_Class_Mutex,   //互斥鎖  
  26. #endif  
  27. #ifdef RT_USING_EVENT  
  28.     RT_Object_Class_Event,  //事件  
  29. #endif  
  30. #ifdef RT_USING_MAILBOX  
  31.     RT_Object_Class_MailBox,  //郵箱  
  32. #endif  
  33. #ifdef RT_USING_MESSAGEQUEUE  
  34.     RT_Object_Class_MessageQueue,  //消息隊列  
  35. #endif  
  36. #ifdef RT_USING_MEMHEAP  
  37.     RT_Object_Class_MemHeap,      //內存堆  
  38. #endif  
  39. #ifdef RT_USING_MEMPOOL  
  40.     RT_Object_Class_MemPool,     //內存池  
  41. #endif  
  42. #ifdef RT_USING_DEVICE  
  43.     RT_Object_Class_Device,     //設備驅動  
  44. #endif  
  45.     RT_Object_Class_Timer,      //時鐘  
  46. #ifdef RT_USING_MODULE  
  47.     RT_Object_Class_Module,     //模塊  
  48. #endif  
  49.     RT_Object_Class_Unknown,      //未知內核對象類型  
  50.     RT_Object_Class_Static = 0x80  //rt-thread以此位標誌是否爲系統內核對象  
  51. };  

需要注意的是,rt-thread將內核對象的type的最高位若爲1,則表示此內核對象爲系統內核對象,否則非系統內核對象.


 1.2 內核對象容器

RTT使用內核對象容器來管理同一類型的內核對象,並將其放入同一鏈表中,便於訪問.內核對象信息的結構如下定義:

[cpp] view plain copy
  1. /** 
  2.  * The information of the kernel object 
  3.  */  
  4. struct rt_object_information  
  5. {  
  6.     enum rt_object_class_type type;          //內核對象類型  
  7.     rt_list_t                 object_list;   //內核對象鏈表  
  8.     rt_size_t                 object_size;   //內核對象所佔的大小  
  9. };  


 1.3 內核對象管理系統

RTT中,每一類型的內核對象都會有一內核對象容器來包容,這個類型的內核對象容器實際上是用一鏈表(見1.2節所示的內核對象容器結構定義),這個鏈表將所有相同類型的內核對象鏈接起來.由於每一類型都對應着有一個這樣的內核對象容器來管理,那麼所有內核對象容器整體就叫做內核對象管理系統.

如下圖示:


RTT中,內核對象管理系統是用一個rt_object_information數組來實現的,如下:

[cpp] view plain copy
  1. #define _OBJ_CONTAINER_LIST_INIT(c)\//內核對象容器的鏈表初始化,這裏用一個宏來定義,鏈表的前一節點和後一節點在初始化時都指向本身所在地址  
  2.     {&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}  
  3.   
  4. //內核對象管理系統,這裏用rt_object_information數組來實現  
  5. struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =  
  6. {  
  7.     /* initialize object container - thread */)},//線程對象信息  
  8.     {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Thread), sizeof(struct rt_thread#ifdef RT_USING_SEMAPHORE  
  9.     /* initialize object container - semaphore *///信號量對象信息  
  10.     {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Semaphore), sizeof(struct rt_semaphore)},  
  11. #endif  
  12. #ifdef RT_USING_MUTEX  
  13.     /* initialize object container - mutex *///互斥鎖對象信息  
  14.     {RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Mutex), sizeof(struct rt_mutex)},  
  15. #endif  
  16. #ifdef RT_USING_EVENT  
  17.     /* initialize object container - event *///事件對象信息  
  18.     {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Event), sizeof(struct rt_event)},  
  19. #endif  
  20. #ifdef RT_USING_MAILBOX  
  21.     /* initialize object container - mailbox *///郵箱對象信息  
  22.     {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MailBox), sizeof(struct rt_mailbox)},  
  23. #endif  
  24. #ifdef RT_USING_MESSAGEQUEUE  
  25.     /* initialize object container - message queue *///消息隊列對象信息  
  26.     {RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MessageQueue), sizeof(struct rt_messagequeue)},  
  27. #endif  
  28. #ifdef RT_USING_MEMHEAP  
  29.     /* initialize object container - memory heap *///內存堆對象信息  
  30.     {RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemHeap), sizeof(struct rt_memheap)},  
  31. #endif  
  32. #ifdef RT_USING_MEMPOOL  
  33.     /* initialize object container - memory pool *///內存池對象信息  
  34.     {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemPool), sizeof(struct rt_mempool)},  
  35. #endif  
  36. #ifdef RT_USING_DEVICE  
  37.     /* initialize object container - device *///設備驅動對象信息  
  38.     {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Device), sizeof(struct rt_device)},  
  39. #endif  
  40.     /* initialize object container - timer *///時鐘對象信息  
  41.     {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Timer), sizeof(struct rt_timer)},  
  42. #ifdef RT_USING_MODULE  
  43.     /* initialize object container - module *///模塊對象信息  
  44.     {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Module), sizeof(struct rt_module)},  
  45. #endif  
  46. };  


 2 內核對象接口

2.1 內核對象初始化

RTT提供靜態和動態兩種初始化接口,如下:

靜態初始化是將一個已經存在的且佔有內存空間的對象初始化,它的接口如下:

[cpp] view plain copy
  1. /** 
  2.  * This function will initialize an object and add it to object system 
  3.  * management. 
  4.  * 
  5.  * @param object the specified object to be initialized. 
  6.  * @param type the object type. 
  7.  * @param name the object name. In system, the object's name must be unique. 
  8.  */  
  9. void rt_object_init(struct rt_object         *object,//指向已存在的對象指針  
  10.                     enum rt_object_class_type type,  //對象的類型  
  11.                     const char               *name)  //對象的名字字符串  
  12. {  
  13.     register rt_base_t temp;  
  14.     struct rt_object_information *information;      //對象容器  
  15.   
  16. #ifdef RT_USING_MODULE //如果使用了模塊,那麼對象容器指向本線程所包含的對象窗口,否則指向全局對象管理系統中對應的容器  
  17.     /* get module object information */  
  18.     information = (rt_module_self() != RT_NULL) ?   
  19.         &rt_module_self()->module_object[type] : &rt_object_container[type];  
  20. #else  
  21.     /* get object information */  
  22.     information = &rt_object_container[type];  
  23. #endif  
  24.   
  25.     /* initialize object's parameters */  
  26.   
  27.     /* set object type to static */  
  28.     object->type = type | RT_Object_Class_Static;//設置系統對象標誌  
  29.   
  30.     /* copy name */  
  31.     rt_strncpy(object->name, name, RT_NAME_MAX);//給名字賦值  
  32.   
  33.     RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));//使用鉤子函數  
  34.   
  35.     /* lock interrupt */  
  36.     temp = rt_hw_interrupt_disable();//關中斷  
  37.   
  38.     /* insert object into information object list */  
  39.     rt_list_insert_after(&(information->object_list), &(object->list));//將初始化的內核對象加入到對應容器中  
  40.   
  41.     /* unlock interrupt */  
  42.     rt_hw_interrupt_enable(temp);//開中斷  
  43. }  

動態初始化是指對象原本並不存在,在不內存中,需要動態爲其分配內存,其接口如下:


 

[cpp] view plain copy
  1. /** 
  2.  * This function will allocate an object from object system 
  3.  * 
  4.  * @param type the type of object 
  5.  * @param name the object name. In system, the object's name must be unique. 
  6.  * 
  7.  * @return object 
  8.  */  
  9. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)//動態初始化接口只需要傳入名字和類型  
  10. {  
  11.     struct rt_object *object;  
  12.     register rt_base_t temp;  
  13.     struct rt_object_information *information;//對象容器  
  14.   
  15.     RT_DEBUG_NOT_IN_INTERRUPT;  
  16.   
  17. #ifdef RT_USING_MODULE//同上面那個接口一樣,獲取對象容器  
  18.     /* 
  19.      * get module object information, 
  20.      * module object should be managed by kernel object container 
  21.      */  
  22.     information = (rt_module_self() != RT_NULL && (type != RT_Object_Class_Module)) ?  
  23.                   &rt_module_self()->module_object[type] : &rt_object_container[type];  
  24. #else  
  25.     /* get object information */  
  26.     information = &rt_object_container[type];  
  27. #endif  
  28.   
  29.     object = (struct rt_object *)rt_malloc(information->object_size);//爲對象動態分配內存空間  
  30.     if (object == RT_NULL)  
  31.     {  
  32.         /* no memory can be allocated */  
  33.         return RT_NULL;  
  34.     }  
  35.       
  36.     /* initialize object's parameters */  
  37.   
  38.     /* set object type */  
  39.     object->type = type;//設置類型  
  40.   
  41.     /* set object flag */  
  42.     object->flag = 0;//設置標誌爲0  
  43.   
  44. #ifdef RT_USING_MODULE  
  45.     if (rt_module_self() != RT_NULL)  
  46.     {  
  47.         object->flag |= RT_OBJECT_FLAG_MODULE;//如果使用了模塊功能,則將flag標誌設置爲模塊標誌  
  48.     }  
  49.     object->module_id = (void *)rt_module_self();//設置模塊ID  
  50. #endif  
  51.   
  52.     /* copy name */  
  53.     rt_strncpy(object->name, name, RT_NAME_MAX);//給名稱賦值  
  54.   
  55.     RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));//使用鉤子函數  
  56.   
  57.     /* lock interrupt */  
  58.     temp = rt_hw_interrupt_disable();//關中斷  
  59.   
  60.     /* insert object into information object list */  
  61.     rt_list_insert_after(&(information->object_list), &(object->list));//將此對象加入對應容器  
  62.   
  63.     /* unlock interrupt */  
  64.     rt_hw_interrupt_enable(temp);//關中斷  
  65.   
  66.     /* return object */  
  67.     return object;  
  68. }  


 2.2 脫離或刪除對象

如果對象是靜態初始化的,那麼對應的是脫離,如果是動態初始化的,則是刪除.

脫離接口如下:

[cpp] view plain copy
  1. /** 
  2.  * This function will detach a static object from object system, 
  3.  * and the memory of static object is not freed. 
  4.  * 
  5.  * @param object the specified object to be detached. 
  6.  */  
  7. void rt_object_detach(rt_object_t object)  
  8. {  
  9.     register rt_base_t temp;  
  10.   
  11.     /* object check */  
  12.     RT_ASSERT(object != RT_NULL);  
  13.   
  14.     RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));//使用鉤子函數  
  15.     /* lock interrupt */  
  16.     temp = rt_hw_interrupt_disable();//關中斷  
  17.   
  18.     /* remove from old list */  
  19.     rt_list_remove(&(object->list));//從窗口中移除  
  20.   
  21.     /* unlock interrupt */  
  22.     rt_hw_interrupt_enable(temp);//開中斷  
  23. }  

刪除接口如下:


 

[cpp] view plain copy
  1. /** 
  2.  * This function will delete an object and release object memory. 
  3.  * 
  4.  * @param object the specified object to be deleted. 
  5.  */  
  6. void rt_object_delete(rt_object_t object)  
  7. {  
  8.     register rt_base_t temp;  
  9.   
  10.     /* object check */  
  11.     RT_ASSERT(object != RT_NULL);  
  12.     RT_ASSERT(!(object->type & RT_Object_Class_Static));//刪除的對象必須是非系統對象  
  13.     RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));//使用鉤子函數  
  14.   
  15.     /* lock interrupt */  
  16.     temp = rt_hw_interrupt_disable();//關中斷  
  17.   
  18.     /* remove from old list */  
  19.     rt_list_remove(&(object->list));//從對應的容器中移除  
  20.   
  21.     /* unlock interrupt */  
  22.     rt_hw_interrupt_enable(temp);//開中斷  
  23.   
  24. #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)//如果使用了模塊功能且採用的是SLAB動態內存管理模式  
  25.     if (object->flag & RT_OBJECT_FLAG_MODULE)   
  26.         rt_module_free((rt_module_t)object->module_id, object);//釋放模塊ID所佔空間  
  27.     else  
  28. #endif  
  29.   
  30.     /* free the memory of object */  
  31.     rt_free(object);//釋放內核對象所佔空間  
  32. }  


 其中rt_list_remove會自動找到對象的前一節點和後一節點,然後刪除本身節點.

2.3 判斷是否爲系統內核對象

[cpp] view plain copy
  1. /** 
  2.  * This function will judge the object is system object or not. 
  3.  * Normally, the system object is a static object and the type 
  4.  * of object set to RT_Object_Class_Static. 
  5.  * 
  6.  * @param object the specified object to be judged. 
  7.  * 
  8.  * @return RT_TRUE if a system object, RT_FALSE for others. 
  9.  */  
  10. rt_bool_t rt_object_is_systemobject(rt_object_t object)  
  11. {  
  12.     /* object check */  
  13.     RT_ASSERT(object != RT_NULL);  
  14.   
  15.     if (object->type & RT_Object_Class_Static)//RTT是通過內核對象的type的最高位是否爲1來判斷此對象是否爲系統內核對象的  
  16.         return RT_TRUE;  
  17.   
  18.     return RT_FALSE;  
  19. }  

2.4  查找內核對象


 

[cpp] view plain copy
  1. /** 
  2.  * This function will find specified name object from object 
  3.  * container. 
  4.  * 
  5.  * @param name the specified name of object. 
  6.  * @param type the type of object 
  7.  * 
  8.  * @return the found object or RT_NULL if there is no this object 
  9.  * in object container. 
  10.  * 
  11.  * @note this function shall not be invoked in interrupt status. 
  12.  */  
  13. rt_object_t rt_object_find(const char *name, rt_uint8_t type)  
  14. {  
  15.     struct rt_object *object;  
  16.     struct rt_list_node *node;  
  17.     struct rt_object_information *information;  
  18.     extern volatile rt_uint8_t rt_interrupt_nest;  
  19.   
  20.     /* parameter check *///輸入系統檢查  
  21.     if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))  
  22.         return RT_NULL;  
  23.   
  24.     /* which is invoke in interrupt status */  
  25.     if (rt_interrupt_nest != 0)//確保當前沒有中斷嵌套  
  26.         RT_ASSERT(0);  
  27.   
  28.     /* enter critical */  
  29.     rt_enter_critical();//進入臨界區  
  30.   
  31.     /* try to find object */  
  32.     information = &rt_object_container[type];//獲取對應的對象容器  
  33.     for (node  = information->object_list.next;//開始通過名字來掃描內核對象  
  34.          node != &(information->object_list);  
  35.          node  = node->next)  
  36.     {  
  37.         object = rt_list_entry(node, struct rt_object, list);//獲取內核對象  
  38.         if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)//判斷名字是否相符  
  39.         {  
  40.             /* leave critical */  
  41.             rt_exit_critical();//退出臨界區  
  42.   
  43.             return object;  
  44.         }  
  45.     }  
  46.   
  47.     /* leave critical */  
  48.     rt_exit_critical();//退出臨界區  
  49.   
  50.     return RT_NULL;  
  51. }  

 

3 內核對象系統初始化

[cpp] view plain copy
  1. /** 
  2.  * @ingroup SystemInit 
  3.  * 
  4.  * This function will initialize system object management. 
  5.  * 
  6.  * @deprecated since 0.3.0, this function does not need to be invoked 
  7.  * in the system initialization. 
  8.  */  
  9. void rt_system_object_init(void)  
  10. {  
  11. }  


從源代碼可以看出,自從0.3.0以後,RTT就已經沒有必須再使用此接口來對內核對象初始化了,因此,此函數是空的,但在系統初始化時還會保留調用些函數.

發佈了1 篇原創文章 · 獲贊 48 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章