caroline11 发表于 2021-4-25 08:38:59

RT-Thread系统面向对象思想浅析

一切皆对象,将对象的属性和操作封装成类的方式。基本的三个特性就是封装,继承和多态,具体体现就是结构体的嵌套。

使用面向对象思想进行开发有以下优点: 1、易维护 采用面向对象思想设计的结构,可读性高,由于继承的存在,即使改变需求,那么维护也只是在局部模块,所以维护起来是非常方便和较低成本的。 2、质量高 在设计时,可重用现有的,在以前的项目的领域中已被测试过的类使系统满足业务需求并具有较高的质量。 3、效率高 在软件开发时,根据设计的需要对现实世界的事物进行抽象,产生类。使用这样的方法解决问题,接近于日常生活和自然的思考方式,势必提高软件开发的效率和质量。 4、易扩展 由于继承、封装、多态的特性,自然设计出高内聚、低耦合的系统结构,使得系统更灵活、更容易扩展,而且成本较低。

对象、继承、派生
RTT内核对于对象的管理有个万能的对象类型数据结构struct rt_object,具体属性如下
struct rt_object
{
    char       name;                     /**< name of kernel object */
    rt_uint8_t type;                                    /**< type of kernel object */
    rt_uint8_t flag;                                    /**< flag of kernel object */

#ifdef RT_USING_MODULE
    void      *module_id;                               /**< id of application module */
#endif
    rt_list_tlist;                                    /**< list node of kernel object */
};
typedef struct rt_object *rt_object_t;                  /**< Type for kernel objects. */
对象有对应的名称,类型,标识以及下一个的链表。

RT-Thread 内核对象继承关系
从上图可以看到,线程结构体,内存池结构体,定时器结构体,设备结构体等都是继承了rt_object,并在此基础上派生出了自己的私有属性
/**
* Thread structure
*/
struct rt_thread
{
    /* rt object */
    char      name;                      /**< the name of thread */
    rt_uint8_ttype;                                 /**< type of object */
    rt_uint8_tflags;                                  /**< thread's flags */

#ifdef RT_USING_MODULE
    void       *module_id;                              /**< id of application module */
#endif

    rt_list_t   list;                                 /**< the object list */
    rt_list_t   tlist;                                  /**< the thread list */

    /* stack point and entry */
    void       *sp;                                     /**< stack point */
    void       *entry;                                  /**< entry */
    void       *parameter;                              /**< parameter */
    void       *stack_addr;                           /**< stack address */
    rt_uint32_t stack_size;                           /**< stack size */

    /* error code */
    rt_err_t    error;                                  /**< error code */

    rt_uint8_tstat;                                 /**< thread status */

#ifdef RT_USING_SMP
    rt_uint8_tbind_cpu;                               /**< thread is bind to cpu */
    rt_uint8_toncpu;                                  /**< process on cpu` */

    rt_uint16_t scheduler_lock_nest;                  /**< scheduler lock count */
    rt_uint16_t cpus_lock_nest;                         /**< cpus lock count */
#endif /*RT_USING_SMP*/

    /* priority */
    rt_uint8_tcurrent_priority;                     /**< current priority */
    rt_uint8_tinit_priority;                        /**< initialized priority */
#if RT_THREAD_PRIORITY_MAX > 32
    rt_uint8_tnumber;
    rt_uint8_thigh_mask;
#endif
    rt_uint32_t number_mask;

#if defined(RT_USING_EVENT)
    /* thread event */
    rt_uint32_t event_set;
    rt_uint8_tevent_info;
#endif

#if defined(RT_USING_***NALS)
    rt_sigset_t   sig_pending;                        /**< the pending signals */
    rt_sigset_t   sig_mask;                           /**< the mask bits of signal */

#ifndef RT_USING_SMP
    void            *sig_ret;                           /**< the return stack pointer from signal */
#endif
    rt_sighandler_t *sig_vectors;                     /**< vectors of signal handler */
    void            *si_list;                           /**< the signal infor list */
#endif

    rt_ubase_tinit_tick;                              /**< thread's initialized tick */
    rt_ubase_tremaining_tick;                         /**< remaining tick */

    struct rt_timer thread_timer;                     /**< built-in thread timer */

    void (*cleanup)(struct rt_thread *tid);             /**< cleanup function when thread exit */

    /* light weight process if present */
#ifdef RT_USING_LWP
    void      *lwp;
#endif

    rt_uint32_t user_data;                           /**< private user data beyond this thread */
};
typedef struct rt_thread *rt_thread_t;
RT-Thread对象管理
相关接口如下所示:
// object.c

/**
* 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);
总结
RT-Thread内核的设计思想就是基于面向对象的方式,使得开发起来的耦合性更强,针对组件的维护对于整体来说改动较小,对于不同的场景应用,可以通过裁剪不同的对象来精简代码。

页: [1]
查看完整版本: RT-Thread系统面向对象思想浅析