traffic_synch.c 6.9 KB

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