RT thread kernel learning notes - kernel object initialization linked list organization

Posted by gfX on Mon, 07 Mar 2022 22:26:25 +0100

RT thread kernel learning notes - kernel object rt_object

RT thread kernel learning notes - kernel object management

RT thread kernel learning notes - kernel object operation API

RT thread kernel learning notes - kernel object initialization linked list organization

RT thread kernel learning notes - in depth understanding of kernel object linked list structure

RT thread kernel learning notes - device model_ Device understanding

RT thread kernel learning notes - understanding defunct zombie threads

background

  • Recently, I was looking at the source code of RT thread kernel. The kernel objects are organized by linked list.
  • RT thread has complete operation API s and actual use cases of [two-way linked list] and [one-way linked list]
  • Kernel objects, such as threads, timers and devices derived from the kernel, are managed by chain and table chains.

Introduction to linked list

/**
 * Double List structure
 */
struct rt_list_node
{
    struct rt_list_node *next;                          /**< point to next node. */
    struct rt_list_node *prev;                          /**< point to prev node. */
};
typedef struct rt_list_node rt_list_t;                  /**< Type for lists. */
  • Linked list is a data structure, similar to other structures. After initialization, it occupies memory space and has its own memory address.
  • Generally, the internal members of a two-way linked list are pointers to the structure of the list itself. Note that the pointer is not determined after initialization.
  • When initializing the two-way linked list of RT thread, the internal pointer points to its own address, that is, the initial value is assigned to the members of the linked list (pointer content, usually address).
  • When using a linked list, nodes are generally global structure variables, global static initialization, or dynamic memory application (global).
  • Do not use global linked list nodes. Be careful not to link into the linked list structure. Otherwise, the node address will be wrong because the memory is released at the end of the [life cycle], and the nodes of each linked list cannot be managed.

Bidirectional linked list API

  • Note here to mention the insertion order of linked list nodes. Because I encountered a little confusion, I studied it deeply.
  • Understanding: [latest node] [previous node] [earliest node]
  • The problem of the header of the linked list: the kernel object uses the [object container], which is global. After the object is initialized, RT is used_ list_ insert_ after
/* From: object c : rt_object_init */
        /* insert object into information object list */
        rt_list_insert_after(&(information->object_list), &(object->list));
  • Attention, rt_list_insert_after, the insertion position, [not the tail of the linked list, but after the first parameter of the first linked list node.]
  • That is, if three kernel objects are created, the default sorting is as follows:
Not: [container] head] --- [obj1] --- [obj2] --- [obj2] 
Instead: [container] head] --- [obj3] --- [obj2] --- [obj1] 
  • If the linked list is inserted after the [tail], first move the pointer of the linked list to the tail, and then execute: rt_list_insert_after.

View kernel objects

  • In fact, thread, device and other objects are derived from kernel objects.
  • RT thread provides a list_thread,list_device, etc. to view the object of the kernel.
  • Check the thread initialization sequence and the last print thread, which is the first thread created.
  • Generally, you can traverse each linked list node from the [header] of the linked list. As follows: the last node is: main thread
  • In fact, the main thread was first created.
msh />list_thread
thread   pri  status      sp     stack size max used left tick  error
-------- ---  ------- ---------- ----------  ------  ---------- ---
persim    16  suspend 0x000001ec 0x0000c000    08%   0x00000003 000
sens      28  suspend 0x000000d8 0x00001000    13%   0x00000019 000
hws       28  suspend 0x000000d8 0x00000800    10%   0x00000032 000
dcm_tpo   10  suspend 0x00000090 0x00000800    14%   0x00000004 000
dcm_tpo   10  suspend 0x00000090 0x00000800    14%   0x00000002 000
dcm_tpo   10  suspend 0x00000090 0x00000800    15%   0x00000004 000
tshell    20  running 0x000001fc 0x00001000    26%   0x0000000a 000
touch     16  suspend 0x00000098 0x00000800    18%   0x00000013 000
usbd       8  suspend 0x000000ac 0x00001000    04%   0x00000014 000
at_clnt    9  suspend 0x000000c0 0x00000600    12%   0x00000002 000
ulog_asy  30  suspend 0x00000084 0x00000c00    09%   0x00000006 000
mmcsd_de  22  suspend 0x000000a0 0x00000400    48%   0x00000014 000
alarmsvc  10  suspend 0x000000a8 0x00000800    27%   0x00000003 000
rils      12  suspend 0x000000b0 0x00000800    08%   0x0000001e 000
tidle0    31  ready   0x00000058 0x00000800    04%   0x0000001d 000
timer      4  suspend 0x00000074 0x00000800    08%   0x00000009 000
main      10  suspend 0x00000120 0x00000800    41%   0x00000012 000  /* The thread created first and printed last */
  • Linked list of kernel object initialization [order]:

summary

  • When using the linked list, pay attention to the insertion order, especially the read-write processing of FIFO buffer
  • Familiar with RT thread kernel objects and object management methods.
  • In depth study of the use of basic data structure: linked list

Topics: rt-thread