A timer management method based on an embedded real-time operating system

By employing a software timer management list and a linked list structure of high-precision timers in the embedded real-time operating system, the high precision and quantity requirements of timer management in embedded network devices are solved, achieving efficient and accurate timing services.

CN117149367BActive Publication Date: 2026-05-05SHENYANG INST OF AUTOMATION - CHINESE ACAD OF SCI
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SHENYANG INST OF AUTOMATION - CHINESE ACAD OF SCI
Filing Date
2022-05-24
Publication Date
2026-05-05

AI Technical Summary

Technical Problem

Existing embedded real-time operating system timer management methods cannot meet the needs of real-time embedded network devices for high-precision and numerous timing services, and traditional methods result in excessive CPU load and large timing errors.

Method used

It adopts a linked list structure consisting of a software timer management list, a software timer timeout management list, and a software timer service function management list. Combined with a high-precision timer, it supports multi-threaded access and multiple service modes. By optimizing timer management through unidirectional and doubly linked list structures, it reduces processing operations within interrupt service functions and improves timing accuracy and efficiency.

Benefits of technology

It supports multiple timing service modes, improves the accuracy and efficiency of timing services, reduces CPU load, and meets the timing service requirements of real-time embedded network devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117149367B_ABST
    Figure CN117149367B_ABST
Patent Text Reader

Abstract

This invention relates to a timer management method based on an embedded real-time operating system. The method first creates a list, then adds the timer services required by each thread in the system to the software timer management list in sequence. The software timer timeout management list is arranged according to the software timer timeout duration, and the system timers are invoked to execute the timing services in the order of the timeout durations in the software timer timeout management list. When a timer counts to its timeout duration, the corresponding software timer service function is loaded into the software timer service function management list, and finally, the software timer service functions in the list are executed to provide the timer services required by each thread in the system. This invention enables the overall device system to meet the timing service needs of numerous and diverse real-time embedded network devices even in the absence of high-precision hardware timers, and has broad application prospects.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of network communication, and specifically relates to a timer management method based on an embedded real-time operating system. Background Technology

[0002] In response to the complexity and dynamism of production processes in industrial production, as well as the new trend of business collaboration, enabling controllers to have self-learning capabilities and solve real-time sensing, real-time control, and intelligent data analysis close to the field has become a new development trend in modern control systems. Therefore, real-time embedded network devices are playing an increasingly important role in modern control systems.

[0003] In real-time embedded network devices, activities such as scheduling data transmission and reception for various communication protocols, starting and stopping communication protocol stacks, and delaying, synchronizing, and switching system tasks in real-time operating systems all require accurate and high-speed timers to provide timing services. While the software timers provided by embedded operating systems can meet the actual functional requirements for the number of timers, their timing accuracy is low and they cannot achieve fast switching responses. Hardware timers in real-time embedded network devices can provide high-precision timing services, and their switching response speed meets the actual functional requirements, but their limited number cannot meet the demand for a sufficient number of timers in practical applications. Therefore, implementing a stable and efficient timer management algorithm in real-time embedded network devices to meet their timing service requirements is a crucial problem that urgently needs to be solved.

[0004] Current traditional timer management methods all have their own limitations and drawbacks. For example, traditional methods use hardware timers to run at a fixed frequency to generate periodic interrupts. These periodic interrupts are then used as a timing reference source, and software counting is used to accumulate them. When a preset value is reached, a preset task is executed, thus providing a large number of high-precision timing services. However, this method requires frequent interrupt entry and exit, which puts a heavy burden on the CPU. Furthermore, each interrupt entry and exit introduces timing errors. When the system requires timers with high concurrency and high timeout rates, the above timer management method cannot meet the system's needs. Summary of the Invention

[0005] In view of the above-mentioned shortcomings in the existing technology, the technical problem to be solved by the present invention is to provide a timer management method based on an embedded real-time operating system, so as to meet the timer service requirements of real-time embedded network devices.

[0006] The technical solution adopted by this invention to achieve the above objectives is: a timer management method based on an embedded real-time operating system, comprising the following steps:

[0007] Step 1: Create a software timer management list, a software timer timeout management list, and a software timer service function management list;

