当前位置:网站首页>RT thread analysis - object container implementation and function
RT thread analysis - object container implementation and function
2022-07-06 04:49:00 【Just think Quiet】
Catalog
2) Container definition _object_container[]
2.3 Object container link graph
3 Get object container rt_object_get_information()
4 Object initialization rt_object_init()
5 Object delete rt_object_delete()
6 Look for objects rt_object_find()
1 Preface
In kernel , All data structures are treated as one object , Include : Threads 、 Semaphore 、 The mutex 、 event 、 mailbox 、 Message queue 、 Memory heap 、 Memory pool 、 Equipment and timers . For the convenience of management , When the user or kernel creates an object , The object will be put into the container . The container is a global array in the kernel , Record the information of all objects .
2 Relevant data structure
2.1 object
1) type
enum rt_object_class_type
{
RT_Object_Class_Null = 0x00, /**< The object is not used. */
RT_Object_Class_Thread = 0x01, /**< The object is a thread. */
RT_Object_Class_Semaphore = 0x02, /**< The object is a semaphore. */
RT_Object_Class_Mutex = 0x03, /**< The object is a mutex. */
RT_Object_Class_Event = 0x04, /**< The object is a event. */
RT_Object_Class_MailBox = 0x05, /**< The object is a mail box. */
RT_Object_Class_MessageQueue = 0x06, /**< The object is a message queue. */
RT_Object_Class_MemHeap = 0x07, /**< The object is a memory heap. */
RT_Object_Class_MemPool = 0x08, /**< The object is a memory pool. */
RT_Object_Class_Device = 0x09, /**< The object is a device. */
RT_Object_Class_Timer = 0x0a, /**< The object is a timer. */
RT_Object_Class_Module = 0x0b, /**< The object is a module. */
RT_Object_Class_Unknown = 0x0c, /**< The object is unknown. */
RT_Object_Class_Static = 0x80 /**< The object is a static object. */
};2) data structure
struct rt_object
{
// Object name , Use string representation , Length from RT_NAME_MAX decision
char name[RT_NAME_MAX];
// object type
rt_uint8_t type;
// Object state
rt_uint8_t flag;
// Intrusive linked list , Link the object to the container
rt_list_t list;
};
typedef struct rt_object *rt_object_t; 2.3 Containers
The global structure array is used in the kernel (rt_object_information _object_container[]) To define the container . Each container holds objects of the same type , Save its information , Use a linked list to link all objects for easy management .
1) data structure
struct rt_object_information
{
// The object type under the container
enum rt_object_class_type type;
// Link all objects under the container
rt_list_t object_list;
// Its object size
rt_size_t object_size;
};2) Container definition _object_container[]
//2.1) Containers are represented by arrays , The following is to define the sequence numbers of different objects in the array
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. */
};
//2.2) Each container holds objects of the same type , such as _object_container[0] Are storage objects RT_Object_Class_Thread, Then use the linked list object_list Link all objects
#define _OBJ_CONTAINER_LIST_INIT(c) \
{&(_object_container[c].object_list), &(_object_container[c].object_list)}
static struct rt_object_information _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
};2.3 Object container link graph

3 Get object container rt_object_get_information()
struct rt_object_information* rt_object_get_information(enum rt_object_class_type type)
{
int index;
// Traverse container array _object_container[], Find the object container by type
for (index = 0; index < RT_Object_Info_Unknown; index ++)
if (_object_container[index].type == type)
return &_object_container[index];
return RT_NULL;
}4 Object initialization rt_object_init()
void rt_object_init(struct rt_object *object,
enum rt_object_class_type type,
const char *name)
{
//1 By object type , Find the container
information = rt_object_get_information(type);
//2 Traverse all objects under the container , If it is consistent with the object to be initialized , Enter assertion
//PS: The function of this error check ???
for (node = information->object_list.next;
node != &(information->object_list);
node = node->next)
{
struct rt_object *obj;
obj = rt_list_entry(node, struct rt_object, list);
if (obj) /* skip warning when disable debug */
{
RT_ASSERT(obj != object);
}
}
//3 Set static tags
object->type = type | RT_Object_Class_Static;
//4 Set object name
rt_strncpy(object->name, name, RT_NAME_MAX);
//5 Insert the object into the end of the container list
rt_list_insert_after(&(information->object_list), &(object->list));
}5 Object delete rt_object_delete()
void rt_object_delete(rt_object_t object)
{
//1 Reset object type
object->type = RT_Object_Class_Null;
//2 Remove objects from the container list
rt_list_remove(&(object->list));
//3 Free object memory
RT_KERNEL_FREE(object);
}6 Look for objects rt_object_find()
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
{
//1 Find the corresponding container by type
information = rt_object_get_information((enum rt_object_class_type)type);
//2 Context detection , This function is not allowed to call
RT_DEBUG_NOT_IN_INTERRUPT;
}7 summary
- Every data structure in the kernel is abstracted as an object rt_object, Include : Threads 、 Semaphore 、 The mutex 、 event 、 mailbox 、 Message queue 、 Memory heap 、 Memory pool 、 Equipment and timers .
- In actual use , Object properties can be inherited
- The kernel maintains containers using global struct arrays , Each member of the array manages all objects of the same type , It is convenient to query the information of each object at any time
边栏推荐
- word封面下划线
- [HBZ share] reasons for slow addition and deletion of ArrayList and fast query
- Fuzzy -- basic application method of AFL
- [FreeRTOS interrupt experiment]
- Can Flink SQL read multiple topics at the same time. How to write in with
- Vulnerability discovery - vulnerability probe type utilization and repair of web applications
- 关于imx8mp的es8316的芯片调试
- 图论的扩展
- How to realize automatic playback of H5 video
- CADD course learning (8) -- virtual screening of Compound Library
猜你喜欢

Programmers' position in the Internet industry | daily anecdotes

CADD course learning (8) -- virtual screening of Compound Library

ue5 小知识点 开启lumen的设置

几种RS485隔离通讯的方案介绍

你需要知道的 TCP 三次握手

Yolov5 tensorrt acceleration
![[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University](/img/22/ead74bc121a64910ef6ef374cd029b.png)
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University

ORM aggregate query and native database operation

SQL注入漏洞(MSSQL注入)

程序员在互联网行业的地位 | 每日趣闻
随机推荐
A blog to achieve embedded entry
Biscuits (examination version)
Nestjs配置文件上传, 配置中间件以及管道的使用
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
Basic knowledge and examples of binary tree
The most detailed and comprehensive update content and all functions of guitar pro 8.0
Selection of slow motion function
Why does MySQL need two-phase commit
ISP learning (2)
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
Can CDC pull the Oracle table in full
SQL注入漏洞(MSSQL注入)
[buuctf.reverse] 159_[watevrCTF 2019]Watshell
[buuctf.reverse] 159_ [watevrCTF 2019]Watshell
L'introduction en bourse de MSK Electronics a pris fin: 800 millions de RMB d'actifs de Henan étaient des actionnaires
Dry goods collection | Vulkan game engine video tutorial
Postman断言
Tengine kernel parameters
【LGR-109】洛谷 5 月月赛 II & Windy Round 6
Luogu deep foundation part 1 Introduction to language Chapter 2 sequential structure programming