Timed task execution method, electronic device, readable storage medium and program product
By constructing a timer data structure and a spinlock mechanism, the timed tasks are tightly integrated with coroutines, solving the problem of low efficiency in timed task management in existing technologies. This achieves efficient task scheduling and multi-threaded safety, improving system performance and resource utilization.
Patent Information
- Application Number
- CN202511255568.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-04
- Publication Date
- 2025-12-05
- Estimated Expiration
- 2045-09-04
AI Technical Summary
Existing technologies cannot efficiently utilize coroutines for task scheduling when managing large-scale scheduled tasks, resulting in poor execution performance. Furthermore, it is difficult to guarantee thread safety and tight integration with coroutines in a multi-threaded environment.
By constructing a timer data structure, the time and task dimensions are abstracted into data structures. By using skip lists and spin locks, the timed tasks and coroutines are tightly integrated, supporting fast insertion, searching, deletion, and sorting, and ensuring thread safety and efficient scheduling in a multi-threaded environment.
It improves the execution efficiency and scheduling flexibility of scheduled tasks, enhances the overall performance and resource utilization of the system, and ensures stable operation in high-concurrency scenarios.
Smart Images

Figure CN120803667B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, in particular to a timing task execution method, electronic device, computer readable storage medium and computer program product. BACKGROUND
[0002] The timing task is a task required to be executed at a specified time point or according to a certain period, and the timer manages the time and sequence of the timing task execution, and when the timing task expires, it is handed over to the corresponding coroutine for execution. When the related technology utilizes the timer to schedule each timing task, it cannot be closely combined with the execution coroutine of the timing task, cannot efficiently utilize the coroutine for task scheduling, and affects the execution performance of the timing task. SUMMARY
[0003] The present application provides a timing task execution method, device, electronic device, computer readable storage medium and computer program product, which can closely combine the timing task and the execution coroutine, efficiently schedule the timing task by utilizing the coroutine, and effectively improve the execution performance of the timing task.
[0004] To solve the above technical problems, the present application provides the following technical solutions:
[0005] The present application provides a timing task execution method, comprising:
[0006] The time dimension attribute item and the task dimension attribute item are used as data elements to construct the timer data structure, and the data elements of the timer data structure are initialized according to the current task execution parameter; the task dimension attribute item includes scheduling domain resource information and task scanning parameter; the task scanning parameter is a parameter for scheduling domain to scan the expiration time of each timing task in the domain.
[0007] When there is a new timing task, the task data structure is generated according to the running parameter information and the target execution coroutine parameter of the new timing task, and the task data structure is used as a task node and inserted into the timer data structure corresponding to the target scheduling domain to which the new timing task belongs.
[0008] During the checking of the expiration process of each timing task by the timer according to the check time interval of the time dimension attribute item, if the current parameter value of the task dimension attribute is a new timing task, the new timing task and the target execution coroutine are transferred to the respective task queue corresponding thereto.
[0009] The present application also provides an electronic device comprising a memory and a processor, wherein the processor is used to implement the steps of any one of the above timing task execution methods when executing the computer program stored in the memory.
[0010] The application further provides a computer readable storage medium, and the computer readable storage medium stores a computer program.
[0011] The application further provides a computer program product, which comprises computer programs / instructions, and the computer programs / instructions are executed by a processor to implement the steps of any of the timing task execution methods.
[0012] The technical scheme provided by the application has the advantages that the time management dimension and the task management dimension of the timer are abstracted into data structures, the timing task is abstracted into data structures according to the task running dimension and the task execution coroutine dimension, the task data structure corresponding to the timing task is embedded into the timer data structure as a data node of the timer data structure, the timing task is bound to the timer, the timing task can be implemented as a node to realize the fast insertion, searching, deletion and sorting of the timing task in the timer, efficient support is provided for large-scale timing task management, and the data structure serves as basic data support for coroutine scheduling. The timing task and the coroutine can be deeply bound and associated through the efficient data structure, the coroutine is prepared for wake-up when the timing task is checked to be due, the coroutine is added to a coroutine scheduling queue, the seamless connection of the coroutine and the timing task is realized, the close combination of the timing task, the timer and the coroutine is realized, the timing task is efficiently scheduled by using the coroutine, the characteristics of the coroutine, such as light weight and fast switching, are fully utilized, the execution efficiency and the scheduling flexibility of the timing task are improved, and the overall performance and the resource utilization of the system are effectively improved.
[0013] In addition, the application further provides corresponding electronic devices, computer readable storage media and computer program products for the timing task execution method, so that the method is more practical, and the electronic devices, the computer readable storage media and the computer program products have corresponding advantages. BRIEF DESCRIPTION OF DRAWINGS
[0014] In order to more clearly illustrate the technical scheme of the application or the related art, the following will briefly introduce the drawings needed to be used in the embodiments or the related art description. Obviously, the drawings in the following description are only some embodiments of the application, and other drawings can be obtained by those skilled in the art without creative effort.
[0015] Figure 1 A timing task execution method provided by the application;
[0016] Figure 2 A timing task concurrent execution control flowchart provided by the application;
[0017] Figure 3 A timing task deletion flowchart provided by the present application;
[0018] Figure 4 A node insertion flowchart provided by the present application;
[0019] Figure 5 A timing task addition flowchart provided by the present application;
[0020] Figure 6 A check time interval dynamic adjustment flowchart provided by the present application;
[0021] Figure 7 A timing task execution method provided by the present application in a framework of an exemplary application scenario;
[0022] Figure 8 A task node search flowchart provided by the present application;
[0023] Figure 9 A structure framework of a timing task execution device provided by the present application in an exemplary embodiment;
[0024] Figure 10 A structure diagram of an electronic device provided by the present application in an exemplary embodiment. DETAILED DESCRIPTION
[0025] In order to make the person skilled in the art better understand the technical solutions of the present application, the present application will be further described in detail below in combination with the drawings and specific embodiments. In the specification and the above-mentioned drawings, the terms "first", "second", "third", "fourth" and the like are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion. The term "exemplary" means "as an example, embodiment or illustration". Any embodiment described as "exemplary" herein is not necessarily interpreted as superior or better than other embodiments.
[0026] Timing tasks, i.e., tasks that need to be executed in a timely manner, exist widely in computer systems, embedded devices, network services, and the like. Related technologies use timers to detect whether there are expired timing tasks, and have many deficiencies in the face of large-scale timing tasks, high-concurrency scenarios, and the combination with coroutines. For example, a timer of a related technology manages timing tasks by using a linear linked list. The timer links timing task nodes in order of expiration time. Although the implementation is simple, when searching, inserting, and deleting tasks, the linear linked list needs to be traversed, and the time complexity is O(n), where n is the total number of task nodes. When the number of tasks is large, the efficiency of task execution is low, and the execution demand of timing tasks in large-scale timing task scenarios cannot be met. In addition, the combination with coroutines is not close enough, and the advantages of coroutines cannot be fully utilized. Another timer of a related technology manages timing tasks by using an array. This method stores timing tasks in different positions of the array according to time granularity. Although the search speed is improved in some cases, the size of the array is fixed, and the flexibility is poor. For tasks with a large time span, memory waste is likely to occur, and there are still efficiency problems when inserting and deleting tasks. In addition, in a multi-threaded environment, complex synchronization mechanisms are needed for operations on the array, thread safety cannot be guaranteed, and there are also deficiencies in the combination with coroutines, which cannot efficiently utilize coroutines for task scheduling.
[0027] Therefore, the timer of the related technology has the problems of being unable to be applied to large-scale timing task scenarios, being prone to data inconsistency in a multi-threaded environment, and being difficult to efficiently combine with coroutines, which cannot fully utilize the advantages of coroutines in concurrent programming. In view of this, the present application combines timing tasks and coroutines organically by designing an efficient data structure to improve the efficiency of task execution and scheduling. In addition, the present application combines coroutines with a concurrency control mechanism, which can ensure thread safety in a multi-threaded environment, implement a timer that is efficient, thread-safe, and closely combined with coroutines, and improve the performance and flexibility of the timer in large-scale tasks and high-concurrency scenarios. Various non-limiting embodiments of the present application will be described in detail below with reference to the accompanying drawings and specific embodiments. First, please refer to Figure 1 , Figure 1 A flowchart of a timing task execution method provided by the present embodiment can include the following content:
[0028] S101: Construct a timer data structure by taking time dimension attribute items and task dimension attribute items as data elements, and initialize the data elements of the timer data structure according to the current task execution parameters.
[0029] The time dimension attribute item at least includes a check time interval attribute, and the check time interval attribute is a time interval for checking whether a timer task is due. The task dimension attribute item at least includes scheduling domain resource information and task scanning parameters. The scheduling domain resource information includes which scheduling domain of which timer performs which scheduling task, each scheduling domain stores different types of tasks, and each scheduling domain is responsible for scanning whether each scheduling task in the domain reaches a task execution time through a scheduler, and pushes a task to be executed at the current time to the timer, that is, displays the corresponding parameter value of the task scanning parameter of the timer data structure. The task scanning parameter is a parameter for the scheduling domain to scan the expiration time of each scheduling task in the domain, which can include a scanning range parameter or a search range parameter for determining the scanning speed, a parameter pointing to a scheduling task that needs to be processed at the current time, that is, a scheduling task that reaches the task execution time. When the timer corresponding data structure is created, the related attribute values are initialized according to the current task execution parameters, such as the currently set check time interval value, the task scanning parameter value and the scheduling domain resource corresponding to the timer, such as the search range value. If the current task execution parameters include the numerical value of a data element of the timer data structure, the data element is assigned a default value, which is a preparation for the combination of the coroutine and the scheduling task.
[0030] S102: When there is a new scheduling task, a task data structure is generated according to the running parameter information of the new scheduling task and the target execution coroutine parameter, and the task data structure is inserted into the timer data structure corresponding to the target scheduling domain to which the new scheduling task belongs as a task node.
[0031] The new scheduling task is, for example, a timing data collection, a timing task scheduling, and a timeout processing. When a new scheduling task is created, the corresponding running parameter information, such as the expiration time and the execution times, can be determined according to the type of the scheduling task, such as a one-time scheduling task or a periodic scheduling task, and then the coroutine for executing the scheduling task is obtained. In order to facilitate description, the coroutine for executing the new scheduling task is defined as a target execution coroutine, and the parameters are used as data elements to generate a data structure corresponding to the scheduling task, that is, the task data structure of this step. This structure body combines the scheduling task data and the coroutine instance management into a single object, which can be used as a control block of the coroutine-level scheduling task. Then, the generated task data structure is inserted into the data structure of the timer to which it belongs as a node, and the corresponding coroutine information is associated. The coroutine is a kind of lightweight user-mode thread, which can realize cooperative multi-task scheduling in a program, and has the characteristics of small switching overhead and high resource utilization.
[0032] S103: In the process of checking each timing task expiration according to the time dimension attribute item inspection time interval of the timer, if the current parameter value of the task dimension attribute is a new timing task, the new timing task and the target execution coroutine are transferred to the respective corresponding task queue.
[0033] The timer is a scheduler responsible for managing the time and order of timing task execution. The timing task is a specific task class, which is the specific action of the timer execution. Each timer can include multiple task nodes, each of which can be executed by a coroutine or multiple coroutines. According to the inspection time interval set in S101, when the scheduling domain scans to the expiration of the task node, it is displayed at the corresponding parameter value of the timer data structure. The parameter of the timer data structure is periodically checked for expired tasks. For expired tasks, the new timing task is transferred to the target scheduling domain's to-be-executed task queue, the corresponding coroutine instance is woken up, and the target execution coroutine is added to the task scheduling queue of the coroutine scheduler, so that the timing task is handed over to the corresponding coroutine for execution. After the coroutine runs, the next round of scanning begins.
[0034] In the technical solution provided in this embodiment, the time management dimension and the task management dimension of the timer are abstracted into a data structure, the timing task is abstracted into a data structure according to the task running dimension and the task execution coroutine dimension, and the task data structure corresponding to the timing task is embedded as a data node of the timer data structure. The value of the timer data structure binds the timing task with the timer. Not only can the timing task be implemented as a node to realize the fast insertion, search, deletion and sorting of the timing task in the timer, but also can provide efficient support for large-scale timing task management as the basic data support of coroutine scheduling. Through the efficient data structure, the timing task and the coroutine can be deeply bound and associated. When the expiration of the timing task is checked, the corresponding coroutine is woken up and added to the coroutine scheduling queue, realizing the seamless connection of the coroutine and the timing task, and the close combination of the timing task, the timer and the coroutine. The timing task is scheduled efficiently by using the coroutine, so as to fully utilize the characteristics of the coroutine, such as light weight and fast switching, improve the execution efficiency and scheduling flexibility of the timing task, and effectively improve the overall performance and resource utilization of the system.
[0035] The above embodiments do not make any limitation on how to insert the task data structure as a data node into the corresponding timer data structure. Based on the above embodiments, the present application also provides an exemplary implementation, which can include the following contents:
[0036] The timing tasks of the scheduling domain are taken as task nodes, and the task execution time is taken as the key of the corresponding task node. A skip list structure of the scheduling domain is generated by sorting the task nodes according to the task execution time. Correspondingly, the task scanning parameter includes the number of skip list layers and a head node pointer. The head node pointer points to the first task node of the current arrival task execution time by scanning the corresponding skip list structure in the scheduling domain according to the number of skip list layers. Based on the new timing task as the task node of the target scheduling domain, the task data structure is inserted into the timer data structure, and the target scheduling domain updates the order of each task node in the domain. Correspondingly, when detecting whether there is an expired timing task, the timer only needs to check the head node pointer. That is, if the head node pointer of the task dimension attribute points to a new timing task, the new timing task is transferred to the target scheduling domain to the execution task queue, and the target execution coroutine is added to the task scheduling queue of the coroutine scheduler.
[0037] In the embodiment, the task scanning parameters at least include the number of skip list layers for determining the search rhythm of the skip list structure to which the scheduling domain scanning belongs and the head node pointer pointing to the first task node of the skip list structure. The head node pointer is used to determine the first task node of the current arrival task execution time by scanning the corresponding skip list structure according to the number of skip list layers of the scheduling domain. The task data structure integrates the skip list node, the timing task metadata and the coroutine instance management into a single object, which is both the skip list node and the control block of the coroutine-level timing task. Each scheduling domain has a skip list structure, which is generated by sorting all the timing tasks in the scheduling domain as task nodes and the task execution time as the key of the corresponding task node according to the task execution time. The skip list structure is a randomized data structure with an ordered linked list and multiple indexes, which can realize fast search, insertion and deletion. The skip list structure in this step is not "rewriting a new skip list", but organizing the timing tasks of the scheduling domain as "task nodes" directly into a skip list, that is, each timing task is a skip list node, and the key of the task node is the expiration execution time of the timing task. In this step, the timing task nodes sorted according to the task execution time are connected into a skip list, and the scheduling domain only needs to quickly locate the "earliest timeout" task along the uppermost index when scanning the skip list structure, without traversing all the tasks, which significantly reduces the central processor overhead. Specifically, at the structure level, the task structure body task_t (timing task) is embedded in node_t (node) at the head, which naturally becomes a skip list node. At the memory level, malloc(sizeof(task_t)+level*sizeof(node_t*)): malloc once to pack the task data and the skip list pointer array together + variable length forward[], so there is no need to allocate twice and no need to use pointer to address twice in task_t. Among them, malloc(sizeof(task_t) calculates the number of bytes occupied by all fixed fields in task_t, and level*sizeof(node_t*) represents the reserved space of the variable length forward[] (multi-level index pointer array) of the skip list node, each element is a node_t*, and there are level elements, that is, the size of the allocated memory = the size of the timing task itself + the size of the level pointers. If the concurrency is high, malloc can be replaced by memory pool, and read-write lock can be added. forward[] is a set of "right arrows" in each layer of the skip list structure. The skip list structure has multiple indexes, and each task node has a pointer to the next task node in the same layer, which is placed in an array according to the layer number, which is forward[].
[0038] From the above, the embodiment directly organizes the timing task in the scheduling domain as a task node into a skip list structure through the timing task of the scheduling domain, so that the timing task data structure can be a data node of the timer data structure, which can not only realize the fast insertion, search, deletion and sorting of the timing task in the timer as a skip list node, but also provide efficient support for large-scale timing task management as the basic data support of the coroutine scheduling, and when scanning the skip list structure, only needs to quickly locate the earliest task to be timed out along the uppermost index according to the number of skip list layers, without traversing all tasks, which significantly reduces the central processing unit overhead.
[0039] In order to adapt to data operation in a multi-thread environment and ensure multi-thread data consistency, based on the above embodiment, the application also provides a concurrent control implementation manner, which can include the following contents as shown in the following table. Figure 2
[0040] When receiving the task node operation request, the node spin lock corresponding to the task data structure is acquired through an atomic operation; if the state of the node spin lock is in an unoccupied state, the node spin lock is acquired, and the state of the node spin lock is marked as an occupied state; the global spin lock corresponding to the corresponding timer data structure is acquired through an atomic operation, if the global spin lock is in an unoccupied state, the global spin lock is acquired, and the state of the global spin lock is marked as an occupied state; the corresponding attribute of the task data structure is operated according to the task node operation request, and after completing the task node operation request, the global spin lock and the node spin lock are released through an atomic operation.
[0041] In the embodiment, the task dimension attribute item of the timer data structure also includes a global spin lock attribute, and the task data structure also includes a node spin lock attribute. When a thread fails to acquire a spin lock, it will not enter a blocked state, but will wait for the release of the spin lock in a loop, which is suitable for scenarios where the lock holding time is short. The global spin lock is defined and initialized, and is locked before and after all program interfaces that will touch the skip list structure, the timer state and the timer structure, that is, the lock is taken before entering the critical section and is released after leaving the critical section, so as to ensure that the lock granularity covers the "entire structure", rather than only protecting a certain linked list, so as to ensure that concurrent threads / central processing units do not interfere with each other.
[0042] When a spin lock is associated with each timing task node as a node-level synchronization mechanism, a global data structure level spin lock is defined for the synchronization of overall data structure operation. Before operating on a task node, operations such as insertion, deletion, and state modification, the spin lock of the node is acquired through an atomic operation, which ensures that the lock acquisition behavior cannot be interrupted. After the atomic operation starts, either all operations are completed or none are performed, which can ensure the consistency of data operations in a multi-threaded environment and avoid conflicts caused by simultaneous modification of the lock state by multiple threads. For example, atomic instructions such as atomic_lock can be used to acquire the lock. If the lock is in an unoccupied state, it is immediately acquired and marked as occupied. If it is already occupied, it enters a loop and waits (spins). After successfully acquiring the lock for the task node, specific operations are performed on the task node, such as modifying the expiration time, marking the deletion state, updating the execution count, etc. At this time, other threads spin and wait due to the inability to acquire the lock, ensuring the exclusivity of the operation. When the operation is completed, the spin lock is released through an atomic operation, allowing other threads to acquire the lock and perform subsequent operations.
[0043] As can be seen from the above, the embodiment introduces a spin lock and atomic operation concurrency control mechanism based on data structure operation, ensuring the atomicity of data structure and node operation in a multi-threaded environment, effectively avoiding data inconsistency problems, ensuring operation safety in a multi-threaded environment, and ensuring stable and reliable operation of the coroutine timer in a high-concurrency scenario. The resource competition between coroutines is effectively controlled, and resource access between coroutines is orderly.
[0044] Based on the above embodiment, the present application also implements the implementation process of the task cancellation operation based on task node locking. The task node operation request is a timing task cancellation instruction, and the task data structure further includes a soft deletion marker attribute, such as Figure 3 and Figure 5 As shown, it can include the following content:
[0045] According to the timing task cancellation instruction, the soft deletion marker attribute of the task data structure is updated to a task cancellation identification value to complete the task cancellation operation on the task data structure. When the target execution coroutine is detected to be recycled, the task data structure is deleted from the timer data structure, and the memory resources occupied by the task data structure are released, and the resources of the target execution coroutine are cleaned up.
[0046] In the embodiment, the soft deletion flag attribute of the task data structure is updated to the task cancellation identification value, such as 1, by calling the soft deletion indication kucoro_timer_task_cancel of the timing task, indicating that the timing task is in the state of being cancelled or deleted, and when the parameter is legal, the spin lock is added to the task data structure, the global spin lock is added to the corresponding timer data structure, and then the deletion instruction kucoro_sl_delete of the timer data structure is called, and the soft deletion instruction kucoro_sl_takeout_without_delete is called to realize the logical deletion node. After the coroutine is executed, the cleaning logic of the task node is defined, including judging whether the node needs to be destroyed, releasing the node memory and related coroutine resource operation.
[0047] As can be seen from the above, the embodiment supports the cancellation operation of the task during the execution process, and the data structure node information is updated or the node is removed accordingly. After the coroutine is executed, the related coroutine resources are cleaned, the resource waste and memory leakage are avoided, the coroutine timer can be flexibly adapted to different task requirements and system states, and the scheduling strategy of the coroutine is optimized.
[0048] Based on the above embodiment, the present application also realizes the implementation process of the task node insertion operation on the basis of the task node locking, which can include the following contents as shown in Figure 4 and Figure 5 .
[0049] When the task node insertion instruction kucoro_time_task_add is received, when the instruction parameter is legal, the random layer generation instruction random_level is called, the node creation instruction creat_node is called to create the timing task node, the node expiration time is determined according to the first node expiration time and the task period length, the global spin lock corresponding to the timer data structure is obtained through the atomic operation, if the global spin lock is in the unoccupied state, the global spin lock is obtained, and the state of the global spin lock is marked as the occupied state, the node insertion instruction kucoro_sl_insert is called to insert the timing task node, the node initialization instruction sl_node_init is called to initialize the timing task node, and the global spin lock is released through the atomic operation.
[0050] As can be seen from the above, the embodiment supports the node insertion operation of the task during the execution process, and the data structure node information is updated accordingly, so that the coroutine timer can be flexibly adapted to different task requirements and system states, and the scheduling strategy of the coroutine is optimized.
[0051] Based on the above embodiment, the application is also directed to the implementation process of the task parameter adjustment operation based on the task node locking, the task node operation request is a running parameter reset instruction, the task data structure includes a first expiration time reference value and a task period length, and can include the following contents:
[0052] According to the running parameter reset instruction, it is determined that the running parameter to be reset is a new task execution period length, according to the new task execution period length value, the total number of current task execution periods and the first expiration time reference value, the next node expiration time is determined, the next node expiration time and the new task execution period are converted into corresponding time base values, and the corresponding attribute values of the task data structure are updated correspondingly, and the sleep time of the coroutine is adjusted, so as to complete the running parameter reset operation of the task data structure.
[0053] In this embodiment, for periodic tasks or timing tasks with remaining execution times, after one execution is completed, the expiration time is recalculated and inserted into the data structure, and the state of the coroutine is coordinated to ensure that the coroutine can be awakened in time next time.
[0054] As can be seen from the above, the embodiment supports the parameter reset operation of the task during execution, so that the coroutine timer can flexibly adapt to different task requirements and system states, ensures that system resources are reasonably utilized, optimizes the scheduling strategy of the coroutine, and provides resource guarantee for continuous and efficient operation of the coroutine.
[0055] Based on the above embodiment, the application also provides a dynamic parameter adjustment implementation process, which can include the following contents:
[0056] When the resource required by the task check of the timer is greater than the preset resource threshold, the current check time interval value is adjusted to be lower; the new check time interval value is converted into a new time base value, the new time base value is assigned to the check time interval attribute of the timer data structure, and the time when the last time of the timer checks the expiration of each timing task is converted into a corresponding time base value, and the last check time attribute of the timer data structure is updated.
[0057] When the task node retrieval speed improvement demand is received, the current skip list layer number is adjusted to be higher within the maximum depth range of the skip list structure; the new skip list layer number is assigned to the skip list layer number attribute of the timer data structure.
[0058] In this embodiment, the time dimension attribute item also includes the last check time, such as Figure 6As shown, the embodiment is used for dynamically adjusting the check time interval of the timer. The timer data structure assigns the check time interval attribute of the timer data structure with the corresponding tick (time base) number converted from the check time interval when the timer data structure is created. During the running process, the check frequency can be optimized according to the system running condition, and when it is found that the task check is too frequent to cause excessive performance overhead, the value of the global check interval can be adjusted, and the coroutine timer uses the new check time interval during the next check, so as to realize the dynamic setting of the check interval. When a faster retrieval speed is required during the running process, the value of the skip list layer number can be adjusted, and the adjustment range of the skip list layer number and the check time interval can be flexibly selected according to the actual condition, and the present application does not make any limitation on this.
[0059] As known from the above, the embodiment dynamically adjusts the two global variables of the check time interval and the skip list layer number, so that the coroutine timer can flexibly adapt to different task requirements and system states, avoids the performance overhead caused by frequent checks while ensuring that the tasks are triggered in time, improves the system efficiency, and matches the coroutine scheduling with the system state. Through the global variable and the function setting, the dynamic adjustment of the check interval of the coroutine timer is realized, the system performance is optimized, and the coroutine scheduling is more reasonable.
[0060] Based on the above embodiment, the present application further provides an exemplary implementation of the task data structure, which can include the following contents:
[0061] The running parameter attribute item and the coroutine attribute item are taken as data elements to construct the task data structure; the running parameter attribute item at least includes a main structure attribute and a node time attribute, and the coroutine attribute item at least includes a task retrieval parameter attribute, a task type attribute and a task execution coroutine attribute; according to the running parameter information of the new timing task, the task execution time and the task description information of the new timing task are determined, the task execution time is filled into the node time attribute, the business layer task description information pointer is determined according to the task description information, and is correspondingly filled into the task type attribute; according to the target execution coroutine parameter of the new timing task, the target execution coroutine for executing the new timing task is determined, and the task execution coroutine attribute is determined according to the target execution coroutine; the timer data structure to which the new timing task belongs is determined according to the target scheduling domain to which the new timing task belongs, and is correspondingly filled into the main structure attribute to obtain the task data structure; the retrieval range parameter used for the next task retrieval is determined according to the task retrieval parameter, and is filled into the task retrieval parameter attribute to obtain the task data structure of the new timing task.
[0062] In the embodiment, all runtime information of the timing task node is packaged in a piece of memory, realizing the lightweight timer design of "one node, multiple instances, shared timing, and independent execution". When the target execution coroutine has only one, the target execution coroutine parameter is the target execution coroutine; when the target execution coroutine has multiple, the task execution coroutine attribute includes a coroutine instance linked list, a current running coroutine instance pointing identifier, and a current unexecuted coroutine instance number. The coroutine instance linked list is generated according to the target execution coroutine and is filled into the corresponding position of the task execution coroutine attribute. In the process of processing the new timing task by the first target execution coroutine, the handle of the first target execution coroutine is used as the current running coroutine instance pointing identifier, which is used as the parameter value of the current running coroutine instance pointing identifier of the task execution coroutine attribute, and the current unexecuted coroutine instance number is updated. When the scheduling domain organizes the timing tasks into a skip list structure, the search range parameter can be the number of skip list layers.
[0063] For example, the coroutine attribute item further includes a node state attribute. When the task data structure is inserted into the timer data structure, the node state attribute is updated to the node added state. When the target scheduling domain scans the skip list structure in which the new timing task is located according to the number of skip list layers, when the head node pointer points to the new timing task, the node state attribute is updated to the scheduling preparation state. When the new timing task is transferred to the to-be-executed task queue of the target scheduling domain, the node state attribute is updated to the queue waiting state. When the new timing task is in the process of being processed by the target execution coroutine, the node state attribute is updated to the node running state. Through the node state, debugging, tracking, and prevention of repeated queuing can be facilitated, and visual tracking can be supported.
[0064] For example, the new timing task is a periodic execution task, and the node time attribute can include a node expiration time attribute, a first expiration time attribute, a task period length attribute, and a node trigger execution attribute. According to the task period length and the first expiration time of the runtime parameter information of the new timing task, the next node expiration time is determined, and the corresponding attribute values of the node time attribute are updated. Each time the new timing task is executed by the target execution coroutine, the next node expiration time is determined according to the value of the first expiration time attribute, the value of the task period length attribute, and the total number of triggered executions, and the node trigger execution attribute and the node expiration time attribute are updated.
[0065] Exemplarily, the new timing task is a fixed-time timing task, the running parameter attribute item further comprises a node trigger execution attribute, the total number of times that the new timing task is triggered for execution at the current time is obtained, and the next node expiration time is determined; the node trigger execution attribute is updated according to the total number of times and the total number of task executions of the new timing task, the node time attribute is updated according to the next node expiration time, and the updated task data structure is inserted into the timer data structure again, and the sleep time of the coroutine is adjusted.
[0066] Exemplarily, the running parameter attribute item further comprises a soft deletion flag attribute, if the total number of times and the total number of task executions of the new timing task are the same, the soft deletion flag attribute is updated to a to-be-deleted state identifier value; when it is detected that the target execution coroutine is recycled, the task data structure is deleted from the timer data structure, and the memory resources occupied by the task data structure are released, and the resources of the target execution coroutine are cleaned up.
[0067] In the embodiment, the number of times of execution of the timing task is managed, and the number of times of execution of the associated coroutine is recorded. Whether the task has remaining times is determined according to the remaining execution times attribute of the task, and the attribute value is updated after each execution. When the times are exhausted, the task is marked as a to-be-deleted state, and the scheduler cleans up after the coroutine is executed, supporting multiple modes such as one-time task, limited-time task and unlimited-time task, so that the execution of the coroutine and the management of the number of times of the task are synchronized.
[0068] As can be seen from the above, the embodiment supports real-time updating of node states, periodic task processing and execution times management, so that the coroutine timer can flexibly adapt to different task requirements and system states. The resource cleaning work after task cancellation can clean up the related resources of the cancelled task in time, ensure that the system resources are reasonably utilized, avoid resource waste and memory leakage and the like, and provide resource guarantee for continuous and efficient operation of the coroutine.
[0069] Exemplarily, the present application also provides an exemplary data structure of a timer and a timing task, which can include the following contents:
[0070] The timer data structure can be defined as struct kucoro_timed_cotask, which can also be referred to as co_timer_t.
[0071] typedef(definition)struct kucoro_timed_cotask{
[0072] int sl_lock;
[0073] u64 last_check_time;
[0074] u64 check_interval;
[0075] void *domain;
[0076] unsigned int curr_skiplist_depth;
[0077] kucoro_timed_cotask_t *head;
[0078] } co_timer_t;
[0079] Wherein, the timer data structure is used for implementing a timing task scheduler with a timeout base, which encapsulates "lock + time base + scanning interval + skip list" together, so as to combine "time dimension management" and "task dimension management" in one object, and provide an efficient and concurrent safe timing task management capability for the upper layer. The timer scans whether each timing task node of the timer data structure is expired according to the check time interval (check_interval); safely inserts, deletes and triggers the task in the multi-thread / multi-core concurrent environment; quickly locates the earliest task to be expired through the skip list structure (skip list); and supports multiple scheduling domains (domain), each domain maintaining its own skip list structure head.
[0080] Wherein, sl_lock (global spin lock): protect the entire `co_timer_t` structure, so that concurrent threads / central processing will not interfere with each other when inserting, deleting, scanning tasks. last_check_time (last check timestamp): record the system tick value when the last time the "timer scan function" is called, used to determine "whether it has passed check_interval". check_interval (check interval): indicates how many ticks to trigger a global scan every time, to prevent the central processing unit from idling or too frequent polling. void*domain (representing the scheduling domain pointer): points to the higher layer "scheduler domain" object, which facilitates hanging multiple timers in the same scheduler to be managed uniformly. curr_skiplist_depth (current skip list depth: the maximum number of skip list layers can dynamically grow / shrink, but not more than the macro MAX_SKIPLIST_DEPTH (maximum depth of skip list structure); the higher the number of layers, the faster the skip list search. kucoro_timed_cotask_t*head (skip list head node pointer): points to the topmost head node of the skip list, through which the earliest task node to be timed out can be located. The scheduling domain refers to a place specifically designated to store a task, and different businesses do not interfere with each other, and can be load balanced, prioritized, statistically or power managed by domain); the scheduling domain pointer directly points to the address of the scheduling domain where this task is stored, and each scheduling domain has a skip list, and the head node pointer points to the first node of the skip list (timed task).
[0081] Wherein, it can be defined as struct kucoro_timed_cotask_node, which can be represented as:
[0082] struct kucoro_timed_cotask_node{
[0083] u64 expire;
[0084] struct kucoro_timed_cotask_node *sl_next[MAX_SKIPLIST_DEPTH];
[0085] kucoro_task_info_t_tmp *biz_task;
[0086] void*co;
[0087] kucoro_instances_of_timed_task instance_list;
[0088] u64 co_instance_cnt;
[0089] int sl_node_lock;
[0090] u64 execute_cnt;
[0091] co_timer_t *timer_list;
[0092] uint8_t canceled;
[0093] int state;
[0094] u64 first_timeout;
[0095] u64 interval;
[0096] }.
[0097] The structure of the task data structure of the embodiment integrates the skip list node, the timing task metadata, the coroutine instance management, and the concurrency control into a single object, and all the fields jointly pack the "skip list index capability + business callback capability + coroutine concurrency capability + statistical monitoring capability" into a 64-byte aligned node. In this way, the structure can be used to locate in the skip list at high speed by using expire + sl_next[], and can be used to drive the coroutine life cycle by using the fields of instance_list, co_instance_cnt, and state, thereby realizing the lightweight timer design of "one node, multiple instances, shared timing, and independent execution". Among them, the data elements of expire + sl_next in the structure can realize the fast advantage, the data elements of first_timeout + interval + execute_cnt in the structure can realize the saving advantage, the data elements of biz_task + co + instance_list in the structure can realize the accurate advantage, and the data elements of sl_node_lock + canceled + state in the structure can realize the stable advantage.
[0098] Wherein, expire (the node in the current skip list ordering key), the key value used for skip list ordering, is equal to first_timeout + interval * N (first or Nth trigger), and the skip list structure is linked from small to large according to this time. The scheduler takes the leftmost node each time to know the next task node to the point, thereby ensuring to take the earliest timeout task. For periodic timing tasks, the value can be reused in place. first_timeout (first timeout reference value) indicates that the periodic task is reset each time, and through the parameter, there is no need to re-malloc, avoiding frequent malloc. Interval (period length (tick)) calculates the next expire together with first_timeout, thereby realizing various timing strategies such as one-time, periodic, and exponential backoff. In the periodic task scenario, after each trigger, the next expire can be recalculated using first_timeout and interval, and the node can be reused in place without re-malloc, avoiding frequent malloc. The precision units of expire, first_timeout, and interval are all ticks, and the clock sources seen by all central processors / threads must be consistent. struct kucoro_timed_cotask_node *sl_next [MAX_SKIPLIST_DEPTH] (multi-level index pointer array), 0~MAX_SKIPLIST_DEPTH-1: each layer has a next pointer, maintaining the shape of the skip list, thereby making the lookup, insertion, and deletion maintain O (logN), much faster than the O (N) of the linked list. sl_next [MAX_SKIPLIST_DEPTH]: skip list structure, length fixed to the maximum number of layers; when inserting or deleting the skip list, only these pointers are changed, without involving business fields. kucoro_task_info_t_tmp *biz_task (pointer to business callback description): save the real timeout processing function, parameter, priority, and other metadata, and through the parameter, the task node can be decoupled from the business, and the same node can be shared by multiple coroutine instances, reducing memory copying. biz_task: points to the business layer task description (timeout callback function, parameter, priority, etc.). A kucoro_timed_cotask_node can correspond to a business task, or can be shared by multiple coroutine instances. co represents the single coroutine handle currently executing the timing node, and if there is only one instance in instance_list (coroutine instance linked list), co is equal to it; if there are multiple instances queued, co points to the running coroutine at the head of the linked list. void *co (the coroutine handle currently running this timing logic) triggers the timeout, knows which coroutine to hand over the CPU to continue execution, thereby realizing zero-copy wake-up and avoiding searching the coroutine table again.kucoro_instances_of_timed_taskinstance_list (TAILQ head): Hang all coroutine instances under the current node, support "one timed node -> N coroutines" broadcast mode, through this parameter, the number of task nodes can be saved, and a large number of tasks can be scanned only once. instance_list (TAILQ): Allow multiple coroutine instances to share the same timing node. All instances are hung under this linked list. co_instance_cnt (coroutine instance counter that has not run completely) can quickly determine "how many instances have not run completely"; when co_instance_cnt falls to 0, the node can be recycled or reset. When the count is zero, the node can be safely recycled or reset to prevent premature release. This parameter can avoid traversing the linked list to determine whether it is idle. sl_node_lock (node-level spin lock): Only protect the fields of the current node (internal state, instance list, execute_cnt, etc. concurrent fields), the granularity is finer than the global lock, multiple nodes can be modified by different central processors at the same time, reducing global lock contention. execute_cnt (node trigger count): Statistics / monitoring, frequency limiting, and periodic task calculation of the next trigger. Periodic tasks can use this to implement "maximum trigger N times" or "charge by the number of times" strategies. state (node state attribute) describes the four stages of a single kucoro_timed_cotask_node in its life cycle, which can include added (node added state) -> sched_checking (scheduling preparation state) -> waiting_in_queue (queue waiting state) -> running_in_co (node running state). Among them, Added is used to indicate that the node has just been inserted into the skip list by timer_add(), but has not yet entered the main scanning path of the scheduler. sched_checking: The scheduler is scanning the skip list and finds that the expire of this node has arrived, and is preparing to transfer it from the "index structure" to the "execution queue". waiting_in_queue: The node has been put into the "ready / waiting queue", and all coroutine instances hanging under this node are waiting to be really taken by the scheduler. running_in_co at least one coroutine instance is executing the callback function corresponding to the node; at this time, the node is marked as "running", to prevent repeated enqueueing or concurrent triggering. When the callback ends and the instance count is zero, the node will return to added (periodic task) or be canceled (one-time task), completing a life cycle. canceled (soft deletion flag): Set to 1 to indicate that it has been canceled, the scheduler will skip it during scanning, and the node will remain in the skip list (but the skip list node will not be removed for the time being), avoiding the complexity of concurrent deletion and the complexity of rebalancing caused by removing the node from the skip list in the middle. Real deletion can be delayed for batch recycling.co_timer_t *timer_list (back to the scheduler): the node can quickly find the scheduling domain, statistics, configuration information, without additional hash lookup, direct pointer jump. timer_list (back to) represents the co_timer_t (timer) to which the task node belongs, which facilitates quick positioning of the scheduling domain, statistical information, etc. in the callback.
[0099] As can be seen from the above, the embodiment adopts an efficient data structure to efficiently manage timing tasks, combines concurrent control means such as spin locks, atomic operations, etc. to ensure thread safety in a multi-threaded environment, organically combines the timing tasks with coroutines to improve task execution and scheduling efficiency, and at the same time designs mechanisms such as periodic task processing, task resetting, dynamic adjustment of checking intervals, task cancellation and cleaning, and execution number management, to realize efficient and flexible operation of the coroutine timer.
[0100] Finally, the present application also provides an exemplary embodiment, as shown in Figure 7 The timing task execution system can include a time and time base conversion module, an efficient data structure management module, a concurrent control module, a coroutine depth combination module, and a task processing module.
[0101] The time and time base conversion module realizes the processing of parameters that need to be converted from time to time base value in the system by encapsulating the coroutine time consumption measurement instruction, the main frequency acquisition instruction, and the time base-time conversion instruction. The coroutine time consumption measurement instruction is used to measure the actual execution time consumption of the coroutine, for example, kucoro get ticks can be used. The main frequency acquisition instruction is used to return the central processing unit frequency (Hz), that is, the number of ticks per second, which is used to convert the number of ticks into real time (seconds, milliseconds), for example, kucoro get hz can be used. The time base-time conversion instruction, for example, convert us to ticks, converts microseconds (µs) into central processing unit ticks. The efficient data structure management module includes two data structures, a timer data structure and a task data structure, which are used to create, destroy, and perform operations such as insertion, search, and deletion of task nodes based on the two data structures. The efficient data structure management module defines the structure of the timer data structure and the task data structure representing the data structure node. The node can be created through the create node function, the node can be inserted into the data structure through the insert node function, and the node can be searched through the search node function, as shown in Figure 8As shown, a node can be deleted by a delete node function and destroyed by a destroy node function. The efficient data structure management module implements efficient management of the timing task through these functions and provides data support for the scheduling of the coroutine. The concurrency control module provides a global spin lock, a node spin lock, and atomic operations. When operating on the data structure, in the processes of timer running, task cancellation, and task resetting, the atomic operations are used to lock the timer data structure and the task node, ensuring the atomicity of the modification of the data structure and the node state when concurrently accessed, preventing race conditions between coroutines, ensuring the thread safety of the operation on the data structure and the node in a multi-threaded environment, and guaranteeing the consistency of the coroutine when accessing and operating these resources. The coroutine depth combination module implements the deep association and management of the timing task and the coroutine by encapsulating coroutine instance creation instructions, coroutine instance linked list printing instructions, coroutine recycling or re-shooting instructions, and duplicate checking instructions. The coroutine instance creation instruction is used to create a timing coroutine instance, such as kucoro co t, where "co"=coroutine and "t"=timer. The object address and timeout time are printed in trace. The coroutine instance linked list printing instruction is used to print the current survival coroutine instance linked list, which is convenient for debugging leaks or deadlocks, such as instance list. The coroutine recycling or re-shooting instruction is used to return the control right to the scheduler when a timing coroutine has finished running, preparing for recycling or re-shooting, such as timed task co back to sched. The duplicate checking instruction is used to check for duplicates when inserting a new timing task to prevent the same callback from being repeatedly registered, such as check of duplicate timed task co. When checking the expired task, the coroutine corresponding to the task is prepared for wake-up, and the coroutine is added to the scheduling queue. After the coroutine is executed, the recycling processing logic is defined, including the operations of unbinding the coroutine and the task node, updating the node state, etc., realizing the close combination and efficient scheduling of the task and the coroutine, and fully utilizing the lightweight advantage of the coroutine. The task processing module encapsulates coroutine triggering instructions, task scanning instructions, and timing task screening instructions to periodically process and reset the parameters of the timing task while coordinating the state of the coroutine. The coroutine triggering instruction indicates that the scheduler is triggering a timer expiration and is preparing to start the corresponding timing coroutine, such as kucoro timer run. The task scanning instruction indicates that the scheduler is checking the skip list structure to see which node (timing task) has expired, such as kucoro timed task node check.The timing task screening instruction is used to check whether there is an expired timing task in the current scheduling cycle, and if there is no expired timing task, a normal (non-timer) coroutine is processed, for example, a periodic task or a task with a remaining number of times, after one execution, the next expiration time is recalculated and re-inserted into the data structure, and the sleep time of the coroutine is adjusted. The task reset function is provided, which recalculates the expiration time, updates the execution times, and other operations, so that the task is executed according to the new requirements, and the related parameters of the coroutine are adjusted accordingly.
[0102] Based on the above functional modules, when the system is initialized, the timer data structure is created by the efficient data structure management module, the lock of the data structure and the check time interval are initialized, and the initial value of the check time interval is set, and the related resources required for coroutine scheduling are initialized. When a new timing task needs to be added, such as a periodic log backup task with a task period length of 1 hour and an execution number of 10 times, the create node function is called to create a data structure node, the node expiration time, task period length, and remaining execution number are set, and the corresponding coroutine is associated, and finally the new timing task node is inserted into the timer data structure by the insert node function. The timer periodically executes the check function according to the check time interval, and when the log backup task node is detected to be expired, the coroutine wake-up function is used to prepare the execution of the task corresponding coroutine, and the coroutine is added to the scheduling queue, and the log backup operation is executed by the coroutine scheduler. After the coroutine is executed, the coroutine recycling processing function is triggered to unbind the coroutine and the node, update the remaining execution number attribute of the node, recalculate the next expiration time (i.e. the current expiration time plus 1 hour), and re-insert the node into the data structure by the insert node function, and update the sleep time of the coroutine. If the task period length of the log backup task needs to be modified to 30 minutes during the execution of the task, the task reset function can be called to recalculate the expiration time, update the task period length and the remaining execution number of the node, and adjust the related parameters of the coroutine to ensure that the coroutine can be awakened according to the new period. When the log backup task needs to be cancelled, the task cancellation function is called, and after the node is locked by atomic operation, it is logically deleted from the data structure. After the coroutine is executed, the coroutine recycling processing function detects that the node is in the to-be-deleted state, destroys the node and releases the memory, and cleans up the related resources of the coroutine.
[0103] From the above, the embodiment adopts an efficient data structure to manage tasks, uses an atomic operation and a spin lock to implement concurrent control, realizes the close combination of a timing task and a coroutine, and provides a flexible task processing strategy, thereby improving the overall performance and adaptability of the coroutine timer, enabling efficient management, flexible processing, and safe operation of the timing task, meeting the timing task requirements in complex scenarios, making the processing of the timing task more flexible and controllable, and making the scheduling of the coroutine more adaptive. The problems of low management efficiency of the timer in related technologies, difficulty in ensuring thread safety in a multi-thread environment, loose combination with the coroutine, and insufficient flexibility in task processing can be solved.
[0104] The application also provides a corresponding device for the timing task execution method, which further makes the method more practical. The device can be described from the perspective of functional modules and the perspective of hardware. The timing task execution device provided by the application is introduced below. The device is used to implement the timing task execution method provided by the application. In this embodiment, the timing task execution device can include or be divided into one or more program modules, which are stored in a storage medium and executed by one or more processors to complete the timing task execution method disclosed in embodiment one. The program module referred to in this embodiment refers to a series of computer program instruction segments that can complete a specific function. The program module is more suitable for describing the execution process of the timing task execution device in the storage medium than the program itself. The functions of each program module in this embodiment will be specifically introduced below. The timing task execution device described below can be mutually referred to the timing task execution method described above.
[0105] From the perspective of functional modules, refer to Figure 9 , Figure 9 The structure diagram of the timing task execution device provided by this embodiment in a specific implementation manner can include:
[0106] The timer structure generation module 901 is configured to construct a timer data structure by taking the time dimension attribute item and the task dimension attribute item as data elements, and initialize the data elements of the timer data structure according to the current task execution parameter; the task dimension attribute item includes scheduling domain resource information and task scanning parameters; the task scanning parameters are parameters for scanning the expiration time of each timing task in the scheduling domain.
[0107] The task node insertion module 902 is configured to, when there is a new timing task, generate a task data structure according to the running parameter information and the target execution coroutine parameter of the new timing task, and insert the task data structure as a task node into the timer data structure corresponding to the target scheduling domain to which the new timing task belongs.
[0108] The task execution module 903 is configured to, during the expiration of each timing task in the timer according to the check time interval of the time dimension attribute item, if the current parameter value of the task dimension attribute is a new timing task, transfer the new timing task and the target execution coroutine to the respective corresponding task queue.
[0109] For example, in some embodiments of the present embodiment, the task node insertion module 902 is further configured to: take each timing task of the scheduling domain as a task node, take the task execution time as the key of the corresponding task node, and generate the skip list structure of the scheduling domain by sorting each task node according to the task execution time; the task scanning parameter includes the number of skip list layers and the head node pointer, the head node pointer points to the first task node of the current reached task execution time when the scheduling domain scans the corresponding skip list structure according to the number of skip list layers; and based on the new timing task as the task node of the target scheduling domain, insert the task data structure into the timer data structure, and update the order of each task node in the target scheduling domain.
[0110] For example, in some other embodiments of the present embodiment, the task execution module 903 is further configured to: the task dimension attribute item further includes a global spin lock attribute, the task data structure further includes a node spin lock attribute, when receiving a task node operation request, acquire the node spin lock corresponding to the task data structure through an atomic operation; if the state of the node spin lock is in an unoccupied state, acquire the node spin lock and mark the state of the node spin lock as an occupied state; acquire the global spin lock corresponding to the corresponding timer data structure through an atomic operation, if the global spin lock is in an unoccupied state, acquire the global spin lock and mark the state of the global spin lock as an occupied state; according to the task node operation request, operate the corresponding attribute of the task data structure, and after completing the task node operation request, release the global spin lock and the node spin lock through an atomic operation.
[0111] As an example of the above embodiment, the task execution module 903 is further configured to: the task node operation request is a timing task cancellation instruction, the task data structure further includes a soft deletion mark attribute, according to the timing task cancellation instruction, update the soft deletion mark attribute of the task data structure to a task cancellation identification value, to complete the task cancellation operation of the task data structure; when detecting that the target execution coroutine is recycled, delete the task data structure from the timer data structure, release the memory resources occupied by the task data structure, and clean up the resources of the target execution coroutine.
[0112] As another exemplary implementation of the above embodiment, the task execution module 903 can be further configured to: the task node operation request is a running parameter reset instruction, the task data structure includes a first expiration time reference value and a task period length, according to the running parameter reset instruction, it is determined that the running parameter to be reset is a new task execution period length, according to the new task execution period length value, the total number of current task execution periods and the first expiration time reference value, the next node expiration time is determined; the next node expiration time and the new task execution period are converted into corresponding time base values, and the corresponding attribute values of the task data structure are updated correspondingly, and the sleep time of the coroutine is adjusted, so as to complete the running parameter reset operation of the task data structure.
[0113] Exemplarily, in some other embodiments of the present embodiment, the task execution module 903 can be further configured to: the time dimension attribute item further includes a last check time, when the task checking required resources of the timer are greater than a preset resource threshold, the current check time interval value is lowered; the new check time interval value is converted into a new time base value, and the new time base value is assigned to the check time interval attribute of the timer data structure, and the last check time of the timer is converted into a corresponding time base value, and the last check time attribute of the timer data structure is updated.
[0114] Exemplarily, in some other embodiments of the present embodiment, the task execution module 903 can be further configured to: when receiving a task node retrieval speed improvement requirement, the current skip list layer number is increased within the maximum depth range of the skip list structure; the new skip list layer number is assigned to the skip list layer number attribute of the timer data structure.
[0115] Exemplarily, in some other embodiments of the present embodiment, the task node insertion module 902 can be further configured to: the running parameter attribute item and the coroutine attribute item are used as data elements to construct the task data structure; the running parameter attribute item includes at least a main structure attribute and a node time attribute, and the coroutine attribute item includes at least a task retrieval parameter attribute, a task type attribute and a task execution coroutine attribute; according to the running parameter information of the new timing task, the task execution time and the task description information of the new timing task are determined, the task execution time is filled into the node time attribute, the business layer task description information pointer is determined according to the task description information and is filled into the task type attribute correspondingly; according to the target execution coroutine parameter of the new timing task, the target execution coroutine for executing the new timing task is determined, and the task execution coroutine attribute is determined according to the target execution coroutine; according to the target scheduling domain to which the new timing task belongs, the timer data structure to which it belongs is determined and is filled into the main structure attribute correspondingly, so as to obtain the task data structure; the skip list layer number used for the next task retrieval is determined according to the task retrieval parameter and is filled into the task retrieval parameter attribute, so as to obtain the task data structure of the new timing task.
[0116] As an exemplary implementation of the above embodiment, the task node insertion module 902 can be further configured to: when the new timing task is a fixed number of timing task, the running parameter attribute item further comprises a node trigger execution attribute, the total number of times that the new timing task is triggered for execution at the current time is obtained, and the next node expiration time is determined; the node trigger execution attribute is updated according to the total number of times and the total number of task executions of the new timing task, the node time attribute is updated according to the next node expiration time, and the updated task data structure is inserted into the timer data structure again, while the sleep time of the coroutine is adjusted.
[0117] As another exemplary implementation of the above embodiment, the task node insertion module 902 can be further configured to: the running parameter attribute item further comprises a soft deletion flag attribute, if the total number of times and the total number of task executions of the new timing task are the same, the soft deletion flag attribute is updated to a to-be-deleted state identifier value; when it is detected that the target execution coroutine is recycled, the task data structure is deleted from the timer data structure, the memory resources occupied by the task data structure are released, and the resources of the target execution coroutine are cleaned up.
[0118] As another exemplary implementation of the above embodiment, the task node insertion module 902 can be further configured to: the coroutine attribute item further comprises a node state attribute, when the task data structure is inserted into the timer data structure, the node state attribute is updated to a node added state; when the target scheduling domain scans the skip list structure in which the new timing task is located according to the number of skip list layers, if the head node pointer points to the new timing task, the node state attribute is updated to a scheduling preparation state; when the new timing task is transferred to the to-be-executed task queue of the target scheduling domain, the node state attribute is updated to a queue waiting state; when the new timing task is in the processing process of the target execution coroutine, the node state attribute is updated to a node running state.
[0119] As another exemplary implementation of the above embodiment, the task node insertion module 902 can be further configured to: the target execution coroutine has a plurality of target execution coroutines, the task execution coroutine attribute comprises a coroutine instance linked list, a currently running coroutine instance pointing identifier, and a number of currently unexecuted coroutine instances; the coroutine instance linked list is generated according to each target execution coroutine, and is filled into the corresponding position of the task execution coroutine attribute; in the process of processing the new timing task by the first target execution coroutine, the handle of the first target execution coroutine is filled into the corresponding position of the task execution coroutine attribute as the currently running coroutine instance pointing identifier, while the number of currently unexecuted coroutine instances is updated.
[0120] As another exemplary implementation of the above embodiment, the task node insertion module 902 can be further configured to: the new timing task is a periodic execution task, the node time attribute includes a node expiration time attribute, a first expiration time attribute, a task period length attribute, and a node triggered execution attribute, according to the task period length and the first expiration time of the running parameter information of the new timing task, determine the next node expiration time, and update the corresponding attribute value of the node time attribute; each time the new timing task is executed by the target execution coroutine, the next node expiration time is determined according to the value of the first expiration time attribute, the value of the task period length attribute, and the total number of triggered executions, and the node triggered execution attribute and the node expiration time attribute are updated correspondingly.
[0121] The timing task execution device mentioned above is described from the perspective of functional modules, and further, the present application also provides an electronic device, which is described from the perspective of hardware. Figure 10 The electronic device provided by the embodiment of the present application has the structure shown in the structural schematic diagram in one embodiment. The electronic device includes a memory 101 and a processor 102, the memory 101 stores a computer program, and the processor 102 is configured to run the computer program to execute the steps in any of the above timing task execution method embodiments.
[0122] The embodiment of the present application also provides a computer readable storage medium, which stores a computer program, wherein the computer program is configured to execute the steps in any of the above timing task execution method embodiments when running.
[0123] In one exemplary embodiment, the above computer readable storage medium can include, but is not limited to, a U disk, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and various media that can store computer programs.
[0124] The embodiment of the present application also provides a computer program product, which includes a computer program, and the computer program is executed by a processor to implement the steps in any of the above timing task execution method embodiments.
[0125] The embodiment of the present application also provides another computer program product, which includes a non-volatile computer readable storage medium, the non-volatile computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement the steps in any of the above timing task execution method embodiments.
[0126] The timing task execution method, the electronic device, the computer readable storage medium and the computer program product provided by the present application are described in detail. In the specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts of each embodiment can be referred to each other. The units and algorithm steps of each example described in each disclosed embodiment are executed by electronic hardware or computer software, which depends on the specific application and design constraints of the technical solution. The described functions can be implemented by different methods for each specific application by professional technicians, and such implementation should not be considered beyond the scope of the present application. Without departing from the principles of the present application, the present application can be improved and modified, and these improvements and modifications also fall within the scope of the present application.
Claims
1. A method for executing a scheduled task, characterized in that, The application comprises the following steps: The time dimension attribute item and the task dimension attribute item are used as data elements to construct a timer data structure, and the data elements of the timer data structure are initialized according to the current task execution parameters; the task dimension attribute item comprises scheduling domain resource information and task scanning parameters; the task scanning parameters are parameters for scanning the expiration time of each timing task in the scheduling domain; When a new timing task exists, a task data structure is generated according to the running parameter information and the target execution coroutine parameter of the new timing task, and the task data structure is used as a task node and inserted into the timer data structure corresponding to the target scheduling domain to which the new timing task belongs; During the checking of each timing task expiration by the timer at the checking time interval of the time dimension attribute item, if the current parameter value of the task dimension attribute item is the new timing task, the new timing task and the target execution coroutine are transferred to the task queue corresponding thereto; The generation process of the task data structure comprises the following steps: the running parameter attribute item and the coroutine attribute item are used as data elements to construct a task data structure; the running parameter attribute item at least comprises a main structure attribute and a node time attribute, and the coroutine attribute item at least comprises a task retrieval parameter attribute, a task type attribute and a task execution coroutine attribute; the task execution time and the task description information of the new timing task are determined according to the running parameter information of the new timing task, the task execution time is filled into the node time attribute, the business layer task description information pointer is determined according to the task description information and filled into the task type attribute; the target execution coroutine for executing the new timing task is determined according to the target execution coroutine parameter of the new timing task, and the task execution coroutine attribute is determined according to the target execution coroutine; the timer data structure to which the new timing task belongs is determined according to the target scheduling domain to which the new timing task belongs, and the main structure attribute is filled accordingly; the retrieval range parameter used for the next task retrieval is determined according to the task retrieval parameter and filled into the task retrieval parameter attribute, thereby obtaining the task data structure of the new timing task; The task scanning parameters comprise the number of skip list layers and a head node pointer; the head node pointer points to the first task node reaching the current task execution time when the scheduling domain scans the corresponding skip list structure according to the number of skip list layers; the coroutine attribute item further comprises a node state attribute; when the task data structure is inserted into the timer data structure, the node state attribute is updated to a node added state; when the target scheduling domain scans the skip list structure in which the new timing task is located according to the number of skip list layers, if the head node pointer points to the new timing task, the node state attribute is updated to a scheduling preparation state; when the new timing task is transferred to the to-be-executed task queue of the target scheduling domain, the node state attribute is updated to a queue waiting state; when the new timing task is in the process of being processed by the target execution coroutine, the node state attribute is updated to a node running state.
2. The method of claim 1, wherein, The task data structure is used as a task node and inserted into the timer data structure corresponding to the target scheduling domain to which the new timing task belongs, which comprises the following steps: The time tasks of the scheduling domain are taken as task nodes, and the task execution times are taken as keys of the corresponding task nodes, and a skip list structure of the scheduling domain is generated by sorting the task nodes according to the task execution times; The task data structure is inserted into the timer data structure based on the new time task as a task node of the target scheduling domain, and meanwhile, the target scheduling domain updates the order of the task nodes in the domain.
3. The method of claim 1, wherein, The task dimension attribute item further includes a global spin lock attribute, and the task data structure further includes a node spin lock attribute, and after the task data structure is inserted into the timer data structure corresponding to the target scheduling domain to which the new time task belongs, the following operations are further included: When a task node operation request is received, the node spin lock corresponding to the task data structure is acquired through an atomic operation; If the state of the node spin lock is in an unoccupied state, the node spin lock is acquired, and the state of the node spin lock is marked as an occupied state; The global spin lock corresponding to the corresponding timer data structure is acquired through an atomic operation, and if the global spin lock is in an unoccupied state, the global spin lock is acquired, and the state of the global spin lock is marked as an occupied state; According to the task node operation request, the corresponding attributes of the task data structure are operated, and after the task node operation request is completed, the global spin lock and the node spin lock are released through an atomic operation.
4. The method of claim 3, wherein, The task node operation request is a time task cancellation instruction, and the task data structure further includes a soft deletion mark attribute, and according to the task node operation request, the corresponding attributes of the task data structure are operated, including: According to the time task cancellation instruction, the soft deletion mark attribute of the task data structure is updated to a task cancellation identification value, so as to complete the task cancellation operation of the task data structure; When it is detected that the target execution coroutine is recycled, the task data structure is deleted from the timer data structure, and the memory resources occupied by the task data structure are released, and the resources of the target execution coroutine are cleaned up.
5. The method of claim 3, wherein, The task node operation request is a running parameter reset instruction, and the task data structure includes a first expiration time reference value and a task period length, and according to the task node operation request, the corresponding attributes of the task data structure are operated, including: According to the running parameter reset instruction, it is determined that the to-be-reset running parameter is a new task execution period length, and according to the new task execution period length, the total number of current task execution periods and the first expiration time reference value, a next node expiration time is determined; The next node expiration time and the new task execution period length are converted into corresponding time base values, and the corresponding attribute values of the task data structure are updated correspondingly, and the sleep time of the coroutine is adjusted, so as to complete the running parameter reset operation of the task data structure.
6. The method of claim 1, wherein, The time dimension attribute item further includes a last check time attribute, and after the timer data structure is constructed by taking the time dimension attribute item and the task dimension attribute item as data elements, the following operations are further included: When the task of the timer requires resources greater than a preset resource threshold, the value of the current check time interval is reduced; The new check time interval value is converted into a new time base value, and the new time base value is assigned to the check time interval attribute of the timer data structure. Meanwhile, the time when the last time the timer checked the expiration of each timing task is converted into a corresponding time base value, and the last check time attribute of the timer data structure is updated.
7. The method of claim 1, wherein, The new timing task is a fixed-time timing task, and the running parameter attribute item further includes a node trigger execution attribute. During the checking of the expiration of each timing task by the timer according to the check time interval of the time dimension attribute item, the following are included: The total number of times that the new timing task is triggered for execution at the current time is obtained, and the next node expiration time is determined; The node trigger execution attribute is updated according to the total number of times and the total number of task executions of the new timing task, the node time attribute is updated according to the next node expiration time, and the updated task data structure is inserted into the timer data structure again, while the sleep time of the coroutine is adjusted.
8. The method of claim 7, wherein, The running parameter attribute item further includes a soft deletion flag attribute. The node trigger execution attribute is updated according to the total number of times and the total number of task executions of the new timing task, including: If the total number of times and the total number of task executions of the new timing task are the same, the soft deletion flag attribute is updated to a to-be-deleted state identifier value; When it is detected that the target execution coroutine is recycled, the task data structure is deleted from the timer data structure, and the memory resources occupied by the task data structure are released, and the resources of the target execution coroutine are cleaned up.
9. The method of claim 1, wherein, The target execution coroutine has a plurality of target execution coroutines, and the task execution coroutine attribute includes a coroutine instance linked list, a currently running coroutine instance pointing identifier, and a number of currently unexecuted coroutine instances; After the new timing task and the target execution coroutine are transferred to the respective task queues, the following are further included: A coroutine instance linked list is generated according to each target execution coroutine, and is filled into the corresponding position of the task execution coroutine attribute; During the processing of the new timing task by the first target execution coroutine, the handle of the first target execution coroutine is filled into the corresponding position of the task execution coroutine attribute as the currently running coroutine instance pointing identifier, and the number of currently unexecuted coroutine instances is updated.
10. The method of claim 1, wherein, The new timing task is a periodic execution task, and the node time attribute includes a node expiration time attribute, a first expiration time attribute, a task period length attribute, and a node trigger execution attribute. According to the running parameter information of the new timing task and the target execution coroutine parameter, a task data structure is generated, including: According to the task period length and the first expiration time of the running parameter information of the new timing task, the next node expiration time is determined, and the value of the node expiration time attribute is updated accordingly; The new timing task is a fixed-time timing task, and the running parameter attribute item further includes a node trigger execution attribute. During the checking of the expiration of each timing task by the timer according to the check time interval of the time dimension attribute item, the following are included: The total number of times that the new timing task is triggered for execution at the current time is obtained, and the next node expiration time is determined; The node trigger execution attribute is updated according to the total number of times and the total number of task executions of the new timing task, the node time attribute is updated according to the next node expiration time, and the updated task data structure is inserted into the timer data structure again, while the sleep time of the coroutine is adjusted. The running parameter attribute item further includes a soft deletion flag attribute. The node trigger execution attribute is updated according to the total number of times and the total number of task executions of the new timing task, including: If the total number of times and the total number of task executions of the new timing task are the same, the soft deletion flag attribute is updated to a to-be-deleted state identifier value; When it is detected that the target execution coroutine is recycled, the task data structure is deleted from the timer data structure, and the memory resources occupied by the task data structure are released, and the resources of the target execution coroutine are cleaned up. The target execution coroutine has a plurality of target execution coroutines, and the task execution coroutine attribute includes a coroutine instance linked list, a currently running coroutine instance pointing identifier, and a number of currently unexecuted coroutine instances; After the new timing task and the target execution coroutine are transferred to the respective task queues, the following are further included: A coroutine instance linked list is generated according to each target execution coroutine, and is filled into the corresponding position of the task execution coroutine attribute; During the processing of the new timing task by the first target execution coroutine, the handle of the first target execution coroutine is filled into the corresponding position of the task execution coroutine attribute as the currently running coroutine instance pointing identifier, and the number of currently unexecuted coroutine instances is updated. The new timing task is a periodic execution task, and the node time attribute includes a node expiration time attribute, a first expiration time attribute, a task period length attribute, and a node trigger execution attribute. According to the running parameter information of the new timing task and the target execution coroutine parameter, a task data structure is generated, including: According to the task period length and the first expiration time of the running parameter information of the new timing task, the next node expiration time is determined, and the value of the node expiration time attribute is updated accordingly; Whenever the new timing task is executed once by the target execution coroutine, the next node expiration time is determined according to the value of the first expiration time attribute, the value of the task period length attribute and the total number of triggered executions, and the node triggered execution attribute and the node expiration time attribute are updated correspondingly.
11. An electronic device, comprising: The method comprises the following steps: a memory for storing a computer program; a processor for executing the computer program to implement the steps of the timing task execution method according to any one of claims 1 to 10.
12. A computer-readable storage medium, characterized in that, The computer program is stored on the computer readable storage medium, and the computer program is executed by the processor to implement the steps of the timing task execution method according to any one of claims 1 to 10.
13. A computer program product comprising computer programs / instructions, characterized in that, The computer program / instruction is executed by the processor to implement the steps of the timing task execution method according to any one of claims 1 to 10.
Citation Information
Patent Citations
Task scheduling method, system and equipment and storage medium
CN113157426A
Timed task execution method and device, computer equipment and readable storage medium
CN119597411A