Thread lock processing method, electronic device, storage medium and program product

By querying in the lock-holding queue and using CPU affinity to determine the proxy task and adding it to the temporary queue for priority operation, the problem of waiting-locking time caused by the lock-holding task is solved, and the performance of the whole machine is improved.

CN119415158BActive Publication Date: 2025-04-08ALIBABA CLOUD COMPUTING CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510018704.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-07
Publication Date
2025-04-08
Estimated Expiration
2045-01-07

AI Technical Summary

Technical Problem

In the group mixed-unit cluster, the lock holding task cannot be released in time due to current limit or suppression, resulting in a longer locking time and the performance of the entire machine is affected.

Method used

The query in the lock-holding queue corresponding to the thread lock is in the non-executive proxy state and the CPU affinity includes the tasks of the current CPU as the proxy task. If the proxy task is ready and not running, it is added to the temporary queue of the current CPU, and the temporary queue is used to run the proxy task first and release the thread lock after it is completed.

Benefits of technology

By prioritizing the operation of agent tasks, locks can be quickly released, locks can be reduced, and locks can be improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119415158B_ABST
    Figure CN119415158B_ABST
Patent Text Reader

Abstract

The present application provides a thread lock processing method, electronic device, storage medium and program product, which relates to the field of operating system technology. The method includes: when the current task is waiting to hold the thread lock, query the task that is currently in a non-executing proxy state and whose CPU affinity includes the current CPU in the lock queue corresponding to the thread lock, as the proxy task of the current task; if the status of the proxy task is ready, and the proxy task is not currently running, then add the proxy task to the temporary queue of the current CPU, use the current CPU to run the proxy task according to the temporary queue, and release the thread lock after the proxy task is completed; use the thread lock to run the current task on the current CPU. In the example of the present application, the proxy task that is in the ready state but is not currently running is added to the temporary queue, and the proxy task is run preferentially according to the temporary queue, so that the proxy task releases the lock as soon as possible, reduces the waiting time for the lock, and improves the performance of the whole machine.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of operating systems, and in particular to a thread lock processing method, electronic equipment, storage medium and program product. Background Art

[0002] Thread lock is a synchronization mechanism used to control multiple threads' access to shared resources to avoid data contention and inconsistency. Locks ensure that only one thread can access a specific code block or resource at a time, thereby preventing multiple threads from modifying shared data at the same time, which could lead to inconsistent states.

[0003] A group colocation cluster is a physical or virtual computing cluster that runs multiple different types of tasks at the same time. These tasks may come from different service departments or project teams. Group colocation clusters have problems caused by waiting for locks. For example, the lock-holding tasks cannot be released in time due to flow control or suppression, resulting in too many tasks waiting for locks, a longer waiting time, and a serious impact on the performance of the entire machine. Summary of the invention

[0004] The embodiments of the present application provide a thread lock processing method, electronic device, storage medium and program product to achieve rapid lock release of the lock holding task, reduce the lock waiting time, and improve the performance of the entire machine.

[0005] In a first aspect, an embodiment of the present application provides a thread lock processing method, including:

[0006] When the current task is waiting to hold the thread lock, query the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes the current CPU, as the proxy task of the current task; if the proxy task is in a ready state and is not currently running, add the proxy task to the temporary queue of the current CPU, use the current CPU to run the proxy task according to the temporary queue, and release the thread lock after the proxy task is completed; the entry and exit order of the temporary queue is first in, last out; use the thread lock to run the current task on the current CPU.

[0007] In a second aspect, an embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored in the memory, wherein the processor implements any of the above methods when executing the computer program.

[0008] In a third aspect, an embodiment of the present application provides a computer-readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, any of the above methods is implemented.

[0009] In a fourth aspect, an embodiment of the present application provides a computer program product, the computer program product including a computer program, and the computer program implements any of the above methods when executed by a processor.

[0010] Compared with the prior art, this application has the following advantages:

[0011] The present application provides a thread lock processing method, electronic device, storage medium and program product. When the current task is waiting to hold the thread lock, the lock holding queue corresponding to the thread lock is queried for tasks that are currently in a non-executing proxy state and whose CPU affinity includes the current CPU, as the proxy task of the current task; if the status of the proxy task is ready and the proxy task is not currently running, the proxy task is added to the temporary queue of the current CPU, and the current CPU is used to run the proxy task according to the temporary queue, and the thread lock is released after the proxy task is completed; the entry and exit order of the temporary queue is first in, last out; the thread lock is used to run the current task on the current CPU. In the example of the present application, the proxy task that is in the ready state but not currently running in the lock holding queue is added to the temporary queue. Since the entry and exit order of the temporary queue is first in, last out, the proxy task is run preferentially according to the temporary queue, so that the proxy task releases the lock as soon as possible, reduces the waiting time for the lock, and improves the performance of the whole machine.

