traffic_synch.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. kprintf("test success, %d", test[0]);
  98. globlock = lock_create("lightlock");
  99. if (!(globlock))
  100. {
  101. panic("Failed to create lock!\n");
  102. }
  103. // initilaize array of active cars
  104. activeinit();
  105. //kprintf("finished init steps\n");
  106. }
  107. /*
  108. * The simulation driver will call this function once after
  109. * the simulation has finished
  110. *
  111. * You can use it to clean up any synchronization and other variables.
  112. *
  113. */
  114. void intersection_sync_cleanup()
  115. {
  116. KASSERT(active);
  117. cleanup();
  118. lock_destroy(globlock);
  119. }
  120. /*
  121. * The simulation driver will call this function each time a vehicle
  122. * tries to enter the intersection, before it enters.
  123. * This function should cause the calling simulation thread
  124. * to block until it is OK for the vehicle to enter the intersection.
  125. *
  126. * parameters:
  127. * * origin: the Direction from which the vehicle is arriving
  128. * * destination: the Direction in which the vehicle is trying to go
  129. *
  130. * return value: none
  131. */
  132. void intersection_before_entry(Direction origin, Direction destination)
  133. {
  134. lock_acquire(globlock);
  135. car * new = newcar(origin, destination);
  136. RESTART:
  137. //kprintf("starting the for loop for entering\n");
  138. for (int i = 0; i < 12; ++i)
  139. {
  140. car * temp = *(active + i);
  141. if (temp)
  142. {
  143. //kprintf("temp at %d was not null\n", i);
  144. 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))))
  145. {
  146. continue;
  147. }
  148. else
  149. {
  150. // create cv for temp if it doesn't have one yet
  151. if(!(temp->cv))
  152. {
  153. temp->cv = cv_create("carcv");
  154. }
  155. //kprintf("put something to sleep\n");
  156. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  157. goto RESTART; // now we have to make sure there's nothing else screwing us over
  158. }
  159. }
  160. else
  161. {
  162. //kprintf("skipped %d because it was null\n", i);
  163. continue;
  164. }
  165. }
  166. //kprintf("made it out of the for loop\n");
  167. push(new);
  168. //kprintf("added a car to the int\n");
  169. lock_release(globlock);
  170. }
  171. /*
  172. * The simulation driver will call this function each time a vehicle
  173. * leaves the intersection.
  174. *
  175. * parameters:
  176. * * origin: the Direction from which the vehicle arrived
  177. * * destination: the Direction in which the vehicle is going
  178. *
  179. * return value: none
  180. */
  181. void intersection_after_exit(Direction origin, Direction destination)
  182. {
  183. lock_acquire(globlock);
  184. int position = *(dirs + origin) + *(*(compass + origin) + destination);
  185. clearint(*(active + position));
  186. //kprintf("released a car from the int\n");
  187. lock_release(globlock);
  188. }