由于系統(tǒng)其它地方都會(huì)涉及到內(nèi)核對(duì)象斋枢,所以就先看看該部分的代碼
系統(tǒng)版本: V3.1.3
RTT的內(nèi)核管理這部分就我理解實(shí)際上是一個(gè)大容器椎麦,里面有很多小容器窍奋,每個(gè)小容器代表一個(gè)類別芭梯,例如線程茅坛、事件等音半。并且小容器是一套鏈表,串上了該分類的所有對(duì)象贡蓖。外部則留出增刪查的接口曹鸠。
這里引用一張官方文檔的圖
該文章涉及到兩個(gè)文件,rtdef.h中包含了小容器結(jié)構(gòu)體斥铺、類別彻桃、各個(gè)列表的對(duì)象結(jié)構(gòu)體相關(guān)定義。object.c則包含大容器晾蜘、增刪查的接口函數(shù)邻眷。接下來從代碼來看看各個(gè)部分。
內(nèi)核對(duì)象類別
對(duì)象有: 線程剔交、信號(hào)量肆饶、互斥量、事件岖常、郵箱驯镊、消息隊(duì)列、內(nèi)存堆竭鞍、內(nèi)存池板惑、設(shè)備驅(qū)動(dòng)、時(shí)鐘笼蛛、模塊洒放、未知對(duì)象蛉鹿。
如果去找相關(guān)定義的話滨砍,其實(shí)會(huì)找到兩處,其一是在rtdef.h的369行
enum rt_object_class_type
{
RT_Object_Class_Null = 0, /**< The object is not used. */
RT_Object_Class_Thread, /**< The object is a thread. */
RT_Object_Class_Semaphore, /**< The object is a semaphore. */
RT_Object_Class_Mutex, /**< The object is a mutex. */
RT_Object_Class_Event, /**< The object is a event. */
RT_Object_Class_MailBox, /**< The object is a mail box. */
RT_Object_Class_MessageQueue, /**< The object is a message queue. */
RT_Object_Class_MemHeap, /**< The object is a memory heap */
RT_Object_Class_MemPool, /**< The object is a memory pool. */
RT_Object_Class_Device, /**< The object is a device */
RT_Object_Class_Timer, /**< The object is a timer. */
RT_Object_Class_Module, /**< The object is a module. */
RT_Object_Class_Unknown, /**< The object is unknown. */
RT_Object_Class_Static = 0x80 /**< The object is a static object. */
};
該處定義作用于小容器妖异,也就是在區(qū)分類別是后真正使用的類型惋戏,注意其中的RT_Object_Class_Static定義,該定義是用于和上列定義相與他膳,使其變成靜態(tài)標(biāo)識(shí)响逢。
還有一處類似定義是在object.c中:
enum rt_object_info_type
{
RT_Object_Info_Thread = 0, /**< The object is a thread. */
#ifdef RT_USING_SEMAPHORE
RT_Object_Info_Semaphore, /**< The object is a semaphore. */
#endif
#ifdef RT_USING_MUTEX
RT_Object_Info_Mutex, /**< The object is a mutex. */
#endif
#ifdef RT_USING_EVENT
RT_Object_Info_Event, /**< The object is a event. */
#endif
#ifdef RT_USING_MAILBOX
RT_Object_Info_MailBox, /**< The object is a mail box. */
#endif
#ifdef RT_USING_MESSAGEQUEUE
RT_Object_Info_MessageQueue, /**< The object is a message queue. */
#endif
#ifdef RT_USING_MEMHEAP
RT_Object_Info_MemHeap, /**< The object is a memory heap */
#endif
#ifdef RT_USING_MEMPOOL
RT_Object_Info_MemPool, /**< The object is a memory pool. */
#endif
#ifdef RT_USING_DEVICE
RT_Object_Info_Device, /**< The object is a device */
#endif
RT_Object_Info_Timer, /**< The object is a timer. */
#ifdef RT_USING_MODULE
RT_Object_Info_Module, /**< The object is a module. */
#endif
RT_Object_Info_Unknown, /**< The object is unknown. */
};
該處主要是用于創(chuàng)建內(nèi)核對(duì)象的大容器,所以不包含除實(shí)際對(duì)象外的其他定義棕孙。
內(nèi)核對(duì)象
/**
* Base structure of Kernel object
*/
struct rt_object
{
char name[RT_NAME_MAX]; /*名稱*/
rt_uint8_t type; /*類型 */
rt_uint8_t flag; /*參數(shù) */
#ifdef RT_USING_MODULE
void *module_id; /**< id of application module */
#endif
rt_list_t list; /**< list node of kernel object */
};
內(nèi)核對(duì)象即使該管理系統(tǒng)中的最小單位舔亭。也就是小容器中鏈表串聯(lián)的對(duì)象些膨。
小容器
其實(shí)也可以理解成火車頭,屁股后面跟了一大堆該類別的對(duì)象钦铺。
struct rt_object_information
{
enum rt_object_class_type type; /**< object class type */
rt_list_t object_list; /**< object list */
rt_size_t object_size; /**< object size */
};
大容器
#define _OBJ_CONTAINER_LIST_INIT(c) \
{&(rt_object_container[c].object_list), &(rt_object_container[c].object_list)}
static struct rt_object_information rt_object_container[RT_Object_Info_Unknown] =
{
/* initialize object container - thread */
{RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Thread), sizeof(struct rt_thread)},
#ifdef RT_USING_SEMAPHORE
/* initialize object container - semaphore */
{RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Semaphore), sizeof(struct rt_semaphore)},
#endif
#ifdef RT_USING_MUTEX
/* initialize object container - mutex */
{RT_Object_Class_Mutex, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Mutex), sizeof(struct rt_mutex)},
#endif
#ifdef RT_USING_EVENT
/* initialize object container - event */
{RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Event), sizeof(struct rt_event)},
#endif
#ifdef RT_USING_MAILBOX
/* initialize object container - mailbox */
{RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MailBox), sizeof(struct rt_mailbox)},
#endif
#ifdef RT_USING_MESSAGEQUEUE
/* initialize object container - message queue */
{RT_Object_Class_MessageQueue, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MessageQueue), sizeof(struct rt_messagequeue)},
#endif
#ifdef RT_USING_MEMHEAP
/* initialize object container - memory heap */
{RT_Object_Class_MemHeap, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemHeap), sizeof(struct rt_memheap)},
#endif
#ifdef RT_USING_MEMPOOL
/* initialize object container - memory pool */
{RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_MemPool), sizeof(struct rt_mempool)},
#endif
#ifdef RT_USING_DEVICE
/* initialize object container - device */
{RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Device), sizeof(struct rt_device)},
#endif
/* initialize object container - timer */
{RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Timer), sizeof(struct rt_timer)},
#ifdef RT_USING_MODULE
/* initialize object container - module */
{RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Info_Module), sizeof(struct rt_dlmodule)},
#endif
};
大容器是通過數(shù)組方式建立的订雾,數(shù)組的每個(gè)對(duì)象都是一個(gè)小容器rt_object_information ,其中宏定義_OBJ_CONTAINER_LIST_INIT是用來初始化小容器的鏈表.
綜上也就是內(nèi)核對(duì)象rt_object被鏈接在小容器rt_object_information 中矛洞,然后所以小容器被包含在大容器rt_object_container的數(shù)組中洼哎。
增刪查的接口函數(shù)
- 通過類型得到小容器鏈表頭
struct rt_object_information *
rt_object_get_information(enum rt_object_class_type type)
{
int index;
for (index = 0; index < RT_Object_Info_Unknown; index ++)
if (rt_object_container[index].type == type) return &rt_object_container[index];
return RT_NULL;
}
RTM_EXPORT(rt_object_get_information);
- 通過類型和名稱查找具體的對(duì)象
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
{
struct rt_object *object = RT_NULL;
struct rt_list_node *node = RT_NULL;
struct rt_object_information *information = RT_NULL;
/* parameter check */
if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
return RT_NULL;
/* which is invoke in interrupt status */
RT_DEBUG_NOT_IN_INTERRUPT;
/* enter critical */
rt_enter_critical();
/* try to find object */
if (information == RT_NULL)
{
information = rt_object_get_information((enum rt_object_class_type)type);
RT_ASSERT(information != RT_NULL);
}
for (node = information->object_list.next;
node != &(information->object_list);
node = node->next)
{
object = rt_list_entry(node, struct rt_object, list);
if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
{
/* leave critical */
rt_exit_critical();
return object;
}
}
/* leave critical */
rt_exit_critical();
return RT_NULL;
}
由于函數(shù)中的語句都比較明顯,也就沒有添加自己的注釋沼本,接下來是增噩峦,這里有靜態(tài)和動(dòng)態(tài)兩種,前者其實(shí)就是外部傳入一個(gè)對(duì)象抽兆,將其添加到容器鏈表中识补,后者是在函數(shù)中動(dòng)態(tài)申請(qǐng)內(nèi)存,填充信息后放入鏈表郊丛。
- 靜態(tài)初始化對(duì)象并添加進(jìn)對(duì)象管理系統(tǒng)
void rt_object_init(struct rt_object *object,
enum rt_object_class_type type,
const char *name)
{
register rt_base_t temp;
struct rt_object_information *information;
#ifdef RT_USING_MODULE
struct rt_dlmodule *module = dlmodule_self();
#endif
/* get object information */
information = rt_object_get_information(type); //找到小容器
RT_ASSERT(information != RT_NULL);
/* initialize object's parameters */
/* set object type to static */
object->type = type | RT_Object_Class_Static; //類型修改為靜態(tài)
/* copy name */
rt_strncpy(object->name, name, RT_NAME_MAX);
RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));
/* lock interrupt */
temp = rt_hw_interrupt_disable();
#ifdef RT_USING_MODULE
if (module)
{
rt_list_insert_after(&(module->object_list), &(object->list));
object->module_id = (void *)module;
}
else
#endif
{
/* insert object into information object list */
rt_list_insert_after(&(information->object_list), &(object->list));//插入
}
/* unlock interrupt */
rt_hw_interrupt_enable(temp);
}
- 移除添加的靜態(tài)對(duì)象
void rt_object_detach(rt_object_t object)
{
register rt_base_t temp;
/* object check */
RT_ASSERT(object != RT_NULL);
RT_OBJECT_HOOK_CALL(rt_object_detach_hook, (object));
/* reset object type */
object->type = 0;
/* lock interrupt */
temp = rt_hw_interrupt_disable();
/* remove from old list */
rt_list_remove(&(object->list));
/* unlock interrupt */
rt_hw_interrupt_enable(temp);
}
大致就是這么了李请。