traffic_synch.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = NULL;
  17. // all of our offset arrays
  18. int * dirs;
  19. int ** compass;
  20. int * northa;
  21. int * easta;
  22. int * southa;
  23. int * 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. // initialize the array active
  38. static void activeinit(void)
  39. {
  40. active = kmalloc(12 * sizeof(car *));
  41. if (!(active))
  42. {
  43. panic("Failed to create an array");
  44. }
  45. for (int i = 0; i < 12; ++i)
  46. {
  47. *(active + i) = NULL;
  48. }
  49. }
  50. // push a car to the active array
  51. static void push(car * newcar)
  52. {
  53. // dirs + origin is our offset for where to start in the array
  54. // compass + origin is the array we use to determine what to add to the first number
  55. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  56. int total = *(dirs + newcar->origin) + *(*(compass + newcar->origin) + newcar->dest);
  57. kprintf("halfway through push\n");
  58. // with our offset, set the pointer here to newcar
  59. *(active + total) = newcar;
  60. }
  61. // called when a car clears the intersection
  62. static void clearint(car * done)
  63. {
  64. // dirs + origin is our offset for where to start in the array
  65. // compass + origin is the array we use to determine what to add to the first number
  66. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  67. // set the array of active cars to null at this value
  68. kprintf("first third: %d", *(dirs + done->origin));
  69. kprintf("second third: %p", *(compass + done->origin));
  70. kprintf("third third: %d", *(*(compass + done->origin) + done->dest));
  71. int total = *(dirs + done->origin) + *(*(compass + done->origin) + done->dest);
  72. *(active + total) = NULL;
  73. if (done->cv) // if this car was blocking something
  74. {
  75. cv_broadcast(done->cv, globlock); // wake all/inform them you're all good
  76. cv_destroy(done->cv);
  77. }
  78. kfree(done);
  79. }
  80. // delete active array, offset calculation arrays
  81. static void cleanup()
  82. {
  83. kfree(active);
  84. kfree(northa);
  85. kfree(easta);
  86. kfree(southa);
  87. kfree(westa);
  88. kfree(compass);
  89. kfree(dirs);
  90. }
  91. // returns true if a car is turning right
  92. static bool rightturn(car * car)
  93. {
  94. int temp = car->origin - car->dest;
  95. return (temp == 1 || temp == -3);
  96. }
  97. // inits all arrays we need, lock.
  98. void intersection_sync_init()
  99. {
  100. globlock = lock_create("lightlock");
  101. if (!(globlock))
  102. {
  103. panic("Failed to create lock!\n");
  104. }
  105. // initilaize array of active cars
  106. activeinit();
  107. // represents offests for the origin coordinate in our array
  108. dirs = kmalloc(4 * sizeof(int));
  109. *dirs = 0;
  110. *(dirs + 1) = 3;
  111. *(dirs + 2) = 6;
  112. *(dirs + 3) = 9;
  113. // holds the 4 offests for the dest coordinate in our array
  114. compass = kmalloc(4 * sizeof(int *));
  115. *compass = northa;
  116. *(compass + 1) = easta;
  117. *(compass + 2) = southa;
  118. *(compass + 3) = westa;
  119. // represents offests for the dest coordinate in our array
  120. northa = kmalloc(4 * sizeof(int));
  121. *northa = 0;
  122. *(northa + 1) = 0;
  123. *(northa + 2) = 1;
  124. *(northa + 3) = 2;
  125. // represents offests for the dest coordinate in our array
  126. easta = kmalloc(4 * sizeof(int));
  127. *easta = 2;
  128. *(easta + 1) = 0;
  129. *(easta + 2) = 0;
  130. *(easta + 3) = 1;
  131. // represents offests for the dest coordinate in our array
  132. southa = kmalloc(4 * sizeof(int));
  133. *southa = 1;
  134. *(southa + 1) = 2;
  135. *(southa + 2) = 0;
  136. *(southa + 3) = 0;
  137. // represents offests for the dest coordinate in our array
  138. westa = kmalloc(4 * sizeof(int));
  139. *westa = 0;
  140. *(westa + 1) = 1;
  141. *(westa + 2) = 2;
  142. *(westa + 3) = 0;
  143. if ((!compass) || !(dirs) || !(northa) || !(easta) || !(southa) || !(westa))
  144. {
  145. panic("Could not allocate offset arrays.\n");
  146. }
  147. kprintf("finished init steps\n");
  148. }
  149. /*
  150. * The simulation driver will call this function once after
  151. * the simulation has finished
  152. *
  153. * You can use it to clean up any synchronization and other variables.
  154. *
  155. */
  156. void intersection_sync_cleanup()
  157. {
  158. KASSERT(active);
  159. cleanup();
  160. lock_destroy(globlock);
  161. }
  162. /*
  163. * The simulation driver will call this function each time a vehicle
  164. * tries to enter the intersection, before it enters.
  165. * This function should cause the calling simulation thread
  166. * to block until it is OK for the vehicle to enter the intersection.
  167. *
  168. * parameters:
  169. * * origin: the Direction from which the vehicle is arriving
  170. * * destination: the Direction in which the vehicle is trying to go
  171. *
  172. * return value: none
  173. */
  174. void intersection_before_entry(Direction origin, Direction destination)
  175. {
  176. lock_acquire(globlock);
  177. car * new = newcar(origin, destination);
  178. RESTART:
  179. kprintf("starting the for loop for entering\n");
  180. for (int i = 0; i < 12; ++i)
  181. {
  182. car * temp = *(active + i);
  183. if (temp)
  184. {
  185. kprintf("temp at %d was not null\n", i);
  186. 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))))
  187. {
  188. continue;
  189. }
  190. else
  191. {
  192. // create cv for temp if it doesn't have one yet
  193. if(!(temp->cv))
  194. {
  195. temp->cv = cv_create("carcv");
  196. }
  197. kprintf("put something to sleep\n");
  198. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  199. goto RESTART; // now we have to make sure there's nothing else screwing us over
  200. }
  201. }
  202. else
  203. {
  204. kprintf("skipped %d because it was null\n", i);
  205. continue;
  206. }
  207. }
  208. kprintf("made it out of the for loop\n");
  209. push(new);
  210. kprintf("added a car to the int\n");
  211. lock_release(globlock);
  212. }
  213. /*
  214. * The simulation driver will call this function each time a vehicle
  215. * leaves the intersection.
  216. *
  217. * parameters:
  218. * * origin: the Direction from which the vehicle arrived
  219. * * destination: the Direction in which the vehicle is going
  220. *
  221. * return value: none
  222. */
  223. void intersection_after_exit(Direction origin, Direction destination)
  224. {
  225. lock_acquire(globlock);
  226. int position = *(dirs + origin) + *(*(compass + origin) + destination);
  227. clearint(*(active + position));
  228. kprintf("released a car from the int\n");
  229. lock_release(globlock);
  230. }