溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

RT-Thread中的內(nèi)核對象操作API怎么理解

發(fā)布時間:2021-12-17 15:28:43 來源:億速云 閱讀:185 作者:柒染 欄目:互聯(lián)網(wǎng)科技

這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)RT-Thread中的內(nèi)核對象操作API怎么理解,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

背景

  • 目的還是學(xué)習(xí)并熟悉RT-Thread 操作系統(tǒng)。

  • 從最簡單的對象管理切入

  • 了解操作系統(tǒng)最基本的組成單位:Object

內(nèi)核對象API

內(nèi)核對象的主要操作方法:內(nèi)核文件:object.c中實現(xiàn)

RT-Thread中的內(nèi)核對象操作API怎么理解

知識點

查看內(nèi)核文件:object.c,發(fā)現(xiàn)的主要的幾個知識點

2021-01-24_215932.png

驗證與測試

  • 光看內(nèi)核代碼,不如敲一敲(抄一下)。

  • 可以使用模擬器,寫幾個測試函數(shù),看看對象操作的流程。

測試用例如下:

/* RT-Thread 內(nèi)核對象學(xué)習(xí) */
#include <rtthread.h>

struct _obj_type
{
    enum rt_object_class_type type;
    const char* name;
};

/* 靜態(tài)的對象定義 */
static struct rt_object _obj[] = { 0 };

/* 測試用,線程對象 */
static const struct _obj_type _obj_tbl[] =
{
    { RT_Object_Class_Thread, "th_01" },
    { RT_Object_Class_Thread, "th_02" },
    { RT_Object_Class_Thread, "th_03" },
    { RT_Object_Class_Thread, "th_04" },
    { RT_Object_Class_Thread, "th_05" },
};

#define OBJ_TEST_TBL_SIZE       (sizeof(_obj_tbl) / sizeof(_obj_tbl[0]))

/* 靜態(tài)初始化對象 */
void obj_test_init(void)
{
    rt_uint8_t index = 0;
    rt_uint8_t obj_size = 0;

    for (index = 0; index < OBJ_TEST_TBL_SIZE; index++)
    {
        rt_object_init(&_obj[index], _obj_tbl[index].type, _obj_tbl[index].name);
    }
}

/* 動態(tài)創(chuàng)建對象 obj_test_create thread1 */
void obj_test_create(uint8_t argc, char** argv)
{
    struct rt_object* obj = RT_NULL;

    if (argc >= 2)
    {
        rt_kprintf(" obj_name=%s\n", argv[1]);
    }

    obj = rt_object_find(argv[1], RT_Object_Class_Thread);
    if (obj != RT_NULL)
    {
        rt_kprintf("obj_name=%s, exist!!\n", obj->name);
        return;
    }
    else
    {
        rt_object_allocate(RT_Object_Class_Thread, argv[1]);
        rt_kprintf("create obj_name=%s\n", argv[1]);
    }
}

/* 對象的打印 */
void obj_test_dump(void)
{
    rt_uint8_t index = 0;
    rt_uint8_t obj_size = 0;
    struct rt_object* obj_pointers[OBJ_TEST_TBL_SIZE + 10] = { 0 };

    obj_size = rt_object_get_pointers(RT_Object_Class_Thread, obj_pointers, sizeof(obj_pointers));
    rt_kprintf("object init : object size=%d\n", obj_size);

    rt_kprintf("| index |     name     |  flag  |  type  |\n");
    rt_kprintf("+-------+--------------+--------+--------+\n");
    for (index = 0; index < obj_size; index++)
    {
        if (obj_pointers[index] == RT_NULL)
        {
            break;
        }
        rt_kprintf("|  %03d  |  %10s  |   %02d   |  0x%02x  |\n", index,
            obj_pointers[index]->name, obj_pointers[index]->flag,
            obj_pointers[index]->type);
    }
    rt_kprintf("+-------+--------------+--------+--------+\n");
}

/* 查找線程對象 */
void obj_test_find(uint8_t argc, char** argv)
{
    struct rt_object* obj = RT_NULL;

    if (argc >= 2)
    {
        rt_kprintf(" obj_name=%s\n", argv[1]);
    }

    obj = rt_object_find(argv[1], RT_Object_Class_Thread);
    if (obj != RT_NULL)
    {
        rt_kprintf("find obj_name=%s\n", obj->name);
    }
    else
    {
        rt_kprintf("not find obj_name=%s\n", argv[1]);
    }
}

/* 靜態(tài)對象 detach */
void obj_test_detach(uint8_t argc, char** argv)
{
    struct rt_object* obj = RT_NULL;

    if (argc >= 2)
    {
        rt_kprintf(" obj_name=%s\n", argv[1]);
    }

    obj = rt_object_find(argv[1], RT_Object_Class_Thread);
    if (obj != RT_NULL)
    {
        rt_kprintf("find obj_name=%s\n", obj->name);
        rt_object_detach(obj);
        rt_kprintf("detach obj_name=%s\n", obj->name);
    }
    else
    {
        rt_kprintf("not find obj_name=%s\n", argv[1]);
    }
}

