对RT-Thread系统的理解-(系统对象)

http://www.rt-thread.org/

对RT-Thread系统的理解-(系统对象)
0
推荐

由于工作的原因,好久没有关注嵌入式系统了,在读书的时候看了一下ucos的源码,后来发现它不再是免费的了,所以在网上搜索了一下,看看还有没有其它的免费替代品,还真发现了不少,足以见得,嵌入式系统的发展势头了,对于嵌入式操作系统不再是一个神秘的东西。

RT-Thread,是一个国内的开源项目,目前来说是GPL2,希望其一直能开源+免费,它采用了面向对象的方法来设计的,把系统中的大部分资源看成是对象,比如有线程对象、事件对象、信号量对象、设备对象等等,有统一的对象容器进行存储,其实说白了,就是个双向链表

首先上第一个关键结构:

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_list_t object_list;

系统首先是通过定义一个静态结构数组,把能保存各类对象的容器建立起来:

struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =
{
 /* initialize object container - thread */
 {RT_Object_Class_Thread, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Thread), sizeof(struct rt_thread)},
#ifdef RT_USING_SEMAPHORE
 /* initialize object container - semaphore */
 {RT_Object_Class_Semaphore, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Semaphore), sizeof(struct rt_semaphore)},
#endif
#ifdef RT_USING_MUTEX
 /* initialize object container - mutex */
 {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 */
 {RT_Object_Class_Event, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Event), sizeof(struct rt_event)},
#endif
#ifdef RT_USING_MAILBOX
 /* initialize object container - mailbox */
 {RT_Object_Class_MailBox, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_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_Class_MessageQueue), sizeof(struct rt_messagequeue)},
#endif
#ifdef RT_USING_MEMPOOL
 /* initialize object container - memory pool */
 {RT_Object_Class_MemPool, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_MemPool), sizeof(struct rt_mempool)},
#endif
#ifdef RT_USING_DEVICE
 /* initialize object container - device */
 {RT_Object_Class_Device, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Device), sizeof(struct rt_device)},
#endif
 /* initialize object container - timer */
 {RT_Object_Class_Timer, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Timer), sizeof(struct rt_timer)},
#ifdef RT_USING_MODULE
 /* initialize object container - module */
 {RT_Object_Class_Module, _OBJ_CONTAINER_LIST_INIT(RT_Object_Class_Module), sizeof(struct rt_module)},
#endif
};

系统对象的基类:

/**
 * Base structure of Kernel object
 */
struct rt_object
{
 char        name[RT_NAME_MAX];                      /**< 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_t   list;                                   /**< list node of kernel object */
};
typedef struct rt_object *rt_object_t;                  /* Type for kernel objects.     */

任何系统对象都是由此基类派生而来,比如说线程对象

struct rt_thread
{
 /* rt object */
 char        name[RT_NAME_MAX];                      /**< the name of thread                     */
 rt_uint8_t  type;                                   /**< type of object                         */
 rt_uint8_t  flags;                                  /**< 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_uint16_t stack_size;                             /**< stack size                             */

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

 rt_uint8_t  stat;                                   /**< thread stat                            */

 /* priority */
 rt_uint8_t  current_priority;                       /**< current priority                       */
 rt_uint8_t  init_priority;                          /**< initialized priority                   */
#if RT_THREAD_PRIORITY_MAX > 32
 rt_uint8_t  number;
 rt_uint8_t  high_mask;
#endif
 rt_uint32_t number_mask;

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

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

 struct rt_timer thread_timer;                       /**< thread timer                           */

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

 rt_uint32_t user_data;                              /**< user data                              */
};

红色部分就是从基类中继承的,系统中每创建一个线程对象,就在把它放入到线程对象容器中,也就是放入rt_object_container[RT_Object_Class_Thread] 中,其它类型的对象也是从大同小异。


你可能感兴趣的:(对RT-Thread系统的理解-(系统对象))