traffic_synch.c 6.4 KB

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