RTT筆記-內(nèi)核對(duì)象管理

由于系統(tǒng)其它地方都會(huì)涉及到內(nèi)核對(duì)象斋枢,所以就先看看該部分的代碼
系統(tǒng)版本: V3.1.3


RTT的內(nèi)核管理這部分就我理解實(shí)際上是一個(gè)大容器椎麦,里面有很多小容器窍奋,每個(gè)小容器代表一個(gè)類別芭梯,例如線程茅坛、事件等音半。并且小容器是一套鏈表,串上了該分類的所有對(duì)象贡蓖。外部則留出增刪查的接口曹鸠。
這里引用一張官方文檔的圖


image.png

該文章涉及到兩個(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);
}

大致就是這么了李请。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市厉熟,隨后出現(xiàn)的幾起案子导盅,更是在濱河造成了極大的恐慌,老刑警劉巖揍瑟,帶你破解...
    沈念sama閱讀 206,214評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件白翻,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡绢片,警方通過查閱死者的電腦和手機(jī)滤馍,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來底循,“玉大人巢株,你說我怎么就攤上這事∥醯樱” “怎么了阁苞?”我有些...
    開封第一講書人閱讀 152,543評(píng)論 0 341
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)祠挫。 經(jīng)常有香客問我那槽,道長(zhǎng),這世上最難降的妖魔是什么等舔? 我笑而不...
    開封第一講書人閱讀 55,221評(píng)論 1 279
  • 正文 為了忘掉前任骚灸,我火速辦了婚禮,結(jié)果婚禮上慌植,老公的妹妹穿的比我還像新娘甚牲。我一直安慰自己义郑,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,224評(píng)論 5 371
  • 文/花漫 我一把揭開白布丈钙。 她就那樣靜靜地躺著魔慷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪著恩。 梳的紋絲不亂的頭發(fā)上院尔,一...
    開封第一講書人閱讀 49,007評(píng)論 1 284
  • 那天,我揣著相機(jī)與錄音喉誊,去河邊找鬼邀摆。 笑死,一個(gè)胖子當(dāng)著我的面吹牛伍茄,可吹牛的內(nèi)容都是我干的栋盹。 我是一名探鬼主播,決...
    沈念sama閱讀 38,313評(píng)論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼敷矫,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼例获!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起曹仗,我...
    開封第一講書人閱讀 36,956評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤榨汤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后怎茫,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體收壕,經(jīng)...
    沈念sama閱讀 43,441評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,925評(píng)論 2 323
  • 正文 我和宋清朗相戀三年轨蛤,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蜜宪。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,018評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡祥山,死狀恐怖圃验,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情缝呕,我是刑警寧澤澳窑,帶...
    沈念sama閱讀 33,685評(píng)論 4 322
  • 正文 年R本政府宣布,位于F島的核電站岳颇,受9級(jí)特大地震影響照捡,放射性物質(zhì)發(fā)生泄漏颅湘。R本人自食惡果不足惜话侧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,234評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望闯参。 院中可真熱鬧瞻鹏,春花似錦悲立、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至赫悄,卻和暖如春原献,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背埂淮。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工姑隅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人倔撞。 一個(gè)月前我還...
    沈念sama閱讀 45,467評(píng)論 2 352
  • 正文 我出身青樓讲仰,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親痪蝇。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鄙陡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,762評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容