+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
5384742
master
1package mutex ; 2import dist .*; 3import clocks .*; 4import java .util .*; 5public class RAMutex extends dist .MyProcess implements Lock { 6int myts ; 7LamportClock c =new LamportClock (); 8LinkedList <Integer >pendingQ =new LinkedList <Integer >(); 9int numOkay =0 ; 10public RAMutex (MsgHandler initComm ) { 11super (initComm ); 12myts =Integer .MAX_VALUE ; 13 } 14public synchronized void requestCS () { 15c .tick (); 16myts =c .getValue (); 17sendMsg (neighbors ,"request" ,myts ); 18numOkay =0 ; 19while (numOkay <n -1 ) 20myWait (); 21 } 22public synchronized void releaseCS () { 23myts =Integer .MAX_VALUE ; 24while (!pendingQ .isEmpty ()) 25sendMsg (pendingQ .remove (),"okay" ,c .getValue ()); 26 } 27boolean okayCS () { 28if (myts ==Integer .MAX_VALUE ||numOkay <n -1 )return false ; 29return true ; 30 } 31public synchronized void handleMsg (Msg m ,int src ,String tag ) { 32int timeStamp =m .getMessageInt (); 33c .receiveAction (src ,timeStamp ); 34if (tag .equals ("request" )) { 35if ((timeStamp <myts ) || ((timeStamp ==myts ) && (src <myId ))) 36sendMsg (src ,"okay" ,c .getValue ()); 37else 38pendingQ .add (src ); 39 }else if (tag .equals ("okay" )) 40numOkay ++; 41notifyAll (); 42 } 43}