RT-Thread对象管理解析(通过线程创建源码分析)


layout: post
title: “对象管理”
date: 2024-1-30 15:39:08 +0800
tags: RT-Thread


对象管理

RT-Thread的对象可以使用动态创建以及静态创建, 使用动态的话需要定义一个宏定义RT_USING_HEAP, 有这一个宏定义后会使用堆区进行初始化

一般使用动态的函数为rt_xxx_create(), 静态的为rt_xxx_init()

内核对象的管理

RT-Thread采用内核对象管理系统来访问/管理所有内核对象,内核对象包含了内核中绝大部分设施

这些对象可以是静态的也可以是动态创建的

  1. 使用链表的方式进行管理

RT-Thread对象管理解析(通过线程创建源码分析)_第1张图片

  1. 使用继承的方式进行管理

RT-Thread对象管理解析(通过线程创建源码分析)_第2张图片

可以使用统一的接口对对象进行管理

主要使用的类型

//一个基础类型, 一般位于内核对象的最前方, 用于记录一个对象的信息
struct rt_object
{
    /*内核对象名称*/
    char name[RT_NAME_MAX];
    /*内核对象类型*/
    rt_uint8_t type;
    /*内核对象的参数*/
    rt_uint8_t flag;
    /*内核对象管理链表*/
    rt_list_t list;
};
/**可以使用的对象类型
 *  The object type can be one of the follows with specific
 *  macros enabled:
 *  - Thread
 *  - Semaphore
 *  - Mutex
 *  - Event
 *  - MailBox
 *  - MessageQueue
 *  - MemHeap
 *  - MemPool
 *  - Device
 *  - Timer
 *  - Unknown
 *  - Static
 */
enum rt_object_class_type
{
    RT_Object_Class_Null          = 0x00,      /**< The object is not used. */
    RT_Object_Class_Thread        = 0x01,      /**< 对象为线程类型 */
    RT_Object_Class_Semaphore     = 0x02,      /**< 对象为信号量类型 */
    RT_Object_Class_Mutex         = 0x03,      /**< 对象为互斥量类型 */
    RT_Object_Class_Event         = 0x04,      /**< 对象为事件类型 */
    RT_Object_Class_MailBox       = 0x05,      /**< 对象为邮箱类型 */
    RT_Object_Class_MessageQueue  = 0x06,      /**< 对象为消息队列类型 */
    RT_Object_Class_MemHeap       = 0x07,      /**< memory heap. */
    RT_Object_Class_MemPool       = 0x08,      /**< memory pool. */
    RT_Object_Class_Device        = 0x09,      /**< 对象为设备类型 */
    RT_Object_Class_Timer         = 0x0a,      /**< 对象为定时器类型 */
    RT_Object_Class_Unknown       = 0x0c,      /**< 对象类型未知 */
    RT_Object_Class_Static        = 0x80       /**< 对象为静态对象, 这是一个标志位 */
};
/**
 * The information of the kernel object, 这一个是用于记录初始化信息的临时变量
 */
struct rt_object_information
{
    enum rt_object_class_type type;                     /**< object class type */
    rt_list_t                 object_list;              /**< object list */
    rt_size_t                 object_size;              /**< object size */
};

RT-Thread使用一个结构体数组static struct rt_object_information rt_object_container[RT_Object_Info_Unknown]保存默认的信息

管理的API

初始化一个静态的对象

/**实际上是对一个以及存在的类型进行赋值以及链表的插入
 * 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,
                    enum rt_object_class_type type,
                    const char               *name)

系统会把这个对象放置到对象容器中进行管理,即初始化对象的一些参数,然后把这个对象节点插入到对象容器的对象链表中

脱离一个对象

/**
 * 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_object_detach(rt_object_t object)

使得一个静态内核对象从内核对象容器中脱离出来,即从内核对象容器链表上删除相应的对象节点。

动态创建以及删除

/**清除类型以及链表的脱离
 * This function will allocate an object from object system
 *
 * @param type the type of 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 type, const char *name)
 /**多了一个释放内存
 * 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)

判断一个对象是不是静态对象

/**判断static标志位
 * 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_Class_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)

实际的管理示例(动态)

  • 以下用线程的初始化为例
/**
 * This function will create a thread object and allocate thread object memory
 * and stack.
 *
 * @param name the name of thread, which shall be unique
 * @param entry the entry function of thread
 * @param parameter the parameter of thread enter function
 * @param stack_size the size of thread stack
 * @param priority the priority of thread
 * @param tick the time slice if there are same priority thread
 *
 * @return the created thread object
 */
rt_thread_t rt_thread_create(const char *name,
                             void (*entry)(void *parameter),
                             void       *parameter,
                             rt_uint32_t stack_size,
                             rt_uint8_t  priority,
                             rt_uint32_t tick)
{
    struct rt_thread *thread;
    void *stack_start;
	//获取一个线程对象
    thread = (struct rt_thread *)rt_object_allocate(RT_Object_Class_Thread,
                                                    name);
    if (thread == RT_NULL)
        return RT_NULL;
	//获取一块栈区
    stack_start = (void *)RT_KERNEL_MALLOC(stack_size);
    if (stack_start == RT_NULL)
    {
        /* allocate stack failure */
        rt_object_delete((rt_object_t)thread);
        return RT_NULL;
    }
	//这个函数主要是用来填充信息的, 以及栈的初始化, 不在这里讨论(详细看线程管理的笔记, 以后会更新)
    _rt_thread_init(thread,
                    name,
                    entry,
                    parameter,
                    stack_start,
                    stack_size,
                    priority,
                    tick);

    return thread;
}
/**
 * This function will allocate an object from object system
 *
 * @param type the type of 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 type, const char *name)
{
    struct rt_object *object;
    register rt_base_t temp;
    struct rt_object_information *information;

    RT_DEBUG_NOT_IN_INTERRUPT;

    /* get object information 获取想要的对象的信息 */
    information = rt_object_get_information(type);
    RT_ASSERT(information != RT_NULL);
	//根据获取的信息进行分配一块内存, object对象实际上是这一个结构体的前几项
    object = (struct rt_object *)RT_KERNEL_MALLOC(information->object_size);
    if (object == RT_NULL)
    {
        /* no memory can be allocated */
        return RT_NULL;
    }

    /* clean memory data of object 把所有的位清零*/
    rt_memset(object, 0x0, information->object_size);

    /* initialize object's parameters 以下是初始化object的变量 */

    /* set object type */
    object->type = type;

    /* set object flag */
    object->flag = 0;

    /* copy name */
    rt_strncpy(object->name, name, RT_NAME_MAX);

    RT_OBJECT_HOOK_CALL(rt_object_attach_hook, (object));

    /* lock interrupt */
    temp = rt_hw_interrupt_disable();

    /* insert object into information object list 插入队列里面 */
    rt_list_insert_after(&(information->object_list), &(object->list));

    /* unlock interrupt */
    rt_hw_interrupt_enable(temp);

    /* return object */
    return object;
}
/**
 * This function will return the specified type of object information.
 *
 * @param type the type of object, which can be
 *             RT_Object_Class_Thread/Semaphore/Mutex... etc
 *
 * @return the object type information or RT_NULL
 */
struct rt_object_information *
rt_object_get_information(enum rt_object_class_type type)
{
    int index;
	//依次和保存的信息结构体里面的数值比较, 获取对应的类型的信息
    for (index = 0; index < RT_Object_Info_Unknown; index ++)
        if (rt_object_container[index].type == type) return &rt_object_container[index];

    return RT_NULL;
}

你可能感兴趣的:(stm32,嵌入式硬件,单片机,mcu,c语言,物联网)