Coroutine-based locking methods and devices

By repeatedly executing the lock operation in the initial coroutine, the attempt to lock operation is performed on the coroutine in the task queue, and the task switching is performed using pure user locks, which solves the problem of high cost of lock waiting and thread switching in the existing mutex technology, and achieves more efficient lock acquisition and task switching.

CN119025293BActive Publication Date: 2025-05-16XIAN TONGXING HENGYAO INFORMATION TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411060847.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-02
Publication Date
2025-05-16
Estimated Expiration
2044-08-02

AI Technical Summary

Technical Problem

When lock waiting and thread switching, existing mutex technology requires frequent switching between user state and kernel state, resulting in high switching costs; at the same time, the real-time performance of interrupt processing is poor.

Method used

The coroutine-based locking method is adopted. By repeatedly executing the attempted locking operation in the initial coroutine, the coroutine in the task queue is executed, and a pure user state lock is used to switch tasks to avoid switching between kernel state and user state.

Benefits of technology

Reduces the cost of lock waiting and awakening, and improves the real-time performance of lock grab and task switching.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119025293B_ABST
    Figure CN119025293B_ABST
Patent Text Reader

Abstract

The present application discloses a coroutine-based locking method and device, and relates to the field of computer technology. A specific implementation of the method includes: in the process of the initial coroutine being called and executed repeatedly performing the attempted locking operation, performing the attempted locking operation on at least one coroutine in the task queue, wherein the task queue includes multiple coroutines that can be scheduled for execution; in response to determining that there is a target coroutine that has been successfully locked in at least one coroutine, determining the target coroutine as the coroutine being called and executed. This implementation effectively improves the real-time performance of lock grabbing and task switching.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, specifically to the field of computer technology, and in particular to a coroutine-based locking method and device. Background Art

[0002] Mutual exclusion (Linux implementation is called "mutex"), when mutual exclusion is performed between user-mode threads / processes, the thread / process that cannot obtain the right to run needs to wait for the opportunity to obtain the right through a certain degree of delay. This delay has two forms, namely spin waiting and suspension waiting (giving up the CPU and switching to other threads / processes).

[0003] In the prior art, mutex technology has the following two main problems: 1) Lock waiting (SYS_futex system call) or thread switching generated in the spin loop stage requires switching between user mode and kernel mode, and the thread switching cost is high; 2) When the user mode program is in the spin loop state, even if it is interrupted by interrupt processing (including timer interrupt) processing, signal processing and other operations, and switches to a thread with a higher priority or a greater chance of obtaining the lock right, the real-time performance of the interrupt is still poor. Summary of the invention

[0004] Embodiments of the present application provide a coroutine-based locking method, apparatus, device, and storage medium.

[0005] According to the first aspect, an embodiment of the present application provides a coroutine-based locking method, the method comprising: performing an attempt to lock operation on at least one coroutine in a task queue during a process in which an initial coroutine being called for execution repeatedly performs an attempt to lock operation, wherein the task queue includes multiple coroutines that can be scheduled for execution; in response to determining that there is a target coroutine in at least one coroutine that has been successfully locked, determining the target coroutine as the coroutine being called for execution.

[0006] According to the second aspect, an embodiment of the present application provides a coroutine-based locking device, which includes: an execution unit and a switching unit, wherein the execution unit is configured to perform an attempt to lock operation on at least one coroutine in a task queue during a process in which an initial coroutine being called for execution repeatedly performs an attempt to lock operation, wherein the task queue includes multiple coroutines that can be scheduled for execution; and the switching unit is configured to determine the target coroutine as the coroutine being called for execution in response to determining that there is a target coroutine that has been successfully locked in at least one coroutine.

[0007] According to the third aspect, an embodiment of the present application provides an electronic device, which includes one or more processors; a storage device, on which one or more programs are stored, and when the one or more programs are executed by the one or more processors, the one or more processors implement a coroutine-based locking method as in any embodiment of the first aspect.

[0008] According to a fourth aspect, an embodiment of the present application provides a computer-readable medium having a computer program stored thereon, which, when executed by a processor, implements a coroutine-based locking method as in any embodiment of the first aspect.

