An implementation method and storage medium for operating a timer in an operating system

By adjusting the priority of the software timer thread when the hardware timer is interrupted and using a doubly linked list to manage the timer, the problems of slow system response and tick error caused by traditional software timers are solved, and a timer mechanism with high efficiency and high precision requirements is achieved.

CN115981814BActive Publication Date: 2026-07-07SUPCON TECH CO LTD

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
SUPCON TECH CO LTD
Filing Date
2022-12-23
Publication Date
2026-07-07

AI Technical Summary

Technical Problem

Traditional software timer implementations rely on the highest priority software timer thread, resulting in slow response times for other system threads, low timer management efficiency, and large system tick errors.

Method used

By determining whether the target timer has expired when the hardware timer is interrupted, and waking up the software timer thread when it is determined to have expired, adjusting its priority to be the same as the expired timer, and using a doubly linked list array to manage the timers, the efficiency of timer management is improved, and the tick timer business is executed in the hardware timer interrupt environment.

Benefits of technology

It improves the response speed of other threads in the system, reduces system performance consumption, solves the problems of inefficiency in timer management and system tick error, and realizes a timer mechanism with multi-precision requirements.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115981814B_ABST
    Figure CN115981814B_ABST
Patent Text Reader

Abstract

The application relates to an implementation method and a storage medium for operating a timer in a system, which comprises the following steps: when a hardware timer interrupts, judging whether there is an expired target timer; when it is determined that there is an expired target timer and the expired target timer is an expired software timer, waking up a software timer thread through a message chain table, adjusting the priority of the software timer thread to the same priority as that of the expired software timer, and executing the business corresponding to the expired software timer in the adjusted software timer thread; when it is determined that there is an expired target timer and the expired target timer is an expired beat timer, executing the business corresponding to the expired beat timer in the interrupt environment of the hardware timer, so that the priority of the software timer thread is dynamically adjusted according to the priority of the expired software timer, and the response speed of other threads of the system is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of operating systems, and more particularly to a method for implementing a timer in an operating system and a storage medium therein. Background Technology

[0002] In operating systems, a typical timer design model involves building a software timer layer on top of a hardware timer. Software timers have no precision differences, and their operations are executed within a software timer thread. Started software timers are managed by inserting them into an array or linked list. A typical timer design model manages a system tick, incrementing the tick by 1 each time a cycle signal is reached.

[0003] However, since the implementation of traditional software timers relies on a software timer thread with the highest priority, and the software timer thread cannot be adjusted, the response speed of other threads in the system is reduced. Summary of the Invention

[0004] (a) Technical problems to be solved

[0005] In view of the above-mentioned shortcomings and deficiencies of the prior art, the present invention provides a method for implementing a timer in an operating system and a storage medium, which solves the technical problem that the response speed of other threads in the system is relatively slow because the software timer thread cannot be adjusted.

[0006] (II) Technical Solution

[0007] To achieve the above objectives, the main technical solutions adopted by the present invention include:

[0008] In a first aspect, embodiments of the present invention provide a method for implementing a timer in an operating system. The method includes: step S1, when a hardware timer is interrupted, determining whether there is an expiring target timer; wherein the target timer includes a software timer and a tick timer; step S2, when it is determined that there is an expiring target timer and the expiring target timer is an expiring software timer, waking up the software timer thread through a message chain, and adjusting the priority of the software timer thread to the same priority as the expiring software timer, so as to obtain an adjusted software timer thread, and executing the business corresponding to the expiring software timer in the adjusted software timer thread; step S3, when it is determined that there is an expiring target timer and the expiring target timer is an expiring tick timer, executing the business corresponding to the expiring tick timer in the interrupt environment of the hardware timer.

[0009] Therefore, this embodiment of the application determines whether a target timer has expired when the hardware timer is interrupted. If a target timer has expired and is a software timer, the software timer thread is woken up via a message list, and its priority is adjusted to the same level as the expired software timer. This results in an adjusted software timer thread, in which the service corresponding to the expired software timer is executed. If a target timer has expired and is a tick timer, the service corresponding to the tick timer is executed within the hardware timer interrupt environment. Thus, the priority of the software timer thread dynamically adjusts with the priority of the expired software timer, preventing it from consistently occupying the highest priority and improving the response speed of other system threads.

