CN112416556B - Data read-write priority balancing method, system, device and storage medium - Google Patents

Data read-write priority balancing method, system, device and storage medium Download PDF

Info

Publication number
CN112416556B
CN112416556B CN202011325771.XA CN202011325771A CN112416556B CN 112416556 B CN112416556 B CN 112416556B CN 202011325771 A CN202011325771 A CN 202011325771A CN 112416556 B CN112416556 B CN 112416556B
Authority
CN
China
Prior art keywords
lock
read
write
priority
counter
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202011325771.XA
Other languages
Chinese (zh)
Other versions
CN112416556A (en
Inventor
何清
王毅
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Xian TPRI Power Station Information Technology Co Ltd
Original Assignee
Xian TPRI Power Station Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Xian TPRI Power Station Information Technology Co Ltd filed Critical Xian TPRI Power Station Information Technology Co Ltd
Priority to CN202011325771.XA priority Critical patent/CN112416556B/en
Publication of CN112416556A publication Critical patent/CN112416556A/en
Application granted granted Critical
Publication of CN112416556B publication Critical patent/CN112416556B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/48Program initiating; Program switching, e.g. by interrupt
    • G06F9/4806Task transfer initiation or dispatching
    • G06F9/4843Task transfer initiation or dispatching by program, e.g. task dispatcher, supervisor, operating system
    • G06F9/4881Scheduling strategies for dispatcher, e.g. round robin, multi-level priority queues
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/526Mutual exclusion algorithms

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The application discloses a data read-write priority balancing method, a system, a device and a storage medium, wherein the method comprises the following steps: locking the data read-write request through the mutual exclusion lock; judging a read-write priority type mark of the current data according to a reference counter of the current data, wherein the read-write priority type mark comprises no read-write priority, read priority and write priority; judging the occupation condition of the read-write lock according to the reference counter of the corresponding variable of the read-write priority type mark, wherein the occupation condition comprises the occupation of the read lock, the exclusive of the write lock and the absence of any lock; obtaining a read-write priority type mark of the data according to the occupation condition of the read-write lock and the maximum allowable number counter residual value of the read-write lock; unlocking for the read-write operation of the request data through the mutual exclusion lock. The method of the application improves the concurrency performance of data reading operation, can ensure that the system runs stably for a long time, has excellent data concurrency access performance, and obtains good economic effect in the application of server software products.

Description

Data read-write priority balancing method, system, device and storage medium
Technical Field
The application belongs to the technical field of data read-write methods in server application scenes, and particularly relates to a data read-write priority balancing method, a system, a device and a storage medium.
Background
Read-write locks are typically used to improve concurrency performance of reads when multi-threaded secure access protection is performed on a resource that allows multiple threads to concurrently read, but only allows one thread to write exclusively. However, the currently widely used read-write locks can be divided into read-priority read-write locks and write-priority read-write locks according to different read-write priorities no matter which implementation technology is adopted. Write-priority locks are suitable for use when the protected resource is a read concurrency is relatively large, and read-priority locks are suitable for use when the protected resource is a write concurrency is relatively large. The main purpose of the selection is to avoid the problem of read-write starvation of the read-write lock, and when the protected resource is read with a relatively large concurrency, if the read priority is used, the deadlock of the write operation is caused by the starvation of the write operation; when the protected resource is a relatively large write concurrency, if the write priority is used, the read operation is blocked due to starvation of the read operation. In some server systems, there is another more complex application scenario, where sometimes the read concurrency is far greater than the write concurrency, and sometimes the write concurrency is far greater than the read concurrency, and the probability and timing of occurrence of the read concurrency are unpredictable, where in this case, whether using the write priority lock or the read priority lock, the service process deadlock may occur unexpectedly.
At present, aiming at the condition that the read-write concurrency is uncertain, the read concurrency is far greater than the write concurrency at times, and the write concurrency is far greater than the read concurrency at times, and in a server application scene that the occurrence probability and the opportunity of the read concurrency cannot be predicted, a mutual exclusion lock is generally used for providing the safe access of a resource concurrency thread, so that the condition that the service process is deadlocked due to the occurrence of read-write starvation of the read-write lock is effectively avoided.
However, the mutual exclusion lock is mutually exclusive to all read-write operations, so that the concurrency of the read operations can not be fully utilized to improve the read performance of the system when the read concurrency is high, and the read concurrency performance of the system can be greatly reduced when the high concurrency server system reads.
Disclosure of Invention
In order to solve the problem of reduced concurrent performance of system reading operation in the prior art, the application provides a data reading and writing priority balancing method, a system, a device and a storage medium.
In order to achieve the above purpose, the application adopts the following technical means:
a data read-write priority balancing method comprises the following steps:
concurrent secure access locking of data read-write priority type flag variable, counter variable and read/write conditional lock by mutex lock;
judging a read-write priority type mark of the data according to the read-write reference counter of the data, wherein the read-write priority type mark comprises no read-write priority, read priority and write priority;
judging the read-write occupation condition of the data according to the read-write reference counter of the data, wherein the read-write occupation condition comprises read lock occupation, write lock exclusive occupation and no lock occupation;
obtaining a read-write priority type mark of the data according to the read-write waiting condition of the data and the maximum allowable number counter residual value;
unlocking for the read-write operation of the request data through the mutual exclusion lock.
As a further refinement of the present application, the request data read-write operation includes an apply read-lock request, an attempt to apply read-lock request, a release read-lock request, an apply write-lock request, an attempt to apply write-lock request, and a release write-lock request.
As a further improvement of the present application, when a user applies for a read/write lock, the method comprises the steps of:
a1. locking the mutual exclusion lock;
b1. checking whether the write/read lock waiting counter is zero, if so, setting a priority type mark as read/write priority, and allowing a read/write lock application;
c1. checking a lock state condition, if the lock reference counter is smaller than 0 or the priority type mark is that the writing/reading priority condition is met, executing a reading/writing waiting condition lock waiting operation, waiting for a condition signal allowing reading/writing, and jumping to the step b1 for circular execution when the signal is triggered; if the lock reference counter is smaller than 0 or the priority type mark is that the writing/reading priority condition is not satisfied, the application of the reading/writing lock is successful, and the flow jumps to the step d 1;
d1. when the application is successfully read, the lock reference counter is increased by 1, and when the application is successfully written, the lock reference counter is set to-1;
e1. if the maximum allowable number counter of the read/write lock is greater than 0, the maximum allowable number counter of the read/write lock is decremented by 1;
f1. if the maximum allowable number counter of the read/write lock is equal to 0, the priority type flag S109 is set to write/read priority at this time;
g1. and unlocking the mutual exclusion lock to finish the application of the read/write lock.
As a further improvement of the present application, when a user tries to apply for a read/write lock, the steps are included as follows:
a2. defining a read/write lock application success flag variable, and setting the variable as 1 to represent that the initial value is that the attempt to apply the read/write lock is successful;
b2. locking the mutual exclusion lock;
c2. checking the lock state, if the lock reference counter is smaller than 0, or the priority type mark is write/read priority, setting the read/write lock application success mark variable to 0, indicating that the attempt to apply for the read/write lock fails, and jumping to the step g 2;
d2. if the condition of the step c2 is not satisfied, the attempt to apply for the read/write lock is successful, and the lock reference counter is increased by 1;
e2. if the maximum allowable number counter of the read/write lock is greater than 0, the maximum allowable number counter of the read/write lock is decremented by 1;
f2. if the maximum allowable number counter of the read/write lock is equal to 0, the priority type mark is set to be write/read priority at the moment;
g2. the mutual exclusion lock S101 is unlocked;
h2. a flag is returned as to whether the read/write lock application was successful.
5. The data read/write priority balancing method according to claim 1, comprising the steps of, when a user releases a read/write lock:
a3. locking the mutual exclusion lock;
b3. the lock reference counter is decremented by 1 when the read is released, and the lock reference counter is set to 0 when the write is released;
c3. checking that if the maximum allowable number counter of the read/write lock is 0 and the write/read lock waiting counter is greater than 0, the executing condition of the step is met, checking that if the lock reference counter is 0, setting a priority type mark as write/read priority, setting the maximum allowable number counter of the write/read lock as the value of the write/read lock waiting counter, indicating that the write/read operation of the maximum allowable number counter value number of the write/read lock can be triggered by the maximum allowable number of the write/read priority at this time is performed, turning over the priority level, and then sending a trigger signal to the write/read waiting condition lock;
d3. if the condition of the step c3 is not satisfied, checking whether a read/write lock waiting counter is larger than 0, if so, setting a priority type mark as read/write priority, and initiating a broadcast signal to the read/write waiting condition lock to allow all read/write lock applications;
e3. if the condition of the step d is not satisfied, the condition indicates that no read/write lock and no write/read lock wait currently, a priority type mark is set to be no priority, and a read/write lock application is waited;
f3. the mutual exclusive lock is unlocked, and the read/write lock is released.
As a further improvement of the application, when the read-write lock is created, a mutual exclusion lock, a read-wait condition lock and a write-wait condition lock object are created at the same time, and a read-lock wait counter, a read-lock maximum allowable number counter, a write-lock wait counter, a write-lock maximum allowable number counter, a lock reference counter and a priority type mark are all set to 0, so that the initial state of the read-write lock is a read-write priority-free, read-write wait-free and read-write lock occupation-free state;
when the read-write lock is destroyed, releasing the mutual exclusive lock, the read-wait condition lock and the write-wait condition lock object, wherein the read-lock wait counter, the read-lock maximum allowable number counter, the write-lock wait counter, the write-lock maximum allowable number counter, the lock reference counter and the priority type mark are all set to 0;
a data read-write priority balancing system, comprising:
the mutual exclusion lock unit is used for locking or unlocking the related variable of concurrent security access of the data read-write request through the mutual exclusion lock;
the priority type mark unit is used for judging the read-write priority type mark of the data according to the read-write reference counter of the data, and comprises no read-write priority, read-write priority and write priority;
the reference counter unit is used for judging the read-write occupation condition of the data according to the read-write reference counter of the data, and comprises read lock occupation, write lock exclusive and no lock occupation;
and the lock control unit is used for obtaining the read-write priority type mark of the data according to the read-write waiting condition of the data and the maximum allowable number counter residual value.
The lock control unit includes:
a waiting condition lock unit for waiting for an allowable condition signal according to the priority type flag;
a waiting counter unit for judging the priority type mark of the current variable;
and the maximum allowable number counter unit is used for judging the maximum allowable number of the priority type mark of the current variable.
A data read-write priority balancing apparatus, the priority balancing apparatus comprising:
at least one processor;
at least one memory for storing at least one program;
the at least one program, when executed by the at least one processor, causes the at least one processor to implement the one data read-write priority balancing method.
A storage medium having stored therein processor-executable instructions which, when executed by a processor, are for implementing the one data read-write priority balancing method.
Compared with the prior art, the application has the following advantages:
the application introduces the read-write priority type mark and the maximum allowable number counter variable of the read/write lock on the basis of the common read-write lock, realizes the dynamic balance scheduling of the read-write priority by comprehensively considering the read-write occupation condition and the read-write waiting condition of the current read-write lock, solves the problem that the common read-write lock is easy to cause deadlock because of read-write starvation under the scene of large read-write concurrency random fluctuation, expands the application range of the read-write lock even if the read-write concurrency is uncertain, the read concurrency is far greater than the write concurrency at times and the write concurrency is far greater than the read concurrency at times, and can improve the read concurrency performance of the service in the server application scene that the probability and the opportunity of occurrence are unpredictable.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the drawings required for the embodiments will be briefly described below, and the drawings are incorporated into and constitute a part of the specification, which illustrate embodiments consistent with the present disclosure and together with the description serve to explain the technical solutions of the present disclosure. It is to be understood that the following drawings illustrate only certain embodiments of the present disclosure and are therefore not to be considered limiting of its scope, for the person of ordinary skill in the art may admit to other equally relevant drawings without inventive effort.
FIG. 1 is a diagram showing a read-write priority balance lock structure;
FIG. 2 is a flow chart of an execution process when a read lock is applied;
FIG. 3 is a flow chart of an execution process when attempting to apply for a read lock;
FIG. 4 is a flow chart of an execution process when releasing a read lock;
FIG. 5 is a flow chart of an execution process when a write lock is applied;
FIG. 6 is a flowchart of an execution process when attempting to apply for a write lock;
FIG. 7 is a flowchart of an execution process when a write lock is released;
FIG. 8 is an exemplary diagram of an implementation interface of a read-write priority balanced lock;
FIG. 9 is a diagram of an example of a read-write priority balanced lock.
Detailed Description
In order to make the objects, features and advantages of the present application more comprehensible, the technical solutions according to the embodiments of the present application will be clearly described in the following with reference to the accompanying drawings, and it is obvious that the described embodiments are only some embodiments of the present application, not all embodiments. All other embodiments, which can be made by those skilled in the art based on the embodiments of the application without making any inventive effort, are intended to be within the scope of the application.
In the description of the present specification, a description referring to terms "one embodiment," "some embodiments," "examples," "specific examples," or "some examples," etc., means that a particular feature, structure, material, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the present application. Furthermore, the particular features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. Furthermore, the different embodiments or examples described in this specification and the features of the different embodiments or examples may be combined and combined by those skilled in the art without contradiction.
Furthermore, the terms "first," "second," and the like, are used for descriptive purposes only and are not to be construed as indicating or implying a relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defining "a first" or "a second" may explicitly or implicitly include at least one such feature. In the description of the present application, the meaning of "a plurality" is two or more, unless explicitly defined otherwise.
The application discloses a data read-write priority balancing method, which comprises the following steps:
concurrent secure access locking of data read-write priority type flag variable, counter variable and read/write conditional lock by mutex lock; the method comprises the steps of carrying out a first treatment on the surface of the
Judging a read-write priority type mark of the data according to the read-write reference counter of the data, wherein the read-write priority type mark comprises no read-write priority, read priority and write priority;
judging the read-write occupation condition of the data according to the read-write reference counter of the data, wherein the read-write occupation condition comprises read lock occupation, write lock exclusive occupation and no lock occupation;
obtaining a read-write priority type mark of the data according to the read-write waiting condition of the data and the maximum allowable number counter residual value;
unlocking for the read-write operation of the request data through the mutual exclusion lock.
The read-write priority balance lock structure of the application is designed as follows:
1) A mutex lock (see mutex lock S101 in fig. 1) providing thread security for read wait condition locks and write wait condition locks and other variable accesses;
2) A read wait condition lock (see read wait condition lock S102 in fig. 1), a read lock wait counter (see read lock wait counter S103 in fig. 1), and a read lock maximum allowable number counter (see read lock maximum allowable number counter S104 in fig. 1) constitute a read lock control unit;
3) A write wait condition lock (see write wait condition lock S105 in fig. 1), a write lock wait counter (see write lock wait counter S106 in fig. 1), and a write lock maximum allowable number counter (see write lock maximum allowable number counter S107 in fig. 1) constitute a write lock control unit;
4) A lock reference counter (see lock reference counter S108 in fig. 1), which when the counter value is greater than 0, indicates how many read locks are occupied, -1 indicates write lock exclusive, and 0 indicates no lock occupancy;
5) A priority type flag (see priority type flag S109 in fig. 1), 0 indicates no read-write priority, 1 indicates read priority, 2 indicates write priority, and the read-write priority balance lock controls the priority of the lock by the variable, thereby realizing dynamic priority balance scheduling.
Based on the read-write lock structure, the application outputs 8 interfaces to the outside, and adopts the following technical scheme to realize the read-write priority balance lock:
1) When the read-write lock is created, the mutual exclusion lock S101, the read waiting condition lock S102 and the write waiting condition lock S105 are simultaneously created, and the read lock waiting counter S103, the read lock maximum allowable number counter S104, the write lock waiting counter S106, the write lock maximum allowable number counter S107, the lock reference counter S108 and the priority type mark S109 are all set to 0, so that the initial state of the read-write lock is the read-write priority-free, read-write waiting-free and read-write lock occupation-free state.
2) When the read-write lock is destroyed, releasing the object examples of the mutual exclusion lock S101, the read waiting condition lock S102 and the write waiting condition lock S105, and setting the read lock waiting counter S103, the read lock maximum allowable number counter S104, the write lock waiting counter S106, the write lock maximum allowable number counter S107, the lock reference counter S108 and the priority type mark S109 to 0;
3) When a user applies for reading a lock, the following processing flow is executed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. checking whether the write lock waiting counter S106 is zero, if so, setting the priority type flag S109 as read priority, and allowing the read lock application;
c. checking a lock state condition, if the lock reference counter S108 is smaller than 0 or the priority type mark S109 is that the write priority condition is met, executing a read waiting condition lock S102 to wait for operation, waiting for a condition signal allowing reading, and jumping to the step b for circular execution when the signal is triggered; if the lock reference counter S108 is smaller than 0 or the priority type mark S109 is that the writing priority condition is not satisfied, the lock is applied to be successfully read, and the flow jumps to the step d;
d. when a read lock is successfully applied, the lock reference counter S108 is incremented by 1;
e. if the maximum allowable number of read locks counter S104 is greater than 0, the maximum allowable number of read locks counter S104 is decremented by 1;
f. if the maximum allowable number of read locks counter S104 is equal to 0, then the priority type flag S109 is set to write priority;
g. the mutual exclusion lock S101 is unlocked, and the read lock application is completed.
4) When a user tries to apply for a read lock, the following process flow is performed:
a. defining a read lock application success flag variable, setting the read lock application success flag variable as 1, and indicating that the initial value is the success of attempting to apply for the read lock;
b. the mutual exclusion lock is locked for operation protection;
c. checking the lock state, if the lock reference counter S108 is smaller than 0, or the priority type mark S109 is write priority, setting the read lock application success mark variable to 0, indicating that the attempt to apply the read lock fails, and jumping to the step g;
d. if the condition of the step c is not satisfied, the attempt to apply for the read lock is successful, and the lock reference counter S108 is increased by 1;
e. if the maximum allowable number of read locks counter S104 is greater than 0, the maximum allowable number of read locks counter S104 is decremented by 1;
f. if the maximum allowable number of read locks counter S104 is equal to 0, then the priority type flag S109 is set to write priority;
g. the mutual exclusion lock S101 is unlocked;
h. a flag is returned as to whether the read lock application was successful.
5) When the user releases the read lock, the following process flow is executed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. the lock reference counter S108 is decremented by 1;
c. checking that if the maximum allowable number counter S104 of the read lock is 0 and the waiting counter S106 of the write lock is greater than 0, the execution condition of the step is satisfied, checking that if the reference counter S108 of the lock is 0, setting the priority type flag S109 to write priority, setting the maximum allowable number counter S107 of the write lock to the value of the waiting counter S106 of the write lock, indicating that the current write priority can trigger the writing operation of the number of times of the maximum allowable number counter S107 of the write lock, turning over the priority level, and sending a trigger signal to the waiting condition lock S105;
d. if the condition of the step c is not satisfied, checking whether the read lock waiting counter S103 is larger than 0, if so, setting the priority type mark S109 as the read priority, and initiating a broadcast signal to the read waiting condition lock S102 to allow all read lock applications;
e. if the condition of the step d is not satisfied, the condition indicates that no read lock and no write lock wait currently, a priority type mark S109 is set to be no priority, and the read lock and the write lock are waited for application;
f. the mutual exclusion lock S101 is unlocked, and the read lock is released.
6) When a user applies for a write lock, the following processing flow is executed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. checking whether the read lock wait counter S103 is zero, if so, setting the priority type flag S109 as write priority, and allowing write lock application;
c. checking a lock state condition, if the lock reference counter S108 is not 0 or the priority type mark S109 is read and the priority condition is met, executing a waiting operation of the waiting condition lock S105, waiting for a condition signal allowing writing, and jumping to the step b for executing circularly when the signal is triggered; if the lock reference counter S108 is not 0 or the read priority condition of the priority type mark S109 is not satisfied, the process jumps to the step d;
d. when a write lock is successfully applied, the lock reference counter S108 is set to-1;
e. if the write lock maximum allowable number counter S107 is greater than 0, the write lock maximum allowable number counter S107 is decremented by 1;
f. if the write lock maximum allowable number counter S107 is equal to 0, then the priority type flag S109 is set to read priority;
a. the mutual exclusion lock S101 is unlocked, and the read lock application is completed.
7) When a user tries to apply for a write lock, a process flow is performed:
a. defining a write lock application success flag variable, setting the write lock application success flag variable as 1, and indicating that the initial value is the success of attempting to apply for the write lock;
b. the mutual exclusion lock S101 is locked for operation protection;
c. checking the lock state, if the lock reference counter S108 is not 0 or the priority type flag S109 is read priority, setting the write lock application success flag variable to 0, indicating that the attempt to apply for the write lock fails, and jumping to the step g;
d. if the condition of the step c is not satisfied, the attempt to apply for the write lock is successful, and the lock reference counter S108 is set to-1;
e. if the write lock maximum allowable number counter S107 is greater than 0, the write lock maximum allowable number counter S107 is decremented by 1;
f. if the write lock maximum allowable number counter S107 is equal to 0, then the priority type flag S109 is set to read priority;
g. the mutual exclusion lock S101 is unlocked;
h. returning a sign of whether the read lock application is successful;
8) When the user releases the write lock, the following process flow is performed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. lock reference counter S108 is set to 0;
c. checking that if the maximum allowable number counter S107 of the write lock is 0 and the execution condition of the step is established when the read lock waiting counter S103 is greater than 0, setting the priority type flag S109 as read priority, setting the maximum allowable number counter S104 of the read lock as the value of the read lock waiting counter S103, indicating that the read priority is the maximum and triggerable number counter S104 of the read lock is the maximum, turning over the priority level after the read operation for the number of times, and then initiating a broadcast signal to the read waiting condition lock S102;
d. if the condition of the step c is not satisfied, checking whether the write lock waiting counter S106 is larger than 0, if so, setting the priority type mark S109 as write priority, and initiating a trigger signal to the write waiting condition lock S105 to allow the write lock application;
e. if the condition of the step d is not satisfied, the condition indicates that no read lock and no write lock wait currently, a priority type mark S109 is set to be no priority, and the read lock and the write lock are waited for application;
f. the mutual exclusion lock S101 is unlocked, and the read lock is released.
The application is described in further detail below with reference to the detailed description and the accompanying drawings.
The application relates to a data read-write priority balancing system, which comprises:
the exclusive lock unit is used for locking concurrent security access of a priority type mark variable, a counter variable and a read/write condition lock for data read-write through the exclusive lock;
the priority type mark unit judges the read-write priority type mark of the data according to the read-write reference counter of the data, and comprises no read-write priority, read-write priority and write priority;
the reference counter unit judges the read-write occupation condition of the data according to the read-write reference counter of the data, wherein the read-write occupation condition comprises read lock occupation, write lock monopolization and no lock occupation;
and the lock control unit obtains a read-write priority type mark of the data according to the read-write waiting condition of the data and the maximum allowable number counter residual value.
The lock control unit includes:
a waiting condition lock unit for waiting for an allowable condition signal according to the priority type flag;
the waiting counter unit is used for judging a priority type mark of current data reading and writing;
and the maximum allowable number counter unit is used for judging the maximum allowable number of the priority type mark of the current data reading and writing.
In one possible implementation of the present application, the raw read-write priority balance lock module 800 outputs the following 8 interfaces (see fig. 8):
1) The interface 801 creates a module 800 instance, and creates an object instance of the mutex lock S101, the read wait condition lock S102 and the write wait condition lock S105, where the read lock wait counter S103, the read lock maximum allowable number counter S104, the write lock wait counter S106, the write lock maximum allowable number counter S107, the lock reference counter S108 and the priority type flag S109 are all set to 0, which indicates that the initial state of the read/write lock is no read/write priority, no read/write wait, and no read/write lock occupation state.
2) The interface 802 destroys one module 800 instance and releases the exclusive lock S101, the read wait condition lock S102 and the write wait condition lock S105 object instance, the read lock wait counter S103, the read lock maximum allowable number counter S104, the write lock wait counter S106, the write lock maximum allowable number counter S107, the lock reference counter S108, and the priority type flag S109 are all set to 0;
3) An interface 803, implementing an application read lock operation for the user according to the execution flow of fig. 2;
4) An interface 804, according to the execution flow of fig. 3, implements an attempt to apply for a read lock operation for the user;
5) Interface 805, implementing a release read lock operation for the user according to the execution flow of fig. 4;
6) Interface 806, implementing a write lock application operation for the user according to the execution flow of fig. 5;
7) An interface 807 for implementing an attempt to apply for a write lock operation for the user in accordance with the execution flow of FIG. 6;
8) Interface 808, implementing a write lock release operation for the user according to the execution flow of FIG. 7;
the read-write priority balance lock is developed based on the C language, and outputs a.h head file and a.c source code file to the outside, so that the read-write priority balance lock is very convenient to be referenced by other system modules, and can be called by other programming languages if the read-write priority balance lock is subjected to dynamic library encapsulation. Meanwhile, the read-write lock supports different operating systems, realizes cross-platform application, can work under Windows, linux and other Unix-like platforms, can also be applied to domestic operating systems, and has wide application prospects.
The application is applied to a plurality of modules of the Rui Teng real-time database system, and obtains good read concurrency performance improvement.
In one specific application example, read-write concurrent multithreading access protection is required for the historical cache data block of each measuring point in the Rui Teng real-time database system. In the Rui Teng real-time database system, a history cache data block of a measuring point is an intermediate layer data cache region of snapshot data and history archiving data of the measuring point, the snapshot data is firstly stored in the history cache data block after being archived, and when the history cache data block is full, the history cache data block is archived and written into a disk file, so that the archiving performance of the Rui Teng real-time database system is improved by utilizing the high-speed reading and writing performance of a memory, and the latest section of hot data of the measuring point has high-speed reading performance.
The historical cache data block of the measuring point obviously meets the application condition of a read-write lock, allows a plurality of read operations to be concurrent at the same time, and only allows an exclusive write operation. It also has a special concurrency characteristic as shown in fig. 9: sometimes, this measurement point is operated by a high concurrency execution 902, which allows concurrent execution after applying 901 a read lock of a lock instance, and sometimes, this measurement point is operated by exclusive execution of a write operation after applying 901 a write lock of a lock instance because 903 and 904 operations need to be applied, the peak values of the read and write operations are dynamically variable and unpredictable, and sometimes fluctuate very much.
At the beginning of the design of the Rui Teng real-time database system, a conservative mutual exclusion lock is adopted for data read-write access protection, the system can work well, but when a plurality of read operations are concurrent at the same time, the mutual exclusion lock cannot realize read concurrency, so that when the read concurrency is more, the performance of the read operation is poorer. The read-write lock pthread_rwlock_t of pthread is adopted to replace a mutual exclusion lock later, the read concurrency performance of a test result is obviously improved, all the test results are quite perfect, but the database is sometimes and delicately deadlocked in the long-time high-strength pressure test process, the database is unresponsive, and even if the read-write priority of the read-write lock is switched by using the pthread_rwlockattr_setkined_np () function, the deadlock cannot be avoided.
Therefore, the technical scheme of the application is provided, and the read-write priority of the lock can be dynamically adjusted according to read-write concurrency to perform read-write priority balance scheduling. Since the Rui Teng real-time database system uses the read-write priority balance lock, the read concurrency performance of the history cache data block is equivalent to the test performance of the PThread_rwlock_t, but the deadlock condition never occurs after long-time high-strength test.
At present, the Rui Teng real-time database system applying the technical scheme of the application is already deployed and operated in SIS projects of a plurality of power plants, and is used for storing and accessing mass production data of the whole plant, the system is stable in long-term operation, high in data concurrency and excellent in access performance, and good economic effect is obtained.
The embodiment of the application provides a data read-write priority balancing device. The device can be an electronic device such as a smart phone, a tablet computer and the like capable of running application programs, or a server. The application relates to a data read-write priority balancing device, which comprises:
at least one processor;
at least one memory for storing at least one program;
the at least one program, when executed by the at least one processor, causes the at least one processor to implement the one data read-write priority balancing method.
The processor may include one or more processing cores. The processor uses various interfaces and lines to connect various portions of the overall electronic device, perform various functions of the electronic device, and process data by executing or executing instructions, programs, code sets, or instruction sets stored in memory, and invoking data stored in memory. Alternatively, the processor may be implemented in at least one hardware form of digital signal processing (Digital Signal Processing, DSP), field-Programmable gate array (FPGA), programmable Logic Array (PLA). The processor may integrate one or a combination of several of a central processing unit (CentralProcessing Unit, CPU), an image processor (Graphics Processing Unit, GPU), and a modem etc. The CPU mainly processes an operating system, a user interface, an application program and the like; the GPU is used for being responsible for rendering and drawing of display content; the modem is used to handle wireless communications. It will be appreciated that the modem may not be integrated into the processor and may be implemented solely by a single communication chip.
The Memory may include random access Memory (Random Access Memory, RAM) or Read-Only Memory (rom). The memory may be used to store instructions, programs, code sets, or instruction sets. The memory may include a stored program area and a stored data area, wherein the stored program area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playing function, an image playing function, etc.), instructions for implementing the various method embodiments described below, etc. The storage data area may also store data created by the electronic device in use (e.g., phonebook, audio-video data, chat-record data), etc.
The embodiment of the application provides a structure of a computer readable storage medium. In which processor-executable instructions are stored which, when executed by a processor, are adapted to carry out the one data read-write priority balancing method.
The computer readable storage medium may be an electronic memory such as a flash memory, an EEPROM (electrically erasable programmable read only memory), an EPROM, a hard disk, or a ROM. Optionally, the computer readable storage medium comprises a non-transitory computer readable medium (non-transitory computer-readable storage medium). The computer readable storage medium has storage space for program code to perform any of the method steps described above. The program code can be read from or written to one or more computer program products.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present application, and are not limiting; although the application has been described in detail with reference to the foregoing embodiments, it will be appreciated by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not drive the essence of the corresponding technical solutions to depart from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims (6)