[0009] According to the fifth aspect, an embodiment of the present application provides a computer program product on which a computer program is stored, and when the program is executed by a processor, a coroutine-based locking method as in any embodiment of the first aspect is implemented.

[0010] The present application performs an attempt to lock operation on at least one coroutine in a task queue during the process of repeatedly performing an attempt to lock operation on an initial coroutine being called for execution, wherein the task queue includes multiple coroutines that can be scheduled for execution; in response to determining that there is a target coroutine that has been successfully locked in at least one coroutine, the target coroutine is determined to be the coroutine being called for execution, that is, a pure user-state lock is used (a pure user-state lock is a coroutine lock, and when a lock wait occurs, the thread suspension mechanism of the mutex is replaced by a coroutine swap, and all lock operations do not generate any system calls or thread switches) to perform task switching. Compared with the mutex mechanism of Linux, the switching between the kernel state and the user state is avoided, and the cost of lock waiting and wake-up is reduced by an order of magnitude. At the same time, by performing an attempt to lock operation on the coroutine in the task queue during the process of repeatedly performing the attempt to lock operation on the current coroutine (the current coroutine can access the context of the coroutine in the task queue), the real-time performance of lock grabbing and task switching is effectively improved.

[0011] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] Figure 1 is a flowchart of an embodiment of a coroutine-based locking method according to the present application;

[0013] Figure 2a is a flowchart of an embodiment of a coroutine-based locking method according to the present application;

[0014] Figure 2b is an architectural diagram of an embodiment of a coroutine-based locking method according to the present application;

[0015] Figure 2c is an architectural diagram of another embodiment of the coroutine-based locking method according to the present application;

[0016] Figure 2d is a flowchart of another embodiment of the coroutine-based locking method according to the present application;

[0017] Figure 3 is a flowchart of an embodiment of a coroutine-based locking device according to the present application;

[0018] Figure 4 It is a structural diagram of a computer system suitable for implementing a server of an embodiment of the present application. DETAILED DESCRIPTION

[0019] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0020] It should be noted that, in the absence of conflict, the embodiments and features in the embodiments of the present application can be combined with each other. The present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0021] Figure 1 The process 100 of an embodiment of a coroutine-based locking method that can be applied to the present application is shown. In this embodiment, the coroutine-based locking method includes the following steps:

[0022] Step 101 , while the initial coroutine being called for execution repeatedly performs an attempt to lock operation, perform an attempt to lock operation on at least one coroutine in the task queue.

[0023] In this embodiment, the execution subject can call the code component during the process of the initial coroutine being called for execution repeatedly trying to lock, that is, during the process of the initial coroutine as the current coroutine repeatedly trying to obtain the lock right of the lock, that is, the spinloop stage, and perform one or more locking attempts on one or more coroutines in the task queue through the code component.

[0024] Here, the current coroutine is the coroutine being called and executed by the coroutine scheduling thread. The task queue may include multiple coroutines that can be scheduled and executed by the coroutine scheduling thread, excluding the coroutine being called and executed.

[0025] Among them, coroutines, also known as "fibers" or "lightweight threads", have an order of magnitude lower switching cost than thread switching (thread switching costs are more than hundreds of microseconds, while a coroutine switch only takes tens of microseconds). In addition, coroutine switching does not need to fall into system calls and can be implemented entirely in user mode.

[0026] The coroutine scheduling thread is used to schedule at least one coroutine. The operation of a coroutine depends on the coroutine scheduling thread to which it belongs. After the coroutine gives up its control through yield, the coroutine scheduling thread can schedule other coroutines to run. That is, the time slice of the coroutine scheduling thread is reused between coroutines.

[0027] Here, the lock attempt can be achieved by executing the CAS (Compare And Swap) operation. CAS is a system primitive that can atomically complete the comparison and swap operations. The coroutines participating in the lock right competition can modify an atomic variable (CAS word) to their expected value through the CAS operation. The one that successfully modifies (that is, the CASword value after the atomic operation is equal to its expected value) obtains the lock right.

[0028] In addition, it should be pointed out that the attempt to lock operation performed on one or more coroutines in the task queue can be performed synchronously with the attempt to lock operation of the current coroutine, or it can be interlaced with the attempt to lock operation of the current coroutine, and this application does not limit this.

