traffic_synch.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include <types.h>
  2. #include <lib.h>
  3. #include <synchprobs.h>
  4. #include <synch.h>
  5. #include <thread.h>
  6. #include <opt-A1.h>
  7. /*
  8. * This simple default synchronization mechanism allows only vehicle at a time
  9. * into the intersection. The intersectionSem is used as a a lock.
  10. * We use a semaphore rather than a lock so that this code will work even
  11. * before locks are implemented.
  12. */
  13. /*
  14. * Replace this default synchronization mechanism with your own (better) mechanism
  15. * needed for your solution. Your mechanism may use any of the available synchronzation
  16. * primitives, e.g., semaphores, locks, condition variables. You are also free to
  17. * declare other global variables if your solution requires them.
  18. */
  19. /*
  20. * replace this with declarations of any synchronization and other variables you need here
  21. */
  22. static struct lock * globlock;
  23. typedef struct cv cv;
  24. // This section contains global vars and useful functions to work with them. As well as the structs used for them
  25. typedef struct car
  26. {
  27. Direction origin;
  28. Direction dest;
  29. struct car * next;
  30. struct cv * cv;
  31. } car;
  32. typedef struct list
  33. {
  34. car * front;
  35. car * back;
  36. } list;
  37. list * active = NULL;
  38. // car initializer/allocator
  39. static car * newcar(Direction origin, Direction dest)
  40. {
  41. car * temp = kmalloc(sizeof(car));
  42. if(!(temp))
  43. {
  44. panic("Failed to create a car.\n");
  45. }
  46. temp->origin = origin;
  47. temp->dest = dest;
  48. temp->next = NULL;
  49. temp->cv = NULL;
  50. return temp;
  51. }
  52. // list initializer/allocator
  53. static list * newlist()
  54. {
  55. list * temp = kmalloc(sizeof(list));
  56. if(!(temp))
  57. {
  58. panic("Could not allocate list.\n");
  59. }
  60. temp->front = NULL;
  61. temp->back = NULL;
  62. return temp;
  63. }
  64. // push a car to the end of the active list
  65. static void push(car * newcar)
  66. {
  67. if (!(active->front))
  68. {
  69. active->front = newcar;
  70. active->back = newcar;
  71. return;
  72. }
  73. active->back->next = newcar;
  74. active->back = newcar;
  75. }
  76. // called when a car clears the intersection
  77. static void clearint(car * done)
  78. {
  79. car * temp = active->front;
  80. car * temp2 = NULL;
  81. while(temp != done)
  82. {
  83. temp2 = temp;
  84. temp = temp->next;
  85. }
  86. // first element of the list is being removed
  87. if (!(temp2))
  88. {
  89. // if this is the only element
  90. if (temp == active->back)
  91. {
  92. active->back = NULL;
  93. }
  94. active->front = active->front->next;
  95. }
  96. else
  97. {
  98. // we are removing the middle or end, so we can set the previous to point to the element after the removed
  99. temp2->next = temp->next;
  100. }
  101. if (temp->cv) // if this car was blocking something
  102. {
  103. //cv_broadcast(temp->cv, globlock); // wake all/inform them you're all good
  104. //cv_destroy(temp->cv);
  105. while (!(wchan_isempty(temp->cv->wc)))
  106. {
  107. cv_signal(temp->cv, globlock);
  108. }
  109. cv_destroy(temp->cv);
  110. }
  111. kfree(temp);
  112. }
  113. // cleans up a list
  114. static void dellist(list * dead)
  115. {
  116. car * temp = dead->front;
  117. while (temp)
  118. {
  119. car * temp2 = temp->next;
  120. if (temp->cv) cv_destroy(temp->cv);
  121. kfree(temp);
  122. temp = temp2;
  123. }
  124. kfree(dead);
  125. }
  126. // returns true if a car is turning right
  127. static bool rightturn(car * car)
  128. {
  129. int temp = car->origin - car->dest;
  130. return (temp == 1 || temp == -3);
  131. }
  132. /*
  133. * The simulation driver will call this function once before starting
  134. * the simulation
  135. *
  136. * You can use it to initialize synchronization and other variables.
  137. *
  138. */
  139. void intersection_sync_init()
  140. {
  141. globlock = lock_create("lightlock");
  142. if (!(globlock))
  143. {
  144. panic("Failed to create lock!\n");
  145. }
  146. active = newlist();
  147. }
  148. /*
  149. * The simulation driver will call this function once after
  150. * the simulation has finished
  151. *
  152. * You can use it to clean up any synchronization and other variables.
  153. *
  154. */
  155. void intersection_sync_cleanup()
  156. {
  157. KASSERT(active);
  158. dellist(active);
  159. lock_destroy(globlock);
  160. }
  161. /*
  162. * The simulation driver will call this function each time a vehicle
  163. * tries to enter the intersection, before it enters.
  164. * This function should cause the calling simulation thread
  165. * to block until it is OK for the vehicle to enter the intersection.
  166. *
  167. * parameters:
  168. * * origin: the Direction from which the vehicle is arriving
  169. * * destination: the Direction in which the vehicle is trying to go
  170. *
  171. * return value: none
  172. */
  173. void intersection_before_entry(Direction origin, Direction destination)
  174. {
  175. lock_acquire(globlock);
  176. car * new = newcar(origin, destination);
  177. RESTART:
  178. // Nothing in intersection, so proceed
  179. if (!(active->front))
  180. {
  181. push(new);
  182. lock_release(globlock);
  183. return;
  184. }
  185. else // things are in the intersection
  186. {
  187. car * temp = active->front;
  188. while (temp)
  189. {
  190. kprintf("New o: %d, Comp o: %d, New d: %d, Comp d: %d\n", new->origin, temp->origin, new->dest, temp->dest);
  191. if (temp->origin == new->origin || (temp->origin == new->dest && temp->dest == new->origin) || (temp->dest != new->dest && (rightturn(new) || rightturn(temp))))
  192. {
  193. kprintf("Everything is fine, continue\n");
  194. temp = temp->next;
  195. continue;
  196. }
  197. else
  198. {
  199. // create cv for temp if it doesn't have one yet
  200. if(!(temp->cv))
  201. {
  202. temp->cv = cv_create("carcv");
  203. }
  204. kprintf("actually sleeping now\n");
  205. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  206. goto RESTART; // now we have to make sure there's nothing else screwing us over
  207. }
  208. }
  209. push(new);
  210. lock_release(globlock);
  211. }
  212. }
  213. /*
  214. * The simulation driver will call this function each time a vehicle
  215. * leaves the intersection.
  216. *
  217. * parameters:
  218. * * origin: the Direction from which the vehicle arrived
  219. * * destination: the Direction in which the vehicle is going
  220. *
  221. * return value: none
  222. */
  223. void intersection_after_exit(Direction origin, Direction destination)
  224. {
  225. lock_acquire(globlock);
  226. car * temp = active->front;
  227. while (temp)
  228. {
  229. if (temp->origin == origin && temp->dest == destination)
  230. {
  231. clearint(temp);
  232. break;
  233. }
  234. else
  235. {
  236. temp = temp->next;
  237. }
  238. }
  239. lock_release(globlock);
  240. }