CN111143080A - Read-write lock optimization method, device and system and storage medium - Google Patents

Read-write lock optimization method, device and system and storage medium Download PDF

Info

Publication number
CN111143080A
CN111143080A CN201911408539.XA CN201911408539A CN111143080A CN 111143080 A CN111143080 A CN 111143080A CN 201911408539 A CN201911408539 A CN 201911408539A CN 111143080 A CN111143080 A CN 111143080A
Authority
CN
China
Prior art keywords
queue
locking
requests
read
scheduled
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201911408539.XA
Other languages
Chinese (zh)
Inventor
娄婷
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Lenovo Beijing Ltd
Original Assignee
Lenovo Beijing Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Lenovo Beijing Ltd filed Critical Lenovo Beijing Ltd
Priority to CN201911408539.XA priority Critical patent/CN111143080A/en
Publication of CN111143080A publication Critical patent/CN111143080A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/524Deadlock detection or avoidance

Abstract

The invention discloses a read-write lock optimization method, a device, a system and a storage medium. Firstly, receiving all locking requests by a queue to be scheduled, namely, the locking requests comprise read locking requests and write locking requests, and orderly inserting the locking requests into the queue to be scheduled according to a principle that the read locking is prior to the write locking requests, so that the read locking can be preferentially processed, and the characteristics and advantages of the original read-write locking method are ensured; and then, all locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence under the condition of meeting the specified condition, and the locking requests in the queue to be executed are processed in sequence according to the read-write locking method, so that all locking requests which are positioned in the queue to be scheduled before can be processed, and the situation that the write-lock requests are blocked for a long time and cannot be processed can be avoided.

Description