[0008] Step 2: Add the timer services required by each thread in the embedded real-time operating system to the software timer management list in sequence;

[0009] Step 3: Traverse the software timer management list, and insert each software timer into the software timer timeout management list in ascending order of timeout duration, based on the timeout duration of each software timer in the software timer management list.

[0010] Step 4: Call the timer in the embedded real-time operating system to execute the timed service in the order of the timeout times in the software timer timeout management list;

[0011] Step 5: When the timer counts to the timeout period, load the software timer service function corresponding to the timeout period into the software timer service function management list, and execute the next timeout period in the software timer timeout management list;

[0012] Step 6: Execute the software timer service functions in the software timer service function management list, and return to Step 3.

[0013] The software timer management list and the software timer service function management list adopt a singly linked list structure, while the software timer timeout management list adopts a doubly linked list structure.

[0014] In step 2, multi-threaded access to the software timer management list within the system is supported, allowing any thread to add the required timer services to the software timer management list. At the same time, the software timer management list is protected by calling the mutex semaphore in the embedded real-time operating system, ensuring that only one thread operates on the software timer management list at any given physical moment.

[0015] In step 2, the supported timer service working modes include single-run mode and cyclic mode.

[0016] In step 4, the timer is the most accurate timer among all timers in the embedded real-time operating system.

[0017] In step 5, when the timer counts to the timeout period and enters the timer interrupt service function, the software timer interrupt service function corresponding to this timeout period is added to the software timer service function management list. According to the execution mode of the software timer service function, the corresponding thread is notified to execute the software timer service function. Then, the next timeout period is loaded from the software timer timeout management list and executed. At the same time, the software timers that have been executed in the software timer timeout management list are deleted.

[0018] In step 5, the supported software timer service function execution methods include interrupt callback trigger execution, semaphore trigger execution, and message queue trigger execution.

[0019] In step 6, after the corresponding software timer service function is executed, different operations are performed according to the timing service working mode of the software timer.

[0020] If the software timer is in single-run mode, remove the software timer from the software timer management list, and then re-execute steps 3-6 in sequence.

[0021] If the software timer is in cyclic mode, calculate the next timeout and add it back to the software timer management list, then repeat steps 3-6 in sequence.

[0022] A timer management device based on an embedded real-time operating system includes a memory and a processor; the memory is used to store a computer program; the processor is used to implement a timer management method based on an embedded real-time operating system when executing the computer program.

[0023] A computer-readable storage medium storing a computer program that, when executed by a processor, implements a timer management method based on an embedded real-time operating system.

[0024] This invention relates to a timer management method based on an embedded real-time operating system. It has the following advantages:

[0025] 1. Multiple types of timer services. The method of this invention supports common timer service working modes, including single-run mode and loop mode, and provides multiple timer service function execution methods, including interrupt callback triggering mode, semaphore triggering mode, and message queue triggering mode. At the same time, the method supports multi-threaded calls to timer services, meeting the vast majority of timer service needs in practical engineering applications.

[0026] 2. High timing service accuracy. This invention selects a high-precision timer as the timing counting source, ensuring the accuracy of the clock reference beat. Furthermore, within the high-precision timer's interrupt service function, only necessary and rapid operations are performed, such as adding the software timer interrupt service function corresponding to the current timeout to the software timer service function management list, notifying the appropriate thread to execute the software timer service function in the corresponding manner, and loading the next timeout time from the software timer timeout management list. This avoids the cumulative error caused by excessive processing operations within the interrupt service function, which would affect the timing service accuracy. Simultaneously, this method is based on an embedded real-time operating system, and the real-time performance of the embedded real-time operating system also effectively guarantees the timing service accuracy.

[0027] 3. High versatility. The method of this invention is based on mature timing management technology and a well-developed embedded real-time operating system. Implementing this method is technically simple and mature, with low system implementation complexity. Furthermore, this management method can select a high-precision hardware timer for the system device, or a high-precision software timer for the real-time embedded system, depending on the actual application. It does not require changes to the hardware system platform, exhibiting strong versatility and ease of implementation. Attached Figure Description

[0028] Figure 1 This is an overall flowchart of the method of the present invention;

[0029] Figure 2 This is a schematic diagram of the linked list element node data structure of the method of the present invention;