[0010] In one possible embodiment, step S2 specifically includes: when it is determined that there is an expiring target timer and the expiring target timer is an expiring software timer, the expiring software timer is attached to the message chain, and the priority of the awakened software timer thread is adjusted to the same priority as the priority of the expiring software timer to be executed in the message chain, so as to obtain the adjusted software timer thread, and the business corresponding to the expiring software timer to be executed is executed in the adjusted software timer thread.

[0011] In one possible embodiment, step S1 specifically includes: when a hardware timer is interrupted, determining the first current system tick value of the operating system; determining the first target doubly linked list node corresponding to the first current system tick value from the doubly linked list; determining whether the expiration tick value of the nth target timer attached to the first target doubly linked list node is equal to the first current system tick value; where n is a positive integer greater than or equal to 1; if the expiration tick value of the nth target timer is equal to the first current system tick value, then determining that the nth target timer has expired.

[0012] In one possible embodiment, the doubly linked list includes multiple doubly linked list nodes, and each of the multiple doubly linked list nodes has a corresponding index;

[0013] The step of determining the first target doubly linked list node corresponding to the first current system beat value from the doubly linked list includes: performing a modulo operation on the first current system beat value and the total number of nodes in the multiple doubly linked list nodes to obtain a first modulo operation result; and taking the doubly linked list node whose index is equal to the first modulo operation result as the first target doubly linked list node.

[0014] In one possible embodiment, before step S1, the method further includes: sorting all target timers attached to each doubly linked list node according to the order of their expiration times;

[0015] Step S1 further includes: if the expiration timer value of the nth target timer is not equal to the first current system timer value, then it is determined that the nth target timer and subsequent target timers have not expired.

[0016] In one possible embodiment, the first current system clock value is determined based on the operating system's corrected clock, and the corrected clock is determined when the signal interval between the current interrupt signal and the last interrupt signal of the operating system is greater than a preset time; wherein, the preset time is used to represent the period of the clock signal.

[0017] In one possible embodiment, the process of mounting the target timer includes: calculating the expiration timer value of the target timer to be mounted based on the delay time of the target timer to be mounted and the second current system tick value of the operating system; determining the second target doubly linked list node for mounting the target timer to be mounted from the doubly linked list based on the expiration timer value of the target timer to be mounted; determining the specific mounting position of the target timer to be mounted under the second target doubly linked list node based on the expiration timer value of the target timer to be mounted; and mounting the target timer to be mounted based on the specific mounting position.

[0018] In one possible embodiment, based on the expiration tick value of the target timer to be attached, a second target doubly linked list node for attaching the target timer to be attached is determined from the doubly linked list, including: performing a modulo operation on the expiration tick value of the target timer to be attached and the total number of nodes of the multiple doubly linked list nodes to obtain a second modulo operation result; and taking the doubly linked list node whose index is equal to the second modulo operation result as the second target doubly linked list node.

[0019] In one possible embodiment, determining the specific mounting position of the target timer under the second target doubly linked list node based on the expiration tick value of the target timer to be mounted includes: calculating the tick difference between the expiration tick value of the m-th target timer mounted under the second target doubly linked list node and the expiration tick value of the target timer to be mounted; where m is a positive integer greater than or equal to 1; converting the tick difference into an unsigned integer array; determining whether the integer array is greater than or equal to a preset array; if the integer array is greater than or equal to the preset array, then determining the specific mounting position as after the m-th target timer; if the integer array is less than the preset array, then determining the specific mounting position as before the m-th target timer.

[0020] Secondly, embodiments of this application provide a storage medium storing a computer program, which, when executed by a processor, performs the method described in the first aspect or any optional implementation thereof.

[0021] Thirdly, embodiments of this application provide an electronic device, including: a processor, a memory, and a bus. The memory stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the memory via the bus. When the machine-readable instructions are executed by the processor, they perform the method described in the first aspect or any optional implementation of the first aspect.

[0022] Fourthly, this application provides a computer program product that, when run on a computer, causes the computer to perform the method in the first aspect or any possible implementation thereof.

[0023] To make the above-mentioned objectives, features and advantages to be achieved by the embodiments of this application more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description

