Read-write lock implementation method, device, electronic device and storage medium
By introducing counters and mutex locks into the read-write lock data structure, using atomic logic processing and condition variables to distinguish the read-write lock operation path, the problem of large performance consumption in the existing technology is solved, and the read concurrency capability is improved and System performance.
Patent Information
- Application Number
- CN202411377351.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-30
- Publication Date
- 2024-12-27
- Estimated Expiration
- 2044-09-30
AI Technical Summary
The existing read and write lock implementation methods in multiprocessor systems consume large performance of the mutex lock, resulting in insufficient concurrency capability of read lock operation, which is difficult to meet the needs of high-performance scenarios.
By pre-defined data structures of read and write locks, including counters and mutexes, atomic logic processing and conditional variables are used to complete the read lock operation, distinguishing between fast paths and slow paths, reducing the performance consumption of read locks and unlocking and unlocking. Improve reading concurrency ability.
It effectively reduces the performance consumption of read lock operation, improves the read concurrency capability, and improves the overall performance of the system.
Smart Images

Figure CN118897741B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of communication technology, and in particular to a read-write lock implementation method, device, electronic equipment and storage medium. Background Art
[0002] A read-write lock is a special lock that can divide users who access shared resources into readers and writers. Readers only read shared resources, while writers write shared resources. A read-write lock can only have one writer or multiple readers at the same time, but not both readers and writers at the same time. In a multi-processor system, multiple readers are allowed to access shared resources at the same time. When the read operation is locked, the write operation is blocked. When the write lock is in place, all attempts to read and write resources will be blocked.
[0003] It is known from the related technology that in the current process of implementing read-write locks, all member data in the read-write lock data structure are often protected by a mutex lock. In the application process, each time the lock is held or released, the mutex lock is first used to protect the member data. Since the holding process of the mutex lock consumes a lot of performance, it is not conducive to concurrent scenarios or high-performance scenarios of read lock operations.
[0004] Therefore, finding a read-write lock implementation method that can reduce performance consumption has become a current research hotspot. Summary of the invention
[0005] The present invention provides a read-write lock implementation method, device, electronic device and storage medium, which can reduce the performance consumption during the read lock locking operation and the read lock releasing operation, and improve the read concurrency capability.
[0006] The present invention provides a read-write lock implementation method, the method comprising: pre-defining a data structure of the read-write lock, wherein the data structure at least comprises a counter, wherein the count value of the counter is used to characterize the number of processes holding the read lock; in the case of receiving a read lock operation request, determining whether the current process has a write lock operation, and obtaining a write lock operation determination result; and completing the read lock operation based on the write lock operation determination result and the atomic logic processing of the counter in the data structure.
[0007] According to a read-write lock implementation method provided by the present invention, the write lock operation judgment result includes a first judgment result, wherein the first judgment result is that there is no write lock operation in the current process; the atomic logical processing based on the write lock operation judgment result and the counter in the data structure to complete the read lock operation specifically includes: when the write lock operation judgment result is the first judgment result, the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure.
[0008] According to a read-write lock implementation method provided by the present invention, the read lock operation request includes a read lock locking operation request; the read lock operation includes a read lock locking operation; the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, specifically including: when the read lock operation request is a read lock locking operation request, the read lock locking operation is completed based on the atomic increment processing of the counter in the data structure.
[0009] According to a read-write lock implementation method provided by the present invention, the read lock operation request includes a read lock release lock operation request; the read lock operation includes a read lock release lock operation; the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, specifically including: when the read lock operation request is a read lock release lock operation request, the read lock release lock operation is completed based on the atomic decrement processing of the counter in the data structure.
[0010] According to a read-write lock implementation method provided by the present invention, the data structure also includes a mutex lock; the write lock operation judgment result includes a second judgment result, wherein the second judgment result is that a write lock operation exists in the current process; the atomic logical processing based on the write lock operation judgment result and the counter in the data structure to complete the read lock operation specifically includes: when the write lock operation judgment result is the second judgment result, based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and calling the mutex lock in the data structure, completing the read lock operation.
[0011] According to a read-write lock implementation method provided by the present invention, the data structure also includes a read operation request counter and a write operation request counter; the read lock operation request includes a read lock locking operation request; the read lock operation includes a read lock locking operation; the atomic increment processing and / or atomic decrement processing based on the counter in the data structure, and calling the mutex lock in the data structure to complete the read lock operation, specifically includes: when the read lock operation request is a read lock locking operation request, controlling the counter in the data structure to perform atomic increment processing; when the counter performs atomic increment processing, calling the mutex lock in the data structure to lock the data structure, and adding 1 to the read request count value in the read operation request counter; when the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, waking up the waiting write lock operation queue and controlling the waiting read lock operation queue to be in a waiting wake-up state; after the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, releasing the mutex lock in the data structure, and waking up the waiting read lock operation queue to complete the read lock locking operation.
[0012] According to a read-write lock implementation method provided by the present invention, after calling the mutex lock in the data structure to lock the data structure and adding 1 to the read request count value in the read operation request counter, the method also includes: when the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, waking up the waiting read lock operation queue and releasing the mutex lock in the data structure to complete the read lock locking operation.
[0013] According to a read-write lock implementation method provided by the present invention, the data structure also includes a read operation request counter and a write operation request counter; the read lock operation request includes a read lock release lock operation request; the read lock operation includes a read lock release lock operation; the atomic increment processing and / or atomic decrement processing based on the counter in the data structure, and calling the mutex lock in the data structure to complete the read lock operation, specifically includes: when the read lock operation request is a read lock release lock operation request, controlling the counter in the data structure to perform atomic decrement processing; when the counter is atomically decremented, calling the mutex lock in the data structure to lock the data structure, and reducing the read request count value in the read operation request counter by 1; when the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, waking up the waiting write lock operation queue; after the waiting write lock operation queue is woken up and there is no waiting write lock operation queue, releasing the mutex lock in the data structure, and waking up the waiting read lock operation queue to complete the read lock release lock operation.
[0014] According to a read-write lock implementation method provided by the present invention, after calling the mutex lock in the data structure to lock the data structure and reducing the read request count value in the read operation request counter by 1, the method also includes: when the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, waking up the waiting read lock operation queue and releasing the mutex lock in the data structure to complete the read lock release operation.
[0015] According to a read-write lock implementation method provided by the present invention, the counter also includes an identification bit, wherein the identification bit is used to identify whether the read-write lock is in a state where there is a queue waiting for a write lock operation; determining whether the read-write lock is in a state where there is a queue waiting for a write lock operation is implemented in the following manner: when the identification bit of the counter is a first preset identification, determining that the read-write lock is in a state where there is a queue waiting for a write lock operation; when the identification bit of the counter is a second preset identification, determining that the read-write lock is not in a state where there is a queue waiting for a write lock operation, wherein the first preset identification is different from the second preset identification.
[0016] According to a read-write lock implementation method provided by the present invention, after the data structure of the read-write lock is predefined, the method further includes: when a write lock operation request is received, calling the mutual exclusion lock in the data structure to complete the write lock operation.
[0017] According to a read-write lock implementation method provided by the present invention, the write lock operation request includes a write lock locking operation request; the write lock operation includes a write lock locking operation; the calling of the mutex lock in the data structure to complete the write lock operation specifically includes: when the write lock operation request is the write lock locking operation request, calling the mutex lock in the data structure to lock the data structure, and adding 1 to the write request count value in the write operation request counter; when the read-write lock is in a state where there is no read lock operation, releasing the mutex lock to complete the write lock locking operation.
[0018] According to a read-write lock implementation method provided by the present invention, after calling the mutex lock in the data structure to lock the data structure and adding 1 to the write request count value in the write operation request counter, the method also includes: when the read-write lock is in a read lock operation, adding the write lock operation to a queue waiting for a write lock operation; after the read lock operation is released, waking up the queue waiting for a write lock operation and releasing the mutex lock to complete the write lock locking operation.
[0019] According to a read-write lock implementation method provided by the present invention, the write lock operation request includes a write lock release lock operation request; the write lock operation includes a write lock release lock operation; the calling of the mutex lock in the data structure to complete the write lock operation specifically includes: when the write lock operation request is the write lock release lock operation request, calling the mutex lock in the data structure to lock the data structure, and reducing the write request count value in the write operation request counter by 1; when the read-write lock is in a queue where there is no waiting write lock operation, releasing the mutex lock to complete the write lock release lock operation.
[0020] According to a read-write lock implementation method provided by the present invention, after calling the mutex lock in the data structure to lock the data structure and reducing the write request count value in the write operation request counter by 1, the method also includes: when the read-write lock is in a waiting write lock operation queue, waking up the waiting write lock operation queue and releasing the mutex lock to complete the write lock release operation.
[0021] According to a read-write lock implementation method provided by the present invention, the method further includes: when the read-write lock is in a state where there is no waiting write lock operation queue, waking up the waiting read lock operation queue and performing a read lock operation process.
[0022] The present invention also provides a read-write lock implementation device, which includes: a definition module, used to predefine the data structure of the read-write lock, wherein the data structure at least includes a counter, wherein the count value of the counter is used to characterize the number of processes holding the read lock; a judgment module, used to determine whether the current process has a write lock operation when receiving a read lock operation request, and obtain a write lock operation judgment result; a processing module, used to complete the read lock operation based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure.
[0023] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any of the above-mentioned read-write lock implementation methods is implemented.
[0024] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the read-write lock implementation method described in any one of the above is implemented.
[0025] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the read-write lock implementation method described in any one of the above is implemented.
[0026] The read-write lock implementation method, device, electronic device and storage medium provided by the present invention predefine the data structure of the read-write lock, wherein the data structure at least includes a counter, wherein the count value of the counter is used to characterize the number of processes holding the read lock; when a read lock operation request is received, it is determined whether the current process has a write lock operation, and a write lock operation determination result is obtained; based on the write lock operation determination result and the atomic logic processing of the counter in the data structure, the read lock operation is completed. By completing the read lock operation through the atomic logic processing of the counter in the data structure instead of based on a mutex lock, the performance consumption during the read lock locking operation and the read lock releasing operation can be reduced, and the read concurrency capability can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0028] Figure 1 It is a flow chart of the read-write lock implementation method provided by the present invention.
[0029] Figure 2 It is one of the flow charts provided by the present invention for completing the read lock operation based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and calling the mutex lock in the data structure.
[0030] Figure 3 This is the second flow chart of the present invention for completing the read lock operation based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and calling the mutex lock in the data structure.
[0031] Figure 4 It is a schematic diagram of an application scenario of the read lock provided by the present invention.
[0032] Figure 5 It is a schematic diagram of an application scenario of the read lock and release lock provided by the present invention.
[0033] Figure 6 It is a schematic diagram of an application scenario of write lock provided by the present invention.
[0034] Figure 7 It is a schematic diagram of an application scenario of the write lock and release lock provided by the present invention.
[0035] Figure 8 It is a structural schematic diagram of the read-write lock implementation device provided by the present invention.
[0036] Fig. 9 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0037] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0038] The purpose of the read-write lock is to read more and write less. The read-write lock implementation method provided by the present invention realizes a high-performance read-write lock method that supports both fast path access and high-concurrency slow path operations through clever data structure definition, as well as atomic addition, subtraction, comparison and assignment, thereby reducing the performance consumption of read lock locking and releasing operations and improving the read concurrency capability.
[0039] Figure 1 It is a flow chart of the read-write lock implementation method provided by the present invention.
[0040] The following will be combined Figure 1 The process of the read-write lock implementation method provided by the present invention is described.
[0041] In an exemplary embodiment of the present invention, Figure 1 It can be seen that the read-write lock implementation method may include steps 110 to 130, and each step will be introduced below.
[0042] In step 110, a data structure of a read-write lock is predefined, wherein the data structure at least includes a counter, wherein a count value of the counter is used to represent the number of processes holding the read lock.
[0043] In one embodiment, a data structure of a read-write lock may be predefined, and the data structure may include at least a counter, wherein the counter is mainly used to track the number of processes holding the read lock; in addition, the counter also has an identification bit (e.g., the highest two bits) MSB, wherein the identification bit may be used to indicate a state change of the read-write lock.
[0044] In another embodiment, the data structure may further include a mutex and a conditional operation variable, wherein the change of the conditional operation variable may further be associated with the values of a read operation request counter (readers) and a write operation request counter (writers). The read operation request counter (readers) is used to characterize the request count of the read lock operation. The write operation request counter (writers) is used to characterize the request count of the write lock operation.
[0045] In another embodiment, in order to facilitate the description of the logic flow of the present invention based on the POSIX standard and the pthread library, the following data structure is defined:
[0046] struct Rw_lock_s
[0047] {
[0048] int32 counter; / / The highest two bits are used to indicate status changes
[0049] uint32 writers; / / Used to record the number of writers holding write locks and waiting for write locks
[0050] uint32 readers; / / Used to record the number of readers waiting for read locks
[0051] pthread_mutex_t mutex; / / User protection data operation in slow path
[0052] pthread_cond_t condr; / / Used to notify read wait
[0053] pthread_cond_t condw; / / Used to notify write wait
[0054] }Rw_lock;
[0055] The counters, writers, and readers are defined as follows:
[0056] When the read-write lock is not locked
[0057] counter=0;
[0058] writers=0;
[0059] readers=0;
[0060] When N read locks have no locks
[0061] counter=N;
[0062] writers=0;
[0063] readers=0;
[0064] When a write lock has no other read locks
[0065] counter=0xC0000000;
[0066] writers=1;
[0067] readers=0;
[0068] When one thread holds a write lock and N threads are waiting for the write lock
[0069] counter=0xC0000000;
[0070] writers=1+N;
[0071] readers=0;
[0072] When one thread holds a write lock and N threads wait for a read lock
[0073] counter=0xC0000000;
[0074] writers=1;
[0075] readers=N;
[0076] When N threads hold read locks and 1 thread holds write locks
[0077] counter=0x80000000+N;
[0078] writers=1;
[0079] readers=0;
[0080] When N threads hold the read lock, X threads wait for the write lock, and Y threads wait for the read lock
[0081] counter=0x80000000+N;
[0082] writers=X;
[0083] readers=Y;
[0084] In step 120, when a read lock operation request is received, it is determined whether the current process has a write lock operation, and a write lock operation determination result is obtained.
[0085] In step 130, based on the write lock operation determination result and the atomic logic processing of the counter in the data structure, the read lock operation is completed.
[0086] In one embodiment, when a read lock operation request is received, it can be first determined whether the current process has a write lock operation, and a write lock operation judgment structure can be obtained. Among them, whether the current process has a write lock operation can be determined by the identification bit in the counter. In one example, it can be determined whether the counter value MSB is 1. If it is 1, it means that a thread is holding a write lock or requesting a write lock, and this scenario can correspond to a slow path. If the counter value MSB is 0, it means that no other thread holds a write lock or requests a write lock, and this scenario can correspond to a fast path. In this embodiment, by dividing the completion of the read lock operation into a slow path and a fast path, the performance consumption of the read lock locking operation and the release operation can be reduced, and the read concurrency capability can be improved.
[0087] In yet another embodiment, the read lock operation may be completed based on the write lock operation determination result (which may include corresponding fast paths and slow paths) and atomic logic processing of the counter in the data structure.
[0088] In the application process, fast-path operations of the lock (i.e., acquiring and releasing a read lock when no process is requesting a write lock) are implemented by manipulating the counter. Slow-path operations (such as acquiring a write lock or handling contention) use the rest of the data structure (including the pthread condition) to queue processes waiting for the lock.
[0089] It should be noted that the counter variable is mainly used to track the number of processes holding the read lock, however, the two most significant bits in the counter are used to indicate special situations. If the MSB (sign bit) is clear, then there is no writer holding or waiting for the lock on the read / write mutex. This is the so-called "fast" state. In this state, a reader can lock the mutex by simply incrementing the counter and unlock the mutex by decrementing the counter. If the MSB (sign bit) is set, there is a writer holding or waiting for the lock. (When the MSB is set, the low-order bits of the counter are non-zero and the mutex is "quiesced"; when the counter is equal to 0x80000000, the mutex is "quiesced" (i.e., there is no process currently holding the lock). Once the mutex is quiesced, the second MSB can be set to move the mutex into exclusive mode, and the next process can execute. When the writer unlocks the mutex, the next writer can execute. If there are no more writers, the second MSB is cleared and the mutex returns to the quiescing / quiesced state. When the mutex is quiesced and there are no pending writers, the MSB (the sign bit, which can correspond to the flag bit) is cleared. This returns the mutex to its fast-path state, and the queue of pending readers can be kicked off.
[0090] The read-write lock implementation method provided by the present invention predefines the data structure of the read-write lock, wherein the data structure at least includes a counter, wherein the count value of the counter is used to characterize the number of processes holding the read lock; when a read lock operation request is received, it is determined whether the current process has a write lock operation, and a write lock operation determination result is obtained; based on the write lock operation determination result and the atomic logic processing of the counter in the data structure, the read lock operation is completed. By completing the read lock operation through the atomic logic processing of the counter in the data structure instead of based on a mutex lock, the performance consumption in the process of the read lock adding operation and the read lock releasing operation can be reduced, and the read concurrency capability can be improved.
[0091] In another exemplary embodiment of the present invention, continuing to use the above-mentioned embodiment as an example process description, the write lock operation judgment result may include a first judgment result, wherein the first judgment result is that there is no write lock operation in the current process.
[0092] Furthermore, based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure, the read lock operation is completed, which can be implemented in the following way:
[0093] When the write lock operation judgment result is the first judgment result, the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure.
[0094] In one embodiment, when the write lock operation judgment result is the first judgment result, it means that there is no write lock operation currently, then the fast path of read-write lock can be taken, that is, the read lock operation can be implemented based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure. In this embodiment, the number of processes currently holding the read lock can be represented by the atomic increment processing and / or atomic decrement processing of the counter, providing a basis for subsequent processing.
[0095] In yet another exemplary embodiment of the present invention, continuing to use the above-mentioned embodiment as an example for description, a read lock operation request may include a read lock plus lock operation request; and a read lock operation may include a read lock plus lock operation.
[0096] Furthermore, based on the atomic increment and / or atomic decrement processing of the counter in the data structure, the read lock operation is completed, which can be implemented in the following way:
[0097] In the case where the read lock operation request is a read lock adding operation request, the read lock adding operation is completed based on the atomic increment processing of the counter in the data structure.
[0098] In one embodiment, when there is no write lock operation process and the read lock operation request is a read lock adding operation request, the read lock adding operation can be completed directly based on the atomic increment +1 processing of the counter in the data structure.
[0099] In another exemplary embodiment of the present invention, continuing to use the above-mentioned embodiment as an example for explanation, the read lock operation request may include a read lock release lock operation request; the read lock operation may include a read lock release lock operation;
[0100] Furthermore, based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, the read lock operation is completed, which can be implemented in the following manner:
[0101] In the case where the read lock operation request is a read lock release lock operation request, the read lock release lock operation is completed based on the atomic decrement processing of the counter in the data structure.
[0102] In one embodiment, when there is no write lock operation process and the read lock operation request is a read lock release lock operation request, the read lock locking operation can be completed directly based on the atomic decrement -1 processing of the counter in the data structure.
[0103] In another exemplary embodiment of the present invention, continuing to use the above-described embodiment as an example for explanation, the data structure may further include a mutex lock; the write lock operation judgment result includes a second judgment result, wherein the second judgment result is that a write lock operation exists in the current process.
[0104] Furthermore, based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure, the read lock operation is completed, which can be implemented in the following way:
[0105] When the write lock operation judgment result is the second judgment result, the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure and calling the mutex lock in the data structure.
[0106] In one embodiment, a mutex can be used to protect a common shared resource, also known as a sleep lock. Only one caller can lock or unlock at the same time. When other callers request to lock again, they will be suspended by the operating system until the lock is available. In the application process, if the write lock operation judgment result is the second judgment result, it means that there is currently a write lock operation. In order to ensure that the common shared resource is only written by one caller at the same time, in addition to the atomic increment processing and / or atomic decrement processing based on the counter in the data structure, it is also necessary to call the mutex in the data structure to complete the read lock operation. Among them, the mutex can be used to lock the structural data in the data structure to ensure that it is only written by one caller at the same time.
[0107] Figure 2 It is one of the flow charts provided by the present invention for completing the read lock operation based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and calling the mutex lock in the data structure.
[0108] The following will be combined Figure 2 The process of atomic increment and / or atomic decrement of a counter in a data structure and calling a mutex in the data structure to complete a read lock operation is described.
[0109] In an exemplary embodiment of the present invention, the data structure may further include a read operation request counter (readers) and a write operation request counter (writers); the read lock operation request may include a read lock plus lock operation request; and the read lock operation may include a read lock plus lock operation. Figure 2 It can be seen that the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and the mutual exclusion lock in the data structure is called, which may include steps 210 to 240, and each step will be introduced below.
[0110] In step 210, when the read lock operation request is a read lock plus lock operation request, the counter in the control data structure is atomically incremented.
[0111] In one embodiment, when the read lock operation request is a read lock lock operation request, the counter in the data structure may be controlled to perform atomic increment processing, wherein the atomic increment operation may be implemented by an assembly instruction __asm__ (lockincl) or the like.
[0112] It should be noted that, since this implementation has already determined whether there is a write lock operation (the write lock operation determination result is the second determination result), that is, by determining whether the counter value MSB is 1, if it is 1, it means that a thread is holding a write lock or requesting a write lock, and a slow path (corresponding to this embodiment) needs to be taken; if the counter value MSB is 0, no other thread is holding a write lock or requesting a write lock, and a fast path is taken, and the read lock request is successful. Among them, the solution of taking the fast path and the read lock request being successful has been described in the previous embodiment.
[0113] The following is a detailed description of the read lock and lock operation along the slow path.
[0114] In step 220, when the counter is atomically incremented, the mutex in the data structure is called to lock the data structure, and the read request count value in the read operation request counter is incremented by 1.
[0115] In one embodiment, when the counter is atomically incremented by +1, the mutex in the data structure can be called to lock the data structure, and the read request count value in the read operation request counter is incremented by 1. That is, the mutex is requested, and the read request count readers +1.
[0116] In step 230, when the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, the queue waiting for the write lock operation is awakened and the queue waiting for the read lock operation is controlled to be in a waiting awakening state.
[0117] In another embodiment, when it is determined that the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, it indicates that there is currently a write lock request or the write lock is held. Further, when the read-write lock is in a silent state, it indicates that there is a process of contention between the read lock and the write lock. Under the aforementioned embodiment, the waiting write lock operation queue can be awakened to perform corresponding operation processing, and the waiting read lock operation queue can be controlled to be in a waiting wake-up state, so that the waiting read lock operation queue can be awakened again to perform the corresponding operation after the write lock operation queue completes the operation.
[0118] In another embodiment, whether quiescing is required can be determined by the counter value of the read-write lock, wherein when the MSB of the counter value is 10b and the remaining counter values are greater than 0, it indicates that the read-write lock is in a quiesced state.
[0119] In yet another embodiment, waking up the waiting write lock operation queue may be achieved by releasing the write condition variable condw.
[0120] In step 240, after the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, the mutex lock in the data structure is released, and the waiting read lock operation queue is awakened to complete the read lock locking operation.
[0121] In another embodiment, after the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, it means that there is no write lock operation currently, so the mutex in the data structure can be released, and the waiting read lock operation queue can be awakened to complete the corresponding read lock locking operation. In this embodiment, the read-write lock is not directly added with a mutex at the beginning, but when there is a write lock operation in the current process, the mutex is added during the corresponding read lock locking operation, thereby effectively reducing the performance consumption during the read lock locking operation and improving the read concurrency capability.
[0122] It should be noted that, in the process of locking the data structure as mentioned above, the data structure may also be encrypted to prevent malicious modification by other operators.
[0123] In yet another exemplary embodiment of the present invention, Figure 2 The embodiment is used as an example for explanation. After calling the mutual exclusion lock in the data structure to lock the data structure and adding 1 to the read request count value in the read operation request counter, the read-write lock implementation method may further include the following steps:
[0124] When the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, the waiting read lock operation queue is awakened and the mutex lock in the data structure is released to complete the read lock operation.
[0125] In one embodiment, when the write request count value in the write operation request counter is detected to be 0 and the current process has completed the write lock operation, it means that there is no write lock request or lock currently, so the read-write lock state can be changed to fast path. In the application process, the waiting read lock operation queue can be awakened and the mutex lock in the data structure can be released to complete the read lock locking operation. Among them, awakening the waiting read lock operation queue can be achieved by broadcasting the release of the read condition variable condr.
[0126] In another embodiment, the read-write lock state is changed to fast path by atomically changing the counter value from 0x80000000 to the readers value. If the atomic operation is successful, the read condition variable condr is released by broadcasting. If the atomic operation fails, it means that there is another read lock request, then the read lock condition variable condr is directly connected to the waiting queue and waits to be awakened by other read requests.
[0127] Figure 3 This is the second flow chart of the present invention for completing the read lock operation based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and calling the mutex lock in the data structure.
[0128] The following will be combined Figure 3 Another process of completing the read lock operation based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure and calling the mutex lock in the data structure is described.
[0129] In an exemplary embodiment of the present invention, the data structure may further include a read operation request counter and a write operation request counter; the read lock operation request may further include a read lock release lock operation request; and the read lock operation may further include a read lock release lock operation. Figure 3 It can be seen that based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and calling the mutex lock in the data structure, completing the read lock operation may include steps 310 to 340, and each step will be introduced below.
[0130] In step 310, when the read lock operation request is a read lock release lock operation request, a counter in the control data structure is atomically decremented.
[0131] In one embodiment, when the read lock operation request is a read lock release lock operation request, the counter in the data structure may be controlled to perform atomic decrement processing, wherein the atomic decrement operation may be implemented by an assembly instruction __asm__ (lockdecl) or the like.
[0132] It should be noted that, since this implementation has already determined whether there is a write lock operation (the write lock operation determination result is the second determination result), that is, by determining whether the counter value MSB is 1, if it is 1, it means that a thread is holding a write lock or requesting a write lock, and a slow path (corresponding to this embodiment) needs to be taken; if the counter value MSB is 0, no other thread is holding a write lock or requesting a write lock, and a fast path is taken, and the read lock release request is successful. Among them, the solution of taking the fast path and the read lock release request being successful has been described in the previous embodiment.
[0133] The read lock release operation along the slow path is described in detail below.
[0134] In step 320, when the counter is atomically decremented, the mutex in the data structure is called to lock the data structure, and the read request count value in the read operation request counter is decremented by 1.
[0135] In one embodiment, when the counter is atomically incremented by -1, the mutex in the data structure can be called to lock the data structure, and the read request count value in the read operation request counter is reduced by 1. That is, the mutex is requested and the read request count is readers-1.
[0136] In step 330, when the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, the waiting write lock operation queue is awakened.
[0137] In one embodiment, when it is determined that the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, it indicates that there is currently a write lock request or the write lock is held. Further, when the read-write lock is in a silent state, it indicates that there is a process of contention between the read lock and the write lock. Under the aforementioned embodiment, the waiting write lock operation queue can be awakened to perform corresponding operation processing, and the waiting read lock operation queue can be controlled to be in a waiting wake-up state, so that the waiting read lock operation queue can be awakened again to perform the corresponding operation after the write lock operation queue completes the operation.
[0138] In another embodiment, whether quiescing is required can be determined by the counter value of the read-write lock, wherein when the MSB of the counter value is 10b and the remaining counter values are greater than 0, it indicates that the read-write lock is in a quiesced state.
[0139] In yet another embodiment, waking up the waiting write lock operation queue may be achieved by releasing the write condition variable condw.
[0140] In step 340, after the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, the mutex lock in the data structure is released, and the waiting read lock operation queue is awakened to complete the read lock release operation.
[0141] In one embodiment, after the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, it means that there is no write lock operation currently, so the mutex in the data structure can be released, and the waiting read lock operation queue can be awakened to complete the corresponding read lock release operation. In this embodiment, the read-write lock is not directly added with a mutex at the beginning, but when there is a write lock operation in the current process, the mutex is added during the corresponding read lock release operation, thereby effectively reducing the performance consumption during the read lock release operation and improving the read concurrency capability.
[0142] In another embodiment, it is also possible to determine whether the current writers is zero. If it is not zero, it indicates that there is a write lock request or a write lock is currently held. The counter value is used to determine whether quiescing is required. If necessary, the read-write lock state is changed to quiescing, and the write condition variable condw is released. When the writers value is zero, it indicates that there is no write lock request or lock is currently held, and the read-write lock state is tried to be changed to fast path, that is, by atomically changing the counter value from 0x80000000 to the readers value. If successful, the read condition variable condr is broadcast and released. If the atomic operation fails, it indicates that there is another read lock request, waiting for other read lock release processing.
[0143] In yet another exemplary embodiment of the present invention, Figure 3 The embodiment described above is used as an example for explanation. After calling the mutual exclusion lock in the data structure to lock the data structure and reducing the read request count value in the read operation request counter by 1, the read-write lock implementation method may further include the following steps:
[0144] When the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, the waiting read lock operation queue is awakened and the mutex lock in the data structure is released to complete the read lock release operation.
[0145] In one embodiment, when the write request count value in the write operation request counter is detected to be 0 and the current process has completed the write lock operation, it means that there is no write lock request or lock currently, so the read-write lock state can be changed to fast path. In the application process, the waiting read lock operation queue can be awakened and the mutex lock in the data structure can be released to complete the read lock locking operation. Among them, awakening the waiting read lock operation queue can be achieved by broadcasting the release of the read condition variable condr.
[0146] In another embodiment, the read-write lock state is changed to fast path by atomically changing the counter value from 0x80000000 to the readers value. If the atomic operation is successful, the read condition variable condr is released by broadcasting. If the atomic operation fails, it means that there is another read lock request, then the read lock condition variable condr is directly connected to the waiting queue and waits to be awakened by other read requests.
[0147] In another exemplary embodiment of the present invention, the counter may further include an identification bit, wherein the identification bit may be used to identify whether the read-write lock is in a state where there is a queue waiting for a write lock operation;
[0148] Among them, determining whether the read-write lock is in a state where there is a queue waiting for a write lock operation can be achieved in the following ways:
[0149] When the flag bit of the counter is the first preset flag, determining that the read-write lock is in a state where there is a queue waiting for a write lock operation;
[0150] When the flag bit of the counter is the second preset flag, it is determined that the read-write lock is not in a state where there is a queue waiting for a write lock operation, wherein the first preset flag is different from the second preset flag.
[0151] In one embodiment, the first preset identifier may be counter=0x80000000, counter=0xC0000000. The second preset identifier may be other identifiers except the second preset identifier. The first preset identifier and the second preset identifier may be adjusted according to actual conditions and are not specifically limited in this embodiment.
[0152] In another exemplary embodiment of the present invention, after predefining the data structure of the read-write lock, the read-write lock implementation method may further include the following steps:
[0153] When a write lock operation request is received, the mutex in the data structure is called to complete the write lock operation.
[0154] In one embodiment, when a write lock operation is required, it is necessary to ensure that only one operator is operating at the same time. Therefore, a mutex in a data structure needs to be called to assist in completing the write lock operation.
[0155] In yet another exemplary embodiment of the present invention, the write lock operation request may include a write lock lock operation request; the write lock operation may include a write lock lock operation;
[0156] Furthermore, calling the mutex in the data structure to complete the write lock operation can be implemented in the following ways:
[0157] In the case where the write lock operation request is a write lock locking operation request, the mutex in the data structure is called to lock the data structure, and the write request count value in the write operation request counter is incremented by 1;
[0158] When the read-write lock is in the absence of a read lock operation, release the mutex lock to complete the write lock operation.
[0159] In one embodiment, when the write lock operation request is a write lock locking operation request, it is necessary to call the mutex in the data structure to lock the data structure, and add 1 to the write request count value in the write operation request counter, that is, request the mutex mutex, set the counter MSB to 1, that is, counter=0x80000000, the write request count writers +1, and the read-write lock enters the silent state.
[0160] In yet another embodiment, when it is determined that the read-write lock is not in a read lock operation, the mutex lock can be directly released to complete the write lock operation.
[0161] In another exemplary embodiment of the present invention, after calling the mutual exclusion lock in the data structure to lock the data structure and adding 1 to the write request count value in the write operation request counter, the read-write lock implementation method may further include the following steps:
[0162] When the read-write lock is in the state of a read lock operation, the write lock operation is added to the queue waiting for the write lock operation;
[0163] After the read lock operation is released, the queue waiting for the write lock operation is woken up and the mutex lock is released to complete the write lock operation.
[0164] In one embodiment, when it is determined that the read-write lock is in a read lock operation, it means that the write lock is operated after the read lock operation is completed. During the application process, the write lock operation can be added to the queue waiting for the write lock operation; and after the read lock operation is released, the queue waiting for the write lock operation is awakened and the mutex lock is released to complete the write lock locking operation.
[0165] In another embodiment, the counter value in the data structure can be set from 0x80000000 to 0xC0000000 using an atomic compare and exchange instruction. If the exchange is successful, it means that all read locks have been released and the write lock is successfully held. When the counter value is successfully atomically set from 0x80000000 to 0xC0000000, it means that the read-write lock has entered a mutually exclusive state. Then the mutually exclusive lock can be released to complete the write lock locking operation.
[0166] In another example, when the counter value is equal to 0x80000000, the read-write lock enters the silent completion state. In another example, 0x80000000 is set to 0xC0000000. If the exchange fails, it means that there are still threads holding the read lock that have not been released. Then you need to continue waiting, add the write lock condition variable waiting queue condw, request to suspend, and wait for other threads to release the lock before waking up, that is, add the write lock operation to the waiting write lock operation queue; and after the read lock operation is released, wake up the waiting write lock operation queue and release the mutex lock to complete the write lock locking operation.
[0167] In yet another exemplary embodiment of the present invention, the write lock operation request may include a write lock release lock operation request; and the write lock operation may include a write lock release lock operation.
[0168] Furthermore, calling the mutex in the data structure to complete the write lock operation can be implemented in the following ways:
[0169] In the case where the write lock operation request is a write lock release lock operation request, calling the mutex lock in the data structure to lock the data structure, and reducing the write request count value in the write operation request counter by 1;
[0170] When the read-write lock is in a state where there is no queue waiting for the write lock operation, the mutex lock is released to complete the write lock release operation.
[0171] In one embodiment, when the write lock operation request is a write lock release operation request, the mutex in the data structure can be called to lock the data structure, and the write request count value in the write operation request counter is reduced by 1, that is, the mutex mutex is requested, the counter MSB is set to 1, that is, counter=0x80000000, the write request count writers +1, and the read-write lock enters a silent state.
[0172] Furthermore, when the read-write lock is in a state where there is no queue waiting for a write lock operation, the mutex lock can be directly released to complete the write lock release operation.
[0173] In another exemplary embodiment of the present invention, after calling the mutual exclusion lock in the data structure to lock the data structure and decrementing the write request count value in the write operation request counter by 1, the read-write lock implementation method further includes the following steps:
[0174] When the read-write lock is in a waiting write lock operation queue, wake up the waiting write lock operation queue and release the mutex lock to complete the write lock release operation.
[0175] In one embodiment, when the read-write lock is in a waiting write lock operation queue, the waiting write lock operation queue can be awakened to perform corresponding operations and release the mutex lock, thereby completing the write lock release operation.
[0176] In another exemplary embodiment of the present invention, the read-write lock implementation method may further include the following steps:
[0177] When there is no waiting write lock operation queue for the read-write lock, wake up the waiting read lock operation queue and perform the read lock operation processing.
[0178] In one embodiment, when the read-write lock is in a state where there is no waiting write lock operation queue, it means that all write lock operations have been processed. In this scenario, the waiting read lock operation queue can be awakened and the read lock operation processing can be performed. In the process of performing the read lock operation processing, there is no need to call the mutex lock, and a fast path can be used for implementation.
[0179] In another embodiment, determining whether the read-write lock is in a state where there is a queue waiting for a write lock operation can also be achieved by judging whether the current counter value is 0. If it is not 0, it means that there are other threads waiting to request a write lock, that is, the read-write lock is in a state where there is a queue waiting for a write lock operation. Then, the condw condition variable can be used to release a signal to wake up other waiting threads, that is, wake up the queue waiting for a write lock operation and release the mutex lock, thereby completing the write lock release operation.
[0180] In another embodiment, the current counter value is 0, indicating that all write lock requests have been processed, that is, the read-write lock is in a state where there is no queue waiting for the write lock operation, and the mutex lock can be released to complete the write lock release operation.
[0181] In another embodiment, when determining whether the read-write lock is in a queue waiting for a write lock operation, the MSB of the counters value can be set to 10b, and the read-write lock state can be changed from a mutually exclusive state to a silent state. Furthermore, the counter can be set from 0x80000000 to the readers value through an atomic comparison and set instruction. If the setting is successful, the condr condition variable is used to release the signal and wake up all read lock request threads. If the setting fails, it means that a read lock request has occurred again in this process, and the current write lock release process will no longer be processed, and the read lock process will be processed.
[0182] Figure 4 It is a schematic diagram of an application scenario of the read lock provided by the present invention. Figure 5 It is a schematic diagram of an application scenario of the read lock and release lock provided by the present invention. Figure 6 It is a schematic diagram of an application scenario of write lock provided by the present invention. Figure 7 It is a schematic diagram of an application scenario of the write lock and release lock provided by the present invention.
[0183] In order to further introduce the read-write lock implementation method provided by the present invention, Figure 4-Figure 7 It is introduced from the perspectives of read lock, read lock release, write lock, and write lock release.
[0184] In one embodiment, in combination Figure 4 It can be seen that in the process of locking the read lock, a request for a read lock can be initiated, and then the counter value is atomically incremented by 1. In one example, the atomic increment operation can be implemented by assembly instructions such as __asm__ (lockincl). Then determine whether the MSB of the counter value is 1. If it is 1, it means that a thread is holding a write lock or requesting a write lock, and the slow path needs to be taken. In the slow path, it is necessary to request the mutex mutex and count the read request readers +1. Then determine whether the current writers is zero. If it is not zero, it means that there is a write lock request or a write lock is currently held. The counter value determines whether quiescence needs to be completed. If necessary, the read-write lock state is turned to quiescence, and the write condition variable condw is released, and the read condition variable condr is added to the waiting queue.
[0185] When the writers value is zero, it means that there is no write lock request at present, then the read-write lock state is switched to fast path, that is, by atomically changing the counter value from 0x80000000 to the readers value. If successful, the read condition variable condr is released through broadcast, waking up all read lock waiting threads. If the atomic operation fails, it means that there is another read lock request, then the read lock condition variable condr waiting queue is directly connected, waiting to be woken up by other read requests, and then the mutex is released to complete the read lock request.
[0186] In another embodiment, if the counter value MSB is 0, no other thread holds the write lock or requests the write lock, and a fast path can be taken to directly release the mutex lock, and the read lock request is successful.
[0187] In one embodiment, in combination Figure 5It can be seen that in the process of releasing the read lock, a request to release the read lock can be initiated, and then the counter value is atomically reduced by 1. In one example, the atomic reduction operation can be implemented by the assembly instruction __asm__(lock decl) and the like. Then determine whether the MSB of the counter value is 1. If it is 1, it means that a thread is holding a write lock or requesting a write lock, and the slow path needs to be taken. Further, request the mutex, and then determine whether the current writers is zero. If it is not zero, it means that there is a write lock request or a write lock is currently held. The counter value determines whether quiescence needs to be completed. If necessary, the read-write lock state is turned to quiescence, and the write condition variable condw is released to wake up the thread waiting for the write lock, and then release the mutex, and the read lock is released.
[0188] In another example, when the writers value is zero, it means that there is no write lock request or lock currently, then try to change the read-write lock state to fast path, that is, by atomically changing the counter value from 0x80000000 to the readers value. If successful, broadcast the release of the read condition variable condr, wake up all read lock waiting threads, and then release the mutex lock to complete the read lock release request. If the atomic operation fails, it means that there is another read lock request, waiting for other read lock release processing.
[0189] In another example, if the MSB value of the counter is 0, no other thread holds the write lock or requests the write lock, and the fast path is taken, and the read lock is released successfully.
[0190] In one embodiment, in combination Figure 6 It can be seen that during the process of locking the write lock, a request for the write lock can be initiated, and then the mutex lock mutex is requested, and the counter MSB is set to 1, that is, counter=0x80000000, the write request count writers+1, and the read-write lock enters the silent state. Further, the counter value is set from 0x80000000 to 0xC00000000 using the atomic compare and exchange instruction. If successful, it means that all read locks have been released, and the mutex lock is further released, and the write lock is successfully held.
[0191] In another embodiment, when the counter value is compared to 0x80000000 and is equal, the read-write lock enters a silent completion state; when the counter value is successfully atomically set from 0x80000000 to 0xC0000000, the read-write lock enters a mutually exclusive state, and further releases the mutually exclusive lock, and successfully holds the write lock.
[0192] When the counter value is unsuccessfully set from 0x80000000 to 0xC0000000, it means that there are still threads holding the read lock that have not released it. In this case, you need to continue waiting, join the write lock condition variable waiting queue condw, request to suspend, and wait for other threads to release the lock before waking up.
[0193] In one embodiment, in combination Figure 7 It can be seen that during the process of releasing the write lock, a request to release the write lock can be initiated, and then the mutex lock mutex is requested, and the write request count writers - 1. Then determine whether the current counter value is 0. If it is not 0, it means that there are other threads waiting to request the write lock. Then, through the condw condition variable, the signal is released to wake up other waiting threads, release the mutex lock mutex, and the write lock holding process ends.
[0194] In another embodiment, the current counter value is 0, which means that all write lock requests have been processed, and then the MSB of the counters value is set to 10b, and the read-write lock state is changed from the mutual exclusion state to the silent state; the counter is set from 0x80000000 to the readers value through the atomic comparison and set instruction. If successful, the condr condition variable is used to release the signal, wake up all read lock request threads, release the mutual exclusion lock mutex, and end the write lock holding process.
[0195] In another embodiment, the atomic compare and set instruction sets the counter from 0x80000000 to the readers value. If it fails, it means that a read lock request has occurred during this process. The current write lock release process will no longer be processed and will be processed by the read lock process.
[0196] According to the foregoing description, the read-write lock implementation method provided by the present invention, the slow path code for acquiring and releasing the read lock and the code for acquiring and releasing the write lock all use the mutex lock in the read-write lock data structure for mutual exclusive access and protection. All operations of the counter are performed using atomic operations, comparison assignments, etc., so that the fast path code for reading and unlocking can be executed in parallel without the need for locks.
[0197] According to the above analysis, the read-write lock implementation method provided by the present invention predefines the data structure of the read-write lock, wherein the data structure at least includes a counter, wherein the count value of the counter is used to characterize the number of processes holding the read lock; when a read lock operation request is received, it is determined whether the current process has a write lock operation, and the write lock operation judgment result is obtained; based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure, the read lock operation is completed. By completing the read lock operation through the atomic logic processing of the counter in the data structure instead of based on a mutex lock, the performance consumption during the read lock locking operation and the read lock releasing operation can be reduced, and the read concurrency capability can be improved.
[0198] Figure 8 It is a structural schematic diagram of the read-write lock implementation device provided by the present invention.
[0199] The read-write lock implementation device provided by the present invention is described below. The read-write lock implementation device described below and the read-write lock implementation method described above can be referenced to each other.
[0200] In an exemplary embodiment of the present invention, Figure 8 It can be seen that the read-write lock implementation device may include a definition module 810, a judgment module 820 and a processing module 830, and each module will be introduced below.
[0201] The definition module 810 may be configured to predefine a data structure of a read-write lock, wherein the data structure includes at least a counter, wherein a count value of the counter is used to represent the number of processes holding the read lock;
[0202] The judgment module 820 may be configured to judge whether the current process has a write lock operation when receiving a read lock operation request, and obtain a write lock operation judgment result;
[0203] The processing module 830 may be configured to complete the read lock operation based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure.
[0204] In an exemplary embodiment of the present invention, the write lock operation judgment result includes a first judgment result, wherein the first judgment result is that there is no write lock operation in the current process;
[0205] The processing module 830 may implement atomic logic processing based on the write lock operation judgment result and the counter in the data structure to complete the read lock operation in the following manner:
[0206] When the write lock operation judgment result is the first judgment result, the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure.
[0207] In an exemplary embodiment of the present invention, the read lock operation request includes a read lock lock operation request; the read lock operation includes a read lock lock operation;
[0208] The processing module 830 may implement the atomic increment processing and / or atomic decrement processing based on the counter in the data structure to complete the read lock operation in the following manner:
[0209] In the case where the read lock operation request is a read lock adding operation request, the read lock adding operation is completed based on the atomic increment processing of the counter in the data structure.
[0210] In an exemplary embodiment of the present invention, the read lock operation request includes a read lock release lock operation request; the read lock operation includes a read lock release lock operation;
[0211] The processing module 830 may implement the atomic increment processing and / or atomic decrement processing based on the counter in the data structure to complete the read lock operation in the following manner:
[0212] In the case where the read lock operation request is a read lock release lock operation request, the read lock release lock operation is completed based on the atomic decrement processing of the counter in the data structure.
[0213] In an exemplary embodiment of the present invention, the data structure further includes a mutex lock; the write lock operation judgment result includes a second judgment result, wherein the second judgment result is that a write lock operation exists in the current process;
[0214] The processing module 830 may implement atomic logic processing based on the write lock operation judgment result and the counter in the data structure to complete the read lock operation in the following manner:
[0215] When the write lock operation judgment result is the second judgment result, the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure, and calling the mutex lock in the data structure.
[0216] In an exemplary embodiment of the present invention, the data structure further includes a read operation request counter and a write operation request counter; the read lock operation request includes a read lock plus lock operation request; the read lock operation includes a read lock plus lock operation;
[0217] The processing module 830 may implement the atomic increment processing and / or atomic decrement processing based on the counter in the data structure, and call the mutex in the data structure to complete the read lock operation in the following manner:
[0218] When the read lock operation request is a read lock and lock operation request, controlling the counter in the data structure to perform atomic increment processing;
[0219] In the case where the counter is atomically incremented, the mutex in the data structure is called to lock the data structure, and the read request count value in the read operation request counter is incremented by 1;
[0220] When the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, waking up the waiting write lock operation queue and controlling the waiting read lock operation queue to be in a waiting wake-up state;
[0221] After the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, the mutex lock in the data structure is released, and the waiting read lock operation queue is awakened to complete the read lock locking operation.
[0222] In an exemplary embodiment of the present invention, the processing module 830 may also be configured to:
[0223] When the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, the waiting read lock operation queue is awakened and the mutex lock in the data structure is released to complete the read lock operation.
[0224] In an exemplary embodiment of the present invention, the data structure further includes a read operation request counter and a write operation request counter; the read lock operation request includes a read lock release lock operation request; the read lock operation includes a read lock release lock operation;
[0225] The processing module 830 may also implement atomic increment processing and / or atomic decrement processing based on the counter in the data structure, and call the mutex in the data structure to complete the read lock operation in the following manner:
[0226] When the read lock operation request is a read lock release lock operation request, controlling the counter in the data structure to perform atomic decrement processing;
[0227] In the case where the counter is atomically decremented, the mutex in the data structure is called to lock the data structure, and the read request count value in the read operation request counter is decremented by 1;
[0228] When the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, waking up the queue waiting for the write lock operation;
[0229] After the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, the mutex lock in the data structure is released, and the waiting read lock operation queue is awakened to complete the read lock release operation.
[0230] In an exemplary embodiment of the present invention, the processing module 830 may also be configured to:
[0231] When the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, the waiting read lock operation queue is awakened and the mutex lock in the data structure is released to complete the read lock release operation.
[0232] In an exemplary embodiment of the present invention, the counter further includes an identification bit, wherein the identification bit is used to identify whether the read-write lock is in a state where there is a queue waiting for a write lock operation;
[0233] The processing module 830 may determine whether the read-write lock is in a state where there is a queue waiting for a write lock operation in the following manner:
[0234] When the flag bit of the counter is a first preset flag, determining that the read-write lock is in a state where there is a queue waiting for a write lock operation;
[0235] When the flag bit of the counter is a second preset flag, it is determined that the read-write lock is not in a state where there is a queue waiting for a write lock operation, wherein the first preset flag is different from the second preset flag.
[0236] In an exemplary embodiment of the present invention, the read-write lock implementation device may further include a calling module, which may be configured to call the mutual exclusion lock in the data structure to complete the write lock operation when a write lock operation request is received.
[0237] In an exemplary embodiment of the present invention, the write lock operation request includes a write lock locking operation request; the write lock operation includes a write lock locking operation;
[0238] The calling module can use the following method to call the mutex lock in the data structure to complete the write lock operation:
[0239] In the case where the write lock operation request is the write lock locking operation request, calling the mutex lock in the data structure to lock the data structure, and adding 1 to the write request count value in the write operation request counter;
[0240] When the read-write lock is in a state where there is no read lock operation, the mutex lock is released to complete the write lock operation.
[0241] In an exemplary embodiment of the present invention, the calling module may also be configured to:
[0242] When the read-write lock is in a state where a read lock operation exists, adding the write lock operation to a queue waiting for a write lock operation;
[0243] After the read lock operation is released, the waiting write lock operation queue is awakened and the mutex lock is released, completing the write lock locking operation.
[0244] In an exemplary embodiment of the present invention, the write lock operation request includes a write lock release lock operation request; the write lock operation includes a write lock release lock operation;
[0245] The calling module can use the following method to call the mutex lock in the data structure to complete the write lock operation:
[0246] In the case where the write lock operation request is a write lock release operation request, calling the mutex lock in the data structure to lock the data structure, and reducing the write request count value in the write operation request counter by 1;
[0247] When the read-write lock is in a state where there is no queue waiting for a write lock operation, the mutex lock is released to complete the write lock release operation.
[0248] In an exemplary embodiment of the present invention, the calling module may also be configured to:
[0249] When the read-write lock is in a waiting write lock operation queue, the waiting write lock operation queue is awakened and the mutex lock is released, thereby completing the write lock release operation.
[0250] In an exemplary embodiment of the present invention, the calling module may also be configured to:
[0251] When the read-write lock is in a state where there is no queue waiting for a write lock operation, the queue waiting for a read lock operation is awakened, and a read lock operation process is performed.
[0252] Fig. 9 An example of a physical structure diagram of an electronic device is shown in FIG. Fig. 9 As shown, the electronic device may include: a processor 910, a communication interface 920, a memory 930 and a communication bus 940, wherein the processor 910, the communication interface 920 and the memory 930 communicate with each other through the communication bus 940. The processor 910 may call the logic instructions in the memory 930 to execute the read-write lock implementation method, which includes: pre-defining a data structure of the read-write lock, wherein the data structure at least includes a counter, wherein the count value of the counter is used to characterize the number of processes holding the read lock; in the case of receiving a read lock operation request, determining whether the current process has a write lock operation, and obtaining a write lock operation determination result; completing the read lock operation based on the write lock operation determination result and the atomic logic processing of the counter in the data structure.
[0253] In addition, the logic instructions in the above-mentioned memory 930 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0254] On the other hand, the present invention also provides a computer program product, which includes a computer program, and the computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the read-write lock implementation method provided by the above-mentioned methods, and the method includes: pre-defining a data structure of a read-write lock, wherein the data structure at least includes a counter, wherein the count value of the counter is used to characterize the number of processes holding the read lock; when a read lock operation request is received, determining whether the current process has a write lock operation, and obtaining a write lock operation judgment result; and completing the read lock operation based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure.
[0255] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the read-write lock implementation method provided by the above-mentioned methods, the method comprising: pre-defining a data structure of a read-write lock, wherein the data structure at least includes a counter, wherein the count value of the counter is used to characterize the number of processes holding a read lock; upon receiving a read lock operation request, determining whether the current process has a write lock operation, and obtaining a write lock operation determination result; and completing the read lock operation based on the write lock operation determination result and the atomic logic processing of the counter in the data structure.
[0256] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0257] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0258] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these 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 invention.
Claims
1. A method for implementing a read-write lock, characterized in that: The method comprises: Predefine a data structure of a read-write lock, wherein the data structure at least includes a counter, wherein a count value of the counter is used to represent the number of processes holding the read lock; When receiving a read lock operation request, determine whether the current process has a write lock operation, and obtain the write lock operation determination result; Based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure, a read lock operation is completed, wherein the data structure also includes a mutex lock; the write lock operation judgment result includes a second judgment result, wherein the second judgment result is that a write lock operation exists in the current process; The atomic logic processing based on the write lock operation judgment result and the counter in the data structure to complete the read lock operation specifically includes: When the write lock operation judgment result is the second judgment result, the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure and calling the mutex lock in the data structure, wherein, The data structure also includes a read operation request counter and a write operation request counter; the read lock operation request includes a read lock locking operation request; the read lock operation includes a read lock locking operation; The atomic increment processing and / or atomic decrement processing of the counter in the data structure and calling the mutex in the data structure to complete the read lock operation specifically include: When the read lock operation request is a read lock and lock operation request, controlling the counter in the data structure to perform atomic increment processing; In the case where the counter is atomically incremented, the mutex in the data structure is called to lock the data structure, and the read request count value in the read operation request counter is incremented by 1; When the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, waking up the waiting write lock operation queue and controlling the waiting read lock operation queue to be in a waiting wake-up state; After the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, the mutex lock in the data structure is released, and the waiting read lock operation queue is awakened to complete the read lock locking operation.
2. The read-write lock implementation method according to claim 1, characterized in that: The write lock operation judgment result includes a first judgment result, wherein the first judgment result is that there is no write lock operation in the current process; The atomic logic processing based on the write lock operation judgment result and the counter in the data structure to complete the read lock operation specifically includes: When the write lock operation judgment result is the first judgment result, the read lock operation is completed based on the atomic increment processing and / or atomic decrement processing of the counter in the data structure.
3. The read-write lock implementation method according to claim 2, characterized in that: The read lock operation request includes a read lock locking operation request; the read lock operation includes a read lock locking operation; The atomic increment processing and / or atomic decrement processing of the counter in the data structure is used to complete the read lock operation, specifically including: In the case where the read lock operation request is a read lock adding operation request, the read lock adding operation is completed based on the atomic increment processing of the counter in the data structure.
4. The read-write lock implementation method according to claim 2, characterized in that: The read lock operation request includes a read lock release lock operation request; the read lock operation includes a read lock release lock operation; The atomic increment processing and / or atomic decrement processing of the counter in the data structure is used to complete the read lock operation, specifically including: In the case where the read lock operation request is a read lock release lock operation request, the read lock release lock operation is completed based on the atomic decrement processing of the counter in the data structure.
5. The read-write lock implementation method according to claim 1, characterized in that: After calling the mutex lock in the data structure to lock the data structure and adding 1 to the read request count value in the read operation request counter, the method further includes: When the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, the waiting read lock operation queue is awakened and the mutex lock in the data structure is released to complete the read lock operation.
6. The read-write lock implementation method according to claim 1, characterized in that: The data structure also includes a read operation request counter and a write operation request counter; the read lock operation request includes a read lock release lock operation request; the read lock operation includes a read lock release lock operation; The atomic increment processing and / or atomic decrement processing of the counter in the data structure and calling the mutex in the data structure to complete the read lock operation specifically include: When the read lock operation request is a read lock release lock operation request, controlling the counter in the data structure to perform atomic decrement processing; In the case where the counter is atomically decremented, the mutex in the data structure is called to lock the data structure, and the read request count value in the read operation request counter is decremented by 1; When the write request count value in the write operation request counter is not 0 and the read-write lock is in a silent state, waking up the queue waiting for the write lock operation; After the waiting write lock operation queue is awakened and there is no waiting write lock operation queue, the mutex lock in the data structure is released, and the waiting read lock operation queue is awakened to complete the read lock release operation.
7. The read-write lock implementation method according to claim 6, characterized in that: After calling the mutex in the data structure to lock the data structure and decrementing the read request count value in the read operation request counter by 1, the method further includes: When the write request count value in the write operation request counter is 0 and the current process has completed the write lock operation, the waiting read lock operation queue is awakened and the mutex lock in the data structure is released to complete the read lock release operation.
8. The read-write lock implementation method according to claim 1, characterized in that: The counter further includes an identification bit, wherein the identification bit is used to identify whether the read-write lock is in a state where there is a queue waiting for a write lock operation; Determine whether the read-write lock is in a state where there is a queue waiting for a write lock operation, and implement it in the following way: When the flag bit of the counter is a first preset flag, determining that the read-write lock is in a state where there is a queue waiting for a write lock operation; When the flag bit of the counter is a second preset flag, it is determined that the read-write lock is not in a state where there is a queue waiting for a write lock operation, wherein the first preset flag is different from the second preset flag.
9. The read-write lock implementation method according to claim 1, characterized in that: After the data structure of the read-write lock is predefined, the method further includes: When a write lock operation request is received, the mutex in the data structure is called to complete the write lock operation.
10. The read-write lock implementation method according to claim 9, characterized in that: The write lock operation request includes a write lock locking operation request; the write lock operation includes a write lock locking operation; The calling of the mutex lock in the data structure to complete the write lock operation specifically includes: In the case where the write lock operation request is the write lock locking operation request, calling the mutex lock in the data structure to lock the data structure, and adding 1 to the write request count value in the write operation request counter; When the read-write lock is in a state where there is no read lock operation, the mutex lock is released to complete the write lock operation.
11. The read-write lock implementation method according to claim 10, characterized in that: After calling the mutex in the data structure to lock the data structure and adding 1 to the write request count value in the write operation request counter, the method further includes: When the read-write lock is in a state where a read lock operation exists, adding the write lock operation to a queue waiting for a write lock operation; After the read lock operation is released, the waiting write lock operation queue is awakened and the mutex lock is released, completing the write lock locking operation.
12. The read-write lock implementation method according to claim 9, characterized in that: The write lock operation request includes a write lock release lock operation request; the write lock operation includes a write lock release lock operation; The calling of the mutex lock in the data structure to complete the write lock operation specifically includes: In the case where the write lock operation request is a write lock release operation request, calling the mutex lock in the data structure to lock the data structure, and reducing the write request count value in the write operation request counter by 1; When the read-write lock is in a state where there is no queue waiting for a write lock operation, the mutex lock is released to complete the write lock release operation.
13. The method for implementing a read-write lock according to claim 12, characterized in that: After calling the mutex in the data structure to lock the data structure and reducing the write request count value in the write operation request counter by 1, the method further includes: When the read-write lock is in a waiting write lock operation queue, the waiting write lock operation queue is awakened and the mutex lock is released, thereby completing the write lock release operation.
14. The read-write lock implementation method according to claim 12, characterized in that: The method further comprises: When the read-write lock is in a state where there is no queue waiting for a write lock operation, the queue waiting for a read lock operation is awakened, and a read lock operation process is performed.
15. A read-write lock implementation device, characterized in that: The device is used to implement the read-write lock implementation method according to any one of claims 1 to 14, and the device includes: A definition module, used to predefine a data structure of a read-write lock, wherein the data structure at least includes a counter, wherein a count value of the counter is used to represent the number of processes holding the read lock; The judgment module is used to judge whether there is a write lock operation in the current process when receiving a read lock operation request, and obtain the write lock operation judgment result; A processing module is used to complete the read lock operation based on the write lock operation judgment result and the atomic logic processing of the counter in the data structure.
16. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the read-write lock implementation method according to any one of claims 1 to 14 is implemented.
17. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the read-write lock implementation method according to any one of claims 1 to 14 is implemented.
18. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the read-write lock implementation method according to any one of claims 1 to 14 is implemented.