1. A data read-write priority balancing method, comprising:
concurrent secure access locking of data read-write priority type flag variable, counter variable and read/write conditional lock by mutex lock;
judging a read-write priority type mark of the data according to the read-write reference counter of the data, wherein the read-write priority type mark comprises no read-write priority, read priority and write priority;
judging the read-write occupation condition of the data according to the read-write reference counter of the data, wherein the read-write occupation condition comprises read lock occupation, write lock exclusive occupation and no lock occupation;
obtaining a read-write priority type mark of the data according to the read-write waiting condition of the data and the maximum allowable number counter residual value;
unlocking for the read-write operation of the request data through the mutual exclusion lock;
the request data read-write operation comprises a request for applying a read lock, a request for attempting to apply the read lock, a request for releasing the read lock, a request for applying a write lock, a request for attempting to apply the write lock and a request for releasing the write lock;
when a user applies for a read lock, the method comprises the following steps:
a1. locking the mutual exclusion lock;
b1. checking whether a write lock waiting counter is zero, if so, setting a priority type mark as a reading priority, and allowing a read lock application;
c1. checking a lock state condition, if the lock reference counter is smaller than 0 or the priority type mark is that a write priority condition is met, executing a read waiting condition lock waiting operation, waiting for a condition signal allowing reading, and jumping to b1 for circularly executing when the signal is triggered; if the lock reference counter is smaller than 0 or the priority type mark is that the writing priority condition is not satisfied, the lock is applied to be successfully read, and the flow jumps to the step d 1;
d1. when a lock is successfully applied for reading, the lock reference counter is increased by 1;
e1. if the maximum allowable number counter of the read lock is greater than 0, the maximum allowable number counter of the read lock is decremented by 1;
f1. if the maximum allowable number counter of the read lock is equal to 0, the priority type mark is set to be write priority at the moment;
g1. unlocking the mutual exclusion lock to finish the read lock application;
when a user applies for a write lock, the method comprises the following steps:
a1. locking the mutual exclusion lock;
b1. checking whether a read lock waiting counter is zero, if so, setting a priority type mark as write priority, and allowing write lock application;
c1. checking a lock state condition, if the lock reference counter is smaller than 0 or the priority type mark is that the read priority condition is met, executing a write waiting condition lock waiting operation, waiting for a condition signal allowing writing, and jumping to the step b1 for executing circularly when the signal is triggered; if the lock reference counter is smaller than 0 or the priority type mark is that the read priority condition is not satisfied, the writing of the lock is successfully applied, and the flow jumps to the step d 1;
d1. when a write lock is successfully applied, the lock reference counter is set to-1;
e1. if the maximum allowable number counter of the write lock is greater than 0, the maximum allowable number counter of the write lock is decremented by 1;
f1. if the maximum allowable number counter of the write lock is equal to 0, the priority type mark is set to be read priority at the moment;
g1. unlocking the mutual exclusive lock to finish the write lock application;
when a user tries to apply for a read lock, the method comprises the following steps:
a2. defining a read lock application success flag variable, setting the read lock application success flag variable as 1, and indicating that the initial value is the success of attempting to apply for the read lock;
b2. locking the mutual exclusion lock;
c2. checking the lock state, if the lock reference counter is smaller than 0, or the priority type mark is write priority, setting the read lock application success mark variable to 0, indicating that the attempt to apply for the read lock fails, and jumping to the step g 2;
d2. if the condition of the step c2 is not satisfied, the attempt to apply for the read lock is successful, and the lock reference counter is increased by 1;
e2. if the maximum allowable number counter of the read lock is greater than 0, the maximum allowable number counter of the read lock is decremented by 1;
f2. if the maximum allowable number counter of the read lock is equal to 0, the priority type mark is set to be write priority at the moment;
g2. unlocking the mutual exclusion lock;
h2. returning a sign of whether the read lock application is successful;
when a user tries to apply for a write lock, the method comprises the following steps:
a2. defining a write lock application success flag variable, setting the write lock application success flag variable as 1, and indicating that the initial value is the success of attempting to apply for the write lock;
b2. locking the mutual exclusion lock;
c2. checking the lock state, if the lock reference counter is smaller than 0, or the priority type mark is read priority, setting the write lock application success mark variable to 0, indicating that the attempt to apply for the write lock fails, and jumping to the step g 2;
d2. if the condition of the step c2 is not satisfied, the attempt to apply for the write lock is successful, and the lock reference counter is increased by 1;
e2. if the maximum allowable number counter of the write lock is greater than 0, the maximum allowable number counter of the write lock is decremented by 1;
f2. if the maximum allowable number counter of the write lock is equal to 0, the priority type mark is set to be read priority at the moment;
g2. unlocking the mutual exclusion lock;
h2. returning a mark whether the write lock application is successful or not;
when the user releases the read lock, the method comprises the following steps:
a3. locking the mutual exclusion lock;
b3. the lock reference counter is decremented by 1 when the read lock is released;
c3. checking that if the maximum allowable number counter of the read lock is 0 and the waiting counter of the write lock is greater than 0, the executing condition of the step is met, checking that if the reference counter of the lock is 0, setting a priority type mark as write priority, setting the maximum allowable number counter of the write lock as the value of the waiting counter of the write lock, indicating that the write priority is the maximum and triggerable number of the maximum allowable number counter of the write lock at this time, turning over the priority level, and then locking a trigger signal to the waiting condition of the write;
d3. if the condition in the step c3 is not satisfied, checking whether a read lock waiting counter is larger than 0, if so, setting a priority type mark as read priority, and initiating a broadcast signal to the read waiting condition lock to allow all read lock applications;
e3. if the d3 step condition is not satisfied, the condition indicates that no read lock and no write lock wait currently, a priority type mark is set to be no priority, and a read-write lock application is waited;
f3. unlocking the mutual exclusion lock to finish releasing the read lock;
when a user releases a write lock, comprising the steps of:
a3. locking the mutual exclusion lock;
b3. the lock reference counter is set to 0 when the write lock is released;
c3. checking that if the maximum allowable number counter of the write lock is 0 and the waiting counter of the read lock is greater than 0, the executing condition of the step is met, checking that if the reference counter of the lock is 0, setting a priority type mark as reading priority, setting the maximum allowable number counter of the read lock as the value of the waiting counter of the read lock, indicating that the reading priority is the maximum and triggerable, performing the turning of the priority after the reading operation of the number of times of the maximum allowable number counter of the read lock is performed, and then sending a trigger signal to the waiting condition lock;
d3. if the condition in the step c3 is not satisfied, checking whether a write lock waiting counter is larger than 0, if so, setting a priority type mark as write priority, and initiating a broadcast signal to a write waiting condition lock to allow all write lock applications;
e3. if the d3 step condition is not satisfied, indicating that no write lock and no read lock wait currently, setting a priority type mark to be no priority, and waiting for a read-write lock application;
f3. and unlocking the mutual exclusion lock to finish releasing the write lock.
2. The method of claim 1, wherein,
when the read-write lock is created, simultaneously creating a mutual exclusive lock, a read-wait condition lock and a write-wait condition lock object, wherein a read-lock wait counter, a read-lock maximum allowable number counter, a write-lock wait counter, a write-lock maximum allowable number counter, a lock reference counter and a priority type mark are all set to 0, so that the initial state of the read-write lock is a state without read-write priority, without read-write wait or without read-write lock occupation;
when the read-write lock is destroyed, the exclusive lock, the read-wait condition lock and the write-wait condition lock object are released, and the read-lock wait counter, the read-lock maximum allowable number counter, the write-lock wait counter, the write-lock maximum allowable number counter, the lock reference counter and the priority type mark are all set to 0.
3. A data read-write priority balancing system, characterized in that it is based on a data read-write priority balancing method as claimed in claim 1 or 2, comprising:
the mutual exclusion lock unit is used for locking or unlocking the related variable of concurrent security access of the data read-write request through the mutual exclusion lock;
the priority type mark unit is used for judging the read-write priority type mark of the data according to the read-write reference counter of the data, and comprises no read-write priority, read-write priority and write priority;
the reference counter unit is used for judging the read-write occupation condition of the data according to the read-write reference counter of the data, and comprises read lock occupation, write lock exclusive and no lock occupation;
and the lock control unit is used for obtaining the read-write priority type mark of the data according to the read-write waiting condition of the data and the maximum allowable number counter residual value.
4. A data read-write priority balancing system according to claim 3, wherein the lock control unit comprises:
a waiting condition lock unit for waiting for an allowable condition signal according to the priority type flag;
a waiting counter unit for judging the priority type mark of the current variable;
and the maximum allowable number counter unit is used for judging the maximum allowable number of the priority type mark of the current variable.
5. A data read-write priority balancing apparatus, characterized in that the priority balancing apparatus comprises:
at least one processor;
at least one memory for storing at least one program;
when the at least one program is executed by the at least one processor, the at least one processor is caused to implement a data read-write priority balancing method as claimed in claim 1 or 2.
6. A storage medium having stored therein instructions executable by a processor, characterized by: the processor-executable instructions, when executed by a processor, are for implementing a data read-write priority balancing method as claimed in claim 1 or 2.
CN202011325771.XA 2020-11-23 2020-11-23 Data read-write priority balancing method, system, device and storage medium Active CN112416556B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202011325771.XA CN112416556B (en) 2020-11-23 2020-11-23 Data read-write priority balancing method, system, device and storage medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011325771.XA CN112416556B (en) 2020-11-23 2020-11-23 Data read-write priority balancing method, system, device and storage medium

