+/EE360P
distributed systems
git clone https://git.julia.blue/+/EE360P
7910ba6
master
1package mutex ; 2import dist .*; 3public class CircToken extends MyProcess implements Lock { 4boolean haveToken ; 5boolean wantCS =false ; 6final int leader =0 ; 7public CircToken (MsgHandler initComm ) { 8super (initComm ); 9haveToken = (myId ==leader ); 10// if (haveToken) sendToken(); 11 } 12public void init (MsgHandler app ) { 13super .init (app ); 14if (haveToken )sendToken (); 15 } 16public synchronized void requestCS () { 17wantCS =true ; 18while (!haveToken )myWait (); 19 } 20public synchronized void releaseCS () { 21wantCS =false ; 22sendToken (); 23 } 24public synchronized boolean getHaveToken (){ 25return haveToken ; 26 } 27void sendToken () { 28if (haveToken && !wantCS ) { 29int next = (myId +1 ) %N ; 30Util .println ("Process " +myId +"has sent the token" ); 31sendMsg (next ,"token" ); 32haveToken =false ; 33 } 34 } 35public synchronized void handleMsg (Msg m ,int src ,String tag ) { 36if (tag .equals ("token" )) { 37haveToken =true ; 38if (!wantCS ) { 39Util .mySleep (1000 ); 40sendToken (); 41 } 42notifyAll (); 43 } 44 } 45}