Method and device for realizing read-write lock
Technical Field
The invention relates to the technical field of communication, in particular to a method and a device for realizing a read-write lock.
Background
The read-write lock is a spin lock, and divides an access user of a shared resource into a read user and a write user, wherein the read user only performs read operation on the shared resource, and the write user performs write operation on the shared resource. One read-write lock can only have one write user or a plurality of read users at the same time, but can not have both read users and write users at the same time. In a multiprocessor system, multiple read users are allowed to access a shared resource at the same time, and write operations are blocked while read operations are locked. When in write-lock, all operations attempting to read from or write to the resource will be blocked.
Currently, the read operation or the write operation is implemented by a thread, and in an operating system with a thread introduced, the thread can be used as a basic unit for independent operation and independent scheduling. Multiple threads can be executed concurrently, so that when the same data is modified, other threads can also modify the data; or, because data cannot be read due to being modified, the system bus is usually locked during a process of performing a read operation or a write operation on the data by a thread, and the system bus is unlocked after the read operation or the write operation is performed.
Therefore, a method for implementing a read/write lock is needed to solve the problem that congestion is easily generated among a plurality of read operations due to the fact that each read operation needs to be locked or unlocked on a system bus in the prior art.
Disclosure of Invention
The embodiment of the invention provides a method and a device for realizing a read-write lock, which aim to solve the technical problem that congestion is easily generated among a plurality of read operations because each read operation needs to lock or unlock a system bus in the prior art.
The embodiment of the invention provides a method for realizing a read-write lock, which comprises the following steps:
receiving a first request message sent by a user;
if the first request message is used for requesting to read data, acquiring the state of a preset read-write lock, and after determining that the state of the preset read-write lock is a first state, reading the data according to the first request message; the first state indicates that there is no thread writing the data.
Optionally, if the first request message is used to request a write operation on the data, the method further includes:
setting the state of the preset read-write lock to be a second state; the second state represents that a thread for writing the data currently exists;
and setting a system bus in a write lock state, and writing the data according to the first request message.
Optionally, after the write operation is completed on the data, the method further includes:
and setting the state of the preset read-write lock to be a first state.
Optionally, setting the state of the preset read-write lock to a first state includes:
if the second request message does not exist, setting the state of the preset read-write lock to be a first state; or if the second request message is determined to exist and the second request message is used for requesting to read data, waking up the second request message and setting the state of the preset read-write lock to be a first state;
wherein the second request message is a request message received in the process of executing the first request message.
Optionally, the method further comprises:
if the preset read-write lock state is determined to be the second state and the system bus is not determined to be in the write-add lock state, setting the system bus to be in the read-add lock state, and performing read operation on the data according to the first request message; the second state indicates that there is a thread writing data.
The embodiment of the invention provides a device for realizing a read-write lock, which comprises:
the receiving unit is used for receiving a first request message sent by a user;
the processing unit is used for acquiring the state of a preset read-write lock if the first request message is used for requesting to read data, and reading the data according to the first request message after determining that the state of the preset read-write lock is a first state; the first state indicates that there is no thread writing the data.
Optionally, the processing unit is further configured to:
if the first request message is used for requesting to write the data, setting the state of the preset read-write lock to be a second state; the second state represents that a thread for writing the data currently exists; and setting a system bus in a write lock state, and performing write operation on the data according to the first request message.
Optionally, after completing the write operation on the data, the processing unit is further configured to:
and setting the state of the preset read-write lock to be a first state.
Optionally, the processing unit is specifically configured to:
if the second request message does not exist, setting the state of the preset read-write lock to be a first state; or if the second request message is determined to exist and the second request message is used for requesting to read data, waking up the second request message and setting the state of the preset read-write lock to be a first state;
wherein the second request message is a request message received in the process of executing the first request message.
Optionally, the processing unit is further configured to:
if the preset read-write lock state is determined to be the second state and the system bus is not determined to be in the write-add lock state, setting the system bus to be in the read-add lock state, and performing read operation on the data according to the first request message; the second state indicates that there is a thread writing data.
In the embodiment of the invention, a first request message sent by a user is received, if the first request message is used for requesting to read data, the state of the preset read-write lock can be obtained, and after the state of the preset read-write lock is determined to be the first state, the data is read according to the first request message. The first state indicates that no thread is currently available for writing data, so that the embodiment of the invention can directly perform read operation on the data according to the first request message after determining that no thread is currently available for writing data according to the state of the read-write lock, and compared with the conventional read-write lock implementation mode in which read operation can be performed only after a read lock is required to be performed on a system bus, the embodiment of the invention does not need to perform the read lock on the system bus, thereby shortening the processing time of the read operation; furthermore, in a high-concurrency multiprocessor system, especially a system with frequent read operations and relatively few write operations, the method provided by the embodiment of the invention can greatly reduce the overhead of thread read lock adding, thereby avoiding the situation that each read operation needs to lock or unlock a system bus to easily cause congestion among a plurality of read operations, reducing the collision probability of lock adding and unlock among a plurality of read operations, and further improving the execution efficiency of the system.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings needed to be used in the description of the embodiments will be briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without inventive exercise.
FIG. 1 is a schematic flow chart of a method for implementing a read/write lock provided in the prior art;
fig. 2 is a schematic flowchart of a method for implementing a read/write lock according to an embodiment of the present invention;
FIG. 3 is a flowchart illustrating another implementation method of a read/write lock according to an embodiment of the present invention;
FIG. 4 is a schematic diagram of an overall flow involved in an embodiment of the present invention;
fig. 5 is a schematic structural diagram of an implementation apparatus of a read-write lock according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention clearer, the present invention will be described in further detail with reference to the accompanying drawings, and it is apparent that the described embodiments are only a part of the embodiments of the present invention, 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 an operating system with threads, when a plurality of threads are executed concurrently, the same data is often required to be accessed. To ensure the integrity of this data, lock mechanisms are currently used. For example, before a thread reads a piece of data, a read lock is first applied to the data, and after the read operation is completed, the read lock is then released. Similarly, before a thread performs a write operation on a datum, the data is locked by writing, and after the execution of the read operation is finished, the lock by writing is unlocked. Further, the thread locks or unlocks the data, that is, locks or unlocks the system bus.
For convenience of understanding, fig. 1 exemplarily shows a flowchart of a method for implementing a read-write lock provided in the prior art, as shown in fig. 1, taking a thread as an example, before the thread performs a write operation on data, the thread first performs a write lock on a system bus, so that no other thread can use the bus, and at this time, a state of the system bus is obtained and analyzed to determine whether there is a thread performing a read operation or a write operation on the data. When the data is allowed to be read, the system bus needs to be locked (new read identification is calculated) first. It can be seen that, in the implementation process of the prior art, since each read operation needs to add a read lock to the system bus, in a high-concurrency multiprocessor system, especially in a system with very frequent read operations and relatively few write operations, such as a routing table and an Address Resolution Protocol (ARP) table, congestion between multiple read operations is easily caused, and thus the execution efficiency of the multiprocessor system is reduced.
In view of this, an embodiment of the present invention provides a method for implementing a read/write lock, which can directly perform a read operation on data according to a first request message after determining that no thread currently exists to perform the write operation on the data according to a state of the read/write lock, and compared with an existing read/write lock implementation mode in which a read lock needs to be performed on a system bus, the read operation in the embodiment of the present invention does not need to perform the read lock on the system bus, so that processing time of the read operation can be shortened; furthermore, in a high-concurrency multiprocessor system, especially a system with frequent read operations and relatively few write operations, the method provided by the embodiment of the invention can greatly reduce the overhead of thread read lock adding, thereby avoiding the situation that each read operation needs to lock or unlock a system bus to easily cause congestion among a plurality of read operations, reducing the collision probability of lock adding and unlock among a plurality of read operations, and further improving the execution efficiency of the system.
The technical scheme provided by the embodiment of the invention is described in the following with the accompanying drawings of the specification.
The first embodiment is as follows:
fig. 2 exemplarily shows a flowchart corresponding to the implementation method of the read-write lock according to the embodiment of the present invention, and as shown in fig. 2, the method specifically includes the following steps:
step 201, a first request message sent by a user is received.
Step 202, if the first request message is used to request a read operation on data, acquiring a state of a preset read-write lock, and after determining that the state of the preset read-write lock is a first state, performing a read operation on the data according to the first request message.
Specifically, in step 202, the preset read/write lock may be set in front of the system bus, so as to execute the corresponding operation according to the state of the preset read/write lock. In the embodiment of the present invention, the preset states of the read-write lock include a first state and a second state, where the first state indicates that there is no thread performing write operation on the data, and the second state indicates that there is a thread performing write operation on the data.
In the embodiment of the present invention, the preset state of the read/write lock may be represented in various forms, for example, in a character string form; alternatively, it can be in text form; alternatively, the numerical value may be used. Taking numerical form as an example, the status of the preset read/write lock may be expressed by setting FLAG and assigning a value to the FLAG. For example, when FLAG is set to 0, the state of the read-write lock is preset to be a first state, that is, no thread currently exists to perform write operation on the data; and when FLAG is set to 1, the state of the preset read-write lock is set to be a second state, that is, the current thread performs write operation on the data.
In the embodiment of the present invention, the executed steps are different according to different obtained preset read-write lock states, and the following description will specifically describe different preset read-write lock states.
(1) Presetting the state of the read-write lock as a first state
If the preset state of the read-write lock is the first state, it indicates that there is no thread performing write operation on the data, specifically, the two cases are included:
in case one, a thread for writing the data does not exist at present, and a thread for reading the data does not exist either;
in case two, there is currently no thread that writes to the data, but there is a thread that reads to the data.
In the second case, although there is a thread that performs a read operation on data, the read-write lock, as a spin lock, may allow multiple read users to exist, and therefore, both cases may perform a read operation on the data according to the first request message.
(2) Presetting the state of the read-write lock as a second state
And if the preset state of the read-write lock is a second state, indicating that a thread for writing the data exists currently. According to the nature of the read-write lock, one read-write lock can only have one write user or a plurality of read users at the same time, but cannot have both the read user and the write user at the same time, so that if the state of the read-write lock is preset to be the second state, the state of the system bus can be further judged.
In the embodiment of the invention, the states of the system bus comprise a write lock state and a non-write lock state. The following is a detailed description of the two different system bus states described above.
The first state: write lock status
And if the state of the system bus is the write lock state, indicating that the current thread still has write operation on the data, and the first request message needs to wait for the completion of the write operation.
And a second state: non-write lock state
And if the state of the system bus is a non-write-lock state, indicating that no thread for writing the data exists currently. That is, the write operation is completed in the time period from the determination that the state of the preset read-write lock is the second state to the determination that the state of the system bus is the non-write-locked state.
Further, the system bus may be set in a read lock state, and the data may be read according to the first request message.
In the embodiment of the present invention, a read operation does not need to lock a system bus, and in a high-concurrency multiprocessor system, especially a system with frequent read operations and relatively few write operations, the method described in the above step 201 and step 202 can greatly reduce the overhead of thread read lock, and improve the concurrency capability among multiple threads, so that a situation that each read operation needs to lock or unlock the system bus, which is likely to cause congestion among multiple read operations, can be avoided, the collision probability of lock and unlock among multiple read operations is reduced, and further, the execution efficiency of the system is improved.
Example two:
fig. 3 exemplarily shows a flowchart corresponding to another implementation method of a read-write lock according to an embodiment of the present invention, and as shown in fig. 3, the method specifically includes the following steps:
step 301, receiving a first request message sent by a user.
Step 302, if the first request message is used to request a write operation on data, setting the state of the preset read-write lock to a second state.
Step 303, setting the system bus in a write lock state, and performing write operation on the data according to the first request message.
In steps 301 and 302, after determining that the received first request message is for requesting to write data, the state of the preset read-write lock may be set to the second state, so as to avoid collision between other messages sent by the user and behind the first request message and the first request message; furthermore, the preset read-write lock state can be reset only when the first request message is used for requesting to write data, and because the write operation times are far less than the read operation times in a scene applicable to the embodiment of the invention, the method can greatly reduce the frequency of resetting the preset read-write lock state, and further improve the system operation efficiency.
In step 303, before the system bus is set to be in the write lock state, the subsequent operations may be executed after waiting for a preset time period. The preset time period can be determined according to the execution time of the read operation and can be determined according to the average execution time of a plurality of read operations; alternatively, the frequency of the read operation and the execution time of the read operation may be determined. The preset time period can be determined by a person skilled in the art according to experience and practical situations, and is not limited specifically. Therefore, the situation that the conflict between the write operation and the read operation requested by the first request message is caused by the fact that the thread performs the read operation on the data before the state of the preset read-write lock is set can be avoided.
Further, after the data is written, the state of the preset read-write lock may be set to the first state, so that the corresponding process can be conveniently executed by a subsequent request message requesting the read operation.
Specifically, after the write operation is completed on the data, before the state of the preset read-write lock is set to the first state, whether a second request message exists may be determined, where the second request message is a request message received in the process of executing the first request message. According to the judgment result, the following three cases can be classified: (1) there is no second request message; (2) the second request message exists and is used for requesting to read the data; (3) there is a second request message, and the second request message is for requesting a write operation to the data.
In the above-described case, if the first case is the case, it may be directly preset to set the state of the write lock to the first state; if the situation is the second situation, the second request message can be awakened, and the state of the preset read-write lock is set to be the first state; if the third condition is satisfied, after a preset time period, resetting the system bus to be in the write lock state, and performing a write operation on the data according to the second request message.
In the embodiment of the present invention, only when the operation requested by the request message is a write operation, the state of the read/write station is reset, and the write lock is applied to the system bus, so that compared with the case that the system bus needs to be locked regardless of the read operation or the write operation in the prior art, the method described in the above step 301 to step 303 is adopted, which can greatly reduce the overhead of thread read lock, and especially can improve the system efficiency of high concurrency and high throughput in a large server.
In order to more clearly introduce the processes described in the first and second embodiments, the processes involved in the embodiments of the present invention are collectively described below with reference to fig. 4. As shown in fig. 4, the following steps may be included:
step 401, a first request message sent by a user is received.
Step 402, judging whether the operation requested by the first request message is a read operation or a write operation, and if the operation is the read operation, executing step 403; if the operation is a write operation, step 414 is performed.
In step 403, the state of the preset read-write lock is obtained.
Step 404, determining whether the preset read-write lock state is a first state or a second state, and if the preset read-write lock state is the first state, executing step 405; if the status is the second status, go to step 406.
Step 405, performing a read operation on the data according to the first request message.
Step 406, determining whether the state of the system bus is a write lock state, and if so, executing step 407; if the status is not write-locked, step 408 is performed.
Step 407, wait until the first request message is woken up.
And step 408, adding a read lock to the system bus to enable the system bus to be in a read lock state, and performing a read operation on the data according to the first request message.
Step 409, the system bus is unlocked.
Step 410, judging whether a thread still exists for reading the data, if so, returning to the step 410; if not, go to step 411.
Step 411, determining whether there is a request message requesting to write data, if yes, executing step 412; if not, the flow ends.
Step 413, waking up the request message for requesting to write data.
Step 414, the state of the preset read-write lock is set to the second state.
Step 415, after a preset time period, setting the system bus to be in a write lock state, and performing a write operation on data according to the first request message.
At step 416, the lock is unlocked to the system bus.
Step 417, determining whether a second request message exists, where the second request message is a request message received in the process of executing the first request message, and if so, executing step 418; if not, go to step 420.
Step 418, determining whether the second request message is used for requesting to read data or to request to write data, if so, executing step 419; if the operation is a write operation, the process returns to step 415.
Step 419 waking up the second request message.
Step 420, the state of the preset read-write lock is set to the first state.
In the above steps 403 to 413, when the state of the preset read/write lock is the first state, it is not necessary to set the system bus in the read/write lock state, so that the time for setting the read/write lock can be reduced; when the preset read-write lock state is the second state, namely a thread for writing data exists, the bus state is set to be in the write-add lock state, and the overhead of adding the read lock by the thread can be greatly reduced.
In the above-mentioned steps 414 to 420, when the first request message requests a write operation, the state of the preset read/write lock needs to be set to the second state, and in order to ensure that the previous read operation is completed successfully, before setting the system bus in the write lock state, the write operation needs to be performed after waiting for a preset time period. After the write operation is completed, the state of the preset read-write lock needs to be set to the first state, so that the corresponding steps can be conveniently executed by the subsequent read-request operation.
That is to say, in the embodiment of the present invention, under the condition that the write operation is not performed, it is not necessary to set the system bus to be in the reading lock state; in the case of a write operation, the state of the set read/write lock needs to be set to the second state.
It should be noted that the step number is only an exemplary representation of an execution flow, and the sequence of each step is not specifically limited in the present application, for example, in the step 419 and the step 420, the state of the preset read/write lock may be set to be the first state, and then the second request message may be awakened.
Example three:
for the above method flow, the present application further provides a device for implementing a read/write lock, and the specific implementation of the device may refer to the above method flow, and fig. 5 exemplarily shows a schematic structural diagram of the device for implementing a read/write lock according to an embodiment of the present invention, and as shown in fig. 5, the device includes a receiving unit 501 and a processing unit 502; wherein the content of the first and second substances,
a receiving unit 501, configured to receive a first request message sent by a user;
a processing unit 502, configured to obtain a state of a preset read/write lock if the first request message is used to request a read operation on data, and perform a read operation on the data according to the first request message after determining that the state of the preset read/write lock is a first state; the first state indicates that there is no thread writing the data.
Optionally, the processing unit 502 is further configured to:
if the first request message is used for requesting to write the data, setting the state of the preset read-write lock to be a second state; the second state represents that a thread for writing the data currently exists; and setting a system bus in a write lock state, and performing write operation on the data according to the first request message.
Optionally, after completing the write operation on the data, the processing unit 502 is further configured to:
and setting the state of the preset read-write lock to be a first state.
Optionally, the processing unit 502 is specifically configured to:
if the second request message does not exist, setting the state of the preset read-write lock to be a first state; or if the second request message is determined to exist and the second request message is used for requesting to read data, waking up the second request message and setting the state of the preset read-write lock to be a first state;
wherein the second request message is a request message received in the process of executing the first request message.
Optionally, the processing unit 502 is further configured to:
if the preset read-write lock state is determined to be the second state and the system bus is not determined to be in the write-add lock state, setting the system bus to be in the read-add lock state, and reading the data according to the first request message; the second state indicates that there is a thread writing data.
In the embodiment of the invention, a first request message sent by a user is received, if the first request message is used for requesting to read data, the state of the preset read-write lock can be obtained, and after the state of the preset read-write lock is determined to be the first state, the data is read according to the first request message. The first state indicates that no thread is currently available for writing data, so that the embodiment of the invention can directly perform read operation on the data according to the first request message after determining that no thread is currently available for writing data according to the state of the read-write lock, and compared with the conventional read-write lock implementation mode in which read operation can be performed only after a read lock is required to be performed on a system bus, the embodiment of the invention does not need to perform the read lock on the system bus, thereby shortening the processing time of the read operation; furthermore, in a high-concurrency multiprocessor system, especially a system with frequent read operations and relatively few write operations, the method provided by the embodiment of the invention can greatly reduce the overhead of thread read lock adding, thereby avoiding the situation that each read operation needs to lock or unlock a system bus to easily cause congestion among a plurality of read operations, reducing the collision probability of lock adding and unlock among a plurality of read operations, and further improving the execution efficiency of the system; furthermore, by using the method provided by the embodiment of the present invention, compared with the prior art, the performance of the read operation can be improved by about 50 times, and if the method is applied to the routing table and the ARP table, the performance of the read operation in the embodiment of the present invention can be improved by 2 to 3 times compared with the performance of three-layer forwarding in the prior art.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
While preferred embodiments of the present invention have been described, additional variations and modifications in those embodiments may occur to those skilled in the art once they learn of the basic inventive concepts. Therefore, it is intended that the appended claims be interpreted as including preferred embodiments and all such alterations and modifications as fall within the scope of the invention.
It will be apparent to those skilled in the art that various changes and modifications may be made in the present invention without departing from the spirit and scope of the invention. Thus, if such modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include such modifications and variations.