Publications (2)

Publication Number Publication Date
CN112416556A CN112416556A (en) 2021-02-26
CN112416556B true CN112416556B (en) 2023-10-20

Family

ID=74777011

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202011325771.XA Active CN112416556B (en) 2020-11-23 2020-11-23 Data read-write priority balancing method, system, device and storage medium

Country Status (1)

Country Link
CN (1) CN112416556B (en)

Families Citing this family (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113010325B (en) * 2021-03-16 2023-07-25 北京百度网讯科技有限公司 Method and device for realizing read-write lock and electronic equipment
CN113590560A (en) * 2021-06-29 2021-11-02 济南浪潮数据技术有限公司 Cache optimization method, system, equipment and storage medium of distributed system
CN114327642A (en) * 2021-12-31 2022-04-12 深圳市兆珑科技有限公司 Data read-write control method and electronic equipment
CN117971137B (en) * 2024-04-02 2024-06-04 山东海润数聚科技有限公司 Multithreading-based large-scale vector data consistency assessment method and system

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107807858A (en) * 2017-10-30 2018-03-16 北京神州绿盟信息安全科技股份有限公司 One kind read-write lock operation method and system, equipment
CN110704198A (en) * 2018-07-10 2020-01-17 阿里巴巴集团控股有限公司 Data operation method, device, storage medium and processor

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8799591B2 (en) * 2007-01-30 2014-08-05 Hewlett-Packard Development Company, L.P. Read-write spinlock
US9329895B2 (en) * 2014-01-22 2016-05-03 International Business Machines Corporation Reader-writer lock
CN109271258B (en) * 2018-08-28 2020-11-17 百度在线网络技术(北京)有限公司 Method, device, terminal and storage medium for realizing re-entry of read-write lock

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107807858A (en) * 2017-10-30 2018-03-16 北京神州绿盟信息安全科技股份有限公司 One kind read-write lock operation method and system, equipment
CN110704198A (en) * 2018-07-10 2020-01-17 阿里巴巴集团控股有限公司 Data operation method, device, storage medium and processor

Also Published As

Publication number Publication date
CN112416556A (en) 2021-02-26

Similar Documents

Publication Publication Date Title
CN112416556B (en) Data read-write priority balancing method, system, device and storage medium
EP3701377B1 (en) Method and apparatus for updating shared data in a multi-core processor environment
CN105511969B (en) Method for mutual exclusion between cross-process threads
Dechev et al. Understanding and effectively preventing the ABA problem in descriptor-based lock-free designs
US8769546B2 (en) Busy-wait time for threads
US10929201B2 (en) Method and system for implementing generation locks
TWI695319B (en) Shared resources in a data processing apparatus for executing a plurality of threads
CN110597640A (en) Inter-process data transmission method and device, terminal and computer storage medium
CN106681836B (en) Semaphore creation method and semaphore creation device
Dechev The ABA problem in multicore data structures with collaborating operations
US9569265B2 (en) Optimization of data locks for improved write lock performance and CPU cache usage in multi core architectures
US20110145515A1 (en) Method for modifying a shared data queue and processor configured to implement same
US9582340B2 (en) File lock
CN113010325B (en) Method and device for realizing read-write lock and electronic equipment
US8161250B2 (en) Methods and systems for partially-transacted data concurrency
US9021483B2 (en) Making hardware objects and operations thread-safe
CN110908968B (en) Method, device, equipment and storage medium for avoiding frightened groups during file lock unlocking
CN111984428A (en) Method, device and equipment for realizing spin lock during resource access
US9507817B2 (en) Method for synchronizing access to shared resources of a computing system and detecting and eliminating deadlocks using lock files
US10310916B2 (en) Scalable spinlocks for non-uniform memory access
US9081630B2 (en) Hardware-implemented semaphore for resource access based on presence of a memory buffer in a memory pool
CN115774724A (en) Concurrent request processing method and device, electronic equipment and storage medium
EP1826671B1 (en) Interruptible thread synchronization method and apparatus
US7447875B1 (en) Method and system for management of global queues utilizing a locked state
CN110837422A (en) Multithreading access method, multithreading access system, electronic terminal and storage medium

Legal Events

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