traffic_synch.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. typedef struct array arr;
  9. // This section contains global vars and useful functions to work with them. As well as the structs used for them
  10. typedef struct car
  11. {
  12. Direction origin;
  13. Direction dest;
  14. struct cv * cv;
  15. } car;
  16. // array of all 12 possible cars in the intersection
  17. car ** active = NULL;
  18. // all of our offset arrays
  19. int * dirs;
  20. int ** compass;
  21. int * northa;
  22. int * easta;
  23. int * southa;
  24. int * westa;
  25. // car initializer/allocator
  26. static car * newcar(Direction origin, Direction dest)
  27. {
  28. car * temp = kmalloc(sizeof(car));
  29. if(!(temp))
  30. {
  31. panic("Failed to create a car.\n");
  32. }
  33. temp->origin = origin;
  34. temp->dest = dest;
  35. temp->cv = NULL;
  36. return temp;
  37. }
  38. // initialize the array active
  39. static void activeinit(void)
  40. {
  41. active = kmalloc(12 * sizeof(car *));
  42. if (!(active))
  43. {
  44. panic("Failed to create an array");
  45. }
  46. for (int i = 0; i < 12; ++i)
  47. {
  48. *(active + i) = NULL;
  49. }
  50. }
  51. // push a car to the active array
  52. static void push(car * newcar)
  53. {
  54. // dirs + origin is our offset for where to start in the array
  55. // compass + origin is the array we use to determine what to add to the first number
  56. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  57. int total = *(dirs + newcar->origin) + *(*(compass + newcar->origin) + newcar->dest);
  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. }
  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. for (int i = 0; i < 12; ++i)
  176. {
  177. car * temp = *(active + i);
  178. if (temp)
  179. {
  180. 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))))
  181. {
  182. continue;
  183. }
  184. else
  185. {
  186. // create cv for temp if it doesn't have one yet
  187. if(!(temp->cv))
  188. {
  189. temp->cv = cv_create("carcv");
  190. }
  191. //kprintf("actually sleeping now\n");
  192. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  193. goto RESTART; // now we have to make sure there's nothing else screwing us over
  194. }
  195. }
  196. else
  197. {
  198. continue;
  199. }
  200. }
  201. push(new);
  202. lock_release(globlock);
  203. }
  204. /*
  205. * The simulation driver will call this function each time a vehicle
  206. * leaves the intersection.
  207. *
  208. * parameters:
  209. * * origin: the Direction from which the vehicle arrived
  210. * * destination: the Direction in which the vehicle is going
  211. *
  212. * return value: none
  213. */
  214. void intersection_after_exit(Direction origin, Direction destination)
  215. {
  216. lock_acquire(globlock);
  217. int position = *(dirs + origin) + *(*(compass + origin) + destination);
  218. clearint(*(active + position));
  219. lock_release(globlock);
  220. }