[0024] To more clearly illustrate the technical solutions of the embodiments of this application, the accompanying drawings used in the embodiments of this application will be briefly introduced below. It should be understood that the following drawings only show some embodiments of this application and should not be regarded as a limitation of the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0025] Figure 1 A flowchart illustrating an implementation method for a timer in an operating system provided in an embodiment of this application is shown;

[0026] Figure 2 This illustration shows a method for attaching a target timer to a doubly linked list according to an embodiment of this application;

[0027] Figure 3A A schematic diagram of a method for periodically detecting an expiring target timer provided in an embodiment of this application is shown;

[0028] Figure 3B A schematic diagram of another method for periodically detecting an expiring target timer provided in an embodiment of this application is shown;

[0029] Figure 4 A schematic diagram of a message chain list provided in an embodiment of this application is shown. Detailed Implementation

[0030] To better explain and facilitate understanding of the present invention, the present invention will be described in detail below with reference to the accompanying drawings and specific embodiments.

[0031] Currently, existing timer implementation methods have at least the following drawbacks: Traditional software timers lack precision differentiation, making them unsuitable for periodic tasks with varying precision requirements; Traditional software timer implementations rely on a single, high-priority software timer thread, which cannot be adjusted, thus reducing the response speed of other system threads; Existing technologies rely on hardware timers sending signals to wake up the software timer thread each timer cycle to detect timer expiration, leading to frequent wake-ups and performance degradation; Traditional software timers are simply managed by inserting data into arrays or linked lists during startup, requiring the software timer thread to poll all started timers for expiration, increasing overhead; Each time a cycle signal arrives, the system tick increments by 1, leading to tick errors if the cycle signal is inaccurate.

[0032] Based on this, this application provides an implementation scheme for timers in an operating system. It utilizes a doubly linked list array to manage started software timers and tick timers, and determines whether a target timer expires when a hardware timer is interrupted. The target timers include both software timers and tick timers. When a target timer is determined to be expiring and is a software timer, the software timer thread is woken up via a message list, and its priority is adjusted to match that of the expiring software timer, resulting in an adjusted software timer thread. The corresponding service for the expiring software timer is then executed within this adjusted software timer thread. When a target timer is determined to be expiring and is a tick timer, the corresponding service for the expiring tick timer is executed within the hardware timer interrupt environment. Therefore, this application can solve the problems of single precision in the timing service execution environment, inefficient timer management, and slow convergence of system tick errors in operating systems.

[0033] To better understand the above technical solutions, exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present invention are shown in the drawings, it should be understood that the present invention can be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that the present invention can be understood more clearly and thoroughly, and that the scope of the present invention can be fully conveyed to those skilled in the art.

[0034] To facilitate understanding of the embodiments of this application, some terms involved in this application are explained below:

[0035] "Operating System": It is a system software program used to manage the low-level hardware resources of a computer and provide a set of interfaces for the development of high-level software;

[0036] "Thread": It is the actual unit of operation in the operating system;

[0037] "Hardware timer": This is an on-chip peripheral and a hardware resource. Furthermore, once the timer interrupt trigger cycle is configured, the hardware will issue an interrupt signal at the end of each cycle.

[0038] "Tick Timer": This refers to a timer whose timer functions are executed in a hardware timer interrupt environment;

[0039] "Software timer": This refers to a timer whose timer functions are executed in a thread environment;

[0040] "Message list": This refers to a mechanism used for thread communication;

[0041] "Software timer thread": This refers to a thread that executes software services;

[0042] "Doubly linked list array": It refers to an array where each element is the head node of a doubly linked list.

[0043] Please see Figure 1 , Figure 1 A flowchart illustrating an implementation method for a timer in an operating system, as provided in an embodiment of this application, is shown. Figure 1 As shown, this implementation method can be executed by a timer implementation device used in an operating system, and the specific device can be configured according to actual needs; the embodiments of this application are not limited thereto. For example, the implementation device can be a computer or a server, etc. Specifically, as... Figure 1 The implementation methods shown include:

[0044] Step S110: Add the target timer to be attached to the doubly linked list. The target timer includes software timers and tick timers.

[0045] Specifically, when starting a target timer, the expiration timer value of the target timer to be attached can be calculated based on the delay time of the target timer to be attached and the second current system clock value of the operating system. Furthermore, based on the expiration timer value of the target timer to be attached, a second target doubly linked list node for attaching the target timer to be attached is determined from the doubly linked list. Also, based on the expiration timer value of the target timer to be attached, the specific attachment position of the target timer to be attached under the second target doubly linked list node is determined. Finally, based on the specific attachment position, the target timer to be attached is attached.