[0030] Figure 3 This is a schematic diagram of the linked list pointer data structure for the software timer management list in the method of this invention;

[0031] Figure 4 This is a schematic diagram of the linked list pointer data structure for the software timer timeout management in the method of this invention;

[0032] Figure 5 This is a schematic diagram of the linked list pointer data structure managed by the software timer service function of the present invention. Detailed Implementation

[0033] The present invention will now be described in further detail with reference to the accompanying drawings and examples.

[0034] This invention relates to a timer management method based on an embedded real-time operating system. The method first creates a software timer management list, a software timer timeout management list, and a software timer service function management list. Then, the timer services required by each thread in the system are added to the software timer management list in sequence. Next, the software timer timeout management list is arranged in order according to the software timer timeout duration, and the system's high-precision timer is invoked to execute the timing services according to the timeout order in the software timer timeout management list. When the high-precision timer counts to its timeout duration, the corresponding software timer service function is loaded into the software timer service function management list. Finally, the software timer service functions in the software timer service function management list are executed, thus providing the timer services required by each thread in the system. This invention implements a stable and efficient timer management algorithm, enabling the overall device system to meet the timing service needs of numerous and diverse real-time embedded network devices even in the absence of hardware high-precision timers, and has broad application prospects.

[0035] A timer management method based on an embedded real-time operating system includes the following steps:

[0036] Step 1: Create a software timer management list, a software timer timeout management list, and a software timer service function management list;

[0037] Step 2: Add the timer services required by each thread in the system to the software timer management list in order;

[0038] Step 3: Iterate through the software timer management list, and insert each software timer into the software timer timeout management list in ascending order of timeout duration, according to the timeout duration of each software timer in the list.

[0039] Step 4: Call the high-precision timer in the system to execute the timed service in the order of the timeout times in the software timer timeout management list;

[0040] Step 5: When the high-precision timer counts to the timeout period, the software timer service function corresponding to the timeout period is loaded into the software timer service function management list, and the next timeout period in the software timer timeout management list is executed.

[0041] Step 6: Execute the software timer service functions in the software timer service function management list. After all the software timer service functions in the timer service function management list have been executed, re-execute Steps 3-6 in sequence.

[0042] In Step 1, the software timer management list and the software timer service function management list are created using a singly linked list structure, while the software timer timeout management list uses a doubly linked list structure.

[0043] In Step 2, the system supports multi-threaded access to the software timer management list, allowing any thread to add the required timer services to the software timer management list. At the same time, the software timer management list is protected by calling the mutex semaphore in the embedded real-time operating system, ensuring that only one thread operates on the software timer management list at any given physical moment.

[0044] In Step 2, the supported timer service working modes include single-run mode and cyclic mode.

[0045] In Step 3, this management method establishes a separate thread for managing and maintaining the software timer timeout management list. This ensures the efficiency of inserting software timers into the software timer timeout management list and avoids spending too much time in the process of inserting software timers into the software timer timeout management list, which could lead to large errors in the timing service.

[0046] In Step 4, the high-precision timer called by this management method can be either a high-precision hardware timer of the system device or a high-precision system timer provided in the embedded real-time operating system.

[0047] In Step 5, when the high-precision timer counts to the timeout period and enters the high-precision timer interrupt service function, the software timer interrupt service function corresponding to this timeout period is added to the software timer service function management list. According to the execution method of the software timer service function, the corresponding thread is notified to execute the software timer service function. Then, the next timeout period is loaded from the software timer timeout management list and executed. At the same time, the software timers that have been executed in the software timer timeout management list are deleted.

[0048] In step 5, the software timer service function execution methods supported by this management method include interrupt callback trigger execution, semaphore trigger execution, and message queue trigger execution.

[0049] In Step 6, after the corresponding software timer service function is executed, different operations are performed according to the timing service working mode of the software timer. If the software timer is in single-time working mode, the software timer is deleted from the software timer management list, and then Steps 3-6 are executed again in sequence. If the software timer is in cyclic working mode, the next timeout time is calculated and added back to the software timer management list, and then Steps 3-6 are executed again in sequence.

[0050] Figure 1 This is an overall flowchart of the method of the present invention. The method first creates a software timer management list, a software timer timeout management list, and a software timer service function management list within the device system. Then, the timing services required by each thread in the system are added to the software timer management list in sequence. Next, the system traverses the software timer management list, inserting each software timer into the software timer timeout management list in ascending order according to its set timeout time. The system then calls a high-precision timer to execute timing counts one by one according to the timeout times in the software timer timeout management list. When the high-precision timer counts to the timeout time and enters the high-precision timer interrupt service function, the software timer service function corresponding to that timeout time is loaded into the software timer service function management list, and the next timeout time in the software timer timeout management list is executed. Finally, the software timer service functions in the software timer service function management list are executed, thereby realizing the timing services required by each thread in the system. The following will describe each step in detail with reference to the specific implementation process of this method in practical applications.

[0051] First, a software timer management list `SoftTimerList`, a software timer timeout management list `SoftArmedTimerList`, and a software timer service function management list `CallBackList` are created in the real-time embedded system within the device. The `SoftTimerList` and `CallBackList` use a singly linked list structure, while the `SoftArmedTimerList` uses a doubly linked list structure. In the specific implementation of this method, to simplify the complexity of element insertion and deletion operations between the linked lists and improve the execution speed of each operation step, the element node structure within each linked list adopts the following... Figure 2Although this implementation method uses the same data structure as shown, it may result in each linked list node having members in its structure that are not related to the management of the linked list, increasing the storage space required for the linked list. However, it can reduce the delay error caused by performing too many complex operations, improve the timing accuracy of the overall software timer, and reduce the complexity of this method. At the same time, with the continuous development of real-time embedded devices, the storage space inside the devices has become more and more abundant. Compared with the storage space wasted in this implementation process, the benefits it brings are more obvious.

[0052] Figure 2 The `tHrtimer` data structure member `m_signalling` in the linked list element node data structure indicates the execution mode of the software timer service function. This method provides three execution modes: interrupt callback triggering, semaphore triggering, and message queue triggering. The `m_Mode` data structure member indicates the working mode of the software timer. This method supports single-run mode and loop mode.

[0053] The `m_signalling` member of the `tHrtimer` structure, which is the data structure of the linked list element node, indicates the execution method of the software timer service function as follows:

[0054] typedef struct tHrtimer

[0055] {

[0056] timer_t m_iTimerId; Timer ID number

[0057] struct tHrtimer*m_pNext; The next software timer in the software timer management list.

[0058] struct tHrtimer*m_pPrevArmed; The next timeout time in the software timer timeout management list.

[0059] struct tHrtimer*m_pNextArmed; The previous timeout time in the software timer timeout management list.

[0060] struct tHrtimer*m_pNextCallback; The next service function in the software timer service function management list.

[0061] tHrtimerSig m_signalling; Execution method of software timer service function

[0062] tHrtimerMode m_Mode; Software timer operating mode

[0063] struct timespec m_timeout; software timer timeout period

[0064] pTimerFunc*m_Func; pointer to the software timer service function

[0065] eTimerState m_state; Software timer running state

[0066] }tHrtimer;

[0067] To facilitate insertion, deletion, traversal, and sorting operations on the three linked lists, this method creates pointers to three linked lists: pTimersList (software timer management list), pArmedTimersList (software timer timeout management list), and pCallbackList (software timer service function management list). These pointers point to the current position in the linked list being accessed by the system. The specific data structures of the three linked list pointers are as follows: Figure 3 , Figure 4 and Figure 5 As shown, each linked list pointer's data structure contains a thread ID structure member, which represents the thread ID number of the thread that calls the linked list pointer to operate on the linked list. This method ensures the efficiency of operating and maintaining each linked list by opening up a dedicated thread to access each one separately.

[0068] The specific data structures of the three linked list pointers are as follows:

[0069] typedef struct pTimersList