[0012] The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] In the accompanying drawings, unless otherwise specified, the same reference numerals throughout the multiple drawings represent the same or similar parts or elements. These drawings are not necessarily drawn to scale. It should be understood that these drawings only depict some embodiments according to the present application and should not be regarded as limiting the scope of the present application.

[0014] Figure 1 The present invention is a flowchart of an application scenario of a thread lock processing method according to an embodiment of the present application.

[0015] Figure 2 Flowchart of a thread lock processing method according to an embodiment of the present application.

[0016] Figure 3 Flowchart of a thread lock processing method according to an embodiment of the present application.

[0017] Figure 4 This is a structural block diagram of a thread lock processing device according to an embodiment of the present application.

[0018] Figure 5 A block diagram of an electronic device used to implement an embodiment of the present application. DETAILED DESCRIPTION

[0019] In the following, only some exemplary embodiments are briefly described. As those skilled in the art will appreciate, the described embodiments may be modified in various ways without departing from the concept or scope of the present application. Therefore, the drawings and descriptions are considered to be exemplary in nature and not restrictive.

[0020] To facilitate understanding of the technical solutions of the embodiments of the present application, the following describes the related technologies of the embodiments of the present application. The following related technologies can be combined with the technical solutions of the embodiments of the present application as optional solutions, and they all belong to the protection scope of the embodiments of the present application.

[0021] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0022] The thread locks applicable to the technical solution of the present application include: mutex, read-write lock, lock designed based on reference counting, and user-mode lock. Among them, the mutex is a lightweight synchronization primitive with only two states: locked and idle. When the mutex is occupied, the process that attempts to lock the mutex will be dormant until the resource is available. The read-write lock is a synchronization mechanism for concurrent control of computer programs, which is used to solve read-write problems. Read operations can be reentered concurrently, and write operations are mutually exclusive. This means that multiple threads can read data at the same time, but an exclusive lock needs to be obtained when writing data. Reference counting is a memory management technology in computer programming languages, which refers to saving the number of times a resource (which can be an object, memory, or disk space, etc.) is referenced, and executing a specified operation when the reference count reaches a preset condition.

[0023] The embodiment of the present application includes a pre-configuration process: registering a global queue, and using the global queue as a locked queue locked_queue. Specifically, for kernel-mode code, the global queue is directly registered using a registration function; for user-mode code, the global queue is registered through a system call.

[0024] Add the waiting lock process call code at the waiting lock / waiting reference count; the waiting lock process call code is used to call the waiting lock function; add the holding lock process call code at the holding lock / increasing reference count; the holding lock process call code is used to call the holding lock function; add the releasing lock process call code at the releasing lock / decreasing reference count; the releasing lock process call code is used to call the releasing lock function. Specifically, for kernel-state code, add a function call at the waiting lock / waiting reference count to call the wait_lock function, add a function call at the holding lock / increasing reference count to call the hold_lock function, and add a function call at the releasing lock / decreasing reference count to call the release_lock function; for user-state code, add a system call at the waiting lock / waiting reference count to call the sys_wait_lock function, add a system call at the holding lock / increasing reference count to call the sys_hold_lock function, and add a system call at the releasing lock / decreasing reference count to call the sys_release_lock function.

[0025] Create a temporary queue for each CPU. The entry and exit order of the temporary queue is first in, last out. The scheduling priority of the temporary queue is higher than the scheduling priority of the queue of the sub-scheduling class.

[0026] After the preconfiguration is completed, the current task waits for locks, holds locks, and releases locks as follows: Figure 1 As shown, in this embodiment, the proxy flag is an attribute set for each task in the embodiment of the present application. If the value of the flag is 1, it means that the task is in the execution proxy state. In the execution proxy state, it means that the task is currently a proxy task for other tasks. If the value of the flag is 0, it means that the task is in the non-execution proxy state. The specific process is as follows:

[0027] When the current task is waiting for the lock, first, the current task tries to hold the lock to determine whether it has the lock. If so, the current task holds the lock and puts the current task into the lock holding queue. It uses the obtained lock to run on the current CPU corresponding to the current task, as shown below: Figure 1 As shown, the task continues to run. After the task is completed, the lock will be released. The current task will be removed from the lock queue and the lock will be released. Determine whether the proxy flag of the current task is 1. If so, since the task has been completed, the proxy flag of the current task will be set to 0, indicating that it is in a non-executing proxy state. Reschedule, that is, schedule the next task. If the proxy flag of the current task is not 1, that is, the current task is in a non-executing proxy state, the next task will continue to run as the current task.

[0028] If the current task does not obtain the lock, the next proxy flag in the lock holding queue corresponding to the current lock (that is, the lock that the current task wants to obtain but has not obtained) is 0, and the CPU affinity (cpu affinity) includes the task A of the current CPU as the proxy task of the current task. Among them, CPU affinity refers to the operating system scheduler's tendency to schedule specific processes or threads to run on specific processor cores. By setting CPU affinity, you can control which CPU cores can execute a specific process or thread. If the CPU affinity of task A includes the current CPU, it means that task A can be run using the current CPU.

[0029] Determine whether there is a task A that meets the conditions. If not, the current task waits for the lock according to the original logic, that is, waits for other lock-holding tasks to complete and release the lock, and then tries to hold the lock again. If there is a task A that meets the conditions, determine whether task A is in the ready state (i.e., running state). If not, it means that task A is in the sleeping state, and task A is awakened. If task A is in the ready state, determine whether task A is running on the CPU. If so, return and continue to query other tasks in the lock-holding queue for proxy tasks that meet the conditions.

[0030] If task A is not running on the CPU, the proxy flag of task A is set to 1, indicating that it is in the execution proxy state. Determine whether task A is in the queue of the current CPU (i.e., the ready queue). For example, current limiting, simultaneous multithreading (SMT) eviction, and other states are considered not in the queue of the current CPU. If task A is not in the queue of the current CPU, migrate task A to the queue of the current CPU. Determine whether the proxy flag of the current task is 1. If so, put the current task into the temporary queue (i.e., the temporary queue of the current CPU), put task A into the temporary queue, and reschedule, giving priority to selecting tasks from the temporary queue. If the proxy flag of the current task is not 1, directly put task A into the temporary queue, and reschedule, giving priority to selecting tasks from the temporary queue. Dequeue the selected task from the temporary queue. Since the order of the temporary queue is first in, last out, and task A is put into the temporary queue later, task A will run first, and task A will be scheduled on the current CPU. After running, task A releases the lock according to the process.

[0031] Among them, task A may not be scheduled to run on the CPU due to current limiting or being suppressed by other scheduling policies. The temporary queue has a higher priority than queues of other sub-scheduling classes. It can be added to the temporary queue and run first, thereby releasing the lock as soon as possible and reducing the waiting time for other tasks.

[0032] In this embodiment, the update_curr function can be used to check whether the task is limited. If it is limited and the proxy flag is 0, rescheduling is performed. Compared with the related art that tasks will be rescheduled as long as they are limited, the proxy task can obtain CPU resources to run before releasing the lock, thereby releasing the lock as soon as possible. In addition, suppression strategies such as SMT eviction are usually implemented based on the Inter-Processor Interrupt (IPI) signal. In the related art, after receiving the IPI signal, the CPU will determine whether the currently running task should be suppressed. If the task should be suppressed, the task will be scheduled to the CPU by rescheduling. In this embodiment, rescheduling is performed only when the task should be suppressed and the proxy flag is 0, so that the task holding the lock can be completed as soon as possible, thereby releasing the lock as soon as possible, reducing the waiting time for other tasks to lock, and improving the performance of the whole machine.

[0033] An embodiment of the present application provides a thread lock processing method. The method in this embodiment can be applied to servers, terminal devices, platforms, devices, etc. with computing and processing capabilities, wherein the server can be a server cluster or a single server, a server deployed in the cloud, or a local server.

[0034] like Figure 2 The flowchart of the thread lock processing method according to an embodiment of the present application is shown, including:

[0035] Step S201, when the current task is waiting to hold the thread lock, query the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes the current CPU as a proxy task for the current task.

[0036] Among them, the tasks in the lock holding queue are the tasks holding the thread lock. In other words, the proxy task is the task holding the thread lock.

[0037] A property pre-set for each task, the proxy flag. If the proxy flag is 1, it means that the task is in the execution proxy state. Being in the execution proxy state means that the task is currently a proxy task for other tasks. If the proxy flag is 0, it means that the task is in the non-execution proxy state.