[0029] In some optional embodiments, performing an attempt to lock the at least one coroutine in the task queue includes: performing an attempt to lock the at least two coroutines in the task queue.

[0030] In this implementation, the execution subject may call the code component, and through the code component, perform one or more attempts to lock at least two coroutines in the task queue.

[0031] Specifically, the execution subject may call the code component during the process of the current coroutine repeatedly performing the lock attempt operation, and perform one or more lock attempts on all the coroutines in the task queue through the code component.

[0032] This implementation method helps more coroutines in the task queue to obtain lock rights in a timely manner by performing a lock attempt operation on at least two coroutines in the task queue.

[0033] Step 102: In response to determining that there is a target coroutine that has been successfully locked in at least one coroutine, determine the target coroutine as a coroutine that is being called for execution.

[0034] In this embodiment, if there is a target coroutine that is successfully locked (i.e., obtains the lock right) among at least one of the above-mentioned coroutines, that is, the attempted locking operation on the target coroutine is successfully locked, the successfully locked target coroutine is determined as the coroutine that is being called and executed by the coroutine scheduling thread, that is, the current coroutine is switched from the initial coroutine to the successfully locked target coroutine.

[0035] Furthermore, the execution subject may put the initial coroutine into a task queue to wait for the next execution attempt to lock the operation.

[0036] In some optional embodiments, the method further includes: in response to determining that the initial coroutine is locked successfully, outputting information of successful locking.

[0037] In this implementation, during the process of the initial coroutine being called and executed repeatedly trying to lock, that is, during the process of the initial coroutine as the current coroutine repeatedly trying to obtain the lock right, if the initial coroutine locks successfully, the information of successful locking is output.

[0038] Figure 2a A process 200 of another embodiment of a coroutine-based locking method that can be applied to the present application is shown. In this embodiment, the coroutine-based locking method includes the following steps:

[0039] Step 201, in the process of the initial coroutine being called and executed repeatedly performing the lock attempt operation, call the lock assistant module corresponding to at least one coroutine in the task queue and perform the lock attempt operation through the lock assistant module.

[0040] In this embodiment, during the process in which the initial coroutine being called for execution repeatedly performs an attempt to lock the coroutine to request locking, that is, the spin loop stage, the execution subject can call the lock helper module corresponding to each coroutine in one or more coroutines in the task queue, that is, the CAS helper module, and perform the attempt to lock the coroutine through the lock helper module.

[0041] Among them, the lock assistant module can be implemented as a callback function, which is responsible for executing the attempted lock operation on the target lock object and returning information on whether the lock is successful.

[0042] Here, when calling the locking helper modules corresponding to multiple coroutines, the execution subject can call the locking helper modules corresponding to multiple coroutines in the task queue through one call, and perform an attempt to lock the operation through the locking helper module. It can also call the locking helper modules corresponding to multiple coroutines in the task queue through multiple calls, and perform an attempt to lock the operation through the locking helper module. This application does not limit this.

[0043] Specifically, the execution subject can call the lock helper module corresponding to each coroutine in all the coroutines in the task queue at one time, and perform the try lock operation through the lock helper module to realize the try lock operation on multiple coroutines in the task queue; it can also be based on the scheduling mode of the task queue, through multiple calls, such as calling the lock helper module corresponding to one or two coroutines each time, and performing the try lock operation through the lock helper module, to realize the try lock operation on multiple coroutines in the task queue.

[0044] Among them, the scheduling mode of the task queue can be any method for solving scheduling problems in existing technology or future development technology, such as FIFO (First In First Out) queue, RR (Round-Robin) polling distribution queue, priority queue, STCF (Shortest Time-to-Completion First) shortest completion time priority, etc.

[0045] In some optional methods, calling a lock helper module corresponding to at least one coroutine in the task queue includes: based on a scheduling mode of the task queue, calling a lock helper module corresponding to at least one coroutine in the task queue.

[0046] In this implementation, the execution subject can call the locking assistant module corresponding to at least one coroutine in the task queue according to the scheduling mode of the task queue.

[0047] The scheduling mode of the task queue may include one of the following: priority queue, round-robin queue, and first-in-first-out queue.

