traffic_synch.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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[4] = {0, 3, 6, 9};
  19. int northa[4] = {0, 0, 1, 2};
  20. int easta[4] = {2, 0, 0, 1};
  21. int southa[4] = {1, 2, 0, 0};
  22. int westa[4] = {0, 1, 2, 0};
  23. int * compass[4] = {northa, easta, southa, 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. //kprintf("first third: %d", *(dirs + newcar->origin));
  54. //kprintf("second third: %p", *(compass + newcar->origin));
  55. //kprintf("third third: %d", *(*(compass + newcar->origin) + newcar->dest));
  56. // dirs + origin is our offset for where to start in the array
  57. // compass + origin is the array we use to determine what to add to the first number
  58. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  59. int total = *(dirs + newcar->origin) + *(*(compass + newcar->origin) + newcar->dest);
  60. //kprintf("halfway through push\n");
  61. // with our offset, set the pointer here to newcar
  62. *(active + total) = newcar;
  63. }
  64. // called when a car clears the intersection
  65. static void clearint(car * done)
  66. {
  67. // dirs + origin is our offset for where to start in the array
  68. // compass + origin is the array we use to determine what to add to the first number
  69. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  70. // set the array of active cars to null at this value
  71. //kprintf("first third: %d", *(dirs + done->origin));
  72. //kprintf("second third: %p", *(compass + done->origin));
  73. //kprintf("third third: %d", *(*(compass + done->origin) + done->dest));
  74. int total = *(dirs + done->origin) + *(*(compass + done->origin) + done->dest);
  75. *(active + total) = NULL;
  76. if (done->cv) // if this car was blocking something
  77. {
  78. cv_broadcast(done->cv, globlock); // wake all/inform them you're all good
  79. cv_destroy(done->cv);
  80. }
  81. kfree(done);
  82. }
  83. // delete active array, offset calculation arrays
  84. static void cleanup()
  85. {
  86. kfree(active);
  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. //kprintf("finished init steps\n");
  105. }
  106. /*
  107. * The simulation driver will call this function once after
  108. * the simulation has finished
  109. *
  110. * You can use it to clean up any synchronization and other variables.
  111. *
  112. */
  113. void intersection_sync_cleanup()
  114. {
  115. KASSERT(active);
  116. cleanup();
  117. lock_destroy(globlock);
  118. }
  119. /*
  120. * The simulation driver will call this function each time a vehicle
  121. * tries to enter the intersection, before it enters.
  122. * This function should cause the calling simulation thread
  123. * to block until it is OK for the vehicle to enter the intersection.
  124. *
  125. * parameters:
  126. * * origin: the Direction from which the vehicle is arriving
  127. * * destination: the Direction in which the vehicle is trying to go
  128. *
  129. * return value: none
  130. */
  131. void intersection_before_entry(Direction origin, Direction destination)
  132. {
  133. lock_acquire(globlock);
  134. car * new = newcar(origin, destination);
  135. RESTART:
  136. //kprintf("starting the for loop for entering\n");
  137. for (int i = 0; i < 12; ++i)
  138. {
  139. car * temp = *(active + i);
  140. if (temp)
  141. {
  142. //kprintf("temp at %d was not null\n", i);
  143. 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))))
  144. {
  145. continue;
  146. }
  147. else
  148. {
  149. // create cv for temp if it doesn't have one yet
  150. if(!(temp->cv))
  151. {
  152. temp->cv = cv_create("carcv");
  153. }
  154. //kprintf("put something to sleep\n");
  155. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  156. goto RESTART; // now we have to make sure there's nothing else screwing us over
  157. }
  158. }
  159. else
  160. {
  161. //kprintf("skipped %d because it was null\n", i);
  162. continue;
  163. }
  164. }
  165. //kprintf("made it out of the for loop\n");
  166. push(new);
  167. //kprintf("added a car to the int\n");
  168. lock_release(globlock);
  169. }
  170. /*
  171. * The simulation driver will call this function each time a vehicle
  172. * leaves the intersection.
  173. *
  174. * parameters:
  175. * * origin: the Direction from which the vehicle arrived
  176. * * destination: the Direction in which the vehicle is going
  177. *
  178. * return value: none
  179. */
  180. void intersection_after_exit(Direction origin, Direction destination)
  181. {
  182. lock_acquire(globlock);
  183. int position = *(dirs + origin) + *(*(compass + origin) + destination);
  184. clearint(*(active + position));
  185. //kprintf("released a car from the int\n");
  186. lock_release(globlock);
  187. }