A method, device, storage medium and server for implementing a large-scale lock system
Through the combination of the lock pool management layer and the logical lock management layer, the reuse of lock resources and reference counting management are realized, which solves the problem of lock resource occupation and performance balance in large servers, and improves server performance and resource utilization.
Patent Information
- Application Number
- CN202111189055.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-12
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2041-10-12
AI Technical Summary
It is difficult to find a balance between performance and resource occupancy in large-scale servers with the existing technology, resulting in difficulty in efficient use of lock resources, resulting in low performance or resource waste.
Through the combination of the lock pool management layer and the logical lock management layer, it realizes the reuse of lock resources and reference counting management, reduces the occupation of physical locks, provides a large-scale lock system, and supports an efficient 'small lock' application model.
It effectively reduces the occupation of lock resources, avoids thread conflicts, improves server performance, and meets high-performance requirements in multi-tasking environments.
Smart Images

Figure CN113961364B
Abstract
Description
Technical Field
[0001] The present invention relates to the field of thread security protection of large-scale resource objects, and in particular to a method, device, storage medium and server for implementing a large-scale lock system. Background Art
[0002] Locks are an important resource provided by multitasking operating systems. Common forms include critical sections, semaphores, mutexes, etc. Their core function is in the field of parallel programming. When two or more threads are faced with the possibility of accessing a resource (the same variable, the same memory block, the same Socket handle, the same IO handle, etc.) at the same time, in order to avoid read-write conflicts, these read-write operations need to be exclusively protected to ensure that when a thread operates on this resource, other threads are in a waiting state.
[0003] In the field of large-scale server development, multi-threaded, high-concurrency parallel access to large-scale variables, memory objects, handle objects and other resources is required. This highly intensive resource access requires efficient and massive lock resource support to ensure the integrity and security of data access, as well as the efficiency of resource access. However, locks are a system resource, and the number of locks allowed to be held by each process is limited. Extensive use of lock resources will consume a large amount of system handle resources and memory resources, seriously affecting the performance of service processes, and even causing the operating system to crash due to insufficient resources. Therefore, in server applications that use lock resources on a large scale, the following methods are generally used to solve this problem:
[0004] 1. Improve the protection scope of the lock, reduce the lock handle usage, and use one lock to protect a complete data structure, such as one lock to protect all the data in an array, queue, or linked list. This takes up very little operating system resources, but will cause lock collisions to be too frequent during large concurrent accesses, and most threads will spend a lot of time grabbing locks, resulting in low access efficiency.
[0005] 2. Use spin locks to provide independent lock protection for data resources. Since spin locks work in user mode and do not occupy the handle resources and memory resources of the operating system, there will be no shortage of system resources. However, large-scale use of spin locks will occupy a large amount of CPU resources, especially for some complex and time-consuming operations. The use of spin locks will seriously affect server performance.
[0006] 3. Use a status table to register the locking and unlocking status of data resources to implement data resource lock protection. This status table is generally implemented using a hash table or a bit-map table. This design method is widely used in row locks in databases. It can effectively reduce lock conflicts without occupying too many operating system resources, and can improve server performance to a certain extent. However, in this design, it is necessary to obtain the lock status of resources by looking up the table, which has a certain impact on performance. At the same time, when the thread seizes the lock resource, it runs in the user state like the spin lock, and has the same disadvantages as the spin lock, and it is difficult to balance between CPU occupancy and CPU utilization.
[0007] The above three solutions are not perfect. Either the performance is low or the resources are seriously wasted. It is difficult to strike a balance between the two and it is difficult to meet the lock protection needs for massive data in the large server field that takes into account both performance and occupies a small amount of resources. Summary of the invention
[0008] The purpose of the present invention is to address the problems in the above-mentioned prior art and provide a large-scale lock system implementation method, device, storage medium and server, which can use very few operating system resources (physical lock resources, memory, etc.) to provide large-scale lock resources for use by the application layer, so as to realize an efficient "small lock" application model and meet the high-performance development requirements in a multi-tasking environment.
[0009] In order to achieve the above object, the present invention has the following technical solutions:
[0010] In a first aspect, an embodiment of the present invention provides a method for implementing a large-scale lock system, comprising the following steps:
[0011] The application calls the application call interface of this software module to initialize the lock pool management object. The application call interface receives the access request and transfers to the lock pool management layer to implement the service.
[0012] The application calls the application call interface of this software module to create a logical lock object for the object that needs to be locked. The application call interface receives the access request and goes to the logical lock management layer to implement the service;
[0013] The application calls the application call interface of this software module and prepares to hold the lock. After receiving the access request, the application call interface goes to the logical lock operation layer to implement the service. If the critical section lock is requested, the lock() operation is executed. If the read lock is a read-write lock, the rdlock() operation is executed. If the write lock is requested, the wrlock() operation is executed.
[0014] The application calls the application call interface of this software module to prepare to release the held lock. After receiving the access request, the application call interface goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the unlock() operation is executed; if it is a read lock of a read-write lock, the rdunlock() operation is executed; if it is a write lock, the wrunlock() operation is executed.
[0015] As a preferred solution of the large-scale lock system implementation method of the present invention, the operation steps are completed under the protection of the same system physical lock to ensure that the operation steps are not interrupted during execution.
[0016] As a preferred solution of the large-scale lock system implementation method of the present invention, the lock() operation execution condition is that multiple threads access the same resource simultaneously.
[0017] As a preferred solution of the large-scale lock system implementation method of the present invention, the lock() operation execution steps include: after the locking requirement is first proposed, calling the lock pool interface to create a global lock pool, which stores multiple idle locks waiting for use; after the lock pool receives the locking task, it determines whether this resource is the first time to initiate a lock request. If it is the first time to initiate a lock request, the lock pool will actively allocate an idle lock to this resource, and increase the reference count value of this lock by one, and then perform a locking operation; if it is not the first time to initiate a lock request, the lock pool will not allocate a new idle lock for this request, and only increase the reference count value by one based on the original lock of this resource to indicate that this object is being used by multiple parties.
[0018] As a preferred solution of the large-scale lock system implementation method of the present invention, the unlock() operation execution condition is that the current thread has completed using the resource.
[0019] As a preferred solution of the large-scale lock system implementation method of the present invention, the unlock() operation execution steps include: subtracting 1 from the reference count value of the lock occupied by this resource, and using its value and 1 for judgment; if it is greater than 1, other threads still have the need to lock this resource, and no processing is done here; if it is equal to 1, there is no subsequent need to preempt this resource. When there is no subsequent need to preempt this resource, return this resource lock to the lock pool and wait for the next locking demand to arrive.
[0020] In a second aspect, an embodiment of the present invention further provides a large-scale lock system implementation device, including:
[0021] Initialization module, used for application program to call the application program calling interface of this software module to initialize the lock pool management object. After the application program calling interface receives the access request, it will go to the lock pool management layer to implement the service;
[0022] The logic lock creation module is used for the application to call the application call interface of this software module to create a logic lock object for the object that needs to be locked. The application call interface receives the access request and transfers to the logic lock management layer to implement the service;
[0023] The locking module is used by the application to call the application call interface of this software module and prepare to hold the lock. The application call interface receives the access request and goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the lock() operation is executed; if it is a read lock of a read-write lock, the rdlock() operation is executed; if it is a write lock, the wrlock() operation is executed;
[0024] The unlocking module is used by the application to call the application call interface of this software module to prepare to release the held lock. The application call interface receives the access request and goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the unlock() operation is executed; if it is a read lock of a read-write lock, the rdunlock() operation is executed; if it is a write lock, the wrunlock() operation is executed.
[0025] In a third aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the large-scale locking system implementation method is implemented.
[0026] In a fourth aspect, an embodiment of the present invention further provides a server, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the large-scale lock system implementation method when executing the computer program.
[0027] Compared with the prior art, the present invention has at least the following beneficial effects: the number of threads of the server process is limited, with a few threads at a time and only a few hundred threads at a time. Therefore, in the case where there is no thread conflict in the server, the server thread will only use a few hundred lock objects at most. Even if lock resources are used on a large scale in the server process, only a few lock resources are accessed at the same time, and a large number of other lock resources are idle, which is actually a huge waste of system resources. If the server process can be recycled and reused in a timely manner like system memory resources, it can achieve the purpose of using very few lock resources and realizing large-scale lock applications. Since more than one million objects require thread safety, it can avoid that too many locks cause excessive memory and lock resource pressure on the system. If critical section locks are used on a large scale, due to the continuous occurrence of multi-threaded competition, Event handles are also constantly created. If the process exceeds the number of handles of 16M, the process may not be able to run. When using the large-scale lock system designed by the present invention, only a few critical section locks can be used to complete the security function of multi-threading. BRIEF DESCRIPTION OF THE DRAWINGS
[0028] Figure 1 A schematic diagram of the structure of a large-scale lock system constructed by an embodiment of the present invention;
[0029] Figure 2 A schematic diagram of the locking process of a large-scale locking system implementation method according to an embodiment of the present invention;
[0030] Figure 3 A schematic diagram of the unlocking process of the large-scale lock system implementation method according to an embodiment of the present invention. DETAILED DESCRIPTION
[0031] The present invention will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present invention, rather than to limit the present invention. In addition, it should be noted that, for ease of description, only parts related to the present invention, rather than all structures, are shown in the accompanying drawings.
[0032] As an implementation of a large-scale lock system, the most important thing is the scheduling and utilization of lock resources, that is, the process of locking and unlocking.
[0033] See also Figure 1 The large-scale lock system structure constructed by the present invention directly calls the API interface through the application program without regard to the internal logic. The logical lock management layer stores a number of logical locks, and the locks in the lock pool are taken through the logical locks. The lock pool management layer interacts with the logical lock management layer to realize the operation of taking the locks in the lock pool.
[0034] Application program call interface: used to define the operation method of this module, which can be called by other computer programming languages to realize function output;
[0035] Lock pool management layer: used to provide pool-based lock resource recovery and allocation algorithm logic to achieve lock resource reuse;
[0036] Logical lock management layer: used to define a logical lock object, which is not a real lock, but a management object of a logical lock, which is used to store an object pointer and reference counter of a real lock. The logical lock management layer provides create() and destroy() operation methods for creating and releasing logical lock objects;
[0037] Logical lock operation layer: used to provide lock algorithm logic and provide corresponding lock type operation characteristics for each implemented lock. For example, critical section locks provide lock(), unlock(), trylock() operations, while read-write locks provide rdlock(), rdunlock(), wrlock(), wrunlock() and other operations.
[0038] The large-scale lock system implementation method proposed by the present invention comprises the following steps:
[0039] The application calls the application call interface of this software module to initialize the lock pool management object. The application call interface receives the access request and transfers to the lock pool management layer to implement the service.
[0040] The application calls the application call interface of this software module to create a logical lock object for the object that needs to be locked. The application call interface receives the access request and goes to the logical lock management layer to implement the service;
[0041] The application calls the application call interface of this software module and prepares to hold the lock. After receiving the access request, the application call interface goes to the logical lock operation layer to implement the service. If the critical section lock is requested, the lock() operation is executed. If the read lock is a read-write lock, the rdlock() operation is executed. If the write lock is requested, the wrlock() operation is executed.
[0042] The application calls the application call interface of this software module to prepare to release the held lock. After receiving the access request, the application call interface goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the unlock() operation is executed; if it is a read lock of a read-write lock, the rdunlock() operation is executed; if it is a write lock, the wrunlock() operation is executed.
[0043] All the above actions are completed under the protection of the same system physical lock to ensure that all actions are not interrupted and ensure the multi-threaded security of this software module itself.
[0044] See also Figure 2 , the lock() operation execution condition is that multiple threads access the same resource at the same time, in order to ensure the integrity, reliability and security of the data. When multiple people operate the same data in the database at the same time, if the same data is not locked, there is a situation where multiple people compete for it, so the final integrity and reliability of the data cannot be guaranteed, and it also poses a hidden danger to the safety of the subsequent data use. The lock() operation execution steps include: after the first lock request is raised, the lock pool interface is called to create a global lock pool, which stores multiple idle locks waiting for use; after the lock pool receives the lock task, it determines whether this resource is the first time to initiate a lock request. If it is the first time to initiate a lock request, the lock pool will actively allocate an idle lock to this resource, and increase the reference count value of this lock by one, and then perform a lock operation; if it is not the first time to initiate a lock request, the lock pool will not allocate a new idle lock for this request, and only increase the reference count value by one based on the original lock of this resource to indicate that this object is being used by multiple parties.
[0045] See also Figure 3The unlock() operation execution condition is that the current thread has finished using the resource, in order to ensure that other threads can normally access and process the requests raised by this resource. The unlock() operation execution steps include: subtract 1 from the reference count value of the lock occupied by this resource, and use its value and 1 for judgment; if it is greater than 1, other threads still have the need to lock this resource, and no processing is done here; if it is equal to 1, there is no subsequent need to seize this resource. When there is no subsequent need to seize this resource, return this resource lock to the lock pool and wait for the next lock demand to arrive.
[0046] Another embodiment of the present invention further provides a large-scale lock system implementation device, comprising:
[0047] Initialization module, used for application program to call the application program calling interface of this software module to initialize the lock pool management object. After the application program calling interface receives the access request, it will go to the lock pool management layer to implement the service;
[0048] The logic lock creation module is used for the application to call the application call interface of this software module to create a logic lock object for the object that needs to be locked. The application call interface receives the access request and transfers to the logic lock management layer to implement the service;
[0049] The locking module is used by the application to call the application call interface of this software module and prepare to hold the lock. The application call interface receives the access request and goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the lock() operation is executed; if it is a read lock of a read-write lock, the rdlock() operation is executed; if it is a write lock, the wrlock() operation is executed;
[0050] The unlocking module is used by the application to call the application call interface of this software module to prepare to release the held lock. The application call interface receives the access request and goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the unlock() operation is executed; if it is a read lock of a read-write lock, the rdunlock() operation is executed; if it is a write lock, the wrunlock() operation is executed.
[0051] Another embodiment of the present invention further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the large-scale locking system implementation method is implemented.
[0052] Another embodiment of the present invention further provides a server, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the large-scale lock system implementation method when executing the computer program.
[0053] Exemplarily, the computer program may be divided into one or more modules / units, which are stored in a computer-readable storage medium and executed by the processor to complete the steps in the large-scale lock system implementation method described in the present application. The one or more modules / units may be a series of computer-readable instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program in the server.
[0054] The server may be a computing device such as a smart phone, a notebook, a PDA, or a cloud server. The server may include, but is not limited to, a processor and a memory. Those skilled in the art will appreciate that the server may also include more or fewer components, or a combination of certain components, or different components, for example, the server may also include input and output devices, network access devices, buses, etc.
[0055] The 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.
[0056] The memory may be an internal storage unit of the server, such as a hard disk or memory of the server. The memory may also be an external storage device of the server, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the server. Furthermore, the memory may include both an internal storage unit of the server and an external storage device. The memory is used to store the computer-readable instructions and other programs and data required by the server. The memory may also be used to temporarily store data that has been output or is to be output.
[0057] It should be noted that the information interaction, execution process, etc. between the above-mentioned devices / units are based on the same concept as the method embodiment. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.
[0058] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.
[0059] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may at least include: any entity or device that can carry the computer program code to the camera device / terminal device, recording medium, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electrical carrier signal, telecommunication signal and software distribution medium. For example, a USB flash drive, a mobile hard disk, a disk or an optical disk.
[0060] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
Claims
1. A method for implementing a large-scale lock system, It is characterized in that The following steps are involved: The application calls the application call interface of this software module to initialize the lock pool management object. The application call interface receives the access request and transfers to the lock pool management layer to implement the service. The application calls the application call interface of this software module to create a logical lock object for the object that needs to be locked. The application call interface receives the access request and goes to the logical lock management layer to implement the service; The application calls the application call interface of this software module and prepares to hold the lock. After receiving the access request, the application call interface goes to the logical lock operation layer to implement the service. If the critical section lock is requested, the lock() operation is executed. If the read lock is a read-write lock, the rdlock() operation is executed. If the write lock is requested, the wrlock() operation is executed. The application calls the application call interface of this software module to prepare to release the held lock. The application call interface receives the access request and goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the unlock() operation is executed. If it is a read lock of a read-write lock, the rdunlock() operation is executed. If it is a write lock, the wrunlock() operation is executed. The lock() operation execution steps include: after the first lock request is made, calling the lock pool interface to create a global lock pool, which stores multiple idle locks waiting for use; after the lock pool receives the lock task, it determines whether this resource is the first time to initiate a lock request. If it is the first time to initiate a lock request, the lock pool will actively allocate an idle lock to this resource, and increase the reference count value of this lock by one, and then perform a lock operation; if it is not the first time to initiate a lock request, the lock pool will not allocate a new idle lock for this request, and only increase the reference count value by one based on the original lock of this resource to indicate that this object is being used by multiple parties.
2. The method for implementing a large-scale lock system according to claim 1, Features: Complete the operation steps under the protection of the same system physical lock to ensure that the operation steps are not interrupted during execution.
3. The method for implementing a large-scale lock system according to claim 1, Features: The lock() operation execution condition is that multiple threads access the same resource simultaneously.
4. The method for implementing a large-scale lock system according to claim 1, Features: The unlock() operation execution condition is that the current thread has finished using the resource.
5. The method for implementing a large-scale lock system according to claim 1, It is characterized in that The unlock() operation execution steps include: subtracting 1 from the reference count value of the lock occupied by this resource, and using its value and 1 for judgment; if it is greater than 1, other threads still have the need to lock this resource, and no processing is done here; if it is equal to 1, there is no subsequent need to seize this resource. When there is no subsequent need to seize this resource, return this resource lock to the lock pool and wait for the next locking demand to arrive.
6. A large-scale locking system implementation device, It is characterized in that include: Initialization module, used for application program to call the application program calling interface of this software module to initialize the lock pool management object. After the application program calling interface receives the access request, it will go to the lock pool management layer to implement the service; The logic lock creation module is used for the application to call the application call interface of this software module to create a logic lock object for the object that needs to be locked. The application call interface receives the access request and transfers to the logic lock management layer to implement the service; The locking module is used by the application to call the application call interface of this software module and prepare to hold the lock. The application call interface receives the access request and goes to the logical lock operation layer to implement the service. If the request is for a critical section lock, the lock() operation is executed; if it is a read lock of a read-write lock, the rdlock() operation is executed; if it is a write lock, the wrlock() operation is executed; Unlocking module, used by application program to call the application program calling interface of this software module, ready to release the held lock. After receiving the access request, the application program calling interface goes to the logic lock operation layer to implement the service. If the requested lock is a critical section lock, the unlock() operation is executed; if the requested lock is a read-write lock, the rdunlock() operation is executed; if the requested lock is a read-write lock, the wrunlock() operation is executed; The lock() operation execution steps include: after the first lock request is made, calling the lock pool interface to create a global lock pool, which stores multiple idle locks waiting for use; after the lock pool receives the lock task, it determines whether this resource is the first time to initiate a lock request. If it is the first time to initiate a lock request, the lock pool will actively allocate an idle lock to this resource, and increase the reference count value of this lock by one, and then perform a lock operation; if it is not the first time to initiate a lock request, the lock pool will not allocate a new idle lock for this request, and only increase the reference count value by one based on the original lock of this resource to indicate that this object is being used by multiple parties.
7. A computer-readable storage medium storing a computer program, It is characterized in that When the computer program is executed by a processor, the large-scale locking system implementation method according to any one of claims 1 to 5 is implemented.
8. A server comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the computer program, the large-scale locking system implementation method according to any one of claims 1 to 5 is implemented.
Citation Information
Patent Citations
Method for system concurrency control through business logic lock
CN102156928A
Key-Value type write-once read-many lock pool software module and running method thereof
CN102681892A
Thread synchronization method and server
CN108304251A
Multi-thread access method and device
CN108874446A
Method and apparatus for providing dynamic locks for global resources
US20040088573A1