[0070] {

[0071] int m_TaskID; Thread ID

[0072] tHrtimer*m_pTimers; The position pointed to in the current software timer management list.

[0073] tHrtimer*m_pFirstTimer; Header element node in the software timer management list

[0074] tHrtimer*m_pLastTimer; the last element node in the software timer management list.

[0075] pTimersList;

[0076] typedef struct pArmedTimersList

[0077] {

[0078] int m_TaskID; Thread ID

[0079] tHrtimer*m_pArmedTimers; The position pointed to in the current software timer timeout management list.

[0080] tHrtimer*m_pFirstArmedTimer; Header element node in the software timer timeout management list.

[0081] tHrtimer*m_pLastArmedTimer; the last element node in the software timer timeout management list.

[0082] pArmedTimersList;

[0083] typedef struct pCallbackList

[0084] {

[0085] int m_TaskID; Thread ID

[0086] tHrtimer*m_pCallback; The position pointed to in the current software timer service function management list.

[0087] tHrtimer*m_pFirstCallback; The head element node in the software timer service function management list.

[0088] tHrtimer*m_pLastCallback; A software timer service function that manages the last element node in the list.

[0089] pCallbackList;

[0090] Then, the timed services required by each thread in the system are... Figure 2 The linked list element node structure shown is inserted into the software timer management list. Taking the timer service Timer_A required by thread A as an example, its required working mode is single-shot mode, and the timing period is T. AUpon entering a timer interrupt, the function `FunctionA` needs to be executed. The software timer service function is executed using a semaphore-triggered execution method. This method creates a corresponding `tHrtimer` type structure variable `Hrtimer_A` based on the required timer service, adds basic information such as the timer service's working mode, timeout, interrupt execution function, and service function execution method to the `Hrtimer_A` structure variable, and inserts it at the tail of the linked list of the software timer management list `SoftTimerList`. Simultaneously, it modifies the corresponding linked list pointer `pTimersList`. This method supports multi-threaded access to the software timer management list within the system, allowing any thread to add the required timer service to the list. However, to prevent multiple threads from simultaneously performing insertion operations on the software timer management list, which could lead to errors, this method uses a mutex semaphore mechanism within the embedded real-time operating system to protect each thread from interference when inserting software timer element nodes into the software timer management list.

[0091] When the thread responsible for operating and maintaining the software timer management list SoftTimerList detects that a new element node has been added to the SoftTimerList, it passes the newly added element node to the thread responsible for managing and maintaining the software timer timeout management list SoftArmedTimerList. This thread will then insert the new element node into the correct position in the SoftArmedTimerList according to the timeout of the software timer. Since the SoftArmedTimerList needs to be traversed, sorted, inserted, and deleted frequently, and the time taken to perform these operations significantly affects the timing accuracy of the timer, this method uses a doubly linked list data structure to implement the SoftArmedTimerList. Compared to a singly linked list, the time taken to traverse and sort a doubly linked list is shorter, which can greatly reduce the latency caused by the above operations and improve the overall timing accuracy of the software timer.

[0092] If the system device has a high-precision hardware timer, then the timeout times in the software timeout management list SoftArmedTimerList are executed sequentially. If the system device cannot provide a high-precision hardware timer, the system timer provided by the real-time embedded system running on the device can be used to execute the timeout times in the software timeout management list SoftArmedTimerList. In this implementation, the system device does not have a high-precision hardware timer. Therefore, the high-precision system timer provided by the real-time embedded system running on the device is selected and the timeout times in the above list are executed sequentially.

[0093] When the high-precision timer reaches its timeout period, the high-precision timer interrupt service function is entered, and the following operations are executed in sequence:

[0094] 1) Load the corresponding software timer service function into the software timer service function management list;

[0095] 2) Based on the execution method of the software timer service function, notify the corresponding thread to execute the software timer service function in the corresponding manner;

[0096] 3) Load the next software timer timeout from the SoftArmedTimerList;

[0097] 4) Delete the software timer element nodes that have finished counting in the SoftArmedTimerList software timer timeout management list;

[0098] In this example, the software timer service function is executed using a semaphore-triggered method. Therefore, a semaphore is released in the high-precision timer interrupt service function to notify the thread responsible for managing the software timer service function management list (CallBackList) to execute the software timer service functions loaded into the list. When the thread responsible for operating and maintaining the CallBackList detects the trigger signal passed by the high-precision timer interrupt service function, it executes the corresponding service function in the order of the linked list. By moving the service functions with complex execution operations from the high-precision timer interrupt service function to a dedicated thread, the execution time within the high-precision timer interrupt service function is reduced, and the cumulative error caused by the high-precision timer frequently entering the interrupt service function is reduced, thereby ensuring the timing accuracy of the final software timing service.

