回答

收藏

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

RT-Thread RT-Thread 2310 人阅读 | 0 人回复 | 2021-04-25

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

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

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

  6. #ifdef RT_USING_MODULE
  7.     void      *module_id;                               /**< id of application module */
  8. #endif
  9.     rt_list_t  list;                                    /**< list node of kernel object */
  10. };
  11. typedef struct rt_object *rt_object_t;                  /**< Type for kernel objects. */
复制代码

对象有对应的名称,类型,标识以及下一个的链表。

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

  10. #ifdef RT_USING_MODULE
  11.     void       *module_id;                              /**< id of application module */
  12. #endif

  13.     rt_list_t   list;                                   /**< the object list */
  14.     rt_list_t   tlist;                                  /**< the thread list */

  15.     /* stack point and entry */
  16.     void       *sp;                                     /**< stack point */
  17.     void       *entry;                                  /**< entry */
  18.     void       *parameter;                              /**< parameter */
  19.     void       *stack_addr;                             /**< stack address */
  20.     rt_uint32_t stack_size;                             /**< stack size */

  21.     /* error code */
  22.     rt_err_t    error;                                  /**< error code */

  23.     rt_uint8_t  stat;                                   /**< thread status */

  24. #ifdef RT_USING_SMP
  25.     rt_uint8_t  bind_cpu;                               /**< thread is bind to cpu */
  26.     rt_uint8_t  oncpu;                                  /**< process on cpu` */

  27.     rt_uint16_t scheduler_lock_nest;                    /**< scheduler lock count */
  28.     rt_uint16_t cpus_lock_nest;                         /**< cpus lock count */
  29. #endif /*RT_USING_SMP*/

  30.     /* priority */
  31.     rt_uint8_t  current_priority;                       /**< current priority */
  32.     rt_uint8_t  init_priority;                          /**< initialized priority */
  33. #if RT_THREAD_PRIORITY_MAX > 32
  34.     rt_uint8_t  number;
  35.     rt_uint8_t  high_mask;
  36. #endif
  37.     rt_uint32_t number_mask;

  38. #if defined(RT_USING_EVENT)
  39.     /* thread event */
  40.     rt_uint32_t event_set;
  41.     rt_uint8_t  event_info;
  42. #endif

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

  46. #ifndef RT_USING_SMP
  47.     void            *sig_ret;                           /**< the return stack pointer from signal */
  48. #endif
  49.     rt_sighandler_t *sig_vectors;                       /**< vectors of signal handler */
  50.     void            *si_list;                           /**< the signal infor list */
  51. #endif

  52.     rt_ubase_t  init_tick;                              /**< thread's initialized tick */
  53.     rt_ubase_t  remaining_tick;                         /**< remaining tick */

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

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

  56.     /* light weight process if present */
  57. #ifdef RT_USING_LWP
  58.     void        *lwp;
  59. #endif

  60.     rt_uint32_t user_data;                             /**< private user data beyond this thread */
  61. };
  62. typedef struct rt_thread *rt_thread_t;
复制代码

RT-Thread对象管理
相关接口如下所示:
  1. // object.c

  2. /**
  3. * This function will initialize an object and add it to object system
  4. * management.
  5. *
  6. * @param object the specified object to be initialized.
  7. * @param type the object type.
  8. * @param name the object name. In system, the object's name must be unique.
  9. */
  10. void rt_object_init(struct rt_object         *object,
  11.                     enum rt_object_class_type type,
  12.                     const char               *name);
  13. /**
  14. * This function will detach a static object from object system,
  15. * and the memory of static object is not freed.
  16. *
  17. * @param object the specified object to be detached.
  18. */
  19. void rt_object_detach(rt_object_t object);

  20. /**
  21. * This function will allocate an object from object system
  22. *
  23. * @param type the type of object
  24. * @param name the object name. In system, the object's name must be unique.
  25. *
  26. * @return object
  27. */
  28. rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name);

  29. /**
  30. * This function will delete an object and release object memory.
  31. *
  32. * @param object the specified object to be deleted.
  33. */
  34. void rt_object_delete(rt_object_t object);
复制代码

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

分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条