Read-write lock read-write method and device, equipment and storage medium
By introducing two native read-write locks with the same functions into the operating system, write lock priority, nesting, upgrade and downgrade are realized, which solves the problem that existing read-write locks do not support these features and improves the efficiency of multi-threaded software development. and throughput.
Patent Information
- Application Number
- CN202410965348.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-18
- Publication Date
- 2024-09-03
AI Technical Summary
Existing read-write locks do not support write lock priority, write lock nesting, read lock upgrade, and write lock degradation features in multi-threaded software development, resulting in write starvation problems in frequent read environments and reducing system processing throughput and efficiency. .
By introducing two native read-write locks with the same function into the operating system, the write lock number counter and the read lock number counter are used to implement write lock priority, write lock nesting, read lock upgrade and write lock downgrade, combined with lock status bit recording , optimize the read-write lock acquisition and release process.
It effectively solves the write starvation problem in frequent reading environments, reduces the difficulty of multi-threaded software development, reduces the possibility of deadlock, and improves the efficiency of obtaining read locks and system processing throughput.
Smart Images

Figure CN118585347A
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a read-write lock reading and writing method, device, equipment and storage medium. Background Art
[0002] When developing multi-threaded software, locks are usually needed to protect shared resources between threads to avoid data confusion caused by multiple threads modifying shared resources at the same time. At present, compared with the mutual exclusion lock that can only have one thread access to shared resources at the same time, the read-write lock further divides the access to resources into two situations: reading and modifying. It allows multiple threads to obtain read locks at the same time, so that multiple threads can read shared resources at the same time, thereby improving the access efficiency of multiple threads to shared resources and the running performance of multi-threaded programs, especially for scenarios with more reading and less writing. The performance improvement is even more significant. Therefore, when developing multi-threaded software, read-write locks are often used in large quantities.
[0003] However, when using C / C++ language for multi-threaded software development, although various mainstream operating systems (OS, Operating System) usually provide native C interfaces for read-write locks, such as SRW Lock (Slim Reader / Writer Lock) interface on Windows, pthread_rwlock interface on Linux, Android, macOS, iOS, UNIX, and the implementation of these interfaces is relatively simple and efficient, they do not support some important features of read-write locks, such as write lock priority, write lock nesting, read lock upgrade, write lock downgrade, etc. In addition to the native read-write lock of the operating system, some other read-write lock implementation solutions usually use mutex locks and critical section technology. Due to the characteristics of mutex locks and critical section technology, that is, only one thread can enter at the same time, and other threads are blocked. Therefore, even if there are only read threads in the running environment, there will be threads blocked by mutex locks or critical sections. Although the blocked threads can be quickly awakened, the efficiency of requesting to obtain read locks is greatly reduced, thereby reducing the throughput of system processing.
[0004] In summary, how to enable the read-write lock to support the features of write lock priority, write lock nesting, read lock upgrade, and write lock downgrade while inheriting the high read operation performance and non-blocking of the native read-write lock of the operating system is a technical problem that needs to be further solved in this field. Summary of the invention
[0005] In view of this, the purpose of this application is to provide a read-write lock reading and writing method, device, equipment and storage medium, which can make up for the shortcomings of native read-write locks, solve the write starvation problem in a read-frequent environment, reduce the difficulty of using read-write locks for multi-threaded software development and programming, reduce the occurrence of deadlocks, and improve the efficiency and throughput of obtaining read locks. The specific scheme is as follows:
[0006] In a first aspect, the present application discloses a read-write lock reading and writing method, which is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same function, including:
[0007] When a write lock request for a target thread is obtained, a write lock quantity counter is used to determine whether the current target thread has obtained a write lock;
[0008] If the target thread has obtained the write lock, the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread;
[0009] If the target thread has not obtained the write lock, sending a request to obtain the write lock to the first native read-write lock to obtain the first native write lock;
[0010] Determine whether the target thread has obtained the read lock by using a read lock quantity counter, and if the target thread has obtained the read lock, release the read lock that the second native read-write lock has obtained;
[0011] Sending a request to obtain a write lock to the second native read-write lock to obtain a second native write lock;
[0012] The first native write lock in the write lock state is released, and the target thread is set to the write lock state, and then the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread.
[0013] Optionally, the read-write lock reading and writing method further includes:
[0014] Obtain the number of write locks that the current target thread has obtained, and obtain the number of write locks;
[0015] If the number of write locks is equal to 0, it indicates that the write lock release process of the target thread has been completed;
[0016] If the number of write locks is greater than 1, a count-decrement operation is performed on the number of write locks of the target thread to complete the write lock release process of the target thread;
[0017] If the write lock quantity is equal to 1, determine whether the current target thread has obtained a read lock through the read lock quantity counter;
[0018] If the target thread currently obtains the read lock, the number of write locks of the target thread is counted down to complete the write lock release process of the target thread;
[0019] If the current target thread has not obtained a read lock, the second native write lock in the write lock state that has been obtained by the target thread is released, and the number of write locks of the target thread is counted and decremented to complete the write lock release process of the target thread.
[0020] Optionally, the read-write lock reading and writing method further includes:
[0021] When a read lock request for the target thread is obtained, determining whether the current target thread has obtained a read lock by using the read lock quantity counter;
[0022] If the target thread has obtained a read lock, the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread;
[0023] If the target thread has not obtained the read lock, determining whether the current target thread has obtained the write lock by using the write lock quantity counter;
[0024] If the target thread obtains the write lock, the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread;
[0025] If the target thread has not obtained the write lock, a request to obtain a read lock is sent to the first native read-write lock to obtain a first native read lock, and a request to obtain a read lock is sent to the second native read-write lock to obtain a second native read lock;
[0026] The second native read lock in the read lock state is released, and the target thread is set to the read lock state, and then the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread.
[0027] Optionally, the read-write lock reading and writing method further includes:
[0028] Obtain the number of read locks that the current target thread has obtained, and obtain the number of read locks;
[0029] If the number of read locks is equal to 0, it indicates that the read lock release process of the target thread has been completed;
[0030] If the number of read locks is greater than 0, the number of read locks of the target thread is counted down to complete the read lock release process of the target thread;
[0031] If the read lock quantity is equal to 1, determine whether the current target thread has obtained a write lock through the write lock quantity counter;
[0032] If the target thread currently obtains the write lock, the number of read locks of the target thread is counted down to complete the read lock release process of the target thread;
[0033] If the current target thread has not obtained the write lock, determine whether the current target thread is in the read lock state according to the lock state bit of the current target thread;
[0034] If the target thread is currently in a read lock state, releasing the second native read lock that has been acquired by the target thread;
[0035] If the current target thread is in a write lock state, releasing the second native write lock that has been acquired by the target thread;
[0036] The number of read locks of the target thread is counted and decremented to complete the read lock release process of the target thread.
[0037] Optionally, the read-write lock reading and writing method further includes:
[0038] The lock state of the target thread is recorded via a preset thread lock state bit.
[0039] In a second aspect, the present application discloses a read-write lock reading and writing device, which is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same functions, including:
[0040] A first judgment module is used to, when a write lock request for a target thread is obtained, determine whether the current target thread has obtained a write lock through a write lock quantity counter;
[0041] A first counting self-incrementing module is used for counting and self-incrementing the number of write locks of the target thread if the target thread has obtained the write lock, so as to complete the write lock acquisition process of the target thread;
[0042] A first request sending module, configured to send a request to obtain a write lock to the first native read-write lock if the target thread has not obtained the write lock, so as to obtain the first native write lock;
[0043] A second judgment module is used to judge whether the target thread has obtained the read lock through a read lock quantity counter;
[0044] a read lock releasing module, configured to release the read lock acquired by the second native read-write lock if the target thread has acquired the read lock;
[0045] A second request sending module, used to send a request to obtain a write lock to the second native read-write lock, to obtain a second native write lock;
[0046] A write lock acquisition module is used to release the first native write lock in the write lock state, set the target thread to the write lock state, and then count and increment the number of write locks of the target thread to complete the write lock acquisition process of the target thread.
[0047] The read-write lock read-write device also includes:
[0048] A write lock quantity acquisition unit, used to acquire the number of write locks that the current target thread has acquired, to obtain the number of write locks;
[0049] A first write lock releasing unit, configured to indicate that the write lock releasing process of the target thread has been completed if the number of write locks is equal to 0;
[0050] A first counting self-decrementing unit, configured to, if the number of write locks is greater than 1, perform a counting self-decrementing operation on the number of write locks of the target thread to complete a write lock release process of the target thread;
[0051] A target judgment unit, configured to judge whether the current target thread has obtained a read lock by using the read lock number counter if the write lock number is equal to 1;
[0052] A second counting self-decrementing unit is used for, if the target thread currently obtains the read lock, to count and decrement the number of write locks of the target thread to complete the write lock release process of the target thread;
[0053] The second write lock releasing unit is used to release the second native write lock in the write lock state that has been obtained by the target thread if the current target thread has not obtained the read lock, and to count and decrement the number of write locks of the target thread to complete the write lock release process of the target thread.
[0054] The read-write lock read-write device also includes:
[0055] The lock state recording unit is used to record the lock state of the target thread through a preset thread lock state bit.
[0056] In a third aspect, the present application discloses an electronic device, comprising a processor and a memory; wherein the processor implements the aforementioned read-write lock reading and writing method when executing a computer program stored in the memory.
[0057] In a fourth aspect, the present application discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the aforementioned read-write lock reading and writing method is implemented.
[0058] It can be seen that the present application is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the functions of the first native read-write lock and the second native read-write lock are the same, including: when a write lock request for a target thread is obtained, a write lock number counter is used to determine whether the current target thread has obtained a write lock; if the target thread has obtained a write lock, the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread; if the target thread has not obtained a write lock, a write lock acquisition request is sent to the first native read-write lock to obtain a first native write lock; a read lock number counter is used to determine whether the target thread has obtained a read lock, and if the target thread has obtained a read lock, the read lock that has been obtained by the second native read-write lock is released; a write lock acquisition request is sent to the second native read-write lock to obtain a second native write lock; the first native write lock in the write lock state is released, and the target thread is set to the write lock state, and the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread. The present application performs read and write locks by combining two native read-write locks with the same functions, inheriting the advantages of high read operation performance and non-blocking of the native read-write lock of the operating system, realizing write lock priority, write lock nesting, read lock upgrade and write lock downgrade, making up for the shortcomings of the native read-write lock, solving the write starvation problem in a read-frequent environment, reducing the difficulty of multi-threaded software development and programming using read-write locks, reducing the occurrence of deadlocks, and improving the efficiency and throughput of obtaining read locks. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0060] Figure 1 A flowchart for obtaining a write lock disclosed in this application;
[0061] Figure 2 A specific flow chart for releasing a write lock disclosed in this application;
[0062] Figure 3 A specific read lock request flow chart disclosed in this application;
[0063] Figure 4 A specific read lock release flow chart disclosed in this application;
[0064] Figure 5 This is a schematic diagram of the structure of a read-write device of a read-write lock disclosed in this application;
[0065] Figure 6 This is a structural diagram of an electronic device disclosed in this application. DETAILED DESCRIPTION
[0066] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0067] The embodiment of the present application discloses a method for reading and writing a read-write lock, which is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same functions, see Figure 1 As shown, the method includes:
[0068] Step S11: when a write lock request for a target thread is obtained, it is determined by using a write lock quantity counter whether the target thread has obtained a write lock.
[0069] It should be pointed out that the read-write lock read-write solution proposed in this application is applied to the operating system, and the operating system contains two native read-write locks with the same functions, namely the first native read-write lock (RWLock1) and the second native read-write lock (RWLock2), and these two native read-write locks have the functions of adding write locks and adding read locks, wherein the first native read-write lock can be used as a waiting read-write lock, and the second native read-write lock can be used as a main read-write lock, and each thread can perform corresponding read and write operations through these two native read-write locks. Specifically, when a write lock request for the current thread, that is, the target thread, is obtained, the write lock number counter (WLockNum) can be used to determine whether the current target thread has obtained a write lock. The write lock number counter (WLockNum) is used to record the number of write locks that the thread has obtained.
[0070] In addition, it should be noted that the operating system may also include a read lock number counter (RLockNum); the read lock number counter (RLockNum) is used to record the number of read locks that the thread has obtained. Furthermore, the operating system may also include a thread lock state bit (LockPhase) for recording the thread lock state, such as the write lock state and the read lock state.
[0071] Step S12: If the target thread has obtained the write lock, the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread.
[0072] In this embodiment, if the count value of the write lock quantity counter is not 0, it indicates that the target thread has obtained the write lock. At this time, the write lock quantity of the target thread can be counted and incremented to complete the write lock acquisition process of the target thread.
[0073] Step S13: If the target thread has not obtained the write lock, a write lock acquisition request is sent to the first native read-write lock to obtain the first native write lock.
[0074] In this embodiment, if the target thread has not obtained a write lock, a write lock acquisition request is sent to the first native read-write lock in the operating system, thereby obtaining a native write lock, namely, the first native write lock.
[0075] Step S14: determining whether the target thread has obtained a read lock by using a read lock quantity counter; if the target thread has obtained a read lock, releasing the read lock that the second native read-write lock has obtained.
[0076] In this embodiment, after sending a write lock acquisition request to the first native read-write lock (RWLock1) to obtain the first native write lock, further, it is possible to determine whether the target thread has already obtained the read lock through the read lock number counter (RLockNum). If the target thread has already obtained the read lock, the read lock already obtained by the second native read-write lock (RWLock2) is released. Specifically, it is possible to determine whether the target thread has already obtained the read lock through whether the read lock number counter (RLockNum) is not equal to 0.
[0077] Step S15: Send a write lock acquisition request to the second native read-write lock to obtain the second native write lock.
[0078] In this embodiment, after the read lock that has been acquired by the second native read-write lock (RWLock2) is released, a request to acquire a write lock is sent to the second native read-write lock (RWLock2), thereby obtaining a second native write lock.
[0079] Step S16: releasing the first native write lock in the write lock state, setting the target thread to the write lock state, and then counting and incrementing the number of write locks of the target thread to complete the write lock acquisition process of the target thread.
[0080] In this embodiment, after sending a write lock request to the second native read-write lock to obtain the second native write lock, the first native write lock in the write lock state is released, and then the state of the current target thread is set to the write lock state, and the lock state of the current target thread is recorded through the thread lock state bit (LockPhase), and then the number of write locks of the above target thread is counted + 1, that is, the count value of the write lock number counter (WLockNum) is increased by 1, thereby completing the entire write lock process of the target thread.
[0081] It can be seen that the embodiment of the present application is applied to an operating system, and the operating system includes a first native read-write lock and a second native read-write lock, and the functions of the first native read-write lock and the second native read-write lock are the same, including: when a write lock request for a target thread is obtained, the write lock number counter is used to determine whether the current target thread has obtained the write lock; if the target thread has obtained the write lock, the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread; if the target thread has not obtained the write lock, a write lock acquisition request is sent to the first native read-write lock to obtain the first native write lock; whether the target thread has obtained the read lock is determined by the read lock number counter, and if the target thread has obtained the read lock, the read lock that has been obtained by the second native read-write lock is released; a write lock acquisition request is sent to the second native read-write lock to obtain the second native write lock; the first native write lock in the write lock state is released, and the target thread is set to the write lock state, and the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread. The embodiment of the present application performs read locks and write locks by combining two native read-write locks with the same functions, inherits the advantages of high read operation performance and non-blocking of the native read-write lock of the operating system, realizes write lock priority, write lock nesting, read lock upgrade and write lock downgrade, makes up for the shortcomings of the native read-write lock, can solve the write starvation problem in a read-frequent environment, reduces the difficulty of using read-write locks for multi-threaded software development and programming, reduces the occurrence of deadlocks, and improves the efficiency and throughput of obtaining read locks.
[0082] The embodiment of the present application discloses a specific read-write lock reading and writing method, which is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same functions, see Figure 2 As shown in the figure, the function of releasing the write lock can also be achieved through two native read-write locks with the same functions:
[0083] Step S21: Obtain the number of write locks that the current target thread has obtained, and obtain the number of write locks.
[0084] In this embodiment, the acquired write lock can also be released through two native read-write locks with the same function. Specifically, the number of write locks that the current target thread has acquired is first obtained to obtain the corresponding number of write locks.
[0085] Step S22: If the number of write locks is equal to 0, it indicates that the write lock release process of the target thread has been completed.
[0086] In this embodiment, after the number of write locks that the current target thread has obtained is obtained, if the number of write locks is equal to 0, it indicates that the write lock release process of the target thread has been completed.
[0087] Step S23: If the number of write locks is greater than 1, a count-decrement operation is performed on the number of write locks of the target thread to complete the write lock release process of the target thread.
[0088] In this embodiment, if the number of write locks is greater than 1, a count-decrement operation is performed on the number of write locks of the target thread, thereby completing the write lock release process of the target thread.
[0089] Step S24: If the write lock quantity is equal to 1, determine whether the current target thread has obtained a read lock through a read lock quantity counter.
[0090] In this embodiment, if the write lock number=1, whether the read lock number counter (RLockNum) is not equal to 0 is used to determine whether the current target thread has obtained the read lock.
[0091] Step S25: If the target thread currently obtains the read lock, the number of write locks of the target thread is counted down to complete the write lock release process of the target thread.
[0092] In this embodiment, if the current target thread has obtained a read lock, the number of write locks of the target thread is counted -1, thereby completing the write lock release process of the target thread.
[0093] Step S26: If the target thread has not obtained a read lock at present, the second native write lock in the write lock state that has been obtained by the target thread is released, and the number of write locks of the target thread is counted and decremented to complete the write lock release process of the target thread.
[0094] In this embodiment, if the current target thread has not obtained a read lock, the second native write lock that has been obtained by the target thread and is in a write lock state is released, and then the number of write locks of the target thread is counted by -1, that is, the count value of the write lock number counter (WLockNum) is set to -1, thereby completing the write lock release process of the target thread.
[0095] It can be seen that the embodiment of the present application provides a detailed introduction to the process of releasing the write lock. It is based on the combined use of two native read-write locks in the operating system. The method is simple and inherits the advantages of high read operation performance and non-blocking of the native read-write lock of the operating system. It can not only release the acquired write lock, but also very simply implement the write priority feature of the read-write lock, and at the same time simplifies the write lock downgrade process, making up for the shortcomings of the native read-write lock of the operating system. Compared with other read-write locks implemented using mutex locks or critical section technology, the present application can support non-blocking acquisition of read locks. In scenarios with more reads and less writes, both efficiency and throughput are greatly improved.
[0096] The embodiment of the present application discloses a specific read-write lock reading and writing method, which is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same functions, see Figure 3 As shown in the figure, the function of acquiring a read lock can also be achieved through two native read-write locks with the same functions:
[0097] Step S31: when a read lock request for a target thread is obtained, it is determined by using a read lock quantity counter whether the target thread has obtained a read lock.
[0098] In this embodiment, when a read lock request for the current thread, that is, the target thread, is obtained, it can be determined by checking whether the count value of the read lock number counter (RLockNum) is 0 to determine whether the current target thread has obtained the read lock.
[0099] Step S32: If the target thread has obtained the read lock, the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread.
[0100] In this embodiment, if the current count value of the read lock quantity counter is not 0, it indicates that the target thread has obtained the read lock. At this time, the read lock quantity of the target thread can be counted +1 to complete the read lock acquisition process of the target thread.
[0101] Step S33: If the target thread has not obtained the read lock, determine whether the current target thread has obtained the write lock through the write lock quantity counter.
[0102] In this embodiment, if the target thread has not obtained a read lock, it is possible to determine whether the current target thread has obtained a write lock through a write lock number counter (WLockNum), that is, to determine whether the count value of the write lock number counter (WLockNum) is 0.
[0103] Step S34: If the target thread obtains the write lock, the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread.
[0104] In this embodiment, if the target thread currently obtains the write lock, the number of read locks of the target thread may be counted +1, thereby completing the read lock acquisition process of the target thread.
[0105] Step S35: If the target thread has not obtained the write lock, a request to obtain a read lock is sent to the first native read-write lock to obtain the first native read lock, and a request to obtain a read lock is sent to the second native read-write lock to obtain the second native read lock.
[0106] In this embodiment, if the current target thread has not obtained a write lock, a read lock acquisition request can be sent to the above-mentioned first native read-write lock to obtain a native read lock, i.e., the first native read lock, and then a read lock acquisition request can be sent to the above-mentioned second native read-write lock to obtain a native read lock, i.e., the second native read lock.
[0107] Step S36: releasing the second native read lock in the read lock state, setting the target thread to the read lock state, and then counting and incrementing the number of read locks of the target thread to complete the read lock acquisition process of the target thread.
[0108] In this embodiment, a read lock acquisition request is sent to the second native read-write lock. After obtaining the second native read lock, the second native read lock in the read lock state can be released, and then the target thread is set to the read lock state, and the lock state of the current target thread is recorded through the thread lock state bit (LockPhase), and then the number of read locks of the target thread is counted + 1, that is, the count value of the read lock number counter (RLockNum) is increased by 1, thereby completing the read lock acquisition process of the target thread.
[0109] It can be seen that the embodiment of the present application has made a detailed introduction to the process of requesting a read lock. When acquiring a read lock, first determine whether the current target thread has obtained a read lock. If the target thread has not obtained a read lock, then determine whether the current target thread has obtained a write lock through a write lock number counter. If the target thread has obtained a write lock, then count the number of read locks of the target thread and add them to complete the read lock acquisition process of the target thread; if the target thread has not obtained a write lock, then send a request to obtain a read lock to the first native read-write lock to obtain a first native read lock, and send a request to obtain a read lock to the second native read-write lock to obtain a second native read lock, and release the second native read lock in the read lock state, and then set the target thread to the read lock state, and finally count the number of read locks of the target thread and add them to complete the read lock acquisition process of the target thread. The above method can very simply implement the write priority feature of the read-write lock, while simplifying the upgrade process of the read lock, making up for the shortcomings of the native read-write lock of the operating system, and reducing the efficiency of requesting to obtain a read lock, thereby reducing the throughput of system processing.
[0110] The embodiment of the present application discloses a specific read-write lock reading and writing method, which is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same functions, see Figure 4 As shown in the figure, the function of releasing the read lock can also be achieved through two native read-write locks with the same functions:
[0111] Step S41: Obtain the number of read locks that the current target thread has obtained, and obtain the number of read locks.
[0112] In this embodiment, the number of read locks that the current target thread has obtained may be first obtained through the count value of the read lock number counter (RLockNum) to obtain the corresponding read lock number.
[0113] Step S42: If the number of read locks is equal to 0, it indicates that the read lock release process of the target thread has been completed.
[0114] In this embodiment, if the number of read locks=0, it indicates that the read lock release process of the target thread has been completed.
[0115] Step S43: If the number of read locks is greater than 0, the number of read locks of the target thread is counted down to complete the read lock release process of the target thread.
[0116] In this embodiment, if the read lock number is greater than 0, it indicates that the read lock has been obtained. At this time, the read lock number of the target thread can be counted down (ie, the count value -1) to complete the read lock release process of the target thread.
[0117] Step S44: If the read lock quantity is equal to 1, determine whether the current target thread has obtained a write lock through a write lock quantity counter.
[0118] In this embodiment, if the read lock number=1, it is possible to further determine whether the current target thread has obtained the write lock by using the count value of the write lock number counter (WLockNum).
[0119] Step S45: If the target thread currently obtains the write lock, the number of read locks of the target thread is counted down to complete the read lock release process of the target thread.
[0120] In this embodiment, if the current count value of the write lock number counter (WLockNum) is not 0, it indicates that the target thread has obtained the write lock. At this time, the read lock number of the target thread can be counted -1 to complete the read lock release process of the target thread.
[0121] Step S46: If the current target thread has not obtained the write lock, determine whether the current target thread is in the read lock state through the lock state bit of the current target thread.
[0122] In this embodiment, if the count value of the current write lock number counter (WLockNum) = 0, it indicates that the current target thread has not obtained a write lock. At this time, it can be determined whether the current target thread is in a read lock state according to the lock state bit of the current target thread.
[0123] Step S47: If the target thread is currently in a read lock state, the second native read lock that has been acquired by the target thread is released.
[0124] In this embodiment, if the current target thread is in a read lock state, the second native read lock that has been acquired in the target thread is released.
[0125] Step S48: If the target thread is currently in a write lock state, then the second native write lock that has been acquired by the target thread is released.
[0126] In this embodiment, if the current target thread is in a write lock state, the second native write lock that has been acquired in the target thread is released.
[0127] Step S49: decrement the number of read locks of the target thread to complete the read lock release process of the target thread.
[0128] In this embodiment, after the write lock and the read lock of the target thread are released, the number of read locks of the target thread is counted down, that is, the count value of the read lock number counter (RLockNum) is -1, thereby completing the read lock release process of the target thread.
[0129] It can be seen that the embodiment of the present application provides a detailed introduction to the process of releasing the read lock. By using the combination of two operating system native read-write locks, write lock priority, write lock nesting, read lock upgrade and write lock downgrade are achieved. The method is simple and efficient, which makes up for the shortcomings of the current operating system native read-write lock, and inherits the advantages of high read operation performance and non-blocking of the operating system native read-write lock. Compared with a traditional operating system native read-write lock, the present application supports write lock priority, write lock nesting, read lock upgrade and write lock downgrade. In addition, compared with other read-write locks implemented using mutex locks or critical section technology, the present application can support non-blocking acquisition of read locks. In the scenario of more reads and less writes, the efficiency and throughput are greatly improved.
[0130] Correspondingly, the embodiment of the present application further discloses a read-write lock reading and writing device, which is applied to an operating system, wherein the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same functions, see Figure 5 As shown, the device comprises:
[0131] A first judgment module 11 is used to determine whether the current target thread has obtained a write lock through a write lock quantity counter when a write lock request for the target thread is obtained;
[0132] A first counting self-increasing module 12 is used for counting and self-increasing the number of write locks of the target thread if the target thread obtains the write lock, so as to complete the write lock acquisition process of the target thread;
[0133] A first request sending module 13, configured to send a request to obtain a write lock to the first native read-write lock if the target thread has not obtained a write lock, so as to obtain a first native write lock;
[0134] A second judgment module 14 is used to judge whether the target thread has obtained the read lock through a read lock quantity counter;
[0135] A read lock releasing module 15, configured to release the read lock acquired by the second native read-write lock if the target thread has acquired the read lock;
[0136] A second request sending module 16, configured to send a request to obtain a write lock to the second native read-write lock to obtain a second native write lock;
[0137] The write lock acquisition module 17 is used to release the first native write lock in the write lock state, set the target thread to the write lock state, and then count the number of write locks of the target thread to complete the write lock acquisition process of the target thread.
[0138] Among them, the specific working processes of the above-mentioned modules can refer to the corresponding contents disclosed in the aforementioned embodiments, which will not be repeated here.
[0139] It can be seen that the embodiment of the present application is applied to an operating system, and the operating system includes a first native read-write lock and a second native read-write lock, and the functions of the first native read-write lock and the second native read-write lock are the same, including: when a write lock request for a target thread is obtained, the write lock number counter is used to determine whether the current target thread has obtained the write lock; if the target thread has obtained the write lock, the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread; if the target thread has not obtained the write lock, a write lock acquisition request is sent to the first native read-write lock to obtain the first native write lock; whether the target thread has obtained the read lock is determined by the read lock number counter, and if the target thread has obtained the read lock, the read lock that has been obtained by the second native read-write lock is released; a write lock acquisition request is sent to the second native read-write lock to obtain the second native write lock; the first native write lock in the write lock state is released, and the target thread is set to the write lock state, and the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread. The embodiment of the present application performs read locks and write locks by combining two native read-write locks with the same functions, inherits the advantages of high read operation performance and non-blocking of the native read-write lock of the operating system, realizes write lock priority, write lock nesting, read lock upgrade and write lock downgrade, makes up for the shortcomings of the native read-write lock, can solve the write starvation problem in a read-frequent environment, reduces the difficulty of using read-write locks for multi-threaded software development and programming, reduces the occurrence of deadlocks, and improves the efficiency and throughput of obtaining read locks.
[0140] In some specific embodiments, the read-write lock read-write device may further include:
[0141] A write lock quantity acquisition unit, used to acquire the number of write locks that the current target thread has acquired, to obtain the number of write locks;
[0142] A first write lock releasing unit, configured to indicate that the write lock releasing process of the target thread has been completed if the number of write locks is equal to 0;
[0143] A first counting self-decrementing unit, configured to, if the number of write locks is greater than 1, perform a counting self-decrementing operation on the number of write locks of the target thread to complete a write lock release process of the target thread;
[0144] A target judgment unit, configured to judge whether the current target thread has obtained a read lock by using the read lock number counter if the write lock number is equal to 1;
[0145] A second counting self-decrementing unit is used for, if the target thread currently obtains the read lock, to count and decrement the number of write locks of the target thread to complete the write lock release process of the target thread;
[0146] The second write lock releasing unit is used to release the second native write lock in the write lock state that has been obtained by the target thread if the current target thread has not obtained the read lock, and to count and decrement the number of write locks of the target thread to complete the write lock release process of the target thread.
[0147] In some specific embodiments, the read-write lock read-write device may further include:
[0148] A first judgment unit is used to judge whether the current target thread has obtained the read lock by using the read lock quantity counter when a read lock request for the target thread is obtained;
[0149] A first counting self-increasing unit is used to count and self-increase the number of read locks of the target thread if the target thread has obtained the read lock, so as to complete the read lock acquisition process of the target thread;
[0150] A second judgment unit is used to judge whether the current target thread has obtained the write lock by using the write lock quantity counter if the target thread has not obtained the read lock;
[0151] A second counting self-increasing unit is used for counting and self-increasing the number of read locks of the target thread if the target thread obtains the write lock, so as to complete the read lock acquisition process of the target thread;
[0152] a request sending unit, configured to send a request to obtain a read lock to the first native read-write lock to obtain a first native read lock, and to send a request to obtain a read lock to the second native read-write lock to obtain a second native read lock if the target thread has not obtained a write lock;
[0153] The read lock acquisition unit is used to release the second native read lock in the read lock state, set the target thread to the read lock state, and then count the number of read locks of the target thread to complete the read lock acquisition process of the target thread.
[0154] In some specific embodiments, the read-write lock read-write device may further include:
[0155] A read lock number acquisition unit is used to acquire the number of read locks that the current target thread has acquired, and obtain the number of read locks;
[0156] A first read lock releasing unit, configured to indicate that the read lock releasing process of the target thread has been completed if the number of read locks is equal to 0;
[0157] A third counting self-decrementing unit is used for, if the number of read locks is greater than 0, to perform a counting self-decrementing operation on the number of read locks of the target thread to complete the read lock release process of the target thread;
[0158] A third judgment unit is used to judge whether the current target thread has obtained a write lock through the write lock number counter if the read lock number is equal to 1;
[0159] A fourth counting self-decrementing unit is used for, if the target thread currently obtains the write lock, to count the number of read locks of the target thread to complete the read lock release process of the target thread;
[0160] A fourth judgment unit is used to judge whether the current target thread is in a read lock state according to the lock state bit of the current target thread if the current target thread has not obtained the write lock;
[0161] A second read lock releasing unit, configured to release the second native read lock acquired by the target thread if the target thread is currently in a read lock state;
[0162] a third write lock releasing unit, configured to release the second native write lock that has been acquired by the target thread if the target thread is currently in a write lock state;
[0163] The fifth counting self-decrementing unit is used to count and decrement the number of read locks of the target thread to complete the read lock release process of the target thread.
[0164] In some specific embodiments, the read-write lock read-write device may further include:
[0165] The lock state recording unit is used to record the lock state of the target thread through a preset thread lock state bit.
[0166] Furthermore, the present application also discloses an electronic device. Figure 6 This is a structural diagram of an electronic device 20 according to an exemplary embodiment. The content in the diagram cannot be considered as any limitation on the scope of use of the present application.
[0167] Figure 6A schematic diagram of the structure of an electronic device 20 provided in an embodiment of the present application. The electronic device 20 may specifically include: at least one processor 21, at least one memory 22, a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26. The memory 22 is used to store a computer program, which is loaded and executed by the processor 21 to implement the relevant steps in the read-write lock reading and writing method disclosed in any of the aforementioned embodiments. In addition, the electronic device 20 in this embodiment may specifically be an electronic computer.
[0168] In this embodiment, the power supply 23 is used to provide working voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and the external device, and the communication protocol it follows is any communication protocol that can be applied to the technical solution of the present application, and is not specifically limited here; the input and output interface 25 is used to obtain external input data or output data to the outside world, and its specific interface type can be selected according to specific application needs and is not specifically limited here.
[0169] In addition, the memory 22, as a carrier for storing resources, can be a read-only memory, a random access memory, a disk or an optical disk, etc. The resources stored thereon can include an operating system 221, a computer program 222, etc., and the storage method can be temporary storage or permanent storage.
[0170] The operating system 221 is used to manage and control the hardware devices and computer program 222 on the electronic device 20, which can be Windows Server, Netware, Unix, Linux, etc. In addition to including a computer program that can be used to complete the read-write lock reading and writing method performed by the electronic device 20 disclosed in any of the aforementioned embodiments, the computer program 222 can further include a computer program that can be used to complete other specific tasks.
[0171] Furthermore, the present application also discloses a computer-readable storage medium for storing a computer program; wherein, when the computer program is executed by a processor, the aforementioned disclosed read-write lock reading and writing method is implemented. The specific steps of the method can refer to the corresponding contents disclosed in the aforementioned embodiments, and will not be repeated here.
[0172] In this specification, each embodiment is described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part.
[0173] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0174] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented directly using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0175] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0176] The above is a detailed introduction to a read-write lock reading and writing method, device, equipment and storage medium provided by the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A read-write lock reading and writing method, characterized in that: Applied to an operating system, the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same function, including: When a write lock request for a target thread is obtained, a write lock quantity counter is used to determine whether the current target thread has obtained a write lock; If the target thread has obtained the write lock, the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread; If the target thread has not obtained the write lock, sending a request to obtain the write lock to the first native read-write lock to obtain the first native write lock; Determine whether the target thread has obtained the read lock by using a read lock quantity counter, and if the target thread has obtained the read lock, release the read lock that the second native read-write lock has obtained; Sending a request to obtain a write lock to the second native read-write lock to obtain a second native write lock; The first native write lock in the write lock state is released, and the target thread is set to the write lock state, and then the number of write locks of the target thread is counted and incremented to complete the write lock acquisition process of the target thread.
2. The read-write lock reading and writing method according to claim 1, characterized in that: Also includes: Obtain the number of write locks that the current target thread has obtained, and obtain the number of write locks; If the number of write locks is equal to 0, it indicates that the write lock release process of the target thread has been completed; If the number of write locks is greater than 1, a count-decrement operation is performed on the number of write locks of the target thread to complete the write lock release process of the target thread; If the write lock quantity is equal to 1, determine whether the current target thread has obtained a read lock through the read lock quantity counter; If the target thread currently obtains the read lock, the number of write locks of the target thread is counted down to complete the write lock release process of the target thread; If the current target thread has not obtained a read lock, the second native write lock in the write lock state that has been obtained by the target thread is released, and the number of write locks of the target thread is counted and decremented to complete the write lock release process of the target thread.
3. The read-write lock reading and writing method according to claim 1, characterized in that: Also includes: When a read lock request for the target thread is obtained, determining whether the current target thread has obtained a read lock by using the read lock quantity counter; If the target thread has obtained a read lock, the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread; If the target thread has not obtained the read lock, determining whether the current target thread has obtained the write lock by using the write lock quantity counter; If the target thread obtains the write lock, the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread; If the target thread has not obtained the write lock, a request to obtain a read lock is sent to the first native read-write lock to obtain a first native read lock, and a request to obtain a read lock is sent to the second native read-write lock to obtain a second native read lock; The second native read lock in the read lock state is released, and the target thread is set to the read lock state, and then the number of read locks of the target thread is counted and incremented to complete the read lock acquisition process of the target thread.
4. The read-write lock reading and writing method according to claim 3, characterized in that: Also includes: Obtain the number of read locks that the current target thread has obtained, and obtain the number of read locks; If the number of read locks is equal to 0, it indicates that the read lock release process of the target thread has been completed; If the number of read locks is greater than 0, the number of read locks of the target thread is counted down to complete the read lock release process of the target thread; If the read lock quantity is equal to 1, determine whether the current target thread has obtained a write lock through the write lock quantity counter; If the target thread currently obtains the write lock, the number of read locks of the target thread is counted down to complete the read lock release process of the target thread; If the current target thread has not obtained the write lock, determine whether the current target thread is in the read lock state according to the lock state bit of the current target thread; If the target thread is currently in a read lock state, releasing the second native read lock that has been acquired by the target thread; If the current target thread is in a write lock state, releasing the second native write lock that has been acquired by the target thread; The number of read locks of the target thread is counted and decremented to complete the read lock release process of the target thread.
5. The read-write lock reading and writing method according to any one of claims 1 to 4, characterized in that: Also includes: The lock state of the target thread is recorded via a preset thread lock state bit.
6. A read-write lock reading and writing device, characterized in that: Applied to an operating system, the operating system includes a first native read-write lock and a second native read-write lock, and the first native read-write lock and the second native read-write lock have the same function, including: A first judgment module is used to, when a write lock request for a target thread is obtained, determine whether the current target thread has obtained a write lock through a write lock quantity counter; A first counting self-incrementing module is used for counting and self-incrementing the number of write locks of the target thread if the target thread has obtained the write lock, so as to complete the write lock acquisition process of the target thread; A first request sending module, configured to send a request to obtain a write lock to the first native read-write lock if the target thread has not obtained the write lock, so as to obtain the first native write lock; A second judgment module is used to judge whether the target thread has obtained the read lock through a read lock quantity counter; a read lock releasing module, configured to release the read lock acquired by the second native read-write lock if the target thread has acquired the read lock; A second request sending module, used to send a request to obtain a write lock to the second native read-write lock, to obtain a second native write lock; A write lock acquisition module is used to release the first native write lock in the write lock state, set the target thread to the write lock state, and then count and increment the number of write locks of the target thread to complete the write lock acquisition process of the target thread.
7. The read-write lock device according to claim 6, characterized in that: Also includes: A write lock quantity acquisition unit, used to acquire the number of write locks that the current target thread has acquired, to obtain the number of write locks; A first write lock releasing unit, configured to indicate that the write lock releasing process of the target thread has been completed if the number of write locks is equal to 0; A first counting self-decrementing unit, configured to, if the number of write locks is greater than 1, perform a counting self-decrementing operation on the number of write locks of the target thread to complete a write lock release process of the target thread; A target judgment unit, configured to judge whether the current target thread has obtained a read lock by using the read lock number counter if the write lock number is equal to 1; A second counting self-decrementing unit is used for, if the target thread currently obtains the read lock, to count and decrement the number of write locks of the target thread to complete the write lock release process of the target thread; The second write lock releasing unit is used to release the second native write lock in the write lock state that has been obtained by the target thread if the current target thread has not obtained the read lock, and to count and decrement the number of write locks of the target thread to complete the write lock release process of the target thread.
8. The read-write lock device according to claim 6, characterized in that: Also includes: The lock state recording unit is used to record the lock state of the target thread through a preset thread lock state bit.
9. An electronic device, characterized in that: It comprises a processor and a memory; wherein, when the processor executes the computer program stored in the memory, the read-write lock reading and writing method as claimed in any one of claims 1 to 5 is implemented.
10. A computer-readable storage medium, characterized in that: Used to store computer programs; wherein, when the computer program is executed by a processor, the read-write lock reading and writing method according to any one of claims 1 to 5 is implemented.
Citation Information
Cited By
Realization method of read-write lock capable of configuring read-write priority strategy of MMU-free embedded real-time operating system
CN122346366A
