traffic_synch.c 3.7 KB

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