chan MoneyChan int; chan AckChan bool; class Branch { static int DEPOSIT = 2; static int WITHDRAWL = 3; static int DEPOSIT_STATUS = 4; static int WITHDRAWL_STATUS = 5; static bool SEND = true; static bool RECV = false; IBank bank; int account; int funds; void Deposit (int amount) { funds = funds + amount; account = account + amount; } bool Withdraw (int amount) { bool ok; if (amount > account) return false; ok = amount > funds; if ( ! ok) { send(bank.withdrawl, amount); select { receive(bank.withdrawlStatus, ok) -> if (ok) funds = funds + amount; } } if (ok) funds = funds - amount; return ok; } void Run () { int amt; int x; while (true) { select end visible { event(DEPOSIT,RECV) -> { Deposit(amt); event(DEPOSIT_STATUS, SEND); } event(WITHDRAWL,RECV) -> { bool ok; ok = Withdraw(amt); event(WITHDRAWL_STATUS, SEND); } } } } }; class IBank { MoneyChan withdrawl; MoneyChan deposit; AckChan withdrawlStatus; AckChan depositStatus; void Run () { int x; bool b; while (true) { select end { receive(withdrawl,x) -> { b = true; //choose(bool); send(withdrawlStatus, b); } receive(deposit,x) -> { b = true; //choose(bool); send(depositStatus, b); } } } } }; class System { static activate void Init () { Branch br = new Branch; IBank b = new IBank; br.bank = b; b.withdrawl = new MoneyChan; b.deposit = new MoneyChan; b.withdrawlStatus = new AckChan; b.depositStatus = new AckChan; async br.Run(); async b.Run(); } };