traffic_synch.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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[12] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 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. // push a car to the active array
  38. static void push(car * newcar)
  39. {
  40. //kprintf("first third: %d", *(dirs + newcar->origin));
  41. //kprintf("second third: %p", *(compass + newcar->origin));
  42. //kprintf("third third: %d", *(*(compass + newcar->origin) + newcar->dest));
  43. // dirs + origin is our offset for where to start in the array
  44. // compass + origin is the array we use to determine what to add to the first number
  45. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  46. int total = *(dirs + newcar->origin) + *(*(compass + newcar->origin) + newcar->dest);
  47. //kprintf("halfway through push\n");
  48. // with our offset, set the pointer here to newcar
  49. active[total] = newcar;
  50. }
  51. // called when a car clears the intersection
  52. static void clearint(car * done)
  53. {
  54. // dirs + origin is our offset for where to start in the array
  55. // compass + origin is the array we use to determine what to add to the first number
  56. // newcar->dest gives the direction, which we use to find the offset indicating it's additive value
  57. // set the array of active cars to null at this value
  58. //kprintf("first third: %d", *(dirs + done->origin));
  59. //kprintf("second third: %p", *(compass + done->origin));
  60. //kprintf("third third: %d", *(*(compass + done->origin) + done->dest));
  61. int total = *(dirs + done->origin) + *(*(compass + done->origin) + done->dest);
  62. active[total] = NULL;
  63. if (done->cv) // if this car was blocking something
  64. {
  65. cv_broadcast(done->cv, globlock); // wake all/inform them you're all good
  66. cv_destroy(done->cv);
  67. }
  68. kfree(done);
  69. }
  70. // returns true if a car is turning right
  71. static bool rightturn(car * car)
  72. {
  73. int temp = car->origin - car->dest;
  74. return (temp == 1 || temp == -3);
  75. }
  76. // inits all arrays we need, lock.
  77. void intersection_sync_init()
  78. {
  79. globlock = lock_create("lightlock");
  80. if (!(globlock))
  81. {
  82. panic("Failed to create lock!\n");
  83. }
  84. }
  85. /*
  86. * The simulation driver will call this function once after
  87. * the simulation has finished
  88. *
  89. * You can use it to clean up any synchronization and other variables.
  90. *
  91. */
  92. void intersection_sync_cleanup()
  93. {
  94. KASSERT(active);
  95. lock_destroy(globlock);
  96. }
  97. /*
  98. * The simulation driver will call this function each time a vehicle
  99. * tries to enter the intersection, before it enters.
  100. * This function should cause the calling simulation thread
  101. * to block until it is OK for the vehicle to enter the intersection.
  102. *
  103. * parameters:
  104. * * origin: the Direction from which the vehicle is arriving
  105. * * destination: the Direction in which the vehicle is trying to go
  106. *
  107. * return value: none
  108. */
  109. void intersection_before_entry(Direction origin, Direction destination)
  110. {
  111. lock_acquire(globlock);
  112. car * new = newcar(origin, destination);
  113. RESTART:
  114. //kprintf("starting the for loop for entering\n");
  115. for (int i = 0; i < 12; ++i)
  116. {
  117. car * temp = active[i];
  118. if (temp)
  119. {
  120. //kprintf("temp at %d was not null\n", i);
  121. 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))))
  122. {
  123. continue;
  124. }
  125. else
  126. {
  127. // create cv for temp if it doesn't have one yet
  128. if(!(temp->cv))
  129. {
  130. temp->cv = cv_create("carcv");
  131. }
  132. //kprintf("put something to sleep\n");
  133. cv_wait(temp->cv, globlock); // sleep and reacquire lock once woken
  134. goto RESTART; // now we have to make sure there's nothing else screwing us over
  135. }
  136. }
  137. else
  138. {
  139. //kprintf("skipped %d because it was null\n", i);
  140. continue;
  141. }
  142. }
  143. //kprintf("made it out of the for loop\n");
  144. push(new);
  145. //kprintf("added a car to the int\n");
  146. lock_release(globlock);
  147. }
  148. /*
  149. * The simulation driver will call this function each time a vehicle
  150. * leaves the intersection.
  151. *
  152. * parameters:
  153. * * origin: the Direction from which the vehicle arrived
  154. * * destination: the Direction in which the vehicle is going
  155. *
  156. * return value: none
  157. */
  158. void intersection_after_exit(Direction origin, Direction destination)
  159. {
  160. lock_acquire(globlock);
  161. int position = *(dirs + origin) + *(*(compass + origin) + destination);
  162. clearint(active[position]);
  163. //kprintf("released a car from the int\n");
  164. lock_release(globlock);
  165. }