traffic_synch.c 6.8 KB

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