[0038] Query the lock-holding queue for tasks that are in non-executing proxy state (i.e., proxy flag is 0) and whose CPU affinity includes the current CPU as proxy tasks. CPU affinity refers to the operating system scheduler's tendency to schedule specific processes or threads to run on specific processor cores. By setting CPU affinity, you can control which CPU cores can execute a specific process or thread. If the CPU affinity of a task includes the current CPU, it means that the task can be run on the current CPU.

[0039] Step S202, if the status of the proxy task is ready and the proxy task is not currently running, the proxy task is added to the temporary queue of the current CPU, the current CPU is used to run the proxy task according to the temporary queue, and the thread lock is released after the proxy task is completed; the entry and exit order of the temporary queue is first in, last out.

[0040] Specifically, by reading the state in the properties of the proxy task, it is determined whether the proxy task is in the ready state. If the value of state is running, it means that the proxy task is in the ready state. Tasks in the ready state may be running or not. If not running, the proxy task is added to the temporary queue of the current CPU. Since the order of entry and exit in the temporary queue is first in, last out, the newly added proxy task is processed first and scheduled to run on the current CPU first. After the operation is completed, the lock is released.

[0041] Step S203, using a thread lock to run the current task on the current CPU.

[0042] After the proxy task releases the lock, there may be other tasks in the lock holding queue holding the thread lock. After the execution is completed, the lock is released, and then the current task holds the lock and is scheduled to run on the current CPU. If there are no other tasks holding the lock, the current task uses the lock released by the proxy task to run on the current CPU. Since the proxy task is added to the temporary queue, the lock can be released faster, so that other tasks can get the lock as soon as possible.

[0043] The thread lock processing method provided in the embodiment of the present application, when the current task is waiting to hold the thread lock, queries the lock queue corresponding to the thread lock for tasks that are currently in a non-executing proxy state and whose CPU affinity includes the current CPU, as the proxy task of the current task; if the status of the proxy task is ready, and the proxy task is not currently running, then the proxy task is added to the temporary queue of the current CPU, and the current CPU is used to run the proxy task according to the temporary queue, and the thread lock is released after the proxy task is completed; the entry and exit order of the temporary queue is first in, last out; the thread lock is used to run the current task on the current CPU. In the example of the present application, the proxy task that is in the ready state but not currently running in the lock queue is added to the temporary queue. Since the entry and exit order of the temporary queue is first in, last out, the proxy task is run preferentially according to the temporary queue, so that the proxy task releases the lock as soon as possible, reducing the waiting time for the lock and improving the performance of the whole machine.

[0044] The following introduces the specific implementation process of the above steps through multiple implementation methods:

[0045] In one implementation, the thread lock processing method further includes: registering a global queue and using the global queue as a lock holding queue before searching the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes the task of the current CPU as a proxy task of the current task.

[0046] In actual applications, for kernel-mode code, the registration function is used to directly register the global queue as a lock-holding queue. Specifically, the data structure of the queue is defined, and the queue is initialized, and then the queue is stored as a global variable to allow the kernel to access the queue. In order to allow other kernel codes to interact with this queue, an application programming interface function is set for basic operations such as queue entry and queue exit, and a lock should be acquired during each operation to ensure thread safety.

[0047] For user-mode code, register a global queue as a lock queue by means of a system call. Specifically, in order to enable user-mode code to register a global queue (such as locked_queue) by means of a system call, it is necessary to implement a new system call in the kernel and write a corresponding application programming interface in user space to call this system call. First, define and implement the system call in kernel mode: First, you need to assign a unique system call number to the system call and write a function to handle the system call logic. The function performs the following operations: verify the validity of the incoming parameters; check permissions to ensure that the caller has sufficient permissions to perform this operation; execute the actual queue registration logic, such as storing the queue information provided by the user mode into a global variable in the kernel and returning the result to the user mode. Then, register the system call and edit the relevant part of the kernel source code to register this system call. Compile and load the kernel module, compile the above code into a kernel module, and load it into the running kernel. Secondly, define the user-mode data structure. Define the user-mode structure corresponding to the kernel-mode data structure to pass the necessary information. Create a function to encapsulate the call to the system call, for example, the syscall function.

[0048] In this embodiment, by registering a global queue as a lock holding queue, a proxy task can be selected from the lock holding tasks in the global scope, so that the lock holding task can be run as quickly as possible and the lock can be released as soon as possible.

