Task processing method, device and equipment and readable storage medium
By changing the variables to subscribe to the logic function, determine the order of task execution, solving the problems of complex logical design and inaccurate task execution in the polling mode, and achieving the reliability and accuracy of task execution.
Patent Information
- Application Number
- CN202411159095.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-22
- Publication Date
- 2024-11-01
AI Technical Summary
The logic design in the polling mode is complex, error-prone, and it is difficult to ensure the accuracy and reliability of task execution.
Subscribe to different logical functions through variable changes, determine the triggering and execution order of the logical functions, and use the variable data structure and subscription mechanism to achieve the sequential execution of the task list.
The logical design of the polling mode is simplified, the accuracy and reliability of task execution are ensured, and the execution order of the logical function corresponds to the timing of variable changes.
Smart Images

Figure CN118885280A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a task processing method, device, equipment and readable storage medium. Background Art
[0002] The main means of thread synchronization and information transmission are shared memory, message queues, pipes, lock mechanisms, and conditional variables, etc. These methods will cause a large number of kernel context switches and interrupts, resulting in a lot of delays and overhead.
[0003] Placing logic code (including device driver code) in user mode can avoid kernel context switching. Use polling mode to place related logic (tasks) on the task execution list of the same thread and execute them in sequence. However, in polling mode, there are often multiple variables, and changes in different variables need to trigger different subsequent tasks, and the execution of different tasks may cause changes in different variables. This cycle makes the logic design complicated and error-prone.
[0004] In summary, how to effectively solve the problem of how to ensure the accuracy of logic design in the polling mode is a technical problem that technical personnel in this field urgently need to solve. Summary of the invention
[0005] The purpose of this application is to provide a task processing method, device, equipment and readable storage medium, which subscribes to different logic functions through variable changes, thereby determining the triggering and execution order of the logic functions, which can simplify the logic design in the polling description, ensure the accuracy of the logic design, and ensure the reliability of task execution.
[0006] In order to solve the above technical problems, this application provides the following technical solutions:
[0007] A task processing method, comprising:
[0008] Get the new value of the target variable;
[0009] Writing the new value into new data of a variable data structure; wherein the variable data structure corresponds to the target variable;
[0010] Reading an old value from old data in the variable data structure;
[0011] In the case where the old value is inconsistent with the new value, inserting the target task subscribed by the target variable into the list of tasks to be executed; wherein the tasks to be executed in the list of tasks to be executed are logic functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables;
[0012] After the old values in the old data are updated with the new values in the new data, the tasks in the to-be-executed task list are executed in the order in which the tasks are inserted.
[0013] Preferably, inserting the target task subscribed by the target variable into a list of tasks to be executed includes:
[0014] Traverse the callback linked list corresponding to the target variable to obtain the task subscribed by the target variable;
[0015] Determine the task obtained through traversal as the target task;
[0016] Insert the target task at the end of the list of tasks to be executed.
[0017] Preferably, the callback linked list is a single linked list, and traversing the callback linked list corresponding to the target variable to obtain the task subscribed by the target variable includes:
[0018] According to the first-in-first-out order, traverse each element in the callback linked list to obtain the task subscribed by the target variable;
[0019] Accordingly, inserting the target task into the tail of the list of tasks to be executed includes:
[0020] The target task is inserted into the tail of the to-be-executed task list in order according to the task acquisition order.
[0021] Preferably, executing the tasks in the to-be-executed task list in the order in which the tasks are inserted includes:
[0022] Read the current task from the list of tasks to be executed in a first-in-first-out order;
[0023] Calling the logic function interface corresponding to the current task to execute the current task;
[0024] Correspondingly, after executing the current task, it also includes:
[0025] Obtaining a first new value of a first variable output by the logic function interface;
[0026] Writing the first new value into the first new data in the first variable data structure; wherein the first variable data structure corresponds to the first variable;
[0027] Reading a first old value from the first old data in the first variable data structure;
[0028] When the first old value is inconsistent with the first new value, updating the first old value in the first old data with the first new value in the first new data;
[0029] Insert the first task subscribed by the first variable into the list of tasks to be executed, so as to wait for execution of the first task in the order of insertion.
[0030] Preferably, the new data and the old data in the variable data structure are both typeless pointers; the typeless pointers record the memory addresses allocated to the new value and the old value;
[0031] Accordingly, reading the old value from the old data of the variable data structure includes:
[0032] The old value is read from the memory address storing the old data using the typeless pointer corresponding to the old data.
[0033] Preferably, writing the new value into new data of the variable data structure comprises:
[0034] Determining a data type of the new value, and using the data type to determine a bit width of the new value;
[0035] Forcefully convert the data types of the new data and the old data to the same data type as the new value;
[0036] The new value is written into the new data after the data type conversion.
[0037] Preferably, obtaining a new value of the target variable includes:
[0038] Get the new values of multiple specified variables;
[0039] Performing a preset logical operation on the new values of the multiple variables to obtain a logical operation result; the preset logical operation includes at least one of an AND logical operation and an OR logical operation and a NOT logical operation;
[0040] The logical operation result is determined as a new value of the target variable.
[0041] A task processing device, comprising:
[0042] New value acquisition module, used to obtain the new value of the target variable;
[0043] A data writing module, used for writing the new value into new data of a variable data structure; wherein the variable data structure corresponds to the target variable;
[0044] A data reading module, used for reading old values from old data in the variable data structure;
[0045] A task triggering module, used for inserting the target task subscribed by the target variable into a list of tasks to be executed when the old value is inconsistent with the new value; wherein the tasks to be executed in the list of tasks to be executed are logic functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables;
[0046] The task execution module is used to execute the tasks in the to-be-executed task list in the order in which the tasks are inserted, after the old values in the old data are updated with the new values in the new data.
[0047] An electronic device, comprising:
[0048] Memory for storing computer programs;
[0049] A processor is used to implement the steps of the above-mentioned task processing method when executing the computer program.
[0050] A readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the task processing method are implemented.
[0051] A computer program product includes a computer program / instruction, which implements the steps of the above-mentioned task processing method when executed by a processor.
[0052] Apply the method provided in the embodiment of the present application to obtain a new value of the target variable; write the new value into new data in the variable data structure; wherein the variable data structure corresponds to the target variable; read the old value from the old data in the variable data structure; when the old value is inconsistent with the new value, insert the target task subscribed by the target variable into the list of tasks to be executed; wherein the tasks to be executed in the list of tasks to be executed are logical functions to be called, and the sequence of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables; after updating the old value in the old data with the new value in the new data, execute the tasks in the list of tasks to be executed in the order in which the tasks were inserted.
[0053] In the present application, after obtaining the new value of the target variable, the new value can be written into the new data of the variable data structure. The variable has a corresponding relationship with the variable data structure, and the old value of the target variable can be read based on the old data of the variable data structure. By comparing the new and old values of the target variable, the execution of the target task can be triggered when it is determined that the new value is different from the old value. Specifically, the target task can be inserted into the list of tasks to be executed by subscribing the target variable to the target task, when it is determined that the new and old values are different. The tasks to be executed in the list of tasks to be executed are all logical functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the numerical changes in response to the changes. In other words, based on the changes in the variables, it can be determined whether to execute the corresponding logical functions, and the order of executing these logical functions corresponds to the time sequence of the changes in the variables. In this way, after completing the update of the new and old data, the corresponding tasks can be executed in the order of task insertion.
[0054] Technical effect: This application can detect the value change of variables with the help of variable data structure; with the help of subscription mechanism, the variable change triggers the relevant logic function; with the help of the list of tasks to be executed, it can be clearly determined that the execution order of the logic function corresponds to the timing of the value change of the relevant variables. In this way, the logic design of the polling mode can be greatly simplified, the accuracy of the logic design can be guaranteed, and the reliability of task execution can be guaranteed.
[0055] Correspondingly, the embodiments of the present application also provide a task processing device, equipment, readable storage medium and computer program product corresponding to the above-mentioned task processing method, which have the above-mentioned technical effects and are not repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the related technologies, the drawings required for use in the embodiments or the related technical descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0057] Figure 1 This is a flowchart of an implementation method of a task processing method in an embodiment of the present application;
[0058] FIG2 is a schematic diagram of an asynchronously executed function interface in an embodiment of the present application;
[0059] FIG3 is a schematic diagram of the logical association between a variable and a task;
[0060] FIG4 is a schematic diagram of a task scheduling;
[0061] FIG5 is another schematic diagram of the logical association between variables and tasks;
[0062] FIG6 is a schematic diagram of a logical association between variables and tasks in an embodiment of the present application;
[0063] FIG7 is a schematic diagram of a specific implementation of a task processing method in an embodiment of the present application;
[0064] FIG8 is a schematic diagram of the structure of a task processing device in an embodiment of the present application;
[0065] FIG9 is a schematic diagram of the structure of an electronic device in an embodiment of the present application;
[0066] FIG. 10 is a schematic diagram of the specific structure of an electronic device in an embodiment of the present application. DETAILED DESCRIPTION
[0067] In order to enable those skilled in the art to better understand the present application, the present application is further described in detail below in conjunction with the accompanying drawings and specific implementation methods. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present application.
[0068] Please refer to Figure 1 , Figure 1 This is a flowchart of a task processing method in an embodiment of the present application, the method comprising the following steps:
[0069] S101. Obtain a new value of the target variable.
[0070] The target variable may be a storage space, and the new value refers to the value to be stored in the storage space. Correspondingly, the old data refers to the data originally existing in the storage space.
[0071] In the present application, the new value of the target variable may be directly input by an upper layer application, or may be output by executing a logic function.
[0072] S102, writing the new value into the new data of the variable data structure.
[0073] Among them, the variable data structure corresponds to the target variable.
[0074] In the embodiment of the present application, a corresponding variable data structure can be set for each variable that needs to trigger the next task after each value change, and the change data structure mainly includes new data and old data.
[0075] After obtaining the new value, the new value can be written to the new data of the variable data structure.
[0076] S103. Read an old value from old data in the variable data structure.
[0077] After obtaining the new index, the old value can be read from the old data in the variable data structure.
[0078] S104: When the old value is inconsistent with the new value, insert the target task subscribed by the target variable into the list of tasks to be executed.
[0079] The tasks to be executed in the list of tasks to be executed are logic functions to be called, and the order of the tasks to be executed corresponds to the time sequence of value changes of the corresponding variables.
[0080] In this embodiment, the corresponding tasks can be subscribed to different variables in advance, so that when the value of the variable changes, the corresponding target task can be triggered. Considering that the number and type of variables are not single in actual applications, and the tasks to be triggered by variable changes are not single, it can be set to subscribe to the corresponding tasks for each variable. In addition, a list of tasks to be executed needs to be set, in which the tasks to be executed that are triggered by value changes can be stored.
[0081] In this way, after obtaining the old and new values of the target variable, the two can be compared. When the old and new values are found to be inconsistent, the target task subscribed by the target variable can be inserted into the list of tasks to be executed, so that the call of the related logic function can be triggered based on the list of tasks to be executed.
[0082] In a specific implementation of the present application, inserting a target task subscribed by a target variable into a list of tasks to be executed includes:
[0083] Traverse the callback linked list corresponding to the target variable to obtain the task subscribed by the target variable;
[0084] Determine the traversed tasks as target tasks;
[0085] Insert the target task at the end of the list of tasks to be executed.
[0086] For ease of description, the above three steps are combined for explanation below.
[0087] In this embodiment, the callback list can be used to record the tasks subscribed by the target variable. By traversing the callback list corresponding to the target variable, the tasks subscribed by the target variable can be clearly identified. All the tasks obtained by the traversal are determined as target tasks, and then the target tasks are inserted into the tail of the task list to be executed.
[0088] Specifically, the callback linked list is a single linked list. The callback linked list corresponding to the target variable is traversed to obtain the tasks subscribed by the target variable, including:
[0089] According to the first-in-first-out order, traverse each element in the callback list to obtain the task subscribed by the target variable;
[0090] Accordingly, the target task is inserted at the end of the list of tasks to be executed, including:
[0091] Insert the target task to the end of the list of tasks to be executed in the order in which the tasks are obtained.
[0092] In other words, the callback linked list can be in the form of a single linked list, where first is used to point to the first element of the linked list, last is used to point to the last element of the linked list, and count is used to record the number of elements in the linked list (that is, the length of the linked list). The reason for using a single linked list is that the callback interface added to the linked list needs to comply with the first-in-first-out principle and must be strictly orderly, and a single linked list is sufficient to meet the requirements.
[0093] For example: The specific process of subscribing a callback task to a variable is as follows:
[0094] First, define the linked list elements as follows:
[0095] struct slist_node{
[0096] struct slist_node*next; / / link list element
[0097] }
[0098] That is, the linked list elements only record the element addresses. The elements themselves are typeless (void), and the element addresses are forcibly converted to void* type for recording.
[0099] Callback list definition:
[0100] struct slist{
[0101] struct slist_node*first; / / points to the first element of the linked list
[0102] struct slist_node*last; / / points to the last element of the linked list
[0103] int32 count; / / Record the number of elements in the linked list
[0104] }
[0105] The linked list is in the form of a single linked list, first is used to point to the first element of the linked list, last is used to point to the last element of the linked list, and count is used to record the number of elements in the linked list (that is, the length of the linked list).
[0106] In a specific implementation of the present application, when the logic design needs to be modified, the variables to be adjusted and the subscription tasks are clarified;
[0107] Insert or remove the elements in the callback linked list corresponding to the variable to obtain the changed callback linked list;
[0108] After the value of the variable changes, the task is obtained by traversing the callback linked list based on the change and inserted into the list of tasks to be executed.
[0109] The callback linked list can adopt a single linked list, so that when inserting and removing elements, it has the characteristics of fewer steps and simple operation.
[0110] S105 , after the old values in the old data are updated with the new values in the new data, the tasks in the to-be-executed task list are executed in the order in which the tasks are inserted.
[0111] In the present application, when it is found that the new and old values are different, the new value can be used to update the old value of the old data, and then the tasks in the to-be-executed task list are executed in the order in which the tasks are inserted.
[0112] In a specific implementation of the present application, executing tasks in the to-be-executed task list in the order in which the tasks are inserted includes:
[0113] Read the current task from the list of tasks to be executed in first-in-first-out order;
[0114] Call the logic function interface corresponding to the current task to execute the current task;
[0115] Correspondingly, after executing the current task, it also includes:
[0116] Obtain a first new value of a first variable output by the logic function interface;
[0117] Writing the first new value into the first new data in the first variable data structure; wherein the first variable data structure corresponds to the first variable;
[0118] Reading a first old value from a first old data structure of a first variable data structure;
[0119] When the first old value is inconsistent with the first new value, updating the first old value in the first old data with the first new value in the first new data;
[0120] Insert the first task subscribed by the first variable into the list of tasks to be executed, so that the first task waits for execution in the order of insertion.
[0121] For ease of description, the above steps are combined for explanation below.
[0122] When executing a task in the to-be-executed task list, the current task can be read from the to-be-executed task list in the order of first-in, first-out, and then the logic function interface corresponding to the current task is called to execute the corresponding logic function.
[0123] After the target task is executed, the logic function interface may output the first new value of the first variable. That is to say, after the triggered target task corresponding to the target variable is executed, the first new value of the first variable corresponding to the first task to be triggered may be generated. In this case, the first new value can be written into the first new data in the first variable data structure; accordingly, the first variable data structure corresponds to the first variable; then, the first old value is read from the first old data in the first variable data structure; by comparison, when the first old value is inconsistent with the first new value, the first new value in the first new data can be used to update the first old value in the first old data; the first task subscribed by the first variable is inserted into the list of tasks to be executed, so as to wait for the execution of the first task in the order of insertion. That is, the insertion method of the first task is similar to the insertion method of the target task, and can refer to the above description, which will not be repeated here.
[0124] Apply the method provided in the embodiment of the present application to obtain a new value of the target variable; write the new value into new data in the variable data structure; wherein the variable data structure corresponds to the target variable; read the old value from the old data in the variable data structure; when the old value is inconsistent with the new value, insert the target task subscribed by the target variable into the list of tasks to be executed; wherein the tasks to be executed in the list of tasks to be executed are logical functions to be called, and the sequence of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables; after updating the old value in the old data with the new value in the new data, execute the tasks in the list of tasks to be executed in the order in which the tasks were inserted.
[0125] In the present application, after obtaining the new value of the target variable, the new value can be written into the new data of the variable data structure. The variable has a corresponding relationship with the variable data structure, and the old value of the target variable can be read based on the old data of the variable data structure. By comparing the new and old values of the target variable, the execution of the target task can be triggered when it is determined that the new value is different from the old value. Specifically, the target task can be inserted into the list of tasks to be executed by subscribing the target variable to the target task, when it is determined that the new and old values are different. The tasks to be executed in the list of tasks to be executed are all logical functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the numerical changes in response to the changes. In other words, based on the changes in the variables, it can be determined whether to execute the corresponding logical functions, and the order of executing these logical functions corresponds to the time sequence of the changes in the variables. In this way, after completing the update of the new and old data, the corresponding tasks can be executed in the order of task insertion.
[0126] Technical effect: This application can detect the value change of variables with the help of variable data structure; with the help of subscription mechanism, the variable change triggers the relevant logic function; with the help of the list of tasks to be executed, it can be clearly determined that the execution order of the logic function corresponds to the timing of the value change of the relevant variables. In this way, the logic design of the polling mode can be greatly simplified, the accuracy of the logic design can be guaranteed, and the reliability of task execution can be guaranteed.
[0127] It should be noted that, based on the above embodiments, the embodiments of the present application also provide corresponding improved solutions. In the preferred / improved embodiments, the same steps or corresponding steps as those in the above embodiments can be referenced to each other, and the corresponding beneficial effects can also be referenced to each other, which will not be described one by one in the preferred / improved embodiments of this article.
[0128] In one implementation of the present application, both the new data and the old data in the variable data structure are typeless pointers; the typeless pointers record the memory addresses allocated for the new value and the old value;
[0129] Accordingly, the old value is read from the old data of the variable data structure, including:
[0130] Using the untyped pointer corresponding to the old data, read the old value from the memory address where the old data is stored.
[0131] Among them, writing the new value into the new data of the variable data structure includes:
[0132] Determine the data type of the new value, and use the data type to determine the bit width of the new value;
[0133] Force the data type of the new and old data to be the same data type as the new value;
[0134] Write the new value into the new data after data type conversion.
[0135] For ease of explanation, the above steps are combined for explanation below.
[0136] In this embodiment, the variable data structure defines:
[0137]
[0138] As above, in the definition of variable data structure, new_value is used to receive newly written data, and old_value records the original data. This is done to distinguish whether the newly written data is different from the original data when the data is written. When data is written to the data structure, the data is first written to new_value, and new_value is compared with old_value. If new_value is not equal to old_value, the value of new_value is updated to old_value, and then the task asynchronous scheduling is triggered.
[0139] The new input data new_value and the old data old_value are defined as untyped pointers (void*) because the type of the variable used as the input is determined when it is defined. It can be 8-bit, 16-bit, 32-bit, or 64-bit. As receivers, new_value and old_value do not distinguish between data types. They only record the memory address allocated for the data. When data needs to be read from new_value or old_value, it can be converted according to the data type (8-bit / 16-bit / 32-bit / 64-bit).
[0140] The Callback_logic_list single linked list can be associated with multiple logic function interfaces. The same variable can have multiple processing logics, which are executed in the order of first in first out. Generally, only one logic function can be registered for a variable.
[0141] In a specific implementation of the present application, obtaining a new value of the target variable includes:
[0142] Get the new values of multiple specified variables;
[0143] Performing a preset logical operation on the new values of the multiple variables to obtain a logical operation result; the preset logical operation includes at least one of an AND logical operation, an OR logical operation, and a NON-logical operation;
[0144] Determine the result of the logical operation as the new value of the target variable.
[0145] For ease of description, the above steps are combined for explanation below.
[0146] In this embodiment, it is considered that the execution of some tasks may depend on multiple variables being in different states before being triggered. For example, for variables 1, 2, and 3, the triggering of task 1 requires that variables 1 and 2 change at the same time; the triggering of task 2 requires that variable 1 remains unchanged and variable 2 changes; the triggering of task 3 requires that variables 1, 2, and 3 all change before being triggered.
[0147] Therefore, when determining the new value of the target variable, the new value of each variable can be obtained, and then the preset logical operation can be performed on the new values of multiple variables. The logical operation here can correspond to the triggering conditions of different tasks (for example, if the triggering of task 1 requires variables 1 and 2 to change at the same time, then variables 1 and 2 need to be calculated). In this way, by performing logical operations on multiple variables and treating their conclusions as an independent variable, the complex triggering conditions can be simplified.
[0148] When executing tasks, you can use asynchronous call interface definition and execution logic:
[0149]
[0150] As shown in the above data structure, the asynchronous call interface is also organized in the form of a single linked list. The tasks to be executed will be added to this single linked list. The reason for using a single linked list is that the tasks added to the linked list must comply with the first-in-first-out principle and must be strictly kept in order. A single linked list is sufficient to meet the needs, and the logical steps for inserting and removing elements are few and the operation is simple. Tasks registered on async_list need to run on the specified CPU core (that is, executed on the same thread), combined with the first-in-first-out operation of the single linked list, to strictly ensure the order of task execution.
[0151] As shown in the above data structure, the asynchronously executed function interface is async_run, which will be added to the async_list linked list. When the function interface on the async_list is scheduled for execution, async_run will run. The working logic definition of async_run is shown in Figure 2.
[0152] To help those skilled in the art better understand the task processing method provided in the embodiment of the present application, the task processing method is described in detail below with reference to specific examples.
[0153] In the relevant scheme, as shown in Figure 3, there are two logically related tasks on the same thread, Task 1 and Task 2, and Task 2 is logically related to Task 1 by virtue of variable a. For example, Task 1 modifies variable a to 1, and Task 2 executes different logical branches depending on the value of variable a. If variable a is 1, Task 2 executes logical branch x, and if variable a is not 1, Task 2 executes logical branch y. There can be multiple logically related tasks, and here we take two tasks as an example to briefly explain the logical relationship of mutual dependence between tasks.
[0154] As shown in Figure 4, after modifying variable a, task 1 inserts task 2 that depends on variable a into the "list of tasks to be executed" (generally, the newly inserted task will be placed at the end of the list of tasks to be executed), and task 2 waits in the list for scheduling and execution.
[0155] It should be noted that the example in Figure 2 is relatively simple. In actual applications, there will be multiple logical variables that are associated with Task 1 and Task 2, and there will be more than two tasks that are logically associated.
[0156] As shown in Figure 5, the six variables a, b, c, d, e, and f are the input and output of the three tasks respectively. The logical relationship of the three tasks is coupled together through these six variables. Variables a and b are the input of Task 1. Changes in variables a or b will cause the logic in Task 1 to be executed. Variables c and d are the output of Task 1. Variables c and d are the input of Task 2. Changes in variables c or d will cause the logic in Task 2 to be executed. After the logic of Task 1 is executed, it is necessary to decide whether to schedule Task 2 based on whether variables c or d have changed. If variables c or d have changed, Task 2 needs to be scheduled. If neither variables c nor d have changed, Task 2 does not need to be scheduled. Variables e and f are the output of Task 2. Variables e and f are the input of Task 3. Changes in variables e or f will cause the logic in Task 3 to be executed. After the logic of Task 2 is executed, it is necessary to decide whether to schedule Task 2 based on whether variables e or d have changed. Whether to schedule task 3 is determined based on whether there is a change. If variable e or variable f changes, task 3 needs to be scheduled. If variable e and variable f do not change, task 3 does not need to be scheduled. Variable a is the input of task 1. Changes in variable a will cause the logic in task 1 to be executed. After the logic of task 3 is executed, it is necessary to decide whether to schedule task 1 based on whether variable a changes. If variable a changes, task 1 needs to be scheduled. If variable a does not change, task 1 does not need to be scheduled. Variable c is the input of task 2. Changes in variable c will cause the logic in task 2 to be executed. After the logic of task 3 is executed, it is necessary to decide whether to schedule task 2 based on whether variable c changes. If variable c changes, task 2 needs to be scheduled. If variable c does not change, task 2 does not need to be scheduled.
[0157] As shown above, 6 variables and 3 tasks can derive multiple combinations. After the variables change, the tasks that are coupled with these variables need to be scheduled in time, and the task calls must also ensure a strict timing relationship based on the changes in the variables. This brings great difficulties to logic design.
[0158] After applying the task processing method provided by the embodiment of the present application, as shown in FIG6 , variable a, variable b, variable c, variable d, variable e, and variable f, these six variables are the input and output of three tasks respectively, variable a and variable b are the input of task 1, variable a and variable b both subscribe to task 2, and if variable a or variable b changes, task 2 will be immediately inserted into the "list of tasks to be executed"; variable c and variable d are the input of task 2, variable c and variable d both subscribe to task 3, and if variable c or variable d changes, task 3 will be immediately inserted into the "list of tasks to be executed"; variable e and variable f are the input of task 3, variable e and variable f both subscribe to task 1, and if variable e or variable f changes, task 1 will be immediately inserted into the "list of tasks to be executed"; task 1, task 2, and task 3 will be added to the "list of tasks to be executed" in strict accordance with the triggering order and the timing, and will be scheduled in turn. The input variable and task subscription relationship can be implemented with a set of templates, and the design of logically related tasks becomes concise.
[0159] Taking the above task 1 as an example to describe the execution logic of the asynchronous task async_run, a simple example is used to illustrate it.
[0160] Prerequisites:
[0161] Variable definition:
[0162] struct value_input_output input_a / / input variable
[0163] struct value_input_output input_b / / input variable
[0164] struct value_input_output output_c / / output variable
[0165] struct value_input_output output_d / / output variable
[0166] Calculation logic:
[0167] The calculation logic refers to the logic interface registered in callback_logic_list, as shown below:
[0168]
[0169]
[0170] Variables are logically associated with calculations:
[0171] struct value_input_output input_a is associated with calculate_logic;
[0172] struct value_input_output input_b is associated with calculate_logic.
[0173] Public data structure variable definition:
[0174] Variable a corresponds to input_a 16 bits wide
[0175] Variable b corresponds to input_b 16 bits wide
[0176] Variable c corresponds to output_c 16 bits wide
[0177] Variable d corresponds to output_d 16 bits wide
[0178] Below, based on the above prerequisites, we will take an actual operation as an example to describe the execution logic of async_run.
[0179] The original value of input_a is 1. A new value 2 is written to input_a. The newly written data in input_a will be stored in the memory area specified by new_value. Then the value in new_value is compared with the value in old_value (it is known that the written data is 16 bits wide. The data type of new_value and old_value has been forced to be converted before comparison, and it has been converted into 16-bit data). Since new_value is different from old_value, the asynchronous task async_run is triggered to execute.
[0180] async_run first reads in "variables input_a, input_b, output_c, output_d", forces the new_value of input_a to be converted into 16-bit data, and then writes it into variable a in the public data structure, forces the new_value of input_a to be converted into 16-bit data, and then writes it into variable b in the public data structure, forces the new_value of output_c to be converted into 16-bit data, and then writes it into variable c in the public data structure, and forces the new_value of output_d to be converted into 16-bit data, and then writes it into variable d in the public data structure.
[0181] Execute the registered side logic interface calculate_logic on callback_logic_list (the content of the logic interface is customized, and the operation logic of variables a, variable b, variable c, and variable d are customized according to actual needs). Since the public data structure variable a is written to 2, the condition is met, and the public data structure variable c is updated to 2. At this point, the calculation logic of calculate_logic is executed.
[0182] Then, the variable c obtained by the settlement in the public data structure is output to the new_value in output_c, and the variable d obtained by the settlement in the public data structure is output to the new_value in output_d.
[0183] The above takes Task 1 as an example to illustrate the execution logic of asynchronous tasks. Output_c and output_d are used as input variables of Task 2. When data is input into output_c and output_d, Task 2 will be automatically triggered to schedule execution. All logic is automatically executed and strictly guaranteed.
[0184] From variable input to task scheduling:
[0185] Task scheduling belongs to the public interface, which is not the focus of the present invention, and will not be described here. Here, only the logical relationship of variable input triggering task scheduling is explained.
[0186] As shown in Figure 7, when a variable is input, new_value and old_value are compared first. If new_value and old_value are not equal, the asynchronous task async_run is triggered. Async_run calls the callback_logic_list logic interface registered on the input variable to perform calculations, and outputs the calculation results to the new_value of the recipient variable. In this way, the calculation logic subscribed to the recipient variable can be automatically triggered to execute automatically (the above data structure only extracts the members related to the present invention, and other members related to the public platform are not described in detail here).
[0187] It can be seen that the task processing method provided in the embodiment of the present application is aimed at the development of features or modules with non-single functions, involving scenarios with multi-object multi-input and multi-output coupling, and scenarios with strict logical dependencies and strict sequential relationships in the execution process. It provides abstraction of common execution logic and simplifies the design process. At the same time, for design scenarios with strict execution order requirements, it can automatically maintain the order of their execution logic.
[0188] Corresponding to the above method embodiment, the embodiment of the present application further provides a task processing device. The task processing device described below and the task processing method described above can refer to each other.
[0189] As shown in FIG8 , the device includes the following modules:
[0190] New value acquisition module 101, used to obtain a new value of the target variable;
[0191] A data writing module 102, used to write a new value into new data of a variable data structure; wherein the variable data structure corresponds to a target variable;
[0192] The data reading module 103 is used to read old values from old data in the variable data structure;
[0193] The task triggering module 104 is used to insert the target task subscribed by the target variable into the list of tasks to be executed when the old value is inconsistent with the new value; wherein the tasks to be executed in the list of tasks to be executed are logic functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables;
[0194] The task execution module 105 is used to execute the tasks in the to-be-executed task list in the order in which the tasks are inserted, after the old values in the old data are updated with the new values in the new data.
[0195] Apply the device provided in the embodiment of the present application to obtain a new value of the target variable; write the new value into new data in the variable data structure; wherein the variable data structure corresponds to the target variable; read the old value from the old data in the variable data structure; when the old value is inconsistent with the new value, insert the target task subscribed by the target variable into the list of tasks to be executed; wherein the tasks to be executed in the list of tasks to be executed are logical functions to be called, and the sequence of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables; after updating the old value in the old data with the new value in the new data, execute the tasks in the list of tasks to be executed in the order in which the tasks are inserted.
[0196] In the present application, after obtaining the new value of the target variable, the new value can be written into the new data of the variable data structure. The variable has a corresponding relationship with the variable data structure, and the old value of the target variable can be read based on the old data of the variable data structure. By comparing the new and old values of the target variable, the execution of the target task can be triggered when it is determined that the new value is different from the old value. Specifically, the target task can be inserted into the list of tasks to be executed by subscribing the target variable to the target task, when it is determined that the new and old values are different. The tasks to be executed in the list of tasks to be executed are all logical functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the numerical changes in response to the changes. In other words, based on the changes in the variables, it can be determined whether to execute the corresponding logical functions, and the order of executing these logical functions corresponds to the time sequence of the changes in the variables. In this way, after completing the update of the new and old data, the corresponding tasks can be executed in the order of task insertion.
[0197] Technical effect: This application can detect the value change of variables with the help of variable data structure; with the help of subscription mechanism, the variable change triggers the relevant logic function; with the help of the list of tasks to be executed, it can be clearly determined that the execution order of the logic function corresponds to the timing of the value change of the relevant variables. In this way, the logic design of the polling mode can be greatly simplified, the accuracy of the logic design can be guaranteed, and the reliability of task execution can be guaranteed.
[0198] In a specific implementation of the present application, the task triggering module is specifically used to traverse the callback linked list corresponding to the target variable to obtain the task subscribed by the target variable;
[0199] Determine the traversed tasks as target tasks;
[0200] Insert the target task at the end of the list of tasks to be executed.
[0201] In a specific implementation of the present application, the callback linked list is a single linked list, and the task trigger module is specifically used to traverse each element in the callback linked list in order of first-in-first-out to obtain the task subscribed by the target variable; accordingly, the target task is inserted into the tail of the task list to be executed in order according to the order in which the tasks are obtained.
[0202] In a specific implementation of the present application, the task execution module is specifically used to read the current task from the list of tasks to be executed in a first-in-first-out order;
[0203] Call the logic function interface corresponding to the current task to execute the current task;
[0204] Correspondingly, after executing the current task, it also includes:
[0205] Triggering a new number acquisition module to obtain a first new value of a first variable output by the logic function interface;
[0206] A data writing module, used for writing a first new value into the first new data in the first variable data structure; wherein the first variable data structure corresponds to the first variable;
[0207] A data reading module, used for reading a first old value from the first old data of the first variable data structure;
[0208] A task triggering module is used to update the first old value in the first old data using the first new value in the first new data when the first old value is inconsistent with the first new value; insert the first task subscribed by the first variable into the list of tasks to be executed so as to wait for the execution of the first task in the order of insertion.
[0209] In a specific implementation of the present application, the new data and the old data in the variable data structure are both typeless pointers; the typeless pointers record the memory addresses allocated for the new value and the old value;
[0210] Correspondingly, the data reading module is specifically used to use the typeless pointer corresponding to the old data to read the old value from the memory address storing the old data.
[0211] In a specific implementation of the present application, the data writing module is specifically used to determine the data type of the new value, and use the data type to determine the bit width of the new value;
[0212] Force the data type of the new and old data to be the same data type as the new value;
[0213] Write the new value into the new data after data type conversion.
[0214] In a specific implementation of the present application, the new number acquisition module is specifically used to obtain new values of multiple specified variables;
[0215] Performing a preset logical operation on the new values of the multiple variables to obtain a logical operation result; the preset logical operation includes at least one of an AND logical operation, an OR logical operation, and a NON-logical operation;
[0216] Determine the result of the logical operation as the new value of the target variable.
[0217] Corresponding to the above method embodiment, the embodiment of the present application further provides an electronic device. The electronic device described below and the task processing method described above can refer to each other.
[0218] As shown in FIG9 , the electronic device includes:
[0219] A memory 332, for storing computer programs;
[0220] The processor 322 is used to implement the steps of the task processing method of the above method embodiment when executing a computer program.
[0221] Specifically, please refer to Figure 10, which is a schematic diagram of the specific structure of an electronic device provided in this embodiment. The electronic device may have relatively large differences due to different configurations or performances, and may include one or more processors (central processing units, CPU) (for example, one or more processors) and a memory 332, and the memory 332 stores one or more computer programs 342 or data 344. Among them, the memory 332 can be a temporary storage or a permanent storage. The program stored in the memory 332 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the data processing device. Furthermore, the processor 322 can be configured to communicate with the memory 332 to execute a series of instruction operations in the memory 332 on the electronic device 301.
[0222] The electronic device 301 may further include one or more power supplies 326 , one or more wired or wireless network interfaces 350 , one or more input and output interfaces 358 , and / or one or more operating systems 341 .
[0223] The steps in the task processing method described above can be implemented by the structure of an electronic device.
[0224] Corresponding to the above method embodiment, the embodiment of the present application further provides a readable storage medium. The readable storage medium described below and the task processing method described above can refer to each other.
[0225] A readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the task processing method of the above method embodiment are implemented.
[0226] The readable storage medium may specifically be a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, or other readable storage medium that can store program codes.
[0227] Corresponding to the above method embodiment, the embodiment of the present application further provides a computer program product. The computer program product described below and the task processing method described above can be referred to in correspondence with each other.
[0228] A computer program product includes a computer program / instruction. When the computer program / instruction is executed by a processor, the steps of the above-mentioned task processing method are implemented.
[0229] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0230] Those skilled in the art may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented with electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0231] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0232] Finally, it should be noted that, in this article, relationships such as first and second, etc. are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms include, include or any other variations are intended to cover non-exclusive inclusion, so that a process, method, article or device that includes a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device.
[0233] Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core idea of the present application. At the same time, for those skilled in the art, according to the idea of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A task processing method, characterized in that: include: Get the new value of the target variable; Writing the new value into new data of a variable data structure; wherein the variable data structure corresponds to the target variable; Reading an old value from old data in the variable data structure; In the case where the old value is inconsistent with the new value, inserting the target task subscribed by the target variable into the list of tasks to be executed; wherein the tasks to be executed in the list of tasks to be executed are logic functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables; After the old values in the old data are updated with the new values in the new data, the tasks in the to-be-executed task list are executed in the order in which the tasks are inserted.
2. The method according to claim 1, characterized in that Insert the target task subscribed by the target variable into the list of tasks to be executed, including: Traverse the callback linked list corresponding to the target variable to obtain the task subscribed by the target variable; Determine the task obtained through traversal as the target task; Insert the target task at the end of the list of tasks to be executed.
3. The method according to claim 2, characterized in that The callback linked list is a single linked list. Traversing the callback linked list corresponding to the target variable to obtain the task subscribed by the target variable includes: According to the first-in-first-out order, traverse each element in the callback linked list to obtain the task subscribed by the target variable; Accordingly, inserting the target task into the tail of the list of tasks to be executed includes: The target task is inserted into the tail of the to-be-executed task list in order according to the task acquisition order.
4. The method according to claim 1, characterized in that: Executing the tasks in the to-be-executed task list in the order in which the tasks are inserted includes: Read the current task from the list of tasks to be executed in a first-in-first-out order; Calling the logic function interface corresponding to the current task to execute the current task; Correspondingly, after executing the current task, it also includes: Obtaining a first new value of a first variable output by the logic function interface; Writing the first new value into the first new data in the first variable data structure; wherein the first variable data structure corresponds to the first variable; Reading a first old value from the first old data in the first variable data structure; When the first old value is inconsistent with the first new value, updating the first old value in the first old data with the first new value in the first new data; Insert the first task subscribed by the first variable into the list of tasks to be executed, so as to wait for execution of the first task in the order of insertion.
5. The method according to claim 1, characterized in that The new data and the old data in the variable data structure are both typeless pointers; the typeless pointers record the memory addresses allocated for the new value and the old value; Accordingly, reading the old value from the old data of the variable data structure includes: The old value is read from the memory address storing the old data using the typeless pointer corresponding to the old data.
6. The method according to claim 5, characterized in that Writing the new value into the new data of the variable data structure comprises: Determining a data type of the new value, and using the data type to determine a bit width of the new value; Forcefully convert the data types of the new data and the old data to the same data type as the new value; The new value is written into the new data after the data type conversion.
7. The method according to any one of claims 1 to 6, characterized in that: Get new values for the target variable, including: Get the new values of multiple specified variables; Performing a preset logical operation on the new values of the multiple variables to obtain a logical operation result; the preset logical operation includes at least one of an AND logical operation and an OR logical operation and a NOT logical operation; The logical operation result is determined as a new value of the target variable.
8. A task processing device, characterized in that: include: New value acquisition module, used to obtain the new value of the target variable; A data writing module, used for writing the new value into new data of a variable data structure; wherein the variable data structure corresponds to the target variable; A data reading module, used for reading old values from old data in the variable data structure; A task triggering module, used for inserting the target task subscribed by the target variable into a list of tasks to be executed when the old value is inconsistent with the new value; wherein the tasks to be executed in the list of tasks to be executed are logic functions to be called, and the order of the tasks to be executed corresponds to the time sequence of the value changes of the corresponding variables; The task execution module is used to execute the tasks in the to-be-executed task list in the order in which the tasks are inserted, after the old values in the old data are updated with the new values in the new data.
9. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, configured to implement the steps of the task processing method according to any one of claims 1 to 7 when executing the computer program.
10. A readable storage medium, characterized in that: The readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the task processing method according to any one of claims 1 to 7 are implemented.
