3328 views|0 replies

118

Posts

1

Resources
The OP
 

A Brief Analysis of Object-Oriented Thinking in RT-Thread System [Copy link]

This post was last edited by ID.LODA on 2020-4-22 16:37

Everything is an object, and the properties and operations of an object are encapsulated into a class. The three basic characteristics are encapsulation, inheritance, and polymorphism, which are specifically manifested in the nesting of structures.

The advantages of using object-oriented thinking for development are as follows: 1. Easy to maintain. The structure designed with object-oriented thinking is highly readable. Due to the existence of inheritance, even if the requirements are changed, maintenance is only in the local module, so maintenance is very convenient and low-cost. 2. High quality. During design, existing classes that have been tested in the field of previous projects can be reused to make the system meet business needs and have high quality. 3. High efficiency. During software development, things in the real world are abstracted and classes are generated according to the needs of the design. Using such a method to solve problems is close to daily life and natural thinking, which is bound to improve the efficiency and quality of software development. 4. Easy to expand. Due to the characteristics of inheritance, encapsulation, and polymorphism, a high-cohesion, low-coupling system structure is naturally designed, making the system more flexible, easier to expand, and low-cost.

Objects, inheritance, derivation

The RTT kernel has a universal object type data structure struct rt_object for object management. The specific attributes are as follows

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. */

Objects have a corresponding name, type, identifier, and a linked list of the next one.

RT-Thread kernel object inheritance relationship

As can be seen from the figure above, the thread structure, memory pool structure, timer structure, device structure, etc. all inherit rt_object and derive their own private properties on this basis.

/**
 * Thread structure
 */
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_uint32_t stack_size; /**< stack size */

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

  rt_uint8_t stat; /**< thread status */

#ifdef RT_USING_SMP
  rt_uint8_t bind_cpu; /**< thread is bind to cpu */
  rt_uint8_t oncpu; /**< 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_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

#if defined(RT_USING_SIGNALS)
  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_t init_tick; /**< thread's initialized tick */
  rt_ubase_t remaining_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 Management

The relevant interfaces are as follows:

// 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);

Summarize

The design concept of the RT-Thread kernel is based on an object-oriented approach, which makes the development more coupled and the maintenance of components less change overall. For different application scenarios, the code can be streamlined by cutting different objects.

This post is from Embedded System

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list