Kernel Object Management System Analysis of rt-thread

Source: Internet
Author: User

Rt-thread uses the Kernel Object Management System to access and manage all kernel objects. first, let's take a look at how the rt-thread kernel object is defined: 1. Data Structure 1.1. The object control block is in include/rtdef. the Kernel object in the h header file has the structure definition: [cpp]/*** Base structure of Kernel object */struct rt_object {char name [RT_NAME_MAX]; // name rt_uint8_t type; // Kernel Object Type rt_uint8_t flag; // Kernel Object flag # ifdef RT_USING_MODULE void * module_id; // module ID # endif rt_list_t list; // Kernel Object linked list node }; typedef struct rt_object * rt_object_t; note that the above kernel object control block contains an rt_list _ T type member list, which is a linked list node. This kernel object can be added to a linked list. Its structure is defined as follows: [cpp] struct rt_list_node {struct rt_list_node * next; // point to the next node struct rt_list_node * prev; // point to the previous node}; typedef struct rt_list_node rt_list_t; other kernel object types include: [cpp]/*** The object type can be one of the follows with specific * macros enabled: *-Thread *-Semaphore *-Mutex *-Event *-MailBox *-MessageQueue *-MemHeap *-MemPool *-Device *- Timer *-Module *-Unknown *-Static */enum rt_object_class_type {RT_Object_Class_Thread = 0, // thread # ifdef success, // semaphore # endif # ifdef RT_USING_MUTEX RT_Object_Class_Mutex, // mutex lock # endif # ifdef RT_USING_EVENT RT_Object_Class_Event, // event # endif # ifdef RT_USING_MAILBOX RT_Object_Class_MailBox, // mailbox # endif # ifdef define RT_Object_Class_MessageQ Ueue, // Message Queue # endif # ifdef zookeeper, // memory heap # endif # ifdef RT_USING_MEMPOOL RT_Object_Class_MemPool, // memory pool # endif # ifdef RT_USING_DEVICE RT_Object_Class_Device, // Device Driver # endif RT_Object_Class_Timer, // clock # ifdef RT_USING_MODULE RT_Object_Class_Module, // Module # endif RT_Object_Class_Unknown, // unknown Kernel Object Type RT_Object_Class_Static = 0x80 // rt-thread indicates whether it is a system kernel object}; note that rt-thr If the type of the kernel object is set to 1, ead indicates that the kernel object is a system kernel object, otherwise it is not a system kernel object. 1.2 kernel object container RTT uses the kernel object container to manage the same type of kernel objects and put them in the same linked list for easy access. the kernel object information structure is defined as follows: [cpp]/*** the information of The kernel object */struct rt_object_information {enum rt_object_class_type type; // the kernel object type rt_list_t object_list; // Kernel Object linked list rt_size_t object_size; // kernel object size}; 1.3 in Kernel Object Management System RTT, each type of kernel object has a kernel object container for inclusion, this type of kernel object container actually uses a linked list (see the kernel object container structure definition shown in section 1.2). This linked list includes all the kernel objects of the same type. Object link. because each type corresponds to a kernel object container for management, all kernel object containers are called the kernel object management system as a whole. in RTT, the kernel object management system is implemented using an rt_object_information array, as follows: [cpp] # define _ OBJ_CONTAINER_LIST_INIT (c) \ // initialize the linked list of the kernel object container, A macro is defined here. The previous and next nodes of the linked list point to their own address during initialization {& (rt_object_container [c]. object_list), & (rt_object_container [c]. object_list)} // The Kernel Object Management System. The rt_object_information array is used to implement struct rt_object_information rt_object_container [RT_Object_Class_Unknown] = {/* Initialize object container-thread */)}, // thread object information {RT_Object_Class_Thread, _ OBJ_CONTAINER_LIST_INIT (RT_Object_Class_Thread ), sizeof (struct rt_thread # ifdef RT_USING_SEMAPHORE/* initialize object container-semaphore * // semaphore object information {signal, _ sign (signal), sizeof (struct rt_semaphore )}, # endif # ifdef RT_USING_MUTEX/* initialize object container -Mutex * // mutex object information {RT_Object_Class_Mutex, _ OBJ_CONTAINER_LIST_INIT (RT_Object_Class_Mutex), sizeof (struct rt_mutex )}, # endif # ifdef RT_USING_EVENT/* initialize object container-event * // event object information {RT_Object_Class_Event, _ encode (RT_Object_Class_Event), sizeof (struct rt_event )}, # endif # ifdef RT_USING_MAILBOX/* initialize object container-mailbox * // email object information {RT_Object_Class_Ma IlBox, _ encode (RT_Object_Class_MailBox), sizeof (struct rt_mailbox)}, # endif # ifdef RT_USING_MESSAGEQUEUE/* initialize object container-message queue * // message queue object information {region, _ Queue (RT_Object_Class_MessageQueue), sizeof (struct rt_messagequeue)}, # endif # ifdef RT_USING_MEMHEAP/* initialize object container-memory heap * // memory heap object information {RT_Object _ Class_MemHeap, _ partition (bytes), sizeof (struct rt_memheap)}, # endif # ifdef RT_USING_MEMPOOL/* initialize object container-memory pool * // memory pool object information {RT_Object_Class_MemPool, _ partition (RT_Object_Class_MemPool), sizeof (struct rt_mempool)}, # endif # ifdef RT_USING_DEVICE/* initialize object container-device * // information about the device driver object {RT_Object_Class_Device, _ OBJ _ CONTAINER_LIST_INIT (RT_Object_Class_Device), sizeof (struct rt_device)}, # endif/* initialize object container-timer * // clock object information {RT_Object_Class_Timer, _ timer (timer ), sizeof (struct rt_timer)}, # ifdef RT_USING_MODULE/* initialize object container-module * // module object information {RT_Object_Class_Module, _ partition (RT_Object_Class_Module), sizeof (struct rt_modul E) },# endif}; 2 kernel object interface 2.1 kernel object initialization RTT provides two static and dynamic initialization interfaces: static initialization initializes an existing object that occupies the memory space. Its interface is as follows: [cpp]/*** This function will initialize an object and add it to object system * management. ** @ param object the specified object to be initialized. * @ param type the object type. * @ param name the object name. in system, the object's name must be unique. */void rt_object_init (struct rt_object * object, // indicates To the existing object pointer enum rt_object_class_type type, // The object type const char * name) // The Object name string {register rt_base_t temp; struct rt_object_information * information; // object container # ifdef RT_USING_MODULE // If a module is used, the object container points to the Object window contained in the thread, otherwise, point to the corresponding container in the Global object Management System/* get module object information */information = (rt_module_self ()! = RT_NULL )? & Rt_module_self ()-> module_object [type]: & rt_object_container [type]; # else/* get object information */information = & rt_object_container [type]; # endif/* initialize object's parameters * // * set object type to static */object-> type = type | RT_Object_Class_Static; // set the system object flag/* copy name */rt_strncpy (object-> name, name, RT_NAME_MAX); // assign RT_OBJECT_HOOK_CALL (rt_object_attach_hook, (object) to the name ));/ /Use the hook function/* lock interrupt */temp = rt_hw_interrupt_disable (); // Guanzhong disconnection/* insert object into information object list */rt_list_insert_after (& (information-> object_list ), & (object-> list); // Add the initialized kernel object to the corresponding container/* unlock interrupt */rt_hw_interrupt_enable (temp ); // open interrupt} dynamic initialization means that the object does not exist originally. In non-memory mode, you need to dynamically allocate memory for it. Its interface is as follows: [cpp]/*** This function will allocate an object from object system ** @ param type the type Object * @ param name the object name. in system, the object's name must be unique. ** @ return object */rt_object_t rt_object_allocate (enum rt_object_class_type, const char * name) // for the dynamic initialization interface, you only need to input the name and type {struct rt_object * object; register rt_base_t temp; struct rt_object_information * information; // object container RT_DEBUG_NOT_IN_INTERRUPT; # ifdef RT_USING_MODULE // obtain the object container/** get module object I like the interface above Nformation, * module object shocould be managed by kernel object container */information = (rt_module_self ()! = RT_NULL & (type! = RT_Object_Class_Module ))? & Rt_module_self ()-> module_object [type]: & rt_object_container [type]; # else/* get object information */information = & rt_object_container [type]; # endif object = (struct rt_object *) rt_malloc (information-> object_size); // dynamically allocates memory space for the object if (object = RT_NULL) {/* no memory can be allocated */return RT_NULL;}/* initialize object's parameters * // * set object type */object-> type = type; // set the type/* set Object flag */object-> flag = 0; // set the flag to 0 # ifdef RT_USING_MODULE if (rt_module_self ()! = RT_NULL) {object-> flag | = RT_OBJECT_FLAG_MODULE; // If the module function is used, set the flag to the module flag} object-> module_id = (void *) rt_module_self (); // set the module ID # endif/* copy name */rt_strncpy (object-> name, name, RT_NAME_MAX); // assign the RT_OBJECT_HOOK_CALL (rt_object_attach_hook, (object); // use the hook function/* lock interrupt */temp = rt_hw_interrupt_disable (); // Guanzhong disconnection/* insert object into information object list */rt_list_insert_after (& (Information-> object_list), & (object-> list); // Add this object to the corresponding container/* unlock interrupt */rt_hw_interrupt_enable (temp ); // Guanzhong disconnected/* return object */return object;} 2.2 disconnects from or deletes an object. If the object is statically initialized, the object is detached. if the object is dynamically initialized, is deleted. the escape interface is as follows: [cpp]/*** This function will detach a static object from object system, * and the memory of static object is not freed. ** @ param object the specified object to be detached. */void rt_obj Ect_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); // use the hook function/* lock interrupt */temp = rt_hw_interrupt_disable (); // Guanzhong disconnection/* remove from old list */rt_list_remove (& (object-> list); // remove from the window/* unlock interrupt */rt_hw_interrupt_enable (temp ); // open interrupt} The deletion interface is as follows: [cpp]/*** This function will delete an object and release object memory. ** @ param object the specified object to be deleted. */ Void rt_object_delete (rt_object_t object) {register rt_base_t temp;/* object check */RT_ASSERT (object! = RT_NULL); RT_ASSERT (! (Object-> type & RT_Object_Class_Static); // The deleted object must be a non-System object RT_OBJECT_HOOK_CALL (rt_object_detach_hook, (object )); // use the hook function/* lock interrupt */temp = rt_hw_interrupt_disable (); // Guanzhong disconnection/* remove from old list */rt_list_remove (& (object-> list )); // remove/* unlock interrupt */rt_hw_interrupt_enable (temp); // open interrupt # if defined (RT_USING_MODULE) & defined (RT_USING_SLAB) from the corresponding container) // if the module function is used and the SLAB dynamic memory management mode is used, if (object -> Flag & RT_OBJECT_FLAG_MODULE) rt_module_free (rt_module_t) object-> module_id, object ); // release the space occupied by the module ID else # endif/* free the memory of object */rt_free (object ); // release the space occupied by the kernel object} among them, rt_list_remove will automatically find the previous node and the next node of the object, and then delete its own node. 1.3 determine whether it is the system kernel object [cpp]/*** This function will judge the object is system object or not. * Normally, the system object is a static object and the type * of object set to rt_object_cia Ss_Static. ** @ param object the specified object to be judged. ** @ return RT_TRUE if a system object, RT_FALSE for others. */rt_bool_t rt_object_is_systemobject (rt_object_t object) {/* object check */RT_ASSERT (object! = RT_NULL); if (object-> type & RT_Object_Class_Static) // RTT determines whether the object is the return RT_TRUE of the system kernel object by checking whether the highest bit of the type of the kernel object is 1; return RT_FALSE;} 1.4 search for the kernel object [cpp]/*** This function will find specified name object from object * container. ** @ param name the specified name of object. * @ param type the type of object ** @ return the found object or RT_NULL if there is no this object * in object container. ** @ no Te this function shall not be invoked in interrupt status. */rt_object_t rt_object_find (const char * name, struct type) {struct rt_object * object; struct rt_list_node * node; struct rt_object_information * information; extern volatile implements rt_interrupt_nest; /* parameter check * // Input System check if (name = RT_NULL) | (type> RT_Object_Class_Unknown) return RT_NULL;/* which is invoke in inter Rupt status */if (rt_interrupt_nest! = 0) // ensure that no nested RT_ASSERT (0) is interrupted currently;/* enter critical */rt_enter_critical (); // enter the critical section/* try to find object */information = & rt_object_container [type]; // obtain the corresponding object container for (node = information-> object_list.next; // start scanning the kernel object node by name! = & (Information-> object_list); node = node-> next) {object = rt_list_entry (node, struct rt_object, list ); // obtain the kernel object if (rt_strncmp (object-> name, name, RT_NAME_MAX) = 0) // determine whether the name matches {/* leave critical */rt_exit_critical (); // exit the critical section return object;}/* leave critical */rt_exit_critical (); // exit the critical section return RT_NULL ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.