traffic_synch.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <types.h>
  2. #include <lib.h>
  3. #include <synchprobs.h>
  4. #include <synch.h>
  5. #include <opt-A1.h>
  6. static struct lock * globlock;
  7. typedef struct cv cv;
  8. // This section contains global vars and useful functions to work with them. As well as the structs used for them
  9. typedef struct car
  10. {
  11. Direction origin;
  12. Direction dest;
  13. struct cv * cv;
  14. } car;
  15. // array of all 12 possible cars in the intersection
  16. car * active[12] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  17. // all of our offset arrays
  18. int dirs[4] = {0, 3, 6, 9};
  19. int northa[4] = {0, 0, 1, 2};
  20. int easta[4] = {2, 0, 0, 1};
  21. int southa[4] = {1, 2, 0, 0};
  22. int westa[4] = {0, 1, 2, 0};
  23. int * compass[4] = {northa, easta, southa, westa};
  24. // car initializer/allocator
  25. static car * newcar(Direction origin, Direction dest)
  26. {
  27. car * temp = kmalloc(sizeof(car));
  28. if(!(temp))
  29. {
  30. panic("Failed to create a car.\n");
  31. }
  32. temp->origin = origin;
  33. temp->dest = dest;
  34. temp->cv = NULL;
  35. return temp;
  36. }
  37. // push a car to the active array
  38. static void push(car * newcar)
  39. {
  40. // dirs + origin is our offset for where to start in the array
  41. // compass + origin is the array we use to determine what to add to the first number
  42. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  43. int total = dirs[newcar->origin] + *(compass[newcar->origin] + newcar->dest);
  44. // with our offset, set the pointer here to newcar
  45. active[total] = newcar;
  46. }
  47. // called when a car clears the intersection
  48. static void clearint(car * done)
  49. {
  50. // dirs + origin is our offset for where to start in the array
  51. // compass + origin is the array we use to determine what to add to the first number
  52. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  53. // set the array of active cars to null at this value
  54. int total = dirs[done->origin] + *(compass[done->origin] + done->dest);
  55. active[total] = NULL;
  56. if (done->cv) // if this car was blocking something
  57. {
  58. cv_broadcast(done->cv, globlock); // wake all/inform them you're all good
  59. cv_destroy(done->cv);
  60. }
  61. kfree(done);
  62. }
  63. // returns true if a car is turning right
  64. static bool rightturn(car * car)
  65. {
  66. int temp = car->origin - car->dest;
  67. return (temp == 1 || temp == -3);
  68. }
  69. // inits all arrays we need, lock.
  70. void intersection_sync_init()
  71. {
  72. globlock = lock_create("lightlock");
  73. if (!(globlock))
  74. {
  75. panic("Failed to create lock!\n");
  76. }
  77. }
  78. /*
  79. * The simulation driver will call this function once after
  80. * the simulation has finished
  81. *
  82. * You can use it to clean up any synchronization and other variables.
  83. *
  84. */
  85. void intersection_sync_cleanup()
  86. {
  87. KASSERT(active);
  88. lock_destroy(globlock);
  89. }
  90. /*
  91. * The simulation driver will call this function each time a vehicle
  92. * tries to enter the intersection, before it enters.
  93. * This function should cause the calling simulation thread
  94. * to block until it is OK for the vehicle to enter the intersection.
  95. *
  96. * parameters:
  97. * * origin: the Direction from which the vehicle is arriving
  98. * * destination: the Direction in which the vehicle is trying to go
  99. *
  100. * return value: none
  101. */
  102. void intersection_before_entry(Direction origin, Direction destination)
  103. {
  104. lock_acquire(globlock);
  105. car * new = newcar(origin, destination);
  106. RESTART:
  107. for (int i = 0; i < 12; ++i)
  108. {
  109. car * temp = active[i];
  110. if (temp)
  111. {
  112. if ((temp->origin == new->origin && temp->dest != new->dest) || (temp->origin == new->dest && temp->dest == new->origin) || (temp->dest != new->dest && (rightturn(new) || rightturn(temp))))
  113. {
  114. continue;
  115. }
  116. else
  117. {
  118. // create cv for temp if it doesn't have one yet
  119. if(!(temp->cv))
  120. {
  121. temp->cv = cv_create("carcv");
  122. }
  123. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  124. goto RESTART; // now we have to make sure there's nothing else screwing us over
  125. }
  126. }
  127. else
  128. {
  129. continue;
  130. }
  131. }
  132. push(new);
  133. lock_release(globlock);
  134. }
  135. /*
  136. * The simulation driver will call this function each time a vehicle
  137. * leaves the intersection.
  138. *
  139. * parameters:
  140. * * origin: the Direction from which the vehicle arrived
  141. * * destination: the Direction in which the vehicle is going
  142. *
  143. * return value: none
  144. */
  145. void intersection_after_exit(Direction origin, Direction destination)
  146. {
  147. lock_acquire(globlock);
  148. int position = dirs[origin] + *(compass[origin] + destination);
  149. clearint(active[position]);
  150. lock_release(globlock);
  151. }