traffic_synch.c 3.7 KB

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