/* Binary shared variable with test-and-set instruction */ Process V[2] // two states of variable Event fetchAndSetA0, fetchAndSetA1, loadA0 // access by A Event fetchAndSetB0, fetchAndSetB1, loadB0 // access by B V[0] <- fetchAndSetA0 V[1] + loadA0 V[0] + fetchAndSetB0 V[1] + loadB0 V[0] V[1] <- fetchAndSetA1 V[1] + loadA0 V[0] + fetchAndSetB1 V[1] + loadB0 V[0] // display V[0] /* Two mutual-exclusion worker processes */ Process A, AWaiting, B, BWaiting Event nonCriticalA, startCriticalA, finishCriticalA Event nonCriticalB, startCriticalB, finishCriticalB A <- nonCriticalA AWaiting AWaiting <- fetchAndSetA1 AWaiting + fetchAndSetA0 startCriticalA finishCriticalA loadA0 A B <- nonCriticalB BWaiting BWaiting <- fetchAndSetB1 BWaiting + fetchAndSetB0 startCriticalB finishCriticalB loadB0 B /* System */ Process System System <- A*B*V[0] //display ~System /* Abstracted System */ Event varEvents, nonCriticals varEvents = fetchAndSetA0 fetchAndSetA1 loadA0 fetchAndSetB0 fetchAndSetB1 loadB0 nonCriticals = nonCriticalA nonCriticalB display ~(System - varEvents - nonCriticals) /* Mutual exclusion property */ Process Mutex Mutex <- startCriticalA finishCriticalA Mutex + startCriticalB finishCriticalB Mutex print "(System - varEvents - nonCriticals) ==may Mutex ? " print (may_eq((System - varEvents - nonCriticals),Mutex)) print "\n"