[0099] After the thread responsible for maintaining the CallBackList, the software timer service function management list, finishes executing its corresponding software timer service function, it will perform different operations based on the software timer's timing service working mode. If the software timer is in single-time working mode, it will notify the thread responsible for maintaining the SoftTimerList, the software timer management list, to remove the software timer from the list. If the software timer is in cyclic working mode, it will calculate the next timeout and write it to the corresponding position in the software timer element node, and then pass the software timer element node to the thread responsible for maintaining the SoftTimerList. Upon receiving the new software timer element node, the thread responsible for maintaining the SoftTimerList will delete the old software timer element node from the list and add the new software timer element node to the end of the list.

Claims

1. A timer management method based on an embedded real-time operating system, characterized in that, Includes the following steps: Step 1: Create a software timer management list, a software timer timeout management list, and a software timer service function management list; Step 2: Add the timer services required by each thread in the embedded real-time operating system to the software timer management list in sequence; Step 3: Traverse the software timer management list, and insert each software timer into the software timer timeout management list in ascending order of timeout duration, based on the timeout duration of each software timer in the software timer management list. Step 4: Call the timer in the embedded real-time operating system to execute the timed service in the order of the timeout times in the software timer timeout management list; Step 5: When the timer counts to the timeout period, load the software timer service function corresponding to the timeout period into the software timer service function management list, and execute the next timeout period in the software timer timeout management list; Step 6: Execute the software timer service functions in the software timer service function management list, and return to Step 3.

2. The timer management method based on an embedded real-time operating system according to claim 1, characterized in that: The software timer management list and the software timer service function management list adopt a singly linked list structure, while the software timer timeout management list adopts a doubly linked list structure.

3. The timer management method based on an embedded real-time operating system according to claim 1, characterized in that: In step 2, multi-threaded access to the software timer management list within the system is supported, allowing any thread to add the required timer services to the software timer management list. At the same time, the software timer management list is protected by calling the mutex semaphore in the embedded real-time operating system, ensuring that only one thread operates on the software timer management list at any given physical moment.

4. The timer management method based on an embedded real-time operating system according to claim 1, characterized in that: In step 2, the supported timer service working modes include single-run mode and cyclic mode.

5. The timer management method based on an embedded real-time operating system according to claim 1, characterized in that: In step 4, the timer is the most accurate timer among all timers in the embedded real-time operating system.

6. The timer management method based on an embedded real-time operating system according to claim 1, characterized in that: In step 5, when the timer counts to the timeout period and enters the timer interrupt service function, the software timer interrupt service function corresponding to this timeout period is added to the software timer service function management list. According to the execution mode of the software timer service function, the corresponding thread is notified to execute the software timer service function. Then, the next timeout period is loaded from the software timer timeout management list and executed. At the same time, the software timers that have been executed in the software timer timeout management list are deleted.

7. The timer management method based on an embedded real-time operating system according to claim 1, characterized in that: In step 5, the supported software timer service function execution methods include interrupt callback trigger execution, semaphore trigger execution, and message queue trigger execution.

8. The timer management method based on an embedded real-time operating system according to claim 1, characterized in that: In step 6, after the corresponding software timer service function is executed, different operations are performed according to the timing service working mode of the software timer. If the software timer is in single-run mode, remove the software timer from the software timer management list, and then re-execute steps 3-6 in sequence. If the software timer is in cyclic mode, calculate the next timeout and add it back to the software timer management list, then repeat steps 3-6 in sequence.

9. A timer management device based on an embedded real-time operating system, characterized in that, It includes a memory and a processor; the memory is used to store a computer program; the processor is used to implement, when executing the computer program, a timer management method based on an embedded real-time operating system as described in any one of claims 1-8.

10. A computer-readable storage medium, characterized in that, The storage medium stores a computer program, which, when executed by a processor, implements a timer management method based on an embedded real-time operating system as described in any one of claims 1-8.

Citation Information

Patent Citations

  • Software timer implementation method for embedded system

    CN108845872A

  • Methods for reconciling interrupt conflicts

    CN109196470A