[0049] In one implementation, a thread lock processing method further includes: creating a temporary queue for the current CPU, wherein the scheduling priority of the temporary queue is higher than the scheduling priority of the queue of the sub-scheduling class, and before querying the lock holding queue corresponding to the thread lock, the task that is currently in a non-executing proxy state and whose CPU affinity includes the task of the current CPU is used as a proxy task for the current task.

[0050] Specifically, creating a temporary queue for each CPU can be implemented in user mode or kernel mode.

[0051] In user mode, you can use a variety of programming languages ​​(for example, C) and libraries to create a temporary queue. Specifically, it includes: defining the data structure of the queue, introducing a lock mechanism to ensure that operations on the queue are safe in a multi-threaded environment, setting queue operation functions, implementing basic operations such as enqueue and dequeue, and ensuring that these operations are thread-safe.

[0052] The process of creating a temporary queue in kernel mode includes: defining the queue structure, which can use the data structure provided by the kernel. Synchronization mechanism, using the lock mechanism provided by the kernel, such as read-write locks, etc.

[0053] Set the scheduling priority of the temporary queue to be higher than the scheduling priority of the queue of the sub-scheduling class, and set the entry and exit order of the temporary queue to first in, last out, that is, the tasks that join the temporary queue first will be dequeued last, and the tasks that join the temporary queue later will run first.

[0054] In this embodiment, by creating a temporary queue and setting the scheduling priority of the temporary queue to be higher than the scheduling priority of the queue of the sub-scheduling class, the proxy task can be dequeued and scheduled to run on the CPU after being placed in the temporary queue, so as to complete the execution and release the lock as soon as possible.

[0055] In one implementation, a task that is currently in a non-executing proxy state and whose CPU affinity includes the current CPU is queried in the lock holding queue corresponding to the thread lock. Before serving as a proxy task for the current task, the thread lock processing method further includes: adding a waiting lock process calling code at the waiting lock location / waiting for the reference count; the waiting lock process calling code is used to call the waiting lock function; adding a holding lock process calling code at the holding lock location / increasing the reference count; the holding lock process calling code is used to call the holding lock function; adding a releasing lock process calling code at the releasing lock location / decreasing the reference count; the releasing lock process calling code is used to call the releasing lock function.

[0056] Specifically, for kernel-state code, a function call is added at the place where the lock is waiting / waiting for the reference count to call the wait_lock function, a function call is added at the place where the lock is held / the reference count is increased to call the hold_lock function, and a function call is added at the place where the lock is released / the reference count is reduced to call the release_lock function; for user-state code, a system call is added at the place where the lock is waiting / waiting for the reference count to call the sys_wait_lock function, a system call is added at the place where the lock is held / the reference count is increased to call the sys_hold_lock function, and a system call is added at the place where the lock is released / the reference count is reduced to call the sys_release_lock function.

[0057] In this embodiment, the logic of waiting for lock, holding lock, and releasing lock is abstracted, and corresponding process calling codes are added at the lock-related positions, which is flexible and convenient to use.

[0058] In one implementation, the thread lock processing method also includes: if the state of the proxy task is a sleep state, waking up the proxy task; if the proxy task is not running after being woken up, and the proxy task is not in the ready queue of the current CPU, migrating the proxy task to the ready queue of the current CPU, and adding the proxy task to a temporary queue, using the current CPU to run the proxy task according to the temporary queue, and releasing the thread lock after the proxy task is completed.

[0059] In actual applications, if the proxy task is not in the ready state, that is, in the sleeping state, it needs to be awakened. After awakening, it is determined whether the proxy task is running. If not, the proxyflag of the proxy task is set to 1, and it is determined whether the proxy task is in the ready queue of the current CPU. If not, the proxy task is migrated to the ready queue of the current CPU in order to use the current CPU to run. The proxy task is added to the temporary queue and scheduled from the temporary queue to the current CPU to run, so that the proxy task releases the lock as soon as possible.

[0060] In one implementation, before adding the proxy task to the temporary queue, the thread lock processing method further includes: if the current task is in the proxy execution state, adding the current task to the temporary queue.

[0061] In actual applications, by reading the value of the proxy flag of the current task, it can be determined whether the current task is in the execution proxy state. If the proxy flag is 1, it means that the current task is in the execution proxy state. In this case, the current task is added to the temporary queue and is executed first after obtaining the released lock, thereby reducing the waiting time for other tasks to lock.

[0062] In one implementation, before using the thread lock to run the current task on the current CPU, the thread lock processing method further includes: using the current task to hold the thread lock, and adding the current task to a lock holding queue.

