traffic_synch.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #include <types.h>
  2. #include <lib.h>
  3. #include <synchprobs.h>
  4. #include <synch.h>
  5. #include <opt-A1.h>
  6. /*
  7. * This simple default synchronization mechanism allows only vehicle at a time
  8. * into the intersection. The intersectionSem is used as a a lock.
  9. * We use a semaphore rather than a lock so that this code will work even
  10. * before locks are implemented.
  11. */
  12. /*
  13. * Replace this default synchronization mechanism with your own (better) mechanism
  14. * needed for your solution. Your mechanism may use any of the available synchronzation
  15. * primitives, e.g., semaphores, locks, condition variables. You are also free to
  16. * declare other global variables if your solution requires them.
  17. */
  18. /*
  19. * replace this with declarations of any synchronization and other variables you need here
  20. */
  21. static struct lock * globlock;
  22. typedef struct cv cv;
  23. // This section contains global vars and useful functions to work with them. As well as the structs used for them
  24. typedef struct car
  25. {
  26. Direction origin;
  27. Direction dest;
  28. bool old;
  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.");
  45. }
  46. temp->origin = origin;
  47. temp->dest = dest;
  48. temp->old = 0;
  49. temp->next = NULL;
  50. temp->cv = NULL;
  51. return temp;
  52. }
  53. // list initializer/allocator
  54. static list * newlist()
  55. {
  56. list * temp = kmalloc(sizeof(list));
  57. if(!(temp))
  58. {
  59. panic("Could not allocate list.");
  60. }
  61. temp->front = NULL;
  62. temp->back = NULL;
  63. return temp;
  64. }
  65. // push a car to the end of the active list
  66. static void push(car * newcar)
  67. {
  68. if (!(active->front))
  69. {
  70. active->front = newcar;
  71. active->back = newcar;
  72. return;
  73. }
  74. active->back->next = newcar;
  75. active->back = newcar;
  76. }
  77. // called when a car clears the intersection
  78. static void clearint(car * done)
  79. {
  80. kprintf("vehicle cleared");
  81. car * temp = active->front;
  82. car * temp2 = NULL;
  83. while(temp != done)
  84. {
  85. temp2 = temp;
  86. temp = temp->next;
  87. }
  88. // first element of the list is being removed
  89. if (!(temp2))
  90. {
  91. // if this is the only element
  92. if (temp == active->back)
  93. {
  94. active->back = NULL;
  95. }
  96. active->front = active->front->next;
  97. goto SKIP1;
  98. }
  99. temp2->next = temp->next;
  100. SKIP1:
  101. if (temp->cv) // if this car was blocking something
  102. {
  103. lock_acquire(globlock);
  104. cv_broadcast(temp->cv, globlock); // wake all/inform them you're all good
  105. lock_release(globlock);
  106. cv_destroy(temp->cv);
  107. }
  108. kfree(temp);
  109. }
  110. // cleans up a list
  111. static void dellist(list * dead)
  112. {
  113. car * temp = dead->front;
  114. while (temp)
  115. {
  116. car * temp2 = temp->next;
  117. if (temp->cv) cv_destroy(temp->cv);
  118. kfree(temp);
  119. temp = temp2;
  120. }
  121. kfree(dead);
  122. }
  123. // returns true if a car is turning right
  124. static bool rightturn(car * car)
  125. {
  126. int temp = car->origin - car->dest;
  127. return (temp == 1 || temp == -3);
  128. }
  129. /*
  130. * The simulation driver will call this function once before starting
  131. * the simulation
  132. *
  133. * You can use it to initialize synchronization and other variables.
  134. *
  135. */
  136. void intersection_sync_init()
  137. {
  138. globlock = lock_create("lightlock");
  139. if (!(globlock))
  140. {
  141. panic("Failed to create lock!");
  142. }
  143. active = newlist();
  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. dellist(active);
  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. // Nothing in intersection, so proceed
  176. if (!(active->front))
  177. {
  178. push(new);
  179. lock_release(globlock);
  180. return;
  181. }
  182. else // things are in the intersection
  183. {
  184. car * temp = active->front;
  185. while (temp)
  186. {
  187. if (temp->origin == new->origin || (temp->origin == new->dest && temp->dest == new->origin) || (temp->dest != new->dest && (rightturn(new) || rightturn(temp))))
  188. {
  189. temp = temp->next;
  190. continue;
  191. }
  192. else
  193. {
  194. new->old = 1; // make new "old", since now it has already waited once
  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. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  201. goto RESTART; // now we have to make sure there's nothing else screwing us over
  202. }
  203. }
  204. push(new);
  205. lock_release(globlock);
  206. }
  207. }
  208. /*
  209. * The simulation driver will call this function each time a vehicle
  210. * leaves the intersection.
  211. *
  212. * parameters:
  213. * * origin: the Direction from which the vehicle arrived
  214. * * destination: the Direction in which the vehicle is going
  215. *
  216. * return value: none
  217. */
  218. void intersection_after_exit(Direction origin, Direction destination)
  219. {
  220. car * temp = active->front;
  221. while (temp)
  222. {
  223. if (temp->origin == origin && temp->dest == destination)
  224. {
  225. clearint(temp);
  226. break;
  227. }
  228. else
  229. {
  230. temp = temp->next;
  231. }
  232. }
  233. }