背景
内核对象API
内核对象的主要操作方法:内核文件:object.c中实现
知识点
查看内核文件:object.c,发现的主要的几个知识点
验证与测试
- 光看内核代码,不如敲一敲(抄一下)。
- 可以使用interwetten与威廉的赔率体系 器,写几个测试函数,看看对象操作的流程。
测试用例如下:
/* RT-Thread 内核对象学习 */
#include < rtthread.h >
struct _obj_type
{
enum rt_object_class_type type;
const char* name;
};
/* 静态的对象定义 */
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]))
/* 静态初始化对象 */
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);
}
}
/* 动态创建对象 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]);
}
}
/* 静态对象 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]);
}
}
/* 动态对象 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]);
}
}
/* 导出命令 */
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);