[0048] Specifically, Figure 2b As shown, the scheduling mode in the task queue may include a first-in-first-out queue. In the process of the initial coroutine as the current coroutine repeatedly performing the attempt to lock operation, the execution subject may first call the lock helper module corresponding to the first coroutine ranked first in the task queue and perform the attempt to lock the first coroutine through the lock helper module (i.e., CAS helper module). If the first coroutine is successfully locked, the current coroutine is switched from the initial coroutine to the first coroutine (i.e., the target coroutine), and the initial coroutine is placed in the task queue and waits for resume. If the lock fails, the first coroutine is adjusted to the end of the task queue to obtain a new task queue. Further, the execution subject may call the lock helper module corresponding to the second coroutine ranked first in the new task queue and perform the attempt to lock the second coroutine through the lock helper module, and so on.

[0049] If the process of repeatedly executing the lock attempt operation of the initial coroutine as the current coroutine ends, and the initial coroutine and the coroutines executing the lock attempt operation in the task queue fail to lock successfully, the initial coroutine is put into the lock waiting queue, that is, yield.

[0050] This implementation method calls the lock assistant module corresponding to at least one coroutine in the task queue through a scheduling mode based on the task queue, which helps to improve the effectiveness of coroutine lock grabbing in the task queue.

[0051] In some optional embodiments, the method further includes: before the initial coroutine repeatedly performs the attempt to lock operation, updating the priority of the initial coroutine based on the execution time from the last time the execution duration of the initial coroutine was updated to the current time; in the process of the initial coroutine repeatedly performing the attempt to lock operation, in response to determining that the execution result of the initial coroutine's attempt to lock operation is a lock failure, calling the lock assistant module corresponding to at least one coroutine in the task queue to perform the attempt to lock operation.

[0052] In this implementation, the scheduling mode includes a priority queue. Before the initial coroutine repeatedly executes the lock attempt operation, the priority of the initial coroutine is updated based on the execution time from the last time the execution time of the initial coroutine was updated to the current time.

[0053] In the process of the initial coroutine being called for execution repeatedly performing the attempt to lock operation to request locking, the attempt to lock operation includes: performing the attempt to lock operation through the attempt to apply for lock module, updating the priority of the coroutine executing this attempt to apply for lock operation based on the duration of this attempt to apply for lock operation through the priority update module, and outputting the execution result of this attempt to apply for lock operation through the output module, and the execution result may be a successful lock or a failed lock; in response to determining that the execution result of the attempt to lock operation executed by the initial coroutine is a failed lock, calling the lock assistant module corresponding to at least one coroutine in the task queue to perform the above-mentioned attempt to lock operation on at least one coroutine.

[0054] Among them, the priority is negatively correlated with the execution time, that is, the longer the execution time, the lower the priority.

[0055] Specifically, before the initial coroutine repeatedly executes the lock attempt operation, the execution time from the time when the execution time of the initial coroutine was last updated to the current time is counted for the initial coroutine, and the priority of the initial coroutine is updated according to the above time.

[0056] Furthermore, a lock application attempt module is declared, and the lock application attempt module calls the priority update module and the output module. If the execution result output by the output module in the lock application attempt operation executed by the initial coroutine is a lock failure, then a lock assistant module corresponding to a coroutine (for example, coroutine A) in the task queue is called to perform the lock application attempt operation, that is, the lock application attempt operation is performed through the lock application attempt module, the priority of coroutine A is updated based on the duration of this lock application attempt operation through the priority update module, and the execution result of this lock application attempt operation is output through the output module, for example, lock failure.

[0057] In some optional embodiments, the method further includes: in response to determining that there is a target coroutine that has been successfully locked in at least one coroutine or there is a target coroutine with a higher priority than the initial coroutine in the task queue, calculating the priority of the initial coroutine, and placing the initial coroutine in the task queue based on the priority of the initial coroutine.

