Guest User

Untitled

a guest
Mar 14th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define D_NORTH 0
  2. #define D_SOUTH 1
  3.  
  4. #define N 2
  5. #define getDirection(no) ((no < N/2) -> D_NORTH : D_SOUTH)
  6. #define P(s) atomic { s > 0 -> s = s - 1 }
  7. #define V(s) atomic { s = s + 1 }
  8.  
  9. byte mutex = 1;
  10. byte queue = 0;
  11. byte inCrit = 0;
  12. byte direction = 0;
  13. byte waiting = 0, inAlley = 0;
  14.  
  15. byte carsInDirection[2];
  16. pid cars[N];
  17.  
  18. proctype Car(int no)
  19. {
  20.     byte temp;
  21.  
  22.     do
  23.      ::
  24.         skip;
  25.  
  26. enter:
  27.         P(mutex);
  28.         inCrit++;
  29.        
  30.         if
  31.          :: (0 == inAlley) -> direction = getDirection(no);
  32.          :: else -> skip;
  33.         fi;
  34.  
  35.         if
  36.          :: (getDirection(no) == direction) ->
  37.             temp = inAlley;
  38.             inAlley = temp + 1;
  39.  
  40.             carsInDirection[getDirection(no)]++;
  41.             inCrit--;
  42.  
  43.             V(mutex);
  44.          :: else ->
  45.             temp = waiting;
  46.             waiting = temp + 1;
  47.            
  48.             inCrit--;
  49.             V(mutex);
  50.             P(queue);
  51.            
  52.             direction = getDirection(no);
  53.            
  54.             temp = inAlley;
  55.             inAlley = temp + 1;
  56.  
  57.             temp = waiting;
  58.             waiting = temp - 1;
  59.  
  60.             carsInDirection[getDirection(no)]++;
  61.            
  62.             if
  63.              :: (0 < waiting) -> V(queue)
  64.              :: else -> inCrit--; V(mutex);
  65.             fi;
  66.         fi;
  67.  
  68. in_alley:
  69.         skip;
  70.  
  71. leave:
  72.         P(mutex);
  73.         inCrit++;
  74.         carsInDirection[getDirection(no)]--;
  75.  
  76.         temp = inAlley;
  77.         inAlley = temp - 1;
  78.  
  79.         if
  80.          :: (0 == inAlley && 0 < waiting) -> V(queue);
  81.          :: else -> inCrit--; V(mutex);
  82.         fi;
  83.     od;
  84. }
  85.  
  86. #define CHECK(P) atomic { !(P) -> assert(P) }
  87.  
  88. active proctype Monitor()
  89. {
  90.     // Safety invariants
  91.  
  92.     if
  93.      :: CHECK(inAlley <= N/2 && inAlley >= 0)
  94.      :: CHECK(inCrit == 0 || inCrit == 1)
  95.      :: CHECK(!(carsInDirection[D_SOUTH] && carsInDirection[D_NORTH]))
  96.     fi
  97. }
  98.  
  99.  
  100. init {
  101.     atomic {
  102.         int i = 0;
  103.  
  104.         for (i in cars) {
  105.             cars[i] = run Car(i);
  106.         }
  107.     }
  108. }
Add Comment
Please, Sign In to add comment