[0063] In actual applications, after the proxy task releases the thread lock, the current task tries to hold the lock. If it successfully holds the thread lock, the current task is added to the lock holding queue. The tasks in the lock holding queue are the tasks holding the thread lock. The loop processing begins and the next task again determines the proxy task corresponding to the next task in the lock holding queue.

[0064] In one implementation, after the current task is executed on the current CPU using the thread lock, the thread lock processing method further includes: removing the current task from the lock holding queue and releasing the thread lock.

[0065] In actual applications, after the current task has finished running on the CPU using the thread lock released by the proxy task, it no longer needs to hold the lock, so it is removed from the lock holding queue and the thread lock is released for use by other tasks.

[0066] An embodiment of the present application provides a thread lock processing method. The method in this embodiment can be applied to servers, terminal devices, platforms, devices, etc. with computing and processing capabilities, wherein the server can be a server cluster or a single server, a server deployed in the cloud, or a local server.

[0067] like Figure 3 The flowchart of the thread lock processing method according to an embodiment of the present application is shown, including:

[0068] Step S301, register a global queue and use the global queue as a lock queue.

[0069] Step S302: Create a temporary queue for the current CPU. The scheduling priority of the temporary queue is higher than the scheduling priority of the queue of the sub-scheduling class.

[0070] Step S303, when the current task is waiting to hold the thread lock, query the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes the current CPU as a proxy task for the current task.

[0071] Step S304, if the status of the proxy task is ready and the proxy task is not currently running, the proxy task is added to the temporary queue of the current CPU corresponding to the current task, and the entry and exit order of the temporary queue is first in, last out.

[0072] Step S305: Use the current CPU to run the proxy task according to the temporary queue, and release the thread lock after the proxy task is completed.

[0073] Step S306: Use the current task to hold the thread lock, and add the current task to the lock holding queue.

[0074] Step S307, using the thread lock released by the proxy task to run the current task on the current CPU.

[0075] Step S308, remove the current task from the lock queue and release the thread lock.

[0076] In the present application example, the proxy tasks that are in the ready state but not currently running in the lock holding queue are added to the temporary queue. Since the entry and exit order of the temporary queue is first in, last out, the proxy tasks are run preferentially according to the temporary queue, so that the proxy tasks release the lock as soon as possible, reducing the waiting time for the lock and improving the performance of the entire machine.

[0077] Corresponding to the application scenario and method of the method provided in the embodiment of the present application, the embodiment of the present application also provides a thread lock processing device. Figure 4 The figure is a structural block diagram of a thread lock processing device according to an embodiment of the present application, the device comprising:

[0078] The proxy determination module 401 is used to query the lock queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes the current CPU as a proxy task for the current task when the current task is waiting to hold the thread lock.

[0079] The proxy running module 402 is used to add the proxy task to the temporary queue of the current CPU if the status of the proxy task is ready and the proxy task is not currently running, use the current CPU to run the proxy task according to the temporary queue, and release the thread lock after the proxy task is completed; the entry and exit order of the temporary queue is first in, last out.

[0080] The task running module 403 is used to run the current task on the current CPU using a thread lock.

[0081] The thread lock processing device provided in the embodiment of the present application, when the current task is waiting to hold the thread lock, queries the lock queue corresponding to the thread lock for tasks that are currently in a non-executing proxy state and whose CPU affinity includes the current CPU, as the proxy task of the current task; if the status of the proxy task is ready, and the proxy task is not currently running, then the proxy task is added to the temporary queue of the current CPU, and the current CPU is used to run the proxy task according to the temporary queue, and the thread lock is released after the proxy task is completed; the entry and exit order of the temporary queue is first in, last out; the thread lock is used to run the current task on the current CPU. In the example of the present application, the proxy task that is in the ready state but not currently running in the lock queue is added to the temporary queue. Since the entry and exit order of the temporary queue is first in, last out, the proxy task is run preferentially according to the temporary queue, so that the proxy task releases the lock as soon as possible, reducing the waiting time for the lock and improving the performance of the whole machine.

[0082] In one implementation, the thread lock processing device is also used to: query the lock holding queue corresponding to the thread lock for a task that is currently in a non-execution proxy state and whose CPU affinity includes the current CPU, and before serving as a proxy task for the current task, create a temporary queue for the current CPU, and the scheduling priority of the temporary queue is higher than the scheduling priority of the queue of the sub-scheduling class.