Read-write lock optimization method, device and system and storage medium
Technical Field
The invention relates to the field of read-write locks, in particular to a read-write lock optimization method, a device, a system and a storage medium.
Background
When a plurality of threads share the same resource, the threads read the shared resource at the same time, but once one thread writes the shared data and other threads read and write the shared resource, the data security problem occurs, so that the read-write lock is generated. The read-write lock allows multiple threads to acquire the read lock at the same time, but after one thread acquires the write lock, other threads enter the wait queue to wait. The read-write lock is particularly suitable for the scenes that only a few cases of reading are writing under most cases, and is a key solution technology for maintaining the consistency of data.
Currently, a read-write lock in a standard library is an opportunistic lock. Once a thread acquires a read lock, the read lock applications of other threads arriving before the thread releases the read lock automatically acquire the read lock to form a read lock queue, only when the thread operations in the read lock queue are all completed, the read lock is released, and the thread needing write operations has an opportunity to acquire the write lock.
In this case, there are the following problems: if the thread applies for the read lock continuously, the thread B may be in a waiting state for a long time and even become a deadlock.
Disclosure of Invention
In view of the above problems, the present inventors creatively provide a method, an apparatus, a system and a storage medium for optimizing a read-write lock.
According to a first aspect of the embodiments of the present invention, there is provided a method for optimizing a read-write lock, the method including: receiving a locking request, wherein the locking request comprises a reading locking request and a writing locking request; the locking requests are orderly inserted into a queue to be scheduled according to the principle that the read lock is prior, so that the read lock requests are all arranged before the write lock requests; under the condition of meeting specified conditions, all locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence; and processing the locking requests in the queue to be executed in sequence according to the read-write locking method.
According to an embodiment of the present invention, before receiving the locking request, the method further comprises: establishing a queue to be scheduled and initializing; establishing a queue to be executed and initializing; and establishing a read-write lock data structure and initializing the read-write lock data structure, wherein the data structure comprises a pointer pointing to a queue to be scheduled, a pointer pointing to a queue to be executed, the number of requests of read locks in the queue to be scheduled, the number of requests of write locks in the queue to be scheduled, the number of requests of read locks in the queue to be executed, the number of requests of write locks in the queue to be executed and a pointer pointing to a currently executed locking request.
According to an embodiment of the present invention, the orderly inserting locking requests into the queue to be scheduled according to the read lock priority principle includes: setting a priority for the locking request according to the type of the locking request, wherein if the locking request is a request for locking, the locking request is set with a priority with a negative numerical value, if the locking request is a request for locking, the locking request is set with a priority with a positive numerical value, and the absolute value of the priority is a thread identification number of the locking request; and orderly inserting the locking requests into the queue to be dispatched according to a sorting method with the priority from small to large.
According to an embodiment of the present invention, moving all locking requests in a queue to be scheduled to a queue to be executed according to an original sequence when a specified condition is satisfied includes: and after the interval of the specified time slices, all locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence.
According to an embodiment of the present invention, when a specific condition is satisfied, moving all locking requests in a queue to be scheduled to a queue to be executed according to an original sequence includes: and when the locking requests of the queue to be executed become zero, all the locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence.
According to a second aspect of the embodiments of the present invention, there is provided a read/write lock optimization apparatus, including: the device comprises a receiving module, a locking module and a locking module, wherein the receiving module is used for receiving locking requests, and the locking requests comprise reading locking requests and writing locking requests; the inserting module is used for orderly inserting the locking requests into a queue to be scheduled according to the read lock priority principle so that the read lock requests are arranged before the write lock requests; the moving module is used for moving all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence under the condition of meeting the specified condition; and the read-write lock processing module is used for sequentially processing the locking requests in the to-be-executed queue according to the read-write lock method.
According to an embodiment of the present invention, the apparatus further comprises: the initialization module is used for establishing a queue to be scheduled and initializing the queue; establishing a queue to be executed and initializing; and establishing a read-write lock data structure and initializing the read-write lock data structure, wherein the data structure comprises a pointer pointing to a queue to be scheduled, a pointer pointing to a queue to be executed, the number of requests of read locks in the queue to be scheduled, the number of requests of write locks in the queue to be scheduled, the number of requests of read locks in the queue to be executed, the number of requests of write locks in the queue to be executed and a pointer pointing to a currently executed locking request.
According to an embodiment of the present invention, wherein the insertion module includes: the priority setting unit is used for setting a priority for the locking request according to the type of the locking request, wherein if the locking request is a request for locking, a priority with a negative numerical value is set for the locking request, if the locking request is a request for locking, a priority with a positive numerical value is set for the locking request, and the absolute value of the priority is a thread identification number of the locking request; and the inserting unit is used for orderly inserting the locking requests into the queue to be scheduled according to a sorting method with the priority from small to large.
According to an embodiment of the present invention, the moving module is specifically configured to move all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence after a specified time slice is set.
According to an embodiment of the present invention, the moving module is specifically configured to move all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence after a specified time slice is set.
According to a third aspect of the embodiments of the present invention, there is provided a read-write lock optimization system, including a processor and a memory, where the memory stores computer program instructions, and the computer program instructions are used by the processor to execute any one of the read-write lock optimization methods.
According to a fourth aspect of the embodiments of the present invention, there is provided a computer storage medium, which includes a set of computer executable instructions, and when the instructions are executed, the computer storage medium is configured to perform any one of the read-write lock optimization methods described above.
The embodiment of the invention provides a read-write lock optimization method, a device, a system and a storage medium. The read-write lock optimization method provided by the embodiment of the invention optimizes the read-write lock method, creatively introduces concepts of a queue to be scheduled and a queue to be executed, and firstly, the queue to be scheduled receives all locking requests, namely the locking requests comprise read locking requests and write locking requests, and the locking requests are orderly inserted into the queue to be scheduled according to the principle that the read locking is prior to the write locking requests, so that the read locking can be preferentially processed, and the characteristics and advantages of the original read-write lock method are ensured; and then, all locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence under the condition of meeting the specified condition, and the locking requests in the queue to be executed are processed in sequence according to the read-write locking method, so that all locking requests which are positioned in the queue to be scheduled before can be processed, and the situation that the write-lock requests are blocked for a long time and cannot be processed can be avoided. Therefore, the characteristics and the advantages of the original read-write lock method can be ensured, and the problem that the write lock request can be blocked for a long time and cannot be processed is solved.
Drawings
The above and other objects, features and advantages of exemplary embodiments of the present invention will become readily apparent from the following detailed description read in conjunction with the accompanying drawings. Several embodiments of the invention are illustrated by way of example, and not by way of limitation, in the figures of the accompanying drawings and in which:
in the drawings, the same or corresponding reference numerals indicate the same or corresponding parts.
FIG. 1 is a schematic diagram of an implementation flow of a read-write lock optimization method according to an embodiment of the present invention;
fig. 2 is a schematic structural diagram of a read-write lock optimization device according to an embodiment of the present invention.
Detailed Description
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In the description herein, references to the description of the term "one embodiment," "some embodiments," "an example," "a specific example," or "some examples," etc., mean that a particular feature, structure, material, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the invention. Furthermore, the particular features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. Furthermore, various embodiments or examples and features of different embodiments or examples described in this specification can be combined and combined by one skilled in the art without contradiction.
Furthermore, the terms "first", "second" and "first" are used for descriptive purposes only and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one such feature. In the description of the present invention, "a plurality" means two or more unless specifically defined otherwise.
Fig. 1 shows an implementation flow of the read-write lock optimization method according to the embodiment of the present invention. Referring to fig. 1, the method includes: an operation 110 of receiving a locking request, the locking request including a read lock request and a write lock request; operation 120, inserting locking requests into the queue to be scheduled in order according to the read lock priority principle, so that the read lock requests are all arranged before the write lock requests; operation 130, moving all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence when the specified condition is met; at operation 140, the locking requests in the queue to be executed are processed in sequence according to the read-write locking method.
In operation 110, a unified request for locking requests is made without distinguishing between read lock requests and write lock requests. Where the locking requests are all requests for the same shared resource.
In operation 120, all lock requests received are sorted in the pending queue and all read lock requests are guaranteed to be queued before the write lock. Therefore, the characteristics and the advantages of the read-write lock can be ensured, on one hand, all read-lock requests can be preferentially processed, on the other hand, all read-lock requests arranged in front of the write lock can be continuously processed only by applying for once read-lock, the advantages of the read-write lock are fully utilized, the locking and unlocking operations are reduced, and the processing efficiency of the locking requests is greatly improved.
In operation 130, to ensure that all locking requests in the queue to be scheduled, whether read lock requests or write lock requests, can be processed under the specified conditions, a queue to be executed is created to exclusively load the locking requests to be executed and process the requests in sequence. Once a condition is satisfied, all locking requests in the pending schedule are placed in the pending queue for pending execution. After all locking requests to be scheduled are placed in the queue to be executed, the queue to be scheduled is emptied, and then the subsequent locking requests received in operation 110 may be inserted continuously according to the method described in operation 120 to form the queue to be scheduled in the next stage. Since all locking requests in the queue to be executed are processed, a situation that a certain write lock request cannot be processed for a long time because of continuous read lock requests does not occur.
In operation 140, the read-write lock method is a common read-write lock method, that is, if the read-write lock is not locked by any locking request, the read-write lock can be immediately obtained by the write-lock request, otherwise, the write-lock request must wait until the read-write lock is released; if the read-write lock is acquired by the read-lock request, the subsequent read-lock request can add one to the number of readers to immediately acquire the read-write lock without being blocked; if the read-write lock is acquired by a write lock request, then whether the subsequent locking request is a read lock request or a write lock request, it will be blocked, and the write task that initiated the write locking request must wait for the write operation to complete to release the read lock. The read lock requests and write lock requests processed here are not the unordered, random original requests received in operation 110, but rather all locking requests for a period of time are intercepted, ordered via operations 120 and 130, including all read lock requests in the first, consecutive, period of time of the queue, and all write lock requests in the period of time after all read lock requests in the last column of the queue. Therefore, the specific operation performed in operation 140 is simpler, that is, the read lock request queued at the front of the queue to be executed obtains the read/write lock after the first read lock request obtains the read/write lock, and the read operation can be performed simultaneously, and the number of readers is reduced by one after the read lock request obtains the read/write lock. And the write requests arranged at the rear column of the queue to be executed need to wait for all the read tasks initiating the read lock requests to complete the read operation, namely the number of readers is changed to 0, and the read-write lock can be obtained only when the read-write lock is released. Therefore, the characteristics and the advantages of the original read-write lock method can be ensured, and the problem that the write lock request can be blocked for a long time and cannot be processed is solved.
According to an embodiment of the present invention, before receiving the locking request, the method further comprises: establishing a queue to be scheduled and initializing; establishing a queue to be executed and initializing; and establishing a read-write lock data structure and initializing the read-write lock data structure, wherein the data structure comprises a pointer pointing to a queue to be scheduled, a pointer pointing to a queue to be executed, the number of requests of read locks in the queue to be scheduled, the number of requests of write locks in the queue to be scheduled, the number of requests of read locks in the queue to be executed, the number of requests of write locks in the queue to be executed and a pointer pointing to a currently executed locking request.
In order to implement the above read/write lock optimization method, an embodiment of the present invention further creatively provides a data structure for recording the current state of the read/write lock, for performing locking and unlocking management on the read/write lock, and for operating a queue to be scheduled and a queue to be executed. The data structure comprises a pointer pointing to a queue to be scheduled, a pointer pointing to a queue to be executed, the number of requests of read locks in the queue to be scheduled, the number of requests of write locks in the queue to be scheduled, the number of requests of read locks in the queue to be executed, the number of requests of write locks in the queue to be executed, and a pointer pointing to a currently executed locking request.
According to an embodiment of the present invention, the orderly inserting locking requests into the queue to be scheduled according to the read lock priority principle includes: setting a priority for the locking request according to the type of the locking request, wherein if the locking request is a request for locking, the locking request is set with a priority with a negative numerical value, if the locking request is a request for locking, the locking request is set with a priority with a positive numerical value, and the absolute value of the priority is a thread identification number of the locking request; and orderly inserting the locking requests into the queue to be dispatched according to a sorting method with the priority from small to large.
In this embodiment, the priority values of all read lock requests are negative numbers, the priority values of all write lock requests are positive numbers, and after the locking requests are orderly inserted into the queue to be scheduled according to a sorting method of which the priority levels are from small to large, all read lock requests are naturally arranged in front of all write lock requests.
According to an embodiment of the present invention, moving all locking requests in a queue to be scheduled to a queue to be executed according to an original sequence when a specified condition is satisfied includes: and after the interval of the specified time slices, all locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence.
In this embodiment, a time slice scheduling manner is adopted, that is, the received locking request can be moved from the queue to be scheduled to the queue to be executed every fixed time interval. This embodiment has the advantage that the pending queue is not too long and the write requests in the pending queue do not wait too long, ensuring that every fixed period during which write requests originating can be moved to the pending queue and then processed without being blocked by a write lock request inserted in front of the queue in the pending queue for all the time.
According to an embodiment of the present invention, when a specific condition is satisfied, moving all locking requests in a queue to be scheduled to a queue to be executed according to an original sequence includes: and when the locking requests of the queue to be executed become zero, all the locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence.
In this embodiment, a strategy is adopted in which a batch of locking requests is processed and a next batch of locking requests is processed immediately, so that the locking requests in the queue of the to-be-scheduled requests can be processed immediately after the locking requests in the queue to be executed are processed, without waiting for the arrival of the next time slice, and thus the read-write lock is used more fully to continuously access the shared resource.
How the read-write lock optimization method works is described in detail below with reference to a specific application example of the read-write lock optimization method according to the embodiment of the present invention.
Assuming that the application instance manages locking, unlocking, and usage of the read-write lock using a time-sliced schedule and using the following data structure, assuming that the structure has just been initialized, and is ready to receive a locking request, the initial values of the fields are shown in table 1:
exec_reader req_reader exec_writer req_writer curr exec_queu req_queue
0 0 0 0 null [] []
TABLE 1
Wherein exec _ reader is the number of requests for reading locks in the queue to be executed; the req _ reader is the number of requests for reading locks in the queue to be scheduled; exec _ writer is the number of requests for write locks in the queue to be executed; req _ writer is the number of requests for write locks in the queue to be scheduled; curr is a pointer pointing to the currently executed locking request; exec _ queue is a pointer pointing to the queue to be executed; and the req _ queue is a pointer pointing to the queue to be scheduled.
Assuming that in the first time slice, the read lock requests initiated by the thread 1, the thread 3 and the thread 4 and the write lock requests initiated by the thread 2 are received, according to the read-write lock optimization method provided by the embodiment of the invention, the priority of each locking request is assigned according to the thread identification number, the mode that the read lock request takes a negative value and the mode that the write lock request takes a positive value, and the locking requests are inserted into the queue to be scheduled according to the sorting method of the priorities from small to large. At this time, the values of the above data structure are shown in table 2:
Figure BDA0002349340570000091
TABLE 2
And at the end of the first time slice, assuming that the time slice is 5 seconds, namely when the 5 th second of the first time slice is reached, moving locking requests in the queue to be scheduled, namely the read lock requests initiated by the threads 1, 3 and 4 and the write lock requests initiated by the threads 2 into the queue to be executed according to the original sequence. And then, performing read-write lock operation on the queue to be executed, wherein the read-write lock is successfully performed on the read-write lock by the thread 4 because the current lock is not locked. At the same time, thread 3 and thread 1 will also acquire a read lock to perform the corresponding read operation, while thread 2 is blocked because of the write lock to be added. At this time, the values of the data structure are changed to the values shown in table 3:
Figure BDA0002349340570000092
TABLE 3
Assume that in the second time slice, read lock requests initiated by thread 6, thread 7, and thread 8 and write lock requests initiated by thread 5 are received again. During this period, the read tasks of thread 4, thread 3, and thread 1 have been completed, and the exec _ reader is decremented by one, the values of the data structure are changed to the values shown in table 4:
Figure BDA0002349340570000101
TABLE 4
Then at the end of the second time slice, that is, at the 10 th second, the locking requests in the queue to be scheduled, that is, the read lock requests initiated by the threads 6, 7 and 8 and the write lock requests initiated by the thread 5, are all moved to the queue to be executed according to the original sequence. At this time, since the read-write lock is released, the thread 2 is awakened to successfully add the write lock and starts to execute the write operation, and other read threads are all blocked until the thread 2 completes the write operation. At this time, the values of the above data structure are changed to the values shown in table 5:
Figure BDA0002349340570000102
TABLE 5
After the write task of the thread 2 is completed, the read-write lock is released, the thread 8 is awakened, then the read-write lock is added to successfully obtain the read lock, the thread 8 becomes the current thread, then the thread 7 and the thread 6 can obtain the read lock, and the thread 8, the thread 7 and the thread 6 can simultaneously perform read operation. Assuming that no other locking requests are received during this period, the values of the data structure are changed to the values shown in table 6:
Figure BDA0002349340570000103
TABLE 6
From the above description, it can be seen that although the read lock requests initiated by the thread 6, the thread 7, and the thread 8 come from the second time slice, the write lock request initiated by the thread 2 in the first time slice can still be processed in time before the read lock requests initiated by the thread 6, the thread 7, and the thread 8 in the second time slice after the read lock request in the first time slice is processed. This is so that thread 2 is not always blocked.
It should be noted that the above specific application example of the read-write lock optimization method according to the embodiment of the present invention is only an example for implementing the read-write lock optimization method according to the embodiment of the present invention, and is only referred to by the implementing party. The implementation of the read-write lock optimization method in the embodiment of the present invention is not limited by the specific application example, and may be implemented in any suitable form and method.
Further, the embodiment of the invention also provides a read-write lock optimization device. As shown in fig. 2, the apparatus 20 includes: a receiving module 201, configured to receive a locking request, where the locking request includes a read lock request and a write lock request; an inserting module 202, configured to insert locking requests into a queue to be scheduled in an ordered manner according to a read lock priority principle, so that the read lock requests are all queued before the write lock requests; the moving module 203 is configured to move all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence when the specified condition is met; and the read-write lock processing module 204 is configured to sequentially process the locking requests in the queue to be executed according to the read-write lock method.
According to an embodiment of the present invention, the apparatus 20 further comprises: the initialization module is used for establishing a queue to be scheduled and initializing the queue; establishing a queue to be executed and initializing; and establishing a read-write lock data structure and initializing the read-write lock data structure, wherein the data structure comprises a pointer pointing to a queue to be scheduled, a pointer pointing to a queue to be executed, the number of requests of read locks in the queue to be scheduled, the number of requests of write locks in the queue to be scheduled, the number of requests of read locks in the queue to be executed, the number of requests of write locks in the queue to be executed and a pointer pointing to a currently executed locking request.
According to an embodiment of the present invention, the insertion module 202 includes: the priority setting unit is used for setting a priority for the locking request according to the type of the locking request, wherein if the locking request is a request for locking, a priority with a negative numerical value is set for the locking request, if the locking request is a request for locking, a priority with a positive numerical value is set for the locking request, and the absolute value of the priority is a thread identification number of the locking request; and the inserting unit is used for orderly inserting the locking requests into the queue to be scheduled according to a sorting method with the priority from small to large.
According to an embodiment of the present invention, the moving module 203 is specifically configured to move all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence after a specified time slice is set.
According to an embodiment of the present invention, the moving module 203 is specifically configured to move all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence after a specified time slice is set.
According to a third aspect of the embodiments of the present invention, there is provided a read-write lock optimization system, including a processor and a memory, where the memory stores computer program instructions, and the computer program instructions are used by the processor to execute any one of the read-write lock optimization methods.
According to a fourth aspect of embodiments of the present invention there is provided a computer storage medium comprising a set of computer executable instructions for performing any of the above methods when the instructions are executed.
Here, it should be noted that: the above description on the embodiment of the read/write lock optimization device, the above description on the embodiment of the read/write lock optimization system, and the above description on the embodiment of the computer storage medium are similar to the descriptions on the foregoing method embodiments, and have similar beneficial effects to the foregoing method embodiments, and therefore, no further description is given. For technical details that are not disclosed yet in the description of the embodiment of the read-write lock optimization device, the description of the embodiment of the read-write lock optimization system, and the description of the embodiment of the computer storage medium of the present invention, please refer to the description of the foregoing method embodiments of the present invention for understanding, and therefore, for brevity, details are not repeated.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
In the several embodiments provided in the present application, it should be understood that the disclosed apparatus and method may be implemented in other ways. The above-described device embodiments are merely illustrative, for example, the division of a unit is only one logical function division, and there may be other division ways in actual implementation, such as: multiple units or components may be combined, or may be integrated into another device, or some features may be omitted, or not implemented. In addition, the coupling, direct coupling or communication connection between the components shown or discussed may be through some interfaces, and the indirect coupling or communication connection between the devices or units may be electrical, mechanical or other forms.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units; can be located in one place or distributed on a plurality of network units; some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, all the functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may be separately regarded as one unit, or two or more units may be integrated into one unit; the integrated unit can be realized in a form of hardware, or in a form of hardware plus a software functional unit.
Those of ordinary skill in the art will understand that: all or part of the steps for realizing the method embodiments can be completed by hardware related to program instructions, the program can be stored in a computer readable storage medium, and the program executes the steps comprising the method embodiments when executed; and the aforementioned storage medium includes: various media capable of storing program codes, such as a removable storage medium, a Read Only Memory (ROM), a magnetic disk, and an optical disk.
Alternatively, the integrated unit of the present invention may be stored in a computer-readable storage medium if it is implemented in the form of a software functional module and sold or used as a separate product. Based on such understanding, the technical solutions of the embodiments of the present invention may be essentially implemented or a part contributing to the prior art may be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for enabling a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the methods of the embodiments of the present invention. And the aforementioned storage medium includes: a removable storage medium, a ROM, a magnetic disk, an optical disk, or the like, which can store the program code.
The above description is only for the specific embodiments of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art can easily conceive of the changes or substitutions within the technical scope of the present invention, and the changes or substitutions should be covered within the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims (10)

