traffic_synch.c 4.1 KB

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