当前位置:网站首页>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
边栏推荐
- I'd like to ask about the current MySQL CDC design. In the full volume phase, if a chunk's binlog backfill phase,
- Distributed transaction solution
- 【Try to Hack】john哈希破解工具
- Meet diverse needs: jetmade creates three one-stop development packages to help efficient development
- [数学建模] 微分方程--捕鱼业的持续发展
- [mathematical modeling] differential equation -- sustainable development of fishing industry
- Upload nestjs configuration files, configure the use of middleware and pipelines
- 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
- Finance online homework
- Summary of three log knowledge points of MySQL
猜你喜欢
SQL injection vulnerability (MSSQL injection)
Case of Jiecode empowerment: professional training, technical support, and multiple measures to promote graduates to build smart campus completion system
SQL注入漏洞(MSSQL注入)
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
Bill Gates posted his 18-year-old resume and expected an annual salary of $12000 48 years ago
[数学建模] 微分方程--捕鱼业的持续发展
比尔·盖茨晒18岁个人简历,48年前期望年薪1.2万美元
Why does MySQL need two-phase commit
View workflow
Acwing week 58
随机推荐
Zynq learning notes (3) - partial reconfiguration
Selection of slow motion function
Tengine kernel parameters
Raspberry pie 3.5-inch white screen display connection
MPLS experiment
行业专网对比公网,优势在哪儿?能满足什么特定要求?
团队协作出了问题,项目经理怎么办?
驱动开发——第一个HelloDDK
web工程导入了mysql驱动jar包却无法加载到驱动的问题
Supreme Court, judgment standard of divorce cases
Distributed transaction solution
Ue5 small knowledge freezerendering view rendered objects in the cone
Weng Kai C language third week 3.1 punch in
Postman断言
Digital children < daily question> (Digital DP)
[HBZ sharing] how to locate slow queries in cloud database
[FreeRTOS interrupt experiment]
2021 RoboCom 世界机器人开发者大赛-本科组(复赛)
ISP learning (2)
Redis 排查大 key 的4種方法,優化必備