A lightweight single-threaded asynchronous method and system based on development environment
By defining task modules and taskless return modules in the C# environment to perform asynchronous operations, it solves the program blocking and performance overhead problems caused by the traditional asynchronous programming model, and achieves asynchronous operations with high responsiveness and low resource consumption.
Patent Information
- Application Number
- CN202411117348.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-15
- Publication Date
- 2024-10-25
- Estimated Expiration
- 2044-08-15
Smart Images

Figure CN118642695B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer software, and in particular relates to a lightweight single-thread asynchronous method and system based on a development environment. Background Art
[0002] In the traditional synchronous programming model in the C# development environment, the program will execute each operation in sequence and continue to the next operation after each operation is completed. This method will cause the program to be blocked when processing time-consuming operations, reducing the responsiveness and concurrency of the program. The asynchronous programming model allows the program to continue to perform other tasks while executing an operation without waiting for the completion of the current operation. The callback function is one of the most basic implementation methods of asynchronous programming. When an asynchronous operation is completed, a pre-registered function (i.e., callback function) will be called to process the result of the operation. This method is simple and intuitive, but it is easy to cause the callback function to be nested more and more deeply, that is, the multi-layer nested callback function makes the code difficult to understand and maintain.
[0003] Although C# provides keywords such as async and await to simplify asynchronous programming, these keywords are usually used in conjunction with task classes, which allow asynchronous code to be written in an almost synchronous manner while avoiding the traditional callback model. However, when using asynchronous methods, there is an invisible performance overhead of applying for task object memory. Although there is no direct application, the asynchronous method state machine is still executed. In the development of Unity, Unity is single-threaded, and any engine-related operations must be performed in the main thread. There is thread switching inside the asynchronous method. Although the statements after await will be executed by the main thread, the operations behind it will also have additional performance overhead, which increases the overhead of thread switching and context switching, reduces the responsiveness of the application, and also increases the consumption of memory and CPU resources. Summary of the invention
[0004] In order to solve the above technical problems, the present invention provides a lightweight single-threaded asynchronous method and system based on a development environment, which are used to solve the technical problems in the prior art.
[0005] On the one hand, the present invention provides the following technical solution, a lightweight single-threaded asynchronous method based on a development environment, comprising:
[0006] Define task modules and task behavior modules in C# environment;
[0007] Define the no-task return module and the no-task return behavior module in the C# environment;
[0008] Obtain an asynchronous task and determine whether the asynchronous task has a return value;
[0009] If the asynchronous task has a return value, a first asynchronous method is defined through the task module and the first asynchronous method is registered in the task module through the task behavior module, the asynchronous task is executed through the first asynchronous method, and it is determined whether the asynchronous task is completed. If the asynchronous task is completed, an asynchronous result is output;
[0010] If the asynchronous task has no return value, a second asynchronous method is defined through the no-task return module and the second asynchronous method is registered to the no-task return module through the no-task return behavior module, the asynchronous task is executed through the second asynchronous method, and it is determined whether the asynchronous task is completed. If the asynchronous task is completed, the asynchronous result is output.
[0011] Compared with the prior art, the beneficial effects of the present invention are as follows: the present invention first defines a task module and a task behavior module in a C# environment; then defines a no-task return module and a no-task return behavior module in a C# environment; then obtains an asynchronous task and determines whether the asynchronous task has a return value; if the asynchronous task has a return value, defines a first asynchronous method through the task module and registers the first asynchronous method to the task module through the task behavior module, executes the asynchronous task through the first asynchronous method, and determines whether the asynchronous task is completed; if the asynchronous task is completed, outputs an asynchronous result; if the asynchronous task has no return value, defines a second asynchronous method through the no-task return module and registers the second asynchronous method to the no-task return module through the no-task return behavior module; The method executes the asynchronous task and determines whether the asynchronous task is completed. If the asynchronous task is completed, the asynchronous result is output. The asynchronous method provided by the present invention can be used for asynchronous loading types returned by requests related to network downloading, resource loading, etc. By encapsulating the extended waiter method and creating a custom task, the result setting of the custom task is set when the asynchronous loading is completed. This process does not involve any thread switching and does not require a synchronization context. Moreover, external calls only require one line of code to implement the synchronous writing of GC-free asynchronous statements through await, thereby reducing the overhead of thread switching and context switching, thereby improving the responsiveness of the application. At the same time, there is no need to create additional threads or tasks to perform asynchronous operations, reducing the consumption of memory and CPU resources, writing custom wait objects, getting rid of coroutines, and achieving GC-free.
[0012] Preferably, the step of defining the task module in the C# environment includes:
[0013] Create a blank task module, define a method for obtaining a task object in the blank task module, and the method for obtaining a task object is used to return the asynchronous task object itself;
[0014] A status field is defined in the blank task module, wherein the status field at least includes a waiting state, a completed field, and an error field, and the status field is used to track the progress of the first asynchronous method;
[0015] A completion attribute is defined in the blank task module, and the completion attribute is used to return the execution status of the current asynchronous task. When the status field is a completed field, the completion attribute is false, otherwise it is true. When the asynchronous task starts, the completion attribute is accessed to see if it is true. If the completion attribute is true, the asynchronous result is output;
[0016] A method for obtaining an asynchronous result is defined in the blank task module, and the asynchronous result method is used to obtain the result of the asynchronous task;
[0017] Initializing a thread-safe queue variable in the blank task module, wherein the queue variable is used to identify available asynchronous task objects in the cache pool;
[0018] A method for creating a task object is defined in the blank task module, wherein the method for creating a task object is used to create an asynchronous task object and determine whether there is an available asynchronous task object in the cache pool when creating the object. If there is an available asynchronous task object in the cache pool, the object is obtained from the cache pool; if there is no available asynchronous task object in the cache pool, a new asynchronous task object is created;
[0019] A cache pool recycling method is defined in the blank task module, and the cache pool recycling method is used to set the state field to a waiting state when calling the cache pool recycling method and determine whether the number of objects in the cache pool exceeds a maximum preset number, and if the number of objects in the cache pool does not exceed the maximum preset number, put the corresponding asynchronous task object back into the cache pool;
[0020] A set result method is defined in the blank task module, and the set result method is used to modify the status field to a completed state and execute to the next await when the asynchronous operation is successful;
[0021] A setting exception method is defined in the blank task module, wherein the setting exception method is used to modify the status field to an error state when an asynchronous operation is abnormal, capture the exception and execute to the next await;
[0022] The blank task module after definition is marked with AsyncMethodBuilderAttribute and corresponding generic parameters are added. After the control mark and parameters are added, the blank task module inherits the ICriticalNotifyCompletion interface and implements all methods of the ICriticalNotifyCompletion interface to obtain a task module.
[0023] Preferably, the step of defining the task behavior module in the C# environment includes:
[0024] Create a blank task behavior module, define a first asynchronous task builder in the blank task behavior module, and the first asynchronous task builder is used to modify the state machine generated by the compiler async method and support the construction and execution of the first asynchronous method;
[0025] Adding an instance of a state machine and an asynchronous task object in the blank task behavior module;
[0026] A first static creation method is defined in the blank task behavior module, wherein the first static creation method is used to create a first asynchronous task and initialize an asynchronous task object;
[0027] A first start method is defined in the blank task behavior module, wherein the first start method starts the execution of the first asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine;
[0028] A first object judgment method is defined in the blank task behavior module, and the first object judgment method is used to determine whether the current state machine object is empty. If it is not empty, it is recycled and then emptied, and a set result method of the asynchronous task object is called;
[0029] A first exception judgment method is defined in the blank task behavior module, and the first exception judgment method is used to determine whether the current state machine object is empty, and if it is not empty, it is recycled and then emptied, and the setting exception method of the asynchronous task object is called;
[0030] A first callback method is defined in the blank task behavior module, the first callback method is used to re-register the first asynchronous method and call back when the asynchronous task is not completed, change the state field of the asynchronous task object and return to execute the asynchronous task;
[0031] After the definition is completed, the same generic parameters as those of the task module are added to the blank task behavior module to obtain a task behavior module.
[0032] Preferably, the step of defining a non-task return module in a C# environment includes:
[0033] Create a blank no-task return module, add a no-task completion attribute to the blank no-task return module, and set the no-task completion attribute to true;
[0034] A collaborative method is defined in the blank task-free return module, and the collaborative method is used to imitate the calling method of the coroutine;
[0035] The blank no-task return module after definition is marked with AsyncMethodBuilderAttribute to establish a connection with the no-task return behavior module, and the blank no-task return module after control marking inherits the ICriticalNotifyCompletion interface and implements all methods of the ICriticalNotifyCompletion interface to obtain a no-task return module.
[0036] Preferably, the step of defining the no-task return behavior module in the C# environment includes:
[0037] Create a blank no-task-returning behavior module, define a second asynchronous task builder in the blank no-task-returning behavior module, and the second asynchronous task builder is used to modify the state machine generated by the compiler async method and support the construction and execution of the second asynchronous method;
[0038] Adding an instance of a state machine and an asynchronous task object in the blank no-task return behavior module;
[0039] A second static creation method is defined in the blank no-task return behavior module, wherein the second static creation method is used to create a second asynchronous task and initialize the asynchronous task object;
[0040] A second start method is defined in the blank no-task return behavior module, wherein the second start method starts the execution of the second asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine;
[0041] A second object determination method is defined in the blank no-task return behavior module, the second object determination method is used to determine whether the current state machine object is empty, if not empty, recycle and then empty it, and call the set result method of the asynchronous task object;
[0042] A second exception judgment method is defined in the blank no-task return behavior module, and the second exception judgment method is used to determine whether the current state machine object is empty, and if it is not empty, it is recycled and then emptied, and the setting exception method of the asynchronous task object is called;
[0043] A second callback method is defined in the blank no-task return behavior module, where the second callback method is used to re-register the second asynchronous method and call back after the asynchronous task is not completed, change the state field of the asynchronous task object and return to execute the asynchronous task;
[0044] After the blank no-task return behavior module is defined, a no-task return behavior module is obtained.
[0045] Preferably, the steps of defining the first asynchronous method through the task module and registering the first asynchronous method in the task module through the task behavior module include:
[0046] Define a first asynchronous method that returns a custom task type, and mark the first asynchronous method with async;
[0047] Add the first synchronous statement and the first asynchronous statement to the marked first asynchronous method, and use the await keyword to wait before the first asynchronous statement to return a custom task type;
[0048] Generate a first state machine A that inherits IAsyncStateMachine through the async tag;
[0049] Define a first state machine B and control the first state machine B to inherit IAsyncStateMachine to simulate an asynchronous execution environment and control the asynchronous task to be executed in a single thread;
[0050] Creating an object of the first state machine B through a cache pool and passing a reference of the first state machine A to the first state machine B, and passing the MoveNext method inside the first state machine A to an asynchronous waiting object, so as to encapsulate the first state machine A and the MoveNext method inside the first state machine A into the first state machine B;
[0051] The first synchronous statement and the first asynchronous statement are loaded into the MoveNext method inside the first state machine B to register the first asynchronous method to the task module.
[0052] Preferably, the step of executing the asynchronous task by the first asynchronous method and determining whether the asynchronous task is completed, and if the asynchronous task is completed, outputting the asynchronous result comprises:
[0053] The startup of the first state machine B is encapsulated by the first startup method in the task behavior module, and the first state machine B is passed into the first startup method. The task behavior module receives the first startup method and executes the MoveNext method inside the first state machine B, and records the status of the first startup;
[0054] Determine whether the state of the first startup is a first preset value, if the state of the first startup is the first preset value, execute the first synchronous statement until the first asynchronous statement, the first asynchronous statement returns an asynchronous waiting object and determines whether the asynchronous waiting object is completed;
[0055] If the asynchronous waiting object is completed, the state of the asynchronous task is set to completed and the asynchronous result is output, the MoveNext method inside the first state machine B registered in the asynchronous task object is executed again, and the asynchronous result is obtained from the MoveNext method and returned through the first state machine B;
[0056] If the asynchronous waiting object is not completed, the state of the first state machine B is set to 0 and the asynchronous waiting object and the first state machine B are passed to the first callback method of the task behavior module.
[0057] Preferably, the steps of defining a second asynchronous method through the taskless return module and registering the second asynchronous method to the taskless return module through the taskless return behavior module include:
[0058] Define a second asynchronous method that returns a custom task type, and mark the second asynchronous method with async;
[0059] Add the second synchronous statement and the second asynchronous statement to the marked second asynchronous method, and use the await keyword to wait before the second asynchronous statement to return a custom task type;
[0060] Generate a second state machine A that inherits IAsyncStateMachine through the async tag;
[0061] Define a second state machine B and control the second state machine B to inherit IAsyncStateMachine to simulate an asynchronous execution environment and control the asynchronous task to be executed in a single thread;
[0062] Creating an object of the second state machine B through the cache pool and passing the reference of the second state machine A to the second state machine B, and passing the MoveNext method inside the second state machine A to the asynchronous waiting object, so as to encapsulate the second state machine A and the MoveNext method inside the second state machine A into the first state machine B;
[0063] The second synchronous statement and the second asynchronous statement are loaded into the MoveNext method inside the second state machine B to register the second asynchronous method to the task-free return module.
[0064] Preferably, the step of executing the asynchronous task by the second asynchronous method and determining whether the asynchronous task is completed, and if the asynchronous task is completed, outputting the asynchronous result comprises:
[0065] The startup of the second state machine B is encapsulated by the second startup method in the no-task return behavior module, and the second state machine B is passed into the second startup method. The no-task return behavior module receives the second startup method and executes the MoveNext method inside the second state machine B, and records the status of the first startup;
[0066] Determine whether the state of the first startup is a second preset value, if the state of the first startup is the second preset value, execute the second synchronous statement until the second asynchronous statement, the second asynchronous statement returns an asynchronous waiting object and determines whether the asynchronous waiting object is completed;
[0067] If the asynchronous waiting object is completed, the state of the asynchronous task is set to completed and the asynchronous result is output, the MoveNext method inside the second state machine B registered in the asynchronous task object is executed again, and the asynchronous result is obtained from the MoveNext method and returned through the second state machine B;
[0068] If the asynchronous waiting object is not completed, the state of the second state machine B is set to 0 and the asynchronous waiting object and the second state machine B are passed to the first callback method of the no-task return behavior module.
[0069] In a second aspect, the present invention provides the following technical solution: a lightweight single-threaded asynchronous system based on a development environment, the system comprising:
[0070] The first definition module is used to define the task module and the task behavior module in the C# environment;
[0071] The second definition module is used to define a no-task return module and a no-task return behavior module in a C# environment;
[0072] A task module is used to obtain an asynchronous task and determine whether the asynchronous task has a return value;
[0073] A first execution module is used for, if the asynchronous task has a return value, defining a first asynchronous method through the task module and registering the first asynchronous method to the task module through the task behavior module, executing the asynchronous task through the first asynchronous method, and determining whether the asynchronous task is completed, and outputting an asynchronous result if the asynchronous task is completed;
[0074] The second execution module is used to define a second asynchronous method through the no-task return module and register the second asynchronous method to the no-task return module through the no-task return behavior module if the asynchronous task has no return value, execute the asynchronous task through the second asynchronous method, and determine whether the asynchronous task is completed. If the asynchronous task is completed, output an asynchronous result. BRIEF DESCRIPTION OF THE DRAWINGS
[0075] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0076] Figure 1 A flowchart of a lightweight single-threaded asynchronous method based on a development environment provided in Embodiment 1 of the present invention;
[0077] Figure 2 A structural block diagram of a lightweight single-threaded asynchronous system based on a development environment provided in Embodiment 2 of the present invention;
[0078] Figure 3 A schematic diagram of the hardware structure of a computer provided in another embodiment of the present invention.
[0079] The embodiments of the present invention will be further described below with reference to the accompanying drawings. DETAILED DESCRIPTION
[0080] Embodiments of the present invention are described in detail below, examples of which are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the embodiments of the present invention, and should not be construed as limiting the present invention.
[0081] In the description of the embodiments of the present invention, it should be understood that the terms "length", "width", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside" and "outside" etc., indicating orientations or positional relationships, are based on the orientations or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the embodiments of the present invention and simplifying the description, and do not indicate or imply that the referred device or element must have a specific orientation, be constructed and operated in a specific orientation, and therefore should not be understood as a limitation on the present invention.
[0082] In addition, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of the embodiments of the present invention, the meaning of "plurality" is two or more, unless otherwise clearly and specifically defined.
[0083] In the embodiments of the present invention, unless otherwise clearly specified and limited, the terms "installed", "connected", "connected", "fixed" and the like should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium, it can be the internal connection of two elements or the interaction relationship between two elements. For ordinary technicians in this field, the specific meanings of the above terms in the embodiments of the present invention can be understood according to specific circumstances.
[0084] Embodiment 1
[0085] In the first embodiment of the present invention, Figure 1 As shown, a lightweight single-threaded asynchronous method based on a development environment includes:
[0086] S1. Define the task module and task behavior module in C# environment;
[0087] The steps of defining a task module in a C# environment include:
[0088] S111. Create a blank task module, and define a method for obtaining a task object in the blank task module, wherein the method for obtaining a task object is used to return the asynchronous task object itself.
[0089] S112. Define a status field in the blank task module, wherein the status field at least includes a waiting state, a completed field, and an error field, and the status field is used to track the progress of the first asynchronous method.
[0090] S113. Define a completion attribute in the blank task module, and the completion attribute is used to return the execution status of the current asynchronous task. When the status field is a completed field, the completion attribute is false, otherwise it is true. When the asynchronous task starts, access the completion attribute to see if it is true. If the completion attribute is true, output the asynchronous result.
[0091] S114. Define an asynchronous result acquisition method in the blank task module, where the asynchronous result acquisition method is used to acquire the result of the asynchronous task.
[0092] S115. Initialize a thread-safe queue variable in the blank task module, where the queue variable is used to identify available asynchronous task objects in the cache pool.
[0093] S116. Define a task object creation method in the blank task module, wherein the task object creation method is used to create an asynchronous task object and determine whether there is an available asynchronous task object in the cache pool during creation. If there is an available asynchronous task object in the cache pool, the object is obtained from the cache pool; if there is no available asynchronous task object in the cache pool, a new asynchronous task object is created.
[0094] S117. Define a cache pool recycling method in the blank task module, wherein the cache pool recycling method is used to set the status field to a waiting state when calling the cache pool recycling method and to determine whether the number of objects in the cache pool exceeds a maximum preset number; if the number of objects in the cache pool does not exceed the maximum preset number, the corresponding asynchronous task object is put back into the cache pool.
[0095] S118. Define a result setting method in the blank task module, wherein the result setting method is used to modify the status field to a completed status and execute to the next await when the asynchronous operation is successful.
[0096] S119. Define an exception setting method in the blank task module, wherein the exception setting method is used to modify the status field to an error state when an asynchronous operation is abnormal, capture the exception and execute to the next await.
[0097] S1110. Mark the blank task module after definition with AsyncMethodBuilderAttribute and add corresponding generic parameters, and control the blank task module after adding the mark and parameters to inherit the ICriticalNotifyCompletion interface and implement all methods of the CriticalNotifyCompletion interface to obtain a task module.
[0098] Specifically, in the above steps, await is an identifier introduced in C# 5.0. As a core keyword of asynchronous programming, it can be understood as an asynchronous-specific "return", which returns a running asynchronous task and waits for the asynchronous task to end before executing the statement after await; AsyncMethodBuilderAttribute is specifically an attribute used to associate a custom task type with a task behavior. This attribute also provides a mechanism for the compiler to identify and use task behaviors that match the custom task type when compiling asynchronous methods containing the async and await keywords; the CriticalNotifyCompletion interface is the interface of the underlying waiter. This interface is used here to implement a custom task object that inherits the interface as a waiter, thereby achieving the effect of optimizing the original method by the custom waiter.
[0099] Specifically, for the defined task module, the module is marked with the attribute AsyncMethodBuilderAttribute, with a generic parameter, which is used for the return value type of the asynchronous method. In order to be waited by the await keyword, the module needs to define a method for obtaining the task object, which returns the custom task object itself. With this method, it is also necessary to inherit the ICriticalNotifyCompletion interface and implement all methods within the interface. When the asynchronous task needs to wait, the state machine will call the interface to register the callback; the module defines a status field (waiting, completed, and errored) to track the progress of the asynchronous method; the module also contains a completion attribute, which is used to return the execution status of the current asynchronous task. When the status field is waiting, the completion field is false, otherwise the completion field is true, and it will be accessed at the beginning of the task to determine whether the completion field is true. If it is completed, it will return directly; the module contains a method for obtaining asynchronous results. After the asynchronous task ends, the state machine will call it to obtain the result and return it. The two members of the completion field and the task result cannot be omitted, and the compiler will check them during compilation. If a default situation occurs, the compilation will fail; the module initializes a A thread-safe queue variable is used to store available task objects in the pool. It includes a method for creating task objects. When creating a task object, it first determines whether there is an available asynchronous task object in the storage pool. If so, it is obtained from the storage pool, otherwise a new asynchronous task object is created directly; this module includes a recycling method for the cache pool. When the recycling method is called, the status field in the module is set to waiting, and it is determined whether the number of storage pools has reached the maximum value of 1000. If not, the object is put back into the pool and GC is eliminated through the object pool; this module includes a method for setting results, which is called when the asynchronous operation is successful, modifies the status field to completed, and executes to the next await; this module includes a method for setting exceptions. When an asynchronous operation is abnormal, the status field is modified to have an error, the exception is captured, and the execution is continued to the next await.
[0100] The steps of defining a task behavior module in a C# environment include:
[0101] S121. Create a blank task behavior module, define a first asynchronous task builder in the blank task behavior module, and the first asynchronous task builder is used to modify the state machine generated by the compiler async method and support the construction and execution of the first asynchronous method.
[0102] S122: Add an instance of a state machine and an asynchronous task object in the blank task behavior module.
[0103] S123. Define a first static creation method in the blank task behavior module, where the first static creation method is used to create a first asynchronous task and initialize the asynchronous task object.
[0104] S124. Define a first start method in the blank task behavior module, wherein the first start method starts the execution of the first asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine.
[0105] S125. Define a first object judgment method in the blank task behavior module, wherein the first object judgment method is used to determine whether the current state machine object is empty. If it is not empty, recycle it and then set it to empty, and call the set result method of the asynchronous task object.
[0106] S126. Define a first exception judgment method in the blank task behavior module, wherein the first exception judgment method is used to determine whether the current state machine object is empty. If it is not empty, the state machine object is recycled and then set to empty, and the setting exception method of the asynchronous task object is called.
[0107] S127. Define a first callback method in the blank task behavior module, where the first callback method is used to re-register the first asynchronous method and call back after the asynchronous task is not completed, change the status field of the asynchronous task object and return to execute the asynchronous task.
[0108] S128. Add the same generic parameters as those of the task module to the blank task behavior module after definition to obtain a task behavior module.
[0109] Specifically, async is an identifier introduced in C# 5.0. As the core keyword of asynchronous programming, it indicates that a method contains asynchronous operations, and the method represented by async will be executed until the first await encountered, and asynchronous operation will start after await.
[0110] For the defined task behavior module, the members contained in this module can be regarded as the interface provided by the state machine to the outside world. It can have at most one generic parameter, which is the same as the generic parameter of the task module. This type is used as the return value type of the asynchronous method. The custom asynchronous task builder is used to modify the state machine generated by the compiler async method to support the construction and execution of asynchronous methods. The module contains instances of the state machine and objects of asynchronous tasks. The module contains a first static creation method for creating a first asynchronous method generator, which is used to generate the first asynchronous method and initialize the asynchronous task object. The module implements The first start method starts the execution of the asynchronous method and initializes the state machine by receiving an instance of a reference to the state machine; the module contains the first object judgment method, which determines whether the current state machine object is empty when the method is called. If it is not empty, it will be recycled and then set to zero, and the set result method of the asynchronous task object will be called. This method is called by the state machine when the asynchronous operation succeeds; the module contains the first exception judgment method, which determines whether the current state machine object is empty when the method is called. If it is not empty, it will be recycled and then set to zero, and the set exception method of the asynchronous task object will be called. This method is called by the state machine when the asynchronous operation encounters an exception. The module contains the waiting completion method for registering the first callback method after the waiting task ends, which is also the method that the state machine needs to execute next, that is, the code after the await statement.
[0111] S2. Define the no-task return module and the no-task return behavior module in the C# environment;
[0112] The steps of defining a non-task return module in a C# environment include:
[0113] S211. Create a blank no-task return module, add a no-task completion attribute to the blank no-task return module, and set the no-task completion attribute to true.
[0114] S212: define a collaborative method in the blank task-free return module, wherein the collaborative method is used to imitate the calling method of the coroutine.
[0115] S213, marking the blank no-task return module after definition with AsyncMethodBuilderAttribute to establish a connection with the no-task return behavior module, and controlling the blank no-task return module after marking to inherit ICriticalNotifyCompletion interface and implement all methods of the ICriticalNotifyCompletion interface to obtain a no-task return module.
[0116] Among them, for the defined no-task return module, the module is marked with the attribute AsyncMethodBuilderAttribute to establish a relationship with the custom no-task return behavior module. The module also inherits the ICriticalNotifyCompletion interface and implements all methods within the interface. The module also contains a no-task completion attribute and is set to true; the module also defines a collaborative method to imitate the calling method of the coroutine.
[0117] The steps of defining a non-task return behavior module in a C# environment include:
[0118] S221. Create a blank no-task-returning behavior module, define a second asynchronous task builder in the blank no-task-returning behavior module, and the second asynchronous task builder is used to modify the state machine generated by the compiler async method and support the construction and execution of the second asynchronous method.
[0119] S222: Add an instance of a state machine and an asynchronous task object in the blank no-task return behavior module.
[0120] S223. Define a second static creation method in the blank no-task return behavior module, where the second static creation method is used to create a second asynchronous task and initialize the asynchronous task object.
[0121] S224. Define a second start method in the blank no-task return behavior module, wherein the second start method starts the execution of the second asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine.
[0122] S225. Define a second object judgment method in the blank no-task return behavior module, and the second object judgment method is used to determine whether the current state machine object is empty. If it is not empty, it is recycled and then set to zero, and the set result method of the asynchronous task object is called.
[0123] S226. Define a second exception judgment method in the blank no-task return behavior module, and the second exception judgment method is used to determine whether the current state machine object is empty. If it is not empty, it is recycled and then set to empty, and the setting exception method of the asynchronous task object is called.
[0124] S227. Define a second callback method in the blank no-task return behavior module, where the second callback method is used to re-register the second asynchronous method and call back after the asynchronous task is not completed, change the status field of the asynchronous task object and return to execute the asynchronous task.
[0125] S228. After the blank no-task return behavior module is defined, a no-task return behavior module is obtained.
[0126] Specifically, for the defined no-task return behavior module, the members contained in the module can be regarded as the interface provided by the state machine to the outside. The defined no-task return behavior module is used to modify the state machine generated by the compiler async method to support the construction and execution of the coroutine method. It contains an instance of the state machine and an instance of the no-task return module for initialization; the module contains a second static creation method for creating a no-task return behavior module; the module implements the second start method by receiving a reference to the state machine instance as the execution of the start asynchronous method and the initialization state machine; the module contains a second object judgment method, which determines whether the current state machine object is empty when the method is called, and if it is not empty, it is recycled and then set to zero. This method is called by the state machine when the asynchronous operation succeeds; the module contains a second exception judgment method for setting, which determines whether the current state machine object is empty when the method is called, and if it is not empty, it is recycled and then set to zero, and calls the setting exception method of the asynchronous task object, which is called by the state machine when the asynchronous operation encounters an exception. The module contains a waiting completion method for registering the second callback method after the waiting task ends, which is also the method that the state machine needs to execute next, that is, the code after the await statement.
[0127] S3, obtaining an asynchronous task and determining whether the asynchronous task has a return value;
[0128] Specifically, by judging whether the asynchronous task has a return value, a task module, a task behavior module, a non-task return module, or a non-task return behavior module is selected to execute the asynchronous task.
[0129] S4. If the asynchronous task has a return value, define a first asynchronous method through the task module and register the first asynchronous method to the task module through the task behavior module, execute the asynchronous task through the first asynchronous method, and determine whether the asynchronous task is completed. If the asynchronous task is completed, output an asynchronous result;
[0130] The steps of defining a first asynchronous method through the task module and registering the first asynchronous method to the task module through the task behavior module include:
[0131] S411. Define a first asynchronous method that returns a custom task type, and mark the first asynchronous method with async.
[0132] S412. Add a first synchronous statement and a first asynchronous statement to the marked first asynchronous method, and use the await keyword to wait before the first asynchronous statement to return a custom task type.
[0133] Specifically, the task type is the task type of the asynchronous task that needs to be executed subsequently.
[0134] S413: Generate a first state machine A that inherits IAsyncStateMachine through the async tag.
[0135] S414. Define a first state machine B and control the first state machine B to inherit IAsyncStateMachine to simulate an asynchronous execution environment and control the asynchronous task to be executed in a single thread.
[0136] Specifically, for the first state machine B, the first state machine B is a generic class, and the generic T of the module is constrained to inherit the IAsyncStateMachine interface, because the state machine generated by async compilation also inherits the interface. The module initializes a thread-safe queue variable for storing the available state machine objects in the cache pool; the module contains instantiation objects that define the state machine and the methods executed in the state machine; the module contains a parameterless constructor for assigning values to the methods to be executed in the state machine; the module also contains a state machine acquisition method. When acquiring, it first determines whether there is an available state machine in the cache pool. If so, it is acquired from the cache pool, otherwise a new state machine is directly created; the module contains a cache pool recovery method. When calling the recovery method, it determines whether the number of top pools has reached the maximum value of 1000. If not, the object is put back into the pool.
[0137] S415, creating an object of the first state machine B through the cache pool and passing the reference of the first state machine A to the first state machine B, and passing the MoveNext method inside the first state machine A to the asynchronous waiting object, so as to encapsulate the first state machine A and the MoveNext method inside the first state machine A into the first state machine B;
[0138] Specifically, for encapsulating the first state machine A into the first state machine B, a first state machine B is generated to store the generated first state machine A. The first state machine B is generated through the cache pool, that is, the first state machine B is GC-free. Since the first state machine B includes the second state machine A, it is equivalent to that the second state machine A is also taken from the cache pool. In order to avoid the system from frequently generating the first state machine B, the first state machine B in the cache pool is used to store a state machine A, thereby eliminating GC.
[0139] S416: Load the first synchronous statement and the first asynchronous statement into the MoveNext method in the first state machine B to register the first asynchronous method to the task module.
[0140] The step of executing the asynchronous task by the first asynchronous method and determining whether the asynchronous task is completed, and if the asynchronous task is completed, outputting the asynchronous result comprises:
[0141] S421. Encapsulate the startup of the first state machine B through the first startup method in the task behavior module, pass the first state machine B into the first startup method, the task behavior module receives the first startup method and executes the MoveNext method inside the first state machine B, and records the status of the first startup.
[0142] Among them, the MoveNext method is a method inside the state machine, which records synchronous statements and asynchronous statements. It is called for the first time when the state machine is established, and when the state changes, the method will be called in a callback-like method. In this application, it is called in the registration, and the timing of the call depends on when the asynchrony is completed.
[0143] S422. Determine whether the state of the first startup is a first preset value. If the state of the first startup is the first preset value, execute the first synchronous statement until the first asynchronous statement. The first asynchronous statement returns an asynchronous waiting object and determines whether the asynchronous waiting object is completed.
[0144] S423. If the asynchronous waiting object is completed, the state of the asynchronous task is set to completed and the asynchronous result is output, the MoveNext method inside the first state machine B registered in the asynchronous task object is executed again, and the asynchronous result is obtained from the MoveNext method and returned through the first state machine B.
[0145] S424: If the asynchronous waiting object is not completed, the state of the first state machine B is set to 0 and the asynchronous waiting object and the first state machine B are passed to the first callback method of the task behavior module.
[0146] S5. If the asynchronous task has no return value, define a second asynchronous method through the no-task return module and register the second asynchronous method to the no-task return module through the no-task return behavior module, execute the asynchronous task through the second asynchronous method, and determine whether the asynchronous task is completed. If the asynchronous task is completed, output the asynchronous result.
[0147] The steps of defining a second asynchronous method through the no-task return module and registering the second asynchronous method to the no-task return module through the no-task return behavior module include:
[0148] S511. Define a second asynchronous method that returns a custom task type, and mark the second asynchronous method with async.
[0149] S512. Add a second synchronous statement and a second asynchronous statement to the marked second asynchronous method, and use the await keyword to wait before the second asynchronous statement to return a custom task type.
[0150] S513: Generate a second state machine A that inherits IAsyncStateMachine through the async tag.
[0151] S514: Define a second state machine B and control the second state machine B to inherit IAsyncStateMachine to simulate an asynchronous execution environment and control the asynchronous task to be executed in a single thread.
[0152] S515. Create an object of the second state machine B through the cache pool and pass the reference of the second state machine A to the second state machine B, and pass the MoveNext method inside the second state machine A to the asynchronous waiting object to encapsulate the second state machine A and the MoveNext method inside the second state machine A into the first state machine B.
[0153] S516: Load the second synchronous statement and the second asynchronous statement into the MoveNext method inside the second state machine B to register the second asynchronous method to the task-free return module.
[0154] Specifically, the construction process of the second state machine B is similar to that of the first state machine B. The second state machine B depends on a blank no-task return module and a blank no-task return behavior module, and the first state machine B depends on a task module and a task behavior module.
[0155] The step of executing the asynchronous task by the second asynchronous method and determining whether the asynchronous task is completed, and if the asynchronous task is completed, outputting the asynchronous result comprises:
[0156] S521. Encapsulate the startup of the second state machine B through the second startup method in the no-task return behavior module, pass the second state machine B into the second startup method, the no-task return behavior module receives the second startup method and executes the MoveNext method inside the second state machine B, and records the status of the first startup.
[0157] S522. Determine whether the state of the first startup is the second preset value. If the state of the first startup is the second preset value, execute the second synchronous statement until the second asynchronous statement. The second asynchronous statement returns an asynchronous waiting object and determines whether the asynchronous waiting object is completed.
[0158] S523. If the asynchronous waiting object is completed, the state of the asynchronous task is set to completed and the asynchronous result is output, the MoveNext method inside the second state machine B registered in the asynchronous task object is executed again, and the asynchronous result is obtained from the MoveNext method and returned through the second state machine B.
[0159] S524: If the asynchronous waiting object is not completed, the state of the second state machine B is set to 0 and the asynchronous waiting object and the second state machine B are passed to the first callback method of the no-task return behavior module.
[0160] The lightweight single-threaded asynchronous method based on the development environment provided in the first embodiment of the present invention first defines a task module and a task behavior module in a C# environment; then defines a no-task return module and a no-task return behavior module in the C# environment; then obtains the asynchronous task and determines whether the asynchronous task has a return value; if the asynchronous task has a return value, defines a first asynchronous method through the task module and registers the first asynchronous method with the task module through the task behavior module, executes the asynchronous task through the first asynchronous method, and determines whether the asynchronous task is completed; if the asynchronous task is completed, outputs an asynchronous result; if the asynchronous task has no return value, defines a second asynchronous method through the no-task return module and registers the second asynchronous method with the no-task return module through the no-task return behavior module, and executes the asynchronous task through the second asynchronous method , and determine whether the asynchronous task is completed. If the asynchronous task is completed, the asynchronous result is output. The asynchronous method provided by the present invention can be used for asynchronous loading types returned by requests related to network downloading, resource loading, etc. By encapsulating the extended waiter method and creating a custom task, the result setting of the custom task is set when the asynchronous loading is completed. This process does not involve any thread switching and does not require a synchronization context. Moreover, external calls only require one line of code to implement the synchronous writing of GC-free asynchronous statements through await, thereby reducing the overhead of thread switching and context switching, thereby improving the responsiveness of the application. At the same time, there is no need to create additional threads or tasks to perform asynchronous operations, reducing the consumption of memory and CPU resources, writing custom wait objects, getting rid of coroutines, and achieving GC-free.
[0161] Embodiment 2
[0162] like Figure 2As shown, in the second embodiment of the present invention, a lightweight single-threaded asynchronous system based on a development environment is provided, and the system includes:
[0163] The first definition module 1 is used to define the task module and the task behavior module in the C# environment;
[0164] The second definition module 2 is used to define a no-task return module and a no-task return behavior module in a C# environment;
[0165] Task module 3, used to obtain an asynchronous task and determine whether the asynchronous task has a return value;
[0166] A first execution module 4 is used for, if the asynchronous task has a return value, defining a first asynchronous method through the task module and registering the first asynchronous method to the task module through the task behavior module, executing the asynchronous task through the first asynchronous method, and determining whether the asynchronous task is completed, and outputting an asynchronous result if the asynchronous task is completed;
[0167] The second execution module 5 is used to define a second asynchronous method through the no-task return module and register the second asynchronous method to the no-task return module through the no-task return behavior module if the asynchronous task has no return value, execute the asynchronous task through the second asynchronous method, and determine whether the asynchronous task is completed. If the asynchronous task is completed, output the asynchronous result.
[0168] Wherein, the first definition module 1 includes:
[0169] A first creation submodule is used to create a blank task module, in which a method for obtaining a task object is defined, and the method for obtaining a task object is used to return the asynchronous task object itself;
[0170] A field submodule, used to define a status field in the blank task module, wherein the status field at least includes a waiting state, a completed field, and an error field, and the status field is used to track the progress of the first asynchronous method;
[0171] The attribute submodule is used to define a completion attribute in the blank task module. The completion attribute is used to return the execution status of the current asynchronous task. When the status field is a completed field, the completion attribute is false, otherwise it is true. When the asynchronous task starts, the completion attribute is accessed to see if it is true. If the completion attribute is true, the asynchronous result is output;
[0172] A result submodule, used to define an asynchronous result acquisition method in the blank task module, wherein the asynchronous result acquisition method is used to acquire the result of the asynchronous task;
[0173] A variable submodule, used to initialize a thread-safe queue variable in the blank task module, wherein the queue variable is used to identify available asynchronous task objects in the cache pool;
[0174] An object submodule, used to define a method for creating a task object in the blank task module, wherein the method for creating a task object is used to create an asynchronous task object and determine whether there is an available asynchronous task object in the cache pool when creating the object. If there is an available asynchronous task object in the cache pool, the object is obtained from the cache pool; if there is no available asynchronous task object in the cache pool, a new asynchronous task object is created;
[0175] A recycling submodule, used to define a cache pool recycling method in the blank task module, wherein the cache pool recycling method is used to set the state field to a waiting state when calling the cache pool recycling method and to determine whether the number of objects in the cache pool exceeds a maximum preset number, and if the number of objects in the cache pool does not exceed the maximum preset number, put the corresponding asynchronous task object back into the cache pool;
[0176] A result setting submodule is used to define a result setting method in the blank task module, and the result setting method is used to modify the status field to a completed state and execute to the next await when the asynchronous operation is successful;
[0177] An exception submodule is used to define an exception setting method in the blank task module, wherein the exception setting method is used to modify the status field to an error state when an asynchronous operation is abnormal, capture the exception and execute to the next await;
[0178] The parameter submodule is used to mark the blank task module after definition with AsyncMethodBuilderAttribute and add corresponding generic parameters, and control the blank task module after the marking and parameter addition to inherit the ICriticalNotifyCompletion interface and implement all methods of the ICriticalNotifyCompletion interface to obtain the task module.
[0179] The first definition module 1 also includes:
[0180] A second creation submodule is used to create a blank task behavior module, in which a first asynchronous task builder is defined, wherein the first asynchronous task builder is used to modify a state machine generated by a compiler async method and to support construction and execution of the first asynchronous method;
[0181] A first instance submodule, used for adding an instance of a state machine and an asynchronous task object in the blank task behavior module;
[0182] A first static submodule, used to define a first static creation method in the blank task behavior module, wherein the first static creation method is used to create a first asynchronous task and initialize an asynchronous task object;
[0183] A first start submodule, used for defining a first start method in the blank task behavior module, wherein the first start method starts the execution of the first asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine;
[0184] A first object judgment submodule is used to define a first object judgment method in the blank task behavior module, wherein the first object judgment method is used to determine whether the current state machine object is empty, and if not empty, the object is recycled and then set to empty, and a set result method of the asynchronous task object is called;
[0185] A first exception determination submodule is used to define a first exception determination method in the blank task behavior module, wherein the first exception determination method is used to determine whether the current state machine object is empty, and if not empty, the state machine object is recycled and then set to empty, and a setting exception method of the asynchronous task object is called;
[0186] A first callback submodule is used to define a first callback method in the blank task behavior module, wherein the first callback method is used to re-register the first asynchronous method and call back after the asynchronous task is not completed, change the state field of the asynchronous task object and return to execute the asynchronous task;
[0187] The parameter adding submodule is used to add the same generic parameters as those of the task module to the blank task behavior module after the definition is completed, so as to obtain the task behavior module.
[0188] The second definition module 2 includes:
[0189] The third creation submodule is used to create a blank no-task return module, add a no-task completion attribute to the blank no-task return module, and set the no-task completion attribute to true;
[0190] A coroutine submodule, used to define a collaborative method in the blank no-task return module, wherein the collaborative method is used to imitate the calling mode of the coroutine;
[0191] The contact establishment submodule is used to mark the blank taskless return module after the definition is completed with AsyncMethodBuilderAttribute, and is used to establish a connection with the taskless return behavior module, and control the blank taskless return module after the marking is completed to inherit the ICriticalNotifyCompletion interface and implement all methods of the ICriticalNotifyCompletion interface to obtain the taskless return module.
[0192] The second definition module 2 also includes:
[0193] A fourth creation submodule is used to create a blank no-task return behavior module, in which a second asynchronous task builder is defined, wherein the second asynchronous task builder is used to modify the state machine generated by the compiler async method and to support the construction and execution of the second asynchronous method;
[0194] A second instance submodule is used to add an instance of a state machine and an asynchronous task object in the blank no-task return behavior module;
[0195] A second static submodule is used to define a second static creation method in the blank no-task return behavior module, wherein the second static creation method is used to create a second asynchronous task and initialize the asynchronous task object;
[0196] A second start submodule, used for defining a second start method in the blank no-task return behavior module, wherein the second start method starts the execution of the second asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine;
[0197] A second object judgment submodule is used to define a second object judgment method in the blank no-task return behavior module, wherein the second object judgment method is used to determine whether the current state machine object is empty, and if not empty, the state machine object is recycled and then set to empty, and a set result method of the asynchronous task object is called;
[0198] A second exception judgment submodule is used to define a second exception judgment method in the blank no-task return behavior module, wherein the second exception judgment method is used to determine whether the current state machine object is empty, and if not empty, recycle it and then set it to empty, and call the setting exception method of the asynchronous task object;
[0199] A second callback submodule is used to define a second callback method in the blank no-task return behavior module, wherein the second callback method is used to re-register the second asynchronous method and call back after the asynchronous task is not completed, change the state field of the asynchronous task object and return to execute the asynchronous task;
[0200] The output submodule is used after the blank no-task return behavior module is defined to obtain the no-task return behavior module.
[0201] The first execution module 4 includes:
[0202] A first asynchronous submodule is used to define a first asynchronous method that returns a custom task type, and mark the first asynchronous method with async;
[0203] The first statement submodule is used to add a first synchronous statement and a first asynchronous statement in the marked first asynchronous method, and use the await keyword to wait before the first asynchronous statement to return a custom task type;
[0204] The first state machine submodule is used to generate a first state machine A that inherits IAsyncStateMachine through an async tag;
[0205] A first control submodule, used for defining a first state machine B and controlling the first state machine B to inherit IAsyncStateMachine, so as to simulate an asynchronous execution environment and control the execution of asynchronous tasks in a single thread;
[0206] A first encapsulation submodule, used to create an object of the first state machine B through a cache pool and pass the reference of the first state machine A to the first state machine B, and pass the MoveNext method inside the first state machine A to an asynchronous waiting object, so as to encapsulate the first state machine A and the MoveNext method inside the first state machine A into the first state machine B;
[0207] The first loading submodule is used to load the first synchronous statement and the first asynchronous statement into the MoveNext method inside the first state machine B, so as to register the first asynchronous method to the task module.
[0208] The first execution module 4 also includes:
[0209] A first recording submodule, used for encapsulating the startup of the first state machine B through the first startup method in the task behavior module, passing the first state machine B into the first startup method, the task behavior module receiving the first startup method and executing the MoveNext method inside the first state machine B, and recording the state of the first startup;
[0210] A first state judgment submodule is used to judge whether the state of the first startup is a first preset value. If the state of the first startup is the first preset value, the first synchronous statement is executed until the first asynchronous statement, and the first asynchronous statement returns an asynchronous waiting object and judges whether the asynchronous waiting object is completed;
[0211] The first asynchronous result submodule is used to set the state of the asynchronous task to completion and output the asynchronous result if the asynchronous waiting object is completed, execute the MoveNext method inside the first state machine B registered in the asynchronous task object again, and obtain the asynchronous result from the MoveNext method and return it through the first state machine B;
[0212] The first transfer submodule is used for setting the state of the first state machine B to 0 and transferring the asynchronous waiting object and the first state machine B to the first callback method of the task behavior module if the asynchronous waiting object is not completed.
[0213] The second execution module 5 includes:
[0214] The second asynchronous submodule is used to define a second asynchronous method that returns a custom task type, and mark the second asynchronous method with async;
[0215] The second statement submodule is used to add a second synchronous statement and a second asynchronous statement in the marked second asynchronous method, and use the await keyword to wait before the second asynchronous statement to return a custom task type;
[0216] The second state machine submodule is used to generate a second state machine A that inherits IAsyncStateMachine through an async tag;
[0217] A second control submodule, used for defining a second state machine B and controlling the second state machine B to inherit IAsyncStateMachine, so as to simulate an asynchronous execution environment and control the execution of asynchronous tasks in a single thread;
[0218] A second encapsulation submodule, used to create an object of the second state machine B through a cache pool and pass the reference of the second state machine A to the second state machine B, and pass the MoveNext method inside the second state machine A to the asynchronous waiting object, so as to encapsulate the second state machine A and the MoveNext method inside the second state machine A into the first state machine B;
[0219] The second loading submodule is used to load the second synchronous statement and the second asynchronous statement into the MoveNext method inside the second state machine B, so as to register the second asynchronous method to the task-free return module.
[0220] The second execution module 5 also includes:
[0221] A second recording submodule, used for encapsulating the startup of the second state machine B through the second startup method in the no-task return behavior module, passing the second state machine B into the second startup method, the no-task return behavior module receiving the second startup method and executing the MoveNext method inside the second state machine B, and recording the state of the first startup;
[0222] A second state judgment submodule is used to judge whether the state of the first startup is a second preset value. If the state of the first startup is the second preset value, the second synchronous statement is executed until the second asynchronous statement, and the second asynchronous statement returns an asynchronous waiting object and judges whether the asynchronous waiting object is completed;
[0223] The second asynchronous result submodule is used to set the state of the asynchronous task to completed and output the asynchronous result if the asynchronous waiting object is completed, execute the MoveNext method inside the second state machine B registered in the asynchronous task object again, and obtain the asynchronous result from the MoveNext method and return it through the second state machine B;
[0224] The second transfer submodule is used for setting the state of the second state machine B to 0 and transferring the asynchronous waiting object and the second state machine B to the first callback method of the no-task return behavior module if the asynchronous waiting object is not completed.
[0225] In some other embodiments of the present invention, the embodiments of the present invention provide the following technical solution: a computer, comprising a memory 102, a processor 101, and a computer program stored in the memory 102 and executable on the processor 101, wherein the processor 101 implements the lightweight single-threaded asynchronous method based on the development environment as described above when executing the computer program.
[0226] Specifically, the processor 101 may include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or may be configured to implement one or more integrated circuits of the embodiment of the present invention.
[0227] Among them, the memory 102 may include a large-capacity memory for data or instructions. By way of example and not limitation, the memory 102 may include a hard disk drive (HDD), a floppy disk drive, a solid state drive (SSD), a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive, or a combination of two or more of these. Where appropriate, the memory 102 may include a removable or non-removable (or fixed) medium. Where appropriate, the memory 102 may be inside or outside the data processing device. In a specific embodiment, the memory 102 is a non-volatile memory. In a specific embodiment, the memory 102 includes a read-only memory (ROM) and a random access memory (RAM). Where appropriate, the ROM may be a mask-programmed ROM, a programmable ROM (PROM), an erasable PROM (EPROM), an electrically erasable PROM (EEPROM), an electrically alterable ROM (EAROM) or a flash memory (FLASH), or a combination of two or more of these. Under appropriate circumstances, the RAM can be a static random access memory (SRAM) or a dynamic random access memory (DRAM), wherein the DRAM can be a fast page mode dynamic random access memory (FPMDRAM), an extended data output dynamic random access memory (EDODRAM), a synchronous dynamic random access memory (SDRAM), etc.
[0228] The memory 102 may be used to store or cache various data files that need to be processed and / or used for communication, as well as possible computer program instructions executed by the processor 101 .
[0229] The processor 101 implements the above-mentioned lightweight single-threaded asynchronous method based on the development environment by reading and executing the computer program instructions stored in the memory 102 .
[0230] In some embodiments, the computer may further include a communication interface 103 and a bus 100. Figure 3 As shown, the processor 101, the memory 102, and the communication interface 103 are connected via a bus 100 and communicate with each other.
[0231] The communication interface 103 is used to implement communication between the modules, devices, units and / or equipment in the embodiment of the present invention. The communication interface 103 can also implement data communication with other components such as: external devices, image / data acquisition equipment, databases, external storage, and image / data processing workstations.
[0232] The bus 100 includes hardware, software or both, and couples the components of the computer device to each other. The bus 100 includes but is not limited to at least one of the following: a data bus, an address bus, a control bus, an expansion bus, and a local bus. By way of example and not limitation, bus 100 may include an Accelerated Graphics Port (AGP) or other graphics bus, an Extended Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an InfiniBand interconnect, a Low Pin Count (LPC) bus, a memory bus, a Micro Channel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a Serial Advanced Technology Attachment (SATA) bus, a Video Electronics Standards Association Local Bus (VLB) bus, or other suitable buses or a combination of two or more of these. Where appropriate, bus 100 may include one or more buses. Although embodiments of the present invention describe and illustrate a particular bus, the present invention contemplates any suitable bus or interconnect.
[0233] The computer can execute the lightweight single-threaded asynchronous method based on the development environment of the present invention based on the acquisition of the lightweight single-threaded asynchronous system based on the development environment, thereby realizing lightweight single-threaded asynchronous system based on the development environment.
[0234] In some further embodiments of the present invention, in combination with the above-mentioned lightweight single-threaded asynchronous method based on a development environment, an embodiment of the present invention provides the following technical solution, a storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the above-mentioned lightweight single-threaded asynchronous method based on a development environment is implemented.
[0235] Those skilled in the art will appreciate that the logic and / or steps represented in the flowchart or otherwise described herein, for example, may be considered as an ordered list of executable instructions for implementing logical functions, and may be specifically implemented in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute instructions), or in conjunction with such instruction execution systems, devices or apparatuses. For purposes of this specification, "computer-readable medium" may be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in conjunction with such instruction execution systems, devices or apparatuses.
[0236] More specific examples of readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk case (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be a paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, deciphering or, if necessary, processing in another suitable manner, and then stored in a computer memory.
[0237] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, multiple steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or a combination thereof: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0238] The technical features of the above-described embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above-described embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0239] The above-mentioned embodiments only express several implementation methods of the present invention, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for ordinary technicians in this field, several variations and improvements can be made without departing from the concept of the present invention, and these all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be subject to the attached claims.
Claims
1. A lightweight single-threaded asynchronous method based on a development environment, characterized in that: include: Define task modules and task behavior modules in C# environment; Define the no-task return module and the no-task return behavior module in the C# environment; Obtain an asynchronous task and determine whether the asynchronous task has a return value; If the asynchronous task has a return value, a first asynchronous method is defined through the task module and the first asynchronous method is registered in the task module through the task behavior module, the asynchronous task is executed through the first asynchronous method, and it is determined whether the asynchronous task is completed. If the asynchronous task is completed, an asynchronous result is output; If the asynchronous task has no return value, a second asynchronous method is defined through the no-task return module and the second asynchronous method is registered in the no-task return module through the no-task return behavior module, the asynchronous task is executed through the second asynchronous method, and it is determined whether the asynchronous task is completed. If the asynchronous task is completed, an asynchronous result is output; The steps of defining a task module in a C# environment include: Create a blank task module, define a method for obtaining a task object in the blank task module, and the method for obtaining a task object is used to return the asynchronous task object itself; A status field is defined in the blank task module, wherein the status field at least includes a waiting state, a completed field, and an error field, and the status field is used to track the progress of the first asynchronous method; A completion attribute is defined in the blank task module, and the completion attribute is used to return the execution status of the current asynchronous task. When the status field is a completed field, the completion attribute is false, otherwise it is true. When the asynchronous task starts, the completion attribute is accessed to see if it is true. If the completion attribute is true, the asynchronous result is output; A method for obtaining an asynchronous result is defined in the blank task module, and the asynchronous result method is used to obtain the result of the asynchronous task; Initializing a thread-safe queue variable in the blank task module, wherein the queue variable is used to identify available asynchronous task objects in the cache pool; A method for creating a task object is defined in the blank task module, wherein the method for creating a task object is used to create an asynchronous task object and determine whether there is an available asynchronous task object in the cache pool when creating the object. If there is an available asynchronous task object in the cache pool, the object is obtained from the cache pool; if there is no available asynchronous task object in the cache pool, a new asynchronous task object is created; A cache pool recycling method is defined in the blank task module, and the cache pool recycling method is used to set the state field to a waiting state when calling the cache pool recycling method and determine whether the number of objects in the cache pool exceeds a maximum preset number, and if the number of objects in the cache pool does not exceed the maximum preset number, put the corresponding asynchronous task object back into the cache pool; A set result method is defined in the blank task module, and the set result method is used to modify the status field to a completed state and execute to the next await when the asynchronous operation is successful; A setting exception method is defined in the blank task module, wherein the setting exception method is used to modify the status field to an error state when an asynchronous operation is abnormal, capture the exception and execute to the next await; The blank task module after definition is marked with AsyncMethodBuilderAttribute and corresponding generic parameters are added, and the blank task module after control marking and parameter addition is completed inherits ICriticalNotifyCompletion interface and implements all methods of the ICriticalNotifyCompletion interface to obtain a task module; The steps of defining the task behavior module in the C# environment include: Create a blank task behavior module, define a first asynchronous task builder in the blank task behavior module, and the first asynchronous task builder is used to modify the state machine generated by the compiler async method and support the construction and execution of the first asynchronous method; Adding an instance of a state machine and an asynchronous task object in the blank task behavior module; A first static creation method is defined in the blank task behavior module, wherein the first static creation method is used to create a first asynchronous task and initialize an asynchronous task object; A first start method is defined in the blank task behavior module, wherein the first start method starts the execution of the first asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine; A first object judgment method is defined in the blank task behavior module, and the first object judgment method is used to determine whether the current state machine object is empty. If it is not empty, it is recycled and then emptied, and a set result method of the asynchronous task object is called; A first exception judgment method is defined in the blank task behavior module, and the first exception judgment method is used to determine whether the current state machine object is empty, and if it is not empty, it is recycled and then emptied, and the setting exception method of the asynchronous task object is called; A first callback method is defined in the blank task behavior module, the first callback method is used to re-register the first asynchronous method and call back when the asynchronous task is not completed, change the state field of the asynchronous task object and return to execute the asynchronous task; Adding the same generic parameters as those of the task module to the blank task behavior module after definition is completed to obtain a task behavior module; The steps of defining a non-task return module in a C# environment include: Create a blank no-task return module, add a no-task completion attribute to the blank no-task return module, and set the no-task completion attribute to true; A collaborative method is defined in the blank task-free return module, wherein the collaborative method is used to imitate the calling method of the coroutine; The blank no-task return module after definition is marked with AsyncMethodBuilderAttribute to establish a connection with the no-task return behavior module, and the blank no-task return module after marking is controlled to inherit the ICriticalNotifyCompletion interface and implement all methods of the ICriticalNotifyCompletion interface to obtain a no-task return module; The steps of defining a non-task return behavior module in a C# environment include: Create a blank no-task-returning behavior module, define a second asynchronous task builder in the blank no-task-returning behavior module, and the second asynchronous task builder is used to modify the state machine generated by the compiler async method and support the construction and execution of the second asynchronous method; Adding an instance of a state machine and an asynchronous task object in the blank no-task return behavior module; A second static creation method is defined in the blank no-task return behavior module, wherein the second static creation method is used to create a second asynchronous task and initialize the asynchronous task object; A second start method is defined in the blank no-task return behavior module, wherein the second start method starts the execution of the second asynchronous method and initializes the state machine by receiving a reference to an instance of the state machine; A second object determination method is defined in the blank no-task return behavior module, the second object determination method is used to determine whether the current state machine object is empty, if not empty, recycle and then empty it, and call the set result method of the asynchronous task object; A second exception judgment method is defined in the blank no-task return behavior module, and the second exception judgment method is used to determine whether the current state machine object is empty, and if it is not empty, it is recycled and then emptied, and the setting exception method of the asynchronous task object is called; A second callback method is defined in the blank no-task return behavior module, where the second callback method is used to re-register the second asynchronous method and call back after the asynchronous task is not completed, change the state field of the asynchronous task object and return to execute the asynchronous task; After the blank no-task return behavior module is defined, a no-task return behavior module is obtained.
2. The lightweight single-threaded asynchronous method based on the development environment according to claim 1, characterized in that: The step of defining the first asynchronous method through the task module and registering the first asynchronous method to the task module through the task behavior module comprises: Define a first asynchronous method that returns a custom task type, and mark the first asynchronous method with async; Add the first synchronous statement and the first asynchronous statement to the marked first asynchronous method, and use the await keyword to wait before the first asynchronous statement to return a custom task type; Generate a first state machine A that inherits IAsyncStateMachine through the async tag; Define a first state machine B and control the first state machine B to inherit IAsyncStateMachine to simulate an asynchronous execution environment and control the asynchronous task to be executed in a single thread; Creating an object of the first state machine B through a cache pool and passing a reference of the first state machine A to the first state machine B, and passing the MoveNext method inside the first state machine A to an asynchronous waiting object, so as to encapsulate the first state machine A and the MoveNext method inside the first state machine A into the first state machine B; The first synchronous statement and the first asynchronous statement are loaded into the MoveNext method inside the first state machine B to register the first asynchronous method to the task module.
3. The lightweight single-threaded asynchronous method based on the development environment according to claim 2, characterized in that: The step of executing the asynchronous task by the first asynchronous method and determining whether the asynchronous task is completed, and if the asynchronous task is completed, outputting an asynchronous result comprises: The startup of the first state machine B is encapsulated by the first startup method in the task behavior module, and the first state machine B is passed into the first startup method. The task behavior module receives the first startup method and executes the MoveNext method inside the first state machine B, and records the status of the first startup; Determine whether the state of the first startup is a first preset value, if the state of the first startup is the first preset value, execute the first synchronous statement until the first asynchronous statement, the first asynchronous statement returns an asynchronous waiting object and determines whether the asynchronous waiting object is completed; If the asynchronous waiting object is completed, the state of the asynchronous task is set to completed and the asynchronous result is output, the MoveNext method inside the first state machine B registered in the asynchronous task object is executed again, and the asynchronous result is obtained from the MoveNext method and returned through the first state machine B; If the asynchronous waiting object is not completed, the state of the first state machine B is set to 0 and the asynchronous waiting object and the first state machine B are passed to the first callback method of the task behavior module.
4. The lightweight single-threaded asynchronous method based on the development environment according to claim 1, characterized in that: The step of defining a second asynchronous method through the taskless return module and registering the second asynchronous method to the taskless return module through the taskless return behavior module comprises: Define a second asynchronous method that returns a custom task type, and mark the second asynchronous method with async; Add the second synchronous statement and the second asynchronous statement to the marked second asynchronous method, and use the await keyword to wait before the second asynchronous statement to return a custom task type; Generate a second state machine A that inherits IAsyncStateMachine through the async tag; Define a second state machine B and control the second state machine B to inherit IAsyncStateMachine to simulate an asynchronous execution environment and control the asynchronous task to be executed in a single thread; Creating an object of the second state machine B through the cache pool and passing the reference of the second state machine A to the second state machine B, and passing the MoveNext method inside the second state machine A to the asynchronous waiting object, so as to encapsulate the second state machine A and the MoveNext method inside the second state machine A into the second state machine B; The second synchronous statement and the second asynchronous statement are loaded into the MoveNext method inside the second state machine B to register the second asynchronous method to the task-free return module.
5. The lightweight single-threaded asynchronous method based on the development environment according to claim 4, characterized in that: The step of executing the asynchronous task by the second asynchronous method and determining whether the asynchronous task is completed, and if the asynchronous task is completed, outputting the asynchronous result comprises: The startup of the second state machine B is encapsulated by the second startup method in the no-task return behavior module, and the second state machine B is passed into the second startup method. The no-task return behavior module receives the second startup method and executes the MoveNext method inside the second state machine B, and records the status of the first startup; Determine whether the state of the first startup is a second preset value, if the state of the first startup is the second preset value, execute the second synchronous statement until the second asynchronous statement, the second asynchronous statement returns an asynchronous waiting object and determines whether the asynchronous waiting object is completed; If the asynchronous waiting object is completed, the state of the asynchronous task is set to completed and the asynchronous result is output, the MoveNext method inside the second state machine B registered in the asynchronous task object is executed again, and the asynchronous result is obtained from the MoveNext method and returned through the second state machine B; If the asynchronous waiting object is not completed, the state of the second state machine B is set to 0 and the asynchronous waiting object and the second state machine B are passed to the first callback method of the no-task return behavior module.
6. A lightweight single-threaded asynchronous system based on a development environment, the system adopts the lightweight single-threaded asynchronous method based on a development environment as claimed in claim 1, characterized in that: The system comprises: The first definition module is used to define the task module and the task behavior module in the C# environment; The second definition module is used to define a no-task return module and a no-task return behavior module in a C# environment; A task module is used to obtain an asynchronous task and determine whether the asynchronous task has a return value; A first execution module is used for, if the asynchronous task has a return value, defining a first asynchronous method through the task module and registering the first asynchronous method to the task module through the task behavior module, executing the asynchronous task through the first asynchronous method, and determining whether the asynchronous task is completed, and outputting an asynchronous result if the asynchronous task is completed; The second execution module is used to define a second asynchronous method through the no-task return module and register the second asynchronous method to the no-task return module through the no-task return behavior module if the asynchronous task has no return value, execute the asynchronous task through the second asynchronous method, and determine whether the asynchronous task is completed. If the asynchronous task is completed, output an asynchronous result.