[0058] In this implementation, the scheduling mode includes a priority queue. In the process of the initial coroutine being called for execution repeatedly performing the attempt to lock operation to request locking, the execution subject calls the lock assistant module corresponding to at least one coroutine in the task queue and performs the attempt to lock operation through the lock assistant module. If there is a coroutine in at least one coroutine, that is, the target coroutine, which is locked successfully or there is a target coroutine in the task queue with a higher priority than the initial coroutine, the priority of the initial coroutine can be calculated, and based on the priority of the initial coroutine, the initial coroutine can be placed in the task queue. The initial coroutine added to the task queue can subsequently perform an attempt to lock operation by calling the corresponding lock assistant module of the coroutine being scheduled for execution.

[0059] It should be pointed out that the target coroutine that is locked successfully has a higher degree of urgency than the initial coroutine lock operation. Therefore, even if the priority of the target coroutine is lower than that of the initial coroutine, the current coroutine will be switched from the initial coroutine to the target coroutine. If it is not switched, other coroutines requesting the same lock will be delayed. Furthermore, if there is no target coroutine that is locked successfully, and there is a target coroutine with a higher priority than the initial coroutine in the task queue, the current coroutine will be switched from the initial coroutine to the target coroutine with a higher priority, that is, the target coroutine with a higher priority will be determined as the coroutine currently being scheduled for execution.

[0060] This implementation method calculates the priority of the initial coroutine in response to determining that there is a target coroutine that has been successfully locked in at least one coroutine, and puts the initial coroutine into the task queue based on the priority of the initial coroutine, so that when the initial coroutine subsequently becomes the coroutine being scheduled for execution again (i.e., the current coroutine), the lock attempt can continue to be performed on it.

[0061] In some optional embodiments, the method further includes: in response to determining that there is a target coroutine with a higher priority than the initial coroutine in the task queue, determining the target coroutine with the higher priority as the coroutine being called for execution.

[0062] In this implementation, when the initial coroutine being called for execution repeatedly attempts to lock, if it is determined that there is a target coroutine with a higher priority than the initial coroutine in the task queue, the target coroutine with a higher priority is determined as the coroutine being called for execution.

[0063] This implementation method helps to lock the higher priority coroutine in the task queue in time by determining that there is a target coroutine with a higher priority than the initial coroutine in the task queue and determining the higher priority target coroutine as the coroutine being called for execution.

[0064] In some optional embodiments, the method further includes: counting the number of times the initial coroutine executes the lock attempt operation; in response to determining that the counted number reaches a preset number threshold, and the initial coroutine and at least one coroutine have not been locked successfully, placing the initial coroutine in a lock waiting queue.

[0065] In this implementation, while the initial coroutine being called for execution repeatedly performs lock attempts to request locks, the execution subject can count the number of times the initial coroutine performs lock attempts. If it is determined that the statistical number reaches a preset threshold, and both the initial coroutine and the coroutines in the task queue that perform lock attempts fail to lock successfully, the initial coroutine will be placed in the lock waiting queue.

[0066] Among them, the lock waiting queue is used to maintain the coroutines suspended and waiting on the lock object. When the unlocking operation is performed, a coroutine will be popped out of the queue and mounted to the task queue of the coroutine scheduling thread where it is located.

[0067] Specifically, Figure 2c As shown. If it is determined that the statistical number reaches the preset number threshold, and the initial coroutine and the coroutine in the task queue that calls the lock assistant module (i.e., CAS helper module) to operate the atomic state value (i.e., CAS word) of the lock object have not been successfully locked, that is, they have not obtained the lock right of the lock object, then the initial coroutine will be placed in the lock waiting queue. When unlocking, a coroutine in the lock waiting queue will be mounted to the task queue.

[0068] Furthermore, if it is determined that the statistical number of times does not reach the preset number threshold, the initial coroutine is locked successfully, and the information of successful locking is output; if it is determined that the statistical number of times does not reach the preset number threshold, and there is a target coroutine that is successfully locked among the coroutines that execute the attempted locking operation in the task queue, the current coroutine is switched from the initial coroutine to the target coroutine, and the initial coroutine is placed in the task queue.

