traffic_synch.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <types.h>
  2. #include <lib.h>
  3. #include <synchprobs.h>
  4. #include <synch.h>
  5. #include <opt-A1.h>
  6. /*
  7. * This simple default synchronization mechanism allows only vehicle at a time
  8. * into the intersection. The intersectionSem is used as a a lock.
  9. * We use a semaphore rather than a lock so that this code will work even
  10. * before locks are implemented.
  11. */
  12. /*
  13. * Replace this default synchronization mechanism with your own (better) mechanism
  14. * needed for your solution. Your mechanism may use any of the available synchronzation
  15. * primitives, e.g., semaphores, locks, condition variables. You are also free to
  16. * declare other global variables if your solution requires them.
  17. */
  18. /*
  19. * replace this with declarations of any synchronization and other variables you need here
  20. */
  21. static struct semaphore * intersectionSem;
  22. /*
  23. * The simulation driver will call this function once before starting
  24. * the simulation
  25. *
  26. * You can use it to initialize synchronization and other variables.
  27. *
  28. */
  29. void
  30. intersection_sync_init(void)
  31. {
  32. /* replace this default implementation with your own implementation */
  33. intersectionSem = sem_create("intersectionSem",1);
  34. if (intersectionSem == NULL)
  35. {
  36. panic("could not create intersection semaphore");
  37. }
  38. return;
  39. }
  40. /*
  41. * The simulation driver will call this function once after
  42. * the simulation has finished
  43. *
  44. * You can use it to clean up any synchronization and other variables.
  45. *
  46. */
  47. void
  48. intersection_sync_cleanup(void)
  49. {
  50. /* replace this default implementation with your own implementation */
  51. KASSERT(intersectionSem);
  52. sem_destroy(intersectionSem);
  53. }
  54. /*
  55. * The simulation driver will call this function each time a vehicle
  56. * tries to enter the intersection, before it enters.
  57. * This function should cause the calling simulation thread
  58. * to block until it is OK for the vehicle to enter the intersection.
  59. *
  60. * parameters:
  61. * * origin: the Direction from which the vehicle is arriving
  62. * * destination: the Direction in which the vehicle is trying to go
  63. *
  64. * return value: none
  65. */
  66. void
  67. intersection_before_entry(Direction origin, Direction destination)
  68. {
  69. /* replace this default implementation with your own implementation */
  70. (void)origin; /* avoid compiler complaint about unused parameter */
  71. (void)destination; /* avoid compiler complaint about unused parameter */
  72. KASSERT(intersectionSem);
  73. P(intersectionSem);
  74. }
  75. /*
  76. * The simulation driver will call this function each time a vehicle
  77. * leaves the intersection.
  78. *
  79. * parameters:
  80. * * origin: the Direction from which the vehicle arrived
  81. * * destination: the Direction in which the vehicle is going
  82. *
  83. * return value: none
  84. */
  85. void
  86. intersection_after_exit(Direction origin, Direction destination)
  87. {
  88. /* replace this default implementation with your own implementation */
  89. (void)origin; /* avoid compiler complaint about unused parameter */
  90. (void)destination; /* avoid compiler complaint about unused parameter */
  91. KASSERT(intersectionSem);
  92. V(intersectionSem);
  93. }