CN112416556A - 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
CN112416556A
CN112416556A CN202011325771.XA CN202011325771A CN112416556A CN 112416556 A CN112416556 A CN 112416556A CN 202011325771 A CN202011325771 A CN 202011325771A CN 112416556 A CN112416556 A CN 112416556A
Authority
CN
China
Prior art keywords
read
lock
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.)
Granted
Application number
CN202011325771.XA
Other languages
Chinese (zh)
Other versions
CN112416556B (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

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/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 invention 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 a mutual exclusion lock; judging the read-write priority type mark of the current data according to the 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 variable corresponding to the read-write priority type mark, wherein the occupation condition comprises the occupation of the read lock, the exclusive occupation of the write lock and the absence of any lock occupation; obtaining a read-write priority type mark of the data according to the occupation condition of the read-write lock and the residual value of the maximum allowable number counter of the read-write lock; and unlocking the requested data read-write operation through the mutual exclusion lock. The method of the invention improves the concurrency performance of data reading operation, can ensure that the system runs stably for a long time, has excellent data high 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 invention belongs to the technical field of data reading and writing methods in server application scenes, and particularly relates to a data reading and writing priority balancing method, system, device and storage medium.
Background
When multi-threaded security access protection is performed on a resource that allows multiple threads to concurrently read, but allows only one thread to exclusively write, a read-write lock is typically used to improve the concurrent performance of reads. No matter which implementation technology is adopted by the read-write locks widely used at present, the read-write locks can be divided into read-priority read-write locks and write-priority read-write locks according to different read-write priorities. When the protected resource has a large read concurrency amount, the write-priority lock is suitable to be used, and when the protected resource has a large write concurrency amount, the read-priority lock is suitable to be used. The main purpose of the selection is to avoid the problem of read-write starvation of the read-write lock, and when the read concurrency of the protected resource is larger, if the read priority is used, the write operation is dead locked due to starvation of the write operation; when the amount of the protected resources for writing concurrency is large, if the writing priority is used, the reading operation is starved, so that the reading operation is deadlocked. In some server systems, another more complex application scenario exists, sometimes the read concurrency is far greater than the write concurrency, sometimes the write concurrency is far greater than the read concurrency, and the probability and the time of occurrence of the read concurrency and the write concurrency are unpredictable.
At present, in a server application scenario that read-write concurrency is uncertain, sometimes the read concurrency is far greater than the write concurrency, sometimes the write concurrency is far greater than the read concurrency, and the probability and the opportunity of the occurrence of the read-write concurrency are unpredictable, a mutual exclusion lock is generally used for providing safe access to a resource concurrency thread, so that the situation that a service process is deadlocked due to the fact that the read-write lock is starved is effectively avoided.
However, the mutual exclusion lock is mutually exclusive for all read-write operations, and when the read concurrency is high, the read operation concurrency cannot be fully utilized to improve the read performance of the system, and for a server system with high concurrency to read, the read operation concurrency performance of the system can be greatly reduced.
Disclosure of Invention
The method improves the read operation concurrency performance, can ensure that the system runs stably for a long time, has high data concurrency access performance and good economic effect.
In order to achieve the purpose, the invention adopts the following technical means:
a data read-write priority balancing method comprises the following steps:
the concurrent security access locking is carried out on a priority type flag variable, a counter variable and a read/write conditional lock of data reading and writing through a mutual exclusion lock;
judging the read-write priority type marks of the data according to the read-write reference counter of the data, wherein the read-write priority type marks comprise 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 the occupation of a read lock, the exclusive occupation of a write lock and the absence of any lock occupation;
obtaining a reading and writing priority type mark of the data according to the reading and writing waiting condition of the data and the residual value of the maximum allowable number counter;
and unlocking the requested data read-write operation through the mutual exclusion lock.
As a further improvement of the invention, the request data read-write operation comprises a request for applying a read lock, a request for attempting to apply a read lock, a request for releasing a read lock, a request for applying a write lock, a request for attempting to apply a write lock and a request for releasing a write lock.
As a further improvement of the present invention, when a user applies for a read/write lock, the method comprises the following steps:
a1. mutually exclusive locking and locking;
b1. checking whether a write/read lock waiting counter is zero, if so, setting a priority type mark as read/write priority, and allowing the read/write lock to apply;
c1. checking the lock state condition, if the lock reference counter is less than 0 or the priority type mark is that the write/read priority condition is satisfied, executing the read/write waiting condition lock waiting operation, waiting for a condition signal for allowing read/write, and jumping to the step b1 for circular execution when the signal is triggered; if the lock reference counter is less than 0 or the priority type mark is that the write/read priority condition is not satisfied, the application of the read/write lock is successful, and the flow jumps to the step d 1;
d1. when the reading is successfully applied, the lock reference counter is increased by 1, and when the writing lock is successfully applied, the lock reference counter is set to be-1;
e1. if the maximum allowable number counter of the read/write lock is larger than 0, subtracting 1 from the maximum allowable number counter of the read/write lock;
f1. if the maximum allowable number counter of the read/write lock is equal to 0, the priority type flag S109 is set as write/read priority at this time;
g1. and unlocking the mutual exclusion lock to complete the application of the read/write lock.
As a further improvement of the present invention, when a user attempts to apply for a read/write lock, the method comprises the following steps:
a2. defining a flag variable for successful application of the read/write lock, setting the flag variable to be 1, and indicating that the initial value is successful in attempting to apply the read/write lock;
b2. mutually exclusive locking and locking;
c2. checking the lock state, if the lock reference counter is less 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 the read/write lock fails, and jumping to the step g 2;
d2. if the condition of the step c2 is not satisfied, it indicates that the attempt to apply for the read/write lock is successful, and the lock reference counter is incremented by 1;
e2. if the maximum allowable number counter of the read/write lock is larger than 0, subtracting 1 from the maximum allowable number counter of the read/write lock;
f2. if the maximum allowable number counter of the read/write lock is equal to 0, the priority type mark is set as write/read priority at the moment;
g2. unlocking the mutual exclusion lock S101;
h2. a flag is returned indicating whether the read/write lock application was successful.
5. The method of claim 1, wherein when the user releases the read/write lock, the method comprises the following steps:
a3. mutually exclusive locking and locking;
b3. subtracting 1 from the lock reference counter when the read is released, and setting 0 to the lock reference counter when the write lock is released;
c3. checking 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 execution condition of the step is satisfied, checking if the lock introduction counter is 0, setting the priority type flag 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 priority can trigger the write/read operation of the maximum allowable number counter value number of times of the write/read lock at the maximum time, then 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 the read/write lock waiting counter is greater than 0, if so, setting the 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 in the step d is not satisfied, the condition indicates that no read/write lock exists currently and the write/read lock waits, the priority type mark is set to be non-priority, and the read/write lock is waited for application;
f3. and unlocking the mutual exclusion lock to finish releasing the read/write lock.
As a further improvement of the invention, when the read-write lock is created, a mutex lock, a read waiting condition lock and a write waiting condition lock object are created at the same time, and a read lock waiting counter, a read lock maximum allowable number counter, a write lock waiting counter, a write lock maximum allowable number counter, a lock reference counter and a priority type flag are all set to be 0, which indicates that the initial state of the read-write lock is a state without read-write priority, without read-write waiting and without occupation of the read-write lock;
when the read-write lock is destroyed, the mutex lock, the read-wait conditional lock and the write-wait conditional lock object are released, and a read-lock wait counter, a read-lock maximum allowed number counter, a write-lock wait counter, a write-lock maximum allowed number counter, a lock reference counter and a priority type flag are all set to be 0;
a data read-write priority balancing system comprising:
the mutual exclusion lock unit is used for locking or unlocking the data read-write request and the related security access variable through the mutual exclusion lock;
the priority type marking unit is used for judging the read-write priority type marking of the data according to the read-write reference counter of the data, and the priority type marking comprises no read-write priority, read 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, wherein the read-write occupation condition comprises read lock occupation, write lock exclusive occupation and no lock occupation;
and the lock control unit is used for obtaining the reading and writing priority type mark of the data according to the reading and writing waiting condition of the data and the residual value of the maximum allowable number counter.
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 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;
when the at least one program is executed by the at least one processor, the at least one processor is enabled to implement the data read-write priority balancing method.
A storage medium having stored therein processor-executable instructions for implementing the one data read-write priority balancing method when executed by a processor.
Compared with the prior art, the invention has the following advantages:
the invention 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, and solves the problem that the common read-write lock is easy to cause deadlock due to read-write starvation under the scene of large random fluctuation of read-write concurrency, thereby expanding the application range of the read-write lock.
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 herein are incorporated into and constitute a part of this specification, and illustrate embodiments consistent with the present disclosure and together with the description serve to explain the technical solutions of the present disclosure. It is appreciated that the following drawings depict only certain embodiments of the disclosure and are therefore not to be considered limiting of its scope, for those skilled in the art will be able to derive additional related drawings therefrom without the benefit of the inventive faculty.
FIG. 1 is a structural design diagram of a read-write priority balanced lock;
FIG. 2 is a flow chart of the execution process when a lock is requested to be read;
FIG. 3 is a flowchart of an execution process when attempting to apply for a read lock;
FIG. 4 is a flowchart of an execution process when releasing a read lock;
FIG. 5 is a flowchart of an execution process when applying for a write lock;
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 a diagram of an exemplary interface for a read/write priority balanced lock;
FIG. 9 is a diagram of an embodiment of a read-write priority balanced lock.
Detailed Description
In order to make the objects, features and advantages of the present invention more obvious and understandable, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention, and it is apparent that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
In the description herein, references to the description of the term "one embodiment," "some embodiments," "an example," "a specific example," or "some examples," etc., mean that a particular feature, structure, material, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of the invention. Furthermore, the particular features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. Furthermore, various embodiments or examples and features of different embodiments or examples described in this specification can be combined and combined by one skilled in the art without contradiction.
Furthermore, the terms "first", "second" and "first" are used for descriptive purposes only and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include at least one such feature. In the description of the present invention, "a plurality" means two or more unless specifically defined otherwise.
The invention relates to a data read-write priority balancing method, which comprises the following steps:
the concurrent security access locking is carried out on a priority type flag variable, a counter variable and a read/write conditional lock of data reading and writing through a mutual exclusion lock; (ii) a
Judging the read-write priority type marks of the data according to the read-write reference counter of the data, wherein the read-write priority type marks comprise 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 the occupation of a read lock, the exclusive occupation of a write lock and the absence of any lock occupation;
obtaining a reading and writing priority type mark of the data according to the reading and writing waiting condition of the data and the residual value of the maximum allowable number counter;
and unlocking the requested data read-write operation through the mutual exclusion lock.
The structure design of the read-write priority balance lock is as follows:
1) a mutex lock (see mutex lock S101 in fig. 1) that provides thread security protection for the read wait condition lock and the write wait condition lock, as well as for 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 allowed number counter (see read lock maximum allowed number counter S104 in fig. 1) constitute a read lock control unit;
3) a write wait conditional lock (see write wait conditional lock S105 in fig. 1), a write lock wait counter (see write lock wait counter S106 in fig. 1), and a write lock maximum allowed number counter (see write lock maximum allowed number counter S107 in fig. 1) constitute the write lock control unit;
4) a lock reference counter (see lock reference counter S108 in FIG. 1) indicating how many read locks are occupied when the counter value is greater than 0, a value of-1 indicating exclusive write lock and a value of 0 indicating no lock is occupied;
5) and a priority type mark (see a priority type mark S109 in FIG. 1), wherein 0 represents no read-write priority, 1 represents read priority, and 2 represents write priority, and the read-write priority balance lock controls the priority of the lock through the variable to realize dynamic priority balance scheduling.
Based on the above read-write lock structure, the invention outputs 8 interfaces to the outside, and adopts the following technical scheme to realize a read-write priority balance lock:
1) when the read-write lock is created, the object instances of the mutex lock S101, the read-wait conditional lock S102 and the write-wait conditional lock S105 are created at the same time, and the read-lock wait counter S103, the read-lock maximum-allowed-number counter S104, the write-lock wait counter S106, the write-lock maximum-allowed-number counter S107, the lock-indexing 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 the state without read-write priority, without read-write wait and without occupation of the read-write lock.
2) When the read-write lock pin is damaged, object instances of a mutex lock S101, a read waiting condition lock S102 and a write waiting condition lock S105 are released, and a read lock waiting counter S103, a read lock maximum allowable number counter S104, a write lock waiting counter S106, a write lock maximum allowable number counter S107, a lock indexing counter S108 and a priority type flag S109 are all set to be 0;
3) when the user applies for the lock reading, the following processing flow is executed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. checking whether a write lock waiting counter S106 is zero, if so, setting a priority type mark S109 as read priority, and allowing a read lock application;
c. checking the lock state condition, if the lock reference counter S108 is less than 0 or the priority type mark S109 is that the write priority condition is satisfied, executing the read waiting condition lock S102 to wait for the read permission condition signal, and jumping to the step b for circular execution when the signal is triggered; if the lock reference counter S108 is less than 0 or the priority type mark S109 is not the write priority condition, the application of the read lock is successful, and the process skips to the step d;
d. when a lock reading 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, then the maximum allowable number of read locks counter S104 is decremented by 1;
f. if the maximum allowable number counter S104 of the read lock is equal to 0, the priority type mark S109 is set as write priority at the moment;
g. and unlocking the mutual exclusion lock S101 to complete the lock reading application.
4) When the user tries to apply for the read lock, the following processing flow is executed:
a. defining a reading lock application success mark variable, setting the reading lock application success mark variable as 1, and indicating that the initial value is that the attempt of applying for the reading lock is successful;
b. carrying out operation protection by mutually exclusive locking and locking;
c. checking the lock state, if the lock reference counter S108 is less than 0 or the priority type mark S109 is write priority, setting the read lock application success mark variable to be 0, indicating that the attempt of applying the read lock fails, and jumping to the step g;
d. if the condition in step c is not satisfied, it indicates that the attempt to apply for the read lock is successful, at this time, the lock reference counter S108 is incremented by 1;
e. if the maximum allowable number of read locks counter S104 is greater than 0, then the maximum allowable number of read locks counter S104 is decremented by 1;
f. if the maximum allowable number counter S104 of the read lock is equal to 0, the priority type mark S109 is set as write priority at the moment;
g. unlocking the mutual exclusion lock S101;
h. and returning a mark indicating whether the read lock application is successful or not.
5) When the user releases the read lock, the following processing flow is executed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. subtracting 1 from the lock introduction counter S108;
c. checking that if the read lock maximum allowed number counter S104 is 0 and the write lock wait counter S106 is greater than 0, the execution condition of this step is satisfied, then checking that if the lock reference counter S108 is 0, the priority type flag S109 is set as write priority, the write lock maximum allowed number counter S107 is set as the value of the write lock wait counter S106, indicating that the write priority can trigger the write operation of the write lock maximum allowed number counter S107 for the maximum number of times at this time, then turning over the priority level, and then sending a trigger signal to the write wait condition lock S105;
d. if the condition in step c is not satisfied, checking whether the read lock waiting counter S103 is greater than 0, if so, setting the priority type mark S109 as read priority, and initiating a broadcast signal to the read waiting condition lock S102 to allow all read lock applications;
e. if the condition in the step d is not satisfied, the condition indicates that no read lock and no write lock wait at present, and the priority type mark S109 is set to have no priority to wait for the application of the read-write lock;
f. and the mutual exclusion lock S101 is unlocked to complete the release of the read lock.
6) When the user applies for the write lock, the following processing flow is executed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. checking whether a read lock waiting counter S103 is zero, if so, setting a priority type mark S109 as write priority, and allowing a write lock application;
c. checking the lock state condition, if the lock reference counter S108 is not 0 or the priority type flag S109 is satisfied, executing a write waiting condition lock S105 waiting operation, waiting for a condition signal for allowing writing, and jumping to the step b for circular execution when the signal is triggered; if the lock reference counter S108 is not 0 or the priority type mark S109 does not satisfy the read priority condition, the write lock application is successful, and the process skips 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 allowed number counter S107 is greater than 0, then the write lock maximum allowed number counter S107 is decremented by 1;
f. if the maximum allowable write lock number counter S107 is equal to 0, the priority type flag S109 is set as read priority at this time;
a. and unlocking the mutual exclusion lock S101 to complete the lock reading application.
7) When the user tries to apply for the write lock, the following processing flows are executed:
a. defining a write lock application success flag variable, setting the write lock application success flag variable to be 1, and indicating that the initial value is that the attempt of applying the write lock is successful;
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 mark S109 is read priority, setting the write lock application success mark variable to be 0, indicating that the attempt to apply the write lock fails, and jumping to the step g;
d. if the condition in the step c is not satisfied, the successful application of the write lock is indicated, and at the moment, the lock reference counter S108 is set to be-1;
e. if the write lock maximum allowed number counter S107 is greater than 0, then the write lock maximum allowed number counter S107 is decremented by 1;
f. if the maximum allowable write lock number counter S107 is equal to 0, the priority type flag S109 is set as read priority at this time;
g. unlocking the mutual exclusion lock S101;
h. returning a sign whether the read lock application is successful or not;
8) when the user releases the write lock, the following process flow is executed:
a. the mutual exclusion lock S101 is locked for operation protection;
b. the lock quote counter S108 is set to 0;
c. checking that if the write lock maximum allowed number counter S107 is 0 and the read lock wait counter S103 is greater than 0, the execution condition of this step is satisfied, the priority type flag S109 is set as read priority, the read lock maximum allowed number counter S104 is set as the value of the read lock wait counter S103, indicating that the read priority can trigger the read operation of the value number of the read lock maximum allowed number counter S104 at the maximum number of times this time, then turning over the priority level, and then initiating a broadcast signal to the read wait condition lock S102;
d. if the condition in step c is not satisfied, checking whether a write lock waiting counter S106 is greater than 0, if so, setting a priority type mark S109 as write priority, and initiating a trigger signal to the write wait condition lock S105 to allow the write lock application;
e. if the condition in the step d is not satisfied, the condition indicates that no read lock and no write lock wait at present, and the priority type mark S109 is set to have no priority to wait for the application of the read-write lock;
f. and the mutual exclusion lock S101 is unlocked to complete the release of the read lock.
The present invention will be described in further detail with reference to the following detailed description and accompanying drawings.
The invention relates to a data read-write priority balancing system, which comprises:
the mutual exclusion lock unit is used for marking variables of priority types of data reading and writing, variables of a counter and a reading/writing conditional lock for concurrent security access locking through the mutual exclusion lock;
the priority type marking unit judges the read-write priority type marks of the data according to the read-write reference counter of the data, wherein the read-write priority type marks comprise no read-write priority, read 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 exclusive occupation and no lock occupation;
and the lock control unit obtains the read-write priority type mark of the data according to the read-write waiting condition of the data and the residual value of the maximum allowable number counter.
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 the prior type mark of the current data read-write;
and the maximum allowable number counter unit is used for judging the maximum allowable number of the priority type mark of the current data read-write.
In a possible embodiment of the present invention, the 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 allowed number counter S104, the write lock wait counter S106, the write lock maximum allowed number counter S107, the lock reference counter S108, and the priority type flag S109 are all set to 0, indicating that the initial state of the read/write lock is the read-write priority free state, the read-write wait free state, and the read-write lock occupation free state.
2) The interface 802 destroys an instance of the module 800, and releases the object instances of the mutex lock S101, the read wait condition lock S102, and the write wait condition lock S105, and all of the read lock wait counter S103, the read lock maximum allowed number counter S104, the write lock wait counter S106, the write lock maximum allowed number counter S107, the lock reference counter S108, and the priority type flag S109 are set to 0;
3) an interface 803, which implements a lock application operation for the user according to the execution flow of fig. 2;
4) an interface 804, which implements an attempt to apply for a read lock operation for the user according to the execution flow of fig. 3;
5) interface 805, implementing a release read lock operation for the user according to the execution flow of fig. 4;
6) interface 806, which implements a write lock application for the user according to the execution flow of fig. 5;
7) an interface 807 for implementing an attempt to apply for write lock operation for the user according to 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 C language, and outputs an 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 quoted by other system modules, and can be called by other programming languages if dynamic library packaging is carried out on the read-write priority balance lock. Meanwhile, the read-write lock supports different operating systems, realizes cross-platform application, can work under Windows, Linux and other Unix-like platforms, can be applied to a domestic operating system, and has wide application prospect.
The invention is applied to a plurality of modules of the Ruitang real-time database system, and achieves good reading concurrency performance improvement.
In a specific application example, reading and writing are required to be carried out on a history cache data block of each measuring point in the Ruitang real-time database system, and multi-thread access protection is required. In the Ruitang real-time database system, a history cache data block of a measuring point is a middle layer data cache area of snapshot data and history archive data of the measuring point, the snapshot data is stored in the history cache data block after being archived, and the history cache data block is archived and written to a disk file after being full, so that the archival performance of the Ruitang real-time database system is improved by utilizing the high-speed read-write performance of a memory, and the latest section of hot data of the measuring point can have the high-speed read 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 simultaneously and concurrently performed, and only allows one exclusive write operation. It also has a special concurrency characteristic as shown in fig. 9: sometimes this measurement point is operated by highly concurrent execution 902, which allows concurrent execution after applying for a read lock of the 901 lock instance, and sometimes this measurement point is operated by 903 and 904, which require exclusive execution of a write operation after applying for a write lock of the 901 lock instance, and the peak of the read and write operations is dynamically variable and unpredictable, and sometimes fluctuates greatly.
At the beginning of the design of Ruitang real-time database system, the conservative mutual exclusion lock is adopted to perform data read-write access protection, the system can work well, but when a plurality of read operations are simultaneously and concurrently performed, the mutual exclusion lock cannot achieve read concurrency, so that when the number of read concurrency is more, the performance of the read operation is worse. Later, a pthread-write lock pthread _ rwlock _ t is adopted to replace a mutex lock, the read concurrency performance of a test result is obviously improved, all the results are perfect, but the result is discovered in a long-time high-intensity pressure test process, the database can sometimes be inexplicably deadlocked, so that the database is not responded, and even if the pthread _ rwocktr _ setkind _ np () function is used for switching the read-write priority of the read-write lock, the deadlock cannot be avoided.
Therefore, the technical scheme provided by the invention can be used for dynamically adjusting the read-write priority of the lock according to the read-write concurrency and performing read-write priority balance scheduling, so that the problem of deadlock caused by read-write starvation can be well avoided. Since the Rui Teng real-time database system uses the read-write priority balance lock, the read concurrency performance of the historical cache data block is equivalent to the test performance of the pthread _ rwlock _ t, but the deadlock situation never occurs after long-time high-intensity test.
At present, the Ruiteng real-time database system applying the technical scheme of the invention is deployed and operated in SIS projects of a plurality of power plants, bears the storage and access of massive production data of the whole plant, has stable long-term operation and excellent data concurrent access performance, and obtains good economic effect.
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 and a tablet computer capable of running an application program, or a server. In this application, a data read-write priority balancing device includes:
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 enabled to implement the data read-write priority balancing method.
A processor may include one or more processing cores. The processor, using the various interfaces and lines to connect the various parts throughout the electronic device, performs various functions of the electronic device and processes 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 hardware using at least one of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), and Programmable Logic Array (PLA). The processor may integrate one or more of a Central Processing Unit (CPU), a Graphics Processing Unit (GPU), a modem, and the like. Wherein, the CPU mainly processes an operating system, a user interface, an application program and the like; the GPU is used for rendering and drawing display content; the modem is used to handle wireless communications. It is to be understood that the modem may be implemented by a communication chip without being integrated into the processor.
The Memory may include a Random Access Memory (RAM) or a Read-Only Memory (Read-Only Memory). The memory may be used to store an instruction, a program, code, a set of codes, or a set of instructions. 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 various method embodiments described below, and the like. The data storage area can also store data (such as a phone book, audio and video data, chatting record data) and the like created by the electronic equipment in use.
The structure of a computer-readable storage medium provided by the embodiment of the application. In which processor-executable instructions are stored, which when executed by a processor, are used to implement 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. Alternatively, the computer-readable storage medium includes a non-transitory computer-readable storage medium. The computer readable storage medium has a storage space for program code for performing any of the method steps of the above-described method. 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 used to illustrate the technical solutions of the present application, and not to limit the same; although the present application has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications and substitutions do not necessarily depart from the spirit and scope of the corresponding technical solutions in the embodiments of the present application.

Claims (10)

1. A data read-write priority balancing method is characterized by comprising the following steps:
the concurrent security access locking is carried out on a priority type flag variable, a counter variable and a read/write conditional lock of data reading and writing through a mutual exclusion lock;
judging the read-write priority type marks of the data according to the read-write reference counter of the data, wherein the read-write priority type marks comprise 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 the occupation of a read lock, the exclusive occupation of a write lock and the absence of any lock occupation;
obtaining a reading and writing priority type mark of the data according to the reading and writing waiting condition of the data and the residual value of the maximum allowable number counter;
and unlocking the requested data read-write operation through the mutual exclusion lock.
2. The method of claim 1, wherein the request data read/write operation comprises a request for a read lock, a request for an attempt to apply a read lock, a request for a release of a read lock, a request for a write lock, and a request for a write lock.
3. The method of claim 1, wherein when a user applies for a read/write lock, the method comprises the following steps:
a1. mutually exclusive locking and locking;
b1. checking whether a write/read lock waiting counter is zero, if so, setting a priority type mark as read/write priority, and allowing the read/write lock to apply;
c1. checking the lock state condition, if the lock reference counter is less than 0 or the priority type mark is that the write/read priority condition is satisfied, executing the read/write waiting condition lock waiting operation, waiting for a condition signal for allowing read/write, and jumping to the step b1 for circular execution when the signal is triggered; if the lock reference counter is less than 0 or the priority type mark is that the write/read priority condition is not satisfied, the application of the read/write lock is successful, and the flow jumps to the step d 1;
d1. when the reading is successfully applied, the lock reference counter is increased by 1, and when the writing lock is successfully applied, the lock reference counter is set to be-1;
e1. if the maximum allowable number counter of the read/write lock is larger than 0, subtracting 1 from the maximum allowable number counter of the read/write lock;
f1. if the maximum allowable number counter of the read/write lock is equal to 0, the priority type flag S109 is set as write/read priority at this time;
g1. and unlocking the mutual exclusion lock to complete the application of the read/write lock.
4. The method of claim 1, wherein when a user attempts to apply for a read/write lock, the method comprises the following steps:
a2. defining a flag variable for successful application of the read/write lock, setting the flag variable to be 1, and indicating that the initial value is successful in attempting to apply the read/write lock;
b2. mutually exclusive locking and locking;
c2. checking the lock state, if the lock reference counter is less 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 the read/write lock fails, and jumping to the step g 2;
d2. if the condition of the step c2 is not satisfied, it indicates that the attempt to apply for the read/write lock is successful, and the lock reference counter is incremented by 1;
e2. if the maximum allowable number counter of the read/write lock is larger than 0, subtracting 1 from the maximum allowable number counter of the read/write lock;
f2. if the maximum allowable number counter of the read/write lock is equal to 0, the priority type mark is set as write/read priority at the moment;
g2. unlocking the mutual exclusion lock S101;
h2. a flag is returned indicating whether the read/write lock application was successful.
5. The method of claim 1, wherein when the user releases the read/write lock, the method comprises the following steps:
a3. mutually exclusive locking and locking;
b3. subtracting 1 from the lock reference counter when the read is released, and setting 0 to the lock reference counter when the write lock is released;
c3. checking 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 execution condition of the step is satisfied, checking if the lock introduction counter is 0, setting the priority type flag 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 priority can trigger the write/read operation of the maximum allowable number counter value number of times of the write/read lock at the maximum time, then 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 the read/write lock waiting counter is greater than 0, if so, setting the 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 in the step d is not satisfied, the condition indicates that no read/write lock exists currently and the write/read lock waits, the priority type mark is set to be non-priority, and the read/write lock is waited for application;
f3. and unlocking the mutual exclusion lock to finish releasing the read/write lock.
6. The method of claim 1, wherein the data read/write priority balancing method,
when a 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 allowed number counter, a write-lock wait counter, a write-lock maximum allowed number counter, a lock reference counter and a priority type flag are all set to be 0, so that the initial state of the read-write lock is a read-write priority-free state, a read-write wait-free state and a read-write lock occupation-free state;
when the read-write lock is destroyed, the mutex lock, the read-wait conditional lock and the write-wait conditional lock object are released, and the read-lock wait counter, the read-lock maximum allowed number counter, the write-lock wait counter, the write-lock maximum allowed number counter, the lock reference counter and the priority type flag are all set to be 0.
7. A system for prioritizing the reading and writing of data, comprising:
the mutual exclusion lock unit is used for locking or unlocking the data read-write request and the related security access variable through the mutual exclusion lock;
the priority type marking unit is used for judging the read-write priority type marking of the data according to the read-write reference counter of the data, and the priority type marking comprises no read-write priority, read 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, wherein the read-write occupation condition comprises read lock occupation, write lock exclusive occupation and no lock occupation;
and the lock control unit is used for obtaining the reading and writing priority type mark of the data according to the reading and writing waiting condition of the data and the residual value of the maximum allowable number counter.
8. The system of claim 7, wherein the lock control unit comprises:
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 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.
9. 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 executed by the at least one processor, cause the at least one processor to implement a method of data read-write priority balancing according to any one of claims 1 to 6.
10. A storage medium having stored therein instructions executable by a processor, the storage medium comprising: the processor-executable instructions, when executed by a processor, are for implementing a method of data read-write priority balancing according to any one of claims 1 to 6.
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 true CN112416556A (en) 2021-02-26
CN112416556B 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)

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113010325A (en) * 2021-03-16 2021-06-22 北京百度网讯科技有限公司 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
WO2023124422A1 (en) * 2021-12-31 2023-07-06 深圳市兆珑科技有限公司 Data read-write control method and electronic device
CN117971137A (en) * 2024-04-02 2024-05-03 山东海润数聚科技有限公司 Multithreading-based large-scale vector data consistency assessment method and system

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080183979A1 (en) * 2007-01-30 2008-07-31 Larson Douglas V Read-write spinlock with no mutual exclusion among readers
US20150205733A1 (en) * 2014-01-22 2015-07-23 International Business Machines Corporation Reader-writer lock
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
US20200073728A1 (en) * 2018-08-28 2020-03-05 Baidu Online Network Technology (Beijing) Co., Ltd. Method and device for implementing read-write lock reentry, terminal and storage medium

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20080183979A1 (en) * 2007-01-30 2008-07-31 Larson Douglas V Read-write spinlock with no mutual exclusion among readers
US20150205733A1 (en) * 2014-01-22 2015-07-23 International Business Machines Corporation Reader-writer lock
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
US20200073728A1 (en) * 2018-08-28 2020-03-05 Baidu Online Network Technology (Beijing) Co., Ltd. Method and device for implementing read-write lock reentry, terminal and storage medium

Cited By (6)

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

Also Published As

Publication number Publication date
CN112416556B (en) 2023-10-20

Similar Documents

Publication Publication Date Title
CN112416556A (en) Data read-write priority balancing method, system, device and storage medium
CN103299272B (en) Use the atom internal memory able to programme of the atom program stored
EP3701377B1 (en) Method and apparatus for updating shared data in a multi-core processor environment
Dechev et al. Understanding and effectively preventing the ABA problem in descriptor-based lock-free designs
US10503671B2 (en) Controlling access to a shared resource
CN110597640A (en) Inter-process data transmission method and device, terminal and computer storage medium
US8769546B2 (en) Busy-wait time for threads
CN108572876B (en) Method and device for realizing read-write lock
CN106681836B (en) Semaphore creation method and semaphore creation device
CN111459691A (en) Read-write method and device for shared memory
CN111737020A (en) Concurrent processing method and device for distributed cluster
CN113010325B (en) Method and device for realizing read-write lock and electronic equipment
US9582340B2 (en) File lock
US20110145515A1 (en) Method for modifying a shared data queue and processor configured to implement same
CN108228331A (en) A kind of method for managing resource and system
CN107315631B (en) Task scheduling method and device
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
US20060218382A1 (en) Data processing disorder preventing method
CN113641872B (en) Hashing method, hashing device, hashing equipment and hashing medium
US9507817B2 (en) Method for synchronizing access to shared resources of a computing system and detecting and eliminating deadlocks using lock files
CN115774724A (en) Concurrent request processing method and device, electronic equipment and storage medium
EP1826671B1 (en) Interruptible thread synchronization method and apparatus
CN115202884B (en) Method for adding read write lock of high-performance system based on polling and application
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