[0069] Specifically, Figure 2d As shown, after the process starts, the execution subject can first count the number of times the initial coroutine that is being called for execution attempts to lock, that is, count the number of spin loops, and determine whether the counted number of spin loops reaches the preset number threshold. If so, and both the initial coroutine and the coroutines that perform the attempt to lock operation in the task queue have not obtained the lock, the initial coroutine will be placed in the lock waiting queue, that is, yield (it can be awakened during the unlocking operation and accept the next scheduling execution), or if so, and there is a target coroutine that successfully locks among the coroutines that perform the attempt to lock operation in the task queue, that is, the lock is obtained, and the initial coroutine is placed in the task queue waiting for rescheduling execution (that is, yield first, and then it will be rescheduled for execution as the priority is increased or based on the scheduling algorithm of other task queues); if not, continue to execute the attempt to lock operation and increase the number of spin loops.

[0070] Further, determine whether the initial coroutine as the current coroutine is locked successfully, that is, whether the lock is obtained. If so, output the information of successful locking and end the process; if not, according to the scheduling mode of the task queue, for example, the priority queue, determine one or more coroutines in the task queue, perform an attempt to lock the one or more coroutines in the task queue, and recalculate the priority of the one or more coroutines that perform the attempt to lock in the above task queue. Further, determine whether there is a target coroutine that has been locked successfully in one or more coroutines or whether there is a target coroutine with a higher priority than the initial coroutine in the task queue. If so, switch the current coroutine from the initial coroutine to the target coroutine, that is, determine the target coroutine as the coroutine that the coroutine scheduling thread is calling and executing, and put the initial coroutine into the task queue; if not, re-determine whether the counted number of spinloops reaches the preset number threshold.

[0071] Step 202: In response to determining that there is a target coroutine that has been successfully locked in at least one coroutine, the target coroutine is determined to be a coroutine that is being called for execution.

[0072] In this embodiment, the implementation details and technical effects of step 202 can refer to the description of step 102 and will not be repeated here.

[0073] from Figure 2a It can be seen that Figure 1Compared with the corresponding embodiments, the process 200 of the coroutine-based locking method in this embodiment reflects that in the process of the initial coroutine being called for execution repeatedly executing the attempt to lock operation, the locking assistant module corresponding to at least one coroutine in the task queue is called and the attempt to lock operation is performed through the locking assistant module. In response to determining that there is a target coroutine that has been successfully locked in at least one coroutine, the successfully locked target coroutine is determined as the coroutine being called for execution, thereby improving the flexibility of controlling the attempt to lock operation on the coroutine in the task queue, and further improving the real-time performance of lock grabbing and task switching.

[0074] Further references Figure 3 As an implementation of the methods shown in the above figures, the present application provides an embodiment of a locking device based on a coroutine, and the device embodiment is similar to Figure 3 Corresponding to the method embodiment shown, the device can be specifically applied to various electronic devices.

[0075] like Figure 3 As shown, the coroutine-based locking device 300 of this embodiment includes: an execution unit 301 and a switching unit 302 .

[0076] The execution unit 301 may be configured to perform a lock attempt operation on at least one coroutine in the task queue while the initial coroutine being called for execution repeatedly performs the lock attempt operation.

[0077] The switching unit 302 may be configured to, in response to determining that there is a target coroutine that has been successfully locked in at least one coroutine, determine the target coroutine as the coroutine that is being called for execution.

[0078] In some optional embodiments of this embodiment, the execution unit is further configured to call a lock assistant module corresponding to at least one coroutine in the task queue and perform a lock attempt operation through the lock assistant module.

[0079] In some optional embodiments of this embodiment, the execution unit is further configured to call a locking assistant module corresponding to at least one coroutine based on a scheduling mode of a task queue.

[0080] In some optional embodiments of this embodiment, the device also includes an update unit, which is configured to update the priority of the initial coroutine based on the execution time from the last time the execution time of the initial coroutine was updated to the current time before the initial coroutine repeatedly performs the attempt to lock operation; in the process of the initial coroutine repeatedly performing the attempt to lock operation, in response to determining that the execution result of the initial coroutine's attempt to lock operation is a lock failure, call the lock assistant module corresponding to at least one coroutine in the task queue to perform the attempt to lock operation.

[0081] In some optional embodiments of this embodiment, the device also includes a computing unit, which is configured to calculate the priority of the current coroutine in response to determining that there is a target coroutine that is successfully locked in at least one coroutine or there is a target coroutine with a higher priority than the initial coroutine in the task queue, and put the initial coroutine into the task queue based on the priority of the initial coroutine.