/* 動態(tài)對象 delete */
void obj_test_delete(uint8_t argc, char** argv)
{
    struct rt_object* obj = RT_NULL;

    if (argc >= 2)
    {
        rt_kprintf(" obj_name=%s\n", argv[1]);
    }

    obj = rt_object_find(argv[1], RT_Object_Class_Thread);
    if (obj != RT_NULL)
    {
        rt_kprintf("find obj_name=%s\n", obj->name);
        rt_object_delete(obj);
        rt_kprintf("delete obj_name=%s\n", obj->name);
    }
    else
    {
        rt_kprintf("not find obj_name=%s\n", argv[1]);
    }
}

/* 導(dǎo)出命令 */
MSH_CMD_EXPORT(obj_test_init, object init test);
MSH_CMD_EXPORT(obj_test_create, obj create test);
MSH_CMD_EXPORT(obj_test_dump, object test dump);
MSH_CMD_EXPORT(obj_test_find, object test find);
MSH_CMD_EXPORT(obj_test_detach, object test detach);
MSH_CMD_EXPORT(obj_test_delete, object test del);

學(xué)習(xí)總結(jié)

總結(jié)一

  • 發(fā)現(xiàn):tshell 是動態(tài)創(chuàng)建的線程

  • 發(fā)現(xiàn):tidle 是靜態(tài)的線程

msh />obj_test_dump
object init : object size=2
| index |     name     |  flag  |  type  |
+-------+--------------+--------+--------+
|  000  |      tshell  |   00   |  0x01  |
|  001  |      tidle0  |   00   |  0x81  |
+-------+--------------+--------+--------+
msh />

總結(jié)二

  • 動態(tài)對象,創(chuàng)建后,內(nèi)存占用增加。

  • 動態(tài)對象,刪除后,內(nèi)存占用恢復(fù)

msh />free
total memory: 8388580
used memory : 5164  /* 【5164】 內(nèi)存原有大小 */
maximum allocated memory: 7336
msh />
msh />obj
obj_test_init
obj_test_create
obj_test_dump
obj_test_find
obj_test_detach
obj_test_delete
msh />obj_test_cre
obj_test_create
msh />obj_test_create hello
 obj_name=hello
create obj_name=hello
msh />
msh />fre
free
msh />free
total memory: 8388580
used memory : 5304   /* 【5304】 內(nèi)存占用 */
maximum allocated memory: 7336
msh />
msh />obj_test_delete hello
 obj_name=hello
find obj_name=hello
delete obj_name=hello
msh />free
total memory: 8388580
used memory : 5164  /* 【5304】,內(nèi)存占用恢復(fù) */
maximum allocated memory: 7336
msh />

總結(jié)三

  • 靜態(tài)初始化的對象,detach(剔除對象管理)后,內(nèi)存占用不變。

msh />obj_test_init
msh />ob
obj_test_init
obj_test_create
obj_test_dump
obj_test_find
obj_test_detach
obj_test_delete
msh />obj_test_du
obj_test_dump
msh />obj_test_dump
object init : object size=7
| index |     name     |  flag  |  type  |
+-------+--------------+--------+--------+
|  000  |       th_05  |   00   |  0x81  |
|  001  |       th_04  |   00   |  0x81  |
|  002  |       th_03  |   00   |  0x81  |
|  003  |       th_02  |   00   |  0x81  |
|  004  |       th_01  |   00   |  0x81  |
|  005  |      tshell  |   00   |  0x01  |
|  006  |      tidle0  |   00   |  0x81  |
+-------+--------------+--------+--------+
msh />free
total memory: 8388580
used memory : 5164
maximum allocated memory: 7336
msh />
msh />obj
obj_test_init
obj_test_create
obj_test_dump
obj_test_find
obj_test_detach
obj_test_delete
msh />obj_test_deta
obj_test_detach
msh />obj_test_detach th_04
 obj_name=th_04
find obj_name=th_04
detach obj_name=th_04
msh />obj_test_du
obj_test_dump
msh />obj_test_dump
object init : object size=6
| index |     name     |  flag  |  type  |
+-------+--------------+--------+--------+
|  000  |       th_05  |   00   |  0x81  |
|  001  |       th_03  |   00   |  0x81  |
|  002  |       th_02  |   00   |  0x81  |
|  003  |       th_01  |   00   |  0x81  |
|  004  |      tshell  |   00   |  0x01  |
|  005  |      tidle0  |   00   |  0x81  |
+-------+--------------+--------+--------+
msh />
msh />free
total memory: 8388580
used memory : 5164
maximum allocated memory: 7336

上述就是小編為大家分享的RT-Thread中的內(nèi)核對象操作API怎么理解了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI