(Latest Revision: Mon Oct 2 22:08:37 PDT 2000 ) Test-and-Set

EXAMPLE FILE: Test-and-Set


Test-and-Set

This example shows how to use a Test-and-Set instruction to implement a solution to the n-Process critical section problem. This solution satisfies the: criteria.


global shared var waiting : array [0 .. n-1] of boolean ;
                  lock : boolean ;

The code for process #me is:

local var j : 0 .. n-1 ;
       testShowsThingIsStillLocked : boolean ;

repeat
  waiting[me] := true ;
  testShowsThingIsStillLocked := true ;
  while waiting[me] and testShowsThingIsStillLocked
  do testShowsThingIsStillLocked := Test-and-Set(lock) ;
  waiting[me] := false ;

  .. CS(me) ..

  j := (me + 1) mod n ;
  while (j <> me) and (not waiting[j])
  do j := (j + 1) mod n ;
  if    j = me
  then  lock := false
  else  waiting[j] := false ;

  .. RS(me) ..

until  false