[0082] In some optional aspects of this embodiment, the device further includes an output unit, and the output unit is configured to output locking success information in response to determining that the initial coroutine is locked successfully.

[0083] In some optional embodiments of this embodiment, the device also includes a statistical unit, which is configured to count the number of times the initial coroutine executes an attempted locking operation; in response to determining that the statistical number reaches a preset number threshold, and the initial coroutine and at least one coroutine have not been locked successfully, the initial coroutine is placed in a lock waiting queue.

[0084] In some optional aspects of this embodiment, the switching unit is further configured to, in response to determining that there is a target coroutine with a higher priority than the initial coroutine in the task queue, determine the target coroutine with a higher priority as the coroutine being called for execution.

[0085] According to an embodiment of the present application, the present application also provides an electronic device and a readable storage medium.

[0086] like Figure 4 , is a block diagram of an electronic device according to a coroutine-based locking method according to an embodiment of the present application.

[0087] 400 is a block diagram of an electronic device according to a coroutine-based locking method according to an embodiment of the present application. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present application described and / or required herein.

[0088] like Figure 4As shown, the electronic device includes: one or more processors 401, a memory 402, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. The various components are interconnected using different buses and can be installed on a common mainboard or installed in other ways as needed. The processor can process instructions executed in the electronic device, including instructions stored in or on the memory to display graphical information of a GUI on an external input / output device (such as a display device coupled to an interface). In other embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple electronic devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 4 A processor 401 is taken as an example.

[0089] The memory 402 is a non-transient computer-readable storage medium provided in the present application. The memory stores instructions executable by at least one processor to enable the at least one processor to execute the coroutine-based locking method provided in the present application. The non-transient computer-readable storage medium of the present application stores computer instructions, which are used to enable a computer to execute the coroutine-based locking method provided in the present application.

[0090] The memory 402 is a non-transient computer-readable storage medium that can be used to store non-transient software programs, non-transient computer executable programs and modules, such as program instructions / modules corresponding to the coroutine-based locking method in the embodiment of the present application (for example, the attached Figure 3 The processor 401 executes various functional applications and data processing of the server by running the non-transient software programs, instructions and modules stored in the memory 402, that is, the coroutine-based locking method in the above method embodiment is implemented.

[0091] The memory 402 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created by the use of a coroutine-based locked electronic device, etc. In addition, the memory 402 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory 402 may optionally include a memory remotely disposed relative to the processor 401, and these remote memories may be connected to the coroutine-based locked electronic device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0092] The electronic device of the coroutine-based locking method may further include: an input device 403 and an output device 404. The processor 401, the memory 402, the input device 403 and the output device 404 may be connected via a bus or other means. Figure 4 The example of connecting through bus is taken in the following.

[0093] The input device 403 can receive input digital or character information, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a track ball, a joystick, and other input devices. The output device 404 may include a display device, an auxiliary lighting device (e.g., an LED), and a tactile feedback device (e.g., a vibration motor). The display device may include, but is not limited to, a liquid crystal display (LCD), a light emitting diode (LED) display, and a plasma display. In some embodiments, the display device may be a touch screen.

[0094] Various implementations of the systems and techniques described herein can be realized in digital electronic circuit systems, integrated circuit systems, dedicated ASICs (application specific integrated circuits), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0095] These computer programs (also referred to as programs, software, software applications, or code) include machine instructions for programmable processors and can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. As used herein, the terms "machine-readable medium" and "computer-readable medium" refer to any computer program product, device, and / or means (e.g., disk, optical disk, memory, programmable logic device (PLD)) for providing machine instructions and / or data to a programmable processor, including a machine-readable medium that receives machine instructions as a machine-readable signal. The term "machine-readable signal" refers to any signal for providing machine instructions and / or data to a programmable processor.

[0096] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0097] The systems and techniques described herein may be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), and the Internet.

[0098] A computer system may include clients and servers. Clients and servers are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship to each other.

[0099] According to the technical solution of the embodiment of the present application, the real-time performance of lock grabbing and task switching is effectively improved.

[0100] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this application can be executed in parallel, sequentially or in different orders, as long as the expected results of the technical solution disclosed in this application can be achieved, and this document is not limited here.

