Background technology
Flush bonding processor can connect various peripheral hardwares and realize various functions, though peripheral functionality has nothing in common with each other, flush bonding processor has only two kinds usually with the handshake method of peripheral hardware, and a kind of is inquiry mode, and a kind of is interrupt mode.In the system that has used inquiry mode work, may cause deadlock because of various reasons, guarantee that system is not considerable by deadlock so design reliable software.
For deadlock, different fields can provide different definition, is meant mainly that here flush bonding processor can not do other thing at the waiting facilities complete operation always.The necessary condition of deadlock is concluded following several:
1, mutual exclusion condition: a resource can only be used by a process at every turn.
2, request and conservation condition: when a process is blocked because of request resource, acquired resource is kept not putting.
3, do not deprive condition: the acquired resource of process, before the end uses, can not deprive by force.
4, circular wait condition: form a kind of connected head-to-tail circular wait resources relationship between some processes.
In some built-in system software, above-mentioned " process " can be understood as the particular code (being referred to as task in the description of back) of task or certain section access shared resources.Along with the development of software engineering, solving shared resource access conflict problem has had a variety of methods, such as disabled interrupt, utilizes the mutex amount and uses various resource locks or the like.How to use to different tasks the shared resource reasonable distribution but these methods all are intended to solve, guarantee that task can not cause deadlock because of attempting to obtain certain shared resource.
And in embedded system, task also might occur and obtain shared resource and the phenomenon of deadlock.Possible reason is:
1, shared device breaks down, and then operates complement mark and just may not can be set, and corresponding task will be in waiting status always and cause deadlock.
2, when a task when whether inquiry shared device operation complement mark is put 1, take place one and interrupted or produced task scheduling, this interruption or the new task of carrying out that is scheduled also will be visited this shared device by chance, and Interrupt Service Routine or the new task that is scheduled can be removed shared device operation complement mark (this is the equipment necessary condition of operate as normal again) after having visited shared device, rescheduled and cause before inquiring about shared device operation complement mark 1 the task of whether being put and continue to begin to carry out interrupting returning back or task so, go one of cyclic query can not be put 1 shared device operation complement mark again, then this task is just by deadlock.For example, a task need be gathered certain road simulating signal, after having started analog to digital converter, task begins to wait for that the analog to digital conversion complement mark is set, having taken place one this moment interrupts or task scheduling, also to use same analog to digital converter to gather certain road simulating signal in the new task that happens to be in the Interrupt Service Routine or be scheduled, analog to digital converter will be restarted so, behind EOC, convert and indicate that being shared equipment puts 1 automatically, interruption routine or the new task that is scheduled detect this sign and are set this sign of back removing, carry out other operation then, final interruption returns or task can be rescheduled, and the task of making the set of previous wait analog to digital conversion complement mark obtains continuing to carry out, but this moment, analog to digital converter was in idle state, converted sign and also just can not be set, and this task goes down to cause deadlock with Beyond so.This situation is more hidden, is difficult for being taken into account at the beginning of software design.
In sum, the peripheral hardware of embedded system belongs to hardware resource, and hardware breaks down and may cause system deadlock; The peripheral hardware of embedded system is mutual exclusion, and is imprescriptible, and its inherent characteristic has caused the necessary condition of deadlock; Conventional method can only solve the shared resource assignment problem, promptly can only solve the problem before the resources allocation, and the task in the embedded system of can not solving regains the problem that causes deadlock after the shared device control.
Summary of the invention
The invention provides a kind of main frame of avoiding and cause the method for unusual deadlock at the visit shared device, the problem of deadlock takes place in the task that can solve after having obtained the shared device control.
For reaching goal of the invention, the technical solution adopted in the present invention is:
A kind of main frame of avoiding causes the method for unusual deadlock at the visit shared device, is applicable to embedded system, may further comprise the steps:
A, an access conflict sign is set for described shared device;
B, a low priority task start described shared device;
C, described low priority task are inquired about described shared device operation complement mark and whether are put 1, if described shared device operation complement mark is put 1, then go to step D, otherwise go to step e;
D, described low priority task are finished the visit of described shared device, and described shared device operation complement mark is clear 0, and process ends;
E, described low priority task are inquired about described shared device access conflict sign and whether are put 1, if described shared device access conflict sign is put 1, then with its clear 0 and go to step B, retry is visited described shared device, otherwise goes to step C;
F, when described low priority task during in execution in step B, step C or step e, Interrupt Service Routine or high-priority task have interrupted the execution of described low priority task and have visited described shared device, then in described Interrupt Service Routine or high-priority task, start shared device, and go to step G;
G, described Interrupt Service Routine or high-priority task are inquired about described shared device operation complement mark and whether are put 1, if described shared device operation complement mark is put 1, then go to step H, otherwise re-execute step G;
H, described Interrupt Service Routine or high-priority task are clear 0 with described shared device operation complement mark, and shared device access conflict sign puts 1, and interrupt returning or task reschedules, and described low priority task continues execution in step B, step C or step e.
In task of visiting shared device or Interrupt Service Routine, the time-out count value is set, when the number of times of inquiring about described shared device operation complement mark when described task or Interrupt Service Routine surpasses described time-out count value, then withdraw from the inquiry circulation, carry out time-out error and handle.
In the low priority task of visit shared device the number of retries value is set, when the number of times of visiting described shared device when described low priority task surpassed described number of retries value, the collision error that conducts interviews was handled.
Described shared device is to have the peripherals that the hardware flags mirror operation has been finished, and comprises timer, serial ports controller, iic bus controller, spi bus controller, usb bus controller, PWM controller, comparer, analog to digital converter, digital to analog converter in the embedded system.
Described embedded system is the AM/BAM embedded system, or has the embedded system of operating system.
Adopted technical scheme of the present invention, can solve the problem that the visit shared device that causes owing to hardware or software reason causes the task deadlock, the problem of deadlock takes place in the task that also can solve after having obtained the shared device control, and the realization of method is simple, reliable, it is not high to be specially adapted to processor performance, the embedded system field of memory resource anxiety.
Embodiment
The present invention is further described below in conjunction with accompanying drawing and embodiment.
Fig. 1 avoids visiting the process flow diagram that shared device causes deadlock among the present invention.As shown in Figure 1, this flow process may further comprise the steps:
Step 101, in program, a global access collision flag is set for shared device.
Step 102, the number of retries value of shared device visit is set in low priority task.
Step 103, in low priority task, start shared device and the time-out count value is set.
Step 104, whether inquiry shared device operation complement mark is put 1 in low priority task, if this sign is put 1, then with its clear 0, and with shared device access conflict sign clear 0, represent that described low priority task completes successfully described shared device visit, can carry out other operation in the low priority task, otherwise go to step 105.
Step 105, detect in low priority task whether the time-out count value is 0, if be 0, represent that described low priority task inquires about described shared device operation complement mark number of times greater than maximum number of times of defined, described low priority task should be carried out corresponding time-out error processing, otherwise goes to step 106.
Step 106, in low priority task, detect shared device access conflict sign and whether put 1, if do not put 1, go to step 104 after then the time-out count value being subtracted 1, reenter the circulation of the described shared device operation of inquiry complement mark, otherwise go to step 107.
Step 107, detect in low priority task whether described shared device visit number of retries value is 0, if be 0, show that described low priority task has surpassed the maximum times of defined to the access times of described shared device, and access conflict has all taken place at every turn (during to be described low priority task after starting described shared device to shared device finish assigned operation, there are Interrupt Service Routine or high-priority task to interrupt the execution of low priority task, and visited described shared device), then described low priority task should be carried out corresponding access conflict fault processing, and with described shared device access conflict sign clear 0, otherwise described number of retries value is subtracted 1, and with described shared device access conflict sign clear 0, go to step 103 then, carry out the circulation of retry visit shared device.
If step 108, after low priority task starts shared device, during shared device is finished assigned operation, having Interrupt Service Routine or high-priority task to visit shared device, described low priority task is interrupted, thereby go to step 109, enter in the execution in step of Interrupt Service Routine or high-priority task.
Step 109, the time-out count value is set in Interrupt Service Routine or high-priority task, goes to step 110 then.
Step 110, in Interrupt Service Routine or high-priority task, start shared device, go to step 111 then.
Step 111, whether inquiry shared device operation complement mark is put 1 in Interrupt Service Routine or high-priority task, if this sign is put 1, then goes to step 114, otherwise goes to step 112.
Step 112, in Interrupt Service Routine or high-priority task, detect whether the time-out count value is 0, if be 0, represent that described Interrupt Service Routine or high-priority task inquire about described shared device operation complement mark number of times greater than maximum number of times of defined, described Interrupt Service Routine or high-priority task should be carried out corresponding time-out error and be handled, go to step 113, otherwise after the time-out count value subtracted 1, go to step 111.
Step 113, in Interrupt Service Routine or high-priority task, carry out shared device visit time-out error and handle, go to step 114 then.
Step 114, in Interrupt Service Routine or high-priority task, it is clear 0 that shared device is operated complement mark, and shared device access conflict sign is put 1, carries out other operation in Interrupt Service Routine or the high-priority task then.
Step 115, along with interrupting returning or the rescheduling of task, before the low priority task that is interrupted will continue from being interrupted execution, promptly go to step 104, step 105, step 106 or step 107, as previously mentioned, low priority task will detect shared device access conflict sign and be put 1, thereby carry out the retry operation of shared device visit, and avoided the generation of deadlock.
Under the guidance of above-mentioned steps, can draw embodiment at different embedded systems.
Describe below and avoid visiting the embodiment that shared device causes deadlock in a kind of AM/BAM embedded system.In the AM/BAM embedded system, background program all may be interrupted by foreground program at any time, so background program belongs to the code that may lose the shared device control (being the described low priority task of avoiding visiting in the process step that shared device causes deadlock), need detect shared device access conflict sign in inquiry shared device operation complement mark.Foreground program belongs to and has the code of seizing shared device control ability (being the described Interrupt Service Routine of avoiding visiting in the process step that shared device causes deadlock), if system allows interrupt nesting, and there are a plurality of interruptions to share an equipment, so, foreground program also belongs to the code that may lose the shared device control (being the described low priority task of avoiding visiting in the process step that shared device causes deadlock), so foreground program need be after having visited shared device set shared device access conflict sign, and determine whether that according to actual conditions needs detect shared device access conflict sign in inquiry shared device operation complement mark.
This embodiment is considered a kind of situation widely, in the AM/BAM embedded system, there are a background program and two foreground programs (foreground program 1 and foreground program 2) to share an equipment, this system allows interrupt nesting, and the interrupt priority level of foreground program 2 is higher than the interrupt priority level of foreground program 1.
Background program in the above-mentioned AM/BAM embedded system belongs to the code that may lose the shared device control, foreground program 1 had not only belonged to the code that may lose the shared device control but also had belonged to and had the code of seizing shared device control ability, and foreground program 2 belongs to and has the code of seizing shared device control ability.Provide the C language reference code of each program below respectively.
The background program identifying code:
#define?MaxLoopCycles?1000
#define?MaxRetryCycles?2
Unsigned char LootFlag; // 1. define shared device access conflict sign, be a global variable
void?BackgroundXxx(void)
{
unsigned?int?loop_count=MaxLoopCycles;
unsigned?char?retry_count=MaxRetryCycles;
while(retry_count)
{
StartDevice (); // 2. starting outfit, retry operation is also wanted restart facility
while(DCFlag==0&&LootFlag==0&&loop_count!=0)//③
During query facility operation complement mark DCFlag
The test access of { // simultaneously collision flag LootFlag and
Loop_count--; // time-out count value loop_count
}
=0) // 4. the operation of equipment complement mark is by normal set
{
DCFlag=0; // then will operate the complement mark zero clearing
LootFlag=0; // with the zero clearing of access conflict sign
Break; // and withdraw from the retry circulation
}
=0) if // 5. access conflict taken place, promptly in the wait process by before
The platform program interrupts
{
LootFlag=0; // then with access flag clear 0
Retry_count--; // number of retries subtracts 1
Continue; // and carry out retry
}
If (loop_count==0) if // 6. visit overtime
{
Break; // then carry out time-out error to handle and withdraw from the retry circulation
}
}
If (retry_count==0) if // 7. in the number of retries of regulation, device access is not still
Corresponding fault processing is then carried out in success
{
}
}
The identifying code of foreground program 1:
void?ISR1Xxx(void)
{
unsigned?int?loop_count=MaxLoopCycles;
unsigned?char?retry_count=MaxRetryCycles;
while(retry_count)
{
StartDevice (); // 2. starting outfit, retry operation is also wanted restart facility
while(DCFlag==0&&LootFlag==0&&loop_count!=0)//③
During query facility operation complement mark DCFlag
The test access of { // simultaneously sign LootFlag and
Loop_count--; // time-out count value loop_count
}
=0) // 4. the operation of equipment complement mark is by normal set
{
DCFlag=0; // then will indicate zero clearing
LootFlag=1; // 8. different with background program is, behind the normal complete operation of equipment
Need be the access conflict flag set
Break; // and withdraw from the retry circulation
}
=0) if // 5. access conflict taken place, promptly in the wait process by before
The platform program interrupts
{
LootFlag=0; // then with access flag clear 0
Retry_count--; // number of retries subtracts 1
Continue; // and carry out retry
}
If (loop_count==0) if // 6. visit overtime
{
Break; // then carry out time-out error to handle and withdraw from the retry circulation
}
}
If (retry_count==0) if // 7. in the number of retries of regulation, device access is not still
Corresponding fault processing is then carried out in success
{
}
}
The identifying code of foreground program 2:
void?ISR2Xxx(void)
{
unsigned?int?loop_count=MaxLoopCycles;
StartDevice (); // 1. starting outfit
While (DCFlag==0﹠amp; ﹠amp;=0) // 2. query facility is operated complement mark
During DCFlag
{ // simultaneously detects time-out count value loop_count
loop_count--;
}
If (loop_count==0) if // 3. visit overtimely, then carry out time-out error and handle and withdraw from heavily
The examination circulation
{
}
DCFlag=0; // 4. the operation of equipment complement mark puts 0
LootFlag=1; // 5. access flag puts 1
}
Even this embodiment can avoid issuing at extreme case the phenomenon of life and death lock.Fig. 2 is that the specific embodiment of the invention one avoids visiting the process flow diagram that shared device causes deadlock in the AM/BAM embedded system.As shown in Figure 2, at t
0Constantly, whether background program inquiry shared device operation complement mark DCFlag after starting shared device is put 1, shared device access conflict sign LootFlag whether put 1 and time-out count value loop_count interruption has taken place when whether being 0 (3. the background program note is located), this interruption is the interruption of foreground program 1, then foreground program 1 begins to carry out, shared device operation this moment complement mark DCFlag is not also put 1, and shared device access conflict sign LootFlag also is 0.At t
1Constantly, whether inquiry shared device operation complement mark DCFlag was put 1 after foreground program 1 started shared device, shared device access conflict sign LootFlag whether put 1 and time-out count value loop_count interruption has taken place again when whether being 0 (3. foreground program 1 note is located), system allows interrupt nesting, and this interruption is the interruption of foreground program 2, then foreground program 2 will begin to carry out, shared device operation this moment complement mark DCFlag is not yet put 1, and shared device access conflict sign LootFlag still is 0.Foreground program 2 starts shared device and carries out smoothly afterwards, successfully inquire operation of equipment complement mark DCFlag put 1 after with its clear 0 (4. foreground program 2 notes is located), and shared device access conflict sign LootFlag put 1 (5. foreground program 2 notes are located), then at t
2Constantly turn back to foreground program 1 interrupted place (3. foreground program 1 note is located), if shared device access conflict this moment sign LootFlag is not put 1, so since shared device operation complement mark DCFlag in foreground program 2 the insides by clear 0, foreground program 1 will detect shared device operation complement mark DCFlag never and be put 1, thereby cause deadlock.But put 1 owing to used method of the present invention, foreground program 1 to detect shared device access conflict sign LootFlag, foreground program 1 will withdraw from the inquiry circulation so, and carry out retry operation.Suppose in retry operation, not interrupted by foreground program 2, foreground program 1 will be carried out smoothly so, after being finished, shared device operated clear 0 (4. foreground program 1 note is located) of complement mark DCFlag, and shared device access conflict sign LootFlag put 1 (8. foreground program 1 note is located), then at t
3Constantly be back to background program.Same, if background program does not detect shared device access conflict sign LootFlag, will cause deadlock.But put 1 owing to used method of the present invention, background program to detect shared device access conflict sign LootFlag, background program will withdraw from the inquiry circulation so, and carry out retry operation.Suppose in the background program retry operation, not interrupt, background program will be carried out smoothly so, successfully inquire shared device operation complement mark DCFlag put 1 after with its clear 0, and with shared device access conflict sign clear 0, and then carry out other operation, so just avoided the generation of deadlock.
Of particular note, the time-out count value will be set as the case may be, and assurance equipment is under the situation of normal complete operation, and the time-out count value can not reduce to 0.In addition, because shared device access conflict sign LootFlag is a global variable, belong to the software shared resource, if system can not guarantee can be finished in an instruction to the visit of this sign, need to use extra method to visit this global variable so, such as methods such as use mutex amounts.
The embodiment that solves the deadlock problem in a kind of embedded system with operating system is described below.In the embedded system of operating system is arranged, the task that task may newly be dispatched interrupts, so task had not only belonged to the code that may lose the shared device control (being the described low priority task of avoiding visiting in the process step that shared device causes deadlock) but also had belonged to and have the code of seizing shared device control ability (being the described high-priority task of avoiding visiting in the process step that shared device causes deadlock), whether put 1 o'clock at task inquiry shared device operation complement mark, need to detect shared device access conflict sign, and after task has been visited shared device, need shared device access conflict sign is put 1.
In the embedded system of operating system is arranged, (Interrupt Service Routine herein is the Interrupt Service Routine in the particular system to Interrupt Service Routine, distinguish to some extent with the described Interrupt Service Routine of avoiding visiting in the process step that shared device causes deadlock) with the foreground program in the AM/BAM embedded system, need after having visited shared device, shared device access conflict sign be put 1, and according to whether having nested interruption visit shared device to determine whether needs detect shared device access conflict sign when whether inquiry shared device operation complement mark is put 1.
This embodiment is considered a kind of situation widely, in the embedded system of operating system is arranged, two same Interrupt Service Routines of task are shared an equipment, and Fig. 3 is that the specific embodiment of the invention two avoids visiting the process flow diagram that shared device causes deadlock in the embedded system of operating system is arranged.As shown in Figure 3, task 1 and task 2 had not only belonged to the code that may lose the shared device control but also had belonged to and had the code of seizing shared device control ability, and Interrupt Service Routine belongs to and has the code of seizing shared device control ability.Task 1 is just identical with the form of above-mentioned foreground program 1 with task 2 so, and Interrupt Service Routine is just identical with the form of above-mentioned foreground program 2.Just no longer provided code sample herein.When access conflict takes place, its treatment scheme is similar substantially with above-mentioned treatment scheme, unique different be after Interrupt Service Routine is finished, task 1 may be carried out prior to task 2, this moment is because shared device access conflict sign LootFlag is put 1, so task 1 can deadlock, task 2 also can be put 1 and can deadlock because of shared device access conflict sign LootFlag afterwards.
Obviously, those skilled in the art can carry out various changes and modification to the present invention and not break away from the spirit and scope of the present invention.Like this, if of the present invention these are revised and modification belongs within the scope of claim of the present invention and equivalent technologies thereof, then the present invention also is intended to comprise these changes and modification interior.