Guarantees a critical section from being experiencing race conditions.
Rules of mutual exclusion:
Additional rule
void CriticalSection(){
static uBaseTask * curr; // shared
curr = &uThisTask();
for(int i = 1; i <= 100; i += 1){
if(curr != &uThisTask()){
abort("interference");
}
}
}
enum Intent {WantIn, DontWantIn};
Intent * Last; // who was in last time
_Tast Dekker {
Intent &me, &you;
void main(){
for(i = 1~1000){
for(;;){
me = WantIn;
if(you == DontWantIn) break; // no one is awaiting for use
if(::Last == &me){ // I used last time, so I wait this time
me = DontWantIn;
while(::Last == &me && you == WantIn){}
// shouldn't it set me=WantIn???
}
}
... // Do Something In Critical Section
if(::Last != &me) ::Last = &me;
me = DontWantIn;
}
}
public:
Dekker(Intent & me, Intent &you) : me(me), you(you) {}
};
This algorithm works even if there is no atomic read/write! - RW-safe
enum Intent {WantIn, DontWantIn};
Intent * Last; // who was in last time
_Tast Peterson {
Intent &me, &you;
void main(){
for(i = 1~1000){
me = WantIn;
::Last = &me; //RACE!
while(you == WantIn && ::Last == &me){}
... // Do Something In Critical Section
me = DontWantIn;
}
}
public:
Peterson(Intent & me, Intent &you) : me(me), you(you) {}
};
This is built based upon a tiny bit of ATOMICITY - atomic read/write!
So its is not RW-safe because it write at ::Last at the same time.
some midterm question
they will give you algorithm, then
what would work if some lines gets inter changed.