[0101] The above specific implementations do not constitute a limitation on the protection scope of this application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of this application should be included in the protection scope of this application.

Claims

1. A coroutine-based locking method, the method comprising: In the process of repeatedly performing the lock attempt operation on the initial coroutine being called and executed by the coroutine scheduling thread, a lock assistant module corresponding to at least one coroutine in the task queue is called and the lock attempt operation is performed through the lock assistant module, wherein the task queue includes multiple coroutines that can be scheduled for execution, and the lock assistant module is implemented as a callback function, which is used to perform the lock attempt operation on the target lock object and return information on whether the lock is successful; In response to determining that there is a successfully locked target coroutine in the at least one coroutine, the target coroutine is determined to be a coroutine being called and executed by a coroutine scheduling thread.

2. The method according to claim 1, wherein: The locking assistant module corresponding to at least one coroutine in the calling task queue includes: Based on the scheduling mode of the task queue, the lock assistant module corresponding to the at least one coroutine is called, and the scheduling mode of the task queue includes one of the following: priority queue, polling distribution queue, and first-in-first-out queue.

3. The method according to claim 2, wherein: The scheduling mode includes a priority queue, and the method further includes: Before the initial coroutine repeatedly performs the lock attempt operation, based on the execution time from the last time the execution time of the initial coroutine was updated to the current time, the priority of the initial coroutine is updated; In the process of the initial coroutine repeatedly executing the lock attempt operation, the lock attempt operation includes: executing the lock attempt operation through the lock attempt module, updating the priority of the coroutine executing the lock attempt operation based on the duration of the lock attempt operation through the priority update module, and outputting the execution result of the lock attempt operation through the output module, wherein the execution result is lock success or lock failure; In response to determining that the execution result of the initial coroutine executing the lock attempt operation is a lock failure, a lock assistant module corresponding to at least one coroutine in the task queue is called to execute the lock attempt operation.

4. The method according to claim 2, wherein: The scheduling mode includes a priority queue, and the method further includes: In response to determining that there is a target coroutine that is successfully locked in the at least one coroutine or there is a target coroutine with a higher priority than the initial coroutine in the task queue, the priority of the initial coroutine is calculated, and based on the priority of the initial coroutine, the initial coroutine is placed in the task queue.

5. The method according to claim 2, wherein: The scheduling mode includes a priority queue, and the method further includes: In response to determining that there is a target coroutine with a higher priority than the initial coroutine in the task queue, the target coroutine with a higher priority is determined as the coroutine being called for execution.

6. The method according to claim 1, further comprising: In response to determining that the initial coroutine is locked successfully, information indicating that the locking is successful is output.

7. The method according to claim 1, further comprising: Counting the number of times the initial coroutine attempts to lock the device; In response to determining that the statistical number of times reaches a preset number threshold, and both the initial coroutine and the at least one coroutine are not locked successfully, the initial coroutine is placed in a lock waiting queue.

8. A locking device based on coroutine, the device comprising: The execution unit is configured to call a locking assistant module corresponding to at least one coroutine in a task queue and perform the locking attempt operation through the locking assistant module during the process of the initial coroutine being called and executed by the coroutine scheduling thread repeatedly performing the locking attempt operation, wherein the task queue includes a plurality of coroutines that can be scheduled for execution, and the locking assistant module is implemented as a callback function, and the callback function is used to perform the locking attempt operation on the target lock object and return information on whether the locking is successful; The switching unit is configured to, in response to determining that there is a target coroutine that has been successfully locked in the at least one coroutine, determine the target coroutine as a coroutine that is being called and executed by the coroutine scheduling thread.

9. An electronic device, characterized in that: include: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor so that the at least one processor can perform the method according to any one of claims 1 to 7.

10. A non-transitory computer-readable storage medium storing computer instructions, characterized in that: The computer instructions are used to cause the computer to execute the method according to any one of claims 1 to 7.

11. A computer program product, comprising a computer program, which, when executed by a processor, implements the method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Thread locking method and device, electronic equipment and computer readable medium

    CN115904744A

  • Distributed lock implementation method and device, computer equipment and storage medium

    CN117667421A