[0083] In one implementation, the thread lock processing device is also used to: query the lock holding queue corresponding to the thread lock for a task that is currently in a non-execution proxy state and whose CPU affinity includes the current CPU, and before acting as a proxy task for the current task, register a global queue and use the global queue as a lock holding queue.

[0084] In one implementation, the proxy determination module 401 is used to query the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes the current CPU as a proxy task for the current task.

[0085] In one implementation, the thread lock processing device is also used to: before determining the proxy task of the current task in the lock holding queue corresponding to the thread lock, add a lock waiting process calling code at the lock waiting location / waiting for the reference count; the lock waiting process calling code is used to call the lock waiting function; add a lock holding process calling code at the lock holding location / increase reference count location; the lock holding process calling code is used to call the lock holding function; add a lock releasing process calling code at the lock releasing location / decrease reference count location; the lock releasing process calling code is used to call the lock releasing function.

[0086] In one implementation, the thread lock processing device is also used to: if the state of the proxy task is a sleep state, wake up the proxy task; if the proxy task is not running after being woken up, and the proxy task is not in the ready queue of the current CPU, migrate the proxy task to the ready queue of the current CPU, and add the proxy task to a temporary queue, use the current CPU to run the proxy task according to the temporary queue, and release the thread lock after the proxy task is completed.

[0087] In one implementation, the thread lock processing device is further used for: before adding the proxy task to the temporary queue, if the current task is in the proxy execution state, adding the current task to the temporary queue.

[0088] In one implementation, the thread lock processing device is further used to: before using the thread lock to run the current task on the current CPU, use the current task to hold the thread lock and add the current task to the lock holding queue.

[0089] In one implementation, the thread lock processing device is further used to: after the current task is executed on the current CPU using the thread lock, remove the current task from the lock holding queue and release the thread lock.

[0090] The functions of each module in the embodiment of the present application can be referred to the corresponding description in the above method, and have corresponding beneficial effects, which will not be repeated here.

[0091] Figure 5 FIG. 1 is a block diagram of an electronic device used to implement an embodiment of the present application. Figure 5 As shown, the electronic device includes: a memory 510 and a processor 520. The memory 510 stores a computer program that can be run on the processor 520. When the processor 520 executes the computer program, the method in the above embodiment is implemented. The number of the memory 510 and the processor 520 can be one or more.

[0092] The electronic device also includes:

[0093] The communication interface 530 is used to communicate with external devices and perform data exchange transmission.

[0094] If the memory 510, the processor 520 and the communication interface 530 are implemented independently, the memory 510, the processor 520 and the communication interface 530 can be connected to each other through a bus and communicate with each other. The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 5 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0095] Optionally, in a specific implementation, if the memory 510, the processor 520 and the communication interface 530 are integrated on a chip, the memory 510, the processor 520 and the communication interface 530 can communicate with each other through an internal interface.

[0096] An embodiment of the present application provides a computer-readable storage medium storing a computer program, which implements the method provided in the embodiment of the present application when the program is executed by a processor.

[0097] An embodiment of the present application provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the method provided in the embodiment of the present application is implemented.

[0098] An embodiment of the present application also provides a chip, which includes a processor for calling and executing instructions stored in the memory from the memory, so that a communication device equipped with the chip executes the method provided by the embodiment of the present application.

[0099] An embodiment of the present application also provides a chip, including: an input interface, an output interface, a processor and a memory, wherein the input interface, the output interface, the processor and the memory are connected via an internal connection path, and the processor is used to execute the code in the memory. When the code is executed, the processor is used to execute the method provided in the embodiment of the application.

[0100] It should be understood that the above processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc. It is worth noting that the processor may be a processor that supports the Advanced RISC Machines (ARM) architecture.

[0101] Further, optionally, the above-mentioned memory may include a read-only memory and a random access memory. The memory may be a volatile memory or a non-volatile memory, or may include both volatile and non-volatile memory. Among them, the non-volatile memory may include a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), or a flash memory. The volatile memory may include a random access memory (RAM), which is used as an external cache. By way of exemplary but not limiting description, many forms of RAM are available. For example, static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM) and direct memory bus random access memory (DR RAM).

[0102] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function according to the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium.

[0103] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present application. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine different embodiments or examples described in this specification and the features of different embodiments or examples, unless they are contradictory.

[0104] 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 technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include at least one of the features. In the description of this application, the meaning of "plurality" is two or more, unless otherwise clearly and specifically defined.