[0046] It should be understood that the process for determining the second current system clock value can be set according to the actual needs, and the embodiments of this application are not limited thereto.

[0047] Optionally, the second current system clock value can be determined based on the operating system's correction clock, and the correction clock is determined when the signal interval between the current interrupt signal and the last interrupt signal of the operating system is greater than a preset time. The preset time represents the period of the clock signal, thus enabling this application to quickly converge system clock errors due to the clock correction function. The specific preset time can be set according to actual needs, and this application is not limited to this.

[0048] For example, based on the system clock providing a fundamental time reference for the entire operating system, each time a hardware timer interrupt occurs, and due to factors such as chip voltage, the signal interval between the current interrupt signal and the previous interrupt signal is 20 milliseconds. Furthermore, since an interrupt signal is normally generated every 10 milliseconds, the preset time can be set to 10 milliseconds. Therefore, the corrected clock speed can be determined as (20-10) / 10 = 1. Thus, the second current system clock speed value can be increased by 2, not by 1.

[0049] Correspondingly, the process of determining the first current system beat value can be similar. For details, please refer to the relevant description of the second current system beat value, which will not be repeated here.

[0050] It should also be understood that the specific process of calculating the expiration timer value of the target timer to be attached, based on the delay time of the target timer to be attached and the second current system tick value of the operating system, can be set according to actual needs, and the embodiments of this application are not limited thereto.

[0051] Optionally, the sum of the delay time of the target timer to be mounted and the second current system tick value of the operating system can be calculated, and the sum can be used as the expiration tick value of the target timer to be mounted.

[0052] For example, if the delay time of the target timer to be mounted is 90 milliseconds and the second current system tick value of the operating system is 110, since the system tick increases by 1 every 10 milliseconds, the delay time corresponds to 9 system ticks. Therefore, the expiration tick value of the target timer to be mounted is 119.

[0053] It should also be understood that the specific process of determining the second target doubly linked list node for attaching the target timer from the doubly linked list based on the expiration tick value of the target timer to be attached can be set according to actual needs, and the embodiments of this application are not limited thereto.

[0054] Optionally, when the doubly linked list includes multiple doubly linked list nodes, and each of the multiple doubly linked list nodes has a corresponding index, the expiration tick value of the target timer to be attached is moduloed by the total number of nodes in the multiple doubly linked list to obtain a second modulo operation result. The doubly linked list node whose index is equal to the second modulo operation result is taken as the second target doubly linked list node. The second target doubly linked list node is the head node of the linked list to which the target timer needs to be attached.

[0055] It should also be understood that the specific values ​​of multiple doubly linked list nodes and the index corresponding to each doubly linked list node can be set according to actual needs, and the embodiments of this application are not limited thereto.

[0056] For example, see Figure 2 , Figure 2 This diagram illustrates a method for attaching a target timer to a doubly linked list according to an embodiment of this application. Figure 2 The doubly linked list shown consists of 9 nodes, with indices ranging from 0 to 8. Each node can include a ".next" and a ".pre" field, where ".next" represents the address of the next node and ".pre" represents the address of the previous node.

[0057] To facilitate understanding of the process of determining the second target doubly linked list node, a specific embodiment is described below.

[0058] For example, see continue. Figure 2 If the expiration tick value of the target timer (e.g., software timer or tick timer) to be mounted is 119, the remainder of 119 divided by 9 (i.e. the total number of nodes in the doubly linked list) is 2. Then the third doubly linked list node with index 2 can be used as the second target doubly linked list node.

[0059] It should also be understood that the specific process of determining the specific mounting position of the target timer under the second target doubly linked list node based on the expiration tick value of the target timer to be mounted can also be set according to actual needs, and the embodiments of this application are not limited thereto.

[0060] Optionally, calculate the tick difference between the expiration tick value of the m-th target timer attached to the second target doubly linked list node and the expiration tick value of the target timer to be attached. Here, m is a positive integer greater than or equal to 1. Also, convert the tick difference into an unsigned integer array. And determine whether the integer array is greater than or equal to a preset array. If the integer array is greater than or equal to the preset array, determine that the specific attachment position is after the m-th target timer; if the integer array is less than the preset array, determine that the specific attachment position is before the m-th target timer.

