CN107357648A - The implementation method of spin lock when a kind of multi-core CPU accesses resource - Google Patents
The implementation method of spin lock when a kind of multi-core CPU accesses resource Download PDFInfo
- Publication number
- CN107357648A CN107357648A CN201710376711.2A CN201710376711A CN107357648A CN 107357648 A CN107357648 A CN 107357648A CN 201710376711 A CN201710376711 A CN 201710376711A CN 107357648 A CN107357648 A CN 107357648A
- Authority
- CN
- China
- Prior art keywords
- lock
- resource
- cpu
- internal storage
- storage location
- 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
Links
- 238000000034 method Methods 0.000 title claims abstract description 81
- 230000008569 process Effects 0.000 claims description 27
- 230000007246 mechanism Effects 0.000 abstract description 2
- 230000014509 gene expression Effects 0.000 description 5
- 238000013461 design Methods 0.000 description 4
- 230000001360 synchronised effect Effects 0.000 description 4
- 230000008859 change Effects 0.000 description 3
- 230000006870 function Effects 0.000 description 3
- 238000003780 insertion Methods 0.000 description 3
- 230000037431 insertion Effects 0.000 description 3
- 238000012986 modification Methods 0.000 description 3
- 230000004048 modification Effects 0.000 description 3
- 238000011017 operating method Methods 0.000 description 2
- 238000006467 substitution reaction Methods 0.000 description 2
- 230000003466 anti-cipated effect Effects 0.000 description 1
- 238000013475 authorization Methods 0.000 description 1
- 230000008901 benefit Effects 0.000 description 1
- 230000005540 biological transmission Effects 0.000 description 1
- 230000015556 catabolic process Effects 0.000 description 1
- 238000012790 confirmation Methods 0.000 description 1
- 238000010276 construction Methods 0.000 description 1
- 238000006731 degradation reaction Methods 0.000 description 1
- 230000001419 dependent effect Effects 0.000 description 1
- 238000000151 deposition Methods 0.000 description 1
- 238000010586 diagram Methods 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000000977 initiatory effect Effects 0.000 description 1
- 230000009467 reduction Effects 0.000 description 1
- 239000013589 supplement Substances 0.000 description 1
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements 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/46—Multiprogramming arrangements
- G06F9/50—Allocation of resources, e.g. of the central processing unit [CPU]
- G06F9/5005—Allocation of resources, e.g. of the central processing unit [CPU] to service a request
- G06F9/5027—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals
- G06F9/5038—Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resource being a machine, e.g. CPUs, Servers, Terminals considering the execution order of a plurality of tasks, e.g. taking priority or time dependency constraints into consideration
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F9/00—Arrangements for program control, e.g. control units
- G06F9/06—Arrangements 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/46—Multiprogramming arrangements
- G06F9/52—Program synchronisation; Mutual exclusion, e.g. by means of semaphores
Landscapes
- Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Multi Processors (AREA)
Abstract
The present invention discloses a kind of implementation method of spin lock when multi-core CPU accesses resource, including:Resource to be visited has resource lock, any CPU is when accessing resource, generate and be provided from the spin lock that body uses, the internal storage structure of resource lock and spin lock includes being used for the first internal storage location for identifying lock token and the second internal storage location for preserving next lock address;Before any CPU accesses resource, the first internal storage location of resource lock is arranged to empty, the second internal storage location is arranged to resource lock itself, resource lock itself is formed a self-loopa chained list;Any CPU is locked and unlocked when accessing resource.The implementation method of the spin lock of the present invention distributes a resource lock for each shared resource, and each resource lock forms unidirectional self-loopa chained list when the shared resource right to use is unoccupied.The complexity of programming is reduced using self-loopa chained list mechanism, the service efficiency of system shared resource can be improved.
Description
Technical field
The present invention relates to computer realm, more particularly, to a kind of implementation method of multi-core CPU spin lock.
Background technology
Multi-core CPU is because the operation of its thread is parallel, to ensure that program (or process) is visited the safety of a certain resource
Ask, maintain the uniformity of data, this program run parallel generally requires to be synchronized in bottom using lock mechanism, so as to protect
The uniformity of shared resource data in program operation process is demonstrate,proved, otherwise, the easy inconsistent initiation system mistake of factor data, is caused tight
Weight consequence.In monokaryon CPU, due to each moment, the task of operation only has one, and in multi-core CPU system, multiple threads
Really perform, therefore simply the synchronous method in monokaryon system can not be applied in multi-core processor system parallel,
Needing design one, fast and efficiently method of data synchronization realizes synchronization between CPU core.
Spin lock has simultaneously provided good solution method between CPU core, and its general principle is ought wherein some CPU
When core accesses shared resource, resource is locked, the atomic operation dependent on lock bus is needed in locking procedure.In the process,
Other cores, which have to wait for core release bus, could carry out consecutive Bus lines operation.
General CPU core can all provide the data exchange instruction between register and internal memory, such as the SWP in ARM, x86's
XCHG etc. is ordered, and during performing the order, CPU can lock bus automatically and carry out exchanging for content of registers and memory content.
Simplest spin lock, it is namely based on the instruction of register memory data exchange and realizes, i.e., by the beginning of the value of a certain memory cell of internal memory
Beginning turns to 0.Hereafter register is assigned initial value 1 by each CPU, performs register memory location contents exchange instruction, if being deposited after exchanging
Device value is 0, then illustrates that current system resource is not occupied, if not 0, then continue to exchange, used until obtaining resource when being 0
Power.After the completion of resource use, 0 value is assigned to correspondence memory unit, it is allowed to which other CPU are accessed.This spin lock design method is very
Simply, however, this method can not ensure that CPU obtains the fairness of resource access rights, in the access of frequently shared resource,
Easily there is the possibility that some cores can not access resource for a long time, if the task that the CPU core performs is urgent, system can be had a strong impact on
Real-time performance, influence program operation stability, therefore, in systems in practice, this spin lock is rarely employed.
The appearance for being lined up spin lock (Ticket lock) solves the problems, such as above-mentioned fairness, and this spin lock passes through certainly
Add realization to be lined up, during obtaining resource and adding certainly, carry out atom addition, and obtain sequence number corresponding to itself, concrete principle
It is known to those skilled in the art, do not illustrate herein.It is simple to be lined up spin lock design, but this algorithm depends on atom adding
Method, its operating process generally require several steps, i.e. hardware lock bus;Rdma read content;Modified values;In modified values are write
In depositing;Hardware unlocks bus.Because during lock bus, other CPU cores can be forced to wait if bus is accessed, therefore, this
Individual process easily causes the extra invalid expense of other cores.In addition, more seriously, CPU during locking and unblock,
Need to carry out frequently to change internal storage data value, so as to trigger the cpu cache data of the locks to be obtained such as all not hit, it is necessary to frequency
The problem of renewal caching, cause other CPU efficiency degradations.Therefore, this spin lock has further improved needs.
Cause efficiency low to solve the problems, such as that queuing spin lock caching can not be hit, MCS and K42 spin locks occur.
Both spin lock principles are to allow each CPU core only to be spinned in the local variable of itself, and in locking procedure, each CPU can
An interim spin lock variable is produced, and assigns lock initial value, the CPU of the resources to be obtained such as all is automatically composed an order
Chained list, and CPU is obtained resource access rights successively in order.Relative to spin lock is lined up, although both spin locks design
Complexity improves, but due to solving the problems, such as that caching can not be hit, when CPU core number is more, can largely carry
The execution efficiency of high algorithm.MCS spin locks are prescribed a time limit in acquisition resources use right, it is necessary to assure and internal memory corresponding to lock resource is effective,
I.e. before access resource terminates and is unlocked, the CPU for obtaining resource access right must assure that in access process, itself lock
It is not released.Therefore, during unblock is added, its interface function generally requires extra variable and carries out parameter transmission, corresponding
With causing certain inconvenience.K42 locks solve above mentioned problem, but its method complexity but considerably increases, pair plus unblock
CPU brings extra expense.In addition, the releasing process of MCS spin locks, K42 spin locks add releasing process to all rely on original
Son exchanges, or exchanges to attempt to operate and (exchange the reason for attempting and be often as whether system can not be expected next spin lock
In the presence of caused by).The atomic operation of locking bus would generally bring extra bus to take expense, therefore, both spin locks
Still there is further improved space.
The content of the invention
To make the efficiency of CPU spin locks further improve, reduce the complexity of algorithm, the present invention proposes a kind of self-loopa certainly
Rotation lock implementation method.
In one embodiment of the invention, there is provided the implementation method of spin lock when a kind of multi-core CPU accesses resource, including
Resource to be visited has resource lock, and for any CPU when accessing resource, generation is provided from the spin lock that body uses, and provides
Source is locked and the internal storage structure of spin lock includes being used for identifying the first internal storage location of lock token and next for preserving
Lock the second internal storage location of address;
Before any CPU accesses resource, the first internal storage location of resource lock is arranged to empty, the second internal storage location is set
Resource lock itself is set to, resource lock itself is formed a self-loopa chained list;
Any CPU is locked and unlocked when accessing resource.
According to the method for the present embodiment, a resource lock is distributed for each shared resource, each resource lock is in shared money
When the source right to use is unoccupied, unidirectional self-loopa chained list is formed.Each shared resource possesses a global lock (resource lock), each
CPU core locks spin respective, and cache synchronization can be avoided to cause CPU synchronous efficiencies lower, reduce programming
Complexity, the service efficiency of system shared resource can be improved.
In a preferred embodiment, locking includes:
The first module of the spin lock of any CPU is set to non-NULL by step 101, and second unit is set into empty;
The current queue that the spin lock is placed in acquisition resource by step 103 any CPU is last, while obtains the spin lock
Previous lock mark;
Step 105 judges that what is obtained identifies whether as resource lock, if it is, step 107 is gone to, if not, going to step
112;
Step 107 judges whether the first internal storage location of the resource lock has been labeled, if marked, poll waits,
Until the lock token of the resource lock is eliminated, and go to step 109;
First internal storage location of step 109 markup resources lock, and it is last that resource lock is placed in into queue, while obtain resource lock
The mark of last lock of queue before being placed in after queue;
The second internal storage location that the lock of acquisition identifies is arranged to the address of resource lock by step 111, and goes to step 113;
The second internal storage location for obtaining lock mark is arranged to the CPU spin locks by step 112, goes to step 113;
Step 113 judges whether the second internal storage location of the spin lock of any CPU has been arranged to non-NULL, if it is not, then
Continue waiting for, until being judged as non-NULL, go to step 115;
Step 115 returns to the second internal storage location content of the spin lock of any CPU, next lock in its storage queue
Mark;
And unlocking includes:
First internal storage location of the mark for the lock that locking procedure in step 115 is returned is empty.
According to the method for this preferred embodiment, during CPU is obtained or discharges resource access rights, it is only necessary to using posting
Storage internal memory exchange command reduces CPU and is obtaining resource access right without realizing atomic operation using single bus line command
Bus collision caused by possibility in limited time, improves efficiency.
It is very simple to obtain the CPU unblock operations after access right, null value, solution need to be only assigned to some memory cell
It is higher to lock efficiency.
In a further advantageous embodiment, the implementation method of CPU spin locks also includes attempting the process locked, including:
Step 201 locks CPU resource access queue, not in queue before any CPU is obtained and accessed resource
CPU only could access resource when dequeue locks;
Step 203 judges whether there was only resource lock in addition to any CPU in the queue and judges the first of resource lock
Whether internal storage location is not labeled, if it is, going to step 205, if it is not, then releasing the locking to queue, and returns to trial
Lock failure;
First internal storage location of step 205 markup resources lock, and the locking to queue is released, resource lock mark is returned, into
Work(lock resource, obtain resource access rights.
In a further advantageous embodiment, the implementation method of CPU spin locks also includes:
When any CPU accesses resource, the spin lock of its own is not destroyed before unblock,
Wherein locking includes:
First internal storage location of the spin lock of any CPU is set to non-NULL and is set to the second internal storage location by step 301
It is empty;
The spin lock of any CPU is placed in that queue is last by step 303, while last in queue is locked before obtaining
Mark;
Step 305 judges that the lock of the acquisition is identified whether as resource lock, if so, step 307 is gone to, if it is not, going to step
312;
It is resource lock that step 307, which sets the first internal storage location of the spin lock of any CPU,;
Step 309 judges whether the first internal storage location of resource lock is empty, if so, step 311 is gone to, if it is not, going to step
Rapid 312;
Step 311, which returns, obtains authority success;
Step 312, which returns, obtains authority failure,
And wherein unblock includes
Step 313 reads the lock mark of the first internal storage location of the spin lock of any CPU;
Step 315 judges that the lock is identified whether as sky, if it is not, step 317 is gone to, if so, going to step 325;
Step 317 judges whether the first internal storage location of lock mark is empty, if it is not, poll is until being yes, if so, going to
Step 319;
Step 319 judges that the lock identifies whether the spin lock itself for any CPU, if it is not, step 321 is gone to, if so,
Go to step 325;
The occupancy mark of resource lock is set to non-NULL by step 321 any CPU;
Resource lock is placed in that queue is last by step 323, at the same before obtaining last lock of queue occupancy mark;
Step 325 judges that occupancy of last lock of queue before this is identified whether as sky, if so, then poll until for
It is no, if it is not, then going to step 327;
First internal storage location of next lock of any CPU is set to empty by step 327 so that the occupancy mark of next lock
Note is eliminated.
By the way that in the preferred embodiment, when resource is occupied, CPU need not wait resource lock to obtain, other operations can be carried out.
Prescribed a time limit it is determined that obtaining resources use right, just operating method corresponding to progress.
In a further advantageous embodiment, CPU spin locks implementation method, after step 311 or step 312, this method
Further comprise the procedure for confirming whether CPU has obtained resource access rights:
Step 401 reads the lock mark of the first internal storage location of the spin lock of any CPU;
Step 403 judges that the lock is identified whether as sky, and authority success is obtained if so, returning;If it is not, go to step 405;
Step 405 judges whether the first internal storage location of lock mark is empty, and authority success is obtained if so, returning;If it is not,
Go to step 407;
Step 407, which returns, obtains authority failure.
Brief description of the drawings
By the description to the embodiment of the present invention referring to the drawings, above-mentioned and other purposes of the invention, feature and
Advantage will be apparent from, in the accompanying drawings:
Fig. 1 is the spin lock internal storage structure schematic diagram according to one embodiment of the invention;
Fig. 2 shows that resource lock according to an embodiment of the invention (global lock) initializes content;
Fig. 3 shows the implementation method of spin lock when CPU according to an embodiment of the invention obtains resource;
Fig. 4 shows that CPU according to an embodiment of the invention obtains the flow chart of resource access rights;
Fig. 5 shows the flow chart of trial gain access according to an embodiment of the invention;
Fig. 6 shows that trial according to an embodiment of the invention obtains and will currently attempt information insertion queue, obtains power
The flow chart of limit;
Fig. 7 shows the flow chart according to an embodiment of the invention for detecting whether gain access;And
Fig. 8 shows the flow chart of end cut-and-try process according to an embodiment of the invention.
It should be noted that this specification accompanying drawing is not drawn to draw, and only schematical purpose, therefore, no
It should be understood any limitation on the scope of the present invention and constraint.In the accompanying drawings, similar part is with similar accompanying drawing mark
Number mark.
Embodiment
It is described in detail referring to the drawings, the accompanying drawing forms the part of the present invention, and in the present invention, it is attached
Figure is showed by the explanation of the specific embodiment to implementing the present invention.It should be understood that without departing from the scope of the present invention
In the case of can use other embodiments and can carry out change in structure or in logic.For example, for an implementation
The feature released or described is illustrated to can be used for other embodiments or combined with other embodiments to generate another embodiment.It is anticipated
Figure is that the present invention includes such modifications and variations.These examples are described with specific sentence, but they are understood not to
Limitation to scope of the appended claims.Accompanying drawing is merely for explanatory purpose and is not necessarily drawn to scale.Unless stated otherwise,
For purposes of clarity, corresponding element is adopted in different drawings is indicated by the same numerals.
Term " has ", " containing ", " including ", and " including " etc. is open, and they represent described structure, element
Or the presence of feature, but it is not precluded from additional element or feature.
By taking any CPU in multi-core CPU as an example, when accessing resource, generation is provided from the spin lock that body uses, and defines CPU
Spin lock be LOCK, its structure includes two internal storage locations, as shown in figure 1, pLock units and pNext units.PLock is mono-
Member is used to identify lock token, and pNext units are used to preserve next lock address.
Define a resource lock Lock_g for shared resource (such as internal memory or display) (can set to different shared resources
Fixed different lock, add unblock principle the same, here only by taking one of resource as an example), the resource lock also includes as shown in Figure 1
Two memory cell, pLock units and pNext units.
By taking CPU spin locks LOCK as an example, the implementation of C language described below, it can be stated with following expression:
It will be appreciated by those skilled in the art that CPU spin lock construction and shared resource resource lock statement not
It is limited to use C language.
Write for convenience, take the content of unit in the present invention with [] expression, such as [pLock] represents to take in Fig. 1 in pLock
The content of memory cell, & expressions take the address of internal storage location, and Ru &pLock represents to take the memory address of pLock units.When
When [pLock] is not NULL (NULL represents that content is empty), represents that the lock is in the lock state, then represented in solution if NULL
Lock status.Here it is the convenience that is used for the later stage internal memory to be defined as into address type variable.[pNext] is next currently to lock
Individual lock address value, the CPU of the resource access rights to be obtained such as all are automatically composed a single-track link table by this internal storage location.
Embodiment 1
For the cpu system of any one n core, its CPU number is 0~n-1, then arbitrary number such as kth (0≤k≤n-
1) individual CPU, it is as shown in Figure 3 that it obtains the specific implementation process of resource access rights.
Before k-th of CPU accesses resource, initial value is assigned after being initialized to resource lock, as shown in Fig. 2 pLock is entered as
NULL, i.e. memory content are NULL, represent that Current resource is unoccupied.PNext Fu Zhiwei &Lock_g, i.e., resource lock is next
Individual lock address is itself, and therefore, the resource lock after initialization constitutes a unidirectional self-loopa amount chained list, as shown in Figure 2.
K-th of CPU is locked and unlocked when accessing resource.
It is that each shared resource distributes a resource lock according to the method for the present embodiment, each compared to prior art
Resource lock forms unidirectional self-loopa chained list when the shared resource right to use is unoccupied.Each shared resource possesses an overall situation
Lock (resource lock), each CPU core locks spin respective, cache synchronization can be avoided to cause CPU synchronous efficiencies lower, drops
The low complexity of programming, the service efficiency of system shared resource can be improved.
Embodiment 2
Give a kind of specific embodiment of locking and the unblock of embodiment 1 in the present embodiment, the locking of this method,
In releasing process, CPU need not retain itself spin lock when using resource, specifically as shown in Fig. 4.
The first module of k-th of CPU spin lock is set to non-NULL by S101, and second unit is set into empty.
Wherein, the spin lock is formed in storehouse is run.
In programming operation, it can realize as follows:
CPU is stated first when obtaining shared resource access right.Spin lock LOCK categorical variable me is stated, and
Initial value, [pLock]=&me, [pNext]=NULL are assigned to spin lock.
The current queue that the spin lock is placed in acquisition resource by S103 k-th of CPU is last, while obtains above-mentioned spin lock
It is placed in the mark of last lock of queue before queue, i.e., the mark of the previous lock of k-th spin lock.
In programming operation, it can realize as follows:
CPU itself register (exchange register) pReg is stated, and [pReg]=&me is set, and with Lock_g's
PNext elements exchanges.
S105 judges that what is obtained identifies whether as resource lock, if it is, S107 is gone to, if not, going to S112.
In programming operation, it can realize as follows:
Judge whether [pReg] after exchanging is equal to &Lock_g.
S107 judges whether the first internal storage location of the resource lock has been labeled, if marked, poll waits, until
The lock token of the resource lock is eliminated, and goes to S109.
In programming operation, it can realize as follows:
Whether [pLock] for judging Lock_g is empty, if it is not, continue waiting for, until for sky, if so, step goes to S109.
The first internal storage location (informing that other CPU resources are occupied) of S109 markup resources lock, and by resource
Lock is placed in that queue is last, while obtains the mark of last lock of queue before resource lock is placed in after queue.
In programming operation, it can realize as follows:
Lock_g [pLock]=&Lock_g is set, again handed over register pReg and Lock_g pNext units
Change.
The second internal storage location that the lock of acquisition identifies is arranged to the address of resource lock by S111, and goes to S113.
In programming operation, it can realize as follows:
It is &Lock_g to set [pReg] to point to [pNext] content locked.
The second internal storage location for obtaining lock mark is arranged to the CPU spin locks by S112, goes to S113.
In programming operation, it can realize as follows:
Set [pReg] to point to [pNext]=&me of lock, safeguard self-loopa chained list.
S113 judges whether the second internal storage location of the spin lock of k-th of CPU has been arranged to non-NULL, if it is not, then after
Continuous wait until is judged as non-NULL, goes to S115.
In programming operation, it can realize as follows:
Whether [pNext] for judging LOCK categorical variables me is empty.If so, then continue waiting for, until S113 judgement is
S115 is not gone to for sky, step.
S115 returns to the second internal storage location content of the spin lock of k-th of CPU, the mark of next lock in its storage queue
Know.
In programming operation, it can realize as follows:
Return to me [pNext].
Above-mentioned is the locking procedure of the present embodiment, and after the completion of locking procedure, the method for locking returns to current CPU locks
Next lock address.For convenience of expression, the address value Ji Wei &me_next that locking method is returned.Above-mentioned locking procedure ensure that
Return &me_next always effectively locks.
And the unblock includes:
First internal storage location of the mark for the lock that locking procedure in S115 is returned is empty, that is, empties the lock mark of the lock.
[pLock] internal storage location of the pointed lock of return &me_next values need to be only arranged to NULL by releasing process.Nothing
Any other operation need to be carried out.
It should be noted that next lock address of the current lock returned in locking procedure, lock is pointed to when setting it
After pLock internal storage locations is skies, other CPU will obtain resource access right, while the lock may be destroyed, therefore, the lock after unblock
Address can not reuse the lock.From above-mentioned plus releasing process, whole process is realized simply, at most need to only be performed and be deposited twice
Device internal memory exchange command, such as step S103 and S109, the atomic operation without bus positive lock.Its releasing process is more simple
It is single, it is only necessary to some internal storage location value to be assigned empty.
The C language for providing the above method by taking ARM as an example below is realized:
In function is unlocked, pointer corresponding to unblock should be the pointer returned during locking, after calling unlocking method, the pointer
It can not reuse.
Above-mentioned plus releasing process function realizes that compared with K42 locks simply locking procedure is substantially suitable with MCS.Releasing process only needs
One simple assignment operation, than above-mentioned three kinds locks are all simple, and simplify operation without positive lock bus using process
Meanwhile greatly reduce bus holding time.
Embodiment 3
Above-mentioned plus unblock process does not provide the interface that resource is attempted to access, and this method further comprises that one kind is attempted to obtain
The method for taking resources use right to limit, i.e., when resource is occupied, CPU need not wait resource lock to obtain, and can carry out other operations.True
Surely obtain resources use right to prescribe a time limit, just operating method corresponding to progress.
, can be as shown in Figure 5 using the atomic operation of bus hardware locking mode, the flow of operation when attempting to access:
It is assumed that k-th of CPU, which just attempts to obtain, accesses access authorization for resource, that is, initialize return value pRet=&Lock_g, step
Come S201.
S201 locks CPU resource access queue, the CPU not in queue is only before k-th of CPU is obtained and accessed resource
Resource could be accessed when dequeue locks by having.
In programming operation, it can realize as follows:
Hardware lock bus, when forcing other CPU access shared resources, wait bus release.
S203 judges whether there was only resource lock in addition to k-th of CPU in the queue and judges the first internal memory of resource lock
Whether unit is not labeled, if it is, going to S205, if it is not, then releasing the locking to queue, and returns to trial and locks mistake
Lose.
In programming operation, it can realize as follows:
Whether Lock_g [pNext] is judged for &Lock_g, and whether Lock_g [pLock] is empty, if so, then
Turn to S205.If it is not, then unblock bus, and return to trial and lock failure.
First internal storage location of S205 markup resources lock, and the locking to queue is released, resource lock mark is returned, is successfully locked
Determine resource, obtain resource access rights.
In programming operation, it can realize as follows:
Lock_g [pLock] value=&Lock_g is set, unlocks bus, returns to pRet.
When the lock address that the above method returns is not space-time, i.e. expression obtains resource access rights, otherwise represents not obtain.
After gain access, its releasing process is identical with the releasing process in embodiment 2, only need to be corresponding by the lock of return
PLock content elements be empty.
It will be appreciated by those skilled in the art that the method that the trial of the present embodiment accesses can enter one as embodiment 2
Step supplement, the step performed by it are not limited to before and after some step in embodiment 2.
Embodiment 4
, it is necessary to hardware lock bus during the trial of above-described embodiment 3 obtains resource access rights;In addition when money
When source access conflict is more frequent, it is likely that the situation that resource access rights can not obtain for a long time occur, so as to influence performance.For
Reduction such case occurs, and CPU can be taken to lock the mode that team waits and checked obtains resource access rights, its method bag
Containing three processes:
Attempt to obtain and will currently attempt information insertion queue, return to the situation for obtaining authority;
Detect whether gain access;
Terminate to wait, and unlock this visit.
In addition, in locking, unlocking method in embodiment 2, when CPU uses resource, spin lock may be destroyed.Different
It is that in the locking, unlocking method in the present embodiment, when CPU uses resource, the spin lock of itself is not destroyed before unblock.
Wherein attempt to obtain and will currently attempt information insertion queue, return to the situation for obtaining authority, that is, the process locked
As shown in fig. 6, including:
First internal storage location of k-th of CPU spin lock is set to non-NULL and the second internal storage location is set into empty by S301.
In programming operation, it can realize as follows:
CPU lock [pLock] location contents for setting pMe to point to is pMe, [pNext]=NULL.
K-th of CPU spin lock is placed in that queue is last by S303, while the mark that last in queue is locked before obtaining
Know.
In programming operation, it can realize as follows:
State exchange register pReg, set [pReg]=pMe, and with Lock_g pNext elements exchanges.
S305 judges that the lock of the acquisition is identified whether as resource lock, if so, S307 is gone to, if it is not, going to S312.
In programming operation, it can realize as follows:
Judge whether [pReg] after exchanging is equal to &Lock_g, if so, S307 is gone to, if it is not, going to S312.
It is resource lock that S307, which sets the first internal storage location of k-th of CPU spin lock,.
In programming operation, it can realize as follows:
It is &Lock_g to set pMe to point to the pLock units locked.
S309 judges whether the first internal storage location of resource lock is empty, if so, S311 is gone to, if it is not, going to S312.
In programming operation, it can realize as follows:
Whether [pLock] for judging Lock_g is empty, if so, S311 is gone to, if it is not, going to S312.
S311, which is returned, obtains authority success.
S312, which is returned, obtains authority failure.
The process for terminating to attempt is complex, when terminating to attempt, first it is ensured that having obtained resources use right limit, and
After be unlocked.Because during authority to be obtained is waited, CPU locks come into queue, because queue is single-stranded sheet form,
Therefore, dequeue to wait the right to use to be obtained, could dequeue when itself will drain into queue first.
Unblock as shown in figure 8, including
S313 reads the lock mark of the first internal storage location of k-th of CPU spin lock.
In programming operation, it can realize as follows:
Variable pCheck is stated, and is entered as spin lock [pLock] location contents of pMe sensings.
S315 judges that the lock is identified whether as sky, if it is not, S317 is gone to, if so, going to S325.
In programming operation, it can realize as follows:
Judge whether pCheck is empty, if it is not, S317 is gone to, if so, going to S325.
S317 judges whether the first internal storage location of lock mark is empty, if it is not, poll is until being yes, if so, going to
S319。
In programming operation, it can realize as follows:
Whether [pLock] that pCheck points to lock is empty, if it is not, poll is until being yes, if so, going to S319.
S319 judges that the lock identifies whether the spin lock itself for k-th of CPU, if it is not, S321 is gone to, if so, going to
S325。
In programming operation, it can realize as follows:
Judge whether [pCheck] is equal to [pMe], if it is not, S321 is gone to, if so, going to S325.
The occupancy mark of resource lock is set to non-NULL by k-th of CPU of S321.
In programming operation, it can realize as follows:
Variable pReg is stated, and is entered as pCheck, is set to point to spin lock [pLock]=pCheck.
Resource lock is placed in that queue is last by S323, at the same before obtaining last lock of queue occupancy mark.
In programming operation, it can realize as follows:
By pReg and pCheck pNext elements exchanges, pReg is set to point to [pNext]=pCheck of lock after exchange.
S325 judges that occupancy of last lock of queue before this is identified whether as sky, if so, then poll until being no, if
It is no, then go to S327.
In programming operation, it can realize as follows:
Whether [pNext] for judging pMe is empty (i.e. whether queue is ready), if so, then poll is until being no, if it is not, then
Go to S327.
First internal storage location of k-th of CPU next lock is set to empty by S327 so that the occupancy mark quilt of next lock
Remove.
In programming operation, it can realize as follows:
The pLock contents for setting the lock of pMe [pNext] sensing are NULL.
Check whether pCheck is equal with pMe in above-mentioned flow, in the case where not waiting, pCheck is global money
&Lock_g is locked in source, when terminating to wait, adds the waiting process for obtaining resource lock authority, i.e., checks pCheck locks for the first time
PLock location contents whether be empty.In above three interface, the self-defined locks of CPU are that the lock that pMe is pointed to must be prior to adjusting
Stated with interface, and before resource access rights are abandoned, the lock can not be destroyed.
The implementation example of the interface given below in ARM:
In the method for waiting cpu resource access rights to be obtained, CPU can not have to the acquisition of authority of waiting indefinitely, so as to
Other operations are carried out during authority is obtained, so as to improve CPU utilization rate.
Embodiment 5
After the step S311 or S312 of embodiment 4, the present embodiment also provides whether a kind of confirmation CPU has obtained resource
The procedure of access rights, check whether that the flow for obtaining resource access rights is relatively simple, need to only check that pMe points to lock
PLock unit contents, as shown in Figure 7:
S401 reads the lock mark of the first internal storage location of k-th of CPU spin lock.
In programming operation, it can realize as follows:
Variable pCheck is stated, and is entered as spin lock [pLock] location contents of pMe sensings.
S403 judges that the lock is identified whether as sky, and authority success is obtained if so, returning;If it is not, go to S405.
In programming operation, it can realize as follows:
Judge whether pCheck is empty, authority success is obtained if so, returning;If it is not, go to S405.
S405 judges whether the first internal storage location of lock mark is empty, and authority success is obtained if so, returning;If it is not, turn
To S407.
In programming operation, it can realize as follows:
Judge whether [pLock] that pCheck points to lock is empty, and authority success is obtained if so, returning;If it is not, go to
S407。
S407, which is returned, obtains authority failure.
The present invention is described above by reference to embodiments of the invention.But these embodiments are used for the purpose of saying
Bright purpose, and be not intended to limit the scope of the present invention.The scope of the present invention is limited by appended claims and its equivalent.
The scope of the present invention is not departed from, those skilled in the art can make a variety of substitutions and modifications, and these substitutions and modifications should all fall
Within the scope of the present invention.
Claims (5)
- A kind of 1. implementation method of spin lock when multi-core CPU accesses resource, it is characterised in that includingResource to be visited has resource lock, and for any CPU when accessing resource, generation is provided from the spin lock that body uses, resource lock Internal storage structure with spin lock is including for identifying the first internal storage location of lock token and for preserving next lock ground Second internal storage location of location;Before any CPU accesses resource, the first internal storage location of resource lock is arranged to empty, the second internal storage location is arranged to Resource lock itself, resource lock itself is set to form a self-loopa chained list;Any CPU is locked and unlocked when accessing resource.
- 2. the implementation method of CPU spin locks according to claim 1, it is characterised in thatThe locking includes:The first module of the spin lock of any CPU is set to non-NULL by S101, and second unit is set into empty;The current queue that the spin lock is placed in acquisition resource by S103 any CPU is last, while obtains the previous of the spin lock The mark of individual lock;S105 judges that what is obtained identifies whether as resource lock, if it is, S107 is gone to, if not, going to S112;S107 judges whether the first internal storage location of the resource lock has been labeled, if marked, poll waits, until the money The lock token of source lock is eliminated, and goes to S109;First internal storage location of S109 markup resources lock, and it is last that resource lock is placed in into queue, while obtain resource lock and be placed in team The mark of last lock of queue before after row;The second internal storage location that the lock of acquisition identifies is arranged to the address of resource lock by S111, and goes to S113;The second internal storage location for obtaining lock mark is arranged to the CPU spin locks by S112, goes to S113;S113 judges whether the second internal storage location of the spin lock of any CPU has been arranged to non-NULL, if it is not, then continuing Treat, until being judged as non-NULL, go to S115;S115 returns to the second internal storage location content of the spin lock of any CPU, the mark of next lock in its storage queue;And the unblock includes:First internal storage location of the mark for the lock that locking procedure in S115 is returned is empty.
- 3. the implementation method of CPU spin locks according to claim 1 or 2, it is characterised in that further comprise attempting to lock Process, including:S201 locks CPU resource access queue before any CPU is obtained and accessed resource, and the CPU not in queue only exists Dequeue could access resource when locking;S203 judges whether there was only resource lock in addition to any CPU in the queue and judges the first internal storage location of resource lock Whether it is not labeled, if it is, going to S205, if it is not, then releasing the locking to queue, and returns to trial and lock failure;First internal storage location of S205 markup resources lock, and the locking to queue is released, resource lock mark is returned to, successfully locking money Source, obtain resource access rights.
- 4. the implementation method of CPU spin locks according to claim 1, it is characterised in that also include:When any CPU accesses resource, the spin lock of its own is not destroyed before unblock,Wherein locking includes:First internal storage location of the spin lock of any CPU is set to non-NULL and the second internal storage location is set into empty by S301;The spin lock of any CPU is placed in that queue is last by S303, while the mark that last in queue is locked before obtaining;S305 judges that the lock of the acquisition is identified whether as resource lock, if so, S307 is gone to, if it is not, going to S312;It is resource lock that S307, which sets the first internal storage location of the spin lock of any CPU,;S309 judges whether the first internal storage location of resource lock is empty, if so, S311 is gone to, if it is not, going to S312;S311, which is returned, obtains authority success;S312, which is returned, obtains authority failure,And unlocking includesS313 reads the lock mark of the first internal storage location of the spin lock of any CPU;S315 judges that the lock is identified whether as sky, if it is not, S317 is gone to, if so, going to S325;S317 judges whether the first internal storage location of lock mark is empty, if it is not, poll is until being yes, if so, going to S319;S319 judges that the lock identifies whether the spin lock itself for any CPU, if it is not, S321 is gone to, if so, going to S325;The occupancy mark of resource lock is set to non-NULL by S321 any CPU;Resource lock is placed in that queue is last by S323, at the same before obtaining last lock of queue occupancy mark;S325 judges that occupancy of last lock of queue before this is identified whether as sky, if so, then poll until being no, if it is not, Then go to S327;First internal storage location of next lock of any CPU is set to empty by S327 so that the occupancy mark of next lock is clear Remove.
- 5. CPU spin locks implementation method as claimed in claim 4, after S311 or S312, this method further comprises really Recognize the procedure whether CPU has obtained resource access rights:S401 reads the lock mark of the first internal storage location of the spin lock of any CPU;S403 judges that the lock is identified whether as sky, and authority success is obtained if so, returning;If it is not, go to S405;S405 judges whether the first internal storage location of lock mark is empty, and authority success is obtained if so, returning;If it is not, go to S407;S407, which is returned, obtains authority failure.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201710376711.2A CN107357648B (en) | 2017-05-25 | 2017-05-25 | Method for realizing spin lock when multi-core CPU accesses resources |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201710376711.2A CN107357648B (en) | 2017-05-25 | 2017-05-25 | Method for realizing spin lock when multi-core CPU accesses resources |
Publications (2)
Publication Number | Publication Date |
---|---|
CN107357648A true CN107357648A (en) | 2017-11-17 |
CN107357648B CN107357648B (en) | 2020-05-15 |
Family
ID=60271306
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201710376711.2A Active CN107357648B (en) | 2017-05-25 | 2017-05-25 | Method for realizing spin lock when multi-core CPU accesses resources |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN107357648B (en) |
Cited By (6)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109558241A (en) * | 2018-11-13 | 2019-04-02 | 锐捷网络股份有限公司 | A kind of method and device obtaining spin lock |
CN109901880A (en) * | 2019-02-28 | 2019-06-18 | 福州瑞芯微电子股份有限公司 | A kind of spinlock hardware circuit and electronic equipment |
CN110362348A (en) * | 2018-04-09 | 2019-10-22 | 武汉斗鱼网络科技有限公司 | A kind of method, apparatus and electronic equipment of queue access data |
CN111124979A (en) * | 2019-10-31 | 2020-05-08 | 苏州浪潮智能科技有限公司 | Stack structure-based I2C multi-master access method and system |
CN113448878A (en) * | 2021-05-27 | 2021-09-28 | 山东英信计算机技术有限公司 | LBA processing method, system, storage medium and equipment |
CN113836209A (en) * | 2021-09-02 | 2021-12-24 | 深圳市道通智能汽车有限公司 | Shared resource access method of multi-core processor, processor and multi-core system |
Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20040128663A1 (en) * | 2002-12-31 | 2004-07-01 | Efraim Rotem | Method and apparatus for thermally managed resource allocation |
US20070261057A1 (en) * | 2006-05-04 | 2007-11-08 | Sun Microsystems, Inc. | Multi-threaded shared state variable control |
US20080184258A1 (en) * | 2007-01-30 | 2008-07-31 | Keisuke Toyama | Data processing system |
CN101256509A (en) * | 2008-04-07 | 2008-09-03 | 中兴通讯股份有限公司 | Locking method, unlocking method and implementing method for lock mechanism |
CN102609313A (en) * | 2012-01-13 | 2012-07-25 | 汉柏科技有限公司 | Multi-core system resource delay recycle lock realizing method and multi-core system resource delay recycle lock realizing system |
CN104834505A (en) * | 2015-05-13 | 2015-08-12 | 华中科技大学 | Synchronization method for NUMA (Non Uniform Memory Access) sensing under multi-core and multi-thread environment |
CN105975341A (en) * | 2016-04-12 | 2016-09-28 | 苏州万特锐仪器有限公司 | Multicore processor-oriented internal memory management system |
-
2017
- 2017-05-25 CN CN201710376711.2A patent/CN107357648B/en active Active
Patent Citations (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20040128663A1 (en) * | 2002-12-31 | 2004-07-01 | Efraim Rotem | Method and apparatus for thermally managed resource allocation |
US20070261057A1 (en) * | 2006-05-04 | 2007-11-08 | Sun Microsystems, Inc. | Multi-threaded shared state variable control |
US20080184258A1 (en) * | 2007-01-30 | 2008-07-31 | Keisuke Toyama | Data processing system |
CN101256509A (en) * | 2008-04-07 | 2008-09-03 | 中兴通讯股份有限公司 | Locking method, unlocking method and implementing method for lock mechanism |
CN102609313A (en) * | 2012-01-13 | 2012-07-25 | 汉柏科技有限公司 | Multi-core system resource delay recycle lock realizing method and multi-core system resource delay recycle lock realizing system |
CN104834505A (en) * | 2015-05-13 | 2015-08-12 | 华中科技大学 | Synchronization method for NUMA (Non Uniform Memory Access) sensing under multi-core and multi-thread environment |
CN105975341A (en) * | 2016-04-12 | 2016-09-28 | 苏州万特锐仪器有限公司 | Multicore processor-oriented internal memory management system |
Cited By (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110362348A (en) * | 2018-04-09 | 2019-10-22 | 武汉斗鱼网络科技有限公司 | A kind of method, apparatus and electronic equipment of queue access data |
CN109558241A (en) * | 2018-11-13 | 2019-04-02 | 锐捷网络股份有限公司 | A kind of method and device obtaining spin lock |
CN109901880A (en) * | 2019-02-28 | 2019-06-18 | 福州瑞芯微电子股份有限公司 | A kind of spinlock hardware circuit and electronic equipment |
CN109901880B (en) * | 2019-02-28 | 2020-11-20 | 瑞芯微电子股份有限公司 | Spinlock hardware circuit and electronic equipment |
CN111124979A (en) * | 2019-10-31 | 2020-05-08 | 苏州浪潮智能科技有限公司 | Stack structure-based I2C multi-master access method and system |
CN113448878A (en) * | 2021-05-27 | 2021-09-28 | 山东英信计算机技术有限公司 | LBA processing method, system, storage medium and equipment |
CN113836209A (en) * | 2021-09-02 | 2021-12-24 | 深圳市道通智能汽车有限公司 | Shared resource access method of multi-core processor, processor and multi-core system |
Also Published As
Publication number | Publication date |
---|---|
CN107357648B (en) | 2020-05-15 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN107357648A (en) | The implementation method of spin lock when a kind of multi-core CPU accesses resource | |
CN101183338B (en) | Local piece loading system and method for maintaining storage coherence in the same | |
US4847754A (en) | Extended atomic operations | |
CN1222890C (en) | Dynamic serializing of memory access in multiprocessor system | |
CN104050091B (en) | The network equipment and its method to set up of system are accessed based on Non Uniform Memory Access | |
CN101253483B (en) | Method and system for managing a resource lock | |
US20080288691A1 (en) | Method and apparatus of lock transactions processing in single or multi-core processor | |
US20060143415A1 (en) | Managing shared memory access | |
CN101566977B (en) | Method, device and system of processor accessing shared data | |
CN110442462B (en) | Multithreading data transmission method and device in TEE system | |
US20200034214A1 (en) | Method for arbitration and access to hardware request ring structures in a concurrent environment | |
EP1862907A2 (en) | Cache memory device and caching method | |
CN100367218C (en) | Multi-kernel parallel first-in first-out queue processing system and method | |
WO2014021879A1 (en) | Systems and methods for controlling access to a shared data structure with reader-writer locks using multiple sub-locks | |
CN102566970B (en) | For processing the data processor modifying instruction with cache bypass | |
US8799591B2 (en) | Read-write spinlock | |
CN103034593A (en) | Multi--core processor oriented on-chip lock variable global addressing storage method and device | |
US20060048162A1 (en) | Method for implementing a multiprocessor message queue without use of mutex gate objects | |
CN109558241A (en) | A kind of method and device obtaining spin lock | |
CN109597697A (en) | A kind of resource brings processing method and processing device together | |
CN109117189A (en) | Data processing method, device and computer equipment | |
CN107003954A (en) | Synchronization in computing device | |
CN100573489C (en) | DMAC issue mechanism via streaming ID method | |
US6981108B1 (en) | Method for locking shared resources connected by a PCI bus | |
CN106572036A (en) | SKB management method and apparatus |
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 | ||
TR01 | Transfer of patent right | ||
TR01 | Transfer of patent right |
Effective date of registration: 20211206 Address after: 102208 No. 78, East District, 15 / F, block D, main building, No. 2, Beinong Road, zhuxinzhuang, Huilongguan town, science and Technology Park, Changping District, Beijing Patentee after: Beijing Zhimeng ICT Co.,Ltd. Address before: 100043 Jingyan Hotel, Shijingshan District, Beijing Patentee before: Lv Jinbai |