traffic_synch.c 6.3 KB

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