1. A read-write lock optimization method, characterized in that the method comprises:
receiving locking requests, wherein the locking requests comprise reading locking requests and writing locking requests;
the locking requests are orderly inserted into a queue to be scheduled according to the principle that the read lock is prior, so that the read lock requests are all arranged before the write lock requests;
under the condition that a specified condition is met, all locking requests in the queue to be scheduled are moved to a queue to be executed according to the original sequence;
and processing the locking requests in the queue to be executed in sequence according to a read-write locking method.
2. The method of claim 1, wherein prior to said receiving a locking request, said method further comprises:
establishing a queue to be scheduled and initializing;
establishing a queue to be executed and initializing;
and creating a read-write lock data structure and initializing the read-write lock data structure, wherein the data structure comprises a pointer pointing to the queue to be scheduled, a pointer pointing to the queue to be executed, the number of requests of read locks in the queue to be scheduled, the number of requests of write locks in the queue to be scheduled, the number of requests of read locks in the queue to be executed, the number of requests of write locks in the queue to be executed and a pointer pointing to a currently executed locking request.
3. The method of claim 1, wherein the orderly inserting the locking requests into the queue to be scheduled according to a read lock priority principle comprises:
setting a priority for the locking request according to the type of the locking request, wherein if the locking request is a request for locking, a priority with a negative numerical value is set for the locking request, if the locking request is a request for locking, a priority with a positive numerical value is set for the locking request, and the absolute value of the priority is the thread identification number of the locking request;
and orderly inserting the locking requests into the queue to be dispatched according to a sorting method with the priority from small to large.
4. The method according to claim 1, wherein the moving all locking requests in the queue to be scheduled to the queue to be executed in the original order when the specified condition is met comprises:
and after a time slice appointed by an interval, all locking requests in the queue to be scheduled are moved to a queue to be executed according to the original sequence.
5. The method according to claim 1, wherein the moving all locking requests in the queue to be scheduled to the queue to be executed in the original order when the specified condition is satisfied comprises:
and when the locking requests of the queue to be executed become zero, all the locking requests in the queue to be scheduled are moved to the queue to be executed according to the original sequence.
6. A read-write lock optimization device, the device comprising:
the device comprises a receiving module, a locking module and a locking module, wherein the receiving module is used for receiving locking requests, and the locking requests comprise reading locking requests and writing locking requests;
the inserting module is used for orderly inserting the locking requests into a queue to be scheduled according to a read lock priority principle so that the read lock requests are arranged before the write lock requests;
the moving module is used for moving all locking requests in the queue to be scheduled to the queue to be executed according to the original sequence under the condition that the specified condition is met;
and the read-write lock processing module is used for sequentially processing the locking requests in the queue to be executed according to a read-write lock method.
7. The apparatus of claim 6, further comprising:
the initialization module is used for establishing a queue to be scheduled and initializing the queue; establishing a queue to be executed and initializing; and creating a read-write lock data structure and initializing the read-write lock data structure, wherein the data structure comprises a pointer pointing to the queue to be scheduled, a pointer pointing to the queue to be executed, the number of requests of read locks in the queue to be scheduled, the number of requests of write locks in the queue to be scheduled, the number of requests of read locks in the queue to be executed, the number of requests of write locks in the queue to be executed and a pointer pointing to a currently executed locking request.
8. The apparatus of claim 6, wherein the insertion module comprises:
a priority setting unit, configured to set a priority for the locking request according to the type of the locking request, where if the locking request is a request for locking, a priority whose value is negative is set for the locking request, and if the locking request is a request for locking, a priority whose value is positive is set for the locking request, and an absolute value of the priority is a thread identification number of the locking request;
and the inserting unit is used for orderly inserting the locking requests into the queue to be scheduled according to a sorting method with the priority from small to large.
9. A read-write lock optimization system, comprising a processor and a memory, wherein the memory stores computer program instructions for executing the read-write lock optimization method according to any one of claims 1 to 5 when the computer program instructions are executed by the processor.
10. A storage medium on which program instructions are stored, wherein the program instructions are operable to perform the read-write lock optimization method according to any one of claims 1 to 5.
CN201911408539.XA 2019-12-31 2019-12-31 Read-write lock optimization method, device and system and storage medium Pending CN111143080A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201911408539.XA CN111143080A (en) 2019-12-31 2019-12-31 Read-write lock optimization method, device and system and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201911408539.XA CN111143080A (en) 2019-12-31 2019-12-31 Read-write lock optimization method, device and system and storage medium