[0061] It should also be understood that the specific method for converting the beat difference into an unsigned integer array can be set according to actual needs, and the embodiments of this application are not limited thereto.

[0062] For example, the beat difference can be converted into an unsigned 32-bit variable using an existing compiler.

[0063] It should also be understood that the specific values ​​of the preset array can be set according to actual needs, as described in this application embodiment.

[0064] For example, the preset data is 2 to the power of 31. Furthermore, this application also guarantees that the delay time of the target timer is less than or equal to 2 to the power of 31.

[0065] It should be noted that although the above description uses the m-th target timer as an example, those skilled in the art should understand that the embodiments of this application can sequentially determine the relative mounting positions of the target timer to be mounted and at least one target timer mounted under the second target doubly linked list node (for example, if p target timers are mounted under the second target doubly linked list node, the positions of the target timer to be mounted and the first target timer can be determined first, and the same method can be used to determine the positions of the target timer to be mounted and the second target timer, etc.), thereby determining the specific mounting position of the target timer to be mounted.

[0066] For example, after determining through the above steps that the specific mounting position is after the m-th target timer and before the (m+1)-th target timer, it can be determined based on the above information that the specific mounting position is between the m-th target timer and the (m+1)-th target timer.

[0067] To facilitate understanding of the process of determining the specific mounting location, a specific embodiment will be described below.

[0068] Specifically, see [link to relevant documentation] Figure 2When the second target doubly linked list node is the third doubly linked list node, and a Timer 1 is attached to the third doubly linked list node, this application needs to calculate the relative attachment position of the Timer 2 (i.e., the target timer) and Timer 1. Furthermore, the expiration tick value of Timer 1 can be determined to be 110, thus calculating the tick difference between the expiration tick value of Timer 1 (110) and the expiration tick value of Timer 2 (119) as -9, and converting this tick difference into an unsigned 32-bit value (i.e., 0xffffffffffffff7). It is then determined whether this unsigned 32-bit value is greater than or equal to 2 to the power of 31. Finally, if the determination shows that the unsigned 32-bit value is greater than 2 to the power of 31, Timer 2 can be attached after Timer 1.

[0069] In addition, if no target timer is attached to the second target doubly linked list node, the target timer that needs to be attached can be directly attached to the second target doubly linked list node without needing to determine its specific attachment location.

[0070] In addition, after attaching the target timer that needs to be attached, all the target timers attached to the second target doubly linked list node can be sorted in order of their expiration times.

[0071] For example, see continue. Figure 2 Given that the expiration time of Timer 1 is 110 and the expiration time of Timer 2 is 119, it can be determined that Timer 1 expires before Timer 2. Therefore, the order of the two timers after sorting is Timer 1 and Timer 2.

[0072] Furthermore, although the above description uses the sorting of all target timers attached to the second target doubly linked list node as an example, those skilled in the art should understand that all target timers attached to other doubly linked list nodes can also be sorted, that is, all target timers attached to each doubly linked list node can be sorted according to the order of their expiration times.

[0073] In other words, the timer design method of this application requires first using the expiration tick value of the target timer and the length of the doubly linked list to determine the head node of the doubly linked list to be attached when attaching the target timer. Then, the difference between the expiration tick value of each timer under the head node of the doubly linked list and the expiration tick value of the timer to be attached is calculated to arrange all target timers under the head node of the doubly linked list in ascending order of expiration time. Thus, finding the head node of the doubly linked list to be attached by using the remainder method improves the efficiency of the ascending order of timers. Furthermore, the ascending order of the target timers under the head node of the doubly linked list improves the efficiency of determining whether a timer has expired in the hardware timer interrupt.

[0074] Step S120: When a hardware timer is interrupted, determine whether there is an expired target timer.

[0075] Specifically, after sorting all target timers attached to each doubly linked list node, when a hardware timer interrupts, the first current system tick value of the operating system is determined, and the first target doubly linked list node corresponding to the first current system tick value is identified from the doubly linked list. It is then determined whether the expiration tick value of the nth target timer attached to the first target doubly linked list node is equal to the first current system tick value, where n is a positive integer greater than or equal to 1. If the expiration tick value of the nth target timer is equal to the first current system tick value, the nth target timer is determined to have expired, and the process continues to check whether the expiration tick value of the (n+1)th target timer is equal to the first current system tick value. If they are equal, the timer has expired; otherwise, it has not expired. This process continues until all timers under the first target doubly linked list node have been checked, at which point expired timers can be detected (or detection can stop if unequal target timers are detected). If the expiration tick value of the nth target timer is not equal to the first current system tick value, then the nth target timer and all subsequent target timers are determined not to have expired.

[0076] It should be understood that the specific process of determining the first target doubly linked list node corresponding to the first current system beat value from the doubly linked list can be set according to actual needs, and the embodiments of this application are not limited thereto.

[0077] Optionally, when the doubly linked list includes multiple doubly linked list nodes, and each of the multiple doubly linked list nodes has a corresponding index, the first current system beat value and the total number of nodes of the multiple doubly linked list nodes are moduloed to obtain a first modulo operation result, and the doubly linked list node whose index is equal to the first modulo operation result is taken as the first target doubly linked list node.

[0078] For example, see Figure 3A , Figure 3A A schematic diagram is shown illustrating a method for periodically detecting an expiring target timer according to an embodiment of this application. For example... Figure 3A As shown, when the system tick is 0, the system tick increments by 1 when the hardware timer interrupts, so the first current system tick value is 1. Also, the remainder when 1 is divided by 9 (the total number of nodes in the doubly linked list) is 1, meaning the second doubly linked list node with index 1 is the first target doubly linked list node. Furthermore, no target timer is attached to the second doubly linked list node with index 1, so there is no expired target timer at this time.

[0079] For example, please see Figure 3B , Figure 3B A schematic diagram of another method for periodically detecting an expiring target timer provided in an embodiment of this application is shown. Figure 3B As shown, when the system tick is 1, the system tick increments by 1 when the hardware timer interrupts, so the first current system tick value is 2. Furthermore, the remainder when 2 is divided by 9 (the total number of nodes in the doubly linked list) is 2, meaning the third doubly linked list node with index 2 is the first target doubly linked list node. Also, the third doubly linked list node with index 2 has Timer 1 and Timer 2 attached to it, and Timer 1's expiration tick is 110, while Timer 2's expiration tick is 119. Therefore, neither Timer 1's nor Timer 2's expiration tick is equal to the current first current system tick value, so there is no expiration target timer at this time.

[0080] Step S130: When it is determined that there is an expiring target timer and the expiring target timer is an expiring software timer, the software timer thread is woken up through the message chain and the priority of the software timer thread is adjusted to the same priority as the expiring software timer, so as to obtain the adjusted software timer thread, and the business corresponding to the expiring software timer is executed in the adjusted software timer thread.

[0081] Specifically, when it is determined that there is an expiring target timer and the expiring target timer is an expiring software timer, the expiring software timer can be attached to the message chain, and the priority of the awakened software timer thread can be adjusted to the same priority as the expiring software timer in the message chain, so as to obtain the adjusted software timer thread, and execute the business corresponding to the expiring software timer in the adjusted software timer thread.

[0082] For example, see Figure 4 , Figure 4 A schematic diagram of a message linked list provided in an embodiment of this application is shown. Figure 4 As shown, the message chain includes a message chain head node and multiple expired Timer 1 (i.e., software Timer 1) and Timer 2 (i.e., software Timer 2) attached to it. Then, a First-In-First-Out (FIFO) pattern can be used to attach the expired Timer 3 (i.e., software Timer 3) to the end of the message chain head node. Afterwards, it can be determined whether any software timer threads have been awakened.

[0083] If no software timer thread is already awakened, it can be awakened and its priority adjusted to the same level as Timer 1. This results in an adjusted software timer thread, within which the expired business logic corresponding to Timer 1 is executed. After executing the business logic corresponding to Timer 1, its priority can be adjusted to the same level as Timer 2. This again results in an adjusted software timer thread, within which the expired business logic corresponding to Timer 2 is executed. After executing the business logic corresponding to Timer 2, its priority can be adjusted to the same level as Timer 3. This again results in an adjusted software timer thread, within which the expired business logic corresponding to Timer 3 is executed. After executing the business logic corresponding to the three expired timers, the software timer thread can be closed (or blocked). The business logic corresponding to Timer 1 through Timer 3 can share a single software timer thread for execution.

[0084] If a software timer thread has already been woken up, there is no need to wake it up again. The woken software timer thread can be used to execute the relevant business of the timer that has expired. For details, please refer to the description above. It will not be repeated here.

[0085] It should be understood that the specific business of each expired software timer can be set according to actual needs, and the embodiments of this application are not limited thereto.

[0086] For example, the service corresponding to the first expired software timer could be that the light flashes once; and the service corresponding to the second expired software timer could be that the light flashes once more, etc.

[0087] In other words, this application can design a timer message list and a software timer thread, and the hardware timer can periodically determine whether a target timer has expired. If a software timer expires, the priority of the software timer thread is first modified according to the priority of the expired software timer, and then the blocked software timer thread is woken up through the message list. After the hardware timer interrupt exits, the software timer thread executes the expired software timer business. If a tick timer expires, the tick timer business can be executed directly in the hardware timer interrupt.

[0088] Step S140: When it is determined that there is an expiring target timer and the expiring target timer is an expiring tick timer, the service corresponding to the expiring tick timer is executed in the interrupt environment of the hardware timer.

[0089] Furthermore, if it is determined that there is no expired target timer, the process can return to step S120 to continue determining whether there is an expired target timer when the hardware timer is interrupted again.

[0090] Therefore, in order to address the different precision requirements of periodic services in the operating system, the timer model in this application includes a tick timer and software timers with different priorities, thus realizing a mechanism for multiple precision requirement levels.

[0091] Furthermore, the priority of the software timer thread in this application will be dynamically adjusted according to the priority of the software timer that expires, that is, the software timer thread will not always occupy the highest priority, thereby improving the response speed of other threads in the system.

[0092] Furthermore, to address the performance degradation caused by the hardware timer waking up the software timer thread every timer cycle to detect software timer expiration in the operating system, this application determines the expiration of the software timer within the hardware timer. Only upon expiration does it send a message to the software timer thread via a message chain, waking it up to execute the software timer's business logic. The message chain can have an unlimited number of expired software timers, and the sending efficiency is relatively fast.

[0093] Furthermore, in order to address the system performance consumption issue caused by the need to check all running software timers every time the target timer expires in the hardware timer in the operating system, this application sorts the software timers or tick timers in ascending order of their expiration time and inserts them into a doubly linked list array during the timer startup process. This allows the hardware timer to only need to check whether the first timer in the doubly linked list array has expired, thereby reducing system performance consumption.

[0094] Furthermore, in order to address system clock cycle errors, instead of simply incrementing the system clock cycle by 1 each time a periodic signal arrives, it simultaneously determines whether to add a correction clock cycle. This allows for rapid convergence of system clock cycle errors and improves system stability.

[0095] It should be understood that the above-described method for implementing a timer in an operating system is merely exemplary, and those skilled in the art can make various modifications based on the above method, and such modified solutions also fall within the protection scope of this application.

[0096] This application provides a storage medium storing a computer program, which is executed by a processor to perform the methods described in the embodiments.

[0097] This application also provides a computer program product that, when run on a computer, causes the computer to perform the method described in the method embodiment.

[0098] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0099] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, as well as combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions.

[0100] It should be noted that any reference numerals placed between parentheses in the claims should not be construed as limiting the claims. The word "comprising" does not exclude the presence of components or steps not listed in the claims. The word "a" or "an" preceding a component does not exclude the presence of a plurality of such components. The invention can be implemented by means of hardware comprising several different components and by means of a suitably programmed computer. In claims that enumerate several means, several of these means may be embodied by the same hardware. The use of the terms first, second, third, etc., is merely for convenience of expression and does not indicate any order. These terms can be understood as part of the component names.

[0101] Furthermore, it should be noted that in the description of this specification, the terms "one embodiment," "some embodiments," "embodiment," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Furthermore, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0102] Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the claims should be interpreted to include both the preferred embodiments and all changes and modifications falling within the scope of the invention.

[0103] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, then this invention should also include these modifications and variations.

Claims

1. A method for implementing a timer in an operating system, characterized in that, include: Step S1: When a hardware timer is interrupted, determine whether there is an expired target timer; wherein, the target timer includes a software timer and a tick timer; Step S2: When it is determined that there is an expired target timer and the expired target timer is an expired software timer, the software timer thread is woken up through the message chain, and the priority of the software timer thread is adjusted to the same priority as the priority of the expired software timer, so as to obtain the adjusted software timer thread, and the business corresponding to the expired software timer is executed in the adjusted software timer thread; Step S3: When it is determined that there is an expired target timer and the expired target timer is an expired tick timer, the service corresponding to the expired tick timer is executed in the interrupt environment of the hardware timer; Step S1 specifically includes: When the hardware timer is interrupted, the first current system tick value of the operating system is determined; Determine the first target doubly linked list node corresponding to the first current system beat value from the doubly linked list; Determine whether the expiration tick value of the nth target timer attached to the first target doubly linked list node is equal to the tick value of the first current system; where n is a positive integer greater than or equal to 1; If the expiration timer value of the nth target timer is equal to the current system timer value, then the nth target timer is determined to have expired. The doubly linked list includes multiple doubly linked list nodes, and each of the multiple doubly linked list nodes has a corresponding index; The step of determining the first target doubly linked list node corresponding to the first current system beat value from the doubly linked list includes: The first current system beat value and the total number of nodes in the multiple doubly linked lists are moduloed to obtain the first modulo operation result; The doubly linked list node whose index is equal to the result of the first modulo operation is taken as the first target doubly linked list node.

2. The implementation method according to claim 1, characterized in that, Step S2 specifically includes: When it is determined that there is an expiring target timer and the expiring target timer is an expiring software timer, the expiring software timer is attached to the message chain, and the priority of the awakened software timer thread is adjusted to the same priority as the expiring software timer to be executed in the message chain, so as to obtain the adjusted software timer thread, and the business corresponding to the expiring software timer to be executed is executed in the adjusted software timer thread.

3. The implementation method according to claim 1, characterized in that, Prior to step S1, the implementation method further includes: Sort all target timers attached to each doubly linked list node according to the order of their expiration times; Step S1 further includes: If the expiration timer value of the nth target timer is not equal to the first current system timer value, then it is determined that the nth target timer and all subsequent target timers have not expired.

4. The implementation method according to claim 1, characterized in that, The first current system clock value is determined based on the corrected clock of the operating system, and the corrected clock is determined when the signal interval between the current interrupt signal and the last interrupt signal of the operating system is greater than a preset time; wherein, the preset time is used to represent the period of the clock signal.

5. The implementation method according to claim 1, characterized in that, The mounting process for the target timer includes: Calculate the expiration timer value of the target timer to be attached based on the delay time of the target timer to be attached and the second current system tick value of the operating system; Based on the expiration tick value of the target timer to be attached, a second target doubly linked list node for attaching the target timer to be attached is determined from the doubly linked list; Based on the expiration tick value of the target timer to be attached, determine the specific attachment position of the target timer under the second target doubly linked list node; Based on the specific mounting location, the target timer to be mounted is mounted.

6. The implementation method according to claim 5, characterized in that, The step of determining a second target doubly linked list node for attaching the target timer from the doubly linked list based on the expiration tick value of the target timer to be attached includes: The second modulo operation result is obtained by performing a modulo operation on the expiration tick value of the target timer to be mounted and the total number of nodes of the multiple doubly linked list nodes; The doubly linked list node whose index is equal to the result of the second modulo operation is taken as the second target doubly linked list node.

7. The implementation method according to claim 6, characterized in that, The step of determining the specific mounting position of the target timer under the second target doubly linked list node based on the expiration tick value of the target timer to be mounted includes: Calculate the tick difference between the expiration tick value of the m-th target timer attached to the second target doubly linked list node and the expiration tick value of the target timer to be attached; where m is a positive integer greater than or equal to 1; Convert the beat difference into an unsigned integer array; Determine whether the integer array is greater than or equal to a preset array; If the integer array is greater than or equal to the preset array, then the specific mounting position is determined to be after the m-th target timer; If the integer array is smaller than the preset array, then the specific mounting position is determined to be before the m-th target timer.

8. A storage medium having a computer program stored thereon, characterized in that, The computer program is executed by the processor to perform the implementation method of a timer in an operating system as described in any one of claims 1-7.