[0105] Any process or method described in the flow chart or otherwise described herein can be understood as a module, fragment or portion of a code representing one or more executable instructions for implementing the steps of a specific logical function or process. And the scope of the preferred embodiment of the present application includes other implementations, in which the functions may not be performed in the order shown or discussed, including in a substantially simultaneous manner or in a reverse order according to the functions involved.

[0106] The logic and / or steps described in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, which can be embodied in any computer-readable medium for use by an instruction execution system, apparatus or device (such as a computer-based system, a system including a processor or other system that can fetch instructions from an instruction execution system, apparatus or device and execute instructions), or used in combination with these instruction execution systems, apparatuses or devices.

[0107] It should be understood that the various parts of the present application can be implemented with hardware, software, firmware or a combination thereof. In the above embodiments, multiple steps or methods can be implemented with software or firmware stored in a memory and executed by a suitable instruction execution system. All or part of the steps of the above embodiment method can be completed by instructing the relevant hardware through a program, which can be stored in a computer-readable storage medium, and when the program is executed, it includes one of the steps of the method embodiment or a combination thereof.

[0108] In addition, each functional unit in each embodiment of the present application can be integrated into a processing module, or each unit can exist physically separately, or two or more units can be integrated into one module. The above-mentioned integrated module can be implemented in the form of hardware or in the form of a software functional module. If the above-mentioned integrated module is implemented in the form of a software functional module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. The storage medium can be a read-only memory, a disk or an optical disk, etc.

[0109] The above is only an exemplary embodiment of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various changes or substitutions within the technical scope recorded in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.

Claims

1. A thread lock processing method, characterized in that: include: In the case where the current task is waiting for a thread lock, the lock holding queue corresponding to the thread lock is queried for tasks that are currently in a non-executing proxy state, and the CPU affinity includes the tasks of the current CPU as the proxy tasks of the current task; the CPU affinity refers to the operating system scheduler arranging a preset process or thread to run on a preset processor core; If the status of the proxy task is ready and the proxy task is not currently running, the proxy task is added to the temporary queue of the current CPU, the current CPU is used to run the proxy task according to the temporary queue, and the thread lock is released after the proxy task is completed; The entry and exit order of the temporary queue is first in, last out; Utilize the thread lock to run the current task on the current CPU; The method further comprises: If the state of the proxy task is a sleeping state, waking up the proxy task; If the proxy task is not run after being awakened, and the proxy task is not in the ready queue of the current CPU, the proxy task is migrated to the ready queue of the current CPU, and the proxy task is added to the temporary queue. The current CPU is used to run the proxy task according to the temporary queue, and the thread lock is released after the proxy task is completed.

2. The method according to claim 1, characterized in that Before searching the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes a task of the current CPU as a proxy task of the current task, the method further includes: A temporary queue of the current CPU is created, wherein the scheduling priority of the temporary queue is higher than the scheduling priority of the queue of the sub-scheduling class.

3. The method according to claim 1, characterized in that Before searching the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes a task of the current CPU as a proxy task of the current task, the method further includes: Register a global queue and use the global queue as the lock holding queue.

4. The method according to any one of claims 1 to 3, characterized in that: Before searching the lock holding queue corresponding to the thread lock for a task that is currently in a non-executing proxy state and whose CPU affinity includes a task of the current CPU as a proxy task of the current task, the method further includes: Add a waiting lock process calling code at the waiting lock location / waiting reference count location; the waiting lock process calling code is used to call the waiting lock function; Add a lock holding process calling code at the lock holding location / reference count increase location; the lock holding process calling code is used to call the lock holding function; A lock release process calling code is added at the lock release location / reference count reduction location; the lock release process calling code is used to call the lock release function.

5. The method according to claim 1, characterized in that Before adding the proxy task to the temporary queue, the method further includes: If the current task is in the execution proxy state, the current task is added to the temporary queue.

6. The method according to any one of claims 1 to 3, characterized in that: Before using the thread lock to run the current task on the current CPU, the method further includes: The thread lock is held by using the current task, and the current task is added to the lock holding queue.

7. The method according to any one of claims 1 to 3, characterized in that: After using the thread lock to run the current task on the current CPU, the method further includes: The current task is removed from the lock holding queue, and the thread lock is released.

8. An electronic device, characterized in that: The electronic device comprises a memory, a processor and a computer program stored in the memory, and the processor implements the method according to any one of claims 1 to 7 when executing the computer program.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

10. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Method for detecting scheduling based on time-triggered forwarding

    CN106209684A

  • Realization method and device of reading-writing lock

    CN107291371A