Publications (1)

Publication Number Publication Date
CN111143080A true CN111143080A (en) 2020-05-12

Family

ID=70522415

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201911408539.XA Pending CN111143080A (en) 2019-12-31 2019-12-31 Read-write lock optimization method, device and system and storage medium

Country Status (1)

Country Link
CN (1) CN111143080A (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111880940A (en) * 2020-07-29 2020-11-03 北京浪潮数据技术有限公司 Data processing method, device, equipment and storage medium in storage system
CN111984379A (en) * 2020-07-14 2020-11-24 上海金仕达软件科技有限公司 Read-write transaction control method, system, terminal device and storage medium
CN113010325A (en) * 2021-03-16 2021-06-22 北京百度网讯科技有限公司 Method and device for realizing read-write lock and electronic equipment

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040255086A1 (en) * 2003-06-11 2004-12-16 Hewlett-Packard Development Company, L.P. Reader/writer locking protocol
US20060230411A1 (en) * 2005-04-12 2006-10-12 Microsoft Corporation Resource accessing with locking
CN1920780A (en) * 2006-08-15 2007-02-28 杭州华为三康技术有限公司 Method and system for accessing critical region
US20100275209A1 (en) * 2009-04-28 2010-10-28 Microsoft Corporation Reader/writer lock with reduced cache contention
CN102999378A (en) * 2012-12-03 2013-03-27 中国科学院软件研究所 Read-write lock implement method
CN103500119A (en) * 2013-09-06 2014-01-08 西安交通大学 Task allocation method based on pre-dispatch

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20040255086A1 (en) * 2003-06-11 2004-12-16 Hewlett-Packard Development Company, L.P. Reader/writer locking protocol
US20060230411A1 (en) * 2005-04-12 2006-10-12 Microsoft Corporation Resource accessing with locking
CN1920780A (en) * 2006-08-15 2007-02-28 杭州华为三康技术有限公司 Method and system for accessing critical region
US20100275209A1 (en) * 2009-04-28 2010-10-28 Microsoft Corporation Reader/writer lock with reduced cache contention
CN102999378A (en) * 2012-12-03 2013-03-27 中国科学院软件研究所 Read-write lock implement method
CN103500119A (en) * 2013-09-06 2014-01-08 西安交通大学 Task allocation method based on pre-dispatch

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111984379A (en) * 2020-07-14 2020-11-24 上海金仕达软件科技有限公司 Read-write transaction control method, system, terminal device and storage medium
CN111984379B (en) * 2020-07-14 2023-04-07 上海金仕达软件科技股份有限公司 Read-write transaction control method, system, terminal device and storage medium
CN111880940A (en) * 2020-07-29 2020-11-03 北京浪潮数据技术有限公司 Data processing method, device, equipment and storage medium in storage system
CN113010325A (en) * 2021-03-16 2021-06-22 北京百度网讯科技有限公司 Method and device for realizing read-write lock and electronic equipment
CN113010325B (en) * 2021-03-16 2023-07-25 北京百度网讯科技有限公司 Method and device for realizing read-write lock and electronic equipment

Similar Documents

Publication Publication Date Title
CN111143080A (en) Read-write lock optimization method, device and system and storage medium
US6678802B2 (en) Method and apparatus for controlling access by a plurality of concurrently operating processes to a resource
US8504540B2 (en) Scalable reader-writer lock
US6934950B1 (en) Thread dispatcher for multi-threaded communication library
US9213586B2 (en) Computer-implemented systems for resource level locking without resource level locks
US8495641B2 (en) Efficiently boosting priority of read-copy update readers while resolving races with exiting and unlocking processes
Attiya et al. Transactional scheduling for read-dominated workloads
EP3230860B1 (en) Technologies for efficient synchronization barriers with work stealing support
US20020083063A1 (en) Software and data processing system with priority queue dispatching
US8505015B2 (en) Placing a group work item into every prioritized work queue of multiple parallel processing units based on preferred placement of the work queues
US9069566B1 (en) Implementation of a multiple writer single reader queue in a lock free and a contention free manner
US11204813B2 (en) System and method for multidimensional search with a resource pool in a computing environment
US20210255889A1 (en) Hardware Transactional Memory-Assisted Flat Combining
US20080201712A1 (en) Method and System for Concurrent Message Processing
CN106681836B (en) Semaphore creation method and semaphore creation device
EP1163581B1 (en) Monitor conversion in a multi-threaded computer system
CN111506436A (en) Method for realizing memory sharing, electronic equipment and shared memory data management library
US10313477B2 (en) System and method for use of a non-blocking process with a resource pool in a computing environment
US10846148B2 (en) Request of an MCS lock by guests
CN111679895A (en) Execution method, device and equipment of distributed timing task and readable storage medium
Tamir et al. A heap-based concurrent priority queue with mutable priorities for faster parallel algorithms
US7945912B1 (en) Hierarchical queue-based locks
CN114756287A (en) Data processing method and device for reorder buffer and storage medium
Herbert 3-D landmark recognition from range images
US6636883B1 (en) Mechanism for passing information between queuing and de-queuing processes

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination