CN105224251B - A kind of Read-Write Locks optimization method and system based on Linux - Google Patents

A kind of Read-Write Locks optimization method and system based on Linux Download PDF

Info

Publication number
CN105224251B
CN105224251B CN201510629053.4A CN201510629053A CN105224251B CN 105224251 B CN105224251 B CN 105224251B CN 201510629053 A CN201510629053 A CN 201510629053A CN 105224251 B CN105224251 B CN 105224251B
Authority
CN
China
Prior art keywords
write
read
waiting list
lock application
highest priority
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
CN201510629053.4A
Other languages
Chinese (zh)
Other versions
CN105224251A (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.)
Inspur Beijing Electronic Information Industry Co Ltd
Original Assignee
Inspur Beijing Electronic Information Industry 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 Inspur Beijing Electronic Information Industry Co Ltd filed Critical Inspur Beijing Electronic Information Industry Co Ltd
Priority to CN201510629053.4A priority Critical patent/CN105224251B/en
Publication of CN105224251A publication Critical patent/CN105224251A/en
Application granted granted Critical
Publication of CN105224251B publication Critical patent/CN105224251B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Abstract

The invention discloses a kind of Read-Write Locks optimization method and system based on Linux, including:It receives read lock application or add and writes lock and apply and whether judge before Read-Write Locks in being applied state.It is not on when being applied state, directly handles read lock application or add and write lock application.In the state that is applied and it is occupied when, Read-Write Locks application will be added to be added in preset read-write waiting list, and update the reading highest priority recorded in rwlock structures or write highest priority.When occupied Read-Write Locks are released, judge to read and write in waiting list whether existing plus Read-Write Locks application waits for;It according to judging result and reads highest priority and writes highest priority, handle the read lock application in reading waiting list and write adding in waiting list and write lock application.Scheme through the invention can reduce the situation for either avoiding reader or writer from dying of hunger, and improve the resource utilization and system in parallel process performance of system.

Description

A kind of Read-Write Locks optimization method and system based on Linux
Technical field
The present invention relates to operating system design fields, and in particular to a kind of Read-Write Locks optimization method based on Linux and is System.
Background technology
Include semaphore (Semaphore) and spin lock there are two types of the fundamental mechanism for realizing data mutual exclusion at linux (Spinlock).The characteristics of due to mutual exclusion, having no thread concurrency using the code of spin lock can say, the property of multicomputer system It can be restricted.Thread is divided into reader and writer, multiple readers by read-write spin lock (Reader-Writer Spinlock) It can be allowed to access shared resource simultaneously, shared resource can only be accessed in a period of time by a writer, apply for thread Busy waiting mode is still used within waiting period.
It is to read preferential or write preferential both of which that Linux can configure Read-Write Locks at present, and acquiescence Read-Write Locks are to read preferentially, If there is writer is waiting for lock, reader will not be blocked.If be configured to write preferentially, if there is writer is waiting for lock, after all Continuous reader, which locks, can all block.But when the reader's number for occupying reader's lock is 0, when having writer when waiting for, can preferentially it call out Wake up writer.But the situation that reader or writer die of hunger can all occur in both configuration modes, when be configured to reader it is preferential when, meeting There is the situation that writer dies of hunger, and when being configured to Writer-priority, it may appear that the situation that reader dies of hunger.
Invention content
To solve the above-mentioned problems, the present invention proposes a kind of Read-Write Locks optimization method and system based on Linux, can The situation for either avoiding reader or writer from dying of hunger is reduced, the resource utilization and system in parallel process performance of system are improved.
In order to achieve the above object, the present invention proposes a kind of optimization method based on Linux plus Read-Write Locks, this method Including:
It receives read lock application or adds and write lock application, and whether judge before Read-Write Locks in being applied state.
When judgement Read-Write Locks, which are not on, is applied state, directly handle read lock application or add to write to lock and apply, make reading Person's acquisition read lock, which enters critical section operation or writer's acquisition is made to write, to be locked into critical section operation.
When Read-Write Locks be in be applied state and it is occupied when, by the read lock application received be added it is preset It reads in waiting list, and updates the reading highest priority read in waiting list recorded in rwlock structures;Alternatively, will receive To write-read lock application be added it is preset write in waiting list, and update writing in waiting list of being recorded in rwlock structures Write highest priority.
When occupied Read-Write Locks are released, judge to read whether to have read lock application waiting in waiting list and write In waiting list whether it is existing plus write lock application wait for;It is preferential according to the reading highest recorded in judging result and rwlock structures Grade and write highest priority, processing reads the read lock application in waiting list and writes in waiting list plus write lock application.
Preferably,
Reading highest priority in update rwlock structures in the reading waiting list that records includes:
Whether compare the priority of the read lock application received higher than reading waiting team pre-recorded in rwlock structures The reading highest priority of previous existing read lock application in row, when the priority of the read lock application received is waited for higher than reading In queue when the reading highest priority of previous existing read lock application, the reading waiting list recorded in rwlock structures is updated In reading highest priority, alternatively, when the read lock application for reading in waiting list only to receive, the read lock that will receive The priority of application is as reading highest priority and is recorded in rwlock structures.
Preferably,
Updating the highest priority of writing write in waiting list recorded in rwlock structures includes:
Whether the priority for comparing the write-read lock application received higher than pre-recorded writing in rwlock structures waits for team It is previously existing plus that writes lock application writes highest priority in row, when receiving plus write the priority of lock application higher than writing waiting It is previously existing plus write when writing highest priority of lock application in queue, update recorded in rwlock structures writes waiting list In write highest priority, will be receiving plus write lock alternatively, only receiving plus when writing lock application when writing in waiting list The priority of application is used as and writes highest priority and be recorded in rwlock structures.
Preferably, according to the reading highest priority recorded in judging result and rwlock structures and to write highest preferential Grade, processing read waiting list in read lock application and write in waiting list plus write lock application include:
It reads to have in waiting list read lock application when judgement to wait for and write existing in waiting list plus write lock application etc. When, compare and the reading highest priority recorded in rwlock structures and write highest priority;It is write when reading highest priority is higher than When highest priority, priority processing read lock application;When writing highest priority higher than highest priority is read, priority processing, which adds, to be write Lock application.
When judging to read have read lock application waiting in waiting list, writes in waiting list and write lock application waiting without adding When, the read lock application in waiting list is read in directly processing.
When judgement write in waiting list it is existing plus write lock application wait for, read waiting list in do not have read lock application waiting When, directly processing writes adding in waiting list and writes lock application.
Preferably, this method further includes:
According to the reading highest priority recorded in judging result and rwlock structures and highest priority is being write, handled Read waiting list in read lock application and write in waiting list plus write lock application before, judge to be released be read lock also It is to write lock.
When what is be released is read lock, the number of the current reader for holding read lock of record is subtracted one, and judges to subtract one it Whether the number for holding the reader of read lock afterwards is zero;When it is zero to subtract one to hold the number of the reader of read lock later, according to judgement As a result with the reading highest priority recorded in rwlock structures and write highest priority, processing is read in waiting list plus is read It lock application and writes described in waiting list plus writes lock application;The number for holding the reader of read lock after subtracting one is not zero When, the reader for continuing waiting for currently holding read lock discharges read lock.
When what is be released is to write lock, directly according to the reading highest priority recorded in judging result and rwlock structures And highest priority is write, it handles the read lock application in reading waiting list and writes adding in waiting list and write lock application.
In order to achieve the above object, the optimization system for adding Read-Write Locks based on Linux that the invention also provides a kind of, this is System includes:Judgment module, processing module and update module.
Judgment module writes lock application for receiving read lock application or adding, and judges whether be in by Shen before Read-Write Locks It please state.
Processing module, for when judge Read-Write Locks be not on be applied state when, directly handle read lock application or plus Lock application is write, so that reader is obtained read lock and is entered critical section operation or writer is made to obtain and writes and be locked into critical section operation.
Update module, for when Read-Write Locks be in the state that is applied and it is occupied when, the read lock that will receive Application is added in preset reading waiting list, and it is preferential to update the reading highest read in waiting list recorded in rwlock structures Grade;Alternatively, by the write-read received lock application be added it is preset write in waiting list, and update and record in rwlock structures It writes in waiting list and writes highest priority.
Processing module is additionally operable to when occupied Read-Write Locks are released, judge read waiting list in whether it is existing plus read Whether lock application waits for and writes in waiting list existing plus write lock application and wait for;According in judging result and rwlock structures The reading highest priority of record and highest priority is write, processing reads the read lock application in waiting list and writes waiting list In plus write lock application.
Preferably,
Reading highest priority in update module update rwlock structures in the reading waiting list that records includes:
Whether compare the priority of the read lock application received higher than reading waiting team pre-recorded in rwlock structures The reading highest priority of previous existing read lock application in row, when the priority of the read lock application received is waited for higher than reading In queue when the reading highest priority of previous existing read lock application, the reading waiting list recorded in rwlock structures is updated In reading highest priority, alternatively, when the read lock application for reading in waiting list only to receive, the read lock that will receive The priority of application is as reading highest priority and is recorded in rwlock structures.
Preferably,
Update module updates the highest priority of writing write in waiting list recorded in rwlock structures:
Whether the priority for comparing the write-read lock application received higher than pre-recorded writing in rwlock structures waits for team It is previously existing plus that writes lock application writes highest priority in row, when receiving plus write the priority of lock application higher than writing waiting It is previously existing plus write when writing highest priority of lock application in queue, update recorded in rwlock structures writes waiting list In write highest priority, will be receiving plus write lock alternatively, only receiving plus when writing lock application when writing in waiting list The priority of application is used as and writes highest priority and be recorded in rwlock structures.
Preferably, processing module according to the reading highest priority recorded in judging result and rwlock structures and is write most High priority, processing read waiting list in read lock application and write in waiting list plus write lock application include:
It reads to have in waiting list read lock application when judgement to wait for and write existing in waiting list plus write lock application etc. When, compare and the reading highest priority recorded in rwlock structures and write highest priority;It is write when reading highest priority is higher than When highest priority, priority processing read lock application;When writing highest priority higher than highest priority is read, priority processing, which adds, to be write Lock application.
When judging to read have read lock application waiting in waiting list, writes in waiting list and write lock application waiting without adding When, the read lock application in waiting list is read in directly processing.
When judgement write in waiting list it is existing plus write lock application wait for, read waiting list in do not have read lock application waiting When, directly processing writes adding in waiting list and writes lock application.
Preferably, processing module is additionally operable to:
According to the reading highest priority recorded in judging result and rwlock structures and highest priority is being write, handled Read waiting list in read lock application and write in waiting list plus write lock application before, judge to be released be read lock also It is to write lock.
When what is be released is read lock, the number of the current reader for holding read lock of record is subtracted one, and judges to subtract one it Whether the number for holding the reader of read lock afterwards is zero;When it is zero to subtract one to hold the number of the reader of read lock later, according to judgement As a result with the reading highest priority recorded in rwlock structures and write highest priority, processing is read in waiting list plus is read Lock is applied and is write in waiting list plus writes lock application;When the number for the reader for holding read lock after subtracting one is not zero, after It is continuous that the reader for currently holding read lock is waited for discharge read lock.
When what is be released is to write lock, directly according to the reading highest priority recorded in judging result and rwlock structures And highest priority is write, it handles the read lock application in reading waiting list and writes adding in waiting list and write lock application.
Compared with prior art, the present invention includes:It receives read lock application or adds and write lock application, and before judging Read-Write Locks Whether it is in and is applied state.When judgement Read-Write Locks, which are not on, is applied state, directly handle read lock application or add and write Lock application makes reader obtain read lock and enter critical section operation or writer is made to obtain and writes and be locked into critical section operation.Work as Read-Write Locks In the state that is applied and it is occupied when, the read lock application received is added in preset reading waiting list, and The reading highest priority read in waiting list recorded in update rwlock structures;Apply alternatively, the write-read received is locked Be added it is preset write in waiting list, and update writing in waiting list of being recorded in rwlock structures and write highest priority. When occupied Read-Write Locks are released, judge to read whether to have read lock application waiting in waiting list and write waiting list In whether it is existing plus write lock application wait for;According to the reading highest priority recorded in judging result and rwlock structures and write Highest priority handles the read lock application in reading waiting list and writes adding in waiting list and writes lock application.By this hair Bright scheme can reduce the situation for either avoiding reader or writer from dying of hunger, and improve the resource utilization and system of system Parallel processing performance.
Description of the drawings
The attached drawing in the embodiment of the present invention is illustrated below, the attached drawing in embodiment be for the present invention into one Step understands, for explaining the present invention together with specification, does not constitute limiting the scope of the invention.
Fig. 1 is the optimization method flow chart for adding Read-Write Locks based on Linux of the present invention;
Fig. 2 is the optimization system composition frame chart for adding Read-Write Locks based on Linux of the present invention.
Specific implementation mode
For the ease of the understanding of those skilled in the art, the invention will be further described below in conjunction with the accompanying drawings, not It can be used for limiting the scope of the invention.
Herein by taking linux kernel 2.6.32 as an example, the case where having fully considered reader and writer, it is proposed that one based on reading The optimization method of person's Writer-priority grade reduces to the greatest extent and either avoids the occurrence of the situation that reader or writer die of hunger, improves system Resource utilization and system in parallel process performance.
The optimization of read-write spin lock in the present invention includes three parts:The optimization of read lock adds the optimization for writing lock and releases The optimization of lock is put, processing is all optimized to this three parts in the optimization method of this paper.The present invention mentality of designing be:Based on reading The priority of person and writer are respectively intended to record read-write and wait for team using the filler pad1 and pad2 in rwlock structures Highest priority in row.Then when unlock, judge if read-write waiting list has, to compare the excellent of the two First grade.When reader asks the read lock of a Read-Write Locks, if no writer occupies lock, or the priority of the writer blocked does not have Reader is high, then reader is locked.
Specifically, in order to achieve the above object, the present invention proposes a kind of optimization side based on Linux plus Read-Write Locks Method, as shown in Figure 1, this method includes:
S101, it receives read lock application or adds and write lock application, and whether judge before Read-Write Locks in being applied state.
S102, when judge Read-Write Locks be not on be applied state when, directly handle read lock application or add write lock application, Make reader obtain read lock and enter critical section operation or writer is made to obtain to write and be locked into critical section operation.
S103, when Read-Write Locks be in be applied state and it is occupied when, by the read lock application received be added In preset reading waiting list, and update the reading highest priority read in waiting list recorded in rwlock structures;Alternatively, By the write-read received lock application be added it is preset write in waiting list, and update recorded in rwlock structures write wait for team Highest priority is write in row.
Preferably, updating the reading highest priority in rwlock structures in the reading waiting list that records includes:
Whether compare the priority of the read lock application received higher than reading waiting team pre-recorded in rwlock structures The reading highest priority of previous existing read lock application in row, when the priority of the read lock application received is waited for higher than reading In queue when the reading highest priority of previous existing read lock application, the reading waiting list recorded in rwlock structures is updated In reading highest priority, alternatively, when the read lock application for reading in waiting list only to receive, the read lock that will receive The priority of application is as reading highest priority and is recorded in rwlock structures.
Preferably, updating the highest priority of writing write in waiting list recorded in rwlock structures includes:
Whether the priority for comparing the write-read lock application received higher than pre-recorded writing in rwlock structures waits for team It is previously existing plus that writes lock application writes highest priority in row, when receiving plus write the priority of lock application higher than writing waiting It is previously existing plus write when writing highest priority of lock application in queue, update recorded in rwlock structures writes waiting list In write highest priority, will be receiving plus write lock alternatively, only receiving plus when writing lock application when writing in waiting list The priority of application is used as and writes highest priority and be recorded in rwlock structures.
S104, when occupied Read-Write Locks are released, judge to read whether to have read lock application in waiting list and wait for And it whether existing plus write lock application and wait for writes in waiting list;Most according to the reading recorded in judging result and rwlock structures High priority and highest priority is write, processing reads the read lock application in waiting list and writes in waiting list plus write lock Application.
Preferably, according to the reading highest priority recorded in judging result and rwlock structures and to write highest preferential Grade, processing read waiting list in read lock application and write in waiting list plus write lock application include:
It reads to have in waiting list read lock application when judgement to wait for and write existing in waiting list plus write lock application etc. When, compare and the reading highest priority recorded in rwlock structures and write highest priority;It is write when reading highest priority is higher than When highest priority, priority processing read lock application;When writing highest priority higher than highest priority is read, priority processing, which adds, to be write Lock application.
In embodiments of the present invention, it using the filler pad1 and pad2 in rwlock structures, is respectively intended to record and reads It reading highest priority in waiting list and writes in waiting list and writes highest priority.Here compare in rwlock structures The reading highest priority of record and highest priority is write, that is, compares the size of the value of pad1 and pad2, if the value of pad1 is more than The value of pad2 then wakes up the reader read in waiting list, otherwise wakes up the writer write in waiting list.
When judging to read have read lock application waiting in waiting list, writes in waiting list and write lock application waiting without adding When, the read lock application in waiting list is read in directly processing.
When judgement write in waiting list it is existing plus write lock application wait for, read waiting list in do not have read lock application waiting When, directly processing writes adding in waiting list and writes lock application.
Preferably, this method further includes:
According to the reading highest priority recorded in judging result and rwlock structures and highest priority is being write, handled Read waiting list in read lock application and write in waiting list plus write lock application before, judge to be released be read lock also It is to write lock.
When what is be released is read lock, the number of the current reader for holding read lock of record is subtracted one, and judges to subtract one it Whether the number for holding the reader of read lock afterwards is zero;When it is zero to subtract one to hold the number of the reader of read lock later, according to judgement As a result with the reading highest priority recorded in rwlock structures and write highest priority, processing is read in waiting list plus is read It lock application and writes described in waiting list plus writes lock application;The number for holding the reader of read lock after subtracting one is not zero When, the reader for continuing waiting for currently holding read lock discharges read lock.
When what is be released is to write lock, directly according to the reading highest priority recorded in judging result and rwlock structures And highest priority is write, it handles the read lock application in reading waiting list and writes adding in waiting list and write lock application.
In order to achieve the above object, the optimization system 01 for adding Read-Write Locks based on Linux that the invention also provides a kind of, such as Shown in Fig. 2, which includes:Judgment module 02, processing module 03 and update module 04.
Judgment module 02 writes lock application for receiving read lock application or adding, and judges whether be in quilt before Read-Write Locks Application status.
Processing module 03, for when judge Read-Write Locks be not on be applied state when, directly processing read lock application or Add and write lock application, so that reader is obtained read lock and is entered critical section operation or writer is made to obtain and write and be locked into critical section operation.
Update module 04, for when Read-Write Locks be in be applied state and it is occupied when, by receive plus read Lock application is added in preset reading waiting list, and it is excellent to update the reading highest read in waiting list recorded in rwlock structures First grade;Alternatively, the write-read received is locked, application addition is preset to be write in waiting list, and is updated and recorded in rwlock structures Write in waiting list and write highest priority.
Processing module 03 is additionally operable to when occupied Read-Write Locks are released, judge read waiting list in whether it is existing plus Whether read lock application waits for and writes in waiting list existing plus write lock application and wait for;According to judging result and rwlock structures The reading highest priority of middle record and highest priority is write, processing reads the read lock application in waiting list and write to wait for team Adding in row, writes lock application.
Preferably,
Update module 04 updates the reading highest priority in the reading waiting list that records in rwlock structures:
Whether compare the priority of the read lock application received higher than reading waiting team pre-recorded in rwlock structures The reading highest priority of previous existing read lock application in row, when the priority of the read lock application received is waited for higher than reading In queue when the reading highest priority of previous existing read lock application, the reading waiting list recorded in rwlock structures is updated In reading highest priority, alternatively, when the read lock application for reading in waiting list only to receive, the read lock that will receive The priority of application is as reading highest priority and is recorded in rwlock structures.
Preferably,
Update module 04 updates the highest priority of writing write in waiting list recorded in rwlock structures:
Whether the priority for comparing the write-read lock application received higher than pre-recorded writing in rwlock structures waits for team It is previously existing plus that writes lock application writes highest priority in row, when receiving plus write the priority of lock application higher than writing waiting It is previously existing plus write when writing highest priority of lock application in queue, update recorded in rwlock structures writes waiting list In write highest priority, will be receiving plus write lock alternatively, only receiving plus when writing lock application when writing in waiting list The priority of application is used as and writes highest priority and be recorded in rwlock structures.
Preferably, processing module 03 according to the reading highest priority recorded in judging result and rwlock structures and is write Highest priority, processing read waiting list in read lock application and write in waiting list plus write lock application include:
It reads to have in waiting list read lock application when judgement to wait for and write existing in waiting list plus write lock application etc. When, compare and the reading highest priority recorded in rwlock structures and write highest priority;It is write when reading highest priority is higher than When highest priority, priority processing read lock application;When writing highest priority higher than highest priority is read, priority processing, which adds, to be write Lock application.
When judging to read have read lock application waiting in waiting list, writes in waiting list and write lock application waiting without adding When, the read lock application in waiting list is read in directly processing.
When judgement write in waiting list it is existing plus write lock application wait for, read waiting list in do not have read lock application waiting When, directly processing writes adding in waiting list and writes lock application.
Preferably, processing module 03 is additionally operable to:
According to the reading highest priority recorded in judging result and rwlock structures and highest priority is being write, handled Read waiting list in read lock application and write in waiting list plus write lock application before, judge to be released be read lock also It is to write lock.
When what is be released is read lock, the number of the current reader for holding read lock of record is subtracted one, and judges to subtract one it Whether the number for holding the reader of read lock afterwards is zero;When it is zero to subtract one to hold the number of the reader of read lock later, according to judgement As a result with the reading highest priority recorded in rwlock structures and write highest priority, processing is read in waiting list plus is read Lock is applied and is write in waiting list plus writes lock application;When the number for the reader for holding read lock after subtracting one is not zero, after It is continuous that the reader for currently holding read lock is waited for discharge read lock.
When what is be released is to write lock, directly according to the reading highest priority recorded in judging result and rwlock structures And highest priority is write, it handles the read lock application in reading waiting list and writes adding in waiting list and write lock application.
Compared with prior art, the present invention includes:It receives read lock application or adds and write lock application, and whether judge Read-Write Locks In being applied state.When judgement Read-Write Locks, which are not on, is applied state, directly handle read lock application or add and write lock Shen Please, make reader obtain read lock and enter critical section operation or writer is made to obtain to write and be locked into critical section operation.When Read-Write Locks are in Be applied state and it is occupied when, the read lock application received is added in preset reading waiting list, and is updated What is recorded in rwlock structures reads the reading highest priority in waiting list;It is added alternatively, the write-read received is locked application It is preset to write in waiting list, and update writing in waiting list of being recorded in rwlock structures and write highest priority.Work as quilt When the Read-Write Locks of occupancy are released, judging to read whether to have read lock application waiting in waiting list and write in waiting list is It is no it is existing plus write lock application wait for;According to the reading highest priority recorded in judging result and rwlock structures and write highest Priority handles the read lock application in reading waiting list and writes adding in waiting list and writes lock application.Through the invention Scheme can reduce the situation for either avoiding reader or writer from dying of hunger, and improve the resource utilization and system in parallel of system Process performance.
In short, the beneficial effects of the invention are as follows:By the Read-Write Locks optimization method based on reader, Writer-priority grade, reduce It either avoids and the situation that reader or writer die of hunger occurs, improve the resource utilization and system in parallel treatability of system Can, while not changing the data structure of existing operating system, maintain the compatibility of operating system application.
It should be noted that embodiment described above be merely for convenience of it will be understood by those skilled in the art that, and It is not used in and limits the scope of the invention, under the premise of not departing from the inventive concept of the present invention, those skilled in the art couple Any obvious replacement and improvement that the present invention is made etc. are within protection scope of the present invention.

Claims (8)

1. a kind of Read-Write Locks optimization method based on Linux, which is characterized in that the method includes:
It receives read lock application or adds and write lock application, and whether judge before Read-Write Locks in being applied state;
When judge the Read-Write Locks be not on be applied state when, directly handle the read lock application or described plus write lock Shen Please, make reader obtain read lock and enter critical section operation or writer is made to obtain to write and be locked into critical section operation;
When the Read-Write Locks be in the state that is applied and it is occupied when, the read lock application received is added pre- If reading waiting list in, and update the reading highest priority in the reading waiting list recorded in rwlock structures;Or Person, by the Read-Write Locks application received be added it is preset write in waiting list, and update and remember in the rwlock structures Described write in waiting list of record writes highest priority;
When the occupied Read-Write Locks are released, judge in the reading waiting list whether to have read lock application wait for And it is described write in waiting list it is whether existing plus write lock application and wait for;It is recorded according in judging result and the rwlock structures The reading highest priority and it is described write highest priority, handle the read lock application read in waiting list with And it described write described in waiting list plus writes lock application;
Wherein, described according to the reading highest priority recorded in judging result and the rwlock structures and described to write Highest priority handles the read lock application read in waiting list and described write described in waiting list plus write Lock is applied:
When judge to have in the readings waiting list read lock application wait for and it is described write in waiting list have described in Add reading highest priority when writing lock application and waiting for, recorded in the rwlock structures and described to write highest preferential Grade;When the reading highest priority writes highest priority higher than described in, read lock application described in priority processing;It is write most when described Add when high priority is higher than the reading highest priority, described in priority processing and writes lock application;
When judging that having the read lock application in the readings waiting list waits for, it is described write in waiting list without it is described add write When lock application waits for, the read lock application in the reading waiting list is directly handled;
Have described plus write lock application and wait for when being write described in judgement in waiting list, without described plus reading in the readings waiting list When lock application waits for, described in waiting list is directly write described in processing plus writes lock application.
2. optimization method as described in claim 1, which is characterized in that
Reading highest priority in the reading waiting list recorded in the update rwlock structures includes:
Whether the priority for comparing the read lock application received is pre-recorded described higher than in the rwlock structures The previously reading highest priority of existing read lock application is read in waiting list, it is preferential when the read lock application received Existing for grade is previous higher than in the reading waiting list when reading highest priority of read lock application, the rwlock structures are updated Reading highest priority in the reading waiting list recorded in body, alternatively, only being received when in the reading waiting list When the read lock application, the priority of the read lock application received as the reading highest priority and is recorded in In the rwlock structures.
3. optimization method as described in claim 1, which is characterized in that
The highest priority of writing write in waiting list recorded in the update rwlock structures includes:
Whether the priority for comparing receive described plus writing lock application is pre-recorded described higher than in the rwlock structures It writes in waiting list that previously existing plus that writes lock application writes highest priority, described in receive plus writes the preferential of lock application Grade is write previously existing in waiting list plus writes when writing highest priority of lock application higher than described, updates the rwlock structures Described write in waiting list recorded in body write highest priority, alternatively, when described write only receives in waiting list It is described plus write lock application when, will receive described in plus write lock application priority write highest priority as described in and be recorded in In the rwlock structures.
4. optimization method as described in claim 1, which is characterized in that the method further includes:
According to the reading highest priority that is recorded in the judging result and the rwlock structures and described writing most High priority handles the read lock application read in waiting list and described write described in waiting list plus write lock Before application, judge to be released is read lock or writes lock;
When what is be released is read lock, the number of the reader for currently holding the read lock of record is subtracted one, and judge subtract one it Whether the number for holding the reader of the read lock afterwards is zero;When it is zero to subtract one to hold the number of the reader of the read lock later, According to the reading highest priority that is recorded in the judging result and the rwlock structures and described to write highest preferential Grade handles the read lock application read in waiting list and described write described in waiting list plus write lock application; When the number for the reader for holding the read lock after subtracting one is not zero, the reader's release for currently holding the read lock is continued waiting for The read lock;
When what is be released is to write lock, directly most according to the reading recorded in the judging result and the rwlock structures High priority and it is described write highest priority, handle the read lock application read in waiting list and described write It waits for described in queue plus writes lock application.
5. a kind of Read-Write Locks optimization system based on Linux, which is characterized in that the system comprises:Judgment module, processing module And update module;
The judgment module writes lock application for receiving read lock application or adding, and judges whether be in by Shen before Read-Write Locks It please state;
The processing module, for when judge the Read-Write Locks be not on be applied state when, directly handle the read lock Application is described plus write lock application, so that reader is obtained read lock and is entered critical section operation or writer is made to obtain and writes and be locked into critical zone Operation;
The update module, for when the Read-Write Locks be in the state that is applied and it is occupied when, the institute that will receive It states read lock application to be added in preset reading waiting list, and updates in the reading waiting list recorded in rwlock structures Reading highest priority;Alternatively, by the Read-Write Locks application received be added it is preset write in waiting list, and update described in Described write in waiting list recorded in rwlock structures writes highest priority;
The processing module is additionally operable to when the occupied Read-Write Locks are released, judge in the reading waiting list whether Have read lock application wait for and it is described write in waiting list it is whether existing plus write lock application and wait for;According to judging result and institute State the reading highest priority recorded in rwlock structures and it is described write highest priority, handle the reading waiting list In the read lock application and described write described in waiting list plus write lock application;
Wherein, the processing module according to the reading highest priority recorded in judging result and the rwlock structures with And it is described write highest priority, handle the read lock application read in waiting list and described write in waiting list It is described plus write lock application include:
When judge to have in the readings waiting list read lock application wait for and it is described write in waiting list have described in Add reading highest priority when writing lock application and waiting for, recorded in the rwlock structures and described to write highest preferential Grade;When the reading highest priority writes highest priority higher than described in, read lock application described in priority processing;It is write most when described Add when high priority is higher than the reading highest priority, described in priority processing and writes lock application;
When judging that having the read lock application in the readings waiting list waits for, it is described write in waiting list without it is described add write When lock application waits for, the read lock application in the reading waiting list is directly handled;
Have described plus write lock application and wait for when being write described in judgement in waiting list, without described plus reading in the readings waiting list When lock application waits for, described in waiting list is directly write described in processing plus writes lock application.
6. optimization system as claimed in claim 5, which is characterized in that
The update module updates the reading highest priority packet in the reading waiting list recorded in the rwlock structures It includes:
Whether the priority for comparing the read lock application received is pre-recorded described higher than in the rwlock structures The previously reading highest priority of existing read lock application is read in waiting list, it is preferential when the read lock application received Existing for grade is previous higher than in the reading waiting list when reading highest priority of read lock application, the rwlock structures are updated Reading highest priority in the reading waiting list recorded in body, alternatively, only being received when in the reading waiting list When the read lock application, the priority of the read lock application received as the reading highest priority and is recorded in In the rwlock structures.
7. optimization system as claimed in claim 5, which is characterized in that
The update module updates described write in waiting list recorded in the rwlock structures and writes highest priority packet It includes:
Whether the priority for comparing receive described plus writing lock application is pre-recorded described higher than in the rwlock structures It writes in waiting list that previously existing plus that writes lock application writes highest priority, described in receive plus writes the preferential of lock application Grade is write previously existing in waiting list plus writes when writing highest priority of lock application higher than described, updates the rwlock structures Described write in waiting list recorded in body write highest priority, alternatively, when described write only receives in waiting list It is described plus write lock application when, will receive described in plus write lock application priority write highest priority as described in and be recorded in In the rwlock structures.
8. optimization system as claimed in claim 5, which is characterized in that the processing module is additionally operable to:
According to the reading highest priority that is recorded in the judging result and the rwlock structures and described writing most High priority handles the read lock application read in waiting list and described write described in waiting list plus write lock Before application, judge to be released is read lock or writes lock;
When what is be released is read lock, the number of the reader for currently holding the read lock of record is subtracted one, and judge subtract one it Whether the number for holding the reader of the read lock afterwards is zero;When it is zero to subtract one to hold the number of the reader of the read lock later, According to the reading highest priority that is recorded in the judging result and the rwlock structures and described to write highest preferential Grade handles the read lock application read in waiting list and described write described in waiting list plus write lock application; When the number for the reader for holding the read lock after subtracting one is not zero, the reader's release for currently holding the read lock is continued waiting for The read lock;
When what is be released is to write lock, directly most according to the reading recorded in the judging result and the rwlock structures High priority and it is described write highest priority, handle the read lock application read in waiting list and described write It waits for described in queue plus writes lock application.
CN201510629053.4A 2015-09-28 2015-09-28 A kind of Read-Write Locks optimization method and system based on Linux Active CN105224251B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201510629053.4A CN105224251B (en) 2015-09-28 2015-09-28 A kind of Read-Write Locks optimization method and system based on Linux

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201510629053.4A CN105224251B (en) 2015-09-28 2015-09-28 A kind of Read-Write Locks optimization method and system based on Linux

Publications (2)

Publication Number Publication Date
CN105224251A CN105224251A (en) 2016-01-06
CN105224251B true CN105224251B (en) 2018-09-25

Family

ID=54993257

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201510629053.4A Active CN105224251B (en) 2015-09-28 2015-09-28 A kind of Read-Write Locks optimization method and system based on Linux

Country Status (1)

Country Link
CN (1) CN105224251B (en)

Families Citing this family (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107273214A (en) * 2017-06-30 2017-10-20 郑州云海信息技术有限公司 A kind of multinuclear controller resource access method and its device based on solid state hard disc
CN107729129A (en) * 2017-09-18 2018-02-23 惠州Tcl移动通信有限公司 A kind of multithread processing method based on synchrolock, terminal and storage medium
CN107632794A (en) * 2017-10-20 2018-01-26 北京小米移动软件有限公司 Read-Write Locks control method and device
CN109032522B (en) * 2018-07-25 2021-06-29 浪潮电子信息产业股份有限公司 Data reading method of solid state disk and solid state disk
CN109299046B (en) * 2018-08-30 2023-08-15 湖北工业大学 Collaborative editing method based on TCP WebSocket protocol
CN111104412B (en) * 2018-10-25 2023-05-30 阿里巴巴集团控股有限公司 Concurrent control method, device and system based on single thread
CN109710644A (en) * 2018-12-26 2019-05-03 苏州思必驰信息科技有限公司 The method and apparatus for preventing caching from penetrating
CN110908968B (en) * 2019-11-14 2022-05-27 浪潮电子信息产业股份有限公司 Method, device, equipment and storage medium for avoiding frightened groups during file lock unlocking
CN111984379B (en) * 2020-07-14 2023-04-07 上海金仕达软件科技股份有限公司 Read-write transaction control method, system, terminal device and storage medium
CN111984428A (en) * 2020-07-20 2020-11-24 上海金仕达软件科技有限公司 Method, device and equipment for realizing spin lock during resource access
CN113010325B (en) * 2021-03-16 2023-07-25 北京百度网讯科技有限公司 Method and device for realizing read-write lock and electronic equipment

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1920780A (en) * 2006-08-15 2007-02-28 杭州华为三康技术有限公司 Method and system for accessing critical region
CN101187862A (en) * 2006-11-13 2008-05-28 英特尔公司 Critical section detection and prediction mechanism for hardware lock elision
CN102662747A (en) * 2012-04-23 2012-09-12 深圳市融创天下科技股份有限公司 Method, system and terminal device for threads to access critical zones
US8495642B2 (en) * 2008-04-23 2013-07-23 Red Hat, Inc. Mechanism for priority inheritance for read/write locks

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US8356131B2 (en) * 2009-10-25 2013-01-15 Sony Mobile Communications Ab System and method for controlling interruption of a process in electronic equipment based on priority of the process, and program

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1920780A (en) * 2006-08-15 2007-02-28 杭州华为三康技术有限公司 Method and system for accessing critical region
CN101187862A (en) * 2006-11-13 2008-05-28 英特尔公司 Critical section detection and prediction mechanism for hardware lock elision
US8495642B2 (en) * 2008-04-23 2013-07-23 Red Hat, Inc. Mechanism for priority inheritance for read/write locks
CN102662747A (en) * 2012-04-23 2012-09-12 深圳市融创天下科技股份有限公司 Method, system and terminal device for threads to access critical zones

Also Published As

Publication number Publication date
CN105224251A (en) 2016-01-06

Similar Documents

Publication Publication Date Title
CN105224251B (en) A kind of Read-Write Locks optimization method and system based on Linux
CN103514053B (en) Shared-memory-based method for conducting communication among multiple processes
CN103699437B (en) A kind of resource regulating method and equipment
US8973004B2 (en) Transactional locking with read-write locks in transactional memory systems
US8473952B2 (en) System and method for communication between concurrent transactions using transaction communicator objects
CN102262559B (en) Resource sharing method and system
CN108572876B (en) Method and device for realizing read-write lock
US11442871B2 (en) Supporting concurrent remove operations and add-to-front operations on a Least Recently Used (LRU) queue
CN101631328A (en) Synchronous method performing mutual exclusion access on shared resource, device and network equipment
US20110078385A1 (en) System and Method for Performing Visible and Semi-Visible Read Operations In a Software Transactional Memory
US8495642B2 (en) Mechanism for priority inheritance for read/write locks
CN107357648B (en) Method for realizing spin lock when multi-core CPU accesses resources
CN101510162B (en) Software transaction internal memory implementing method based on delaying policy
CN102929711A (en) Implementing method of real-time transactional memory of software
CN101556597A (en) Self-adaptive optimistic concurrency control method
CN113448701A (en) Multi-process outbound control method, system, electronic equipment and storage medium
CN110730958B (en) Method and system for managing prioritized database transactions
US8868845B1 (en) Dynamic single/multi-reader, single-writer spinlocks
CN100399302C (en) Method and apparatus for raising speed of access USB interface information safety equipment
CN107491314A (en) Processing method is write based on Read-Write Locks algorithm is accessible to HBASE real time datas
CN113961364A (en) Large-scale lock system implementation method and device, storage medium and server
JPH08329019A (en) Data management system of semaphore
CN115951844B (en) File lock management method, equipment and medium of distributed file system
CN104462546B (en) A kind of storage of data access and inter-process data shared system
CN104714837A (en) Technical method for example concurrent processing under workflow engine cluster environment

Legal Events

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